test_suite_cipher.function 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/cipher.h"
  3. #if defined(MBEDTLS_AES_C)
  4. #include "mbedtls/aes.h"
  5. #endif
  6. #if defined(MBEDTLS_GCM_C)
  7. #include "mbedtls/gcm.h"
  8. #endif
  9. #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
  10. #define MBEDTLS_CIPHER_AUTH_CRYPT
  11. #endif
  12. #if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
  13. /* Helper for resetting key/direction
  14. *
  15. * The documentation doesn't explicitly say whether calling
  16. * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
  17. * the default software implementation, but only by accident. It isn't
  18. * guaranteed to work with new ciphers or with alternative implementations of
  19. * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
  20. * it, and instead start with a fresh context.
  21. */
  22. static int cipher_reset_key( mbedtls_cipher_context_t *ctx, int cipher_id,
  23. int use_psa, size_t tag_len, const data_t *key, int direction )
  24. {
  25. mbedtls_cipher_free( ctx );
  26. mbedtls_cipher_init( ctx );
  27. #if !defined(MBEDTLS_USE_PSA_CRYPTO)
  28. (void) use_psa;
  29. (void) tag_len;
  30. #else
  31. if( use_psa == 1 )
  32. {
  33. TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
  34. mbedtls_cipher_info_from_type( cipher_id ),
  35. tag_len ) );
  36. }
  37. else
  38. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  39. {
  40. TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
  41. mbedtls_cipher_info_from_type( cipher_id ) ) );
  42. }
  43. TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
  44. direction ) );
  45. return( 1 );
  46. exit:
  47. return( 0 );
  48. }
  49. /*
  50. * Check if a buffer is all-0 bytes:
  51. * return 1 if it is,
  52. * 0 if it isn't.
  53. */
  54. int buffer_is_all_zero( const uint8_t *buf, size_t size )
  55. {
  56. for( size_t i = 0; i < size; i++ )
  57. if( buf[i] != 0 )
  58. return 0;
  59. return 1;
  60. }
  61. #endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
  62. /* END_HEADER */
  63. /* BEGIN_DEPENDENCIES
  64. * depends_on:MBEDTLS_CIPHER_C
  65. * END_DEPENDENCIES
  66. */
  67. /* BEGIN_CASE */
  68. void mbedtls_cipher_list( )
  69. {
  70. const int *cipher_type;
  71. for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
  72. TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
  73. }
  74. /* END_CASE */
  75. /* BEGIN_CASE */
  76. void cipher_invalid_param_unconditional( )
  77. {
  78. mbedtls_cipher_context_t valid_ctx;
  79. mbedtls_cipher_context_t invalid_ctx;
  80. mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
  81. mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
  82. unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  83. int valid_size = sizeof(valid_buffer);
  84. int valid_bitlen = valid_size * 8;
  85. const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
  86. *( mbedtls_cipher_list() ) );
  87. size_t size_t_var;
  88. (void)valid_mode; /* In some configurations this is unused */
  89. mbedtls_cipher_init( &valid_ctx );
  90. mbedtls_cipher_init( &invalid_ctx );
  91. TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, valid_info ) == 0 );
  92. /* mbedtls_cipher_setup() */
  93. TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
  94. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  95. /* mbedtls_cipher_get_block_size() */
  96. TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
  97. /* mbedtls_cipher_get_cipher_mode() */
  98. TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
  99. MBEDTLS_MODE_NONE );
  100. /* mbedtls_cipher_get_iv_size() */
  101. TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
  102. /* mbedtls_cipher_get_type() */
  103. TEST_ASSERT(
  104. mbedtls_cipher_get_type( &invalid_ctx ) ==
  105. MBEDTLS_CIPHER_NONE);
  106. /* mbedtls_cipher_get_name() */
  107. TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
  108. /* mbedtls_cipher_get_key_bitlen() */
  109. TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
  110. MBEDTLS_KEY_LENGTH_NONE );
  111. /* mbedtls_cipher_get_operation() */
  112. TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
  113. MBEDTLS_OPERATION_NONE );
  114. /* mbedtls_cipher_setkey() */
  115. TEST_ASSERT(
  116. mbedtls_cipher_setkey( &invalid_ctx,
  117. valid_buffer,
  118. valid_bitlen,
  119. valid_operation ) ==
  120. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  121. /* mbedtls_cipher_set_iv() */
  122. TEST_ASSERT(
  123. mbedtls_cipher_set_iv( &invalid_ctx,
  124. valid_buffer,
  125. valid_size ) ==
  126. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  127. /* mbedtls_cipher_reset() */
  128. TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
  129. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  130. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  131. /* mbedtls_cipher_update_ad() */
  132. TEST_ASSERT(
  133. mbedtls_cipher_update_ad( &invalid_ctx,
  134. valid_buffer,
  135. valid_size ) ==
  136. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  137. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  138. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  139. /* mbedtls_cipher_set_padding_mode() */
  140. TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
  141. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  142. #endif
  143. /* mbedtls_cipher_update() */
  144. TEST_ASSERT(
  145. mbedtls_cipher_update( &invalid_ctx,
  146. valid_buffer,
  147. valid_size,
  148. valid_buffer,
  149. &size_t_var ) ==
  150. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  151. /* mbedtls_cipher_finish() */
  152. TEST_ASSERT(
  153. mbedtls_cipher_finish( &invalid_ctx,
  154. valid_buffer,
  155. &size_t_var ) ==
  156. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  157. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  158. /* mbedtls_cipher_write_tag() */
  159. TEST_ASSERT(
  160. mbedtls_cipher_write_tag( &invalid_ctx,
  161. valid_buffer,
  162. valid_size ) ==
  163. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  164. /* mbedtls_cipher_check_tag() */
  165. TEST_ASSERT(
  166. mbedtls_cipher_check_tag( &invalid_ctx,
  167. valid_buffer,
  168. valid_size ) ==
  169. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  170. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  171. exit:
  172. mbedtls_cipher_free( &invalid_ctx );
  173. mbedtls_cipher_free( &valid_ctx );
  174. }
  175. /* END_CASE */
  176. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  177. void cipher_invalid_param_conditional( )
  178. {
  179. mbedtls_cipher_context_t valid_ctx;
  180. mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
  181. mbedtls_operation_t invalid_operation = 100;
  182. mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
  183. unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  184. int valid_size = sizeof(valid_buffer);
  185. int valid_bitlen = valid_size * 8;
  186. const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
  187. *( mbedtls_cipher_list() ) );
  188. size_t size_t_var;
  189. (void)valid_mode; /* In some configurations this is unused */
  190. /* mbedtls_cipher_init() */
  191. TEST_VALID_PARAM( mbedtls_cipher_init( &valid_ctx ) );
  192. TEST_INVALID_PARAM( mbedtls_cipher_init( NULL ) );
  193. /* mbedtls_cipher_setup() */
  194. TEST_VALID_PARAM( mbedtls_cipher_setup( &valid_ctx, valid_info ) );
  195. TEST_INVALID_PARAM_RET(
  196. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  197. mbedtls_cipher_setup( NULL, valid_info ) );
  198. /* mbedtls_cipher_get_block_size() */
  199. TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_block_size( NULL ) );
  200. /* mbedtls_cipher_get_cipher_mode() */
  201. TEST_INVALID_PARAM_RET(
  202. MBEDTLS_MODE_NONE,
  203. mbedtls_cipher_get_cipher_mode( NULL ) );
  204. /* mbedtls_cipher_get_iv_size() */
  205. TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_iv_size( NULL ) );
  206. /* mbedtls_cipher_get_type() */
  207. TEST_INVALID_PARAM_RET(
  208. MBEDTLS_CIPHER_NONE,
  209. mbedtls_cipher_get_type( NULL ) );
  210. /* mbedtls_cipher_get_name() */
  211. TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_name( NULL ) );
  212. /* mbedtls_cipher_get_key_bitlen() */
  213. TEST_INVALID_PARAM_RET(
  214. MBEDTLS_KEY_LENGTH_NONE,
  215. mbedtls_cipher_get_key_bitlen( NULL ) );
  216. /* mbedtls_cipher_get_operation() */
  217. TEST_INVALID_PARAM_RET(
  218. MBEDTLS_OPERATION_NONE,
  219. mbedtls_cipher_get_operation( NULL ) );
  220. /* mbedtls_cipher_setkey() */
  221. TEST_INVALID_PARAM_RET(
  222. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  223. mbedtls_cipher_setkey( NULL,
  224. valid_buffer,
  225. valid_bitlen,
  226. valid_operation ) );
  227. TEST_INVALID_PARAM_RET(
  228. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  229. mbedtls_cipher_setkey( &valid_ctx,
  230. NULL,
  231. valid_bitlen,
  232. valid_operation ) );
  233. TEST_INVALID_PARAM_RET(
  234. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  235. mbedtls_cipher_setkey( &valid_ctx,
  236. valid_buffer,
  237. valid_bitlen,
  238. invalid_operation ) );
  239. /* mbedtls_cipher_set_iv() */
  240. TEST_INVALID_PARAM_RET(
  241. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  242. mbedtls_cipher_set_iv( NULL,
  243. valid_buffer,
  244. valid_size ) );
  245. TEST_INVALID_PARAM_RET(
  246. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  247. mbedtls_cipher_set_iv( &valid_ctx,
  248. NULL,
  249. valid_size ) );
  250. /* mbedtls_cipher_reset() */
  251. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  252. mbedtls_cipher_reset( NULL ) );
  253. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  254. /* mbedtls_cipher_update_ad() */
  255. TEST_INVALID_PARAM_RET(
  256. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  257. mbedtls_cipher_update_ad( NULL,
  258. valid_buffer,
  259. valid_size ) );
  260. TEST_INVALID_PARAM_RET(
  261. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  262. mbedtls_cipher_update_ad( &valid_ctx,
  263. NULL,
  264. valid_size ) );
  265. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  266. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  267. /* mbedtls_cipher_set_padding_mode() */
  268. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  269. mbedtls_cipher_set_padding_mode( NULL, valid_mode ) );
  270. #endif
  271. /* mbedtls_cipher_update() */
  272. TEST_INVALID_PARAM_RET(
  273. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  274. mbedtls_cipher_update( NULL,
  275. valid_buffer,
  276. valid_size,
  277. valid_buffer,
  278. &size_t_var ) );
  279. TEST_INVALID_PARAM_RET(
  280. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  281. mbedtls_cipher_update( &valid_ctx,
  282. NULL, valid_size,
  283. valid_buffer,
  284. &size_t_var ) );
  285. TEST_INVALID_PARAM_RET(
  286. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  287. mbedtls_cipher_update( &valid_ctx,
  288. valid_buffer, valid_size,
  289. NULL,
  290. &size_t_var ) );
  291. TEST_INVALID_PARAM_RET(
  292. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  293. mbedtls_cipher_update( &valid_ctx,
  294. valid_buffer, valid_size,
  295. valid_buffer,
  296. NULL ) );
  297. /* mbedtls_cipher_finish() */
  298. TEST_INVALID_PARAM_RET(
  299. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  300. mbedtls_cipher_finish( NULL,
  301. valid_buffer,
  302. &size_t_var ) );
  303. TEST_INVALID_PARAM_RET(
  304. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  305. mbedtls_cipher_finish( &valid_ctx,
  306. NULL,
  307. &size_t_var ) );
  308. TEST_INVALID_PARAM_RET(
  309. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  310. mbedtls_cipher_finish( &valid_ctx,
  311. valid_buffer,
  312. NULL ) );
  313. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  314. /* mbedtls_cipher_write_tag() */
  315. TEST_INVALID_PARAM_RET(
  316. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  317. mbedtls_cipher_write_tag( NULL,
  318. valid_buffer,
  319. valid_size ) );
  320. TEST_INVALID_PARAM_RET(
  321. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  322. mbedtls_cipher_write_tag( &valid_ctx,
  323. NULL,
  324. valid_size ) );
  325. /* mbedtls_cipher_check_tag() */
  326. TEST_INVALID_PARAM_RET(
  327. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  328. mbedtls_cipher_check_tag( NULL,
  329. valid_buffer,
  330. valid_size ) );
  331. TEST_INVALID_PARAM_RET(
  332. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  333. mbedtls_cipher_check_tag( &valid_ctx,
  334. NULL,
  335. valid_size ) );
  336. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  337. /* mbedtls_cipher_crypt() */
  338. TEST_INVALID_PARAM_RET(
  339. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  340. mbedtls_cipher_crypt( NULL,
  341. valid_buffer, valid_size,
  342. valid_buffer, valid_size,
  343. valid_buffer, &size_t_var ) );
  344. TEST_INVALID_PARAM_RET(
  345. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  346. mbedtls_cipher_crypt( &valid_ctx,
  347. NULL, valid_size,
  348. valid_buffer, valid_size,
  349. valid_buffer, &size_t_var ) );
  350. TEST_INVALID_PARAM_RET(
  351. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  352. mbedtls_cipher_crypt( &valid_ctx,
  353. valid_buffer, valid_size,
  354. NULL, valid_size,
  355. valid_buffer, &size_t_var ) );
  356. TEST_INVALID_PARAM_RET(
  357. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  358. mbedtls_cipher_crypt( &valid_ctx,
  359. valid_buffer, valid_size,
  360. valid_buffer, valid_size,
  361. NULL, &size_t_var ) );
  362. TEST_INVALID_PARAM_RET(
  363. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  364. mbedtls_cipher_crypt( &valid_ctx,
  365. valid_buffer, valid_size,
  366. valid_buffer, valid_size,
  367. valid_buffer, NULL ) );
  368. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  369. /* mbedtls_cipher_auth_encrypt() */
  370. TEST_INVALID_PARAM_RET(
  371. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  372. mbedtls_cipher_auth_encrypt( NULL,
  373. valid_buffer, valid_size,
  374. valid_buffer, valid_size,
  375. valid_buffer, valid_size,
  376. valid_buffer, &size_t_var,
  377. valid_buffer, valid_size ) );
  378. TEST_INVALID_PARAM_RET(
  379. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  380. mbedtls_cipher_auth_encrypt( &valid_ctx,
  381. NULL, valid_size,
  382. valid_buffer, valid_size,
  383. valid_buffer, valid_size,
  384. valid_buffer, &size_t_var,
  385. valid_buffer, valid_size ) );
  386. TEST_INVALID_PARAM_RET(
  387. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  388. mbedtls_cipher_auth_encrypt( &valid_ctx,
  389. valid_buffer, valid_size,
  390. NULL, valid_size,
  391. valid_buffer, valid_size,
  392. valid_buffer, &size_t_var,
  393. valid_buffer, valid_size ) );
  394. TEST_INVALID_PARAM_RET(
  395. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  396. mbedtls_cipher_auth_encrypt( &valid_ctx,
  397. valid_buffer, valid_size,
  398. valid_buffer, valid_size,
  399. NULL, valid_size,
  400. valid_buffer, &size_t_var,
  401. valid_buffer, valid_size ) );
  402. TEST_INVALID_PARAM_RET(
  403. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  404. mbedtls_cipher_auth_encrypt( &valid_ctx,
  405. valid_buffer, valid_size,
  406. valid_buffer, valid_size,
  407. valid_buffer, valid_size,
  408. NULL, &size_t_var,
  409. valid_buffer, valid_size ) );
  410. TEST_INVALID_PARAM_RET(
  411. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  412. mbedtls_cipher_auth_encrypt( &valid_ctx,
  413. valid_buffer, valid_size,
  414. valid_buffer, valid_size,
  415. valid_buffer, valid_size,
  416. valid_buffer, NULL,
  417. valid_buffer, valid_size ) );
  418. TEST_INVALID_PARAM_RET(
  419. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  420. mbedtls_cipher_auth_encrypt( &valid_ctx,
  421. valid_buffer, valid_size,
  422. valid_buffer, valid_size,
  423. valid_buffer, valid_size,
  424. valid_buffer, &size_t_var,
  425. NULL, valid_size ) );
  426. /* mbedtls_cipher_auth_decrypt() */
  427. TEST_INVALID_PARAM_RET(
  428. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  429. mbedtls_cipher_auth_decrypt( NULL,
  430. valid_buffer, valid_size,
  431. valid_buffer, valid_size,
  432. valid_buffer, valid_size,
  433. valid_buffer, &size_t_var,
  434. valid_buffer, valid_size ) );
  435. TEST_INVALID_PARAM_RET(
  436. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  437. mbedtls_cipher_auth_decrypt( &valid_ctx,
  438. NULL, valid_size,
  439. valid_buffer, valid_size,
  440. valid_buffer, valid_size,
  441. valid_buffer, &size_t_var,
  442. valid_buffer, valid_size ) );
  443. TEST_INVALID_PARAM_RET(
  444. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  445. mbedtls_cipher_auth_decrypt( &valid_ctx,
  446. valid_buffer, valid_size,
  447. NULL, valid_size,
  448. valid_buffer, valid_size,
  449. valid_buffer, &size_t_var,
  450. valid_buffer, valid_size ) );
  451. TEST_INVALID_PARAM_RET(
  452. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  453. mbedtls_cipher_auth_decrypt( &valid_ctx,
  454. valid_buffer, valid_size,
  455. valid_buffer, valid_size,
  456. NULL, valid_size,
  457. valid_buffer, &size_t_var,
  458. valid_buffer, valid_size ) );
  459. TEST_INVALID_PARAM_RET(
  460. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  461. mbedtls_cipher_auth_decrypt( &valid_ctx,
  462. valid_buffer, valid_size,
  463. valid_buffer, valid_size,
  464. valid_buffer, valid_size,
  465. NULL, &size_t_var,
  466. valid_buffer, valid_size ) );
  467. TEST_INVALID_PARAM_RET(
  468. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  469. mbedtls_cipher_auth_decrypt( &valid_ctx,
  470. valid_buffer, valid_size,
  471. valid_buffer, valid_size,
  472. valid_buffer, valid_size,
  473. valid_buffer, NULL,
  474. valid_buffer, valid_size ) );
  475. TEST_INVALID_PARAM_RET(
  476. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  477. mbedtls_cipher_auth_decrypt( &valid_ctx,
  478. valid_buffer, valid_size,
  479. valid_buffer, valid_size,
  480. valid_buffer, valid_size,
  481. valid_buffer, &size_t_var,
  482. NULL, valid_size ) );
  483. #endif /* defined(MBEDTLS_CIPHER_MODE_AEAD) */
  484. #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
  485. /* mbedtls_cipher_auth_encrypt_ext */
  486. TEST_INVALID_PARAM_RET(
  487. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  488. mbedtls_cipher_auth_encrypt_ext( NULL,
  489. valid_buffer, valid_size,
  490. valid_buffer, valid_size,
  491. valid_buffer, valid_size,
  492. valid_buffer, valid_size, &size_t_var,
  493. valid_size ) );
  494. TEST_INVALID_PARAM_RET(
  495. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  496. mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
  497. NULL, valid_size,
  498. valid_buffer, valid_size,
  499. valid_buffer, valid_size,
  500. valid_buffer, valid_size, &size_t_var,
  501. valid_size ) );
  502. TEST_INVALID_PARAM_RET(
  503. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  504. mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
  505. valid_buffer, valid_size,
  506. NULL, valid_size,
  507. valid_buffer, valid_size,
  508. valid_buffer, valid_size, &size_t_var,
  509. valid_size ) );
  510. TEST_INVALID_PARAM_RET(
  511. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  512. mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
  513. valid_buffer, valid_size,
  514. valid_buffer, valid_size,
  515. NULL, valid_size,
  516. valid_buffer, valid_size, &size_t_var,
  517. valid_size ) );
  518. TEST_INVALID_PARAM_RET(
  519. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  520. mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
  521. valid_buffer, valid_size,
  522. valid_buffer, valid_size,
  523. valid_buffer, valid_size,
  524. NULL, valid_size, &size_t_var,
  525. valid_size ) );
  526. TEST_INVALID_PARAM_RET(
  527. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  528. mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
  529. valid_buffer, valid_size,
  530. valid_buffer, valid_size,
  531. valid_buffer, valid_size,
  532. valid_buffer, valid_size, NULL,
  533. valid_size ) );
  534. /* mbedtls_cipher_auth_decrypt_ext */
  535. TEST_INVALID_PARAM_RET(
  536. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  537. mbedtls_cipher_auth_decrypt_ext( NULL,
  538. valid_buffer, valid_size,
  539. valid_buffer, valid_size,
  540. valid_buffer, valid_size,
  541. valid_buffer, valid_size, &size_t_var,
  542. valid_size ) );
  543. TEST_INVALID_PARAM_RET(
  544. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  545. mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
  546. NULL, valid_size,
  547. valid_buffer, valid_size,
  548. valid_buffer, valid_size,
  549. valid_buffer, valid_size, &size_t_var,
  550. valid_size ) );
  551. TEST_INVALID_PARAM_RET(
  552. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  553. mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
  554. valid_buffer, valid_size,
  555. NULL, valid_size,
  556. valid_buffer, valid_size,
  557. valid_buffer, valid_size, &size_t_var,
  558. valid_size ) );
  559. TEST_INVALID_PARAM_RET(
  560. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  561. mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
  562. valid_buffer, valid_size,
  563. valid_buffer, valid_size,
  564. NULL, valid_size,
  565. valid_buffer, valid_size, &size_t_var,
  566. valid_size ) );
  567. TEST_INVALID_PARAM_RET(
  568. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  569. mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
  570. valid_buffer, valid_size,
  571. valid_buffer, valid_size,
  572. valid_buffer, valid_size,
  573. NULL, valid_size, &size_t_var,
  574. valid_size ) );
  575. TEST_INVALID_PARAM_RET(
  576. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  577. mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
  578. valid_buffer, valid_size,
  579. valid_buffer, valid_size,
  580. valid_buffer, valid_size,
  581. valid_buffer, valid_size, NULL,
  582. valid_size ) );
  583. #endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
  584. /* mbedtls_cipher_free() */
  585. TEST_VALID_PARAM( mbedtls_cipher_free( NULL ) );
  586. exit:
  587. TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) );
  588. }
  589. /* END_CASE */
  590. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  591. void cipher_special_behaviours( )
  592. {
  593. const mbedtls_cipher_info_t *cipher_info;
  594. mbedtls_cipher_context_t ctx;
  595. unsigned char input[32];
  596. unsigned char output[32];
  597. #if defined (MBEDTLS_CIPHER_MODE_CBC)
  598. unsigned char iv[32];
  599. #endif
  600. size_t olen = 0;
  601. mbedtls_cipher_init( &ctx );
  602. memset( input, 0, sizeof( input ) );
  603. memset( output, 0, sizeof( output ) );
  604. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  605. memset( iv, 0, sizeof( iv ) );
  606. /* Check and get info structures */
  607. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
  608. TEST_ASSERT( NULL != cipher_info );
  609. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  610. /* IV too big */
  611. TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
  612. == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  613. /* IV too small */
  614. TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
  615. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  616. mbedtls_cipher_free( &ctx );
  617. mbedtls_cipher_init( &ctx );
  618. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  619. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
  620. TEST_ASSERT( NULL != cipher_info );
  621. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  622. /* Update ECB with partial block */
  623. TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
  624. == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  625. exit:
  626. mbedtls_cipher_free( &ctx );
  627. }
  628. /* END_CASE */
  629. /* BEGIN_CASE */
  630. void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
  631. int length_val, int pad_mode )
  632. {
  633. size_t length = length_val, outlen, total_len, i, block_size;
  634. unsigned char key[64];
  635. unsigned char iv[16];
  636. unsigned char ad[13];
  637. unsigned char tag[16];
  638. unsigned char inbuf[64];
  639. unsigned char encbuf[64];
  640. unsigned char decbuf[64];
  641. const mbedtls_cipher_info_t *cipher_info;
  642. mbedtls_cipher_context_t ctx_dec;
  643. mbedtls_cipher_context_t ctx_enc;
  644. /*
  645. * Prepare contexts
  646. */
  647. mbedtls_cipher_init( &ctx_dec );
  648. mbedtls_cipher_init( &ctx_enc );
  649. memset( key, 0x2a, sizeof( key ) );
  650. /* Check and get info structures */
  651. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  652. TEST_ASSERT( NULL != cipher_info );
  653. TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
  654. /* Initialise enc and dec contexts */
  655. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  656. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
  657. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
  658. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
  659. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  660. if( -1 != pad_mode )
  661. {
  662. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
  663. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
  664. }
  665. #else
  666. (void) pad_mode;
  667. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  668. /*
  669. * Do a few encode/decode cycles
  670. */
  671. for( i = 0; i < 3; i++ )
  672. {
  673. memset( iv , 0x00 + i, sizeof( iv ) );
  674. memset( ad, 0x10 + i, sizeof( ad ) );
  675. memset( inbuf, 0x20 + i, sizeof( inbuf ) );
  676. memset( encbuf, 0, sizeof( encbuf ) );
  677. memset( decbuf, 0, sizeof( decbuf ) );
  678. memset( tag, 0, sizeof( tag ) );
  679. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
  680. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
  681. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  682. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
  683. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  684. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
  685. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
  686. #endif
  687. block_size = mbedtls_cipher_get_block_size( &ctx_enc );
  688. TEST_ASSERT( block_size != 0 );
  689. /* encode length number of bytes from inbuf */
  690. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
  691. total_len = outlen;
  692. TEST_ASSERT( total_len == length ||
  693. ( total_len % block_size == 0 &&
  694. total_len < length &&
  695. total_len + block_size > length ) );
  696. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
  697. total_len += outlen;
  698. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  699. TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
  700. #endif
  701. TEST_ASSERT( total_len == length ||
  702. ( total_len % block_size == 0 &&
  703. total_len > length &&
  704. total_len <= length + block_size ) );
  705. /* decode the previously encoded string */
  706. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
  707. total_len = outlen;
  708. TEST_ASSERT( total_len == length ||
  709. ( total_len % block_size == 0 &&
  710. total_len < length &&
  711. total_len + block_size >= length ) );
  712. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
  713. total_len += outlen;
  714. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  715. TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
  716. #endif
  717. /* check result */
  718. TEST_ASSERT( total_len == length );
  719. TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
  720. }
  721. /*
  722. * Done
  723. */
  724. exit:
  725. mbedtls_cipher_free( &ctx_dec );
  726. mbedtls_cipher_free( &ctx_enc );
  727. }
  728. /* END_CASE */
  729. /* BEGIN_CASE */
  730. void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
  731. int ret )
  732. {
  733. size_t length = length_val;
  734. unsigned char key[32];
  735. unsigned char iv[16];
  736. const mbedtls_cipher_info_t *cipher_info;
  737. mbedtls_cipher_context_t ctx;
  738. unsigned char inbuf[64];
  739. unsigned char encbuf[64];
  740. size_t outlen = 0;
  741. memset( key, 0, 32 );
  742. memset( iv , 0, 16 );
  743. mbedtls_cipher_init( &ctx );
  744. memset( inbuf, 5, 64 );
  745. memset( encbuf, 0, 64 );
  746. /* Check and get info structures */
  747. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  748. TEST_ASSERT( NULL != cipher_info );
  749. /* Initialise context */
  750. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  751. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
  752. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  753. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  754. #else
  755. (void) pad_mode;
  756. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  757. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
  758. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
  759. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  760. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
  761. #endif
  762. /* encode length number of bytes from inbuf */
  763. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
  764. TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
  765. /* done */
  766. exit:
  767. mbedtls_cipher_free( &ctx );
  768. }
  769. /* END_CASE */
  770. /* BEGIN_CASE */
  771. void dec_empty_buf( int cipher,
  772. int expected_update_ret,
  773. int expected_finish_ret )
  774. {
  775. unsigned char key[32];
  776. unsigned char iv[16];
  777. mbedtls_cipher_context_t ctx_dec;
  778. const mbedtls_cipher_info_t *cipher_info;
  779. unsigned char encbuf[64];
  780. unsigned char decbuf[64];
  781. size_t outlen = 0;
  782. memset( key, 0, 32 );
  783. memset( iv , 0, 16 );
  784. mbedtls_cipher_init( &ctx_dec );
  785. memset( encbuf, 0, 64 );
  786. memset( decbuf, 0, 64 );
  787. /* Initialise context */
  788. cipher_info = mbedtls_cipher_info_from_type( cipher );
  789. TEST_ASSERT( NULL != cipher_info);
  790. TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
  791. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  792. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
  793. key, cipher_info->key_bitlen,
  794. MBEDTLS_DECRYPT ) );
  795. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
  796. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  797. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  798. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
  799. #endif
  800. /* decode 0-byte string */
  801. TEST_ASSERT( expected_update_ret ==
  802. mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
  803. TEST_ASSERT( 0 == outlen );
  804. if ( expected_finish_ret == 0 &&
  805. ( cipher_info->mode == MBEDTLS_MODE_CBC ||
  806. cipher_info->mode == MBEDTLS_MODE_ECB ) )
  807. {
  808. /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
  809. * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
  810. * decrypting an empty buffer.
  811. * On the other hand, CBC and ECB ciphers need a full block of input.
  812. */
  813. expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  814. }
  815. TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
  816. &ctx_dec, decbuf + outlen, &outlen ) );
  817. TEST_ASSERT( 0 == outlen );
  818. exit:
  819. mbedtls_cipher_free( &ctx_dec );
  820. }
  821. /* END_CASE */
  822. /* BEGIN_CASE */
  823. void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
  824. int second_length_val, int pad_mode,
  825. int first_encrypt_output_len, int second_encrypt_output_len,
  826. int first_decrypt_output_len, int second_decrypt_output_len )
  827. {
  828. size_t first_length = first_length_val;
  829. size_t second_length = second_length_val;
  830. size_t length = first_length + second_length;
  831. size_t block_size;
  832. unsigned char key[32];
  833. unsigned char iv[16];
  834. mbedtls_cipher_context_t ctx_dec;
  835. mbedtls_cipher_context_t ctx_enc;
  836. const mbedtls_cipher_info_t *cipher_info;
  837. unsigned char inbuf[64];
  838. unsigned char encbuf[64];
  839. unsigned char decbuf[64];
  840. size_t outlen = 0;
  841. size_t totaloutlen = 0;
  842. memset( key, 0, 32 );
  843. memset( iv , 0, 16 );
  844. mbedtls_cipher_init( &ctx_dec );
  845. mbedtls_cipher_init( &ctx_enc );
  846. memset( inbuf, 5, 64 );
  847. memset( encbuf, 0, 64 );
  848. memset( decbuf, 0, 64 );
  849. /* Initialise enc and dec contexts */
  850. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  851. TEST_ASSERT( NULL != cipher_info);
  852. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  853. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
  854. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
  855. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
  856. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  857. if( -1 != pad_mode )
  858. {
  859. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
  860. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
  861. }
  862. #else
  863. (void) pad_mode;
  864. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  865. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
  866. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
  867. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  868. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
  869. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  870. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
  871. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
  872. #endif
  873. block_size = mbedtls_cipher_get_block_size( &ctx_enc );
  874. TEST_ASSERT( block_size != 0 );
  875. /* encode length number of bytes from inbuf */
  876. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
  877. TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
  878. totaloutlen = outlen;
  879. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
  880. TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
  881. totaloutlen += outlen;
  882. TEST_ASSERT( totaloutlen == length ||
  883. ( totaloutlen % block_size == 0 &&
  884. totaloutlen < length &&
  885. totaloutlen + block_size > length ) );
  886. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
  887. totaloutlen += outlen;
  888. TEST_ASSERT( totaloutlen == length ||
  889. ( totaloutlen % block_size == 0 &&
  890. totaloutlen > length &&
  891. totaloutlen <= length + block_size ) );
  892. /* decode the previously encoded string */
  893. second_length = totaloutlen - first_length;
  894. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
  895. TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
  896. totaloutlen = outlen;
  897. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
  898. TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
  899. totaloutlen += outlen;
  900. TEST_ASSERT( totaloutlen == length ||
  901. ( totaloutlen % block_size == 0 &&
  902. totaloutlen < length &&
  903. totaloutlen + block_size >= length ) );
  904. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
  905. totaloutlen += outlen;
  906. TEST_ASSERT( totaloutlen == length );
  907. TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
  908. exit:
  909. mbedtls_cipher_free( &ctx_dec );
  910. mbedtls_cipher_free( &ctx_enc );
  911. }
  912. /* END_CASE */
  913. /* BEGIN_CASE */
  914. void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
  915. data_t * iv, data_t * cipher,
  916. data_t * clear, data_t * ad, data_t * tag,
  917. int finish_result, int tag_result )
  918. {
  919. unsigned char output[265];
  920. mbedtls_cipher_context_t ctx;
  921. size_t outlen, total_len;
  922. mbedtls_cipher_init( &ctx );
  923. memset( output, 0x00, sizeof( output ) );
  924. #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
  925. ((void) ad);
  926. ((void) tag);
  927. #endif
  928. /* Prepare context */
  929. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  930. mbedtls_cipher_info_from_type( cipher_id ) ) );
  931. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
  932. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  933. if( pad_mode != -1 )
  934. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  935. #else
  936. (void) pad_mode;
  937. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  938. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
  939. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
  940. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  941. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
  942. #endif
  943. /* decode buffer and check tag->x */
  944. total_len = 0;
  945. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
  946. total_len += outlen;
  947. TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
  948. &outlen ) );
  949. total_len += outlen;
  950. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  951. TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
  952. #endif
  953. /* check plaintext only if everything went fine */
  954. if( 0 == finish_result && 0 == tag_result )
  955. {
  956. TEST_ASSERT( total_len == clear->len );
  957. TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
  958. }
  959. exit:
  960. mbedtls_cipher_free( &ctx );
  961. }
  962. /* END_CASE */
  963. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
  964. void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
  965. data_t * ad, data_t * cipher, data_t * tag,
  966. char * result, data_t * clear, int use_psa )
  967. {
  968. /*
  969. * Take an AEAD ciphertext + tag and perform a pair
  970. * of AEAD decryption and AEAD encryption. Check that
  971. * this results in the expected plaintext, and that
  972. * decryption and encryption are inverse to one another.
  973. *
  974. * Do that twice:
  975. * - once with legacy functions auth_decrypt/auth_encrypt
  976. * - once with new functions auth_decrypt_ext/auth_encrypt_ext
  977. * This allows testing both without duplicating test cases.
  978. */
  979. int ret;
  980. int using_nist_kw, using_nist_kw_padding;
  981. mbedtls_cipher_context_t ctx;
  982. size_t outlen;
  983. unsigned char *cipher_plus_tag = NULL;
  984. size_t cipher_plus_tag_len;
  985. unsigned char *decrypt_buf = NULL;
  986. size_t decrypt_buf_len = 0;
  987. unsigned char *encrypt_buf = NULL;
  988. size_t encrypt_buf_len = 0;
  989. #if !defined(MBEDTLS_DEPRECATED_WARNING) && \
  990. !defined(MBEDTLS_DEPRECATED_REMOVED)
  991. unsigned char *tmp_tag = NULL;
  992. unsigned char *tmp_cipher = NULL;
  993. unsigned char *tag_buf = NULL;
  994. #endif /* !MBEDTLS_DEPRECATED_WARNING && !MBEDTLS_DEPRECATED_REMOVED */
  995. /* Null pointers are documented as valid for inputs of length 0.
  996. * The test framework passes non-null pointers, so set them to NULL.
  997. * key, cipher and tag can't be empty. */
  998. if( iv->len == 0 )
  999. iv->x = NULL;
  1000. if( ad->len == 0 )
  1001. ad->x = NULL;
  1002. if( clear->len == 0 )
  1003. clear->x = NULL;
  1004. mbedtls_cipher_init( &ctx );
  1005. /* Initialize PSA Crypto */
  1006. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1007. if( use_psa == 1 )
  1008. PSA_ASSERT( psa_crypto_init( ) );
  1009. #else
  1010. (void) use_psa;
  1011. #endif
  1012. /*
  1013. * Are we using NIST_KW? with padding?
  1014. */
  1015. using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
  1016. cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
  1017. cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
  1018. using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
  1019. cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
  1020. cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
  1021. using_nist_kw_padding;
  1022. /****************************************************************
  1023. * *
  1024. * Part 1: non-deprecated API *
  1025. * *
  1026. ****************************************************************/
  1027. /*
  1028. * Prepare context for decryption
  1029. */
  1030. if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
  1031. MBEDTLS_DECRYPT ) )
  1032. goto exit;
  1033. /*
  1034. * prepare buffer for decryption
  1035. * (we need the tag appended to the ciphertext)
  1036. */
  1037. cipher_plus_tag_len = cipher->len + tag->len;
  1038. ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
  1039. memcpy( cipher_plus_tag, cipher->x, cipher->len );
  1040. memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
  1041. /*
  1042. * Compute length of output buffer according to the documentation
  1043. */
  1044. if( using_nist_kw )
  1045. decrypt_buf_len = cipher_plus_tag_len - 8;
  1046. else
  1047. decrypt_buf_len = cipher_plus_tag_len - tag->len;
  1048. /*
  1049. * Try decrypting to a buffer that's 1B too small
  1050. */
  1051. if( decrypt_buf_len != 0 )
  1052. {
  1053. ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
  1054. outlen = 0;
  1055. ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
  1056. ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
  1057. decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
  1058. TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1059. mbedtls_free( decrypt_buf );
  1060. decrypt_buf = NULL;
  1061. }
  1062. /*
  1063. * Authenticate and decrypt, and check result
  1064. */
  1065. ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
  1066. outlen = 0;
  1067. ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
  1068. ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
  1069. decrypt_buf, decrypt_buf_len, &outlen, tag->len );
  1070. if( strcmp( result, "FAIL" ) == 0 )
  1071. {
  1072. TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  1073. TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
  1074. }
  1075. else
  1076. {
  1077. TEST_ASSERT( ret == 0 );
  1078. ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
  1079. }
  1080. /* Free this, but keep cipher_plus_tag for deprecated function with PSA */
  1081. mbedtls_free( decrypt_buf );
  1082. decrypt_buf = NULL;
  1083. /*
  1084. * Encrypt back if test data was authentic
  1085. */
  1086. if( strcmp( result, "FAIL" ) != 0 )
  1087. {
  1088. /* prepare context for encryption */
  1089. if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
  1090. MBEDTLS_ENCRYPT ) )
  1091. goto exit;
  1092. /*
  1093. * Compute size of output buffer according to documentation
  1094. */
  1095. if( using_nist_kw )
  1096. {
  1097. encrypt_buf_len = clear->len + 8;
  1098. if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
  1099. encrypt_buf_len += 8 - encrypt_buf_len % 8;
  1100. }
  1101. else
  1102. {
  1103. encrypt_buf_len = clear->len + tag->len;
  1104. }
  1105. /*
  1106. * Try encrypting with an output buffer that's 1B too small
  1107. */
  1108. ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
  1109. outlen = 0;
  1110. ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
  1111. ad->x, ad->len, clear->x, clear->len,
  1112. encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
  1113. TEST_ASSERT( ret != 0 );
  1114. mbedtls_free( encrypt_buf );
  1115. encrypt_buf = NULL;
  1116. /*
  1117. * Encrypt and check the result
  1118. */
  1119. ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
  1120. outlen = 0;
  1121. ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
  1122. ad->x, ad->len, clear->x, clear->len,
  1123. encrypt_buf, encrypt_buf_len, &outlen, tag->len );
  1124. TEST_ASSERT( ret == 0 );
  1125. TEST_ASSERT( outlen == cipher->len + tag->len );
  1126. TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
  1127. TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
  1128. tag->x, tag->len ) == 0 );
  1129. mbedtls_free( encrypt_buf );
  1130. encrypt_buf = NULL;
  1131. }
  1132. /****************************************************************
  1133. * *
  1134. * Part 2: deprecated API *
  1135. * *
  1136. ****************************************************************/
  1137. #if !defined(MBEDTLS_DEPRECATED_WARNING) && \
  1138. !defined(MBEDTLS_DEPRECATED_REMOVED)
  1139. /*
  1140. * Prepare context for decryption
  1141. */
  1142. if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
  1143. MBEDTLS_DECRYPT ) )
  1144. goto exit;
  1145. /*
  1146. * Prepare pointers for decryption
  1147. */
  1148. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1149. if( use_psa == 1 )
  1150. {
  1151. /* PSA requires that the tag immediately follows the ciphertext.
  1152. * Fortunately, we already have that from testing the new API. */
  1153. tmp_cipher = cipher_plus_tag;
  1154. tmp_tag = tmp_cipher + cipher->len;
  1155. }
  1156. else
  1157. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1158. {
  1159. tmp_cipher = cipher->x;
  1160. tmp_tag = tag->x;
  1161. }
  1162. /*
  1163. * Authenticate and decrypt, and check result
  1164. */
  1165. ASSERT_ALLOC( decrypt_buf, cipher->len );
  1166. outlen = 0;
  1167. ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
  1168. tmp_cipher, cipher->len, decrypt_buf, &outlen,
  1169. tmp_tag, tag->len );
  1170. if( using_nist_kw )
  1171. {
  1172. /* NIST_KW with legacy API */
  1173. TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  1174. }
  1175. else if( strcmp( result, "FAIL" ) == 0 )
  1176. {
  1177. /* unauthentic message */
  1178. TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  1179. TEST_ASSERT( buffer_is_all_zero( decrypt_buf, cipher->len ) );
  1180. }
  1181. else
  1182. {
  1183. /* authentic message: is the plaintext correct? */
  1184. TEST_ASSERT( ret == 0 );
  1185. ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
  1186. }
  1187. mbedtls_free( decrypt_buf );
  1188. decrypt_buf = NULL;
  1189. mbedtls_free( cipher_plus_tag );
  1190. cipher_plus_tag = NULL;
  1191. /*
  1192. * Encrypt back if test data was authentic
  1193. */
  1194. if( strcmp( result, "FAIL" ) != 0 )
  1195. {
  1196. /* prepare context for encryption */
  1197. if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
  1198. MBEDTLS_ENCRYPT ) )
  1199. goto exit;
  1200. /* prepare buffers for encryption */
  1201. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1202. if( use_psa )
  1203. {
  1204. ASSERT_ALLOC( cipher_plus_tag, cipher->len + tag->len );
  1205. tmp_cipher = cipher_plus_tag;
  1206. tmp_tag = cipher_plus_tag + cipher->len;
  1207. }
  1208. else
  1209. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1210. {
  1211. ASSERT_ALLOC( encrypt_buf, cipher->len );
  1212. ASSERT_ALLOC( tag_buf, tag->len );
  1213. tmp_cipher = encrypt_buf;
  1214. tmp_tag = tag_buf;
  1215. }
  1216. /*
  1217. * Encrypt and check the result
  1218. */
  1219. outlen = 0;
  1220. ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
  1221. clear->x, clear->len, tmp_cipher, &outlen,
  1222. tmp_tag, tag->len );
  1223. if( using_nist_kw )
  1224. {
  1225. TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  1226. }
  1227. else
  1228. {
  1229. TEST_ASSERT( ret == 0 );
  1230. TEST_ASSERT( outlen == cipher->len );
  1231. if( cipher->len != 0 )
  1232. TEST_ASSERT( memcmp( tmp_cipher, cipher->x, cipher->len ) == 0 );
  1233. TEST_ASSERT( memcmp( tmp_tag, tag->x, tag->len ) == 0 );
  1234. }
  1235. }
  1236. #endif /* !MBEDTLS_DEPRECATED_WARNING && !MBEDTLS_DEPRECATED_REMOVED */
  1237. exit:
  1238. mbedtls_cipher_free( &ctx );
  1239. mbedtls_free( decrypt_buf );
  1240. mbedtls_free( encrypt_buf );
  1241. mbedtls_free( cipher_plus_tag );
  1242. #if !defined(MBEDTLS_DEPRECATED_WARNING) && \
  1243. !defined(MBEDTLS_DEPRECATED_REMOVED)
  1244. mbedtls_free( tag_buf );
  1245. #endif /* !MBEDTLS_DEPRECATED_WARNING && !MBEDTLS_DEPRECATED_REMOVED */
  1246. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1247. if( use_psa == 1 )
  1248. PSA_DONE( );
  1249. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1250. }
  1251. /* END_CASE */
  1252. /* BEGIN_CASE */
  1253. void test_vec_ecb( int cipher_id, int operation, data_t * key,
  1254. data_t * input, data_t * result, int finish_result
  1255. )
  1256. {
  1257. mbedtls_cipher_context_t ctx;
  1258. unsigned char output[32];
  1259. size_t outlen;
  1260. mbedtls_cipher_init( &ctx );
  1261. memset( output, 0x00, sizeof( output ) );
  1262. /* Prepare context */
  1263. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  1264. mbedtls_cipher_info_from_type( cipher_id ) ) );
  1265. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
  1266. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
  1267. mbedtls_cipher_get_block_size( &ctx ),
  1268. output, &outlen ) );
  1269. TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
  1270. TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
  1271. &outlen ) );
  1272. TEST_ASSERT( 0 == outlen );
  1273. /* check plaintext only if everything went fine */
  1274. if( 0 == finish_result )
  1275. TEST_ASSERT( 0 == memcmp( output, result->x,
  1276. mbedtls_cipher_get_block_size( &ctx ) ) );
  1277. exit:
  1278. mbedtls_cipher_free( &ctx );
  1279. }
  1280. /* END_CASE */
  1281. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
  1282. void test_vec_crypt( int cipher_id, int operation, data_t *key,
  1283. data_t *iv, data_t *input, data_t *result,
  1284. int finish_result, int use_psa )
  1285. {
  1286. mbedtls_cipher_context_t ctx;
  1287. unsigned char output[32];
  1288. size_t outlen;
  1289. mbedtls_cipher_init( &ctx );
  1290. memset( output, 0x00, sizeof( output ) );
  1291. /* Prepare context */
  1292. #if !defined(MBEDTLS_USE_PSA_CRYPTO)
  1293. (void) use_psa;
  1294. #else
  1295. if( use_psa == 1 )
  1296. {
  1297. PSA_ASSERT( psa_crypto_init( ) );
  1298. TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
  1299. mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
  1300. }
  1301. else
  1302. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1303. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  1304. mbedtls_cipher_info_from_type( cipher_id ) ) );
  1305. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
  1306. if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
  1307. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
  1308. TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
  1309. iv->len, input->x, input->len,
  1310. output, &outlen ) );
  1311. TEST_ASSERT( result->len == outlen );
  1312. /* check plaintext only if everything went fine */
  1313. if( 0 == finish_result )
  1314. TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
  1315. exit:
  1316. mbedtls_cipher_free( &ctx );
  1317. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1318. PSA_DONE( );
  1319. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1320. }
  1321. /* END_CASE */
  1322. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
  1323. void set_padding( int cipher_id, int pad_mode, int ret )
  1324. {
  1325. const mbedtls_cipher_info_t *cipher_info;
  1326. mbedtls_cipher_context_t ctx;
  1327. mbedtls_cipher_init( &ctx );
  1328. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  1329. TEST_ASSERT( NULL != cipher_info );
  1330. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  1331. TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  1332. exit:
  1333. mbedtls_cipher_free( &ctx );
  1334. }
  1335. /* END_CASE */
  1336. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  1337. void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
  1338. )
  1339. {
  1340. mbedtls_cipher_info_t cipher_info;
  1341. mbedtls_cipher_context_t ctx;
  1342. size_t dlen;
  1343. /* build a fake context just for getting access to get_padding */
  1344. mbedtls_cipher_init( &ctx );
  1345. cipher_info.mode = MBEDTLS_MODE_CBC;
  1346. ctx.cipher_info = &cipher_info;
  1347. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  1348. TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
  1349. if( 0 == ret )
  1350. TEST_ASSERT( dlen == (size_t) dlen_check );
  1351. }
  1352. /* END_CASE */