test_suite_aes.function 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  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. mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
  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 = unhexify_alloc( hex_data_unit_string, &data_unit_len );
  98. TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
  99. key = unhexify_alloc( hex_key_string, &key_len );
  100. TEST_ASSERT( key_len % 2 == 0 );
  101. src = unhexify_alloc( hex_src_string, &src_len );
  102. dst = unhexify_alloc( hex_dst_string, &dst_len );
  103. TEST_ASSERT( src_len == dst_len );
  104. output = zero_alloc( dst_len );
  105. TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 );
  106. TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len,
  107. data_unit, src, output ) == 0 );
  108. TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
  109. exit:
  110. mbedtls_aes_xts_free( &ctx );
  111. mbedtls_free( data_unit );
  112. mbedtls_free( key );
  113. mbedtls_free( src );
  114. mbedtls_free( dst );
  115. mbedtls_free( output );
  116. }
  117. /* END_CASE */
  118. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
  119. void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
  120. char *hex_dst_string, char *hex_src_string )
  121. {
  122. enum { AES_BLOCK_SIZE = 16 };
  123. unsigned char *data_unit = NULL;
  124. unsigned char *key = NULL;
  125. unsigned char *src = NULL;
  126. unsigned char *dst = NULL;
  127. unsigned char *output = NULL;
  128. mbedtls_aes_xts_context ctx;
  129. size_t key_len, src_len, dst_len, data_unit_len;
  130. mbedtls_aes_xts_init( &ctx );
  131. data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
  132. TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
  133. key = unhexify_alloc( hex_key_string, &key_len );
  134. TEST_ASSERT( key_len % 2 == 0 );
  135. src = unhexify_alloc( hex_src_string, &src_len );
  136. dst = unhexify_alloc( hex_dst_string, &dst_len );
  137. TEST_ASSERT( src_len == dst_len );
  138. output = zero_alloc( dst_len );
  139. TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 );
  140. TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len,
  141. data_unit, src, output ) == 0 );
  142. TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
  143. exit:
  144. mbedtls_aes_xts_free( &ctx );
  145. mbedtls_free( data_unit );
  146. mbedtls_free( key );
  147. mbedtls_free( src );
  148. mbedtls_free( dst );
  149. mbedtls_free( output );
  150. }
  151. /* END_CASE */
  152. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
  153. void aes_crypt_xts_size( int size, int retval )
  154. {
  155. mbedtls_aes_xts_context ctx;
  156. const unsigned char src[16] = { 0 };
  157. unsigned char output[16];
  158. unsigned char data_unit[16];
  159. size_t length = size;
  160. mbedtls_aes_xts_init( &ctx );
  161. memset( data_unit, 0x00, sizeof( data_unit ) );
  162. /* Valid pointers are passed for builds with MBEDTLS_CHECK_PARAMS, as
  163. * otherwise we wouldn't get to the size check we're interested in. */
  164. TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval );
  165. }
  166. /* END_CASE */
  167. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
  168. void aes_crypt_xts_keysize( int size, int retval )
  169. {
  170. mbedtls_aes_xts_context ctx;
  171. const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
  172. size_t key_len = size;
  173. mbedtls_aes_xts_init( &ctx );
  174. TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval );
  175. TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval );
  176. exit:
  177. mbedtls_aes_xts_free( &ctx );
  178. }
  179. /* END_CASE */
  180. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  181. void aes_encrypt_cfb128( data_t * key_str, data_t * iv_str,
  182. data_t * src_str, data_t * dst )
  183. {
  184. unsigned char output[100];
  185. mbedtls_aes_context ctx;
  186. size_t iv_offset = 0;
  187. memset(output, 0x00, 100);
  188. mbedtls_aes_init( &ctx );
  189. mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  190. TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
  191. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
  192. exit:
  193. mbedtls_aes_free( &ctx );
  194. }
  195. /* END_CASE */
  196. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  197. void aes_decrypt_cfb128( data_t * key_str, data_t * iv_str,
  198. data_t * src_str, data_t * dst )
  199. {
  200. unsigned char output[100];
  201. mbedtls_aes_context ctx;
  202. size_t iv_offset = 0;
  203. memset(output, 0x00, 100);
  204. mbedtls_aes_init( &ctx );
  205. mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  206. TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
  207. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
  208. exit:
  209. mbedtls_aes_free( &ctx );
  210. }
  211. /* END_CASE */
  212. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  213. void aes_encrypt_cfb8( data_t * key_str, data_t * iv_str,
  214. data_t * src_str, data_t * dst )
  215. {
  216. unsigned char output[100];
  217. mbedtls_aes_context ctx;
  218. memset(output, 0x00, 100);
  219. mbedtls_aes_init( &ctx );
  220. mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  221. TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 );
  222. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x,
  223. src_str->len, dst->len ) == 0 );
  224. exit:
  225. mbedtls_aes_free( &ctx );
  226. }
  227. /* END_CASE */
  228. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  229. void aes_decrypt_cfb8( data_t * key_str, data_t * iv_str,
  230. data_t * src_str, data_t * dst )
  231. {
  232. unsigned char output[100];
  233. mbedtls_aes_context ctx;
  234. memset(output, 0x00, 100);
  235. mbedtls_aes_init( &ctx );
  236. mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  237. TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 );
  238. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x,
  239. src_str->len, dst->len ) == 0 );
  240. exit:
  241. mbedtls_aes_free( &ctx );
  242. }
  243. /* END_CASE */
  244. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
  245. void aes_encrypt_ofb( int fragment_size, data_t *key_str,
  246. data_t *iv_str, data_t *src_str,
  247. data_t *expected_output )
  248. {
  249. unsigned char output[32];
  250. mbedtls_aes_context ctx;
  251. size_t iv_offset = 0;
  252. int in_buffer_len;
  253. unsigned char* src_str_next;
  254. memset( output, 0x00, sizeof( output ) );
  255. mbedtls_aes_init( &ctx );
  256. TEST_ASSERT( (size_t)fragment_size < sizeof( output ) );
  257. TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str->x,
  258. key_str->len * 8 ) == 0 );
  259. in_buffer_len = src_str->len;
  260. src_str_next = src_str->x;
  261. while( in_buffer_len > 0 )
  262. {
  263. TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
  264. iv_str->x, src_str_next, output ) == 0 );
  265. TEST_ASSERT( memcmp( output, expected_output->x, fragment_size ) == 0 );
  266. in_buffer_len -= fragment_size;
  267. expected_output->x += fragment_size;
  268. src_str_next += fragment_size;
  269. if( in_buffer_len < fragment_size )
  270. fragment_size = in_buffer_len;
  271. }
  272. exit:
  273. mbedtls_aes_free( &ctx );
  274. }
  275. /* END_CASE */
  276. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  277. void aes_check_params( )
  278. {
  279. mbedtls_aes_context aes_ctx;
  280. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  281. mbedtls_aes_xts_context xts_ctx;
  282. #endif
  283. const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
  284. const unsigned char in[16] = { 0 };
  285. unsigned char out[16];
  286. size_t size;
  287. const int valid_mode = MBEDTLS_AES_ENCRYPT;
  288. const int invalid_mode = 42;
  289. TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) );
  290. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  291. TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) );
  292. #endif
  293. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  294. mbedtls_aes_setkey_enc( NULL, key, 128 ) );
  295. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  296. mbedtls_aes_setkey_enc( &aes_ctx, NULL, 128 ) );
  297. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  298. mbedtls_aes_setkey_dec( NULL, key, 128 ) );
  299. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  300. mbedtls_aes_setkey_dec( &aes_ctx, NULL, 128 ) );
  301. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  302. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  303. mbedtls_aes_xts_setkey_enc( NULL, key, 128 ) );
  304. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  305. mbedtls_aes_xts_setkey_enc( &xts_ctx, NULL, 128 ) );
  306. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  307. mbedtls_aes_xts_setkey_dec( NULL, key, 128 ) );
  308. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  309. mbedtls_aes_xts_setkey_dec( &xts_ctx, NULL, 128 ) );
  310. #endif
  311. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  312. mbedtls_aes_crypt_ecb( NULL,
  313. valid_mode, in, out ) );
  314. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  315. mbedtls_aes_crypt_ecb( &aes_ctx,
  316. invalid_mode, in, out ) );
  317. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  318. mbedtls_aes_crypt_ecb( &aes_ctx,
  319. valid_mode, NULL, out ) );
  320. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  321. mbedtls_aes_crypt_ecb( &aes_ctx,
  322. valid_mode, in, NULL ) );
  323. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  324. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  325. mbedtls_aes_crypt_cbc( NULL,
  326. valid_mode, 16,
  327. out, in, out ) );
  328. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  329. mbedtls_aes_crypt_cbc( &aes_ctx,
  330. invalid_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. valid_mode, 16,
  335. NULL, 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. out, NULL, 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, in, NULL ) );
  344. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  345. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  346. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  347. mbedtls_aes_crypt_xts( NULL,
  348. valid_mode, 16,
  349. in, in, out ) );
  350. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  351. mbedtls_aes_crypt_xts( &xts_ctx,
  352. invalid_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. valid_mode, 16,
  357. NULL, 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. in, NULL, 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, in, NULL ) );
  366. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  367. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  368. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  369. mbedtls_aes_crypt_cfb128( NULL,
  370. valid_mode, 16,
  371. &size, out, in, out ) );
  372. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  373. mbedtls_aes_crypt_cfb128( &aes_ctx,
  374. invalid_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. valid_mode, 16,
  379. NULL, 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. &size, NULL, 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, out, NULL, 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, in, NULL ) );
  392. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  393. mbedtls_aes_crypt_cfb8( NULL,
  394. valid_mode, 16,
  395. out, in, out ) );
  396. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  397. mbedtls_aes_crypt_cfb8( &aes_ctx,
  398. invalid_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. valid_mode, 16,
  403. NULL, 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. out, NULL, 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, in, NULL ) );
  412. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  413. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  414. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  415. mbedtls_aes_crypt_ofb( NULL, 16,
  416. &size, out, in, out ) );
  417. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  418. mbedtls_aes_crypt_ofb( &aes_ctx, 16,
  419. NULL, out, in, out ) );
  420. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  421. mbedtls_aes_crypt_ofb( &aes_ctx, 16,
  422. &size, NULL, in, out ) );
  423. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  424. mbedtls_aes_crypt_ofb( &aes_ctx, 16,
  425. &size, out, NULL, out ) );
  426. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  427. mbedtls_aes_crypt_ofb( &aes_ctx, 16,
  428. &size, out, in, NULL ) );
  429. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  430. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  431. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  432. mbedtls_aes_crypt_ctr( NULL, 16, &size, out,
  433. out, in, out ) );
  434. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  435. mbedtls_aes_crypt_ctr( &aes_ctx, 16, NULL, out,
  436. out, in, out ) );
  437. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  438. mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, NULL,
  439. out, in, out ) );
  440. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  441. mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
  442. NULL, in, out ) );
  443. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  444. mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
  445. out, NULL, out ) );
  446. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
  447. mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
  448. out, in, NULL ) );
  449. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  450. }
  451. /* END_CASE */
  452. /* BEGIN_CASE */
  453. void aes_misc_params( )
  454. {
  455. #if defined(MBEDTLS_CIPHER_MODE_CBC) || \
  456. defined(MBEDTLS_CIPHER_MODE_XTS) || \
  457. defined(MBEDTLS_CIPHER_MODE_CFB) || \
  458. defined(MBEDTLS_CIPHER_MODE_OFB)
  459. mbedtls_aes_context aes_ctx;
  460. const unsigned char in[16] = { 0 };
  461. unsigned char out[16];
  462. #endif
  463. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  464. mbedtls_aes_xts_context xts_ctx;
  465. #endif
  466. #if defined(MBEDTLS_CIPHER_MODE_CFB) || \
  467. defined(MBEDTLS_CIPHER_MODE_OFB)
  468. size_t size;
  469. #endif
  470. /* These calls accept NULL */
  471. TEST_VALID_PARAM( mbedtls_aes_free( NULL ) );
  472. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  473. TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) );
  474. #endif
  475. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  476. TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
  477. 15,
  478. out, in, out )
  479. == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
  480. TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
  481. 17,
  482. out, in, out )
  483. == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
  484. #endif
  485. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  486. TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
  487. 15,
  488. in, in, out )
  489. == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
  490. TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
  491. (1 << 24) + 1,
  492. in, in, out )
  493. == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
  494. #endif
  495. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  496. size = 16;
  497. TEST_ASSERT( mbedtls_aes_crypt_cfb128( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16,
  498. &size, out, in, out )
  499. == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
  500. #endif
  501. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  502. size = 16;
  503. TEST_ASSERT( mbedtls_aes_crypt_ofb( &aes_ctx, 16, &size, out, in, out )
  504. == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
  505. #endif
  506. }
  507. /* END_CASE */
  508. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  509. void aes_selftest( )
  510. {
  511. TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
  512. }
  513. /* END_CASE */