test_suite_cipher.function 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/cipher.h"
  3. #if defined(MBEDTLS_GCM_C)
  4. #include "mbedtls/gcm.h"
  5. #endif
  6. /* END_HEADER */
  7. /* BEGIN_DEPENDENCIES
  8. * depends_on:MBEDTLS_CIPHER_C
  9. * END_DEPENDENCIES
  10. */
  11. /* BEGIN_CASE */
  12. void mbedtls_cipher_list( )
  13. {
  14. const int *cipher_type;
  15. for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
  16. TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
  17. }
  18. /* END_CASE */
  19. /* BEGIN_CASE */
  20. void cipher_null_args( )
  21. {
  22. mbedtls_cipher_context_t ctx;
  23. const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type( *( mbedtls_cipher_list() ) );
  24. unsigned char buf[1] = { 0 };
  25. size_t olen;
  26. mbedtls_cipher_init( &ctx );
  27. TEST_ASSERT( mbedtls_cipher_get_block_size( NULL ) == 0 );
  28. TEST_ASSERT( mbedtls_cipher_get_block_size( &ctx ) == 0 );
  29. TEST_ASSERT( mbedtls_cipher_get_cipher_mode( NULL ) == MBEDTLS_MODE_NONE );
  30. TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &ctx ) == MBEDTLS_MODE_NONE );
  31. TEST_ASSERT( mbedtls_cipher_get_iv_size( NULL ) == 0 );
  32. TEST_ASSERT( mbedtls_cipher_get_iv_size( &ctx ) == 0 );
  33. TEST_ASSERT( mbedtls_cipher_info_from_string( NULL ) == NULL );
  34. TEST_ASSERT( mbedtls_cipher_setup( &ctx, NULL )
  35. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  36. TEST_ASSERT( mbedtls_cipher_setup( NULL, info )
  37. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  38. TEST_ASSERT( mbedtls_cipher_setkey( NULL, buf, 0, MBEDTLS_ENCRYPT )
  39. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  40. TEST_ASSERT( mbedtls_cipher_setkey( &ctx, buf, 0, MBEDTLS_ENCRYPT )
  41. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  42. TEST_ASSERT( mbedtls_cipher_set_iv( NULL, buf, 0 )
  43. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  44. TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, buf, 0 )
  45. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  46. TEST_ASSERT( mbedtls_cipher_reset( NULL ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  47. TEST_ASSERT( mbedtls_cipher_reset( &ctx ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  48. #if defined(MBEDTLS_GCM_C)
  49. TEST_ASSERT( mbedtls_cipher_update_ad( NULL, buf, 0 )
  50. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  51. TEST_ASSERT( mbedtls_cipher_update_ad( &ctx, buf, 0 )
  52. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  53. #endif
  54. TEST_ASSERT( mbedtls_cipher_update( NULL, buf, 0, buf, &olen )
  55. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  56. TEST_ASSERT( mbedtls_cipher_update( &ctx, buf, 0, buf, &olen )
  57. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  58. TEST_ASSERT( mbedtls_cipher_finish( NULL, buf, &olen )
  59. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  60. TEST_ASSERT( mbedtls_cipher_finish( &ctx, buf, &olen )
  61. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  62. #if defined(MBEDTLS_GCM_C)
  63. TEST_ASSERT( mbedtls_cipher_write_tag( NULL, buf, olen )
  64. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  65. TEST_ASSERT( mbedtls_cipher_write_tag( &ctx, buf, olen )
  66. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  67. TEST_ASSERT( mbedtls_cipher_check_tag( NULL, buf, olen )
  68. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  69. TEST_ASSERT( mbedtls_cipher_check_tag( &ctx, buf, olen )
  70. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  71. #endif
  72. }
  73. /* END_CASE */
  74. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  75. void cipher_special_behaviours( )
  76. {
  77. const mbedtls_cipher_info_t *cipher_info;
  78. mbedtls_cipher_context_t ctx;
  79. unsigned char input[32];
  80. unsigned char output[32];
  81. unsigned char iv[32];
  82. size_t olen = 0;
  83. mbedtls_cipher_init( &ctx );
  84. memset( input, 0, sizeof( input ) );
  85. memset( output, 0, sizeof( output ) );
  86. memset( iv, 0, sizeof( iv ) );
  87. /* Check and get info structures */
  88. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
  89. TEST_ASSERT( NULL != cipher_info );
  90. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  91. /* IV too big */
  92. TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
  93. == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  94. /* IV too small */
  95. TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
  96. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  97. /* Update ECB with partial block */
  98. TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
  99. == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  100. exit:
  101. mbedtls_cipher_free( &ctx );
  102. }
  103. /* END_CASE */
  104. /* BEGIN_CASE */
  105. void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
  106. int length_val, int pad_mode )
  107. {
  108. size_t length = length_val, outlen, total_len, i, block_size;
  109. unsigned char key[32];
  110. unsigned char iv[16];
  111. unsigned char ad[13];
  112. unsigned char tag[16];
  113. unsigned char inbuf[64];
  114. unsigned char encbuf[64];
  115. unsigned char decbuf[64];
  116. const mbedtls_cipher_info_t *cipher_info;
  117. mbedtls_cipher_context_t ctx_dec;
  118. mbedtls_cipher_context_t ctx_enc;
  119. /*
  120. * Prepare contexts
  121. */
  122. mbedtls_cipher_init( &ctx_dec );
  123. mbedtls_cipher_init( &ctx_enc );
  124. memset( key, 0x2a, sizeof( key ) );
  125. /* Check and get info structures */
  126. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  127. TEST_ASSERT( NULL != cipher_info );
  128. TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
  129. /* Initialise enc and dec contexts */
  130. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  131. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
  132. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
  133. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
  134. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  135. if( -1 != pad_mode )
  136. {
  137. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
  138. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
  139. }
  140. #else
  141. (void) pad_mode;
  142. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  143. /*
  144. * Do a few encode/decode cycles
  145. */
  146. for( i = 0; i < 3; i++ )
  147. {
  148. memset( iv , 0x00 + i, sizeof( iv ) );
  149. memset( ad, 0x10 + i, sizeof( ad ) );
  150. memset( inbuf, 0x20 + i, sizeof( inbuf ) );
  151. memset( encbuf, 0, sizeof( encbuf ) );
  152. memset( decbuf, 0, sizeof( decbuf ) );
  153. memset( tag, 0, sizeof( tag ) );
  154. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
  155. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
  156. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  157. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
  158. #if defined(MBEDTLS_GCM_C)
  159. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
  160. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
  161. #endif
  162. block_size = mbedtls_cipher_get_block_size( &ctx_enc );
  163. TEST_ASSERT( block_size != 0 );
  164. /* encode length number of bytes from inbuf */
  165. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
  166. total_len = outlen;
  167. TEST_ASSERT( total_len == length ||
  168. ( total_len % block_size == 0 &&
  169. total_len < length &&
  170. total_len + block_size > length ) );
  171. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
  172. total_len += outlen;
  173. #if defined(MBEDTLS_GCM_C)
  174. TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
  175. #endif
  176. TEST_ASSERT( total_len == length ||
  177. ( total_len % block_size == 0 &&
  178. total_len > length &&
  179. total_len <= length + block_size ) );
  180. /* decode the previously encoded string */
  181. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
  182. total_len = outlen;
  183. TEST_ASSERT( total_len == length ||
  184. ( total_len % block_size == 0 &&
  185. total_len < length &&
  186. total_len + block_size >= length ) );
  187. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
  188. total_len += outlen;
  189. #if defined(MBEDTLS_GCM_C)
  190. TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
  191. #endif
  192. /* check result */
  193. TEST_ASSERT( total_len == length );
  194. TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
  195. }
  196. /*
  197. * Done
  198. */
  199. exit:
  200. mbedtls_cipher_free( &ctx_dec );
  201. mbedtls_cipher_free( &ctx_enc );
  202. }
  203. /* END_CASE */
  204. /* BEGIN_CASE */
  205. void enc_fail( int cipher_id, int pad_mode, int key_len,
  206. int length_val, int ret )
  207. {
  208. size_t length = length_val;
  209. unsigned char key[32];
  210. unsigned char iv[16];
  211. const mbedtls_cipher_info_t *cipher_info;
  212. mbedtls_cipher_context_t ctx;
  213. unsigned char inbuf[64];
  214. unsigned char encbuf[64];
  215. size_t outlen = 0;
  216. memset( key, 0, 32 );
  217. memset( iv , 0, 16 );
  218. mbedtls_cipher_init( &ctx );
  219. memset( inbuf, 5, 64 );
  220. memset( encbuf, 0, 64 );
  221. /* Check and get info structures */
  222. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  223. TEST_ASSERT( NULL != cipher_info );
  224. /* Initialise context */
  225. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  226. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
  227. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  228. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  229. #else
  230. (void) pad_mode;
  231. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  232. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
  233. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
  234. #if defined(MBEDTLS_GCM_C)
  235. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
  236. #endif
  237. /* encode length number of bytes from inbuf */
  238. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
  239. TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
  240. /* done */
  241. exit:
  242. mbedtls_cipher_free( &ctx );
  243. }
  244. /* END_CASE */
  245. /* BEGIN_CASE */
  246. void dec_empty_buf()
  247. {
  248. unsigned char key[32];
  249. unsigned char iv[16];
  250. mbedtls_cipher_context_t ctx_dec;
  251. const mbedtls_cipher_info_t *cipher_info;
  252. unsigned char encbuf[64];
  253. unsigned char decbuf[64];
  254. size_t outlen = 0;
  255. memset( key, 0, 32 );
  256. memset( iv , 0, 16 );
  257. mbedtls_cipher_init( &ctx_dec );
  258. memset( encbuf, 0, 64 );
  259. memset( decbuf, 0, 64 );
  260. /* Initialise context */
  261. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
  262. TEST_ASSERT( NULL != cipher_info);
  263. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  264. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) );
  265. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
  266. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  267. #if defined(MBEDTLS_GCM_C)
  268. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
  269. #endif
  270. /* decode 0-byte string */
  271. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
  272. TEST_ASSERT( 0 == outlen );
  273. TEST_ASSERT( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED == mbedtls_cipher_finish(
  274. &ctx_dec, decbuf + outlen, &outlen ) );
  275. TEST_ASSERT( 0 == outlen );
  276. exit:
  277. mbedtls_cipher_free( &ctx_dec );
  278. }
  279. /* END_CASE */
  280. /* BEGIN_CASE */
  281. void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
  282. int second_length_val )
  283. {
  284. size_t first_length = first_length_val;
  285. size_t second_length = second_length_val;
  286. size_t length = first_length + second_length;
  287. size_t block_size;
  288. unsigned char key[32];
  289. unsigned char iv[16];
  290. mbedtls_cipher_context_t ctx_dec;
  291. mbedtls_cipher_context_t ctx_enc;
  292. const mbedtls_cipher_info_t *cipher_info;
  293. unsigned char inbuf[64];
  294. unsigned char encbuf[64];
  295. unsigned char decbuf[64];
  296. size_t outlen = 0;
  297. size_t totaloutlen = 0;
  298. memset( key, 0, 32 );
  299. memset( iv , 0, 16 );
  300. mbedtls_cipher_init( &ctx_dec );
  301. mbedtls_cipher_init( &ctx_enc );
  302. memset( inbuf, 5, 64 );
  303. memset( encbuf, 0, 64 );
  304. memset( decbuf, 0, 64 );
  305. /* Initialise enc and dec contexts */
  306. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  307. TEST_ASSERT( NULL != cipher_info);
  308. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  309. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
  310. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
  311. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
  312. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
  313. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
  314. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  315. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
  316. #if defined(MBEDTLS_GCM_C)
  317. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
  318. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
  319. #endif
  320. block_size = mbedtls_cipher_get_block_size( &ctx_enc );
  321. TEST_ASSERT( block_size != 0 );
  322. /* encode length number of bytes from inbuf */
  323. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
  324. totaloutlen = outlen;
  325. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
  326. totaloutlen += outlen;
  327. TEST_ASSERT( totaloutlen == length ||
  328. ( totaloutlen % block_size == 0 &&
  329. totaloutlen < length &&
  330. totaloutlen + block_size > length ) );
  331. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
  332. totaloutlen += outlen;
  333. TEST_ASSERT( totaloutlen == length ||
  334. ( totaloutlen % block_size == 0 &&
  335. totaloutlen > length &&
  336. totaloutlen <= length + block_size ) );
  337. /* decode the previously encoded string */
  338. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
  339. totaloutlen = outlen;
  340. TEST_ASSERT( totaloutlen == length ||
  341. ( totaloutlen % block_size == 0 &&
  342. totaloutlen < length &&
  343. totaloutlen + block_size >= length ) );
  344. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
  345. totaloutlen += outlen;
  346. TEST_ASSERT( totaloutlen == length );
  347. TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
  348. exit:
  349. mbedtls_cipher_free( &ctx_dec );
  350. mbedtls_cipher_free( &ctx_enc );
  351. }
  352. /* END_CASE */
  353. /* BEGIN_CASE */
  354. void decrypt_test_vec( int cipher_id, int pad_mode,
  355. char *hex_key, char *hex_iv,
  356. char *hex_cipher, char *hex_clear,
  357. char *hex_ad, char *hex_tag,
  358. int finish_result, int tag_result )
  359. {
  360. unsigned char key[50];
  361. unsigned char iv[50];
  362. unsigned char cipher[200];
  363. unsigned char clear[200];
  364. unsigned char ad[200];
  365. unsigned char tag[20];
  366. size_t key_len, iv_len, cipher_len, clear_len;
  367. #if defined(MBEDTLS_GCM_C)
  368. size_t ad_len, tag_len;
  369. #endif
  370. mbedtls_cipher_context_t ctx;
  371. unsigned char output[200];
  372. size_t outlen, total_len;
  373. mbedtls_cipher_init( &ctx );
  374. memset( key, 0x00, sizeof( key ) );
  375. memset( iv, 0x00, sizeof( iv ) );
  376. memset( cipher, 0x00, sizeof( cipher ) );
  377. memset( clear, 0x00, sizeof( clear ) );
  378. memset( ad, 0x00, sizeof( ad ) );
  379. memset( tag, 0x00, sizeof( tag ) );
  380. memset( output, 0x00, sizeof( output ) );
  381. key_len = unhexify( key, hex_key );
  382. iv_len = unhexify( iv, hex_iv );
  383. cipher_len = unhexify( cipher, hex_cipher );
  384. clear_len = unhexify( clear, hex_clear );
  385. #if defined(MBEDTLS_GCM_C)
  386. ad_len = unhexify( ad, hex_ad );
  387. tag_len = unhexify( tag, hex_tag );
  388. #else
  389. ((void) hex_ad);
  390. ((void) hex_tag);
  391. #endif
  392. /* Prepare context */
  393. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  394. mbedtls_cipher_info_from_type( cipher_id ) ) );
  395. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
  396. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  397. if( pad_mode != -1 )
  398. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  399. #else
  400. (void) pad_mode;
  401. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  402. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, iv_len ) );
  403. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
  404. #if defined(MBEDTLS_GCM_C)
  405. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad, ad_len ) );
  406. #endif
  407. /* decode buffer and check tag */
  408. total_len = 0;
  409. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
  410. total_len += outlen;
  411. TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
  412. &outlen ) );
  413. total_len += outlen;
  414. #if defined(MBEDTLS_GCM_C)
  415. TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag, tag_len ) );
  416. #endif
  417. /* check plaintext only if everything went fine */
  418. if( 0 == finish_result && 0 == tag_result )
  419. {
  420. TEST_ASSERT( total_len == clear_len );
  421. TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
  422. }
  423. exit:
  424. mbedtls_cipher_free( &ctx );
  425. }
  426. /* END_CASE */
  427. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
  428. void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
  429. char *hex_ad, char *hex_cipher,
  430. char *hex_tag, char *hex_clear )
  431. {
  432. int ret;
  433. unsigned char key[50];
  434. unsigned char iv[50];
  435. unsigned char cipher[200];
  436. unsigned char clear[200];
  437. unsigned char ad[200];
  438. unsigned char tag[20];
  439. unsigned char my_tag[20];
  440. size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
  441. mbedtls_cipher_context_t ctx;
  442. unsigned char output[200];
  443. size_t outlen;
  444. mbedtls_cipher_init( &ctx );
  445. memset( key, 0x00, sizeof( key ) );
  446. memset( iv, 0x00, sizeof( iv ) );
  447. memset( cipher, 0x00, sizeof( cipher ) );
  448. memset( clear, 0x00, sizeof( clear ) );
  449. memset( ad, 0x00, sizeof( ad ) );
  450. memset( tag, 0x00, sizeof( tag ) );
  451. memset( my_tag, 0xFF, sizeof( my_tag ) );
  452. memset( output, 0xFF, sizeof( output ) );
  453. key_len = unhexify( key, hex_key );
  454. iv_len = unhexify( iv, hex_iv );
  455. cipher_len = unhexify( cipher, hex_cipher );
  456. ad_len = unhexify( ad, hex_ad );
  457. tag_len = unhexify( tag, hex_tag );
  458. /* Prepare context */
  459. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  460. mbedtls_cipher_info_from_type( cipher_id ) ) );
  461. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
  462. /* decode buffer and check tag */
  463. ret = mbedtls_cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
  464. cipher, cipher_len, output, &outlen,
  465. tag, tag_len );
  466. /* make sure we didn't overwrite */
  467. TEST_ASSERT( output[outlen + 0] == 0xFF );
  468. TEST_ASSERT( output[outlen + 1] == 0xFF );
  469. /* make sure the message is rejected if it should be */
  470. if( strcmp( hex_clear, "FAIL" ) == 0 )
  471. {
  472. TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  473. goto exit;
  474. }
  475. /* otherwise, make sure it was decrypted properly */
  476. TEST_ASSERT( ret == 0 );
  477. clear_len = unhexify( clear, hex_clear );
  478. TEST_ASSERT( outlen == clear_len );
  479. TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
  480. /* then encrypt the clear and make sure we get the same ciphertext and tag */
  481. memset( output, 0xFF, sizeof( output ) );
  482. outlen = 0;
  483. ret = mbedtls_cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
  484. clear, clear_len, output, &outlen,
  485. my_tag, tag_len );
  486. TEST_ASSERT( ret == 0 );
  487. TEST_ASSERT( outlen == clear_len );
  488. TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
  489. TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
  490. /* make sure we didn't overwrite */
  491. TEST_ASSERT( output[outlen + 0] == 0xFF );
  492. TEST_ASSERT( output[outlen + 1] == 0xFF );
  493. TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
  494. TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
  495. exit:
  496. mbedtls_cipher_free( &ctx );
  497. }
  498. /* END_CASE */
  499. /* BEGIN_CASE */
  500. void test_vec_ecb( int cipher_id, int operation, char *hex_key,
  501. char *hex_input, char *hex_result,
  502. int finish_result )
  503. {
  504. unsigned char key[50];
  505. unsigned char input[16];
  506. unsigned char result[16];
  507. size_t key_len;
  508. mbedtls_cipher_context_t ctx;
  509. unsigned char output[32];
  510. size_t outlen;
  511. mbedtls_cipher_init( &ctx );
  512. memset( key, 0x00, sizeof( key ) );
  513. memset( input, 0x00, sizeof( input ) );
  514. memset( result, 0x00, sizeof( result ) );
  515. memset( output, 0x00, sizeof( output ) );
  516. /* Prepare context */
  517. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  518. mbedtls_cipher_info_from_type( cipher_id ) ) );
  519. key_len = unhexify( key, hex_key );
  520. TEST_ASSERT( unhexify( input, hex_input ) ==
  521. (int) mbedtls_cipher_get_block_size( &ctx ) );
  522. TEST_ASSERT( unhexify( result, hex_result ) ==
  523. (int) mbedtls_cipher_get_block_size( &ctx ) );
  524. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
  525. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input,
  526. mbedtls_cipher_get_block_size( &ctx ),
  527. output, &outlen ) );
  528. TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
  529. TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
  530. &outlen ) );
  531. TEST_ASSERT( 0 == outlen );
  532. /* check plaintext only if everything went fine */
  533. if( 0 == finish_result )
  534. TEST_ASSERT( 0 == memcmp( output, result,
  535. mbedtls_cipher_get_block_size( &ctx ) ) );
  536. exit:
  537. mbedtls_cipher_free( &ctx );
  538. }
  539. /* END_CASE */
  540. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
  541. void set_padding( int cipher_id, int pad_mode, int ret )
  542. {
  543. const mbedtls_cipher_info_t *cipher_info;
  544. mbedtls_cipher_context_t ctx;
  545. mbedtls_cipher_init( &ctx );
  546. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  547. TEST_ASSERT( NULL != cipher_info );
  548. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  549. TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  550. exit:
  551. mbedtls_cipher_free( &ctx );
  552. }
  553. /* END_CASE */
  554. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  555. void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
  556. {
  557. mbedtls_cipher_info_t cipher_info;
  558. mbedtls_cipher_context_t ctx;
  559. unsigned char input[16];
  560. size_t ilen, dlen;
  561. /* build a fake context just for getting access to get_padding */
  562. mbedtls_cipher_init( &ctx );
  563. cipher_info.mode = MBEDTLS_MODE_CBC;
  564. ctx.cipher_info = &cipher_info;
  565. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  566. ilen = unhexify( input, input_str );
  567. TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
  568. if( 0 == ret )
  569. TEST_ASSERT( dlen == (size_t) dlen_check );
  570. }
  571. /* END_CASE */