test_suite_aes.function 23 KB

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