test_suite_cipher.function 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/cipher.h"
  3. #include "mbedtls/aes.h"
  4. #if defined(MBEDTLS_GCM_C)
  5. #include "mbedtls/gcm.h"
  6. #endif
  7. #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
  8. #define MBEDTLS_CIPHER_AUTH_CRYPT
  9. #endif
  10. /* Check the internal consistency of a cipher info structure, and
  11. * check it against mbedtls_cipher_info_from_xxx(). */
  12. static int check_cipher_info(mbedtls_cipher_type_t type,
  13. const mbedtls_cipher_info_t *info)
  14. {
  15. size_t key_bitlen, block_size, iv_size;
  16. TEST_ASSERT(info != NULL);
  17. TEST_EQUAL(type, mbedtls_cipher_info_get_type(info));
  18. TEST_EQUAL(type, info->type);
  19. TEST_ASSERT(mbedtls_cipher_info_from_type(type) == info);
  20. TEST_EQUAL(info->mode, mbedtls_cipher_info_get_mode(info));
  21. /* Insist that get_name() return the string from the structure and
  22. * not a copy. A copy would have an unknown storage duration. */
  23. TEST_ASSERT(mbedtls_cipher_info_get_name(info) == info->name);
  24. TEST_ASSERT(mbedtls_cipher_info_from_string(info->name) == info);
  25. key_bitlen = mbedtls_cipher_info_get_key_bitlen(info);
  26. block_size = mbedtls_cipher_info_get_block_size(info);
  27. iv_size = mbedtls_cipher_info_get_iv_size(info);
  28. if (info->type == MBEDTLS_CIPHER_NULL) {
  29. TEST_ASSERT(key_bitlen == 0);
  30. TEST_ASSERT(block_size == 1);
  31. TEST_ASSERT(iv_size == 0);
  32. } else if (info->mode == MBEDTLS_MODE_XTS) {
  33. TEST_ASSERT(key_bitlen == 256 ||
  34. key_bitlen == 384 ||
  35. key_bitlen == 512);
  36. } else if (!strncmp(info->name, "DES-EDE3-", 9)) {
  37. TEST_ASSERT(key_bitlen == 192);
  38. TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
  39. TEST_ASSERT(block_size == 8);
  40. } else if (!strncmp(info->name, "DES-EDE-", 8)) {
  41. TEST_ASSERT(key_bitlen == 128);
  42. TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
  43. TEST_ASSERT(block_size == 8);
  44. } else if (!strncmp(info->name, "DES-", 4)) {
  45. TEST_ASSERT(key_bitlen == 64);
  46. TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
  47. TEST_ASSERT(block_size == 8);
  48. } else if (!strncmp(info->name, "AES", 3)) {
  49. TEST_ASSERT(key_bitlen == 128 ||
  50. key_bitlen == 192 ||
  51. key_bitlen == 256);
  52. TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
  53. TEST_ASSERT(block_size == 16);
  54. } else {
  55. TEST_ASSERT(key_bitlen == 128 ||
  56. key_bitlen == 192 ||
  57. key_bitlen == 256);
  58. }
  59. if (strstr(info->name, "-ECB") != NULL) {
  60. TEST_ASSERT(iv_size == 0);
  61. TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
  62. } else if (strstr(info->name, "-CBC") != NULL ||
  63. strstr(info->name, "-CTR") != NULL) {
  64. TEST_ASSERT(iv_size == block_size);
  65. TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
  66. } else if (strstr(info->name, "-GCM") != NULL) {
  67. TEST_ASSERT(iv_size == block_size - 4);
  68. TEST_ASSERT(mbedtls_cipher_info_has_variable_iv_size(info));
  69. }
  70. return 1;
  71. exit:
  72. return 0;
  73. }
  74. #if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
  75. /* Helper for resetting key/direction
  76. *
  77. * The documentation doesn't explicitly say whether calling
  78. * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
  79. * the default software implementation, but only by accident. It isn't
  80. * guaranteed to work with new ciphers or with alternative implementations of
  81. * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
  82. * it, and instead start with a fresh context.
  83. */
  84. static int cipher_reset_key(mbedtls_cipher_context_t *ctx, int cipher_id,
  85. int use_psa, size_t tag_len, const data_t *key, int direction)
  86. {
  87. mbedtls_cipher_free(ctx);
  88. mbedtls_cipher_init(ctx);
  89. #if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
  90. (void) use_psa;
  91. (void) tag_len;
  92. #else
  93. if (use_psa == 1) {
  94. TEST_ASSERT(0 == mbedtls_cipher_setup_psa(ctx,
  95. mbedtls_cipher_info_from_type(cipher_id),
  96. tag_len));
  97. } else
  98. #endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
  99. {
  100. TEST_ASSERT(0 == mbedtls_cipher_setup(ctx,
  101. mbedtls_cipher_info_from_type(cipher_id)));
  102. }
  103. TEST_ASSERT(0 == mbedtls_cipher_setkey(ctx, key->x, 8 * key->len,
  104. direction));
  105. return 1;
  106. exit:
  107. return 0;
  108. }
  109. /*
  110. * Check if a buffer is all-0 bytes:
  111. * return 1 if it is,
  112. * 0 if it isn't.
  113. */
  114. int buffer_is_all_zero(const uint8_t *buf, size_t size)
  115. {
  116. for (size_t i = 0; i < size; i++) {
  117. if (buf[i] != 0) {
  118. return 0;
  119. }
  120. }
  121. return 1;
  122. }
  123. #endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
  124. /* END_HEADER */
  125. /* BEGIN_DEPENDENCIES
  126. * depends_on:MBEDTLS_CIPHER_C
  127. * END_DEPENDENCIES
  128. */
  129. /* BEGIN_CASE */
  130. void mbedtls_cipher_list()
  131. {
  132. const int *cipher_type;
  133. for (cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++) {
  134. const mbedtls_cipher_info_t *info =
  135. mbedtls_cipher_info_from_type(*cipher_type);
  136. mbedtls_test_set_step(*cipher_type);
  137. if (!check_cipher_info(*cipher_type, info)) {
  138. goto exit;
  139. }
  140. }
  141. }
  142. /* END_CASE */
  143. /* BEGIN_CASE */
  144. void cipher_invalid_param_unconditional()
  145. {
  146. mbedtls_cipher_context_t valid_ctx;
  147. mbedtls_cipher_context_t invalid_ctx;
  148. mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
  149. mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
  150. unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  151. int valid_size = sizeof(valid_buffer);
  152. int valid_bitlen = valid_size * 8;
  153. const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
  154. *(mbedtls_cipher_list()));
  155. size_t size_t_var;
  156. (void) valid_mode; /* In some configurations this is unused */
  157. mbedtls_cipher_init(&valid_ctx);
  158. mbedtls_cipher_init(&invalid_ctx);
  159. TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0);
  160. /* mbedtls_cipher_setup() */
  161. TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) ==
  162. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  163. /* mbedtls_cipher_get_block_size() */
  164. TEST_ASSERT(mbedtls_cipher_get_block_size(&invalid_ctx) == 0);
  165. /* mbedtls_cipher_get_cipher_mode() */
  166. TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) ==
  167. MBEDTLS_MODE_NONE);
  168. /* mbedtls_cipher_get_iv_size() */
  169. TEST_ASSERT(mbedtls_cipher_get_iv_size(&invalid_ctx) == 0);
  170. /* mbedtls_cipher_get_type() */
  171. TEST_ASSERT(
  172. mbedtls_cipher_get_type(&invalid_ctx) ==
  173. MBEDTLS_CIPHER_NONE);
  174. /* mbedtls_cipher_get_name() */
  175. TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0);
  176. /* mbedtls_cipher_get_key_bitlen() */
  177. TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) ==
  178. MBEDTLS_KEY_LENGTH_NONE);
  179. /* mbedtls_cipher_get_operation() */
  180. TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) ==
  181. MBEDTLS_OPERATION_NONE);
  182. /* mbedtls_cipher_setkey() */
  183. TEST_ASSERT(
  184. mbedtls_cipher_setkey(&invalid_ctx,
  185. valid_buffer,
  186. valid_bitlen,
  187. valid_operation) ==
  188. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  189. /* mbedtls_cipher_set_iv() */
  190. TEST_ASSERT(
  191. mbedtls_cipher_set_iv(&invalid_ctx,
  192. valid_buffer,
  193. valid_size) ==
  194. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  195. /* mbedtls_cipher_reset() */
  196. TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) ==
  197. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  198. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  199. /* mbedtls_cipher_update_ad() */
  200. TEST_ASSERT(
  201. mbedtls_cipher_update_ad(&invalid_ctx,
  202. valid_buffer,
  203. valid_size) ==
  204. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  205. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  206. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  207. /* mbedtls_cipher_set_padding_mode() */
  208. TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) ==
  209. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  210. #endif
  211. /* mbedtls_cipher_update() */
  212. TEST_ASSERT(
  213. mbedtls_cipher_update(&invalid_ctx,
  214. valid_buffer,
  215. valid_size,
  216. valid_buffer,
  217. &size_t_var) ==
  218. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  219. /* mbedtls_cipher_finish() */
  220. TEST_ASSERT(
  221. mbedtls_cipher_finish(&invalid_ctx,
  222. valid_buffer,
  223. &size_t_var) ==
  224. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  225. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  226. /* mbedtls_cipher_write_tag() */
  227. TEST_ASSERT(
  228. mbedtls_cipher_write_tag(&invalid_ctx,
  229. valid_buffer,
  230. valid_size) ==
  231. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  232. /* mbedtls_cipher_check_tag() */
  233. TEST_ASSERT(
  234. mbedtls_cipher_check_tag(&invalid_ctx,
  235. valid_buffer,
  236. valid_size) ==
  237. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  238. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  239. exit:
  240. mbedtls_cipher_free(&invalid_ctx);
  241. mbedtls_cipher_free(&valid_ctx);
  242. }
  243. /* END_CASE */
  244. /* BEGIN_CASE */
  245. void cipher_invalid_param_conditional()
  246. {
  247. mbedtls_cipher_context_t valid_ctx;
  248. mbedtls_operation_t invalid_operation = 100;
  249. unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  250. int valid_size = sizeof(valid_buffer);
  251. int valid_bitlen = valid_size * 8;
  252. TEST_EQUAL(
  253. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  254. mbedtls_cipher_setkey(&valid_ctx,
  255. valid_buffer,
  256. valid_bitlen,
  257. invalid_operation));
  258. exit:
  259. ;
  260. }
  261. /* END_CASE */
  262. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  263. void cipher_special_behaviours()
  264. {
  265. const mbedtls_cipher_info_t *cipher_info;
  266. mbedtls_cipher_context_t ctx;
  267. unsigned char input[32];
  268. unsigned char output[32];
  269. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  270. unsigned char iv[32];
  271. #endif
  272. size_t olen = 0;
  273. mbedtls_cipher_init(&ctx);
  274. memset(input, 0, sizeof(input));
  275. memset(output, 0, sizeof(output));
  276. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  277. memset(iv, 0, sizeof(iv));
  278. /* Check and get info structures */
  279. cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
  280. TEST_ASSERT(NULL != cipher_info);
  281. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
  282. /* IV too big */
  283. TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1)
  284. == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
  285. /* IV too small */
  286. TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0)
  287. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  288. mbedtls_cipher_free(&ctx);
  289. mbedtls_cipher_init(&ctx);
  290. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  291. cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
  292. TEST_ASSERT(NULL != cipher_info);
  293. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
  294. /* Update ECB with partial block */
  295. TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen)
  296. == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
  297. exit:
  298. mbedtls_cipher_free(&ctx);
  299. }
  300. /* END_CASE */
  301. /* BEGIN_CASE */
  302. void enc_dec_buf(int cipher_id, char *cipher_string, int key_len,
  303. int length_val, int pad_mode)
  304. {
  305. size_t length = length_val, outlen, total_len, i, block_size, iv_len;
  306. unsigned char key[64];
  307. unsigned char iv[16];
  308. unsigned char ad[13];
  309. unsigned char tag[16];
  310. unsigned char inbuf[64];
  311. unsigned char encbuf[64];
  312. unsigned char decbuf[64];
  313. const mbedtls_cipher_info_t *cipher_info;
  314. mbedtls_cipher_context_t ctx_dec;
  315. mbedtls_cipher_context_t ctx_enc;
  316. /*
  317. * Prepare contexts
  318. */
  319. mbedtls_cipher_init(&ctx_dec);
  320. mbedtls_cipher_init(&ctx_enc);
  321. memset(key, 0x2a, sizeof(key));
  322. /* Check and get info structures */
  323. cipher_info = mbedtls_cipher_info_from_type(cipher_id);
  324. TEST_ASSERT(NULL != cipher_info);
  325. TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
  326. TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
  327. cipher_string) == 0);
  328. /* Initialise enc and dec contexts */
  329. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
  330. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
  331. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
  332. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
  333. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  334. if (-1 != pad_mode) {
  335. TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
  336. TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
  337. }
  338. #else
  339. (void) pad_mode;
  340. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  341. /*
  342. * Do a few encode/decode cycles
  343. */
  344. for (i = 0; i < 3; i++) {
  345. memset(iv, 0x00 + i, sizeof(iv));
  346. memset(ad, 0x10 + i, sizeof(ad));
  347. memset(inbuf, 0x20 + i, sizeof(inbuf));
  348. memset(encbuf, 0, sizeof(encbuf));
  349. memset(decbuf, 0, sizeof(decbuf));
  350. memset(tag, 0, sizeof(tag));
  351. if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
  352. iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
  353. * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
  354. } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
  355. cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
  356. iv_len = 12;
  357. } else {
  358. iv_len = sizeof(iv);
  359. }
  360. TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
  361. TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
  362. TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
  363. TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
  364. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  365. int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
  366. cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
  367. 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  368. TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i));
  369. TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i));
  370. #endif
  371. block_size = mbedtls_cipher_get_block_size(&ctx_enc);
  372. TEST_ASSERT(block_size != 0);
  373. /* encode length number of bytes from inbuf */
  374. TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen));
  375. total_len = outlen;
  376. TEST_ASSERT(total_len == length ||
  377. (total_len % block_size == 0 &&
  378. total_len < length &&
  379. total_len + block_size > length));
  380. TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen));
  381. total_len += outlen;
  382. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  383. TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag)));
  384. #endif
  385. TEST_ASSERT(total_len == length ||
  386. (total_len % block_size == 0 &&
  387. total_len > length &&
  388. total_len <= length + block_size));
  389. /* decode the previously encoded string */
  390. TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen));
  391. total_len = outlen;
  392. TEST_ASSERT(total_len == length ||
  393. (total_len % block_size == 0 &&
  394. total_len < length &&
  395. total_len + block_size >= length));
  396. TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen));
  397. total_len += outlen;
  398. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  399. TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag)));
  400. #endif
  401. /* check result */
  402. TEST_ASSERT(total_len == length);
  403. TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
  404. }
  405. /*
  406. * Done
  407. */
  408. exit:
  409. mbedtls_cipher_free(&ctx_dec);
  410. mbedtls_cipher_free(&ctx_enc);
  411. }
  412. /* END_CASE */
  413. /* BEGIN_CASE */
  414. void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val,
  415. int ret)
  416. {
  417. size_t length = length_val;
  418. unsigned char key[32];
  419. unsigned char iv[16];
  420. const mbedtls_cipher_info_t *cipher_info;
  421. mbedtls_cipher_context_t ctx;
  422. unsigned char inbuf[64];
  423. unsigned char encbuf[64];
  424. size_t outlen = 0;
  425. memset(key, 0, 32);
  426. memset(iv, 0, 16);
  427. mbedtls_cipher_init(&ctx);
  428. memset(inbuf, 5, 64);
  429. memset(encbuf, 0, 64);
  430. /* Check and get info structures */
  431. cipher_info = mbedtls_cipher_info_from_type(cipher_id);
  432. TEST_ASSERT(NULL != cipher_info);
  433. /* Initialise context */
  434. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
  435. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT));
  436. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  437. TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
  438. #else
  439. (void) pad_mode;
  440. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  441. TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16));
  442. TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
  443. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  444. int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
  445. cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
  446. 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  447. TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0));
  448. #endif
  449. /* encode length number of bytes from inbuf */
  450. TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen));
  451. TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen));
  452. /* done */
  453. exit:
  454. mbedtls_cipher_free(&ctx);
  455. }
  456. /* END_CASE */
  457. /* BEGIN_CASE */
  458. void dec_empty_buf(int cipher,
  459. int expected_update_ret,
  460. int expected_finish_ret)
  461. {
  462. unsigned char key[32];
  463. unsigned char *iv = NULL;
  464. size_t iv_len = 16;
  465. mbedtls_cipher_context_t ctx_dec;
  466. const mbedtls_cipher_info_t *cipher_info;
  467. unsigned char encbuf[64];
  468. unsigned char decbuf[64];
  469. size_t outlen = 0;
  470. memset(key, 0, 32);
  471. mbedtls_cipher_init(&ctx_dec);
  472. memset(encbuf, 0, 64);
  473. memset(decbuf, 0, 64);
  474. /* Initialise context */
  475. cipher_info = mbedtls_cipher_info_from_type(cipher);
  476. TEST_ASSERT(NULL != cipher_info);
  477. if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
  478. cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
  479. iv_len = 12;
  480. }
  481. ASSERT_ALLOC(iv, iv_len);
  482. memset(iv, 0, iv_len);
  483. TEST_ASSERT(sizeof(key) * 8 >= cipher_info->key_bitlen);
  484. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
  485. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec,
  486. key, cipher_info->key_bitlen,
  487. MBEDTLS_DECRYPT));
  488. TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
  489. TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
  490. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  491. int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
  492. cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
  493. 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  494. TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
  495. #endif
  496. /* decode 0-byte string */
  497. TEST_ASSERT(expected_update_ret ==
  498. mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen));
  499. TEST_ASSERT(0 == outlen);
  500. if (expected_finish_ret == 0 &&
  501. (cipher_info->mode == MBEDTLS_MODE_CBC ||
  502. cipher_info->mode == MBEDTLS_MODE_ECB)) {
  503. /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
  504. * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
  505. * decrypting an empty buffer.
  506. * On the other hand, CBC and ECB ciphers need a full block of input.
  507. */
  508. expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  509. }
  510. TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish(
  511. &ctx_dec, decbuf + outlen, &outlen));
  512. TEST_ASSERT(0 == outlen);
  513. exit:
  514. mbedtls_free(iv);
  515. mbedtls_cipher_free(&ctx_dec);
  516. }
  517. /* END_CASE */
  518. /* BEGIN_CASE */
  519. void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val,
  520. int second_length_val, int pad_mode,
  521. int first_encrypt_output_len, int second_encrypt_output_len,
  522. int first_decrypt_output_len, int second_decrypt_output_len)
  523. {
  524. size_t first_length = first_length_val;
  525. size_t second_length = second_length_val;
  526. size_t length = first_length + second_length;
  527. size_t block_size;
  528. size_t iv_len;
  529. unsigned char key[32];
  530. unsigned char iv[16];
  531. mbedtls_cipher_context_t ctx_dec;
  532. mbedtls_cipher_context_t ctx_enc;
  533. const mbedtls_cipher_info_t *cipher_info;
  534. unsigned char inbuf[64];
  535. unsigned char encbuf[64];
  536. unsigned char decbuf[64];
  537. size_t outlen = 0;
  538. size_t totaloutlen = 0;
  539. memset(key, 0, 32);
  540. memset(iv, 0, 16);
  541. mbedtls_cipher_init(&ctx_dec);
  542. mbedtls_cipher_init(&ctx_enc);
  543. memset(inbuf, 5, 64);
  544. memset(encbuf, 0, 64);
  545. memset(decbuf, 0, 64);
  546. /* Initialise enc and dec contexts */
  547. cipher_info = mbedtls_cipher_info_from_type(cipher_id);
  548. TEST_ASSERT(NULL != cipher_info);
  549. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
  550. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
  551. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
  552. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
  553. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  554. if (-1 != pad_mode) {
  555. TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
  556. TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
  557. }
  558. #else
  559. (void) pad_mode;
  560. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  561. if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
  562. iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
  563. * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
  564. } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
  565. cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
  566. iv_len = 12;
  567. } else {
  568. iv_len = sizeof(iv);
  569. }
  570. TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
  571. TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
  572. TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
  573. TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
  574. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  575. int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
  576. cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
  577. 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  578. TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
  579. TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0));
  580. #endif
  581. block_size = mbedtls_cipher_get_block_size(&ctx_enc);
  582. TEST_ASSERT(block_size != 0);
  583. /* encode length number of bytes from inbuf */
  584. TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen));
  585. TEST_ASSERT((size_t) first_encrypt_output_len == outlen);
  586. totaloutlen = outlen;
  587. TEST_ASSERT(0 ==
  588. mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length,
  589. encbuf + totaloutlen,
  590. &outlen));
  591. TEST_ASSERT((size_t) second_encrypt_output_len == outlen);
  592. totaloutlen += outlen;
  593. TEST_ASSERT(totaloutlen == length ||
  594. (totaloutlen % block_size == 0 &&
  595. totaloutlen < length &&
  596. totaloutlen + block_size > length));
  597. TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen));
  598. totaloutlen += outlen;
  599. TEST_ASSERT(totaloutlen == length ||
  600. (totaloutlen % block_size == 0 &&
  601. totaloutlen > length &&
  602. totaloutlen <= length + block_size));
  603. /* decode the previously encoded string */
  604. second_length = totaloutlen - first_length;
  605. TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen));
  606. TEST_ASSERT((size_t) first_decrypt_output_len == outlen);
  607. totaloutlen = outlen;
  608. TEST_ASSERT(0 ==
  609. mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length,
  610. decbuf + totaloutlen,
  611. &outlen));
  612. TEST_ASSERT((size_t) second_decrypt_output_len == outlen);
  613. totaloutlen += outlen;
  614. TEST_ASSERT(totaloutlen == length ||
  615. (totaloutlen % block_size == 0 &&
  616. totaloutlen < length &&
  617. totaloutlen + block_size >= length));
  618. TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen));
  619. totaloutlen += outlen;
  620. TEST_ASSERT(totaloutlen == length);
  621. TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
  622. exit:
  623. mbedtls_cipher_free(&ctx_dec);
  624. mbedtls_cipher_free(&ctx_enc);
  625. }
  626. /* END_CASE */
  627. /* BEGIN_CASE */
  628. void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key,
  629. data_t *iv, data_t *cipher,
  630. data_t *clear, data_t *ad, data_t *tag,
  631. int finish_result, int tag_result)
  632. {
  633. unsigned char output[265];
  634. mbedtls_cipher_context_t ctx;
  635. size_t outlen, total_len;
  636. mbedtls_cipher_init(&ctx);
  637. memset(output, 0x00, sizeof(output));
  638. #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
  639. ((void) ad);
  640. ((void) tag);
  641. #endif
  642. /* Prepare context */
  643. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
  644. mbedtls_cipher_info_from_type(cipher_id)));
  645. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
  646. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  647. if (pad_mode != -1) {
  648. TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
  649. }
  650. #else
  651. (void) pad_mode;
  652. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  653. TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
  654. TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
  655. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  656. int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
  657. ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
  658. 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  659. TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
  660. #endif
  661. /* decode buffer and check tag->x */
  662. total_len = 0;
  663. TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
  664. total_len += outlen;
  665. TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
  666. &outlen));
  667. total_len += outlen;
  668. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  669. int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
  670. ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
  671. tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  672. TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
  673. #endif
  674. /* check plaintext only if everything went fine */
  675. if (0 == finish_result && 0 == tag_result) {
  676. TEST_ASSERT(total_len == clear->len);
  677. TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
  678. }
  679. exit:
  680. mbedtls_cipher_free(&ctx);
  681. }
  682. /* END_CASE */
  683. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
  684. void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
  685. data_t *ad, data_t *cipher, data_t *tag,
  686. char *result, data_t *clear, int use_psa)
  687. {
  688. /*
  689. * Take an AEAD ciphertext + tag and perform a pair
  690. * of AEAD decryption and AEAD encryption. Check that
  691. * this results in the expected plaintext, and that
  692. * decryption and encryption are inverse to one another.
  693. */
  694. int ret;
  695. int using_nist_kw, using_nist_kw_padding;
  696. mbedtls_cipher_context_t ctx;
  697. size_t outlen;
  698. unsigned char *cipher_plus_tag = NULL;
  699. size_t cipher_plus_tag_len;
  700. unsigned char *decrypt_buf = NULL;
  701. size_t decrypt_buf_len = 0;
  702. unsigned char *encrypt_buf = NULL;
  703. size_t encrypt_buf_len = 0;
  704. /* Null pointers are documented as valid for inputs of length 0.
  705. * The test framework passes non-null pointers, so set them to NULL.
  706. * key, cipher and tag can't be empty. */
  707. if (iv->len == 0) {
  708. iv->x = NULL;
  709. }
  710. if (ad->len == 0) {
  711. ad->x = NULL;
  712. }
  713. if (clear->len == 0) {
  714. clear->x = NULL;
  715. }
  716. mbedtls_cipher_init(&ctx);
  717. /* Initialize PSA Crypto */
  718. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  719. if (use_psa == 1) {
  720. PSA_ASSERT(psa_crypto_init());
  721. }
  722. #else
  723. (void) use_psa;
  724. #endif
  725. /*
  726. * Are we using NIST_KW? with padding?
  727. */
  728. using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
  729. cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
  730. cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
  731. using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
  732. cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
  733. cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
  734. using_nist_kw_padding;
  735. /*
  736. * Prepare context for decryption
  737. */
  738. if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
  739. MBEDTLS_DECRYPT)) {
  740. goto exit;
  741. }
  742. /*
  743. * prepare buffer for decryption
  744. * (we need the tag appended to the ciphertext)
  745. */
  746. cipher_plus_tag_len = cipher->len + tag->len;
  747. ASSERT_ALLOC(cipher_plus_tag, cipher_plus_tag_len);
  748. memcpy(cipher_plus_tag, cipher->x, cipher->len);
  749. memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);
  750. /*
  751. * Compute length of output buffer according to the documentation
  752. */
  753. if (using_nist_kw) {
  754. decrypt_buf_len = cipher_plus_tag_len - 8;
  755. } else {
  756. decrypt_buf_len = cipher_plus_tag_len - tag->len;
  757. }
  758. /*
  759. * Try decrypting to a buffer that's 1B too small
  760. */
  761. if (decrypt_buf_len != 0) {
  762. ASSERT_ALLOC(decrypt_buf, decrypt_buf_len - 1);
  763. outlen = 0;
  764. ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
  765. ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
  766. decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len);
  767. TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
  768. mbedtls_free(decrypt_buf);
  769. decrypt_buf = NULL;
  770. }
  771. /*
  772. * Authenticate and decrypt, and check result
  773. */
  774. ASSERT_ALLOC(decrypt_buf, decrypt_buf_len);
  775. outlen = 0;
  776. ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
  777. ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
  778. decrypt_buf, decrypt_buf_len, &outlen, tag->len);
  779. if (strcmp(result, "FAIL") == 0) {
  780. TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
  781. TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
  782. } else {
  783. TEST_ASSERT(ret == 0);
  784. ASSERT_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
  785. }
  786. mbedtls_free(decrypt_buf);
  787. decrypt_buf = NULL;
  788. /*
  789. * Encrypt back if test data was authentic
  790. */
  791. if (strcmp(result, "FAIL") != 0) {
  792. /* prepare context for encryption */
  793. if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
  794. MBEDTLS_ENCRYPT)) {
  795. goto exit;
  796. }
  797. /*
  798. * Compute size of output buffer according to documentation
  799. */
  800. if (using_nist_kw) {
  801. encrypt_buf_len = clear->len + 8;
  802. if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) {
  803. encrypt_buf_len += 8 - encrypt_buf_len % 8;
  804. }
  805. } else {
  806. encrypt_buf_len = clear->len + tag->len;
  807. }
  808. /*
  809. * Try encrypting with an output buffer that's 1B too small
  810. */
  811. ASSERT_ALLOC(encrypt_buf, encrypt_buf_len - 1);
  812. outlen = 0;
  813. ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
  814. ad->x, ad->len, clear->x, clear->len,
  815. encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len);
  816. TEST_ASSERT(ret != 0);
  817. mbedtls_free(encrypt_buf);
  818. encrypt_buf = NULL;
  819. /*
  820. * Encrypt and check the result
  821. */
  822. ASSERT_ALLOC(encrypt_buf, encrypt_buf_len);
  823. outlen = 0;
  824. ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
  825. ad->x, ad->len, clear->x, clear->len,
  826. encrypt_buf, encrypt_buf_len, &outlen, tag->len);
  827. TEST_ASSERT(ret == 0);
  828. TEST_ASSERT(outlen == cipher->len + tag->len);
  829. TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0);
  830. TEST_ASSERT(memcmp(encrypt_buf + cipher->len,
  831. tag->x, tag->len) == 0);
  832. mbedtls_free(encrypt_buf);
  833. encrypt_buf = NULL;
  834. }
  835. exit:
  836. mbedtls_cipher_free(&ctx);
  837. mbedtls_free(decrypt_buf);
  838. mbedtls_free(encrypt_buf);
  839. mbedtls_free(cipher_plus_tag);
  840. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  841. if (use_psa == 1) {
  842. PSA_DONE();
  843. }
  844. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  845. }
  846. /* END_CASE */
  847. /* BEGIN_CASE */
  848. void test_vec_ecb(int cipher_id, int operation, data_t *key,
  849. data_t *input, data_t *result, int finish_result
  850. )
  851. {
  852. mbedtls_cipher_context_t ctx;
  853. unsigned char output[32];
  854. size_t outlen;
  855. mbedtls_cipher_init(&ctx);
  856. memset(output, 0x00, sizeof(output));
  857. /* Prepare context */
  858. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
  859. mbedtls_cipher_info_from_type(cipher_id)));
  860. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
  861. TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x,
  862. mbedtls_cipher_get_block_size(&ctx),
  863. output, &outlen));
  864. TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx));
  865. TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
  866. &outlen));
  867. TEST_ASSERT(0 == outlen);
  868. /* check plaintext only if everything went fine */
  869. if (0 == finish_result) {
  870. TEST_ASSERT(0 == memcmp(output, result->x,
  871. mbedtls_cipher_get_block_size(&ctx)));
  872. }
  873. exit:
  874. mbedtls_cipher_free(&ctx);
  875. }
  876. /* END_CASE */
  877. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
  878. void test_vec_crypt(int cipher_id, int operation, data_t *key,
  879. data_t *iv, data_t *input, data_t *result,
  880. int finish_result, int use_psa)
  881. {
  882. mbedtls_cipher_context_t ctx;
  883. unsigned char output[32];
  884. size_t outlen;
  885. mbedtls_cipher_init(&ctx);
  886. memset(output, 0x00, sizeof(output));
  887. /* Prepare context */
  888. #if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
  889. (void) use_psa;
  890. #else
  891. if (use_psa == 1) {
  892. PSA_ASSERT(psa_crypto_init());
  893. TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
  894. mbedtls_cipher_info_from_type(cipher_id), 0));
  895. } else
  896. #endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
  897. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
  898. mbedtls_cipher_info_from_type(cipher_id)));
  899. TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
  900. if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
  901. TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
  902. }
  903. TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL,
  904. iv->len, input->x, input->len,
  905. output, &outlen));
  906. TEST_ASSERT(result->len == outlen);
  907. /* check plaintext only if everything went fine */
  908. if (0 == finish_result) {
  909. TEST_ASSERT(0 == memcmp(output, result->x, outlen));
  910. }
  911. exit:
  912. mbedtls_cipher_free(&ctx);
  913. #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
  914. PSA_DONE();
  915. #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
  916. }
  917. /* END_CASE */
  918. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
  919. void set_padding(int cipher_id, int pad_mode, int ret)
  920. {
  921. const mbedtls_cipher_info_t *cipher_info;
  922. mbedtls_cipher_context_t ctx;
  923. mbedtls_cipher_init(&ctx);
  924. cipher_info = mbedtls_cipher_info_from_type(cipher_id);
  925. TEST_ASSERT(NULL != cipher_info);
  926. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
  927. TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
  928. exit:
  929. mbedtls_cipher_free(&ctx);
  930. }
  931. /* END_CASE */
  932. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  933. void check_padding(int pad_mode, data_t *input, int ret, int dlen_check
  934. )
  935. {
  936. mbedtls_cipher_info_t cipher_info;
  937. mbedtls_cipher_context_t ctx;
  938. size_t dlen;
  939. /* build a fake context just for getting access to get_padding */
  940. mbedtls_cipher_init(&ctx);
  941. cipher_info.mode = MBEDTLS_MODE_CBC;
  942. ctx.cipher_info = &cipher_info;
  943. TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
  944. TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen));
  945. if (0 == ret) {
  946. TEST_ASSERT(dlen == (size_t) dlen_check);
  947. }
  948. }
  949. /* END_CASE */
  950. /* BEGIN_CASE */
  951. void iv_len_validity(int cipher_id, char *cipher_string,
  952. int iv_len_val, int ret)
  953. {
  954. size_t iv_len = iv_len_val;
  955. unsigned char iv[16];
  956. /* Initialise iv buffer */
  957. memset(iv, 0, sizeof(iv));
  958. const mbedtls_cipher_info_t *cipher_info;
  959. mbedtls_cipher_context_t ctx_dec;
  960. mbedtls_cipher_context_t ctx_enc;
  961. /*
  962. * Prepare contexts
  963. */
  964. mbedtls_cipher_init(&ctx_dec);
  965. mbedtls_cipher_init(&ctx_enc);
  966. /* Check and get info structures */
  967. cipher_info = mbedtls_cipher_info_from_type(cipher_id);
  968. TEST_ASSERT(NULL != cipher_info);
  969. TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
  970. TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
  971. cipher_string) == 0);
  972. /* Initialise enc and dec contexts */
  973. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
  974. TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
  975. TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
  976. TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
  977. exit:
  978. mbedtls_cipher_free(&ctx_dec);
  979. mbedtls_cipher_free(&ctx_enc);
  980. }
  981. /* END_CASE */