2015-03-12 17:03:29 +08:00
|
|
|
package mse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-03-18 15:16:27 +08:00
|
|
|
"crypto/rand"
|
2017-09-12 22:58:54 +08:00
|
|
|
"crypto/rc4"
|
2015-03-12 17:03:29 +08:00
|
|
|
"io"
|
2015-03-18 15:16:27 +08:00
|
|
|
"io/ioutil"
|
2015-03-12 17:03:29 +08:00
|
|
|
"net"
|
|
|
|
"sync"
|
2015-03-26 14:18:08 +08:00
|
|
|
"testing"
|
2015-03-12 17:03:29 +08:00
|
|
|
|
2017-09-13 14:19:14 +08:00
|
|
|
_ "github.com/anacrolix/envpprof"
|
2018-02-16 07:59:56 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-16 10:13:06 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-03-12 17:03:29 +08:00
|
|
|
)
|
|
|
|
|
2017-11-04 14:07:42 +08:00
|
|
|
func sliceIter(skeys [][]byte) SecretKeyIter {
|
|
|
|
return func(callback func([]byte) bool) {
|
|
|
|
for _, sk := range skeys {
|
|
|
|
if !callback(sk) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 17:03:29 +08:00
|
|
|
func TestReadUntil(t *testing.T) {
|
|
|
|
test := func(data, until string, leftover int, expectedErr error) {
|
|
|
|
r := bytes.NewReader([]byte(data))
|
|
|
|
err := readUntil(r, []byte(until))
|
|
|
|
if err != expectedErr {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Len() != leftover {
|
|
|
|
t.Fatal(r.Len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test("feakjfeafeafegbaabc00", "abc", 2, nil)
|
|
|
|
test("feakjfeafeafegbaadc00", "abc", 0, io.EOF)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSuffixMatchLen(t *testing.T) {
|
|
|
|
test := func(a, b string, expected int) {
|
|
|
|
actual := suffixMatchLen([]byte(a), []byte(b))
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("expected %d, got %d for %q and %q", expected, actual, a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test("hello", "world", 0)
|
|
|
|
test("hello", "lo", 2)
|
|
|
|
test("hello", "llo", 3)
|
|
|
|
test("hello", "hell", 0)
|
|
|
|
test("hello", "helloooo!", 5)
|
|
|
|
test("hello", "lol!", 2)
|
|
|
|
test("hello", "mondo", 0)
|
|
|
|
test("mongo", "webscale", 0)
|
|
|
|
test("sup", "person", 1)
|
|
|
|
}
|
|
|
|
|
2018-02-16 07:36:29 +08:00
|
|
|
func handshakeTest(t testing.TB, ia []byte, aData, bData string, cryptoProvides CryptoMethod, cryptoSelect CryptoSelector) {
|
2015-03-12 17:03:29 +08:00
|
|
|
a, b := net.Pipe()
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-02-16 07:59:56 +08:00
|
|
|
a, cm, err := InitiateHandshake(a, []byte("yep"), ia, cryptoProvides)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, cryptoSelect(cryptoProvides), cm)
|
2015-03-13 11:30:48 +08:00
|
|
|
go a.Write([]byte(aData))
|
|
|
|
|
2015-03-12 17:29:48 +08:00
|
|
|
var msg [20]byte
|
|
|
|
n, _ := a.Read(msg[:])
|
2015-03-13 11:30:48 +08:00
|
|
|
if n != len(bData) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// t.Log(string(msg[:n]))
|
2015-03-12 17:03:29 +08:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-01-04 07:49:28 +08:00
|
|
|
res := ReceiveHandshakeEx(b, sliceIter([][]byte{[]byte("nope"), []byte("yep"), []byte("maybe")}), cryptoSelect)
|
|
|
|
require.NoError(t, res.error)
|
|
|
|
assert.EqualValues(t, "yep", res.SecretKey)
|
|
|
|
b := res.ReadWriter
|
|
|
|
assert.Equal(t, cryptoSelect(cryptoProvides), res.CryptoMethod)
|
2015-03-13 11:30:48 +08:00
|
|
|
go b.Write([]byte(bData))
|
2021-01-04 07:49:28 +08:00
|
|
|
// Need to be exact here, as there are several reads, and net.Pipe is most synchronous.
|
2015-03-13 11:30:48 +08:00
|
|
|
msg := make([]byte, len(ia)+len(aData))
|
2021-01-16 18:59:36 +08:00
|
|
|
n, _ := io.ReadFull(b, msg)
|
2015-03-13 11:30:48 +08:00
|
|
|
if n != len(msg) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// t.Log(string(msg[:n]))
|
2015-03-12 17:03:29 +08:00
|
|
|
}()
|
|
|
|
wg.Wait()
|
2015-03-13 11:30:48 +08:00
|
|
|
a.Close()
|
|
|
|
b.Close()
|
|
|
|
}
|
|
|
|
|
2018-02-16 07:36:29 +08:00
|
|
|
func allHandshakeTests(t testing.TB, provides CryptoMethod, selector CryptoSelector) {
|
2017-09-13 14:19:14 +08:00
|
|
|
handshakeTest(t, []byte("jump the gun, "), "hello world", "yo dawg", provides, selector)
|
|
|
|
handshakeTest(t, nil, "hello world", "yo dawg", provides, selector)
|
|
|
|
handshakeTest(t, []byte{}, "hello world", "yo dawg", provides, selector)
|
2015-03-13 11:30:48 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:19:14 +08:00
|
|
|
func TestHandshakeDefault(t *testing.T) {
|
|
|
|
allHandshakeTests(t, AllSupportedCrypto, DefaultCryptoSelector)
|
2015-03-13 11:30:48 +08:00
|
|
|
t.Logf("crypto provides encountered: %s", cryptoProvidesCount)
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:19:14 +08:00
|
|
|
func TestHandshakeSelectPlaintext(t *testing.T) {
|
2018-02-16 07:36:29 +08:00
|
|
|
allHandshakeTests(t, AllSupportedCrypto, func(CryptoMethod) CryptoMethod { return CryptoMethodPlaintext })
|
2017-09-13 14:19:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHandshakeDefault(b *testing.B) {
|
2021-09-14 11:46:50 +08:00
|
|
|
for i := 0; i < b.N; i += 1 {
|
2017-09-13 14:19:14 +08:00
|
|
|
allHandshakeTests(b, AllSupportedCrypto, DefaultCryptoSelector)
|
2015-03-13 11:30:48 +08:00
|
|
|
}
|
2015-03-12 17:03:29 +08:00
|
|
|
}
|
2015-03-18 15:16:27 +08:00
|
|
|
|
|
|
|
type trackReader struct {
|
|
|
|
r io.Reader
|
|
|
|
n int64
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (tr *trackReader) Read(b []byte) (n int, err error) {
|
|
|
|
n, err = tr.r.Read(b)
|
|
|
|
tr.n += int64(n)
|
2015-03-18 15:16:27 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReceiveRandomData(t *testing.T) {
|
|
|
|
tr := trackReader{rand.Reader, 0}
|
2018-02-16 07:59:56 +08:00
|
|
|
_, _, err := ReceiveHandshake(readWriter{&tr, ioutil.Discard}, nil, DefaultCryptoSelector)
|
2016-09-16 10:13:06 +08:00
|
|
|
// No skey matches
|
|
|
|
require.Error(t, err)
|
|
|
|
// Establishing S, and then reading the maximum padding for giving up on
|
|
|
|
// synchronizing.
|
|
|
|
require.EqualValues(t, 96+532, tr.n)
|
2015-03-18 15:16:27 +08:00
|
|
|
}
|
2017-09-12 22:58:54 +08:00
|
|
|
|
2017-09-13 14:19:14 +08:00
|
|
|
func fillRand(t testing.TB, bs ...[]byte) {
|
|
|
|
for _, b := range bs {
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAndWrite(rw io.ReadWriter, r []byte, w []byte) error {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
var wErr error
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
_, wErr = rw.Write(w)
|
|
|
|
}()
|
|
|
|
_, err := io.ReadFull(rw, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
|
2018-02-16 07:36:29 +08:00
|
|
|
func benchmarkStream(t *testing.B, crypto CryptoMethod) {
|
2017-09-13 14:19:14 +08:00
|
|
|
ia := make([]byte, 0x1000)
|
|
|
|
a := make([]byte, 1<<20)
|
|
|
|
b := make([]byte, 1<<20)
|
|
|
|
fillRand(t, ia, a, b)
|
|
|
|
t.StopTimer()
|
|
|
|
t.SetBytes(int64(len(ia) + len(a) + len(b)))
|
|
|
|
t.ResetTimer()
|
2021-09-14 11:46:50 +08:00
|
|
|
for i := 0; i < t.N; i += 1 {
|
2017-09-13 14:19:14 +08:00
|
|
|
ac, bc := net.Pipe()
|
|
|
|
ar := make([]byte, len(b))
|
|
|
|
br := make([]byte, len(ia)+len(a))
|
|
|
|
t.StartTimer()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer ac.Close()
|
|
|
|
defer wg.Done()
|
2018-02-16 07:59:56 +08:00
|
|
|
rw, _, err := InitiateHandshake(ac, []byte("cats"), ia, crypto)
|
2017-09-13 14:19:14 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, readAndWrite(rw, ar, a))
|
|
|
|
}()
|
|
|
|
func() {
|
|
|
|
defer bc.Close()
|
2018-02-16 07:59:56 +08:00
|
|
|
rw, _, err := ReceiveHandshake(bc, sliceIter([][]byte{[]byte("cats")}), func(CryptoMethod) CryptoMethod { return crypto })
|
2017-09-13 14:19:14 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, readAndWrite(rw, br, b))
|
|
|
|
}()
|
2019-10-01 16:11:44 +08:00
|
|
|
wg.Wait()
|
2017-09-13 14:19:14 +08:00
|
|
|
t.StopTimer()
|
|
|
|
if !bytes.Equal(ar, b) {
|
|
|
|
t.Fatalf("A read the wrong bytes")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(br[:len(ia)], ia) {
|
|
|
|
t.Fatalf("B read the wrong IA")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(br[len(ia):], a) {
|
|
|
|
t.Fatalf("B read the wrong A")
|
|
|
|
}
|
|
|
|
// require.Equal(t, b, ar)
|
|
|
|
// require.Equal(t, ia, br[:len(ia)])
|
|
|
|
// require.Equal(t, a, br[len(ia):])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStreamRC4(t *testing.B) {
|
2017-09-13 16:20:20 +08:00
|
|
|
benchmarkStream(t, CryptoMethodRC4)
|
2017-09-13 14:19:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStreamPlaintext(t *testing.B) {
|
2017-09-13 16:20:20 +08:00
|
|
|
benchmarkStream(t, CryptoMethodPlaintext)
|
2017-09-13 14:19:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPipeRC4(t *testing.B) {
|
2017-09-12 22:58:54 +08:00
|
|
|
key := make([]byte, 20)
|
|
|
|
n, _ := rand.Read(key)
|
|
|
|
require.Equal(t, len(key), n)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
c, err := rc4.NewCipher(key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
r := cipherReader{
|
|
|
|
c: c,
|
|
|
|
r: &buf,
|
|
|
|
}
|
|
|
|
c, err = rc4.NewCipher(key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
w := cipherWriter{
|
|
|
|
c: c,
|
|
|
|
w: &buf,
|
|
|
|
}
|
|
|
|
a := make([]byte, 0x1000)
|
|
|
|
n, _ = io.ReadFull(rand.Reader, a)
|
|
|
|
require.Equal(t, len(a), n)
|
|
|
|
b := make([]byte, len(a))
|
|
|
|
t.SetBytes(int64(len(a)))
|
|
|
|
t.ResetTimer()
|
2021-09-14 11:46:50 +08:00
|
|
|
for i := 0; i < t.N; i += 1 {
|
2017-09-12 22:58:54 +08:00
|
|
|
n, _ = w.Write(a)
|
|
|
|
if n != len(a) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
n, _ = r.Read(b)
|
|
|
|
if n != len(b) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if !bytes.Equal(a, b) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 08:33:46 +08:00
|
|
|
|
|
|
|
func BenchmarkSkeysReceive(b *testing.B) {
|
|
|
|
var skeys [][]byte
|
2021-09-14 11:46:50 +08:00
|
|
|
for i := 0; i < 100000; i += 1 {
|
2021-01-04 08:33:46 +08:00
|
|
|
skeys = append(skeys, make([]byte, 20))
|
|
|
|
}
|
|
|
|
fillRand(b, skeys...)
|
|
|
|
initSkey := skeys[len(skeys)/2]
|
|
|
|
//c := qt.New(b)
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
2021-09-14 11:46:50 +08:00
|
|
|
for i := 0; i < b.N; i += 1 {
|
2021-01-04 08:33:46 +08:00
|
|
|
initiator, receiver := net.Pipe()
|
|
|
|
go func() {
|
|
|
|
_, _, err := InitiateHandshake(initiator, initSkey, nil, AllSupportedCrypto)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
res := ReceiveHandshakeEx(receiver, sliceIter(skeys), DefaultCryptoSelector)
|
|
|
|
if res.error != nil {
|
|
|
|
panic(res.error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|