1
2
3
4
5
6
7
8
9
10
11 package cipher_test
12
13 import (
14 "bytes"
15 "crypto/aes"
16 "crypto/cipher"
17 "crypto/internal/cryptotest"
18 "testing"
19 )
20
21 var cbcAESTests = []struct {
22 name string
23 key []byte
24 iv []byte
25 in []byte
26 out []byte
27 }{
28
29 {
30 "CBC-AES128",
31 commonKey128,
32 commonIV,
33 commonInput,
34 []byte{
35 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
36 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
37 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
38 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7,
39 },
40 },
41 {
42 "CBC-AES192",
43 commonKey192,
44 commonIV,
45 commonInput,
46 []byte{
47 0x4f, 0x02, 0x1d, 0xb2, 0x43, 0xbc, 0x63, 0x3d, 0x71, 0x78, 0x18, 0x3a, 0x9f, 0xa0, 0x71, 0xe8,
48 0xb4, 0xd9, 0xad, 0xa9, 0xad, 0x7d, 0xed, 0xf4, 0xe5, 0xe7, 0x38, 0x76, 0x3f, 0x69, 0x14, 0x5a,
49 0x57, 0x1b, 0x24, 0x20, 0x12, 0xfb, 0x7a, 0xe0, 0x7f, 0xa9, 0xba, 0xac, 0x3d, 0xf1, 0x02, 0xe0,
50 0x08, 0xb0, 0xe2, 0x79, 0x88, 0x59, 0x88, 0x81, 0xd9, 0x20, 0xa9, 0xe6, 0x4f, 0x56, 0x15, 0xcd,
51 },
52 },
53 {
54 "CBC-AES256",
55 commonKey256,
56 commonIV,
57 commonInput,
58 []byte{
59 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
60 0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
61 0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
62 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b,
63 },
64 },
65 }
66
67 func TestCBCEncrypterAES(t *testing.T) {
68 cryptotest.TestAllImplementations(t, "aes", testCBCEncrypterAES)
69 }
70
71 func testCBCEncrypterAES(t *testing.T) {
72 for _, test := range cbcAESTests {
73 c, err := aes.NewCipher(test.key)
74 if err != nil {
75 t.Errorf("%s: NewCipher(%d bytes) = %s", test.name, len(test.key), err)
76 continue
77 }
78
79 encrypter := cipher.NewCBCEncrypter(c, test.iv)
80
81 data := make([]byte, len(test.in))
82 copy(data, test.in)
83
84 encrypter.CryptBlocks(data, data)
85 if !bytes.Equal(test.out, data) {
86 t.Errorf("%s: CBCEncrypter\nhave %x\nwant %x", test.name, data, test.out)
87 }
88 }
89 }
90
91 func TestCBCDecrypterAES(t *testing.T) {
92 cryptotest.TestAllImplementations(t, "aes", testCBCDecrypterAES)
93 }
94
95 func testCBCDecrypterAES(t *testing.T) {
96 for _, test := range cbcAESTests {
97 c, err := aes.NewCipher(test.key)
98 if err != nil {
99 t.Errorf("%s: NewCipher(%d bytes) = %s", test.name, len(test.key), err)
100 continue
101 }
102
103 decrypter := cipher.NewCBCDecrypter(c, test.iv)
104
105 data := make([]byte, len(test.out))
106 copy(data, test.out)
107
108 decrypter.CryptBlocks(data, data)
109 if !bytes.Equal(test.in, data) {
110 t.Errorf("%s: CBCDecrypter\nhave %x\nwant %x", test.name, data, test.in)
111 }
112 }
113 }
114
View as plain text