1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/crypto/chacha20_test.go

78 lines
2.3 KiB
Go
Raw Normal View History

2016-02-23 11:33:54 -05:00
package crypto_test
import (
2016-02-23 11:50:07 -05:00
"crypto/rand"
2016-02-23 11:33:54 -05:00
"encoding/hex"
"testing"
2019-01-06 18:12:04 -05:00
"github.com/google/go-cmp/cmp"
"github.com/v2fly/v2ray-core/v5/common"
. "github.com/v2fly/v2ray-core/v5/common/crypto"
2016-02-23 11:33:54 -05:00
)
func mustDecodeHex(s string) []byte {
b, err := hex.DecodeString(s)
2017-04-28 08:48:23 -04:00
common.Must(err)
2016-02-23 11:33:54 -05:00
return b
}
func TestChaCha20Stream(t *testing.T) {
2021-05-19 17:28:52 -04:00
cases := []struct {
2016-02-23 11:33:54 -05:00
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))
2021-12-23 00:14:43 -05:00
actualOutput := make([]byte, len(c.output))
s.XORKeyStream(actualOutput, input)
if r := cmp.Diff(c.output, actualOutput); r != "" {
2019-01-06 18:12:04 -05:00
t.Fatal(r)
2018-07-13 08:36:09 -04:00
}
2016-02-23 11:33:54 -05:00
}
}
2016-02-23 11:50:07 -05:00
func TestChaCha20Decoding(t *testing.T) {
key := make([]byte, 32)
2018-07-13 08:36:09 -04:00
common.Must2(rand.Read(key))
2016-02-23 11:50:07 -05:00
iv := make([]byte, 8)
2018-07-13 08:36:09 -04:00
common.Must2(rand.Read(iv))
2016-02-23 11:50:07 -05:00
stream := NewChaCha20Stream(key, iv)
payload := make([]byte, 1024)
2018-07-13 08:36:09 -04:00
common.Must2(rand.Read(payload))
2016-02-23 11:50:07 -05:00
x := make([]byte, len(payload))
stream.XORKeyStream(x, payload)
stream2 := NewChaCha20Stream(key, iv)
stream2.XORKeyStream(x, x)
2019-01-06 18:12:04 -05:00
if r := cmp.Diff(x, payload); r != "" {
t.Fatal(r)
2018-07-13 08:36:09 -04:00
}
2016-02-23 11:50:07 -05:00
}