-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectors_test.go
63 lines (56 loc) · 1.68 KB
/
vectors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2016 Andreas Auernhammer. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
package camellia
import (
"bytes"
"encoding/hex"
"testing"
)
func fromHex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
// Test vectors from RFC3713 - https://www.ietf.org/rfc/rfc3713.txt
var vectors = []struct {
key, plaintext, ciphertext string
}{
{
key: "0123456789abcdeffedcba9876543210",
plaintext: "0123456789abcdeffedcba9876543210",
ciphertext: "67673138549669730857065648eabe43",
},
{
key: "0123456789abcdeffedcba98765432100011223344556677",
plaintext: "0123456789abcdeffedcba9876543210",
ciphertext: "b4993401b3e996f84ee5cee7d79b09b9",
},
{
key: "0123456789abcdeffedcba987654321000112233445566778899aabbccddeeff",
plaintext: "0123456789abcdeffedcba9876543210",
ciphertext: "9acc237dff16d76c20ef7c919e3a7509",
},
}
func TestVectors(t *testing.T) {
for i, v := range vectors {
key := fromHex(v.key)
plaintext := fromHex(v.plaintext)
ciphertext := fromHex(v.ciphertext)
buf := make([]byte, BlockSize)
c, err := NewCipher(key)
if err != nil {
t.Fatalf("Test vector %d: Failed to create Camellia instance: %s", i, err)
}
c.Encrypt(buf, plaintext)
if !bytes.Equal(ciphertext, buf) {
t.Fatalf("Test vector %d:\nEncryption failed\nFound: %s\nExpected: %s", i, hex.EncodeToString(buf), hex.EncodeToString(ciphertext))
}
c.Decrypt(buf, buf)
if !bytes.Equal(plaintext, buf) {
t.Fatalf("Test vector %d:\nDecryption failed\nFound: %s\nExpected: %s", i, hex.EncodeToString(buf), hex.EncodeToString(plaintext))
}
}
}