Debian Codegraph @

camellia/vectors_test.go

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