test_aes.c 60 KB

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