test_suite_cipher.function 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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_invalid_param_unconditional( )
  21. {
  22. mbedtls_cipher_context_t valid_ctx;
  23. mbedtls_cipher_context_t invalid_ctx;
  24. mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
  25. mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
  26. unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  27. int valid_size = sizeof(valid_buffer);
  28. int valid_bitlen = valid_size * 8;
  29. const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
  30. *( mbedtls_cipher_list() ) );
  31. size_t size_t_var;
  32. (void)valid_mode; /* In some configurations this is unused */
  33. mbedtls_cipher_init( &valid_ctx );
  34. mbedtls_cipher_setup( &valid_ctx, valid_info );
  35. mbedtls_cipher_init( &invalid_ctx );
  36. /* mbedtls_cipher_setup() */
  37. TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
  38. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  39. /* mbedtls_cipher_get_block_size() */
  40. TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
  41. /* mbedtls_cipher_get_cipher_mode() */
  42. TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
  43. MBEDTLS_MODE_NONE );
  44. /* mbedtls_cipher_get_iv_size() */
  45. TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
  46. /* mbedtls_cipher_get_type() */
  47. TEST_ASSERT(
  48. mbedtls_cipher_get_type( &invalid_ctx ) ==
  49. MBEDTLS_CIPHER_NONE);
  50. /* mbedtls_cipher_get_name() */
  51. TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
  52. /* mbedtls_cipher_get_key_bitlen() */
  53. TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
  54. MBEDTLS_KEY_LENGTH_NONE );
  55. /* mbedtls_cipher_get_operation() */
  56. TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
  57. MBEDTLS_OPERATION_NONE );
  58. /* mbedtls_cipher_setkey() */
  59. TEST_ASSERT(
  60. mbedtls_cipher_setkey( &invalid_ctx,
  61. valid_buffer,
  62. valid_bitlen,
  63. valid_operation ) ==
  64. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  65. /* mbedtls_cipher_set_iv() */
  66. TEST_ASSERT(
  67. mbedtls_cipher_set_iv( &invalid_ctx,
  68. valid_buffer,
  69. valid_size ) ==
  70. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  71. /* mbedtls_cipher_reset() */
  72. TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
  73. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  74. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  75. /* mbedtls_cipher_update_ad() */
  76. TEST_ASSERT(
  77. mbedtls_cipher_update_ad( &invalid_ctx,
  78. valid_buffer,
  79. valid_size ) ==
  80. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  81. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  82. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  83. /* mbedtls_cipher_set_padding_mode() */
  84. TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
  85. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  86. #endif
  87. /* mbedtls_cipher_update() */
  88. TEST_ASSERT(
  89. mbedtls_cipher_update( &invalid_ctx,
  90. valid_buffer,
  91. valid_size,
  92. valid_buffer,
  93. &size_t_var ) ==
  94. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  95. /* mbedtls_cipher_finish() */
  96. TEST_ASSERT(
  97. mbedtls_cipher_finish( &invalid_ctx,
  98. valid_buffer,
  99. &size_t_var ) ==
  100. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  101. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  102. /* mbedtls_cipher_write_tag() */
  103. TEST_ASSERT(
  104. mbedtls_cipher_write_tag( &invalid_ctx,
  105. valid_buffer,
  106. valid_size ) ==
  107. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  108. /* mbedtls_cipher_check_tag() */
  109. TEST_ASSERT(
  110. mbedtls_cipher_check_tag( &invalid_ctx,
  111. valid_buffer,
  112. valid_size ) ==
  113. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  114. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  115. exit:
  116. mbedtls_cipher_free( &invalid_ctx );
  117. mbedtls_cipher_free( &valid_ctx );
  118. }
  119. /* END_CASE */
  120. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  121. void cipher_invalid_param_conditional( )
  122. {
  123. mbedtls_cipher_context_t valid_ctx;
  124. mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
  125. mbedtls_operation_t invalid_operation = 100;
  126. mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
  127. unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  128. int valid_size = sizeof(valid_buffer);
  129. int valid_bitlen = valid_size * 8;
  130. const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
  131. *( mbedtls_cipher_list() ) );
  132. size_t size_t_var;
  133. (void)valid_mode; /* In some configurations this is unused */
  134. /* mbedtls_cipher_init() */
  135. TEST_VALID_PARAM( mbedtls_cipher_init( &valid_ctx ) );
  136. TEST_INVALID_PARAM( mbedtls_cipher_init( NULL ) );
  137. /* mbedtls_cipher_setup() */
  138. TEST_VALID_PARAM( mbedtls_cipher_setup( &valid_ctx, valid_info ) );
  139. TEST_INVALID_PARAM_RET(
  140. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  141. mbedtls_cipher_setup( NULL, valid_info ) );
  142. /* mbedtls_cipher_get_block_size() */
  143. TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_block_size( NULL ) );
  144. /* mbedtls_cipher_get_cipher_mode() */
  145. TEST_INVALID_PARAM_RET(
  146. MBEDTLS_MODE_NONE,
  147. mbedtls_cipher_get_cipher_mode( NULL ) );
  148. /* mbedtls_cipher_get_iv_size() */
  149. TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_iv_size( NULL ) );
  150. /* mbedtls_cipher_get_type() */
  151. TEST_INVALID_PARAM_RET(
  152. MBEDTLS_CIPHER_NONE,
  153. mbedtls_cipher_get_type( NULL ) );
  154. /* mbedtls_cipher_get_name() */
  155. TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_name( NULL ) );
  156. /* mbedtls_cipher_get_key_bitlen() */
  157. TEST_INVALID_PARAM_RET(
  158. MBEDTLS_KEY_LENGTH_NONE,
  159. mbedtls_cipher_get_key_bitlen( NULL ) );
  160. /* mbedtls_cipher_get_operation() */
  161. TEST_INVALID_PARAM_RET(
  162. MBEDTLS_OPERATION_NONE,
  163. mbedtls_cipher_get_operation( NULL ) );
  164. /* mbedtls_cipher_setkey() */
  165. TEST_INVALID_PARAM_RET(
  166. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  167. mbedtls_cipher_setkey( NULL,
  168. valid_buffer,
  169. valid_bitlen,
  170. valid_operation ) );
  171. TEST_INVALID_PARAM_RET(
  172. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  173. mbedtls_cipher_setkey( &valid_ctx,
  174. NULL,
  175. valid_bitlen,
  176. valid_operation ) );
  177. TEST_INVALID_PARAM_RET(
  178. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  179. mbedtls_cipher_setkey( &valid_ctx,
  180. valid_buffer,
  181. valid_bitlen,
  182. invalid_operation ) );
  183. /* mbedtls_cipher_set_iv() */
  184. TEST_INVALID_PARAM_RET(
  185. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  186. mbedtls_cipher_set_iv( NULL,
  187. valid_buffer,
  188. valid_size ) );
  189. TEST_INVALID_PARAM_RET(
  190. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  191. mbedtls_cipher_set_iv( &valid_ctx,
  192. NULL,
  193. valid_size ) );
  194. /* mbedtls_cipher_reset() */
  195. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  196. mbedtls_cipher_reset( NULL ) );
  197. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  198. /* mbedtls_cipher_update_ad() */
  199. TEST_INVALID_PARAM_RET(
  200. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  201. mbedtls_cipher_update_ad( NULL,
  202. valid_buffer,
  203. valid_size ) );
  204. TEST_INVALID_PARAM_RET(
  205. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  206. mbedtls_cipher_update_ad( &valid_ctx,
  207. NULL,
  208. valid_size ) );
  209. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  210. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  211. /* mbedtls_cipher_set_padding_mode() */
  212. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  213. mbedtls_cipher_set_padding_mode( NULL, valid_mode ) );
  214. #endif
  215. /* mbedtls_cipher_update() */
  216. TEST_INVALID_PARAM_RET(
  217. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  218. mbedtls_cipher_update( NULL,
  219. valid_buffer,
  220. valid_size,
  221. valid_buffer,
  222. &size_t_var ) );
  223. TEST_INVALID_PARAM_RET(
  224. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  225. mbedtls_cipher_update( &valid_ctx,
  226. NULL, valid_size,
  227. valid_buffer,
  228. &size_t_var ) );
  229. TEST_INVALID_PARAM_RET(
  230. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  231. mbedtls_cipher_update( &valid_ctx,
  232. valid_buffer, valid_size,
  233. NULL,
  234. &size_t_var ) );
  235. TEST_INVALID_PARAM_RET(
  236. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  237. mbedtls_cipher_update( &valid_ctx,
  238. valid_buffer, valid_size,
  239. valid_buffer,
  240. NULL ) );
  241. /* mbedtls_cipher_finish() */
  242. TEST_INVALID_PARAM_RET(
  243. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  244. mbedtls_cipher_finish( NULL,
  245. valid_buffer,
  246. &size_t_var ) );
  247. TEST_INVALID_PARAM_RET(
  248. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  249. mbedtls_cipher_finish( &valid_ctx,
  250. NULL,
  251. &size_t_var ) );
  252. TEST_INVALID_PARAM_RET(
  253. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  254. mbedtls_cipher_finish( &valid_ctx,
  255. valid_buffer,
  256. NULL ) );
  257. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  258. /* mbedtls_cipher_write_tag() */
  259. TEST_INVALID_PARAM_RET(
  260. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  261. mbedtls_cipher_write_tag( NULL,
  262. valid_buffer,
  263. valid_size ) );
  264. TEST_INVALID_PARAM_RET(
  265. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  266. mbedtls_cipher_write_tag( &valid_ctx,
  267. NULL,
  268. valid_size ) );
  269. /* mbedtls_cipher_check_tag() */
  270. TEST_INVALID_PARAM_RET(
  271. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  272. mbedtls_cipher_check_tag( NULL,
  273. valid_buffer,
  274. valid_size ) );
  275. TEST_INVALID_PARAM_RET(
  276. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  277. mbedtls_cipher_check_tag( &valid_ctx,
  278. NULL,
  279. valid_size ) );
  280. #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
  281. /* mbedtls_cipher_crypt() */
  282. TEST_INVALID_PARAM_RET(
  283. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  284. mbedtls_cipher_crypt( NULL,
  285. valid_buffer, valid_size,
  286. valid_buffer, valid_size,
  287. valid_buffer, &size_t_var ) );
  288. TEST_INVALID_PARAM_RET(
  289. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  290. mbedtls_cipher_crypt( &valid_ctx,
  291. NULL, valid_size,
  292. valid_buffer, valid_size,
  293. valid_buffer, &size_t_var ) );
  294. TEST_INVALID_PARAM_RET(
  295. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  296. mbedtls_cipher_crypt( &valid_ctx,
  297. valid_buffer, valid_size,
  298. NULL, valid_size,
  299. valid_buffer, &size_t_var ) );
  300. TEST_INVALID_PARAM_RET(
  301. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  302. mbedtls_cipher_crypt( &valid_ctx,
  303. valid_buffer, valid_size,
  304. valid_buffer, valid_size,
  305. NULL, &size_t_var ) );
  306. TEST_INVALID_PARAM_RET(
  307. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  308. mbedtls_cipher_crypt( &valid_ctx,
  309. valid_buffer, valid_size,
  310. valid_buffer, valid_size,
  311. valid_buffer, NULL ) );
  312. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  313. /* mbedtls_cipher_auth_encrypt() */
  314. TEST_INVALID_PARAM_RET(
  315. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  316. mbedtls_cipher_auth_encrypt( NULL,
  317. valid_buffer, valid_size,
  318. valid_buffer, valid_size,
  319. valid_buffer, valid_size,
  320. valid_buffer, &size_t_var,
  321. valid_buffer, valid_size ) );
  322. TEST_INVALID_PARAM_RET(
  323. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  324. mbedtls_cipher_auth_encrypt( &valid_ctx,
  325. NULL, valid_size,
  326. valid_buffer, valid_size,
  327. valid_buffer, valid_size,
  328. valid_buffer, &size_t_var,
  329. valid_buffer, valid_size ) );
  330. TEST_INVALID_PARAM_RET(
  331. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  332. mbedtls_cipher_auth_encrypt( &valid_ctx,
  333. valid_buffer, valid_size,
  334. NULL, valid_size,
  335. valid_buffer, valid_size,
  336. valid_buffer, &size_t_var,
  337. valid_buffer, valid_size ) );
  338. TEST_INVALID_PARAM_RET(
  339. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  340. mbedtls_cipher_auth_encrypt( &valid_ctx,
  341. valid_buffer, valid_size,
  342. valid_buffer, valid_size,
  343. NULL, valid_size,
  344. valid_buffer, &size_t_var,
  345. valid_buffer, valid_size ) );
  346. TEST_INVALID_PARAM_RET(
  347. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  348. mbedtls_cipher_auth_encrypt( &valid_ctx,
  349. valid_buffer, valid_size,
  350. valid_buffer, valid_size,
  351. valid_buffer, valid_size,
  352. NULL, &size_t_var,
  353. valid_buffer, valid_size ) );
  354. TEST_INVALID_PARAM_RET(
  355. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  356. mbedtls_cipher_auth_encrypt( &valid_ctx,
  357. valid_buffer, valid_size,
  358. valid_buffer, valid_size,
  359. valid_buffer, valid_size,
  360. valid_buffer, NULL,
  361. valid_buffer, valid_size ) );
  362. TEST_INVALID_PARAM_RET(
  363. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  364. mbedtls_cipher_auth_encrypt( &valid_ctx,
  365. valid_buffer, valid_size,
  366. valid_buffer, valid_size,
  367. valid_buffer, valid_size,
  368. valid_buffer, &size_t_var,
  369. NULL, valid_size ) );
  370. /* mbedtls_cipher_auth_decrypt() */
  371. TEST_INVALID_PARAM_RET(
  372. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  373. mbedtls_cipher_auth_decrypt( NULL,
  374. valid_buffer, valid_size,
  375. valid_buffer, valid_size,
  376. valid_buffer, valid_size,
  377. valid_buffer, &size_t_var,
  378. valid_buffer, valid_size ) );
  379. TEST_INVALID_PARAM_RET(
  380. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  381. mbedtls_cipher_auth_decrypt( &valid_ctx,
  382. NULL, valid_size,
  383. valid_buffer, valid_size,
  384. valid_buffer, valid_size,
  385. valid_buffer, &size_t_var,
  386. valid_buffer, valid_size ) );
  387. TEST_INVALID_PARAM_RET(
  388. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  389. mbedtls_cipher_auth_decrypt( &valid_ctx,
  390. valid_buffer, valid_size,
  391. NULL, valid_size,
  392. valid_buffer, valid_size,
  393. valid_buffer, &size_t_var,
  394. valid_buffer, valid_size ) );
  395. TEST_INVALID_PARAM_RET(
  396. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  397. mbedtls_cipher_auth_decrypt( &valid_ctx,
  398. valid_buffer, valid_size,
  399. valid_buffer, valid_size,
  400. NULL, valid_size,
  401. valid_buffer, &size_t_var,
  402. valid_buffer, valid_size ) );
  403. TEST_INVALID_PARAM_RET(
  404. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  405. mbedtls_cipher_auth_decrypt( &valid_ctx,
  406. valid_buffer, valid_size,
  407. valid_buffer, valid_size,
  408. valid_buffer, valid_size,
  409. NULL, &size_t_var,
  410. valid_buffer, valid_size ) );
  411. TEST_INVALID_PARAM_RET(
  412. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  413. mbedtls_cipher_auth_decrypt( &valid_ctx,
  414. valid_buffer, valid_size,
  415. valid_buffer, valid_size,
  416. valid_buffer, valid_size,
  417. valid_buffer, NULL,
  418. valid_buffer, valid_size ) );
  419. TEST_INVALID_PARAM_RET(
  420. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
  421. mbedtls_cipher_auth_decrypt( &valid_ctx,
  422. valid_buffer, valid_size,
  423. valid_buffer, valid_size,
  424. valid_buffer, valid_size,
  425. valid_buffer, &size_t_var,
  426. NULL, valid_size ) );
  427. #endif /* defined(MBEDTLS_CIPHER_MODE_AEAD) */
  428. /* mbedtls_cipher_free() */
  429. TEST_VALID_PARAM( mbedtls_cipher_free( NULL ) );
  430. exit:
  431. TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) );
  432. }
  433. /* END_CASE */
  434. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  435. void cipher_special_behaviours( )
  436. {
  437. const mbedtls_cipher_info_t *cipher_info;
  438. mbedtls_cipher_context_t ctx;
  439. unsigned char input[32];
  440. unsigned char output[32];
  441. #if defined (MBEDTLS_CIPHER_MODE_CBC)
  442. unsigned char iv[32];
  443. #endif
  444. size_t olen = 0;
  445. mbedtls_cipher_init( &ctx );
  446. memset( input, 0, sizeof( input ) );
  447. memset( output, 0, sizeof( output ) );
  448. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  449. memset( iv, 0, sizeof( iv ) );
  450. /* Check and get info structures */
  451. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
  452. TEST_ASSERT( NULL != cipher_info );
  453. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  454. /* IV too big */
  455. TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
  456. == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  457. /* IV too small */
  458. TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
  459. == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  460. mbedtls_cipher_free( &ctx );
  461. mbedtls_cipher_init( &ctx );
  462. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  463. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
  464. TEST_ASSERT( NULL != cipher_info );
  465. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  466. /* Update ECB with partial block */
  467. TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
  468. == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  469. exit:
  470. mbedtls_cipher_free( &ctx );
  471. }
  472. /* END_CASE */
  473. /* BEGIN_CASE */
  474. void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
  475. int length_val, int pad_mode )
  476. {
  477. size_t length = length_val, outlen, total_len, i, block_size;
  478. unsigned char key[64];
  479. unsigned char iv[16];
  480. unsigned char ad[13];
  481. unsigned char tag[16];
  482. unsigned char inbuf[64];
  483. unsigned char encbuf[64];
  484. unsigned char decbuf[64];
  485. const mbedtls_cipher_info_t *cipher_info;
  486. mbedtls_cipher_context_t ctx_dec;
  487. mbedtls_cipher_context_t ctx_enc;
  488. /*
  489. * Prepare contexts
  490. */
  491. mbedtls_cipher_init( &ctx_dec );
  492. mbedtls_cipher_init( &ctx_enc );
  493. memset( key, 0x2a, sizeof( key ) );
  494. /* Check and get info structures */
  495. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  496. TEST_ASSERT( NULL != cipher_info );
  497. TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
  498. /* Initialise enc and dec contexts */
  499. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  500. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
  501. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
  502. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
  503. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  504. if( -1 != pad_mode )
  505. {
  506. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
  507. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
  508. }
  509. #else
  510. (void) pad_mode;
  511. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  512. /*
  513. * Do a few encode/decode cycles
  514. */
  515. for( i = 0; i < 3; i++ )
  516. {
  517. memset( iv , 0x00 + i, sizeof( iv ) );
  518. memset( ad, 0x10 + i, sizeof( ad ) );
  519. memset( inbuf, 0x20 + i, sizeof( inbuf ) );
  520. memset( encbuf, 0, sizeof( encbuf ) );
  521. memset( decbuf, 0, sizeof( decbuf ) );
  522. memset( tag, 0, sizeof( tag ) );
  523. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
  524. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
  525. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  526. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
  527. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  528. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
  529. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
  530. #endif
  531. block_size = mbedtls_cipher_get_block_size( &ctx_enc );
  532. TEST_ASSERT( block_size != 0 );
  533. /* encode length number of bytes from inbuf */
  534. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
  535. total_len = outlen;
  536. TEST_ASSERT( total_len == length ||
  537. ( total_len % block_size == 0 &&
  538. total_len < length &&
  539. total_len + block_size > length ) );
  540. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
  541. total_len += outlen;
  542. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  543. TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
  544. #endif
  545. TEST_ASSERT( total_len == length ||
  546. ( total_len % block_size == 0 &&
  547. total_len > length &&
  548. total_len <= length + block_size ) );
  549. /* decode the previously encoded string */
  550. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
  551. total_len = outlen;
  552. TEST_ASSERT( total_len == length ||
  553. ( total_len % block_size == 0 &&
  554. total_len < length &&
  555. total_len + block_size >= length ) );
  556. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
  557. total_len += outlen;
  558. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  559. TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
  560. #endif
  561. /* check result */
  562. TEST_ASSERT( total_len == length );
  563. TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
  564. }
  565. /*
  566. * Done
  567. */
  568. exit:
  569. mbedtls_cipher_free( &ctx_dec );
  570. mbedtls_cipher_free( &ctx_enc );
  571. }
  572. /* END_CASE */
  573. /* BEGIN_CASE */
  574. void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
  575. int ret )
  576. {
  577. size_t length = length_val;
  578. unsigned char key[32];
  579. unsigned char iv[16];
  580. const mbedtls_cipher_info_t *cipher_info;
  581. mbedtls_cipher_context_t ctx;
  582. unsigned char inbuf[64];
  583. unsigned char encbuf[64];
  584. size_t outlen = 0;
  585. memset( key, 0, 32 );
  586. memset( iv , 0, 16 );
  587. mbedtls_cipher_init( &ctx );
  588. memset( inbuf, 5, 64 );
  589. memset( encbuf, 0, 64 );
  590. /* Check and get info structures */
  591. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  592. TEST_ASSERT( NULL != cipher_info );
  593. /* Initialise context */
  594. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
  595. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
  596. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  597. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  598. #else
  599. (void) pad_mode;
  600. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  601. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
  602. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
  603. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  604. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
  605. #endif
  606. /* encode length number of bytes from inbuf */
  607. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
  608. TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
  609. /* done */
  610. exit:
  611. mbedtls_cipher_free( &ctx );
  612. }
  613. /* END_CASE */
  614. /* BEGIN_CASE */
  615. void dec_empty_buf( int cipher )
  616. {
  617. unsigned char key[32];
  618. unsigned char iv[16];
  619. mbedtls_cipher_context_t ctx_dec;
  620. const mbedtls_cipher_info_t *cipher_info;
  621. unsigned char encbuf[64];
  622. unsigned char decbuf[64];
  623. size_t outlen = 0;
  624. int expected_ret;
  625. memset( key, 0, 32 );
  626. memset( iv , 0, 16 );
  627. mbedtls_cipher_init( &ctx_dec );
  628. memset( encbuf, 0, 64 );
  629. memset( decbuf, 0, 64 );
  630. /* Initialise context */
  631. cipher_info = mbedtls_cipher_info_from_type( cipher );
  632. TEST_ASSERT( NULL != cipher_info);
  633. TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
  634. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  635. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
  636. key, cipher_info->key_bitlen,
  637. MBEDTLS_DECRYPT ) );
  638. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
  639. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  640. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  641. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
  642. #endif
  643. /* decode 0-byte string */
  644. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
  645. TEST_ASSERT( 0 == outlen );
  646. if ( cipher_info->mode == MBEDTLS_MODE_CBC ||
  647. cipher_info->mode == MBEDTLS_MODE_ECB )
  648. {
  649. /* CBC and ECB ciphers need a full block of input. */
  650. expected_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  651. }
  652. else
  653. {
  654. /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
  655. * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
  656. * decrypting an empty buffer. */
  657. expected_ret = 0;
  658. }
  659. TEST_ASSERT( expected_ret == mbedtls_cipher_finish(
  660. &ctx_dec, decbuf + outlen, &outlen ) );
  661. TEST_ASSERT( 0 == outlen );
  662. exit:
  663. mbedtls_cipher_free( &ctx_dec );
  664. }
  665. /* END_CASE */
  666. /* BEGIN_CASE */
  667. void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
  668. int second_length_val, int pad_mode,
  669. int first_encrypt_output_len, int second_encrypt_output_len,
  670. int first_decrypt_output_len, int second_decrypt_output_len )
  671. {
  672. size_t first_length = first_length_val;
  673. size_t second_length = second_length_val;
  674. size_t length = first_length + second_length;
  675. size_t block_size;
  676. unsigned char key[32];
  677. unsigned char iv[16];
  678. mbedtls_cipher_context_t ctx_dec;
  679. mbedtls_cipher_context_t ctx_enc;
  680. const mbedtls_cipher_info_t *cipher_info;
  681. unsigned char inbuf[64];
  682. unsigned char encbuf[64];
  683. unsigned char decbuf[64];
  684. size_t outlen = 0;
  685. size_t totaloutlen = 0;
  686. memset( key, 0, 32 );
  687. memset( iv , 0, 16 );
  688. mbedtls_cipher_init( &ctx_dec );
  689. mbedtls_cipher_init( &ctx_enc );
  690. memset( inbuf, 5, 64 );
  691. memset( encbuf, 0, 64 );
  692. memset( decbuf, 0, 64 );
  693. /* Initialise enc and dec contexts */
  694. cipher_info = mbedtls_cipher_info_from_type( cipher_id );
  695. TEST_ASSERT( NULL != cipher_info);
  696. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
  697. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
  698. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
  699. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
  700. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  701. if( -1 != pad_mode )
  702. {
  703. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
  704. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
  705. }
  706. #else
  707. (void) pad_mode;
  708. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  709. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
  710. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
  711. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
  712. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
  713. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  714. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
  715. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
  716. #endif
  717. block_size = mbedtls_cipher_get_block_size( &ctx_enc );
  718. TEST_ASSERT( block_size != 0 );
  719. /* encode length number of bytes from inbuf */
  720. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
  721. TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
  722. totaloutlen = outlen;
  723. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
  724. TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
  725. totaloutlen += outlen;
  726. TEST_ASSERT( totaloutlen == length ||
  727. ( totaloutlen % block_size == 0 &&
  728. totaloutlen < length &&
  729. totaloutlen + block_size > length ) );
  730. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
  731. totaloutlen += outlen;
  732. TEST_ASSERT( totaloutlen == length ||
  733. ( totaloutlen % block_size == 0 &&
  734. totaloutlen > length &&
  735. totaloutlen <= length + block_size ) );
  736. /* decode the previously encoded string */
  737. second_length = totaloutlen - first_length;
  738. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
  739. TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
  740. totaloutlen = outlen;
  741. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
  742. TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
  743. totaloutlen += outlen;
  744. TEST_ASSERT( totaloutlen == length ||
  745. ( totaloutlen % block_size == 0 &&
  746. totaloutlen < length &&
  747. totaloutlen + block_size >= length ) );
  748. TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
  749. totaloutlen += outlen;
  750. TEST_ASSERT( totaloutlen == length );
  751. TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
  752. exit:
  753. mbedtls_cipher_free( &ctx_dec );
  754. mbedtls_cipher_free( &ctx_enc );
  755. }
  756. /* END_CASE */
  757. /* BEGIN_CASE */
  758. void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
  759. data_t * iv, data_t * cipher,
  760. data_t * clear, data_t * ad, data_t * tag,
  761. int finish_result, int tag_result )
  762. {
  763. unsigned char output[265];
  764. mbedtls_cipher_context_t ctx;
  765. size_t outlen, total_len;
  766. mbedtls_cipher_init( &ctx );
  767. memset( output, 0x00, sizeof( output ) );
  768. #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
  769. ((void) ad);
  770. ((void) tag);
  771. #endif
  772. /* Prepare context */
  773. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  774. mbedtls_cipher_info_from_type( cipher_id ) ) );
  775. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
  776. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  777. if( pad_mode != -1 )
  778. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
  779. #else
  780. (void) pad_mode;
  781. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  782. TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
  783. TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
  784. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  785. TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
  786. #endif
  787. /* decode buffer and check tag->x */
  788. total_len = 0;
  789. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
  790. total_len += outlen;
  791. TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
  792. &outlen ) );
  793. total_len += outlen;
  794. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  795. TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
  796. #endif
  797. /* check plaintext only if everything went fine */
  798. if( 0 == finish_result && 0 == tag_result )
  799. {
  800. TEST_ASSERT( total_len == clear->len );
  801. TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
  802. }
  803. exit:
  804. mbedtls_cipher_free( &ctx );
  805. }
  806. /* END_CASE */
  807. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
  808. void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
  809. data_t * ad, data_t * cipher, data_t * tag,
  810. char * result, data_t * clear )
  811. {
  812. int ret;
  813. unsigned char output[267]; /* above + 2 (overwrite check) */
  814. unsigned char my_tag[20];
  815. mbedtls_cipher_context_t ctx;
  816. size_t outlen;
  817. mbedtls_cipher_init( &ctx );
  818. memset( output, 0xFF, sizeof( output ) );
  819. memset( my_tag, 0xFF, sizeof( my_tag ) );
  820. /* Prepare context */
  821. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  822. mbedtls_cipher_info_from_type( cipher_id ) ) );
  823. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
  824. /* decode buffer and check tag->x */
  825. ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
  826. cipher->x, cipher->len, output, &outlen,
  827. tag->x, tag->len );
  828. /* make sure we didn't overwrite */
  829. TEST_ASSERT( output[outlen + 0] == 0xFF );
  830. TEST_ASSERT( output[outlen + 1] == 0xFF );
  831. /* make sure the message is rejected if it should be */
  832. if( strcmp( result, "FAIL" ) == 0 )
  833. {
  834. TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  835. goto exit;
  836. }
  837. /* otherwise, make sure it was decrypted properly */
  838. TEST_ASSERT( ret == 0 );
  839. TEST_ASSERT( outlen == clear->len );
  840. TEST_ASSERT( memcmp( output, clear->x, clear->len ) == 0 );
  841. /* then encrypt the clear->x and make sure we get the same ciphertext and tag->x */
  842. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len,
  843. MBEDTLS_ENCRYPT ) );
  844. memset( output, 0xFF, sizeof( output ) );
  845. outlen = 0;
  846. ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
  847. clear->x, clear->len, output, &outlen,
  848. my_tag, tag->len );
  849. TEST_ASSERT( ret == 0 );
  850. TEST_ASSERT( outlen == cipher->len );
  851. TEST_ASSERT( memcmp( output, cipher->x, cipher->len ) == 0 );
  852. TEST_ASSERT( memcmp( my_tag, tag->x, tag->len ) == 0 );
  853. /* make sure we didn't overwrite */
  854. TEST_ASSERT( output[outlen + 0] == 0xFF );
  855. TEST_ASSERT( output[outlen + 1] == 0xFF );
  856. TEST_ASSERT( my_tag[tag->len + 0] == 0xFF );
  857. TEST_ASSERT( my_tag[tag->len + 1] == 0xFF );
  858. exit:
  859. mbedtls_cipher_free( &ctx );
  860. }
  861. /* END_CASE */
  862. /* BEGIN_CASE */
  863. void test_vec_ecb( int cipher_id, int operation, data_t * key,
  864. data_t * input, data_t * result, int finish_result
  865. )
  866. {
  867. mbedtls_cipher_context_t ctx;
  868. unsigned char output[32];
  869. size_t outlen;
  870. mbedtls_cipher_init( &ctx );
  871. memset( output, 0x00, sizeof( output ) );
  872. /* Prepare context */
  873. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  874. mbedtls_cipher_info_from_type( cipher_id ) ) );
  875. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
  876. TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
  877. mbedtls_cipher_get_block_size( &ctx ),
  878. output, &outlen ) );
  879. TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
  880. TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
  881. &outlen ) );
  882. TEST_ASSERT( 0 == outlen );
  883. /* check plaintext only if everything went fine */
  884. if( 0 == finish_result )
  885. TEST_ASSERT( 0 == memcmp( output, result->x,
  886. mbedtls_cipher_get_block_size( &ctx ) ) );
  887. exit:
  888. mbedtls_cipher_free( &ctx );
  889. }
  890. /* END_CASE */
  891. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
  892. void test_vec_crypt( int cipher_id, int operation, data_t *key,
  893. data_t *iv, data_t *input, data_t *result,
  894. int finish_result )
  895. {
  896. mbedtls_cipher_context_t ctx;
  897. unsigned char output[32];
  898. size_t outlen;
  899. mbedtls_cipher_init( &ctx );
  900. memset( output, 0x00, sizeof( output ) );
  901. /* Prepare context */
  902. TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
  903. mbedtls_cipher_info_from_type( cipher_id ) ) );
  904. TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
  905. if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
  906. TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
  907. TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
  908. iv->len, input->x, input->len,
  909. output, &outlen ) );
  910. TEST_ASSERT( result->len == outlen );
  911. /* check plaintext only if everything went fine */
  912. if( 0 == finish_result )
  913. TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
  914. exit:
  915. mbedtls_cipher_free( &ctx );
  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. /* END_CASE */