Source file
src/crypto/aes/aes_test.go
1
2
3
4
5 package aes
6
7 import (
8 "crypto/internal/cryptotest"
9 "fmt"
10 "testing"
11 )
12
13
14
15
16
17 type CryptTest struct {
18 key []byte
19 in []byte
20 out []byte
21 }
22
23 var encryptTests = []CryptTest{
24 {
25
26 []byte{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c},
27 []byte{0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34},
28 []byte{0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32},
29 },
30 {
31
32 []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
33 []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
34 []byte{0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a},
35 },
36 {
37
38 []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
39 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
40 },
41 []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
42 []byte{0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91},
43 },
44 {
45
46 []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
47 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
48 },
49 []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
50 []byte{0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89},
51 },
52 }
53
54
55 func TestCipherEncrypt(t *testing.T) {
56 cryptotest.TestAllImplementations(t, "aes", testCipherEncrypt)
57 }
58
59 func testCipherEncrypt(t *testing.T) {
60 for i, tt := range encryptTests {
61 c, err := NewCipher(tt.key)
62 if err != nil {
63 t.Errorf("NewCipher(%d bytes) = %s", len(tt.key), err)
64 continue
65 }
66 out := make([]byte, len(tt.in))
67 c.Encrypt(out, tt.in)
68 for j, v := range out {
69 if v != tt.out[j] {
70 t.Errorf("Cipher.Encrypt %d: out[%d] = %#x, want %#x", i, j, v, tt.out[j])
71 break
72 }
73 }
74 }
75 }
76
77
78 func TestCipherDecrypt(t *testing.T) {
79 cryptotest.TestAllImplementations(t, "aes", testCipherDecrypt)
80 }
81
82 func testCipherDecrypt(t *testing.T) {
83 for i, tt := range encryptTests {
84 c, err := NewCipher(tt.key)
85 if err != nil {
86 t.Errorf("NewCipher(%d bytes) = %s", len(tt.key), err)
87 continue
88 }
89 plain := make([]byte, len(tt.in))
90 c.Decrypt(plain, tt.out)
91 for j, v := range plain {
92 if v != tt.in[j] {
93 t.Errorf("decryptBlock %d: plain[%d] = %#x, want %#x", i, j, v, tt.in[j])
94 break
95 }
96 }
97 }
98 }
99
100
101 func TestAESBlock(t *testing.T) {
102 cryptotest.TestAllImplementations(t, "aes", testAESBlock)
103 }
104
105 func testAESBlock(t *testing.T) {
106 for _, keylen := range []int{128, 192, 256} {
107 t.Run(fmt.Sprintf("AES-%d", keylen), func(t *testing.T) {
108 cryptotest.TestBlock(t, keylen/8, NewCipher)
109 })
110 }
111 }
112
113 func BenchmarkEncrypt(b *testing.B) {
114 b.Run("AES-128", func(b *testing.B) { benchmarkEncrypt(b, encryptTests[1]) })
115 b.Run("AES-192", func(b *testing.B) { benchmarkEncrypt(b, encryptTests[2]) })
116 b.Run("AES-256", func(b *testing.B) { benchmarkEncrypt(b, encryptTests[3]) })
117 }
118
119 func benchmarkEncrypt(b *testing.B, tt CryptTest) {
120 c, err := NewCipher(tt.key)
121 if err != nil {
122 b.Fatal("NewCipher:", err)
123 }
124 out := make([]byte, len(tt.in))
125 b.SetBytes(int64(len(out)))
126 b.ResetTimer()
127 for i := 0; i < b.N; i++ {
128 c.Encrypt(out, tt.in)
129 }
130 }
131
132 func BenchmarkDecrypt(b *testing.B) {
133 b.Run("AES-128", func(b *testing.B) { benchmarkDecrypt(b, encryptTests[1]) })
134 b.Run("AES-192", func(b *testing.B) { benchmarkDecrypt(b, encryptTests[2]) })
135 b.Run("AES-256", func(b *testing.B) { benchmarkDecrypt(b, encryptTests[3]) })
136 }
137
138 func benchmarkDecrypt(b *testing.B, tt CryptTest) {
139 c, err := NewCipher(tt.key)
140 if err != nil {
141 b.Fatal("NewCipher:", err)
142 }
143 out := make([]byte, len(tt.out))
144 b.SetBytes(int64(len(out)))
145 b.ResetTimer()
146 for i := 0; i < b.N; i++ {
147 c.Decrypt(out, tt.out)
148 }
149 }
150
151 func BenchmarkCreateCipher(b *testing.B) {
152 b.Run("AES-128", func(b *testing.B) { benchmarkCreateCipher(b, encryptTests[1]) })
153 b.Run("AES-192", func(b *testing.B) { benchmarkCreateCipher(b, encryptTests[2]) })
154 b.Run("AES-256", func(b *testing.B) { benchmarkCreateCipher(b, encryptTests[3]) })
155 }
156
157 func benchmarkCreateCipher(b *testing.B, tt CryptTest) {
158 b.ReportAllocs()
159 for i := 0; i < b.N; i++ {
160 if _, err := NewCipher(tt.key); err != nil {
161 b.Fatal(err)
162 }
163 }
164 }
165
View as plain text