test_suite_aria.function 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/aria.h"
  3. /* Maxium size of data used by test vectors
  4. * WARNING: to be adapted if and when adding larger test cases */
  5. #define ARIA_MAX_DATASIZE 160
  6. /* Maximum sizes of hexified things */
  7. #define ARIA_MAX_KEY_STR ( 2 * MBEDTLS_ARIA_MAX_KEYSIZE + 1 )
  8. #define ARIA_BLOCK_STR ( 2 * MBEDTLS_ARIA_BLOCKSIZE + 1 )
  9. #define ARIA_MAX_DATA_STR ( 2 * ARIA_MAX_DATASIZE + 1 )
  10. /* END_HEADER */
  11. /* BEGIN_DEPENDENCIES
  12. * depends_on:MBEDTLS_ARIA_C
  13. * END_DEPENDENCIES
  14. */
  15. /* BEGIN_CASE */
  16. void aria_valid_param( )
  17. {
  18. TEST_VALID_PARAM( mbedtls_aria_free( NULL ) );
  19. }
  20. /* END_CASE */
  21. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  22. void aria_invalid_param( )
  23. {
  24. mbedtls_aria_context ctx;
  25. unsigned char key[128 / 8] = { 0 };
  26. unsigned char input[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
  27. unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
  28. unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
  29. size_t iv_off = 0;
  30. ((void) iv_off);
  31. ((void) iv);
  32. TEST_INVALID_PARAM( mbedtls_aria_init( NULL ) );
  33. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  34. mbedtls_aria_setkey_enc( NULL, key,
  35. sizeof( key ) ) );
  36. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  37. mbedtls_aria_setkey_enc( &ctx, NULL,
  38. sizeof( key ) ) );
  39. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  40. mbedtls_aria_setkey_dec( NULL, key,
  41. sizeof( key ) ) );
  42. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  43. mbedtls_aria_setkey_dec( &ctx, NULL,
  44. sizeof( key ) ) );
  45. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  46. mbedtls_aria_crypt_ecb( NULL, input, output ) );
  47. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  48. mbedtls_aria_crypt_ecb( &ctx, NULL, output ) );
  49. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  50. mbedtls_aria_crypt_ecb( &ctx, input, NULL ) );
  51. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  52. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  53. mbedtls_aria_crypt_cbc( NULL,
  54. MBEDTLS_ARIA_ENCRYPT,
  55. sizeof( input ),
  56. iv,
  57. input,
  58. output ) );
  59. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  60. mbedtls_aria_crypt_cbc( &ctx,
  61. 42 /* invalid mode */,
  62. sizeof( input ),
  63. iv,
  64. input,
  65. output ) );
  66. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  67. mbedtls_aria_crypt_cbc( &ctx,
  68. MBEDTLS_ARIA_ENCRYPT,
  69. sizeof( input ),
  70. NULL,
  71. input,
  72. output ) );
  73. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  74. mbedtls_aria_crypt_cbc( &ctx,
  75. MBEDTLS_ARIA_ENCRYPT,
  76. sizeof( input ),
  77. iv,
  78. NULL,
  79. output ) );
  80. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  81. mbedtls_aria_crypt_cbc( &ctx,
  82. MBEDTLS_ARIA_ENCRYPT,
  83. sizeof( input ),
  84. iv,
  85. input,
  86. NULL ) );
  87. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  88. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  89. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  90. mbedtls_aria_crypt_cfb128( NULL,
  91. MBEDTLS_ARIA_ENCRYPT,
  92. sizeof( input ),
  93. &iv_off,
  94. iv,
  95. input,
  96. output ) );
  97. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  98. mbedtls_aria_crypt_cfb128( &ctx,
  99. 42, /* invalid mode */
  100. sizeof( input ),
  101. &iv_off,
  102. iv,
  103. input,
  104. output ) );
  105. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  106. mbedtls_aria_crypt_cfb128( &ctx,
  107. MBEDTLS_ARIA_ENCRYPT,
  108. sizeof( input ),
  109. NULL,
  110. iv,
  111. input,
  112. output ) );
  113. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  114. mbedtls_aria_crypt_cfb128( &ctx,
  115. MBEDTLS_ARIA_ENCRYPT,
  116. sizeof( input ),
  117. &iv_off,
  118. NULL,
  119. input,
  120. output ) );
  121. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  122. mbedtls_aria_crypt_cfb128( &ctx,
  123. MBEDTLS_ARIA_ENCRYPT,
  124. sizeof( input ),
  125. &iv_off,
  126. iv,
  127. NULL,
  128. output ) );
  129. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  130. mbedtls_aria_crypt_cfb128( &ctx,
  131. MBEDTLS_ARIA_ENCRYPT,
  132. sizeof( input ),
  133. &iv_off,
  134. iv,
  135. input,
  136. NULL ) );
  137. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  138. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  139. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  140. mbedtls_aria_crypt_ctr( NULL,
  141. sizeof( input ),
  142. &iv_off,
  143. iv,
  144. iv,
  145. input,
  146. output ) );
  147. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  148. mbedtls_aria_crypt_ctr( &ctx,
  149. sizeof( input ),
  150. NULL,
  151. iv,
  152. iv,
  153. input,
  154. output ) );
  155. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  156. mbedtls_aria_crypt_ctr( &ctx,
  157. sizeof( input ),
  158. &iv_off,
  159. NULL,
  160. iv,
  161. input,
  162. output ) );
  163. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  164. mbedtls_aria_crypt_ctr( &ctx,
  165. sizeof( input ),
  166. &iv_off,
  167. iv,
  168. NULL,
  169. input,
  170. output ) );
  171. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  172. mbedtls_aria_crypt_ctr( &ctx,
  173. sizeof( input ),
  174. &iv_off,
  175. iv,
  176. iv,
  177. NULL,
  178. output ) );
  179. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  180. mbedtls_aria_crypt_ctr( &ctx,
  181. sizeof( input ),
  182. &iv_off,
  183. iv,
  184. iv,
  185. input,
  186. NULL ) );
  187. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  188. exit:
  189. return;
  190. }
  191. /* END_CASE */
  192. /* BEGIN_CASE */
  193. void aria_encrypt_ecb( data_t *key_str, data_t *src_str,
  194. data_t *expected_output, int setkey_result )
  195. {
  196. unsigned char output[ARIA_MAX_DATASIZE];
  197. mbedtls_aria_context ctx;
  198. size_t i;
  199. memset( output, 0x00, sizeof( output ) );
  200. mbedtls_aria_init( &ctx );
  201. TEST_ASSERT( mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 )
  202. == setkey_result );
  203. if( setkey_result == 0 )
  204. {
  205. for( i = 0; i < src_str->len; i += MBEDTLS_ARIA_BLOCKSIZE )
  206. {
  207. TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str->x + i,
  208. output + i ) == 0 );
  209. }
  210. ASSERT_COMPARE( output, expected_output->len,
  211. expected_output->x, expected_output->len );
  212. }
  213. exit:
  214. mbedtls_aria_free( &ctx );
  215. }
  216. /* END_CASE */
  217. /* BEGIN_CASE */
  218. void aria_decrypt_ecb( data_t *key_str, data_t *src_str,
  219. data_t *expected_output, int setkey_result )
  220. {
  221. unsigned char output[ARIA_MAX_DATASIZE];
  222. mbedtls_aria_context ctx;
  223. size_t i;
  224. memset( output, 0x00, sizeof( output ) );
  225. mbedtls_aria_init( &ctx );
  226. TEST_ASSERT( mbedtls_aria_setkey_dec( &ctx, key_str->x, key_str->len * 8 )
  227. == setkey_result );
  228. if( setkey_result == 0 )
  229. {
  230. for( i = 0; i < src_str->len; i += MBEDTLS_ARIA_BLOCKSIZE )
  231. {
  232. TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str->x + i,
  233. output + i ) == 0 );
  234. }
  235. ASSERT_COMPARE( output, expected_output->len,
  236. expected_output->x, expected_output->len );
  237. }
  238. exit:
  239. mbedtls_aria_free( &ctx );
  240. }
  241. /* END_CASE */
  242. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  243. void aria_encrypt_cbc( data_t *key_str, data_t *iv_str,
  244. data_t *src_str, data_t *expected_output,
  245. int cbc_result )
  246. {
  247. unsigned char output[ARIA_MAX_DATASIZE];
  248. mbedtls_aria_context ctx;
  249. memset( output, 0x00, sizeof( output ) );
  250. mbedtls_aria_init( &ctx );
  251. mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  252. TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_ENCRYPT,
  253. src_str->len, iv_str->x, src_str->x,
  254. output ) == cbc_result );
  255. if( cbc_result == 0 )
  256. {
  257. ASSERT_COMPARE( output, expected_output->len,
  258. expected_output->x, expected_output->len );
  259. }
  260. exit:
  261. mbedtls_aria_free( &ctx );
  262. }
  263. /* END_CASE */
  264. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  265. void aria_decrypt_cbc( data_t *key_str, data_t *iv_str,
  266. data_t *src_str, data_t *expected_output,
  267. int cbc_result )
  268. {
  269. unsigned char output[ARIA_MAX_DATASIZE];
  270. mbedtls_aria_context ctx;
  271. memset( output, 0x00, sizeof( output ) );
  272. mbedtls_aria_init( &ctx );
  273. mbedtls_aria_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
  274. TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_DECRYPT,
  275. src_str->len, iv_str->x, src_str->x,
  276. output ) == cbc_result );
  277. if( cbc_result == 0 )
  278. {
  279. ASSERT_COMPARE( output, expected_output->len,
  280. expected_output->x, expected_output->len );
  281. }
  282. exit:
  283. mbedtls_aria_free( &ctx );
  284. }
  285. /* END_CASE */
  286. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  287. void aria_encrypt_cfb128( data_t *key_str, data_t *iv_str,
  288. data_t *src_str, data_t *expected_output,
  289. int result )
  290. {
  291. unsigned char output[ARIA_MAX_DATASIZE];
  292. mbedtls_aria_context ctx;
  293. size_t iv_offset = 0;
  294. memset( output, 0x00, sizeof( output ) );
  295. mbedtls_aria_init( &ctx );
  296. mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  297. TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_ENCRYPT,
  298. src_str->len, &iv_offset,
  299. iv_str->x, src_str->x, output )
  300. == result );
  301. ASSERT_COMPARE( output, expected_output->len,
  302. expected_output->x, expected_output->len );
  303. exit:
  304. mbedtls_aria_free( &ctx );
  305. }
  306. /* END_CASE */
  307. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  308. void aria_decrypt_cfb128( data_t *key_str, data_t *iv_str,
  309. data_t *src_str, data_t *expected_output,
  310. int result )
  311. {
  312. unsigned char output[ARIA_MAX_DATASIZE];
  313. mbedtls_aria_context ctx;
  314. size_t iv_offset = 0;
  315. memset( output, 0x00, sizeof( output ) );
  316. mbedtls_aria_init( &ctx );
  317. mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  318. TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_DECRYPT,
  319. src_str->len, &iv_offset,
  320. iv_str->x, src_str->x, output )
  321. == result );
  322. ASSERT_COMPARE( output, expected_output->len,
  323. expected_output->x, expected_output->len );
  324. exit:
  325. mbedtls_aria_free( &ctx );
  326. }
  327. /* END_CASE */
  328. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
  329. void aria_encrypt_ctr( data_t *key_str, data_t *iv_str,
  330. data_t *src_str, data_t *expected_output,
  331. int result )
  332. {
  333. unsigned char output[ARIA_MAX_DATASIZE];
  334. unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE];
  335. mbedtls_aria_context ctx;
  336. size_t iv_offset = 0;
  337. memset( output, 0x00, sizeof( output ) );
  338. mbedtls_aria_init( &ctx );
  339. mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  340. TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, src_str->len, &iv_offset,
  341. iv_str->x, blk, src_str->x, output )
  342. == result );
  343. ASSERT_COMPARE( output, expected_output->len,
  344. expected_output->x, expected_output->len );
  345. exit:
  346. mbedtls_aria_free( &ctx );
  347. }
  348. /* END_CASE */
  349. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
  350. void aria_decrypt_ctr( data_t *key_str, data_t *iv_str,
  351. data_t *src_str, data_t *expected_output,
  352. int result )
  353. {
  354. unsigned char output[ARIA_MAX_DATASIZE];
  355. unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE];
  356. mbedtls_aria_context ctx;
  357. size_t iv_offset = 0;
  358. memset( output, 0x00, sizeof( output ) );
  359. mbedtls_aria_init( &ctx );
  360. mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  361. TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, src_str->len, &iv_offset,
  362. iv_str->x, blk, src_str->x, output )
  363. == result );
  364. ASSERT_COMPARE( output, expected_output->len,
  365. expected_output->x, expected_output->len );
  366. exit:
  367. mbedtls_aria_free( &ctx );
  368. }
  369. /* END_CASE */
  370. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  371. void aria_selftest()
  372. {
  373. TEST_ASSERT( mbedtls_aria_self_test( 1 ) == 0 );
  374. }
  375. /* END_CASE */