2016-12-12 16:42:03 +00:00
|
|
|
package crypto_test
|
|
|
|
|
|
|
|
import (
|
2018-08-16 10:05:33 +00:00
|
|
|
"bytes"
|
2016-12-12 16:42:03 +00:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
|
|
|
"testing"
|
2017-02-06 12:31:36 +00:00
|
|
|
|
2018-07-13 12:36:09 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-12-12 16:42:03 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
. "v2ray.com/core/common/crypto"
|
2017-05-02 20:23:07 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
2017-10-24 14:15:35 +00:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-12-12 16:42:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthenticationReaderWriter(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2016-12-12 16:42:03 +00:00
|
|
|
|
|
|
|
key := make([]byte, 16)
|
|
|
|
rand.Read(key)
|
|
|
|
block, err := aes.NewCipher(key)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2016-12-12 16:42:03 +00:00
|
|
|
|
|
|
|
aead, err := cipher.NewGCM(block)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2016-12-12 16:42:03 +00:00
|
|
|
|
2018-03-16 09:22:22 +00:00
|
|
|
const payloadSize = 1024 * 80
|
|
|
|
rawPayload := make([]byte, payloadSize)
|
2017-04-23 11:30:08 +00:00
|
|
|
rand.Read(rawPayload)
|
2016-12-12 16:42:03 +00:00
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
var payload buf.MultiBuffer
|
2018-04-19 20:56:55 +00:00
|
|
|
payload.Write(rawPayload)
|
2018-04-02 18:00:50 +00:00
|
|
|
assert(payload.Len(), Equals, int32(payloadSize))
|
2016-12-12 20:49:04 +00:00
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
cache := bytes.NewBuffer(nil)
|
2016-12-12 20:49:04 +00:00
|
|
|
iv := make([]byte, 12)
|
|
|
|
rand.Read(iv)
|
|
|
|
|
|
|
|
writer := NewAuthenticationWriter(&AEADAuthenticator{
|
2018-04-14 11:10:12 +00:00
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: GenerateStaticBytes(iv),
|
|
|
|
AdditionalDataGenerator: GenerateEmptyBytes(),
|
2018-07-07 13:42:24 +00:00
|
|
|
}, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream, nil)
|
2017-02-06 13:06:41 +00:00
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
assert(writer.WriteMultiBuffer(payload), IsNil)
|
|
|
|
assert(cache.Len(), Equals, int(82658))
|
2017-11-09 21:33:15 +00:00
|
|
|
assert(writer.WriteMultiBuffer(buf.MultiBuffer{}), IsNil)
|
2017-02-06 12:31:36 +00:00
|
|
|
|
2016-12-12 20:49:04 +00:00
|
|
|
reader := NewAuthenticationReader(&AEADAuthenticator{
|
2018-04-14 11:10:12 +00:00
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: GenerateStaticBytes(iv),
|
|
|
|
AdditionalDataGenerator: GenerateEmptyBytes(),
|
2018-07-07 13:42:24 +00:00
|
|
|
}, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream, nil)
|
2016-12-12 20:49:04 +00:00
|
|
|
|
2017-11-08 23:55:28 +00:00
|
|
|
var mb buf.MultiBuffer
|
2017-04-27 11:31:09 +00:00
|
|
|
|
2018-03-16 09:22:22 +00:00
|
|
|
for mb.Len() < payloadSize {
|
2017-11-09 21:33:15 +00:00
|
|
|
mb2, err := reader.ReadMultiBuffer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2017-04-27 11:31:09 +00:00
|
|
|
|
|
|
|
mb.AppendMulti(mb2)
|
|
|
|
}
|
2017-02-06 13:06:41 +00:00
|
|
|
|
2018-04-02 18:00:50 +00:00
|
|
|
assert(mb.Len(), Equals, int32(payloadSize))
|
2018-03-16 09:22:22 +00:00
|
|
|
|
|
|
|
mbContent := make([]byte, payloadSize)
|
2017-04-23 11:30:08 +00:00
|
|
|
mb.Read(mbContent)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(mbContent, Equals, rawPayload)
|
2016-12-12 20:49:04 +00:00
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
_, err = reader.ReadMultiBuffer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, Equals, io.EOF)
|
2016-12-12 20:49:04 +00:00
|
|
|
}
|
2017-05-01 22:28:16 +00:00
|
|
|
|
|
|
|
func TestAuthenticationReaderWriterPacket(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2017-05-01 22:28:16 +00:00
|
|
|
|
|
|
|
key := make([]byte, 16)
|
2018-07-13 12:36:09 +00:00
|
|
|
common.Must2(rand.Read(key))
|
2017-05-01 22:28:16 +00:00
|
|
|
block, err := aes.NewCipher(key)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2017-05-01 22:28:16 +00:00
|
|
|
|
|
|
|
aead, err := cipher.NewGCM(block)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2017-05-01 22:28:16 +00:00
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
cache := buf.New()
|
2017-05-01 22:28:16 +00:00
|
|
|
iv := make([]byte, 12)
|
|
|
|
rand.Read(iv)
|
|
|
|
|
|
|
|
writer := NewAuthenticationWriter(&AEADAuthenticator{
|
2018-04-14 11:10:12 +00:00
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: GenerateStaticBytes(iv),
|
|
|
|
AdditionalDataGenerator: GenerateEmptyBytes(),
|
2018-07-07 13:42:24 +00:00
|
|
|
}, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket, nil)
|
2017-05-01 22:28:16 +00:00
|
|
|
|
2017-11-08 23:55:28 +00:00
|
|
|
var payload buf.MultiBuffer
|
2017-05-01 22:28:16 +00:00
|
|
|
pb1 := buf.New()
|
2018-04-19 20:56:55 +00:00
|
|
|
pb1.Write([]byte("abcd"))
|
2017-05-01 22:28:16 +00:00
|
|
|
payload.Append(pb1)
|
|
|
|
|
|
|
|
pb2 := buf.New()
|
2018-04-19 20:56:55 +00:00
|
|
|
pb2.Write([]byte("efgh"))
|
2017-05-01 22:28:16 +00:00
|
|
|
payload.Append(pb2)
|
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
assert(writer.WriteMultiBuffer(payload), IsNil)
|
2018-04-02 18:00:50 +00:00
|
|
|
assert(cache.Len(), GreaterThan, int32(0))
|
2017-11-09 21:33:15 +00:00
|
|
|
assert(writer.WriteMultiBuffer(buf.MultiBuffer{}), IsNil)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2017-05-01 22:28:16 +00:00
|
|
|
|
|
|
|
reader := NewAuthenticationReader(&AEADAuthenticator{
|
2018-04-14 11:10:12 +00:00
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: GenerateStaticBytes(iv),
|
|
|
|
AdditionalDataGenerator: GenerateEmptyBytes(),
|
2018-07-07 13:42:24 +00:00
|
|
|
}, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket, nil)
|
2017-05-01 22:28:16 +00:00
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
mb, err := reader.ReadMultiBuffer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2017-05-01 22:28:16 +00:00
|
|
|
|
|
|
|
b1 := mb.SplitFirst()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(b1.String(), Equals, "abcd")
|
2017-12-03 12:23:24 +00:00
|
|
|
|
2017-05-01 22:28:16 +00:00
|
|
|
b2 := mb.SplitFirst()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(b2.String(), Equals, "efgh")
|
2018-04-01 21:31:53 +00:00
|
|
|
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(mb.IsEmpty(), IsTrue)
|
2017-05-01 22:28:16 +00:00
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
_, err = reader.ReadMultiBuffer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, Equals, io.EOF)
|
2017-05-01 22:28:16 +00:00
|
|
|
}
|