test_suite_camellia.function 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/camellia.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_CAMELLIA_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void camellia_valid_param( )
  10. {
  11. TEST_VALID_PARAM( mbedtls_camellia_free( NULL ) );
  12. }
  13. /* END_CASE */
  14. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  15. void camellia_invalid_param( )
  16. {
  17. mbedtls_camellia_context ctx;
  18. unsigned char buf[16] = { 0 };
  19. const size_t valid_keybits = 128;
  20. const int invalid_mode = 42;
  21. const int valid_mode = MBEDTLS_CAMELLIA_ENCRYPT;
  22. size_t off;
  23. ((void) off);
  24. TEST_INVALID_PARAM( mbedtls_camellia_init( NULL ) );
  25. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  26. mbedtls_camellia_setkey_enc( NULL,
  27. buf,
  28. valid_keybits ) );
  29. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  30. mbedtls_camellia_setkey_enc( &ctx,
  31. NULL,
  32. valid_keybits ) );
  33. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  34. mbedtls_camellia_setkey_dec( NULL,
  35. buf,
  36. valid_keybits ) );
  37. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  38. mbedtls_camellia_setkey_dec( &ctx,
  39. NULL,
  40. valid_keybits ) );
  41. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  42. mbedtls_camellia_crypt_ecb( NULL,
  43. valid_mode,
  44. buf, buf ) );
  45. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  46. mbedtls_camellia_crypt_ecb( &ctx,
  47. invalid_mode,
  48. buf, buf ) );
  49. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  50. mbedtls_camellia_crypt_ecb( &ctx,
  51. valid_mode,
  52. NULL, buf ) );
  53. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  54. mbedtls_camellia_crypt_ecb( &ctx,
  55. valid_mode,
  56. buf, NULL ) );
  57. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  58. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  59. mbedtls_camellia_crypt_cbc( NULL,
  60. valid_mode,
  61. sizeof( buf ),
  62. buf, buf, buf ) );
  63. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  64. mbedtls_camellia_crypt_cbc( &ctx,
  65. invalid_mode,
  66. sizeof( buf ),
  67. buf, buf, buf ) );
  68. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  69. mbedtls_camellia_crypt_cbc( &ctx,
  70. valid_mode,
  71. sizeof( buf ),
  72. NULL, buf, buf ) );
  73. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  74. mbedtls_camellia_crypt_cbc( &ctx,
  75. valid_mode,
  76. sizeof( buf ),
  77. buf, NULL, buf ) );
  78. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  79. mbedtls_camellia_crypt_cbc( &ctx,
  80. valid_mode,
  81. sizeof( buf ),
  82. buf, buf, NULL ) );
  83. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  84. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  85. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  86. mbedtls_camellia_crypt_cfb128( NULL,
  87. valid_mode,
  88. sizeof( buf ),
  89. &off, buf,
  90. buf, buf ) );
  91. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  92. mbedtls_camellia_crypt_cfb128( &ctx,
  93. invalid_mode,
  94. sizeof( buf ),
  95. &off, buf,
  96. buf, buf ) );
  97. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  98. mbedtls_camellia_crypt_cfb128( &ctx,
  99. valid_mode,
  100. sizeof( buf ),
  101. NULL, buf,
  102. buf, buf ) );
  103. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  104. mbedtls_camellia_crypt_cfb128( &ctx,
  105. valid_mode,
  106. sizeof( buf ),
  107. &off, NULL,
  108. buf, buf ) );
  109. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  110. mbedtls_camellia_crypt_cfb128( &ctx,
  111. valid_mode,
  112. sizeof( buf ),
  113. &off, buf,
  114. NULL, buf ) );
  115. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  116. mbedtls_camellia_crypt_cfb128( &ctx,
  117. valid_mode,
  118. sizeof( buf ),
  119. &off, buf,
  120. buf, NULL ) );
  121. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  122. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  123. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  124. mbedtls_camellia_crypt_ctr( NULL,
  125. sizeof( buf ),
  126. &off,
  127. buf, buf,
  128. buf, buf ) );
  129. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  130. mbedtls_camellia_crypt_ctr( &ctx,
  131. sizeof( buf ),
  132. NULL,
  133. buf, buf,
  134. buf, buf ) );
  135. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  136. mbedtls_camellia_crypt_ctr( &ctx,
  137. sizeof( buf ),
  138. &off,
  139. NULL, buf,
  140. buf, buf ) );
  141. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  142. mbedtls_camellia_crypt_ctr( &ctx,
  143. sizeof( buf ),
  144. &off,
  145. buf, NULL,
  146. buf, buf ) );
  147. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  148. mbedtls_camellia_crypt_ctr( &ctx,
  149. sizeof( buf ),
  150. &off,
  151. buf, buf,
  152. NULL, buf ) );
  153. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  154. mbedtls_camellia_crypt_ctr( &ctx,
  155. sizeof( buf ),
  156. &off,
  157. buf, buf,
  158. buf, NULL ) );
  159. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  160. exit:
  161. return;
  162. }
  163. /* END_CASE */
  164. /* BEGIN_CASE */
  165. void camellia_encrypt_ecb( data_t * key_str, data_t * src_str,
  166. data_t * dst, int setkey_result )
  167. {
  168. unsigned char output[100];
  169. mbedtls_camellia_context ctx;
  170. memset(output, 0x00, 100);
  171. mbedtls_camellia_init( &ctx );
  172. TEST_ASSERT( mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
  173. if( setkey_result == 0 )
  174. {
  175. TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->x, output ) == 0 );
  176. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
  177. }
  178. exit:
  179. mbedtls_camellia_free( &ctx );
  180. }
  181. /* END_CASE */
  182. /* BEGIN_CASE */
  183. void camellia_decrypt_ecb( data_t * key_str, data_t * src_str,
  184. data_t * dst, int setkey_result )
  185. {
  186. unsigned char output[100];
  187. mbedtls_camellia_context ctx;
  188. memset(output, 0x00, 100);
  189. mbedtls_camellia_init( &ctx );
  190. TEST_ASSERT( mbedtls_camellia_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
  191. if( setkey_result == 0 )
  192. {
  193. TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->x, output ) == 0 );
  194. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
  195. }
  196. exit:
  197. mbedtls_camellia_free( &ctx );
  198. }
  199. /* END_CASE */
  200. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  201. void camellia_encrypt_cbc( data_t * key_str, data_t * iv_str,
  202. data_t * src_str, data_t * dst, int cbc_result )
  203. {
  204. unsigned char output[100];
  205. mbedtls_camellia_context ctx;
  206. memset(output, 0x00, 100);
  207. mbedtls_camellia_init( &ctx );
  208. mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  209. TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->len, iv_str->x, src_str->x, output) == cbc_result );
  210. if( cbc_result == 0 )
  211. {
  212. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
  213. dst->len ) == 0 );
  214. }
  215. exit:
  216. mbedtls_camellia_free( &ctx );
  217. }
  218. /* END_CASE */
  219. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  220. void camellia_decrypt_cbc( data_t * key_str, data_t * iv_str,
  221. data_t * src_str, data_t * dst,
  222. int cbc_result )
  223. {
  224. unsigned char output[100];
  225. mbedtls_camellia_context ctx;
  226. memset(output, 0x00, 100);
  227. mbedtls_camellia_init( &ctx );
  228. mbedtls_camellia_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
  229. TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
  230. if( cbc_result == 0 )
  231. {
  232. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
  233. dst->len ) == 0 );
  234. }
  235. exit:
  236. mbedtls_camellia_free( &ctx );
  237. }
  238. /* END_CASE */
  239. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  240. void camellia_encrypt_cfb128( data_t * key_str, data_t * iv_str,
  241. data_t * src_str, data_t * dst )
  242. {
  243. unsigned char output[100];
  244. mbedtls_camellia_context ctx;
  245. size_t iv_offset = 0;
  246. memset(output, 0x00, 100);
  247. mbedtls_camellia_init( &ctx );
  248. mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  249. TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
  250. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
  251. exit:
  252. mbedtls_camellia_free( &ctx );
  253. }
  254. /* END_CASE */
  255. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  256. void camellia_decrypt_cfb128( data_t * key_str, data_t * iv_str,
  257. data_t * src_str,
  258. data_t * dst )
  259. {
  260. unsigned char output[100];
  261. mbedtls_camellia_context ctx;
  262. size_t iv_offset = 0;
  263. memset(output, 0x00, 100);
  264. mbedtls_camellia_init( &ctx );
  265. mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
  266. TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
  267. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
  268. exit:
  269. mbedtls_camellia_free( &ctx );
  270. }
  271. /* END_CASE */
  272. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  273. void camellia_selftest( )
  274. {
  275. TEST_ASSERT( mbedtls_camellia_self_test( 1 ) == 0 );
  276. }
  277. /* END_CASE */