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
|
|
|
"time"
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
payload := make([]byte, 8*1024)
|
|
|
|
rand.Read(payload)
|
|
|
|
|
|
|
|
cache := buf.NewLocal(16 * 1024)
|
|
|
|
iv := make([]byte, 12)
|
|
|
|
rand.Read(iv)
|
|
|
|
|
|
|
|
writer := NewAuthenticationWriter(&AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &StaticBytesGenerator{
|
|
|
|
Content: iv,
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
2017-02-14 08:16:43 -05:00
|
|
|
}, cache, NewShakeUint16Generator([]byte{'a'}))
|
2016-12-12 11:42:03 -05:00
|
|
|
|
|
|
|
nBytes, err := writer.Write(payload)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
|
|
|
assert.Int(cache.Len()).GreaterThan(0)
|
|
|
|
_, err = writer.Write([]byte{})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
reader := NewAuthenticationReader(&AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &StaticBytesGenerator{
|
|
|
|
Content: iv,
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
2017-02-14 08:16:43 -05:00
|
|
|
}, cache, NewShakeUint16Generator([]byte{'a'}))
|
2016-12-12 11:42:03 -05:00
|
|
|
|
|
|
|
actualPayload := make([]byte, 16*1024)
|
|
|
|
nBytes, err = reader.Read(actualPayload)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
2016-12-12 15:44:16 -05:00
|
|
|
assert.Bytes(actualPayload[:nBytes]).Equals(payload)
|
|
|
|
|
|
|
|
_, err = reader.Read(actualPayload)
|
|
|
|
assert.Error(err).Equals(io.EOF)
|
|
|
|
}
|
|
|
|
|
2016-12-12 15:49:04 -05:00
|
|
|
func TestAuthenticationReaderWriterPartial(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()
|
|
|
|
|
|
|
|
payload := make([]byte, 8*1024)
|
|
|
|
rand.Read(payload)
|
|
|
|
|
|
|
|
iv := make([]byte, 12)
|
|
|
|
rand.Read(iv)
|
|
|
|
|
2017-02-06 07:31:36 -05:00
|
|
|
cache := buf.NewLocal(16 * 1024)
|
2016-12-12 15:49:04 -05:00
|
|
|
writer := NewAuthenticationWriter(&AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &StaticBytesGenerator{
|
|
|
|
Content: iv,
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
2017-02-14 08:16:43 -05:00
|
|
|
}, cache, NewShakeUint16Generator([]byte{'a', 'b'}))
|
2016-12-12 15:49:04 -05:00
|
|
|
|
2017-02-06 08:06:41 -05:00
|
|
|
writer.Write([]byte{'a', 'b', 'c', 'd'})
|
|
|
|
|
2016-12-12 15:49:04 -05:00
|
|
|
nBytes, err := writer.Write(payload)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
|
|
|
assert.Int(cache.Len()).GreaterThan(0)
|
|
|
|
_, err = writer.Write([]byte{})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2017-02-06 07:31:36 -05:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
pw.Write(cache.BytesTo(1024))
|
|
|
|
time.Sleep(time.Second * 2)
|
2017-02-06 08:02:17 -05:00
|
|
|
pw.Write(cache.BytesRange(1024, 2048))
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
pw.Write(cache.BytesRange(2048, 3072))
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
pw.Write(cache.BytesFrom(3072))
|
2017-02-06 07:31:36 -05:00
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
pw.Close()
|
|
|
|
}()
|
|
|
|
|
2016-12-12 15:49:04 -05:00
|
|
|
reader := NewAuthenticationReader(&AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &StaticBytesGenerator{
|
|
|
|
Content: iv,
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: &NoOpBytesGenerator{},
|
2017-02-14 08:16:43 -05:00
|
|
|
}, pr, NewShakeUint16Generator([]byte{'a', 'b'}))
|
2016-12-12 15:49:04 -05:00
|
|
|
|
|
|
|
actualPayload := make([]byte, 7*1024)
|
2017-02-06 08:06:41 -05:00
|
|
|
nBytes, err = reader.Read(actualPayload)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(4)
|
|
|
|
assert.Bytes(actualPayload[:nBytes]).Equals([]byte{'a', 'b', 'c', 'd'})
|
|
|
|
|
2016-12-12 15:49:04 -05:00
|
|
|
nBytes, err = reader.Read(actualPayload)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(actualPayload))
|
|
|
|
assert.Bytes(actualPayload[:nBytes]).Equals(payload[:nBytes])
|
|
|
|
|
|
|
|
nBytes, err = reader.Read(actualPayload)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload) - len(actualPayload))
|
|
|
|
assert.Bytes(actualPayload[:nBytes]).Equals(payload[7*1024:])
|
|
|
|
|
|
|
|
_, err = reader.Read(actualPayload)
|
|
|
|
assert.Error(err).Equals(io.EOF)
|
|
|
|
}
|