test_suite_blowfish.function 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_encrypt_ecb( char *hex_key_string, char *hex_src_string,
  10. char *hex_dst_string, int setkey_result )
  11. {
  12. unsigned char key_str[100];
  13. unsigned char src_str[100];
  14. unsigned char dst_str[100];
  15. unsigned char output[100];
  16. mbedtls_blowfish_context ctx;
  17. int key_len;
  18. memset(key_str, 0x00, 100);
  19. memset(src_str, 0x00, 100);
  20. memset(dst_str, 0x00, 100);
  21. memset(output, 0x00, 100);
  22. mbedtls_blowfish_init( &ctx );
  23. key_len = unhexify( key_str, hex_key_string );
  24. unhexify( src_str, hex_src_string );
  25. TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
  26. if( setkey_result == 0 )
  27. {
  28. TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str, output ) == 0 );
  29. hexify( dst_str, output, 8 );
  30. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  31. }
  32. exit:
  33. mbedtls_blowfish_free( &ctx );
  34. }
  35. /* END_CASE */
  36. /* BEGIN_CASE */
  37. void blowfish_decrypt_ecb( char *hex_key_string, char *hex_src_string,
  38. char *hex_dst_string, int setkey_result )
  39. {
  40. unsigned char key_str[100];
  41. unsigned char src_str[100];
  42. unsigned char dst_str[100];
  43. unsigned char output[100];
  44. mbedtls_blowfish_context ctx;
  45. int key_len;
  46. memset(key_str, 0x00, 100);
  47. memset(src_str, 0x00, 100);
  48. memset(dst_str, 0x00, 100);
  49. memset(output, 0x00, 100);
  50. mbedtls_blowfish_init( &ctx );
  51. key_len = unhexify( key_str, hex_key_string );
  52. unhexify( src_str, hex_src_string );
  53. TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
  54. if( setkey_result == 0 )
  55. {
  56. TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str, output ) == 0 );
  57. hexify( dst_str, output, 8 );
  58. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  59. }
  60. exit:
  61. mbedtls_blowfish_free( &ctx );
  62. }
  63. /* END_CASE */
  64. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  65. void blowfish_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
  66. char *hex_src_string, char *hex_dst_string,
  67. int cbc_result )
  68. {
  69. unsigned char key_str[100];
  70. unsigned char iv_str[100];
  71. unsigned char src_str[100];
  72. unsigned char dst_str[100];
  73. unsigned char output[100];
  74. mbedtls_blowfish_context ctx;
  75. int key_len, data_len;
  76. memset(key_str, 0x00, 100);
  77. memset(iv_str, 0x00, 100);
  78. memset(src_str, 0x00, 100);
  79. memset(dst_str, 0x00, 100);
  80. memset(output, 0x00, 100);
  81. mbedtls_blowfish_init( &ctx );
  82. key_len = unhexify( key_str, hex_key_string );
  83. unhexify( iv_str, hex_iv_string );
  84. data_len = unhexify( src_str, hex_src_string );
  85. mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
  86. TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result );
  87. if( cbc_result == 0 )
  88. {
  89. hexify( dst_str, output, data_len );
  90. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  91. }
  92. exit:
  93. mbedtls_blowfish_free( &ctx );
  94. }
  95. /* END_CASE */
  96. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  97. void blowfish_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
  98. char *hex_src_string, char *hex_dst_string,
  99. int cbc_result )
  100. {
  101. unsigned char key_str[100];
  102. unsigned char iv_str[100];
  103. unsigned char src_str[100];
  104. unsigned char dst_str[100];
  105. unsigned char output[100];
  106. mbedtls_blowfish_context ctx;
  107. int key_len, data_len;
  108. memset(key_str, 0x00, 100);
  109. memset(iv_str, 0x00, 100);
  110. memset(src_str, 0x00, 100);
  111. memset(dst_str, 0x00, 100);
  112. memset(output, 0x00, 100);
  113. mbedtls_blowfish_init( &ctx );
  114. key_len = unhexify( key_str, hex_key_string );
  115. unhexify( iv_str, hex_iv_string );
  116. data_len = unhexify( src_str, hex_src_string );
  117. mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
  118. TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result );
  119. if( cbc_result == 0)
  120. {
  121. hexify( dst_str, output, data_len );
  122. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  123. }
  124. exit:
  125. mbedtls_blowfish_free( &ctx );
  126. }
  127. /* END_CASE */
  128. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  129. void blowfish_encrypt_cfb64( char *hex_key_string, char *hex_iv_string,
  130. char *hex_src_string, char *hex_dst_string )
  131. {
  132. unsigned char key_str[100];
  133. unsigned char iv_str[100];
  134. unsigned char src_str[100];
  135. unsigned char dst_str[100];
  136. unsigned char output[100];
  137. mbedtls_blowfish_context ctx;
  138. size_t iv_offset = 0;
  139. int key_len, src_len;
  140. memset(key_str, 0x00, 100);
  141. memset(iv_str, 0x00, 100);
  142. memset(src_str, 0x00, 100);
  143. memset(dst_str, 0x00, 100);
  144. memset(output, 0x00, 100);
  145. mbedtls_blowfish_init( &ctx );
  146. key_len = unhexify( key_str, hex_key_string );
  147. unhexify( iv_str, hex_iv_string );
  148. src_len = unhexify( src_str, hex_src_string );
  149. mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
  150. TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
  151. hexify( dst_str, output, src_len );
  152. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  153. exit:
  154. mbedtls_blowfish_free( &ctx );
  155. }
  156. /* END_CASE */
  157. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  158. void blowfish_decrypt_cfb64( char *hex_key_string, char *hex_iv_string,
  159. char *hex_src_string, char *hex_dst_string )
  160. {
  161. unsigned char key_str[100];
  162. unsigned char iv_str[100];
  163. unsigned char src_str[100];
  164. unsigned char dst_str[100];
  165. unsigned char output[100];
  166. mbedtls_blowfish_context ctx;
  167. size_t iv_offset = 0;
  168. int key_len, src_len;
  169. memset(key_str, 0x00, 100);
  170. memset(iv_str, 0x00, 100);
  171. memset(src_str, 0x00, 100);
  172. memset(dst_str, 0x00, 100);
  173. memset(output, 0x00, 100);
  174. mbedtls_blowfish_init( &ctx );
  175. key_len = unhexify( key_str, hex_key_string );
  176. unhexify( iv_str, hex_iv_string );
  177. src_len = unhexify( src_str, hex_src_string );
  178. mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
  179. TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
  180. hexify( dst_str, output, src_len );
  181. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  182. exit:
  183. mbedtls_blowfish_free( &ctx );
  184. }
  185. /* END_CASE */
  186. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
  187. void blowfish_encrypt_ctr( char *hex_key_string, char *hex_iv_string,
  188. char *hex_src_string, char *hex_dst_string )
  189. {
  190. unsigned char key_str[100];
  191. unsigned char iv_str[100];
  192. unsigned char stream_str[100];
  193. unsigned char src_str[100];
  194. unsigned char dst_str[100];
  195. unsigned char output[100];
  196. mbedtls_blowfish_context ctx;
  197. size_t iv_offset = 0;
  198. int key_len, src_len;
  199. memset(key_str, 0x00, 100);
  200. memset(iv_str, 0x00, 100);
  201. memset(stream_str, 0x00, 100);
  202. memset(src_str, 0x00, 100);
  203. memset(dst_str, 0x00, 100);
  204. memset(output, 0x00, 100);
  205. mbedtls_blowfish_init( &ctx );
  206. key_len = unhexify( key_str, hex_key_string );
  207. unhexify( iv_str, hex_iv_string );
  208. src_len = unhexify( src_str, hex_src_string );
  209. mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
  210. TEST_ASSERT( mbedtls_blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 );
  211. hexify( dst_str, output, src_len );
  212. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  213. exit:
  214. mbedtls_blowfish_free( &ctx );
  215. }
  216. /* END_CASE */