test_suite_aes.function 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/aes.h"
  3. /* Test AES with a copied context.
  4. *
  5. * master, enc and dec must be AES context objects. They don't need to
  6. * be initialized, and are left freed.
  7. */
  8. static int test_copy(const data_t *key,
  9. mbedtls_aes_context *master,
  10. mbedtls_aes_context *enc,
  11. mbedtls_aes_context *dec)
  12. {
  13. unsigned char plaintext[16] = {
  14. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  16. };
  17. unsigned char ciphertext[16];
  18. unsigned char output[16];
  19. // Set key and encrypt with original context
  20. mbedtls_aes_init(master);
  21. TEST_ASSERT(mbedtls_aes_setkey_enc(master, key->x,
  22. key->len * 8) == 0);
  23. TEST_ASSERT(mbedtls_aes_crypt_ecb(master, MBEDTLS_AES_ENCRYPT,
  24. plaintext, ciphertext) == 0);
  25. *enc = *master;
  26. // Set key for decryption with original context
  27. mbedtls_aes_init(master);
  28. TEST_ASSERT(mbedtls_aes_setkey_dec(master, key->x,
  29. key->len * 8) == 0);
  30. *dec = *master;
  31. // Wipe the original context to make sure nothing from it is used
  32. memset(master, 0, sizeof(*master));
  33. // Encrypt with copied context
  34. TEST_ASSERT(mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT,
  35. plaintext, output) == 0);
  36. ASSERT_COMPARE(ciphertext, 16, output, 16);
  37. mbedtls_aes_free(enc);
  38. // Decrypt with copied context
  39. TEST_ASSERT(mbedtls_aes_crypt_ecb(dec, MBEDTLS_AES_DECRYPT,
  40. ciphertext, output) == 0);
  41. ASSERT_COMPARE(plaintext, 16, output, 16);
  42. mbedtls_aes_free(dec);
  43. return 1;
  44. exit:
  45. /* Bug: we may be leaving something unfreed. This is harmless
  46. * in our built-in implementations, but might cause a memory leak
  47. * with alternative implementations. */
  48. return 0;
  49. }
  50. /* END_HEADER */
  51. /* BEGIN_DEPENDENCIES
  52. * depends_on:MBEDTLS_AES_C
  53. * END_DEPENDENCIES
  54. */
  55. /* BEGIN_CASE */
  56. void aes_encrypt_ecb(data_t *key_str, data_t *src_str,
  57. data_t *dst, int setkey_result)
  58. {
  59. unsigned char output[100];
  60. mbedtls_aes_context ctx;
  61. memset(output, 0x00, 100);
  62. mbedtls_aes_init(&ctx);
  63. TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == setkey_result);
  64. if (setkey_result == 0) {
  65. TEST_ASSERT(mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT, src_str->x, output) == 0);
  66. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
  67. }
  68. exit:
  69. mbedtls_aes_free(&ctx);
  70. }
  71. /* END_CASE */
  72. /* BEGIN_CASE */
  73. void aes_decrypt_ecb(data_t *key_str, data_t *src_str,
  74. data_t *dst, int setkey_result)
  75. {
  76. unsigned char output[100];
  77. mbedtls_aes_context ctx;
  78. memset(output, 0x00, 100);
  79. mbedtls_aes_init(&ctx);
  80. TEST_ASSERT(mbedtls_aes_setkey_dec(&ctx, key_str->x, key_str->len * 8) == setkey_result);
  81. if (setkey_result == 0) {
  82. TEST_ASSERT(mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_DECRYPT, src_str->x, output) == 0);
  83. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
  84. }
  85. exit:
  86. mbedtls_aes_free(&ctx);
  87. }
  88. /* END_CASE */
  89. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  90. void aes_encrypt_cbc(data_t *key_str, data_t *iv_str,
  91. data_t *src_str, data_t *dst,
  92. int cbc_result)
  93. {
  94. unsigned char output[100];
  95. mbedtls_aes_context ctx;
  96. memset(output, 0x00, 100);
  97. mbedtls_aes_init(&ctx);
  98. TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
  99. TEST_ASSERT(mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x,
  100. src_str->x, output) == cbc_result);
  101. if (cbc_result == 0) {
  102. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
  103. src_str->len, dst->len) == 0);
  104. }
  105. exit:
  106. mbedtls_aes_free(&ctx);
  107. }
  108. /* END_CASE */
  109. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  110. void aes_decrypt_cbc(data_t *key_str, data_t *iv_str,
  111. data_t *src_str, data_t *dst,
  112. int cbc_result)
  113. {
  114. unsigned char output[100];
  115. mbedtls_aes_context ctx;
  116. memset(output, 0x00, 100);
  117. mbedtls_aes_init(&ctx);
  118. TEST_ASSERT(mbedtls_aes_setkey_dec(&ctx, key_str->x, key_str->len * 8) == 0);
  119. TEST_ASSERT(mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x,
  120. src_str->x, output) == cbc_result);
  121. if (cbc_result == 0) {
  122. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
  123. src_str->len, dst->len) == 0);
  124. }
  125. exit:
  126. mbedtls_aes_free(&ctx);
  127. }
  128. /* END_CASE */
  129. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
  130. void aes_encrypt_xts(char *hex_key_string, char *hex_data_unit_string,
  131. char *hex_src_string, char *hex_dst_string)
  132. {
  133. enum { AES_BLOCK_SIZE = 16 };
  134. unsigned char *data_unit = NULL;
  135. unsigned char *key = NULL;
  136. unsigned char *src = NULL;
  137. unsigned char *dst = NULL;
  138. unsigned char *output = NULL;
  139. mbedtls_aes_xts_context ctx;
  140. size_t key_len, src_len, dst_len, data_unit_len;
  141. mbedtls_aes_xts_init(&ctx);
  142. data_unit = mbedtls_test_unhexify_alloc(hex_data_unit_string,
  143. &data_unit_len);
  144. TEST_ASSERT(data_unit_len == AES_BLOCK_SIZE);
  145. key = mbedtls_test_unhexify_alloc(hex_key_string, &key_len);
  146. TEST_ASSERT(key_len % 2 == 0);
  147. src = mbedtls_test_unhexify_alloc(hex_src_string, &src_len);
  148. dst = mbedtls_test_unhexify_alloc(hex_dst_string, &dst_len);
  149. TEST_ASSERT(src_len == dst_len);
  150. output = mbedtls_test_zero_alloc(dst_len);
  151. TEST_ASSERT(mbedtls_aes_xts_setkey_enc(&ctx, key, key_len * 8) == 0);
  152. TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, src_len,
  153. data_unit, src, output) == 0);
  154. TEST_ASSERT(memcmp(output, dst, dst_len) == 0);
  155. exit:
  156. mbedtls_aes_xts_free(&ctx);
  157. mbedtls_free(data_unit);
  158. mbedtls_free(key);
  159. mbedtls_free(src);
  160. mbedtls_free(dst);
  161. mbedtls_free(output);
  162. }
  163. /* END_CASE */
  164. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
  165. void aes_decrypt_xts(char *hex_key_string, char *hex_data_unit_string,
  166. char *hex_dst_string, char *hex_src_string)
  167. {
  168. enum { AES_BLOCK_SIZE = 16 };
  169. unsigned char *data_unit = NULL;
  170. unsigned char *key = NULL;
  171. unsigned char *src = NULL;
  172. unsigned char *dst = NULL;
  173. unsigned char *output = NULL;
  174. mbedtls_aes_xts_context ctx;
  175. size_t key_len, src_len, dst_len, data_unit_len;
  176. mbedtls_aes_xts_init(&ctx);
  177. data_unit = mbedtls_test_unhexify_alloc(hex_data_unit_string,
  178. &data_unit_len);
  179. TEST_ASSERT(data_unit_len == AES_BLOCK_SIZE);
  180. key = mbedtls_test_unhexify_alloc(hex_key_string, &key_len);
  181. TEST_ASSERT(key_len % 2 == 0);
  182. src = mbedtls_test_unhexify_alloc(hex_src_string, &src_len);
  183. dst = mbedtls_test_unhexify_alloc(hex_dst_string, &dst_len);
  184. TEST_ASSERT(src_len == dst_len);
  185. output = mbedtls_test_zero_alloc(dst_len);
  186. TEST_ASSERT(mbedtls_aes_xts_setkey_dec(&ctx, key, key_len * 8) == 0);
  187. TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_DECRYPT, src_len,
  188. data_unit, src, output) == 0);
  189. TEST_ASSERT(memcmp(output, dst, dst_len) == 0);
  190. exit:
  191. mbedtls_aes_xts_free(&ctx);
  192. mbedtls_free(data_unit);
  193. mbedtls_free(key);
  194. mbedtls_free(src);
  195. mbedtls_free(dst);
  196. mbedtls_free(output);
  197. }
  198. /* END_CASE */
  199. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
  200. void aes_crypt_xts_size(int size, int retval)
  201. {
  202. mbedtls_aes_xts_context ctx;
  203. const unsigned char src[16] = { 0 };
  204. unsigned char output[16];
  205. unsigned char data_unit[16];
  206. size_t length = size;
  207. mbedtls_aes_xts_init(&ctx);
  208. memset(data_unit, 0x00, sizeof(data_unit));
  209. TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src,
  210. output) == retval);
  211. exit:
  212. mbedtls_aes_xts_free(&ctx);
  213. }
  214. /* END_CASE */
  215. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
  216. void aes_crypt_xts_keysize(int size, int retval)
  217. {
  218. mbedtls_aes_xts_context ctx;
  219. const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
  220. size_t key_len = size;
  221. mbedtls_aes_xts_init(&ctx);
  222. TEST_ASSERT(mbedtls_aes_xts_setkey_enc(&ctx, key, key_len * 8) == retval);
  223. TEST_ASSERT(mbedtls_aes_xts_setkey_dec(&ctx, key, key_len * 8) == retval);
  224. exit:
  225. mbedtls_aes_xts_free(&ctx);
  226. }
  227. /* END_CASE */
  228. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  229. void aes_encrypt_cfb128(data_t *key_str, data_t *iv_str,
  230. data_t *src_str, data_t *dst)
  231. {
  232. unsigned char output[100];
  233. mbedtls_aes_context ctx;
  234. size_t iv_offset = 0;
  235. memset(output, 0x00, 100);
  236. mbedtls_aes_init(&ctx);
  237. TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
  238. TEST_ASSERT(mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x,
  239. src_str->x, output) == 0);
  240. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
  241. exit:
  242. mbedtls_aes_free(&ctx);
  243. }
  244. /* END_CASE */
  245. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  246. void aes_decrypt_cfb128(data_t *key_str, data_t *iv_str,
  247. data_t *src_str, data_t *dst)
  248. {
  249. unsigned char output[100];
  250. mbedtls_aes_context ctx;
  251. size_t iv_offset = 0;
  252. memset(output, 0x00, 100);
  253. mbedtls_aes_init(&ctx);
  254. TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
  255. TEST_ASSERT(mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x,
  256. src_str->x, output) == 0);
  257. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
  258. exit:
  259. mbedtls_aes_free(&ctx);
  260. }
  261. /* END_CASE */
  262. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  263. void aes_encrypt_cfb8(data_t *key_str, data_t *iv_str,
  264. data_t *src_str, data_t *dst)
  265. {
  266. unsigned char output[100];
  267. mbedtls_aes_context ctx;
  268. memset(output, 0x00, 100);
  269. mbedtls_aes_init(&ctx);
  270. TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
  271. TEST_ASSERT(mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x,
  272. src_str->x, output) == 0);
  273. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
  274. src_str->len, dst->len) == 0);
  275. exit:
  276. mbedtls_aes_free(&ctx);
  277. }
  278. /* END_CASE */
  279. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  280. void aes_decrypt_cfb8(data_t *key_str, data_t *iv_str,
  281. data_t *src_str, data_t *dst)
  282. {
  283. unsigned char output[100];
  284. mbedtls_aes_context ctx;
  285. memset(output, 0x00, 100);
  286. mbedtls_aes_init(&ctx);
  287. TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
  288. TEST_ASSERT(mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x,
  289. src_str->x, output) == 0);
  290. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
  291. src_str->len, dst->len) == 0);
  292. exit:
  293. mbedtls_aes_free(&ctx);
  294. }
  295. /* END_CASE */
  296. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
  297. void aes_encrypt_ofb(int fragment_size, data_t *key_str,
  298. data_t *iv_str, data_t *src_str,
  299. data_t *expected_output)
  300. {
  301. unsigned char output[32];
  302. mbedtls_aes_context ctx;
  303. size_t iv_offset = 0;
  304. int in_buffer_len;
  305. unsigned char *src_str_next;
  306. memset(output, 0x00, sizeof(output));
  307. mbedtls_aes_init(&ctx);
  308. TEST_ASSERT((size_t) fragment_size < sizeof(output));
  309. TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x,
  310. key_str->len * 8) == 0);
  311. in_buffer_len = src_str->len;
  312. src_str_next = src_str->x;
  313. while (in_buffer_len > 0) {
  314. TEST_ASSERT(mbedtls_aes_crypt_ofb(&ctx, fragment_size, &iv_offset,
  315. iv_str->x, src_str_next, output) == 0);
  316. TEST_ASSERT(memcmp(output, expected_output->x, fragment_size) == 0);
  317. in_buffer_len -= fragment_size;
  318. expected_output->x += fragment_size;
  319. src_str_next += fragment_size;
  320. if (in_buffer_len < fragment_size) {
  321. fragment_size = in_buffer_len;
  322. }
  323. }
  324. exit:
  325. mbedtls_aes_free(&ctx);
  326. }
  327. /* END_CASE */
  328. /* BEGIN_CASE */
  329. void aes_invalid_mode()
  330. {
  331. mbedtls_aes_context aes_ctx;
  332. const unsigned char in[16] = { 0 };
  333. unsigned char out[16];
  334. const int invalid_mode = 42;
  335. TEST_EQUAL(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  336. mbedtls_aes_crypt_ecb(&aes_ctx, invalid_mode, in, out));
  337. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  338. TEST_EQUAL(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  339. mbedtls_aes_crypt_cbc(&aes_ctx, invalid_mode, 16,
  340. out, in, out));
  341. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  342. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  343. mbedtls_aes_xts_context xts_ctx;
  344. TEST_EQUAL(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  345. mbedtls_aes_crypt_xts(&xts_ctx, invalid_mode, 16,
  346. in, in, out));
  347. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  348. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  349. size_t size;
  350. TEST_EQUAL(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  351. mbedtls_aes_crypt_cfb128(&aes_ctx, invalid_mode, 16,
  352. &size, out, in, out));
  353. TEST_EQUAL(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  354. mbedtls_aes_crypt_cfb8(&aes_ctx, invalid_mode, 16,
  355. out, in, out));
  356. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  357. }
  358. /* END_CASE */
  359. /* BEGIN_CASE */
  360. void aes_misc_params()
  361. {
  362. #if defined(MBEDTLS_CIPHER_MODE_CBC) || \
  363. defined(MBEDTLS_CIPHER_MODE_XTS) || \
  364. defined(MBEDTLS_CIPHER_MODE_CFB) || \
  365. defined(MBEDTLS_CIPHER_MODE_OFB)
  366. const unsigned char in[16] = { 0 };
  367. unsigned char out[16];
  368. #endif
  369. #if defined(MBEDTLS_CIPHER_MODE_CBC) || \
  370. defined(MBEDTLS_CIPHER_MODE_CFB) || \
  371. defined(MBEDTLS_CIPHER_MODE_OFB)
  372. mbedtls_aes_context aes_ctx;
  373. #endif
  374. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  375. mbedtls_aes_xts_context xts_ctx;
  376. #endif
  377. #if defined(MBEDTLS_CIPHER_MODE_CFB) || \
  378. defined(MBEDTLS_CIPHER_MODE_OFB)
  379. size_t size;
  380. #endif
  381. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  382. TEST_ASSERT(mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT,
  383. 15,
  384. out, in, out)
  385. == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
  386. TEST_ASSERT(mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT,
  387. 17,
  388. out, in, out)
  389. == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
  390. #endif
  391. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  392. TEST_ASSERT(mbedtls_aes_crypt_xts(&xts_ctx, MBEDTLS_AES_ENCRYPT,
  393. 15,
  394. in, in, out)
  395. == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
  396. TEST_ASSERT(mbedtls_aes_crypt_xts(&xts_ctx, MBEDTLS_AES_ENCRYPT,
  397. (1 << 24) + 1,
  398. in, in, out)
  399. == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
  400. #endif
  401. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  402. size = 16;
  403. TEST_ASSERT(mbedtls_aes_crypt_cfb128(&aes_ctx, MBEDTLS_AES_ENCRYPT, 16,
  404. &size, out, in, out)
  405. == MBEDTLS_ERR_AES_BAD_INPUT_DATA);
  406. #endif
  407. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  408. size = 16;
  409. TEST_ASSERT(mbedtls_aes_crypt_ofb(&aes_ctx, 16, &size, out, in, out)
  410. == MBEDTLS_ERR_AES_BAD_INPUT_DATA);
  411. #endif
  412. /*
  413. * The following line needs to be added to make the code compilable
  414. * when all the conditions above will be not define in a specific
  415. * choice of features.
  416. */
  417. TEST_ASSERT(1);
  418. /* TODO: It will be removed when the whole test will be reworked */
  419. }
  420. /* END_CASE */
  421. /* BEGIN_CASE */
  422. void aes_ecb_copy_context(data_t *key)
  423. {
  424. /* We test context copying multiple times, with different alignments
  425. * of the original and of the copies. */
  426. struct align0 {
  427. mbedtls_aes_context ctx;
  428. };
  429. struct align0 *src0 = NULL;
  430. struct align0 *enc0 = NULL;
  431. struct align0 *dec0 = NULL;
  432. struct align1 {
  433. char bump;
  434. mbedtls_aes_context ctx;
  435. };
  436. struct align1 *src1 = NULL;
  437. struct align1 *enc1 = NULL;
  438. struct align1 *dec1 = NULL;
  439. /* All peak alignment */
  440. ASSERT_ALLOC(src0, 1);
  441. ASSERT_ALLOC(enc0, 1);
  442. ASSERT_ALLOC(dec0, 1);
  443. if (!test_copy(key, &src0->ctx, &enc0->ctx, &dec0->ctx)) {
  444. goto exit;
  445. }
  446. mbedtls_free(src0);
  447. src0 = NULL;
  448. mbedtls_free(enc0);
  449. enc0 = NULL;
  450. mbedtls_free(dec0);
  451. dec0 = NULL;
  452. /* Original shifted */
  453. ASSERT_ALLOC(src1, 1);
  454. ASSERT_ALLOC(enc0, 1);
  455. ASSERT_ALLOC(dec0, 1);
  456. if (!test_copy(key, &src1->ctx, &enc0->ctx, &dec0->ctx)) {
  457. goto exit;
  458. }
  459. mbedtls_free(src1);
  460. src1 = NULL;
  461. mbedtls_free(enc0);
  462. enc0 = NULL;
  463. mbedtls_free(dec0);
  464. dec0 = NULL;
  465. /* Copies shifted */
  466. ASSERT_ALLOC(src0, 1);
  467. ASSERT_ALLOC(enc1, 1);
  468. ASSERT_ALLOC(dec1, 1);
  469. if (!test_copy(key, &src0->ctx, &enc1->ctx, &dec1->ctx)) {
  470. goto exit;
  471. }
  472. mbedtls_free(src0);
  473. src0 = NULL;
  474. mbedtls_free(enc1);
  475. enc1 = NULL;
  476. mbedtls_free(dec1);
  477. dec1 = NULL;
  478. /* Source and copies shifted */
  479. ASSERT_ALLOC(src1, 1);
  480. ASSERT_ALLOC(enc1, 1);
  481. ASSERT_ALLOC(dec1, 1);
  482. if (!test_copy(key, &src1->ctx, &enc1->ctx, &dec1->ctx)) {
  483. goto exit;
  484. }
  485. mbedtls_free(src1);
  486. src1 = NULL;
  487. mbedtls_free(enc1);
  488. enc1 = NULL;
  489. mbedtls_free(dec1);
  490. dec1 = NULL;
  491. exit:
  492. mbedtls_free(src0);
  493. mbedtls_free(enc0);
  494. mbedtls_free(dec0);
  495. mbedtls_free(src1);
  496. mbedtls_free(enc1);
  497. mbedtls_free(dec1);
  498. }
  499. /* END_CASE */
  500. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  501. void aes_selftest()
  502. {
  503. TEST_ASSERT(mbedtls_aes_self_test(1) == 0);
  504. }
  505. /* END_CASE */