test_aes.c 58 KB

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