2016-12-12 11:42:03 -05:00
|
|
|
package crypto_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
|
|
|
"testing"
|
2017-02-06 07:31:36 -05:00
|
|
|
|
2016-12-12 11:42:03 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
. "v2ray.com/core/common/crypto"
|
|
|
|
"v2ray.com/core/testing/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthenticationReaderWriter(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
key := make([]byte, 16)
|
|
|
|
rand.Read(key)
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
aead, err := cipher.NewGCM(block)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2017-04-27 07:31:09 -04:00
|
|
|
rawPayload := make([]byte, 8192*10)
|
2017-04-23 07:30:08 -04:00
|
|
|
rand.Read(rawPayload)
|
2016-12-12 11:42:03 -05:00
|
|
|
|
2017-04-27 07:31:09 -04:00
|
|
|
payload := buf.NewLocal(8192 * 10)
|
2017-04-23 07:30:08 -04:00
|
|
|
payload.Append(rawPayload)
|
2016-12-12 15:49:04 -05:00
|
|
|
|
2017-04-27 07:31:09 -04:00
|
|
|
cache := buf.NewLocal(160 * 1024)
|
2016-12-12 15:49:04 -05:00
|
|
|
iv := make([]byte, 12)
|
|
|
|
rand.Read(iv)
|
|
|
|
|
|
|
|
writer := NewAuthenticationWriter(&AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &StaticBytesGenerator{
|
|
|
|
Content: iv,
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
2017-05-01 18:28:16 -04:00
|
|
|
}, PlainChunkSizeParser{}, cache, ModeStream)
|
2017-02-06 08:06:41 -05:00
|
|
|
|
2017-04-23 07:30:08 -04:00
|
|
|
assert.Error(writer.Write(buf.NewMultiBufferValue(payload))).IsNil()
|
2017-04-27 07:31:09 -04:00
|
|
|
assert.Int(cache.Len()).Equals(83360)
|
2017-04-23 07:30:08 -04:00
|
|
|
assert.Error(writer.Write(buf.NewMultiBuffer())).IsNil()
|
2016-12-12 15:49:04 -05:00
|
|
|
assert.Error(err).IsNil()
|
2017-02-06 07:31:36 -05:00
|
|
|
|
2016-12-12 15:49:04 -05:00
|
|
|
reader := NewAuthenticationReader(&AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &StaticBytesGenerator{
|
|
|
|
Content: iv,
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
2017-05-01 18:28:16 -04:00
|
|
|
}, PlainChunkSizeParser{}, cache, ModeStream)
|
2016-12-12 15:49:04 -05:00
|
|
|
|
2017-04-27 07:31:09 -04:00
|
|
|
mb := buf.NewMultiBuffer()
|
|
|
|
|
|
|
|
for mb.Len() < len(rawPayload) {
|
|
|
|
mb2, err := reader.Read()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
mb.AppendMulti(mb2)
|
|
|
|
}
|
2017-02-06 08:06:41 -05:00
|
|
|
|
2017-04-27 07:31:09 -04:00
|
|
|
mbContent := make([]byte, 8192*10)
|
2017-04-23 07:30:08 -04:00
|
|
|
mb.Read(mbContent)
|
|
|
|
assert.Bytes(mbContent).Equals(rawPayload)
|
2016-12-12 15:49:04 -05:00
|
|
|
|
2017-04-23 07:30:08 -04:00
|
|
|
_, err = reader.Read()
|
2016-12-12 15:49:04 -05:00
|
|
|
assert.Error(err).Equals(io.EOF)
|
|
|
|
}
|
2017-05-01 18:28:16 -04:00
|
|
|
|
|
|
|
func TestAuthenticationReaderWriterPacket(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
key := make([]byte, 16)
|
|
|
|
rand.Read(key)
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
aead, err := cipher.NewGCM(block)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
cache := buf.NewLocal(1024)
|
|
|
|
iv := make([]byte, 12)
|
|
|
|
rand.Read(iv)
|
|
|
|
|
|
|
|
writer := NewAuthenticationWriter(&AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &StaticBytesGenerator{
|
|
|
|
Content: iv,
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
|
|
|
}, PlainChunkSizeParser{}, cache, ModePacket)
|
|
|
|
|
|
|
|
payload := buf.NewMultiBuffer()
|
|
|
|
pb1 := buf.New()
|
|
|
|
pb1.Append([]byte("abcd"))
|
|
|
|
payload.Append(pb1)
|
|
|
|
|
|
|
|
pb2 := buf.New()
|
|
|
|
pb2.Append([]byte("efgh"))
|
|
|
|
payload.Append(pb2)
|
|
|
|
|
|
|
|
assert.Error(writer.Write(payload)).IsNil()
|
|
|
|
assert.Int(cache.Len()).GreaterThan(0)
|
|
|
|
assert.Error(writer.Write(buf.NewMultiBuffer())).IsNil()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
reader := NewAuthenticationReader(&AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &StaticBytesGenerator{
|
|
|
|
Content: iv,
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
|
|
|
}, PlainChunkSizeParser{}, cache, ModePacket)
|
|
|
|
|
|
|
|
mb, err := reader.Read()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
b1 := mb.SplitFirst()
|
|
|
|
assert.String(b1.String()).Equals("abcd")
|
|
|
|
b2 := mb.SplitFirst()
|
|
|
|
assert.String(b2.String()).Equals("efgh")
|
|
|
|
assert.Bool(mb.IsEmpty()).IsTrue()
|
|
|
|
|
|
|
|
_, err = reader.Read()
|
|
|
|
assert.Error(err).Equals(io.EOF)
|
|
|
|
}
|