mirror of
https://github.com/taigrr/arc
synced 2025-01-18 04:33:13 -08:00
66 lines
2.5 KiB
Go
66 lines
2.5 KiB
Go
// Copyright (C) 2016 - Will Glozer. All rights reserved.
|
|
|
|
package xchacha20poly1305
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
// plaintext & key from RFC 7539 with the 12-byte nonce repeated twice and
|
|
// expected ciphertext derived from the SUPERCOP chacha20 reference impl.
|
|
func TestXChaCha20(t *testing.T) {
|
|
plaintext := []byte{
|
|
0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
|
|
0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
|
|
0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
|
|
0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
|
|
0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
|
|
0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
|
|
0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
|
|
0x74, 0x2e,
|
|
}
|
|
|
|
key := []byte{
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
|
|
}
|
|
|
|
nonce := []byte{
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00,
|
|
}
|
|
|
|
ciphertext := []byte{
|
|
0x15, 0x62, 0x3c, 0xe5, 0x14, 0x3a, 0xd8, 0xc8, 0x46, 0x79, 0x0b, 0xc3, 0x8c, 0x5e, 0x81, 0x50,
|
|
0x85, 0x1c, 0xd4, 0xa5, 0x30, 0xf5, 0xfa, 0xb8, 0x02, 0xe6, 0x1e, 0x87, 0x52, 0x91, 0x57, 0x9f,
|
|
0x40, 0x0b, 0x2c, 0x57, 0x79, 0x95, 0xdf, 0x0c, 0xbc, 0xd9, 0x71, 0x30, 0x30, 0xad, 0x64, 0xee,
|
|
0x93, 0x7d, 0xd2, 0xfa, 0x5c, 0x48, 0xff, 0xda, 0x7c, 0x91, 0xd1, 0x5b, 0xc3, 0x41, 0x3e, 0x33,
|
|
0x55, 0x1d, 0x10, 0x97, 0x73, 0x0e, 0x40, 0x51, 0x4a, 0x1f, 0xf9, 0x04, 0xee, 0xb0, 0xe6, 0xd2,
|
|
0x2a, 0x42, 0x79, 0xc0, 0xee, 0xe1, 0xb4, 0x92, 0x9c, 0x5a, 0xe4, 0x24, 0x21, 0xe0, 0x0f, 0x3e,
|
|
0x3b, 0xa3, 0x0c, 0x7b, 0x7a, 0xa3, 0xfa, 0x0f, 0x9d, 0x7b, 0x69, 0xc4, 0x7f, 0xdf, 0x6d, 0xe5,
|
|
0x35, 0xa3,
|
|
}
|
|
|
|
tag := []byte{
|
|
0x80, 0xdf, 0x98, 0xf1, 0xff, 0x1d, 0xc9, 0xc8, 0xdd, 0x08, 0xeb, 0xf8, 0x45, 0x1c, 0x8b, 0xd2,
|
|
}
|
|
|
|
c := XChaCha20Poly1305{}
|
|
|
|
err := c.Init(key, nonce)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
c.Encrypt(plaintext, plaintext)
|
|
|
|
if !bytes.Equal(plaintext, ciphertext) {
|
|
t.Fatal("encrypted plaintext != expected ciphertext")
|
|
}
|
|
|
|
if !bytes.Equal(c.Tag(nil), tag) {
|
|
t.Fatal("actual tag != expected tag")
|
|
}
|
|
}
|