test_suite_des.function 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/des.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_DES_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void des_check_weak( char *key_hex, int ret )
  10. {
  11. unsigned char key[MBEDTLS_DES_KEY_SIZE];
  12. memset( key, 0, sizeof key );
  13. unhexify( key, key_hex );
  14. TEST_ASSERT( mbedtls_des_key_check_weak( key ) == ret );
  15. }
  16. /* END_CASE */
  17. /* BEGIN_CASE */
  18. void des_encrypt_ecb( char *hex_key_string, char *hex_src_string,
  19. char *hex_dst_string )
  20. {
  21. unsigned char key_str[100];
  22. unsigned char src_str[100];
  23. unsigned char dst_str[100];
  24. unsigned char output[100];
  25. mbedtls_des_context ctx;
  26. memset(key_str, 0x00, 100);
  27. memset(src_str, 0x00, 100);
  28. memset(dst_str, 0x00, 100);
  29. memset(output, 0x00, 100);
  30. mbedtls_des_init( &ctx );
  31. unhexify( key_str, hex_key_string );
  32. unhexify( src_str, hex_src_string );
  33. mbedtls_des_setkey_enc( &ctx, key_str );
  34. TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 );
  35. hexify( dst_str, output, 8 );
  36. TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
  37. exit:
  38. mbedtls_des_free( &ctx );
  39. }
  40. /* END_CASE */
  41. /* BEGIN_CASE */
  42. void des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
  43. char *hex_dst_string )
  44. {
  45. unsigned char key_str[100];
  46. unsigned char src_str[100];
  47. unsigned char dst_str[100];
  48. unsigned char output[100];
  49. mbedtls_des_context ctx;
  50. memset(key_str, 0x00, 100);
  51. memset(src_str, 0x00, 100);
  52. memset(dst_str, 0x00, 100);
  53. memset(output, 0x00, 100);
  54. mbedtls_des_init( &ctx );
  55. unhexify( key_str, hex_key_string );
  56. unhexify( src_str, hex_src_string );
  57. mbedtls_des_setkey_dec( &ctx, key_str );
  58. TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 );
  59. hexify( dst_str, output, 8 );
  60. TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
  61. exit:
  62. mbedtls_des_free( &ctx );
  63. }
  64. /* END_CASE */
  65. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  66. void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
  67. char *hex_src_string, char *hex_dst_string, 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_des_context ctx;
  75. int src_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_des_init( &ctx );
  82. unhexify( key_str, hex_key_string );
  83. unhexify( iv_str, hex_iv_string );
  84. src_len = unhexify( src_str, hex_src_string );
  85. mbedtls_des_setkey_enc( &ctx, key_str );
  86. TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
  87. if( cbc_result == 0 )
  88. {
  89. hexify( dst_str, output, src_len );
  90. TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
  91. }
  92. exit:
  93. mbedtls_des_free( &ctx );
  94. }
  95. /* END_CASE */
  96. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  97. void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
  98. char *hex_src_string, char *hex_dst_string, int cbc_result )
  99. {
  100. unsigned char key_str[100];
  101. unsigned char iv_str[100];
  102. unsigned char src_str[100];
  103. unsigned char dst_str[100];
  104. unsigned char output[100];
  105. mbedtls_des_context ctx;
  106. int src_len;
  107. memset(key_str, 0x00, 100);
  108. memset(iv_str, 0x00, 100);
  109. memset(src_str, 0x00, 100);
  110. memset(dst_str, 0x00, 100);
  111. memset(output, 0x00, 100);
  112. mbedtls_des_init( &ctx );
  113. unhexify( key_str, hex_key_string );
  114. unhexify( iv_str, hex_iv_string );
  115. src_len = unhexify( src_str, hex_src_string );
  116. mbedtls_des_setkey_dec( &ctx, key_str );
  117. TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
  118. if( cbc_result == 0 )
  119. {
  120. hexify( dst_str, output, src_len );
  121. TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
  122. }
  123. exit:
  124. mbedtls_des_free( &ctx );
  125. }
  126. /* END_CASE */
  127. /* BEGIN_CASE */
  128. void des3_encrypt_ecb( int key_count, char *hex_key_string,
  129. char *hex_src_string, char *hex_dst_string )
  130. {
  131. unsigned char key_str[100];
  132. unsigned char src_str[100];
  133. unsigned char dst_str[100];
  134. unsigned char output[100];
  135. mbedtls_des3_context ctx;
  136. memset(key_str, 0x00, 100);
  137. memset(src_str, 0x00, 100);
  138. memset(dst_str, 0x00, 100);
  139. memset(output, 0x00, 100);
  140. mbedtls_des3_init( &ctx );
  141. unhexify( key_str, hex_key_string );
  142. unhexify( src_str, hex_src_string );
  143. if( key_count == 2 )
  144. mbedtls_des3_set2key_enc( &ctx, key_str );
  145. else if( key_count == 3 )
  146. mbedtls_des3_set3key_enc( &ctx, key_str );
  147. else
  148. TEST_ASSERT( 0 );
  149. TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 );
  150. hexify( dst_str, output, 8 );
  151. TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
  152. exit:
  153. mbedtls_des3_free( &ctx );
  154. }
  155. /* END_CASE */
  156. /* BEGIN_CASE */
  157. void des3_decrypt_ecb( int key_count, char *hex_key_string,
  158. char *hex_src_string, char *hex_dst_string )
  159. {
  160. unsigned char key_str[100];
  161. unsigned char src_str[100];
  162. unsigned char dst_str[100];
  163. unsigned char output[100];
  164. mbedtls_des3_context ctx;
  165. memset(key_str, 0x00, 100);
  166. memset(src_str, 0x00, 100);
  167. memset(dst_str, 0x00, 100);
  168. memset(output, 0x00, 100);
  169. mbedtls_des3_init( &ctx );
  170. unhexify( key_str, hex_key_string );
  171. unhexify( src_str, hex_src_string );
  172. if( key_count == 2 )
  173. mbedtls_des3_set2key_dec( &ctx, key_str );
  174. else if( key_count == 3 )
  175. mbedtls_des3_set3key_dec( &ctx, key_str );
  176. else
  177. TEST_ASSERT( 0 );
  178. TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 );
  179. hexify( dst_str, output, 8 );
  180. TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
  181. exit:
  182. mbedtls_des3_free( &ctx );
  183. }
  184. /* END_CASE */
  185. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  186. void des3_encrypt_cbc( int key_count, char *hex_key_string,
  187. char *hex_iv_string, char *hex_src_string,
  188. char *hex_dst_string, int cbc_result )
  189. {
  190. unsigned char key_str[100];
  191. unsigned char iv_str[100];
  192. unsigned char src_str[100];
  193. unsigned char dst_str[100];
  194. unsigned char output[100];
  195. mbedtls_des3_context ctx;
  196. int src_len;
  197. memset(key_str, 0x00, 100);
  198. memset(iv_str, 0x00, 100);
  199. memset(src_str, 0x00, 100);
  200. memset(dst_str, 0x00, 100);
  201. memset(output, 0x00, 100);
  202. mbedtls_des3_init( &ctx );
  203. unhexify( key_str, hex_key_string );
  204. unhexify( iv_str, hex_iv_string );
  205. src_len = unhexify( src_str, hex_src_string );
  206. if( key_count == 2 )
  207. mbedtls_des3_set2key_enc( &ctx, key_str );
  208. else if( key_count == 3 )
  209. mbedtls_des3_set3key_enc( &ctx, key_str );
  210. else
  211. TEST_ASSERT( 0 );
  212. TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
  213. if( cbc_result == 0 )
  214. {
  215. hexify( dst_str, output, src_len );
  216. TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
  217. }
  218. exit:
  219. mbedtls_des3_free( &ctx );
  220. }
  221. /* END_CASE */
  222. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  223. void des3_decrypt_cbc( int key_count, char *hex_key_string,
  224. char *hex_iv_string, char *hex_src_string,
  225. char *hex_dst_string, int cbc_result )
  226. {
  227. unsigned char key_str[100];
  228. unsigned char iv_str[100];
  229. unsigned char src_str[100];
  230. unsigned char dst_str[100];
  231. unsigned char output[100];
  232. mbedtls_des3_context ctx;
  233. int src_len;
  234. memset(key_str, 0x00, 100);
  235. memset(iv_str, 0x00, 100);
  236. memset(src_str, 0x00, 100);
  237. memset(dst_str, 0x00, 100);
  238. memset(output, 0x00, 100);
  239. mbedtls_des3_init( &ctx );
  240. unhexify( key_str, hex_key_string );
  241. unhexify( iv_str, hex_iv_string );
  242. src_len = unhexify( src_str, hex_src_string );
  243. if( key_count == 2 )
  244. mbedtls_des3_set2key_dec( &ctx, key_str );
  245. else if( key_count == 3 )
  246. mbedtls_des3_set3key_dec( &ctx, key_str );
  247. else
  248. TEST_ASSERT( 0 );
  249. TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
  250. if( cbc_result == 0 )
  251. {
  252. hexify( dst_str, output, src_len );
  253. TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
  254. }
  255. exit:
  256. mbedtls_des3_free( &ctx );
  257. }
  258. /* END_CASE */
  259. /* BEGIN_CASE */
  260. void des_key_parity_run()
  261. {
  262. int i, j, cnt;
  263. unsigned char key[MBEDTLS_DES_KEY_SIZE];
  264. unsigned int parity;
  265. memset( key, 0, MBEDTLS_DES_KEY_SIZE );
  266. cnt = 0;
  267. // Iterate through all possible byte values
  268. //
  269. for( i = 0; i < 32; i++ )
  270. {
  271. for( j = 0; j < 8; j++ )
  272. key[j] = cnt++;
  273. // Set the key parity according to the table
  274. //
  275. mbedtls_des_key_set_parity( key );
  276. // Check the parity with a function
  277. //
  278. for( j = 0; j < 8; j++ )
  279. {
  280. parity = key[j] ^ ( key[j] >> 4 );
  281. parity = parity ^
  282. ( parity >> 1 ) ^
  283. ( parity >> 2 ) ^
  284. ( parity >> 3 );
  285. parity &= 1;
  286. if( parity != 1 )
  287. TEST_ASSERT( 0 );
  288. }
  289. // Check the parity with the table
  290. //
  291. TEST_ASSERT( mbedtls_des_key_check_key_parity( key ) == 0 );
  292. }
  293. }
  294. /* END_CASE */
  295. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  296. void des_selftest()
  297. {
  298. TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 );
  299. }
  300. /* END_CASE */