Debian Codegraph @

crypto/aes/ct64+test.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use bytes;
5 use crypto::cipher;
6
7 @test fn test_encrypt_128() void = {
8 const key: [16]u8 = [
9 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
10 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
11 ];
12
13 const plain: [16]u8 = [
14 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
15 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
16 ];
17
18 const cipher: [16]u8 = [
19 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
20 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32,
21 ];
22
23 let result: [16]u8 = [0...];
24
25 let block = ct64();
26 ct64_init(&block, key);
27 defer cipher::finish(&block);
28 cipher::encrypt(&block, result[..], plain[..]);
29
30 assert(bytes::equal(cipher, result));
31 };
32
33 @test fn test_encrypt_128_multiple_blocks() void = {
34 const key: [16]u8 = [
35 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
36 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
37 ];
38
39 const plain: [_]u8 = [
40 0xdb, 0x3a, 0x36, 0x50, 0x85, 0x60, 0x4c, 0x65,
41 0xc0, 0x22, 0x5a, 0x0d, 0x68, 0x2f, 0x19, 0xc2,
42 0x16, 0x27, 0xdd, 0xae, 0xd7, 0x28, 0x29, 0x23,
43 0xfa, 0x97, 0x28, 0xb0, 0x9a, 0x90, 0xfe, 0xb3,
44 0xde, 0x57, 0xbc, 0x15, 0x15, 0x8d, 0xb6, 0x29,
45 0x61, 0x40, 0x76, 0x51, 0x32, 0xc2, 0x3d, 0x1e,
46 0xdf, 0x97, 0x6a, 0x5e, 0x0a, 0x6b, 0xf6, 0x40,
47 0xb4, 0x08, 0x7e, 0xde, 0x17, 0xa4, 0x58, 0x98
48 ];
49
50 const cipher: [_]u8 = [
51 0xdd, 0xf4, 0x53, 0x55, 0x18, 0x7a, 0xf0, 0x65,
52 0x59, 0xd6, 0xdc, 0x4e, 0xc2, 0x89, 0xff, 0x7b,
53 0x54, 0x41, 0x6a, 0xda, 0xa0, 0xad, 0x85, 0xbd,
54 0x75, 0x1e, 0x59, 0x15, 0x6d, 0x30, 0xa8, 0xa8,
55 0xa8, 0x54, 0x6e, 0xab, 0x9c, 0x47, 0x84, 0xd,
56 0x6e, 0xb5, 0x5e, 0xba, 0xb0, 0xd9, 0x47, 0xfa,
57 0x53, 0x6b, 0xff, 0x31, 0x47, 0x86, 0xf8, 0x4c,
58 0x6b, 0x65, 0xaa, 0xa2, 0xd2, 0xd7, 0xc1, 0xdf
59 ];
60
61
62 let block = ct64();
63 ct64_init(&block, key[..]);
64 defer cipher::finish(&block);
65
66 // test from 1 to 4 parallel blocks
67 for (let i = 0z; i < 4; i += 1) {
68 let result: [64]u8 = [0...];
69
70 let sz = (i + 1) * 16;
71 cipher::encrypt(&block, result[0..sz], plain[0..sz]);
72
73 assert(bytes::equal(cipher[0..sz], result[0..sz]));
74 };
75 };
76
77 @test fn test_decrypt_128_multiple_blocks() void = {
78 const key: [16]u8 = [
79 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
80 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
81 ];
82
83 const plain: [_]u8 = [
84 0xdb, 0x3a, 0x36, 0x50, 0x85, 0x60, 0x4c, 0x65,
85 0xc0, 0x22, 0x5a, 0x0d, 0x68, 0x2f, 0x19, 0xc2,
86 0x16, 0x27, 0xdd, 0xae, 0xd7, 0x28, 0x29, 0x23,
87 0xfa, 0x97, 0x28, 0xb0, 0x9a, 0x90, 0xfe, 0xb3,
88 0xde, 0x57, 0xbc, 0x15, 0x15, 0x8d, 0xb6, 0x29,
89 0x61, 0x40, 0x76, 0x51, 0x32, 0xc2, 0x3d, 0x1e,
90 0xdf, 0x97, 0x6a, 0x5e, 0x0a, 0x6b, 0xf6, 0x40,
91 0xb4, 0x08, 0x7e, 0xde, 0x17, 0xa4, 0x58, 0x98
92 ];
93
94 const cipher: [_]u8 = [
95 0xdd, 0xf4, 0x53, 0x55, 0x18, 0x7a, 0xf0, 0x65,
96 0x59, 0xd6, 0xdc, 0x4e, 0xc2, 0x89, 0xff, 0x7b,
97 0x54, 0x41, 0x6a, 0xda, 0xa0, 0xad, 0x85, 0xbd,
98 0x75, 0x1e, 0x59, 0x15, 0x6d, 0x30, 0xa8, 0xa8,
99 0xa8, 0x54, 0x6e, 0xab, 0x9c, 0x47, 0x84, 0xd,
100 0x6e, 0xb5, 0x5e, 0xba, 0xb0, 0xd9, 0x47, 0xfa,
101 0x53, 0x6b, 0xff, 0x31, 0x47, 0x86, 0xf8, 0x4c,
102 0x6b, 0x65, 0xaa, 0xa2, 0xd2, 0xd7, 0xc1, 0xdf
103 ];
104
105
106 let block = ct64();
107 ct64_init(&block, key[..]);
108 defer cipher::finish(&block);
109
110 // test from 1 to 4 parallel blocks
111 for (let i = 0z; i < 4; i += 1) {
112 let result: [64]u8 = [0...];
113
114 let sz = (i + 1) * 16;
115 cipher::decrypt(&block, result[0..sz], cipher[0..sz]);
116
117 assert(bytes::equal(plain[0..sz], result[0..sz]));
118 };
119 };
120
121 @test fn test_decrypt_128() void = {
122 const key: []u8 = [
123 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
124 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
125 ];
126
127 const plain: [16]u8 = [
128 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
129 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
130 ];
131
132 const cipher: [16]u8 = [
133 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
134 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32,
135 ];
136
137 let result: [16]u8 = [0...];
138
139 let block = ct64();
140 ct64_init(&block, key[..]);
141 defer cipher::finish(&block);
142
143 cipher::decrypt(&block, result[..], cipher[..]);
144
145 assert(bytes::equal(plain, result));
146 };
147
148 // fips-197.pdf Appendix C.1
149 @test fn test_example_vector1() void = {
150 const key: [_]u8 = [
151 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
152 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
153 ];
154
155 const plain: [_]u8 = [
156 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
157 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
158 ];
159
160 const cipher: [_]u8 = [
161 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
162 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a,
163 ];
164
165 let result: [16]u8 = [0...];
166
167 let block = ct64();
168 ct64_init(&block, key[..]);
169 defer cipher::finish(&block);
170
171 cipher::encrypt(&block, result[..], plain[..]);
172 assert(bytes::equal(cipher, result));
173
174 cipher::decrypt(&block, result[..], cipher[..]);
175 assert(bytes::equal(plain, result));
176 };
177
178 @test fn test_example_vector1_in_place() void = {
179 const key: [_]u8 = [
180 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
181 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
182 ];
183
184 const plain: [_]u8 = [
185 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
186 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
187 ];
188
189 const cipher: [_]u8 = [
190 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
191 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a,
192 ];
193
194 let result: [16]u8 = plain;
195
196 let block = ct64();
197 ct64_init(&block, key[..]);
198 defer cipher::finish(&block);
199
200 cipher::encrypt(&block, result[..], result[..]);
201 assert(bytes::equal(cipher, result));
202
203 cipher::decrypt(&block, result[..], result[..]);
204 assert(bytes::equal(plain, result));
205 };
206
207 // fips-197.pdf Appendix C.2
208 @test fn test_example_vector2() void = {
209 const key: [_]u8 = [
210 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
211 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
212 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
213 ];
214
215 const plain: [_]u8 = [
216 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
217 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
218 ];
219
220 const cipher: [_]u8 = [
221 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
222 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91,
223 ];
224
225 let result: [16]u8 = [0...];
226 let block = ct64();
227 ct64_init(&block, key[..]);
228 defer cipher::finish(&block);
229
230 cipher::encrypt(&block, result[..], plain[..]);
231 assert(bytes::equal(cipher, result));
232
233 cipher::decrypt(&block, result[..], cipher[..]);
234 assert(bytes::equal(plain, result));
235 };
236
237 // fips-197.pdf Appendix C.3
238 @test fn test_example_vector3() void = {
239 const key: []u8 = [
240 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
241 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
242 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
243 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
244 ];
245
246 const plain: []u8 = [
247 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
248 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
249 ];
250
251 const cipher: []u8 = [
252 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
253 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89,
254 ];
255
256 let result: [16]u8 = [0...];
257 let block = ct64();
258 ct64_init(&block, key[..]);
259 defer cipher::finish(&block);
260
261 cipher::encrypt(&block, result[..], plain[..]);
262 assert(bytes::equal(cipher, result));
263
264 cipher::decrypt(&block, result[..], cipher[..]);
265 assert(bytes::equal(plain, result));
266 };