test_suite_blowfish.function 14 KB

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