v2ray-core/common/crypto/chacha20_test.go

77 lines
2.2 KiB
Go
Raw Normal View History

2016-02-23 16:33:54 +00:00
package crypto_test
import (
2016-02-23 16:50:07 +00:00
"crypto/rand"
2016-02-23 16:33:54 +00:00
"encoding/hex"
"testing"
2017-04-28 12:48:23 +00:00
"v2ray.com/core/common"
2016-08-20 18:55:45 +00:00
. "v2ray.com/core/common/crypto"
2017-10-24 14:15:35 +00:00
. "v2ray.com/ext/assert"
2016-02-23 16:33:54 +00:00
)
func mustDecodeHex(s string) []byte {
b, err := hex.DecodeString(s)
2017-04-28 12:48:23 +00:00
common.Must(err)
2016-02-23 16:33:54 +00:00
return b
}
func TestChaCha20Stream(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2016-02-23 16:33:54 +00:00
var cases = []struct {
key []byte
iv []byte
output []byte
}{
{
key: mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000000"),
iv: mustDecodeHex("0000000000000000"),
output: mustDecodeHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7" +
"da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586" +
"9f07e7be5551387a98ba977c732d080dcb0f29a048e3656912c6533e32ee7aed" +
"29b721769ce64e43d57133b074d839d531ed1f28510afb45ace10a1f4b794d6f"),
},
{
key: mustDecodeHex("5555555555555555555555555555555555555555555555555555555555555555"),
iv: mustDecodeHex("5555555555555555"),
output: mustDecodeHex("bea9411aa453c5434a5ae8c92862f564396855a9ea6e22d6d3b50ae1b3663311" +
"a4a3606c671d605ce16c3aece8e61ea145c59775017bee2fa6f88afc758069f7" +
"e0b8f676e644216f4d2a3422d7fa36c6c4931aca950e9da42788e6d0b6d1cd83" +
"8ef652e97b145b14871eae6c6804c7004db5ac2fce4c68c726d004b10fcaba86"),
},
{
key: mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000000"),
iv: mustDecodeHex("000000000000000000000000"),
output: mustDecodeHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"),
},
}
for _, c := range cases {
s := NewChaCha20Stream(c.key, c.iv)
input := make([]byte, len(c.output))
actualOutout := make([]byte, len(c.output))
s.XORKeyStream(actualOutout, input)
2017-10-24 14:15:35 +00:00
assert(c.output, Equals, actualOutout)
2016-02-23 16:33:54 +00:00
}
}
2016-02-23 16:50:07 +00:00
func TestChaCha20Decoding(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2016-02-23 16:50:07 +00:00
key := make([]byte, 32)
rand.Read(key)
iv := make([]byte, 8)
rand.Read(iv)
stream := NewChaCha20Stream(key, iv)
payload := make([]byte, 1024)
rand.Read(payload)
x := make([]byte, len(payload))
stream.XORKeyStream(x, payload)
stream2 := NewChaCha20Stream(key, iv)
stream2.XORKeyStream(x, x)
2017-10-24 14:15:35 +00:00
assert(x, Equals, payload)
2016-02-23 16:50:07 +00:00
}