test_aes.c 59 KB

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