1 /*
2  * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Unlicense OR CC0-1.0
5  */
6 /* mbedTLS AES test
7 */
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdbool.h>
11 #include <esp_system.h>
12 #include "mbedtls/aes.h"
13 #include "mbedtls/gcm.h"
14 #include "unity.h"
15 #include "sdkconfig.h"
16 #include "esp_log.h"
17 #include "esp_timer.h"
18 #include "esp_heap_caps.h"
19 #include "test_utils.h"
20 #include "freertos/FreeRTOS.h"
21 #include "freertos/task.h"
22 #include "freertos/semphr.h"
23 #include "esp_memory_utils.h"
24 #include "soc/lldesc.h"
25 
26 
27 static const uint8_t key_256[] = {
28     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
31     0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
32 };
33 
34 static const uint8_t iv[] = {
35     0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
36     0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
37 };
38 
39 /* Cipher produced via this Python:
40     from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
41     from cryptography.hazmat.backends import default_backend
42 
43     def as_c_array(byte_arr):
44 
45         hex_str = ''
46         for idx, byte in enumerate(byte_arr):
47             hex_str += "0x{:02x}, ".format(byte)
48             bytes_per_line = 8
49             if idx % bytes_per_line == bytes_per_line - 1:
50                 hex_str += '\n'
51 
52         return hex_str
53 
54     key = bytearray(range(32))
55     iv = bytearray(range(16, 0, -1))
56 
57     print("Key: \n{}".format(as_c_array(key)))
58     print("IV: \n{}".format(as_c_array(iv)))
59 
60     # Replace CTR with desired mode
61     cipher = Cipher(algorithms.AES(key), modes.CTR(iv), backend=default_backend())
62     encryptor = cipher.encryptor()
63 
64     input_len = 1000
65 
66     plain = b'\x3A'*input_len
67     print(as_c_array(plain))
68     ct = encryptor.update(plain) + encryptor.finalize()
69 
70     print("Chipertext: {}".format(as_c_array(ct)))
71 */
72 TEST_CASE("mbedtls CBC AES-256 test", "[aes]")
73 {
74     const unsigned SZ = 1600;
75     mbedtls_aes_context ctx;
76     uint8_t nonce[16];
77 
78     const uint8_t expected_cipher_end[] = {
79         0x3e, 0x68, 0x8a, 0x02, 0xe6, 0xf2, 0x6a, 0x9e,
80         0x9b, 0xb2, 0xc0, 0xc4, 0x63, 0x63, 0xd9, 0x25,
81         0x51, 0xdc, 0xc2, 0x71, 0x96, 0xb3, 0xe5, 0xcd,
82         0xbd, 0x0e, 0xf2, 0xef, 0xa9, 0xab, 0xab, 0x2d,
83     };
84 
85     memcpy(nonce, iv, 16);
86 
87     // allocate internal memory
88     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
89     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
90     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
91 
92     TEST_ASSERT_NOT_NULL(chipertext);
93     TEST_ASSERT_NOT_NULL(plaintext);
94     TEST_ASSERT_NOT_NULL(decryptedtext);
95 
96     mbedtls_aes_init(&ctx);
97     mbedtls_aes_setkey_enc(&ctx, key_256, 256);
98 
99     memset(plaintext, 0x3A, SZ);
100     memset(decryptedtext, 0x0, SZ);
101 
102     // Encrypt
103     mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, SZ, nonce, plaintext, chipertext);
104     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + SZ - 32, 32);
105 
106     // Decrypt
107     memcpy(nonce, iv, 16);
108     mbedtls_aes_setkey_dec(&ctx, key_256, 256);
109     mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, SZ, nonce, chipertext, decryptedtext);
110 
111     TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
112 
113     mbedtls_aes_free(&ctx);
114     free(plaintext);
115     free(chipertext);
116     free(decryptedtext);
117 }
118 
119 TEST_CASE("mbedtls CBC AES-256 DMA buffer align test", "[aes]")
120 {
121 #define ALIGN_DOWN(val, align) ((val) & ~((align) - 1))
122     // Size is taken considering the maximum DMA buffer size
123     const unsigned SZ = ALIGN_DOWN((2*LLDESC_MAX_NUM_PER_DESC), 16);
124     mbedtls_aes_context ctx;
125     uint8_t nonce[16];
126 
127     const uint8_t expected_cipher_end[] = {
128         0x9e, 0xcb, 0x1d, 0x24, 0x01, 0xc8, 0x3f, 0xba,
129         0xde, 0x76, 0xea, 0x9c, 0xf3, 0x64, 0x23, 0x19,
130         0x8c, 0x67, 0xd4, 0x1a, 0xd1, 0xe0, 0xbf, 0xc3,
131         0xd2, 0xb8, 0x40, 0x95, 0x89, 0x41, 0x09, 0xdb,
132     };
133 
134     memcpy(nonce, iv, 16);
135 
136     // allocate internal memory
137     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
138     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
139     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
140 
141     TEST_ASSERT_NOT_NULL(chipertext);
142     TEST_ASSERT_NOT_NULL(plaintext);
143     TEST_ASSERT_NOT_NULL(decryptedtext);
144 
145     mbedtls_aes_init(&ctx);
146     mbedtls_aes_setkey_enc(&ctx, key_256, 256);
147 
148     memset(plaintext, 0x3A, SZ);
149     memset(decryptedtext, 0x0, SZ);
150 
151     // Encrypt
152     mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, SZ, nonce, plaintext, chipertext);
153     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + SZ - 32, 32);
154 
155     // Decrypt
156     memcpy(nonce, iv, 16);
157     mbedtls_aes_setkey_dec(&ctx, key_256, 256);
158     mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, SZ, nonce, chipertext, decryptedtext);
159 
160     TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
161 
162     mbedtls_aes_free(&ctx);
163     free(plaintext);
164     free(chipertext);
165     free(decryptedtext);
166 }
167 
168 TEST_CASE("mbedtls CTR AES-256 test", "[aes]")
169 {
170     const unsigned SZ = 1000;
171     mbedtls_aes_context ctx;
172     uint8_t nonce[16];
173     uint8_t stream_block[16];
174     size_t nc_off = 0;
175 
176     const uint8_t expected_cipher_end[] = {
177         0xd4, 0xdc, 0x4f, 0x8f, 0xfe, 0x86, 0xee, 0xb5,
178         0x14, 0x7f, 0xba, 0x30, 0x25, 0xa6, 0x7f, 0x6c,
179         0xb5, 0x73, 0xaf, 0x90, 0xd7, 0xff, 0x36, 0xba,
180         0x2b, 0x1d, 0xec, 0xb9, 0x38, 0xfa, 0x0d, 0xeb,
181     };
182 
183     memcpy(nonce, iv, 16);
184 
185     // allocate internal memory
186     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
187     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
188     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
189 
190     TEST_ASSERT_NOT_NULL(chipertext);
191     TEST_ASSERT_NOT_NULL(plaintext);
192     TEST_ASSERT_NOT_NULL(decryptedtext);
193 
194     mbedtls_aes_init(&ctx);
195     mbedtls_aes_setkey_enc(&ctx, key_256, 256);
196 
197     memset(plaintext, 0x3A, SZ);
198     memset(decryptedtext, 0x0, SZ);
199 
200     // Encrypt
201     mbedtls_aes_crypt_ctr(&ctx, SZ, &nc_off, nonce, stream_block, plaintext, chipertext);
202     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + SZ - 32, 32);
203 
204     // Decrypt
205     nc_off = 0;
206     memcpy(nonce, iv, 16);
207     mbedtls_aes_crypt_ctr(&ctx, SZ, &nc_off, nonce, stream_block, chipertext, decryptedtext);
208 
209     TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
210 
211     mbedtls_aes_free(&ctx);
212     free(plaintext);
213     free(chipertext);
214     free(decryptedtext);
215 }
216 
217 TEST_CASE("mbedtls OFB AES-256 test", "[aes]")
218 {
219     const unsigned SZ = 1000;
220     mbedtls_aes_context ctx;
221     uint8_t nonce[16];
222     size_t nc_off = 0;
223 
224     const uint8_t expected_cipher_end[] = {
225         0xca, 0xc3, 0x05, 0x77, 0xae, 0xb9, 0x38, 0xd6,
226         0x03, 0x0a, 0xad, 0x90, 0x6e, 0xdd, 0xf3, 0x9a,
227         0x41, 0x4d, 0x71, 0x30, 0x04, 0x9f, 0xd3, 0x53,
228         0xb7, 0x5e, 0xb4, 0xfd, 0x93, 0xf8, 0x31, 0x6a,
229     };
230 
231     memcpy(nonce, iv, 16);
232 
233     // allocate internal memory
234     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
235     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
236     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
237 
238     TEST_ASSERT_NOT_NULL(chipertext);
239     TEST_ASSERT_NOT_NULL(plaintext);
240     TEST_ASSERT_NOT_NULL(decryptedtext);
241 
242     mbedtls_aes_init(&ctx);
243     mbedtls_aes_setkey_enc(&ctx, key_256, 256);
244 
245     memset(plaintext, 0x3A, SZ);
246     memset(decryptedtext, 0x0, SZ);
247 
248     // Encrypt
249     mbedtls_aes_crypt_ofb(&ctx, SZ, &nc_off, nonce, plaintext, chipertext);
250     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + SZ - 32, 32);
251 
252     // Decrypt
253     nc_off = 0;
254     memcpy(nonce, iv, 16);
255     mbedtls_aes_crypt_ofb(&ctx, SZ, &nc_off, nonce, chipertext, decryptedtext);
256 
257     TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
258 
259     mbedtls_aes_free(&ctx);
260     free(plaintext);
261     free(chipertext);
262     free(decryptedtext);
263 }
264 
265 TEST_CASE("mbedtls CFB-8 AES-256 test", "[aes]")
266 {
267     const unsigned SZ = 1000;
268     mbedtls_aes_context ctx;
269     uint8_t nonce[16];
270 
271     const uint8_t expected_cipher_end[] = {
272         0x69, 0xdc, 0x1d, 0x8a, 0x0b, 0x9e, 0xbc, 0x84,
273         0x29, 0xa2, 0x04, 0xb6, 0x91, 0x6b, 0xb2, 0x83,
274         0x13, 0x23, 0x54, 0xcb, 0xf9, 0x6d, 0xcc, 0x53,
275         0x04, 0x59, 0xd1, 0xc9, 0xff, 0xab, 0xe2, 0x37,
276     };
277 
278     memcpy(nonce, iv, 16);
279 
280     // allocate internal memory
281     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
282     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
283     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
284 
285     TEST_ASSERT_NOT_NULL(chipertext);
286     TEST_ASSERT_NOT_NULL(plaintext);
287     TEST_ASSERT_NOT_NULL(decryptedtext);
288 
289     mbedtls_aes_init(&ctx);
290     mbedtls_aes_setkey_enc(&ctx, key_256, 256);
291 
292     memset(plaintext, 0x3A, SZ);
293     memset(decryptedtext, 0x0, SZ);
294 
295     // Encrypt
296     mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_ENCRYPT, SZ, nonce, plaintext, chipertext);
297     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + SZ - 32, 32);
298 
299     // Decrypt
300     memcpy(nonce, iv, 16);
301     mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_DECRYPT, SZ, nonce, chipertext, decryptedtext);
302 
303     TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
304 
305     mbedtls_aes_free(&ctx);
306     free(plaintext);
307     free(chipertext);
308     free(decryptedtext);
309 }
310 
311 TEST_CASE("mbedtls CFB-128 AES-256 test", "[aes]")
312 {
313     const unsigned SZ = 1000;
314     mbedtls_aes_context ctx;
315     uint8_t nonce[16];
316     size_t nc_off = 0;
317 
318     const uint8_t expected_cipher_end[] = {
319         0xf3, 0x64, 0x20, 0xa1, 0x70, 0x2a, 0xd9, 0x3f,
320         0xb7, 0x48, 0x8c, 0x2c, 0x1f, 0x65, 0x53, 0xc2,
321         0xac, 0xfd, 0x82, 0xe5, 0x31, 0x24, 0x1f, 0x30,
322         0xaf, 0xcc, 0x8d, 0xb3, 0xf3, 0x63, 0xe1, 0xa0,
323     };
324 
325     memcpy(nonce, iv, 16);
326 
327     // allocate internal memory
328     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
329     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
330     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
331 
332     TEST_ASSERT_NOT_NULL(chipertext);
333     TEST_ASSERT_NOT_NULL(plaintext);
334     TEST_ASSERT_NOT_NULL(decryptedtext);
335 
336     mbedtls_aes_init(&ctx);
337     mbedtls_aes_setkey_enc(&ctx, key_256, 256);
338 
339     memset(plaintext, 0x3A, SZ);
340     memset(decryptedtext, 0x0, SZ);
341 
342     // Encrypt
343     mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_ENCRYPT,  SZ, &nc_off, nonce, plaintext, chipertext);
344     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + SZ - 32, 32);
345 
346     // Decrypt
347     nc_off = 0;
348     memcpy(nonce, iv, 16);
349     mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_DECRYPT, SZ, &nc_off, nonce, chipertext, decryptedtext);
350 
351     TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
352 
353     mbedtls_aes_free(&ctx);
354     free(plaintext);
355     free(chipertext);
356     free(decryptedtext);
357 }
358 
aes_ctr_stream_test(void)359 static void aes_ctr_stream_test(void)
360 {
361     const unsigned SZ = 100;
362     mbedtls_aes_context ctx;
363     uint8_t nonce[16];
364     uint8_t key[16];
365     uint8_t stream_block[16];
366 
367     /* Cipher produced via this Python:
368         import os, binascii
369         from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
370         from cryptography.hazmat.backends import default_backend
371         key = b'\x44' * 16
372         nonce = b'\xee' * 16
373         cipher = Cipher(algorithms.AES(key), modes.CTR(nonce), backend=default_backend())
374         encryptor = cipher.encryptor()
375         ct = encryptor.update(b'\xaa' * 100) + encryptor.finalize()
376         ct_arr = ""
377         for idx, b in enumerate(ct):
378             if idx % 8 == 0:
379                 ct_arr += '\n'
380             ct_arr += "0x{}, ".format(binascii.hexlify(b))
381         print(ct_arr)
382     */
383     const uint8_t expected_cipher[] = {
384         0xc5, 0x78, 0xa7, 0xb4, 0xf3, 0xb9, 0xcb, 0x8b,
385         0x09, 0xe0, 0xd6, 0x89, 0x14, 0x6a, 0x19, 0x09,
386         0xde, 0xaf, 0x37, 0x19, 0x32, 0x4d, 0xca, 0xf6,
387         0xff, 0x6e, 0xd2, 0x5d, 0x87, 0x51, 0xaa, 0x8c,
388         0x1c, 0xe3, 0x3b, 0xbb, 0x18, 0xf5, 0xa0, 0x1b,
389         0xdc, 0x29, 0x52, 0x63, 0xf6, 0x5d, 0x49, 0x85,
390         0x29, 0xf1, 0xf0, 0x69, 0x8f, 0xa6, 0x9f, 0x38,
391         0x5c, 0xdd, 0x26, 0xf8, 0x9d, 0x40, 0xa1, 0xff,
392         0x52, 0x46, 0xe1, 0x72, 0x70, 0x39, 0x73, 0xff,
393         0xd0, 0x5e, 0xe5, 0x3f, 0xc5, 0xed, 0x5c, 0x18,
394         0xa7, 0x84, 0xd8, 0xdf, 0x9d, 0xb5, 0x06, 0xb1,
395         0xa7, 0xcf, 0x2e, 0x7a, 0x51, 0xfc, 0x44, 0xc5,
396         0xb9, 0x5f, 0x22, 0x47,
397     };
398 
399 
400     memset(nonce, 0xEE, 16);
401     memset(key, 0x44, 16);
402 
403     // allocate internal memory
404     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
405     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
406     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
407 
408     TEST_ASSERT_NOT_NULL(chipertext);
409     TEST_ASSERT_NOT_NULL(plaintext);
410     TEST_ASSERT_NOT_NULL(decryptedtext);
411 
412     mbedtls_aes_init(&ctx);
413     mbedtls_aes_setkey_enc(&ctx, key, 128);
414     memset(plaintext, 0xAA, SZ);
415 
416     /* Test that all the end results are the same
417         no matter how many bytes we encrypt each call
418         */
419     for (int bytes_to_process = 1; bytes_to_process < SZ; bytes_to_process++) {
420         ESP_LOGD("test", "bytes_to_process %d", bytes_to_process);
421         memset(nonce, 0xEE, 16);
422         memset(chipertext, 0x0, SZ);
423         memset(decryptedtext, 0x0, SZ);
424 
425         size_t offset = 0;
426         // Encrypt
427         for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
428             // Limit length of last call to avoid exceeding buffer size
429             size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process;
430 
431             mbedtls_aes_crypt_ctr(&ctx, length, &offset, nonce,
432                                   stream_block, plaintext + idx, chipertext + idx );
433         }
434         ESP_LOG_BUFFER_HEXDUMP("expected", expected_cipher, SZ, ESP_LOG_DEBUG);
435         ESP_LOG_BUFFER_HEXDUMP("actual  ", chipertext, SZ, ESP_LOG_DEBUG);
436 
437         TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher, chipertext, SZ);
438 
439         // Decrypt
440         memset(nonce, 0xEE, 16);
441         memset(decryptedtext, 0x22, SZ);
442         offset = 0;
443         for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
444             // Limit length of last call to avoid exceeding buffer size
445             size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process;
446             mbedtls_aes_crypt_ctr(&ctx, length, &offset, nonce,
447                                   stream_block, chipertext + idx, decryptedtext + idx );
448         }
449         ESP_LOG_BUFFER_HEXDUMP("decrypted", decryptedtext, SZ, ESP_LOG_DEBUG);
450         TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
451     }
452 
453     mbedtls_aes_free(&ctx);
454     free(plaintext);
455     free(chipertext);
456     free(decryptedtext);
457 }
458 
459 TEST_CASE("mbedtls CTR stream test", "[aes]")
460 {
461     aes_ctr_stream_test();
462 }
463 
464 
465 TEST_CASE("mbedtls OFB stream test", "[aes]")
466 {
467     const unsigned SZ = 100;
468     mbedtls_aes_context ctx;
469     uint8_t iv[16];
470     uint8_t key[16];
471 
472     /* Cipher produced via this Python:
473         import os, binascii
474         from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
475         from cryptography.hazmat.backends import default_backend
476         key = b'\x44' * 16
477         iv = b'\xee' * 16
478         cipher = Cipher(algorithms.AES(key), modes.OFB(iv), backend=default_backend())
479         encryptor = cipher.encryptor()
480         ct = encryptor.update(b'\xaa' * 100) + encryptor.finalize()
481         ct_arr = ""
482         for idx, b in enumerate(ct):
483             if idx % 8 == 0:
484                 ct_arr += '\n'
485             ct_arr += "0x{}, ".format(binascii.hexlify(b))
486         print(ct_arr)
487     */
488     const uint8_t expected_cipher[] = {
489         0xc5, 0x78, 0xa7, 0xb4, 0xf3, 0xb9, 0xcb, 0x8b,
490         0x09, 0xe0, 0xd6, 0x89, 0x14, 0x6a, 0x19, 0x09,
491         0x0a, 0x33, 0x8b, 0xab, 0x82, 0xcb, 0x20, 0x8f,
492         0x74, 0x2a, 0x6c, 0xb3, 0xc6, 0xe8, 0x18, 0x89,
493         0x09, 0xb6, 0xaf, 0x20, 0xcd, 0xea, 0x74, 0x14,
494         0x48, 0x61, 0xe8, 0x4d, 0x50, 0x12, 0x9f, 0x5e,
495         0xb8, 0x10, 0x53, 0x3b, 0x74, 0xd9, 0xd0, 0x95,
496         0x13, 0xdc, 0x14, 0xcf, 0x0c, 0xa1, 0x90, 0xfd,
497         0xa2, 0x58, 0x12, 0xb2, 0x00, 0x2c, 0x5b, 0x7a,
498         0x2a, 0x76, 0x80, 0x20, 0x82, 0x39, 0xa2, 0x21,
499         0xf8, 0x7a, 0xec, 0xae, 0x82, 0x6a, 0x5c, 0xd3,
500         0x04, 0xd9, 0xbd, 0xe4, 0x53, 0xc9, 0xdf, 0x67,
501         0xaa, 0x5c, 0xaf, 0xa6,
502     };
503 
504 
505     memset(key, 0x44, 16);
506 
507     // allocate internal memory
508     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
509     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
510     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
511 
512     TEST_ASSERT_NOT_NULL(chipertext);
513     TEST_ASSERT_NOT_NULL(plaintext);
514     TEST_ASSERT_NOT_NULL(decryptedtext);
515 
516     mbedtls_aes_init(&ctx);
517     mbedtls_aes_setkey_enc(&ctx, key, 128);
518     memset(plaintext, 0xAA, SZ);
519 
520     /* Test that all the end results are the same
521         no matter how many bytes we encrypt each call
522         */
523 
524     for (int bytes_to_process = 1; bytes_to_process < SZ; bytes_to_process++) {
525         ESP_LOGD("test", "bytes_to_process %d", bytes_to_process);
526         // Encrypt
527         memset(iv, 0xEE, 16);
528         size_t offset = 0;
529         for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
530             // Limit length of last call to avoid exceeding buffer size
531             size_t length = ( (idx + bytes_to_process) > SZ) ? (SZ - idx) : bytes_to_process;
532             mbedtls_aes_crypt_ofb(&ctx, length, &offset, iv, plaintext + idx, chipertext + idx);
533 
534         }
535         TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher, chipertext, SZ);
536 
537         // Decrypt
538         memset(iv, 0xEE, 16);
539         memset(decryptedtext, 0x22, SZ);
540         offset = 0;
541         for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
542             // Limit length of last call to avoid exceeding buffer size
543             size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process;
544             mbedtls_aes_crypt_ofb(&ctx, length, &offset, iv, chipertext + idx, decryptedtext + idx);
545         }
546         TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
547     }
548 
549     mbedtls_aes_free(&ctx);
550     free(plaintext);
551     free(chipertext);
552     free(decryptedtext);
553 }
554 
555 TEST_CASE("mbedtls CFB8 stream test", "[aes]")
556 {
557     const unsigned SZ = 32;
558     mbedtls_aes_context ctx;
559     uint8_t iv[16];
560     uint8_t key[16];
561 
562     /* Cipher produced via this Python:
563         import os, binascii
564         from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
565         from cryptography.hazmat.backends import default_backend
566         key = b'\x44' * 16
567         iv = b'\xee' * 16
568         cipher = Cipher(algorithms.AES(key), modes.CFB8(iv), backend=default_backend())
569         encryptor = cipher.encryptor()
570         ct = encryptor.update(b'\xaa' * 100) + encryptor.finalize()
571         ct_arr = ""
572         for idx, b in enumerate(ct):
573             if idx % 8 == 0:
574                 ct_arr += '\n'
575             ct_arr += "0x{}, ".format(binascii.hexlify(b))
576         print(ct_arr)
577     */
578     const uint8_t expected_cipher[] = {
579         0xc5, 0x2f, 0xb0, 0x9b, 0x94, 0x9c, 0xa4, 0x5c,
580         0x0f, 0x4d, 0xa1, 0x9d, 0xd1, 0x19, 0xfc, 0x04,
581         0xe2, 0x7f, 0x04, 0x82, 0x6a, 0xa3, 0x61, 0xbb,
582         0x07, 0x6f, 0xac, 0xb9, 0xdf, 0x00, 0xf9, 0xa8,
583         0xc4, 0xbe, 0x9d, 0x4d, 0xd9, 0x42, 0x8a, 0x83,
584         0x12, 0x8b, 0xeb, 0xd7, 0x88, 0x70, 0x8a, 0xed,
585         0x46, 0x81, 0x5b, 0x4c, 0x14, 0x67, 0xe0, 0xfb,
586         0xab, 0x34, 0x90, 0x85, 0x24, 0xd2, 0x6b, 0x64,
587         0xdf, 0x1d, 0x04, 0xfd, 0x69, 0xf6, 0x30, 0xbe,
588         0xa6, 0xac, 0x0b, 0x54, 0x25, 0x24, 0x67, 0xd6,
589         0x09, 0xb1, 0x8f, 0x91, 0x63, 0xbd, 0xdf, 0xa1,
590         0x8a, 0xa3, 0x2e, 0xeb, 0x15, 0x7d, 0xe5, 0x37,
591         0xe5, 0x5a, 0x9f, 0xa5,
592     };
593 
594 
595     memset(key, 0x44, 16);
596 
597     // allocate internal memory
598     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
599     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
600     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
601 
602     TEST_ASSERT_NOT_NULL(chipertext);
603     TEST_ASSERT_NOT_NULL(plaintext);
604     TEST_ASSERT_NOT_NULL(decryptedtext);
605 
606     mbedtls_aes_init(&ctx);
607     mbedtls_aes_setkey_enc(&ctx, key, 128);
608     memset(plaintext, 0xAA, SZ);
609 
610     /* Test that all the end results are the same
611         no matter how many bytes we encrypt each call
612         */
613 
614     for (int bytes_to_process = 1; bytes_to_process < SZ; bytes_to_process++) {
615         memset(iv, 0xEE, 16);
616         for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
617             // Limit length of last call to avoid exceeding buffer size
618             size_t length = ( (idx + bytes_to_process) > SZ) ? (SZ - idx) : bytes_to_process;
619             mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_ENCRYPT, length, iv, plaintext + idx, chipertext + idx);
620 
621         }
622         TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher, chipertext, SZ);
623 
624         memset(iv, 0xEE, 16);
625         for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
626             // Limit length of last call to avoid exceeding buffer size
627             size_t length = ( (idx + bytes_to_process) > SZ) ? (SZ - idx) : bytes_to_process;
628             mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_DECRYPT, length, iv, chipertext + idx, decryptedtext + idx);
629 
630         }
631         TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
632     }
633 
634     mbedtls_aes_free(&ctx);
635     free(plaintext);
636     free(chipertext);
637     free(decryptedtext);
638 }
639 
640 TEST_CASE("mbedtls CFB128 stream test", "[aes]")
641 {
642     const unsigned SZ = 32;
643     mbedtls_aes_context ctx;
644     uint8_t iv[16];
645     uint8_t key[16];
646 
647     /* Cipher produced via this Python:
648         import os, binascii
649         from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
650         from cryptography.hazmat.backends import default_backend
651         key = b'\x44' * 16
652         iv = b'\xee' * 16
653         cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
654         encryptor = cipher.encryptor()
655         ct = encryptor.update(b'\xaa' * 100) + encryptor.finalize()
656         ct_arr = ""
657         for idx, b in enumerate(ct):
658             if idx % 8 == 0:
659                 ct_arr += '\n'
660             ct_arr += "0x{}, ".format(binascii.hexlify(b))
661         print(ct_arr)
662     */
663     const uint8_t expected_cipher[] = {
664         0xc5, 0x78, 0xa7, 0xb4, 0xf3, 0xb9, 0xcb, 0x8b,
665         0x09, 0xe0, 0xd6, 0x89, 0x14, 0x6a, 0x19, 0x09,
666         0xf9, 0x08, 0x7e, 0xe1, 0x92, 0x8a, 0x7c, 0xa4,
667         0x25, 0xa5, 0xa7, 0x43, 0x24, 0x8d, 0x85, 0x3e,
668         0x99, 0x28, 0xeb, 0x36, 0x59, 0x74, 0x69, 0x0e,
669         0x09, 0x9f, 0x4e, 0xc0, 0x6d, 0xc3, 0x2b, 0x80,
670         0x01, 0xad, 0xa1, 0x0c, 0x99, 0x90, 0x8b, 0x07,
671         0xd6, 0x00, 0xf0, 0x32, 0xd7, 0x6b, 0xa1, 0xf1,
672         0x4d, 0x14, 0xd0, 0x28, 0xde, 0x64, 0x23, 0x71,
673         0xf4, 0x23, 0x61, 0x12, 0x71, 0xbe, 0x03, 0x74,
674         0x99, 0x81, 0x9d, 0x65, 0x48, 0xd9, 0xd4, 0x67,
675         0xd1, 0x31, 0xe8, 0x44, 0x27, 0x17, 0xd4, 0x2d,
676         0x3d, 0x59, 0xf7, 0xd3,
677     };
678 
679 
680     memset(key, 0x44, 16);
681 
682     // allocate internal memory
683     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
684     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
685     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
686 
687     TEST_ASSERT_NOT_NULL(chipertext);
688     TEST_ASSERT_NOT_NULL(plaintext);
689     TEST_ASSERT_NOT_NULL(decryptedtext);
690 
691     mbedtls_aes_init(&ctx);
692     mbedtls_aes_setkey_enc(&ctx, key, 128);
693     memset(plaintext, 0xAA, SZ);
694 
695     /* Test that all the end results are the same
696         no matter how many bytes we encrypt each call
697     */
698 
699     //for (int bytes_to_process = 1; bytes_to_process < SZ; bytes_to_process++) {
700     int bytes_to_process = 17;
701     size_t offset = 0;
702     memset(iv, 0xEE, 16);
703     for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
704         // Limit length of last call to avoid exceeding buffer size
705         size_t length = ( (idx + bytes_to_process) > SZ) ? (SZ - idx) : bytes_to_process;
706         mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_ENCRYPT, length, &offset, iv, plaintext + idx, chipertext + idx);
707 
708     }
709     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher, chipertext, SZ);
710 
711     offset = 0;
712     memset(iv, 0xEE, 16);
713     for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
714         // Limit length of last call to avoid exceeding buffer size
715         size_t length = ( (idx + bytes_to_process) > SZ) ? (SZ - idx) : bytes_to_process;
716         mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_DECRYPT, length, &offset, iv, chipertext + idx, decryptedtext + idx);
717 
718     }
719     TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
720 
721     mbedtls_aes_free(&ctx);
722     free(plaintext);
723     free(chipertext);
724     free(decryptedtext);
725 }
726 
727 /* Cipher produced via this Python:
728         import os, binascii
729         from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
730         from cryptography.hazmat.backends import default_backend
731         key = b'\x44' * 16
732         nonce = b'\xee' * 16
733         cipher = Cipher(algorithms.AES(key), modes.CTR(nonce), backend=default_backend())
734         encryptor = cipher.encryptor()
735         ct = encryptor.update(b'\xaa' * 100) + encryptor.finalize()
736         ct_arr = ""
737         for idx, b in enumerate(ct):
738             if idx % 8 == 0:
739                 ct_arr += '\n'
740             ct_arr += "0x{}, ".format(binascii.hexlify(b))
741         print(ct_arr)
742 */
743 
744 /* Test the case where the input and output buffers point to the same location */
745 TEST_CASE("mbedtls CTR, input buf = output buf", "[aes]")
746 {
747     const unsigned SZ = 1000;
748     mbedtls_aes_context ctx;
749     uint8_t nonce[16];
750     uint8_t stream_block[16];
751     size_t nc_off = 0;
752 
753     const uint8_t expected_cipher_end[] = {
754         0xd4, 0xdc, 0x4f, 0x8f, 0xfe, 0x86, 0xee, 0xb5,
755         0x14, 0x7f, 0xba, 0x30, 0x25, 0xa6, 0x7f, 0x6c,
756         0xb5, 0x73, 0xaf, 0x90, 0xd7, 0xff, 0x36, 0xba,
757         0x2b, 0x1d, 0xec, 0xb9, 0x38, 0xfa, 0x0d, 0xeb,
758     };
759 
760     memcpy(nonce, iv, 16);
761 
762     // allocate internal memory
763     uint8_t *buf = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
764 
765     TEST_ASSERT_NOT_NULL(buf);
766 
767     mbedtls_aes_init(&ctx);
768     mbedtls_aes_setkey_enc(&ctx, key_256, 256);
769 
770     memset(buf, 0x3A, SZ);
771 
772     // Encrypt
773     mbedtls_aes_crypt_ctr(&ctx, SZ, &nc_off, nonce, stream_block, buf, buf);
774     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, buf + SZ - 32, 32);
775 
776     // Decrypt
777     nc_off = 0;
778     memcpy(nonce, iv, 16);
779     mbedtls_aes_crypt_ctr(&ctx, SZ, &nc_off, nonce, stream_block, buf, buf);
780 
781     for (int i = 0; i < SZ; i++) {
782         TEST_ASSERT_EQUAL_HEX8(0x3A, buf[i]);
783     }
784 
785     mbedtls_aes_free(&ctx);
786     free(buf);
787 }
788 
789 TEST_CASE("mbedtls OFB, chained DMA descriptors", "[aes]")
790 {
791     // Max bytes in a single DMA descriptor is 4095
792     const unsigned SZ = 6000;
793     mbedtls_aes_context ctx;
794     uint8_t nonce[16];
795     size_t nc_off = 0;
796 
797     const uint8_t expected_cipher_end[] = {
798         0xfe, 0xfa, 0xc9, 0x26, 0xb5, 0xc9, 0xea, 0xb0,
799         0xdd, 0x1e, 0xe7, 0x0e, 0xfa, 0x5b, 0x4b, 0x94,
800         0xaa, 0x5f, 0x60, 0x1e, 0xb2, 0x19, 0x3c, 0x2e,
801         0xf6, 0x73, 0x56, 0x9f, 0xa7, 0xd5, 0xb7, 0x21,
802     };
803 
804     memcpy(nonce, iv, 16);
805 
806     // allocate internal memory
807     uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
808     uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
809     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
810 
811     TEST_ASSERT_NOT_NULL(chipertext);
812     TEST_ASSERT_NOT_NULL(plaintext);
813     TEST_ASSERT_NOT_NULL(decryptedtext);
814 
815     mbedtls_aes_init(&ctx);
816     mbedtls_aes_setkey_enc(&ctx, key_256, 256);
817 
818     memset(plaintext, 0x3A, SZ);
819     memset(decryptedtext, 0x0, SZ);
820 
821     // Encrypt
822     mbedtls_aes_crypt_ofb(&ctx, SZ, &nc_off, nonce, plaintext, chipertext);
823     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + SZ - 32, 32);
824 
825 
826     // Decrypt
827     nc_off = 0;
828     memcpy(nonce, iv, 16);
829     mbedtls_aes_crypt_ofb(&ctx, SZ, &nc_off, nonce, chipertext, decryptedtext);
830 
831     TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
832 
833     mbedtls_aes_free(&ctx);
834     free(plaintext);
835     free(chipertext);
836     free(decryptedtext);
837 }
838 
839 
840 
841 const uint8_t expected_cipher_ctr_end[] = {
842     0x93, 0xca, 0xe0, 0x44, 0x96, 0x6d, 0xcb, 0xb2,
843     0xcf, 0x8a, 0x8d, 0x73, 0x8c, 0x6b, 0xfa, 0x4d,
844     0xd6, 0xc4, 0x18, 0x49, 0xdd, 0xc6, 0xbf, 0xc2,
845     0xb9, 0xf0, 0x09, 0x69, 0x45, 0x42, 0xc6, 0x05,
846 };
847 
848 
aes_ctr_alignment_test(uint32_t input_buf_caps,uint32_t output_buf_caps)849 void aes_ctr_alignment_test(uint32_t input_buf_caps, uint32_t output_buf_caps)
850 {
851     mbedtls_aes_context ctx;
852     uint8_t nonce[16];
853     uint8_t key[16];
854     uint8_t stream_block[16];
855     size_t SZ = 32*200;
856     size_t ALIGNMENT_SIZE_BYTES = 64;
857     memset(nonce, 0x2F, 16);
858     memset(key, 0x1E, 16);
859 
860     // allocate memory according the requested caps
861     uint8_t *chipertext = heap_caps_malloc(SZ + ALIGNMENT_SIZE_BYTES, output_buf_caps);
862     uint8_t *plaintext = heap_caps_malloc(SZ + ALIGNMENT_SIZE_BYTES, input_buf_caps);
863     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
864 
865     TEST_ASSERT_NOT_NULL(chipertext);
866     TEST_ASSERT_NOT_NULL(plaintext);
867     TEST_ASSERT_NOT_NULL(decryptedtext);
868 
869     mbedtls_aes_init(&ctx);
870     mbedtls_aes_setkey_enc(&ctx, key, 128);
871     memset(plaintext, 0x26, SZ + ALIGNMENT_SIZE_BYTES);
872 
873     size_t offset;
874 
875     /* Shift buffers and test for all different misalignments */
876     for (int i = 0; i < ALIGNMENT_SIZE_BYTES; i++ ) {
877         // Encrypt with input buffer in external ram
878         offset = 0;
879         memset(nonce, 0x2F, 16);
880         mbedtls_aes_crypt_ctr(&ctx, SZ, &offset, nonce, stream_block, plaintext + i, chipertext + i);
881         TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_ctr_end, chipertext + i + SZ - 32, 32);
882 
883         // Decrypt
884         offset = 0;
885         memset(nonce, 0x2F, 16);
886         // Decrypt with input buffer in instruction memory, the crypto DMA can't access this
887         mbedtls_aes_crypt_ctr(&ctx, SZ, &offset, nonce, stream_block, chipertext + i, decryptedtext);
888 
889         TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
890 
891     }
892 
893     mbedtls_aes_free(&ctx);
894     free(plaintext);
895     free(chipertext);
896     free(decryptedtext);
897 }
898 
899 TEST_CASE("mbedtls AES internal mem alignment tests", "[aes]")
900 {
901     uint32_t internal_dma_caps = MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL;
902     aes_ctr_alignment_test(internal_dma_caps, internal_dma_caps);
903 }
904 
905 
906 #ifdef CONFIG_SPIRAM_USE_MALLOC
907 
aes_psram_one_buf_ctr_test(void)908 void aes_psram_one_buf_ctr_test(void)
909 {
910     mbedtls_aes_context ctx;
911     uint8_t nonce[16];
912     uint8_t key[16];
913     uint8_t stream_block[16];
914     size_t SZ = 32*200;
915     size_t ALIGNMENT_SIZE_BYTES = 32;
916     memset(nonce, 0x2F, 16);
917     memset(key, 0x1E, 16);
918 
919     // allocate external memory
920     uint8_t *buf = heap_caps_malloc(SZ + ALIGNMENT_SIZE_BYTES, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
921 
922     TEST_ASSERT_NOT_NULL(buf);
923 
924     mbedtls_aes_init(&ctx);
925     mbedtls_aes_setkey_enc(&ctx, key, 128);
926     memset(buf, 0x26, SZ + ALIGNMENT_SIZE_BYTES);
927 
928     size_t offset;
929 
930     /* Shift buffers and test for all different misalignments */
931     for (int i = 0; i < ALIGNMENT_SIZE_BYTES; i++ ) {
932         // Encrypt with input buffer in external ram
933         offset = 0;
934         memset(buf, 0x26, SZ + ALIGNMENT_SIZE_BYTES);
935         memset(nonce, 0x2F, 16);
936         mbedtls_aes_crypt_ctr(&ctx, SZ, &offset, nonce, stream_block, buf + i, buf + i);
937         TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_ctr_end, buf + i + SZ - 32, 32);
938 
939         // Decrypt
940         offset = 0;
941         memset(nonce, 0x2F, 16);
942         // Decrypt with input buffer in instruction memory, the crypto DMA can't access this
943         mbedtls_aes_crypt_ctr(&ctx, SZ, &offset, nonce, stream_block, buf + i, buf);
944 
945         TEST_ASSERT_EACH_EQUAL_HEX8(0x26, buf + i, SZ - i);
946 
947     }
948     mbedtls_aes_free(&ctx);
949     free(buf);
950 }
951 
952 
953 const uint8_t long_input[] = {
954     0xf7, 0xe6, 0x6b, 0x8d, 0x2e, 0xbf, 0x88, 0xd6,
955     0xb0, 0x77, 0xdf, 0x72, 0xbf, 0xa8, 0x0, 0x55,
956     0xd5, 0xd1, 0x49, 0xa3, 0x2c, 0xc, 0xfe, 0xdb,
957     0x17, 0x37, 0xa4, 0x1d, 0x70, 0x6b, 0x99, 0xf5,
958     0x9e, 0x6, 0xad, 0x6c, 0xe0, 0x3b, 0xfa, 0x50,
959     0x28, 0xb2, 0x62, 0xf2, 0x99, 0x3a, 0xcc, 0xe4,
960     0x86, 0x5f, 0x1, 0xf8, 0x69, 0xd7, 0xf5, 0xb2,
961     0x8a, 0x5f, 0x5c, 0x38, 0x9f, 0x8a, 0xb8, 0x8c,
962     0xea, 0x6, 0xe1, 0x68, 0xff, 0xaf, 0x5d, 0xd9,
963     0x1f, 0xa5, 0x5c, 0x8c, 0x52, 0xa1, 0x5f, 0x45,
964     0x55, 0xcb, 0x76, 0x59, 0x8f, 0xfe, 0x36, 0xd0,
965     0x85, 0x1f, 0x8, 0x90, 0x6f, 0x62, 0xb1, 0x1a,
966     0xde, 0x75, 0xab, 0x90, 0xb7, 0x75, 0xe9, 0xa0,
967     0xa9, 0xb0, 0xac, 0x61, 0x5, 0x6d, 0x9a, 0xe3,
968     0x3b, 0x43, 0x61, 0x13, 0x8c, 0x3a, 0xa0, 0xaa,
969     0x91, 0xea, 0x3e, 0xe1, 0x87, 0x35, 0xff, 0x90,
970     0xe2, 0x43, 0xa3, 0x70, 0x57, 0x65, 0x2d, 0xa2,
971     0x65, 0xe6, 0xde, 0xb0, 0x52, 0x85, 0x5b, 0xb8,
972     0x3, 0x8, 0x63, 0x8b, 0xa1, 0xc2, 0xe1, 0x35,
973     0x2e, 0xba, 0xe0, 0x84, 0x56, 0x52, 0x5f, 0x12,
974     0xd3, 0x22, 0x8d, 0xa5, 0xbb, 0xe1, 0xd3, 0xfc,
975     0x18, 0x1c, 0x90, 0x3b, 0x79, 0xe, 0xab, 0x2d,
976     0x5e, 0xb0, 0x7, 0xbb, 0x46, 0x73, 0x1d, 0x35,
977     0xd9, 0xc5, 0xa7, 0x87, 0x80, 0xf7, 0xee, 0x29,
978     0xb5, 0x17, 0xf3, 0xaf, 0x30, 0xe5, 0x19, 0x50,
979     0xf9, 0x5d, 0x2b, 0xc3, 0xc0, 0xda, 0x8f, 0xca,
980     0x3c, 0x4d, 0xd5, 0xd7, 0x6c, 0xd2, 0x36, 0xa4,
981     0x22, 0x8, 0x66, 0x48, 0x31, 0xb4, 0x3d, 0xc2,
982     0xf6, 0x6b, 0xce, 0xf0, 0x12, 0xe4, 0x38, 0x5c,
983     0xd8, 0x71, 0xea, 0x30, 0x52, 0xdf, 0x34, 0x62,
984     0xdc, 0xb4, 0x30, 0xe, 0x74, 0xc, 0x5, 0x14,
985     0xf, 0x47, 0x25, 0x5, 0x72, 0xc9, 0x14, 0x7c,
986     0x1f, 0x6e, 0xdb, 0x6f, 0x83, 0x6, 0xa0, 0xb2,
987     0x7f, 0x29, 0xe6, 0xb6, 0xe3, 0x11, 0x23, 0x4b,
988     0x68, 0x92, 0xa, 0x49, 0xb5, 0x9d, 0x5d, 0x39,
989     0x90, 0xff, 0x9, 0xa0, 0xa, 0x69, 0x6b, 0x2,
990     0x18, 0xfb, 0xca, 0x5a, 0x91, 0x1a, 0xd9, 0x19,
991     0x6b, 0xd4, 0x92, 0xd3, 0xd9, 0x7, 0xce, 0xcb,
992     0xc7, 0xf3, 0xa1, 0x33, 0xcd, 0xa9, 0xb1, 0x44,
993     0x8c, 0x93, 0xcd, 0xac, 0xc1, 0x44, 0x12, 0x48,
994     0x95, 0x3, 0xdf, 0xc, 0x2f, 0xfc, 0x34, 0x8d,
995     0x3, 0xde, 0xc1, 0xed, 0xdc, 0xf0, 0xfa, 0xa5,
996     0xb2, 0x62, 0xcd, 0xa2, 0xbf, 0xf7, 0x7e, 0x47,
997     0xb6, 0xcc, 0xe4, 0xa6, 0x4e, 0x51, 0xc6, 0x34,
998     0xee, 0x83, 0x21, 0xb7, 0xc2, 0xe3, 0x13, 0x92,
999     0xfc, 0xc9, 0x6, 0x6b, 0x91, 0x76, 0x7b, 0x2e,
1000     0x1e, 0xa2, 0xe0, 0x17, 0xab, 0x10, 0xfa, 0xac,
1001     0xd1, 0x2, 0x33, 0xb0, 0xd3, 0x3d, 0xb9, 0xce,
1002     0xea, 0xe9, 0x93, 0x5c, 0x98, 0x14, 0x0, 0xc6,
1003     0x2c, 0xa6, 0xdb, 0x1f, 0xdc, 0x76, 0xfb, 0xeb,
1004     0x9d, 0x55, 0xa6, 0x5f, 0xd5, 0x8e, 0x13, 0x39,
1005     0x88, 0x58, 0xff, 0xe8, 0xb4, 0x98, 0x9e, 0x4b,
1006     0xe7, 0x46, 0xdc, 0x7a, 0x68, 0x5b, 0xa8, 0xc2,
1007     0xe5, 0xa9, 0x50, 0xe2, 0x8, 0x31, 0x6, 0x3e,
1008     0x8e, 0xaf, 0x80, 0x24, 0x4e, 0xbd, 0x73, 0x6d,
1009     0xd9, 0x4b, 0xb4, 0x3e, 0x84, 0x5e, 0x31, 0x8e,
1010     0xf7, 0xa8, 0x9b, 0x5e, 0x2c, 0xd5, 0xe9, 0x7c,
1011     0xca, 0xca, 0xfa, 0x8e, 0x87, 0xbf, 0xf5, 0xa3,
1012     0x2f, 0x73, 0x2f, 0xc0, 0x5f, 0x46, 0xf4, 0x2,
1013     0xfd, 0xd1, 0x23, 0x6f, 0xc2, 0xc1, 0xc0, 0x86,
1014     0x62, 0x43, 0xc3, 0x44, 0x3b, 0x2c, 0x3d, 0xc2,
1015     0xd5, 0xe0, 0x2, 0xae, 0x1, 0x5a, 0x9, 0x89,
1016     0x52, 0x34, 0xdf, 0xb1, 0x6c, 0x2b, 0x85, 0x77,
1017     0xa5, 0x83, 0xe3, 0xa5, 0x50, 0x13, 0x2f, 0xf3,
1018     0xa6, 0x83, 0x60, 0x33, 0xba, 0xd5, 0xd2, 0x96,
1019     0x8a, 0xcd, 0xee, 0xfa, 0x76, 0x2a, 0x63, 0xec,
1020     0x41, 0x3a, 0xf3, 0xe5, 0x9e, 0x1d, 0x5e, 0x46,
1021     0x8, 0xd7, 0xe2, 0x3a, 0x25, 0x6f, 0x37, 0x7e,
1022     0x0, 0x2d, 0x3d, 0x1b, 0x86, 0xf4, 0xbe, 0x0,
1023     0x3c, 0xda, 0x82, 0x4a, 0xa3, 0x8, 0x2a, 0x38,
1024     0x95, 0xe, 0x38, 0xf8, 0x18, 0x6c, 0x42, 0x6f,
1025     0x30, 0x19, 0x8e, 0x22, 0xf6, 0xb7, 0x18, 0xb7,
1026     0x93, 0xd, 0x54, 0x72, 0x4, 0x64, 0xc1, 0x19,
1027     0x76, 0x6e, 0xfc, 0x9e, 0xb0, 0x7c, 0x20, 0x37,
1028     0xb0, 0xcb, 0x82, 0x3a, 0x20, 0x1d, 0x12, 0x44,
1029     0xbf, 0x44, 0xc4, 0x4d, 0x33, 0x7e, 0x7b, 0xeb,
1030     0xd8, 0xb8, 0xa1, 0x75, 0x9e, 0x47, 0x99, 0x64,
1031     0x92, 0xd3, 0x21, 0x1d, 0x72, 0x63, 0xc7, 0xb3,
1032     0x3d, 0xfc, 0xb9, 0x4, 0x65, 0x18, 0x94, 0xcc,
1033     0x20, 0xfe, 0x6f, 0x66, 0x36, 0xba, 0x36, 0x2a,
1034     0x7, 0xf0, 0x5e, 0x8a, 0xf2, 0x7, 0x1e, 0x9e,
1035     0x47, 0x2a, 0xc3, 0x7d, 0x7a, 0x20, 0x3c, 0x30,
1036     0x6f, 0xbe, 0x43, 0x5e, 0x71, 0x6f, 0xd, 0xb8,
1037     0x3d, 0x1d, 0x3e, 0x18, 0x65, 0x62, 0x75, 0xe8,
1038     0x34, 0xfd, 0x72, 0xbb, 0xd9, 0x3f, 0xf0, 0xa2,
1039     0x55, 0xee, 0x91, 0x12, 0x88, 0xda, 0x7, 0x3d,
1040     0x44, 0x88, 0x70, 0x1f, 0xe0, 0xbe, 0x4b, 0x88,
1041     0xa8, 0x8e, 0x28, 0x7, 0x73, 0xfd, 0x3f, 0xff,
1042     0x3e, 0xb2, 0xb5, 0xdb, 0x18, 0x48, 0x9e, 0x73,
1043     0x6e, 0xd7, 0x24, 0xa9, 0x25, 0xdb, 0x4, 0xe0,
1044     0xe0, 0xf4, 0x45, 0xc0, 0x1b, 0x82, 0xdf, 0x4e,
1045     0x48, 0x60, 0x85, 0x9c, 0xd8, 0x90, 0x32, 0xca,
1046     0x4b, 0xf9, 0xb4, 0xb8, 0xe1, 0xfe, 0xd2, 0xe0,
1047     0xb2, 0xd6, 0xb8, 0x19, 0x38, 0x34, 0x17, 0x8d,
1048     0x5e, 0xdf, 0xf4, 0xf1, 0xac, 0x2c, 0x88, 0x7f,
1049     0x54, 0xbc, 0xf1, 0x39, 0xf2, 0xaf, 0x5a, 0xff,
1050     0xa7, 0x96, 0x0, 0xf0, 0x27, 0x79, 0x27, 0x2e,
1051     0x9c, 0xf1, 0x4b, 0xa3, 0xad, 0xdc, 0x8a, 0x2c,
1052     0x9, 0x4c, 0xd3, 0xcd, 0xd0, 0x2d, 0xb1, 0xec,
1053     0x4d, 0x68, 0x40, 0xb8, 0xc5, 0x5, 0xfa, 0xb2,
1054     0x61, 0xb8, 0x31, 0x5, 0xea, 0xb8, 0xa3, 0x34,
1055     0xa8, 0x8b, 0x3, 0x5b, 0x22, 0x93, 0xba, 0x91,
1056     0x33, 0x3f, 0x8b, 0x5e, 0xed, 0x86, 0x23, 0x95,
1057     0xbc, 0x9e, 0xdf, 0xa9, 0x8c, 0xca, 0xb9, 0x97,
1058     0x9b, 0xc5, 0xca, 0xf4, 0xff, 0x4d, 0x62, 0x52,
1059     0x1c, 0xd3, 0x4c, 0x42, 0xbf, 0x8a, 0x25, 0x47,
1060     0xc7, 0x9, 0x4e, 0xe0, 0xb1, 0x72, 0x7d, 0x2,
1061     0x8f, 0xca, 0x4f, 0x4, 0xc8, 0x74, 0x82, 0x8e,
1062     0x53, 0xfd, 0xa1, 0x37, 0xda, 0x29, 0x5c, 0xa3,
1063     0x83, 0xe9, 0xa8, 0xd8, 0x25, 0x27, 0xfe, 0xf7,
1064     0x41, 0xc4, 0xb0, 0xee, 0x1d, 0x89, 0x1c, 0xe7,
1065     0xef, 0x86, 0x68, 0xd8, 0x87, 0x4c, 0x4f, 0x49,
1066     0xeb, 0xbc, 0xb3, 0x81, 0xa7, 0xf4, 0xb4, 0x9b,
1067     0xc1, 0x52, 0x93, 0x7e, 0xdf, 0x75, 0x75, 0xfc,
1068     0x45, 0xb2, 0x86, 0xa9, 0x50, 0xb5, 0xa3, 0xf7,
1069     0x61, 0x60, 0xe4, 0x13, 0x99, 0xc0, 0xf8, 0x49,
1070     0x7b, 0x61, 0x8b, 0xa8, 0xfa, 0x77, 0x0, 0xe4,
1071     0x6, 0x9a, 0xc5, 0x51, 0xe4, 0xeb, 0xaf, 0x5f,
1072     0xb9, 0x5c, 0x74, 0xc8, 0xf8, 0x3e, 0x62, 0x26,
1073     0xe, 0xe5, 0x85, 0xca, 0x49, 0xa0, 0x2f, 0xf7,
1074     0x7, 0x99, 0x3e, 0x5c, 0xe0, 0x72, 0xfa, 0xd4,
1075     0x80, 0x2e, 0xd6, 0x40, 0x6, 0xde, 0x5f, 0xc5,
1076     0xc5, 0x1, 0xd, 0xbf, 0xdb, 0xb6, 0xb3, 0x92,
1077     0x76, 0xb3, 0x3f, 0x3d, 0x5d, 0x1, 0x23, 0xb8,
1078     0xa, 0xcb, 0x80, 0x17, 0x31, 0x19, 0xc7, 0x64,
1079     0x69, 0xf1, 0x99, 0x53, 0xe5, 0xf2, 0x9f, 0x9d,
1080     0x3c, 0xda, 0xcb, 0xa6, 0x94, 0x94, 0x44, 0xd3,
1081     0xc6, 0x8b, 0xb5, 0xae, 0x45, 0x25, 0xef, 0x2a,
1082     0x24, 0x1, 0x3a, 0xf6, 0xf, 0xe, 0xcb, 0x10,
1083     0xc4, 0xe0, 0xf4, 0x3d, 0xf4, 0xf5, 0xea, 0x9b,
1084     0xd1, 0x16, 0x1b, 0x62, 0x11, 0x3e, 0x20, 0x3a,
1085     0x68, 0xc8, 0xf0, 0xe, 0x55, 0xbe, 0x51, 0x4d,
1086     0xbe, 0x1f, 0x4f, 0xda, 0x84, 0xda, 0xc4, 0x9e,
1087     0x24, 0xd7, 0x46, 0x82, 0x56, 0x4e, 0x61, 0x63,
1088     0xda, 0x18, 0xea, 0xc6, 0xc3, 0x21, 0x89, 0x18,
1089     0xe, 0x87, 0xb7, 0x91, 0xfe, 0x8d, 0xe, 0xac,
1090     0x75, 0x58, 0xe5, 0x9f, 0x1f, 0x93, 0xa6, 0x49,
1091     0x24, 0xa2, 0xc6, 0xe8, 0x9d, 0x9c, 0x6d, 0xc1,
1092     0xf, 0xfc, 0xe3, 0x57, 0xd3, 0xc2, 0x10, 0x91,
1093     0x9a, 0xa8, 0xaa, 0xd7, 0xf, 0xaa, 0x75, 0x90,
1094     0x4a, 0x10, 0xef, 0xb6, 0xdd, 0x6c, 0xd5, 0x1a,
1095     0xe3, 0xbb, 0xe0, 0x64, 0x44, 0xc, 0x59, 0xa1,
1096     0xef, 0x3, 0x52, 0xac, 0xa4, 0x85, 0x3e, 0x40,
1097     0xee, 0x5c, 0xef, 0xcf, 0xb1, 0xaa, 0x88, 0xe5,
1098     0x56, 0xb8, 0xcd, 0x87, 0xc7, 0xc6, 0xd3, 0xb4,
1099     0x85, 0x8f, 0x2a, 0xc9, 0xcd, 0x8a, 0x8b, 0x25,
1100     0x12, 0x71, 0x76, 0xc9, 0xaa, 0x62, 0x75, 0x80,
1101     0x6e, 0xa3, 0xf9, 0xa5, 0xfc, 0x90, 0xac, 0x28,
1102     0x13, 0x82, 0xbb, 0x5d, 0xa6, 0x93, 0x47, 0xd4,
1103     0xf, 0x3b, 0x19, 0xf6, 0x81, 0xdb, 0x55, 0xb0,
1104     0x47, 0x75, 0x63, 0x93, 0xb4, 0xdd, 0xf0, 0xaf,
1105     0xb7, 0x44, 0xcb, 0x7, 0x7b, 0x35, 0xc5, 0xe4,
1106     0x45, 0xfe, 0xbb, 0x11, 0x1a, 0x90, 0x96, 0x3a,
1107     0x7, 0x2a, 0xef, 0x9c, 0xc, 0xae, 0x38, 0x26,
1108     0xef, 0xc2, 0xc3, 0x53, 0xfa, 0x54, 0xcf, 0x6f,
1109     0xf7, 0xa, 0xea, 0x19, 0xa8, 0xf, 0xbd, 0xa7,
1110     0x3f, 0xcd, 0x38, 0x2c, 0xf3, 0x97, 0xfb, 0xdb,
1111     0xcb, 0xc5, 0x83, 0x80, 0x91, 0x3d, 0xc7, 0x29,
1112     0x67, 0x16, 0xa5, 0xd1, 0x41, 0xd0, 0xa1, 0x9b,
1113     0xde, 0x13, 0x83, 0x12, 0x36, 0x75, 0x81, 0x71,
1114     0x6b, 0xbc, 0x72, 0xcb, 0x37, 0x4, 0x6, 0x7c,
1115     0x3a, 0x22, 0x2b, 0xa, 0x11, 0xd3, 0x33, 0x8f,
1116     0x3, 0x54, 0x8e, 0x79, 0xb6, 0x36, 0x93, 0x92,
1117     0xb8, 0xf, 0x24, 0x4a, 0xd3, 0xd5, 0x27, 0x66,
1118     0xd1, 0xde, 0xe3, 0xaa, 0x4b, 0x2a, 0xe9, 0x22,
1119     0x9b, 0xbf, 0x6e, 0x9a, 0xf7, 0xa, 0x2f, 0x24,
1120     0x13, 0xd5, 0xd5, 0xbb, 0xa3, 0xba, 0x8f, 0xfc,
1121     0x28, 0xa8, 0xbe, 0xe6, 0x9f, 0xea, 0xed, 0xb1,
1122     0xba, 0xaf, 0xf, 0x1c, 0x1e, 0x51, 0xf8, 0xd7,
1123     0x1b, 0xa5, 0xa6, 0x63, 0x40, 0x6e, 0x3f, 0xa2,
1124     0x57, 0x6f, 0x57, 0xe4, 0x27, 0xc2, 0x3c, 0x33,
1125     0xc6, 0x9c, 0x24, 0xd0, 0x53, 0xc4, 0xfc, 0xed,
1126     0x8e, 0x1d, 0xf, 0xc3, 0x86, 0x9, 0x3d, 0x1d,
1127     0xc2, 0xdb, 0x24, 0x1a, 0x65, 0xf4, 0x30, 0xa5,
1128     0xc, 0x48, 0x37, 0xc5, 0x53, 0x35, 0x3b, 0xab,
1129     0xd, 0x96, 0x30, 0xd7, 0x1d, 0x66, 0x18, 0xc2,
1130     0x47, 0x3a, 0xef, 0xbe, 0x2e, 0xe4, 0x54, 0x9d,
1131     0xc4, 0xa5, 0xb9, 0xb3, 0x4c, 0x12, 0x73, 0x35,
1132     0xf0, 0x7, 0xe, 0x36, 0x88, 0xb2, 0x4b, 0x29,
1133     0xb, 0x4e, 0x84, 0x11, 0xaa, 0x9a, 0x3e, 0xb1,
1134     0xd7, 0xec, 0xfb, 0x7f, 0x10, 0x70, 0x1f, 0x26,
1135     0xf0, 0x27, 0x46, 0x5d, 0x4, 0x51, 0x97, 0x29,
1136     0xb4, 0x66, 0x39, 0x1, 0x82, 0x47, 0xd8, 0x5f,
1137     0xa9, 0xb3, 0xa1, 0xb8, 0xde, 0x1, 0xe1, 0xc4,
1138     0x47, 0xc5, 0xe8, 0xe6, 0xbb, 0xc0, 0xb6, 0x41,
1139     0x55, 0x10, 0x79, 0xa8, 0xd0, 0xd, 0x1, 0x56,
1140     0x29, 0x6c, 0xa5, 0x96, 0x87, 0x59, 0x4b, 0xd,
1141     0xc8, 0x3, 0x5, 0xaa, 0xa9, 0x6a, 0xb1, 0x10,
1142     0xbc, 0x1, 0x68, 0xd3, 0xa5, 0x52, 0x41, 0xe1,
1143     0x1f, 0x53, 0x7, 0xc6, 0xad, 0xb8, 0xc4, 0xf0,
1144     0x28, 0xe9, 0x3, 0x3a, 0xee, 0xce, 0x2c, 0xe2,
1145     0xb0, 0xda, 0x78, 0x3d, 0x37, 0x7, 0x2d, 0x1f,
1146     0xf1, 0x47, 0x81, 0x4, 0x67, 0x6e, 0xd, 0xa1,
1147     0x2b, 0x4, 0xe8, 0xd9, 0xf4, 0xaf, 0x35, 0xca,
1148     0xa5, 0xd1, 0xe3, 0xec, 0xc5, 0x82, 0x50, 0x99,
1149     0x9a, 0xee, 0xea, 0x53, 0x41, 0x86, 0x97, 0x44,
1150     0xeb, 0x58, 0x43, 0x47, 0xe7, 0xa0, 0xd3, 0x28,
1151     0xfc, 0xe7, 0x13, 0x8b, 0x56, 0xe3, 0xdb, 0xa9,
1152     0xcd, 0x9, 0xc8, 0x7, 0x11, 0xeb, 0xbf, 0xac,
1153     0x76, 0x72, 0x60, 0xaf, 0x9c, 0xba, 0x8a, 0x64,
1154     0xfb, 0xf4, 0xab, 0x27, 0x29, 0xe7, 0xec, 0x69,
1155     0x21, 0xcb, 0x5b, 0x79, 0x56, 0x10, 0xc1, 0x8,
1156     0xd5, 0x5d, 0x93, 0xb1, 0x70, 0x88, 0xf2, 0x19,
1157     0x41, 0xc6, 0xc2, 0x84, 0xdd, 0xf0, 0xb3, 0x40,
1158     0x12, 0x71, 0x24, 0x54, 0xc4, 0x5e, 0xfb, 0x5f,
1159     0x47, 0x8c, 0xa9, 0x4, 0x5a, 0xd5, 0x61, 0x19,
1160     0xb5, 0x7f, 0xc9, 0xbd, 0x87, 0xb2, 0xcd, 0x57,
1161     0x99, 0x50, 0x67, 0x1d, 0xb0, 0x1d, 0x82, 0xdd,
1162     0xef, 0x32, 0x38, 0xb9, 0xc7, 0x86, 0xb4, 0xd2,
1163     0xd6, 0xe1, 0x33, 0xb2, 0xdb, 0x5e, 0xc2, 0xa3,
1164     0x49, 0xa6, 0x5f, 0x79, 0x32, 0x50, 0x41, 0x5b,
1165     0xd7, 0x87, 0x74, 0xf5, 0xc9, 0x9c, 0x78, 0xb7,
1166     0xb, 0x1f, 0x72, 0xba, 0xd9, 0x3a, 0x4d, 0x18,
1167     0x45, 0x1d, 0xad, 0xef, 0xc4, 0xdc, 0x30, 0xe8,
1168     0x2, 0xb1, 0x7f, 0x6c, 0x8f, 0xaa, 0xd0, 0x40,
1169     0x17, 0xe, 0x58, 0x93, 0x42, 0x49, 0x63, 0x77,
1170     0x48, 0x55, 0x90, 0x2f, 0x7c, 0x3b, 0xee, 0x3c,
1171     0xac, 0xd, 0xd8, 0x72, 0x23, 0xd7, 0xa5, 0x6e,
1172     0xb0, 0xd2, 0x91, 0x25, 0x60, 0x9a, 0x52, 0xab,
1173     0xbd, 0x63, 0xce, 0xba, 0xda, 0xb1, 0xd7, 0xc7,
1174     0x3d, 0x21, 0x4e, 0x9c, 0x5a, 0x1e, 0x8d, 0xf4,
1175     0xa, 0xdb, 0xd9, 0xf, 0x20, 0x7e, 0xfb, 0xbf,
1176     0x36, 0x9c, 0x4f, 0xbd, 0xf7, 0xdb, 0x5b, 0xa2,
1177     0x6, 0xb2, 0x0, 0xe2, 0xa2, 0x9e, 0x4e, 0x19,
1178     0xd4, 0x69, 0xa9, 0x51, 0x69, 0x8b, 0xf5, 0xe1,
1179     0xad, 0x89, 0x8, 0xc5, 0x4f, 0xac, 0x1b, 0x7d,
1180     0xe7, 0xa, 0x9, 0x7d, 0x34, 0xf5, 0x3f, 0x46,
1181     0x80, 0xb9, 0xb9, 0x45, 0x58, 0xcd, 0x6c, 0xb5,
1182     0x5f, 0x60, 0xeb, 0x5a, 0xe3, 0xa3, 0x8, 0x5e,
1183     0xb1, 0xc4, 0x73, 0xc5, 0xa5, 0x67, 0x56, 0xd3,
1184     0xc6, 0x8a, 0x55, 0x6b, 0xd7, 0xd7, 0xc, 0x20,
1185     0xe6, 0xc, 0x73, 0x8, 0x2, 0x4b, 0xfb, 0xdd,
1186     0x4d, 0x4e, 0xa8, 0xb8, 0xd8, 0x4b, 0x53, 0x2f,
1187     0xc2, 0xfb, 0x5d, 0xa1, 0x6a, 0x16, 0x6b, 0xe,
1188     0xf1, 0xa1, 0xa5, 0x5b, 0xdf, 0x9c, 0x23, 0xb5,
1189     0x94, 0x9c, 0xae, 0x7b, 0xbe, 0x42, 0xb5, 0x79,
1190     0x80, 0xc3, 0x43, 0x41, 0xa4, 0x1b, 0x18, 0xfc,
1191     0x52, 0xcf, 0x43, 0xc5, 0x80, 0x7b, 0xbd, 0xc1,
1192     0x20, 0x5e, 0x65, 0xec, 0xc5, 0xfc, 0x3, 0xec,
1193     0x8f, 0x61, 0x66, 0xf5, 0x15, 0x67, 0xc8, 0xb6,
1194     0xef, 0x9a, 0xba, 0xb7, 0xcb, 0x2c, 0xac, 0x1b,
1195     0x50, 0xda, 0xb6, 0x29, 0xa4, 0x37, 0xe9, 0x96,
1196     0xa0, 0x7, 0x7d, 0x49, 0xa6, 0xce, 0xf3, 0xf0,
1197     0x19, 0xdf, 0x61, 0xc7, 0xa4, 0x7b, 0x5a, 0xd4,
1198     0x99, 0xb2, 0x64, 0xe7, 0xd1, 0x6b, 0x7f, 0xe8,
1199     0xb8, 0xd3, 0x89, 0xee, 0x96, 0xc0, 0xed, 0x5d,
1200     0x7e, 0x48, 0x2, 0xd2, 0x25, 0xd0, 0x5, 0xef,
1201     0x93, 0x72, 0x7c, 0x8c, 0xbd, 0x6e, 0x49, 0xd3,
1202     0x38, 0x46, 0x1c, 0xff, 0x28, 0x4e, 0x1b, 0xad,
1203     0x39, 0x2f, 0x65, 0x26, 0xe2, 0x70, 0x3d, 0xb8,
1204     0x7a, 0xd3, 0x38, 0x38, 0xfc, 0x3a, 0x67, 0x78,
1205     0xdb, 0x9, 0xcb, 0xbf, 0xc9, 0xe1, 0xee, 0x69,
1206     0x2b, 0xd, 0xb1, 0x79, 0x13, 0xd0, 0xa5, 0x75,
1207     0x6, 0x8, 0x79, 0xa7, 0x7c, 0xc, 0xe7, 0x1b,
1208     0x9c, 0x36, 0x64, 0xbe, 0x20, 0x65, 0xa2, 0xd4,
1209     0xd9, 0xc, 0x68, 0xe, 0x88, 0x2b, 0x93, 0x60,
1210     0xf1, 0xa5, 0x82, 0xc5, 0x4d, 0x2b, 0x7d, 0x73,
1211     0xe9, 0x13, 0x8c, 0xc1, 0x8, 0xbd, 0x21, 0x65,
1212     0x77, 0x2f, 0x34, 0xb1, 0x97, 0x9f, 0xd8, 0x55,
1213     0xcf, 0x75, 0xc2, 0xf2, 0x41, 0x68, 0xc1, 0x9c,
1214     0x1c, 0xd7, 0x23, 0xbf, 0x83, 0x2a, 0x9, 0x66,
1215     0xce, 0x8f, 0xd2, 0x12, 0x79, 0x93, 0xef, 0x8,
1216     0x9b, 0xeb, 0x2f, 0xc, 0xe4, 0x5b, 0x71, 0x1a,
1217     0xef, 0x11, 0x65, 0xd8, 0x6d, 0x8c, 0x59, 0x53,
1218     0x70, 0x1d, 0xb5, 0x81, 0xff, 0xc0, 0x7d, 0x87,
1219     0xa5, 0x21, 0x5d, 0x9f, 0x63, 0xb2, 0xe7, 0xe9,
1220     0xd0, 0x49, 0x41, 0xc7, 0x3c, 0xe1, 0x2b, 0xb1,
1221     0xac, 0x15, 0xcd, 0xb0, 0xa8, 0xdc, 0xae, 0x3b,
1222     0xef, 0x32, 0x98, 0x8c, 0xc7, 0x40, 0xa6, 0x81,
1223     0x1, 0xa1, 0x7d, 0x89, 0x46, 0x99, 0x91, 0x24,
1224     0xce, 0xb2, 0x70, 0x82, 0x92, 0xf3, 0x60, 0x66,
1225     0x34, 0x6, 0x37, 0xad, 0x5c, 0xed, 0xc3, 0x27,
1226     0x68, 0x8c, 0x56, 0xe7, 0xf, 0x73, 0x5c, 0x7e,
1227     0x9e, 0xd0, 0x8c, 0x99, 0x5a, 0xb1, 0x15, 0x98,
1228     0xbb, 0x79, 0x9f, 0xd1, 0x69, 0xce, 0x76, 0x5,
1229     0xcb, 0x8e, 0x18, 0xb3, 0x84, 0x65, 0xa9, 0x2,
1230     0xbc, 0x43, 0x8b, 0x7e, 0xe9, 0xe2, 0xe6, 0x74,
1231     0x31, 0x8d, 0xe7, 0xa2, 0x42, 0x8f, 0xca, 0x38,
1232     0x59, 0x85, 0x25, 0x47, 0xd2, 0x86, 0x47, 0x9,
1233     0xc2, 0x11, 0x2, 0x91, 0xe6, 0xf3, 0x47, 0xc2,
1234     0x9c, 0x28, 0x2f, 0xbb, 0xac, 0xde, 0x9f, 0xd,
1235     0xc2, 0x96, 0x4f, 0x43, 0xca, 0x32, 0xed, 0x34,
1236     0xba, 0xad, 0xef, 0xbe, 0x68, 0xc7, 0xa2, 0x83,
1237     0xaf, 0xe, 0xd3, 0x72, 0x52, 0xd1, 0x76, 0x3d,
1238     0x9a, 0x98, 0x39, 0xf4, 0x3e, 0x14, 0x27, 0xff,
1239     0xb2, 0x37, 0x23, 0xc5, 0x6d, 0x66, 0xef, 0xaa,
1240     0xfe, 0xe7, 0xe4, 0x86, 0xa1, 0xe, 0x4e, 0x36,
1241     0x64, 0xb1, 0x67, 0xf, 0x94, 0x6f, 0x77, 0xd5,
1242     0xec, 0xe2, 0x5e, 0xc8, 0xe3, 0x64, 0x29, 0x92,
1243     0xd, 0x20, 0x34, 0x9f, 0x19, 0x6e, 0x85, 0xf8,
1244     0x48, 0x78, 0xb0, 0xf, 0x42, 0xb2, 0x8c, 0xea,
1245     0xc2, 0x4d, 0xd3, 0x23, 0xb, 0x4d, 0x20, 0x33,
1246     0xc7, 0x46, 0x0, 0x45, 0x37, 0xc6, 0xcb, 0xd0,
1247     0xec, 0x11, 0xc6, 0x74, 0x91, 0x7d, 0x6b, 0x54,
1248     0x56, 0x10, 0x8d, 0xd0, 0xce, 0xe8, 0x57, 0x3b,
1249     0x83, 0xd8, 0x25, 0x51, 0x79, 0x48, 0xa, 0xa5,
1250     0xc3, 0xe4, 0x65, 0x33, 0xb2, 0x89, 0xa6, 0x4c,
1251     0xe8, 0xc8, 0x9e, 0xce, 0xea, 0x2a, 0x55, 0x40,
1252     0xfc, 0x26, 0x29, 0xd4, 0x2d, 0x7e, 0xe1, 0xb1,
1253     0x4d, 0x65, 0x1, 0xe9, 0x98, 0xc9, 0xf4, 0x69,
1254     0x10, 0xd9, 0xa3, 0xf9, 0x34, 0xaf, 0x3c, 0x34,
1255     0x64, 0x23, 0xde, 0xb8, 0x1c, 0x33, 0x18, 0x74,
1256     0x67, 0xb4, 0x4a, 0x71, 0xa6, 0x89, 0x2, 0xfe,
1257     0xf7, 0xf1, 0x32, 0xc7, 0x98, 0xad, 0xe5, 0x10,
1258     0x98, 0x3c, 0x6c, 0xaf, 0x1f, 0x13, 0x3d, 0xcc,
1259     0xfc, 0x3b, 0x67, 0x33, 0x34, 0xc9, 0x31, 0xcd,
1260     0x3f, 0xd, 0x3c, 0x5a, 0xb6, 0xc2, 0x8, 0xea,
1261     0xe2, 0xae, 0xdd, 0xfc, 0x6f, 0xca, 0xb5, 0x67,
1262     0x11, 0xce, 0xd5, 0xda, 0x3a, 0x8b, 0x7, 0xf2,
1263     0xc0, 0x9e, 0x78, 0x18, 0x92, 0x9f, 0x64, 0x26,
1264     0x9f, 0x66, 0x62, 0x66, 0xa1, 0x7e, 0x3, 0xf5,
1265     0xb9, 0xe6, 0x74, 0x20, 0x88, 0xb7, 0x7e, 0x62,
1266     0x7a, 0x33, 0x21, 0x9, 0x9c, 0x91, 0x3b, 0x62,
1267     0x9, 0x46, 0xd3, 0xd1, 0x1f, 0xc5, 0x3a, 0x8f,
1268     0x69, 0x27, 0x2c, 0x7b, 0xec, 0xda, 0x79, 0xf1,
1269     0xc9, 0xe9, 0x98, 0xd0, 0xa, 0xc9, 0xf6, 0x37,
1270     0x28, 0xf8, 0xfc, 0xe, 0xdc, 0xf, 0xe9, 0x23,
1271     0xf6, 0x84, 0x25, 0x96, 0x2c, 0x24, 0x14, 0xd7,
1272     0xe2, 0x5e, 0x1c, 0x56, 0x7f, 0x99, 0x98, 0x62,
1273     0x76, 0xcc, 0x84, 0x44, 0xd6, 0xb9, 0x47, 0x2b,
1274     0x52, 0xfb, 0x42, 0x40, 0xf3, 0x63, 0xaf, 0xd4,
1275     0x10, 0x5, 0xf9, 0x3b, 0xc8, 0x53, 0xa9, 0x45,
1276     0xa4, 0x50, 0x41, 0x83, 0xe8, 0x4a, 0x9, 0xb6,
1277     0xf1, 0x77, 0x70, 0xe3, 0x61, 0x30, 0xd8, 0x90,
1278     0x49, 0x52, 0x4b, 0x4a, 0xf2, 0x66, 0x84, 0xaf,
1279     0x71, 0x1, 0x40, 0x66, 0xf6, 0x3, 0xc9, 0x23,
1280     0xb1, 0x1a, 0xc1, 0xb2, 0xf7, 0x35, 0x1a, 0xc9,
1281     0x3a, 0x75, 0xb1, 0xa7, 0x4, 0xff, 0x69, 0xa,
1282     0x90, 0x58, 0xd4, 0xf4, 0x16, 0x79, 0xe1, 0xae,
1283     0x39, 0x9d, 0xbb, 0x32, 0x6b, 0x3, 0xe2, 0xf5,
1284     0x73, 0x83, 0x7e, 0x3c, 0xf8, 0x29, 0xab, 0xcc,
1285     0xdc, 0xf0, 0x13, 0xdb, 0x86, 0x28, 0x88, 0x8e,
1286     0xde, 0x6a, 0x29, 0xf1, 0xea, 0x0, 0x83, 0x97,
1287     0x1, 0x32, 0x5f, 0xaa, 0x5b, 0x1b, 0xe4, 0x87,
1288     0xec, 0x90, 0x45, 0xc7, 0xc5, 0x6c, 0x11, 0x83,
1289     0x95, 0xab, 0xdd, 0x71, 0x69, 0x24, 0xc, 0x5c,
1290     0xc0, 0xf3, 0xc1, 0xb0, 0x5e, 0x1, 0x5e, 0x4,
1291     0xa1, 0x6e, 0x6e, 0x7d, 0x3f, 0x6f, 0xbd, 0x5d,
1292     0x9, 0x8f, 0x23, 0x53, 0x74, 0x4b, 0xa9, 0x53,
1293     0xd2, 0x10, 0xa1, 0xc0, 0x8e, 0x18, 0xa, 0x2f,
1294     0x88, 0x8d, 0x4b, 0xf8, 0xc2, 0x3d, 0xeb, 0x34,
1295     0x23, 0xa, 0x80, 0xc, 0x69, 0x21, 0x3, 0xc1,
1296     0x6f, 0xbe, 0xdf, 0xf6, 0x2c, 0x27, 0x77, 0xa2,
1297     0xc5, 0x5c, 0x9, 0x54, 0x5d, 0x4a, 0x4c, 0xb,
1298     0x6b, 0xb5, 0x88, 0x11, 0x42, 0x62, 0x39, 0x89,
1299     0x9e, 0x36, 0xd3, 0x91, 0xf6, 0x70, 0x18, 0x35,
1300     0x79, 0xaf, 0x73, 0xf3, 0x0, 0x75, 0x5a, 0xa3,
1301     0xce, 0xf1, 0x42, 0x80, 0x19, 0x5e, 0x42, 0x56,
1302     0x53, 0x85, 0xbb, 0xf4, 0x29, 0xac, 0x84, 0x1d,
1303     0x97, 0x1, 0x1c, 0xc4, 0x58, 0xcb, 0x33, 0xc4,
1304     0xdc, 0x1e, 0x59, 0x8f, 0x48, 0xa9, 0x59, 0xfd,
1305     0xaf, 0xa3, 0x5c, 0x19, 0x17, 0x6b, 0x46, 0x2d,
1306     0xab, 0x44, 0xa3, 0xcc, 0x1a, 0xaa, 0x23, 0x4e,
1307     0x58, 0x37, 0x7b, 0x11, 0x14, 0xc2, 0xf1, 0xc9,
1308     0x58, 0x99, 0xd3, 0x3c, 0xec, 0xb9, 0xbe, 0x17,
1309     0x3c, 0x8d, 0x1c, 0x87, 0x9d, 0xe1, 0xb9, 0xad,
1310     0x68, 0x36, 0xd5, 0xfc, 0x24, 0x9b, 0x34, 0x5,
1311     0x26, 0xac, 0x15, 0x9f, 0xd6, 0x70, 0x74, 0x6c,
1312     0x72, 0xf, 0x6, 0x6, 0x5a, 0xc, 0xc0, 0x78,
1313     0x47, 0x8e, 0xcf, 0xf2, 0xce, 0x8, 0xe2, 0xa4,
1314     0xc6, 0x7d, 0x2d, 0x70, 0x14, 0xe2, 0xc6, 0xfc,
1315     0x63, 0x7a, 0x42, 0x8c, 0x45, 0xae, 0xe8, 0x3b,
1316     0x30, 0x48, 0xda, 0x3e, 0x14, 0xb5, 0x8b, 0x10,
1317     0xae, 0x56, 0xbd, 0x17, 0xdf, 0xcb, 0x63, 0xf5,
1318     0xb, 0x2b, 0xd7, 0x34, 0x7c, 0x96, 0x43, 0xe9,
1319     0x17, 0xd4, 0x53, 0x2b, 0x4e, 0xba, 0x61, 0x57,
1320     0x92, 0xdb, 0xe8, 0x37, 0xf4, 0xa3, 0x59, 0x88,
1321     0x74, 0xc2, 0x3c, 0x5d, 0x54, 0x30, 0xb9, 0x6,
1322     0xbe, 0x75, 0x13, 0xe8, 0xf2, 0xe8, 0xcb, 0x45,
1323     0x73, 0x70, 0xaf, 0x94, 0xe6, 0xc5, 0xb0, 0xdf,
1324     0xd2, 0xd5, 0x57, 0x97, 0x7c, 0x97, 0xde, 0x55,
1325     0xaf, 0xbb, 0xed, 0x19, 0x35, 0x17, 0xf4, 0x23,
1326     0x38, 0x9c, 0xce, 0x37, 0xfe, 0xd8, 0x4e, 0xd8,
1327     0x99, 0xba, 0x33, 0x22, 0xf2, 0xeb, 0xab, 0x97,
1328     0xee, 0x9d, 0xab, 0x67, 0x95, 0x35, 0xdf, 0xc8,
1329     0xb6, 0xa0, 0xf, 0x15, 0x51, 0xa9, 0x76, 0x15,
1330     0xdd, 0xbd, 0xac, 0x12, 0xce, 0x51, 0xde, 0x68,
1331     0x15, 0xaf, 0x27, 0xcf, 0xd1, 0xba, 0x7c, 0x17,
1332     0xef, 0xbf, 0xbb, 0xc0, 0x6e, 0x58, 0x73, 0xf6,
1333     0x57, 0xe1, 0x8d, 0xb0, 0x9a, 0x5a, 0x9, 0x19,
1334     0xef, 0xdd, 0x4, 0xe1, 0x76, 0x94, 0x31, 0xd7,
1335     0x26, 0x9f, 0x9c, 0x27, 0xc4, 0x2b, 0x4b, 0xf6,
1336     0x3b, 0xa1, 0x8c, 0xf4, 0x21, 0xde, 0x39, 0x14,
1337     0x5a, 0x54, 0xac, 0x95, 0x2f, 0xa0, 0x60, 0x53,
1338     0x87, 0x5b, 0x71, 0x92, 0xae, 0xf9, 0x6c, 0x62,
1339     0x76, 0x7e, 0x91, 0x11, 0xa6, 0xf4, 0xf2, 0xa8,
1340     0xdf, 0xc1, 0xf6, 0x3a, 0xdb, 0x34, 0x96, 0x9,
1341     0x71, 0xb4, 0x4, 0xfa, 0xd4, 0x3, 0x46, 0x16,
1342     0x78, 0x41, 0x42, 0x7d, 0x15, 0x68, 0x63, 0x55,
1343     0x23, 0x4, 0x46, 0x5d, 0xe1, 0xd8, 0xe7, 0x5f,
1344     0x55, 0x39, 0xd2, 0x45, 0xb2, 0x0, 0x35, 0xde,
1345     0xd8, 0x9d, 0xc7, 0x3a, 0x8f, 0x37, 0x7e, 0xe5,
1346     0x9e, 0xcf, 0xd1, 0x6a, 0x22, 0xe1, 0x51, 0xb2,
1347     0xe6, 0x99, 0x3e, 0x83, 0xeb, 0x34, 0x9d, 0x34,
1348     0x7, 0x1c, 0xbe, 0x91, 0x69, 0x9e, 0xaa, 0xcb,
1349     0x86, 0xd2, 0xb6, 0xed, 0xa5, 0x4, 0xf9, 0x7d,
1350     0xf8, 0xba, 0x2a, 0x27, 0x38, 0xe1, 0xaa, 0x22,
1351     0x94, 0x46, 0x1f, 0x1b, 0xcf, 0xc4, 0x78, 0x88,
1352     0x3d, 0x50, 0x83, 0x30, 0x61, 0x87, 0xb6, 0x38,
1353     0x5b, 0x4f, 0x5a, 0x3, 0x2d, 0x5d, 0xa6, 0x33,
1354     0x38, 0xe7, 0x8b, 0x60, 0x1, 0x8e, 0xde, 0x69,
1355     0x8e, 0x4d, 0x60, 0x24, 0x3b, 0x47, 0x4b, 0x56,
1356     0xea, 0xf9, 0xc8, 0xfa, 0x2d, 0x65, 0x7b, 0xad,
1357     0xee, 0xe4, 0x91, 0x20, 0x6f, 0x64, 0x6e, 0x81,
1358     0x69, 0xda, 0xf5, 0x3c, 0x3d, 0xff, 0x4c, 0xe9,
1359     0x9b, 0x4d, 0xa8, 0x67, 0x9e, 0x67, 0x7f, 0x84,
1360     0xdb, 0x7a, 0xb7, 0x24, 0x32, 0xa0, 0x80, 0x16,
1361     0x55, 0x2d, 0x1d, 0xc1, 0x3a, 0x19, 0xd3, 0x17,
1362     0x74, 0x8e, 0x2a, 0x5c, 0xf6, 0x71, 0xf7, 0x25,
1363     0x3a, 0x54, 0x28, 0xef, 0x50, 0x78, 0x14, 0x5,
1364     0x49, 0x8a, 0xbb, 0x71, 0xb2, 0xed, 0xa2, 0x5b,
1365     0xff, 0x2, 0xe, 0xd8, 0x1a, 0x8b, 0x3c, 0xcc,
1366     0x58, 0x27, 0x71, 0x2d, 0xb, 0x11, 0x9f, 0x6,
1367     0xc3, 0xfd, 0x37, 0x19, 0xdb, 0xec, 0xa5, 0x4b,
1368     0x93, 0x81, 0xb6, 0xff, 0xd4, 0xf5, 0x7b, 0xf5,
1369     0x49, 0x5b, 0x95, 0x9, 0xa4, 0xca, 0xa5, 0x33,
1370     0x9a, 0xfc, 0x97, 0xec, 0x7b, 0xb, 0xb9, 0x2e,
1371     0x3b, 0x9d, 0x52, 0xc2, 0xa2, 0x9, 0xc8, 0xbf,
1372     0x39, 0x16, 0xce, 0x42, 0x3, 0x4b, 0xe3, 0xfc,
1373     0xfd, 0xc, 0x37, 0x96, 0x10, 0x36, 0xad, 0x44,
1374     0xda, 0xc5, 0x58, 0x3e, 0x78, 0x52, 0xa1, 0x65,
1375     0xed, 0x89, 0xe7, 0xea, 0xbf, 0xa8, 0x6a, 0xf2,
1376     0xa7, 0x8e, 0x9d, 0x1, 0x25, 0x83, 0x57, 0x5f,
1377     0x51, 0xe6, 0xe1, 0xa4, 0x4f, 0xf6, 0x81, 0xd7,
1378     0xe6, 0x98, 0x29, 0x98, 0x58, 0xfe, 0xda, 0x45,
1379     0xab, 0x38, 0x6, 0x91, 0x97, 0xb7, 0xa3, 0x4f,
1380     0x93, 0x8d, 0x8a, 0x8b, 0x5, 0xe9, 0x5, 0x98,
1381     0x3b, 0xc4, 0xb7, 0xe1, 0x68, 0x58, 0xa0, 0x3b,
1382     0x99, 0xea, 0x8a, 0xa9, 0xfb, 0x55, 0xe2, 0xc7,
1383     0x1d, 0x87, 0x3, 0x40, 0x24, 0x13, 0x28, 0x6a,
1384     0x34, 0x8a, 0xff, 0x62, 0x91, 0xb8, 0x7d, 0x28,
1385     0x1a, 0xd2, 0xfc, 0x4e, 0xa3, 0xda, 0x66, 0x69,
1386     0x15, 0xc0, 0xda, 0x15, 0x3e, 0x67, 0x12, 0x95,
1387     0x6, 0x1b, 0xf4, 0x60, 0xe4, 0x39, 0x82, 0xe9,
1388     0x2e, 0xbe, 0xab, 0x8c, 0x2c, 0x6e, 0xd6, 0x40,
1389     0x91, 0xc0, 0x68, 0xf7, 0xa2, 0x41, 0xd0, 0xa8,
1390     0x7, 0xab, 0x13, 0x34, 0x16, 0xf4, 0x73, 0x4f,
1391     0x1d, 0x21, 0x1a, 0x7d, 0xad, 0x43, 0x12, 0xf,
1392     0xb7, 0xfe, 0xa3, 0x81, 0xe9, 0xb5, 0x2d, 0xd3,
1393     0xa, 0x29, 0xb5, 0x32, 0xcb, 0x49, 0x6f, 0x1,
1394     0x90, 0x45, 0x62, 0xca, 0x1b, 0x66, 0x39, 0x88,
1395     0x1c, 0xee, 0x30, 0xa8, 0xb5, 0x37, 0xd0, 0xfa,
1396     0x46, 0x52, 0x16, 0x30, 0x17, 0xcf, 0x88, 0xd0,
1397     0x4, 0x5d, 0xde, 0x5e, 0x4f, 0xe7, 0xa9, 0xbf,
1398     0x3c, 0x29, 0x3a, 0x63, 0x67, 0x23, 0xb3, 0x7c,
1399     0x51, 0x17, 0xfe, 0x8d, 0xdb, 0xc8, 0x8d, 0x70,
1400     0xe9, 0x6f, 0x56, 0xe5, 0x44, 0xb2, 0x94, 0xeb,
1401     0x47, 0xca, 0x3a, 0xdc, 0xe3, 0x33, 0x87, 0x9c,
1402     0xe8, 0x89, 0x4b, 0x41, 0xb8, 0xb3, 0x69, 0xb0,
1403     0x7f, 0xc8, 0xc7, 0x74, 0xf5, 0xcb, 0x20, 0xad,
1404     0xea, 0xbb, 0x3d, 0x11, 0xc6, 0xc0, 0xd2, 0x88,
1405     0x8b, 0x16, 0xee, 0x62, 0x5a, 0x4d, 0x32, 0xe7,
1406     0x48, 0xae, 0xab, 0x5e, 0xc2, 0x83, 0xc4, 0xfc,
1407     0xd1, 0xb9, 0x71, 0xf2, 0x9, 0x7f, 0xdc, 0xbc,
1408     0x28, 0x74, 0xa0, 0x37, 0xa9, 0x5b, 0x6c, 0x7c,
1409     0x9b, 0x61, 0x94, 0x88, 0xf7, 0x40, 0x84, 0x75,
1410     0xa5, 0x50, 0xab, 0xb0, 0x92, 0x66, 0x10, 0x66,
1411     0xf6, 0xec, 0x6b, 0x5e, 0x31, 0x9b, 0xc4, 0xfa,
1412     0x95, 0x8b, 0xe7, 0xd4, 0xba, 0x81, 0xd2, 0x85,
1413     0x30, 0x4, 0x8b, 0x3d, 0xfa, 0x8a, 0x8f, 0x9b,
1414     0x54, 0x6a, 0x4d, 0x35, 0xa2, 0xe9, 0x58, 0x95,
1415     0xe3, 0xd1, 0x71, 0xcd, 0x3a, 0x54, 0xae, 0xd9,
1416     0x5c, 0x83, 0xd, 0x15, 0x64, 0x66, 0xee, 0x39,
1417     0xa1, 0x85, 0xe2, 0x28, 0xf5, 0x66, 0x5f, 0xec,
1418     0x39, 0x70, 0x96, 0x2c, 0x72, 0x9e, 0x57, 0xfd,
1419     0x57, 0x27, 0xb7, 0xda, 0x79, 0x39, 0xd8, 0x3b,
1420     0x2e, 0xa3, 0xb0, 0xde, 0xbf, 0x60, 0xb6, 0x42,
1421     0x78, 0x9d, 0x8f, 0xe8, 0x1c, 0x7c, 0x45, 0x72,
1422     0x3, 0xc4, 0xd5, 0x81, 0xf6, 0xe6, 0x9, 0x29,
1423     0x1e, 0xcd, 0xf3, 0xe, 0xd6, 0x65, 0xee, 0x6d,
1424     0x90, 0x17, 0x95, 0x20, 0x54, 0xf1, 0xd, 0x2f,
1425     0xa0, 0xac, 0xe3, 0x4b, 0xfc, 0xa4, 0xdc, 0xab,
1426     0x9d, 0x9e, 0x32, 0x63, 0x72, 0xd1, 0xb4, 0xef,
1427     0xf1, 0x83, 0xa7, 0xd7, 0x2b, 0x1a, 0x9a, 0x9e,
1428     0xfa, 0x1e, 0xb, 0x2b, 0xdc, 0x7b, 0x87, 0x96,
1429     0xf, 0xdb, 0x75, 0xb9, 0x6, 0x2b, 0xd3, 0x95,
1430     0xc5, 0xb3, 0x9, 0x53, 0x94, 0x54, 0x1f, 0xd0,
1431     0x75, 0x5a, 0x36, 0x6a, 0x7c, 0x82, 0xdb, 0xb1,
1432     0xa2, 0x17, 0xbc, 0xeb, 0x1f, 0xfa, 0x34, 0x3d,
1433     0xee, 0x68, 0xee, 0x93, 0x33, 0xfb, 0xcb, 0xd2,
1434     0xa3, 0xd1, 0x24, 0x5e, 0xf4, 0x9, 0xbe, 0x5a,
1435     0x68, 0x9e, 0x3e, 0xd4, 0x81, 0xcd, 0xa3, 0x1e,
1436     0x2, 0x13, 0xb4, 0x79, 0x94, 0xc9, 0xb2, 0xde,
1437     0x56, 0xf1, 0x7b, 0x2f, 0xe2, 0x56, 0xe1, 0x10,
1438     0xf4, 0x73, 0x2d, 0xc9, 0xca, 0x4d, 0x5f, 0x11,
1439     0x9e, 0xd6, 0x3c, 0x73, 0x12, 0x57, 0xe9, 0x14,
1440     0xe0, 0x8d, 0xdd, 0x4b, 0x8a, 0xbb, 0xb3, 0x78,
1441     0xbe, 0x16, 0x94, 0x93, 0x51, 0x33, 0x7a, 0xa5,
1442     0x41, 0x14, 0x60, 0x82, 0x94, 0x67, 0x70, 0xea,
1443     0xe6, 0x3, 0x7f, 0xc5, 0xa0, 0x20, 0x15, 0x88,
1444     0x53, 0xe3, 0x7e, 0x16, 0x52, 0xe4, 0xca, 0xa0,
1445     0x6f, 0xb9, 0x68, 0x4e, 0x30, 0xb9, 0x8c, 0xe6,
1446     0x9c, 0x5e, 0xc2, 0x93, 0xf9, 0xe1, 0x41, 0x4b,
1447     0x18, 0x42, 0x6f, 0x8f, 0x96, 0x3d, 0x2b, 0x28,
1448     0xd5, 0x53, 0x62, 0xdd, 0x6b, 0xd0, 0xf8, 0x2e,
1449     0xa6, 0x97, 0xe5, 0x87, 0xc5, 0xf6, 0x96, 0x7b,
1450     0xc4, 0x3e, 0x84, 0xc9, 0xf6, 0x34, 0x63, 0x46,
1451     0xe1, 0x10, 0xa5, 0x91, 0x6b, 0xff, 0x10, 0x3f,
1452     0x50, 0x2e, 0xd7, 0x39, 0x12, 0x7a, 0x15, 0x85,
1453     0xed, 0x99, 0xdb, 0x9b, 0x99, 0x6b, 0xfa, 0xfa,
1454     0x93, 0x7, 0x44, 0xbe, 0xbe, 0x60, 0x23, 0xc1,
1455     0xec, 0x5c, 0xf6, 0x93, 0x38, 0xf9, 0x89, 0x0,
1456     0xc5, 0x5f, 0x5b, 0xe2, 0x9d, 0x2b, 0xea, 0x6b,
1457     0x2e, 0xee, 0xb7, 0x4a, 0x4e, 0x8d, 0xd0, 0x35,
1458     0xe9, 0xc1, 0x5, 0x2b, 0x83, 0xb7, 0x72, 0x25,
1459     0xbb, 0xbe, 0xe8, 0x15, 0xf4, 0x74, 0x69, 0x69,
1460     0x67, 0x8c, 0x5c, 0x31, 0x79, 0x78, 0x2e, 0x43,
1461     0x83, 0xd1, 0xdd, 0x9, 0xc3, 0xa1, 0x0, 0x13,
1462     0x31, 0x4b, 0x86, 0xce, 0xee, 0xd7, 0xec, 0xb1,
1463     0x2c, 0x38, 0x46, 0x68, 0x62, 0xd9, 0x84, 0xdb,
1464     0x24, 0x62, 0x82, 0xc, 0x12, 0xb7, 0x4f, 0x86,
1465     0x54, 0x18, 0xc6, 0xd7, 0x94, 0x8b, 0xf2, 0x4c,
1466     0x17, 0x98, 0xaa, 0xe0,
1467 };
1468 
1469 const uint8_t expected_cipher_long_input_end[] = {
1470     0x05, 0x95, 0x58, 0x7b, 0xb4, 0x60, 0x15,
1471     0x32, 0x9f, 0x38, 0xcc, 0x98, 0x1b, 0xbe, 0x10, 0xa5, 0x06, 0x67, 0xae, 0x38,
1472     0xbd, 0x7d, 0xb5, 0xcd, 0x58, 0x32, 0xdd, 0x9e,
1473     0x6a, 0xde, 0xe3, 0x53,
1474 };
1475 
aes_ext_flash_ctr_test(uint32_t output_buf_caps)1476 void aes_ext_flash_ctr_test(uint32_t output_buf_caps)
1477 {
1478     mbedtls_aes_context ctx;
1479     uint8_t nonce[16];
1480     uint8_t key[16];
1481     uint8_t stream_block[16];
1482     size_t SZ = sizeof(long_input);
1483     memset(nonce, 0x2F, 16);
1484     memset(key, 0x1E, 16);
1485 
1486     uint8_t *chipertext = heap_caps_malloc(SZ, output_buf_caps);
1487     uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
1488 
1489     TEST_ASSERT_NOT_NULL(chipertext);
1490     TEST_ASSERT_NOT_NULL(decryptedtext);
1491 
1492     mbedtls_aes_init(&ctx);
1493     mbedtls_aes_setkey_enc(&ctx, key, 128);
1494 
1495     size_t offset;
1496 
1497     // Encrypt with input buffer in external flash
1498     offset = 0;
1499     memset(nonce, 0x2F, 16);
1500     mbedtls_aes_crypt_ctr(&ctx, SZ, &offset, nonce, stream_block, long_input, chipertext);
1501     TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_long_input_end, chipertext + SZ - 32, 32);
1502 
1503     // Decrypt
1504     offset = 0;
1505     memset(nonce, 0x2F, 16);
1506     // Decrypt with input buffer in external flash, the crypto DMA can't access this
1507     mbedtls_aes_crypt_ctr(&ctx, SZ, &offset, nonce, stream_block, chipertext, decryptedtext);
1508 
1509     TEST_ASSERT_EQUAL_HEX8_ARRAY(long_input, decryptedtext, SZ);
1510 
1511     mbedtls_aes_free(&ctx);
1512     free(chipertext);
1513     free(decryptedtext);
1514 }
1515 
1516 /* Tests how crypto DMA handles data in external memory */
1517 TEST_CASE("mbedtls AES PSRAM tests", "[aes]")
1518 {
1519     aes_ctr_alignment_test(MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
1520     aes_ctr_alignment_test(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
1521     aes_ctr_alignment_test(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
1522     aes_psram_one_buf_ctr_test();
1523 }
1524 
1525 /* Tests how crypto DMA handles data from external flash */
1526 TEST_CASE("mbedtls AES external flash tests", "[aes]")
1527 {
1528     aes_ext_flash_ctr_test(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
1529     aes_ext_flash_ctr_test(MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
1530 }
1531 #endif // CONFIG_SPIRAM_USE_MALLOC
1532 
1533 
1534 static SemaphoreHandle_t done_sem;
1535 
aes_ctr_stream_test_task(void * pv)1536 static void __attribute__((unused)) aes_ctr_stream_test_task(void *pv)
1537 {
1538     aes_ctr_stream_test();
1539     xSemaphoreGive(done_sem);
1540     vTaskDelete(NULL);
1541 }
1542 
1543 #if CONFIG_ESP_SYSTEM_RTC_FAST_MEM_AS_HEAP_DEPCHECK
1544 
1545 TEST_CASE("mbedtls AES stack in RTC RAM", "[mbedtls]")
1546 {
1547     done_sem = xSemaphoreCreateBinary();
1548     static StaticTask_t rtc_task;
1549     size_t STACK_SIZE = 3072;
1550     uint8_t *rtc_stack = heap_caps_calloc(STACK_SIZE, 1, MALLOC_CAP_RTCRAM);
1551     TEST_ASSERT(esp_ptr_in_rtc_dram_fast(rtc_stack));
1552 
1553     TEST_ASSERT_NOT_NULL(xTaskCreateStatic(aes_ctr_stream_test_task, "aes_ctr_task", STACK_SIZE, NULL,
1554                                             3, rtc_stack, &rtc_task));
1555     TEST_ASSERT_TRUE(xSemaphoreTake(done_sem, 10000 / portTICK_PERIOD_MS));
1556 
1557     /* Give task time to cleanup before freeing stack */
1558     vTaskDelay(1000 / portTICK_PERIOD_MS);
1559     free(rtc_stack);
1560 
1561     vSemaphoreDelete(done_sem);
1562 }
1563 
1564 #endif //CONFIG_ESP_SYSTEM_RTC_FAST_MEM_AS_HEAP_DEPCHECK
1565 
1566 #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY && CONFIG_SPIRAM_USE_MALLOC
1567 
1568 TEST_CASE("mbedtls AES stack in PSRAM", "[mbedtls]")
1569 {
1570     done_sem = xSemaphoreCreateBinary();
1571     static StaticTask_t psram_task;
1572     size_t STACK_SIZE = 3072;
1573     uint8_t *psram_stack = heap_caps_calloc(STACK_SIZE, 1, MALLOC_CAP_SPIRAM);
1574 
1575     TEST_ASSERT(esp_ptr_external_ram(psram_stack));
1576 
1577     TEST_ASSERT_NOT_NULL(xTaskCreateStatic(aes_ctr_stream_test_task, "aes_ctr_task", STACK_SIZE, NULL,
1578                                             3, psram_stack, &psram_task));
1579     TEST_ASSERT_TRUE(xSemaphoreTake(done_sem, 10000 / portTICK_PERIOD_MS));
1580 
1581     /* Give task time to cleanup before freeing stack */
1582     vTaskDelay(1000 / portTICK_PERIOD_MS);
1583     free(psram_stack);
1584 
1585     vSemaphoreDelete(done_sem);
1586 }
1587 
1588 #endif //CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY && CONFIG_SPIRAM_USE_MALLOC
1589