test_aes.c 62 KB

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