test_suite_des.function 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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( data_t * key, int ret )
  10. {
  11. TEST_ASSERT( mbedtls_des_key_check_weak( key->x ) == ret );
  12. }
  13. /* END_CASE */
  14. /* BEGIN_CASE */
  15. void des_encrypt_ecb( data_t * key_str, data_t * src_str, data_t * dst )
  16. {
  17. unsigned char output[100];
  18. mbedtls_des_context ctx;
  19. memset(output, 0x00, 100);
  20. mbedtls_des_init( &ctx );
  21. mbedtls_des_setkey_enc( &ctx, key_str->x );
  22. TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str->x, output ) == 0 );
  23. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 );
  24. exit:
  25. mbedtls_des_free( &ctx );
  26. }
  27. /* END_CASE */
  28. /* BEGIN_CASE */
  29. void des_decrypt_ecb( data_t * key_str, data_t * src_str, data_t * dst )
  30. {
  31. unsigned char output[100];
  32. mbedtls_des_context ctx;
  33. memset(output, 0x00, 100);
  34. mbedtls_des_init( &ctx );
  35. mbedtls_des_setkey_dec( &ctx, key_str->x );
  36. TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str->x, output ) == 0 );
  37. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 );
  38. exit:
  39. mbedtls_des_free( &ctx );
  40. }
  41. /* END_CASE */
  42. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  43. void des_encrypt_cbc( data_t * key_str, data_t * iv_str,
  44. data_t * src_str, data_t * dst, int cbc_result )
  45. {
  46. unsigned char output[100];
  47. mbedtls_des_context ctx;
  48. memset(output, 0x00, 100);
  49. mbedtls_des_init( &ctx );
  50. mbedtls_des_setkey_enc( &ctx, key_str->x );
  51. TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
  52. if( cbc_result == 0 )
  53. {
  54. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
  55. dst->len ) == 0 );
  56. }
  57. exit:
  58. mbedtls_des_free( &ctx );
  59. }
  60. /* END_CASE */
  61. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  62. void des_decrypt_cbc( data_t * key_str, data_t * iv_str,
  63. data_t * src_str, data_t * dst,
  64. int cbc_result )
  65. {
  66. unsigned char output[100];
  67. mbedtls_des_context ctx;
  68. memset(output, 0x00, 100);
  69. mbedtls_des_init( &ctx );
  70. mbedtls_des_setkey_dec( &ctx, key_str->x );
  71. TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
  72. if( cbc_result == 0 )
  73. {
  74. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
  75. dst->len ) == 0 );
  76. }
  77. exit:
  78. mbedtls_des_free( &ctx );
  79. }
  80. /* END_CASE */
  81. /* BEGIN_CASE */
  82. void des3_encrypt_ecb( int key_count, data_t * key_str,
  83. data_t * src_str, data_t * dst )
  84. {
  85. unsigned char output[100];
  86. mbedtls_des3_context ctx;
  87. memset(output, 0x00, 100);
  88. mbedtls_des3_init( &ctx );
  89. if( key_count == 2 )
  90. mbedtls_des3_set2key_enc( &ctx, key_str->x );
  91. else if( key_count == 3 )
  92. mbedtls_des3_set3key_enc( &ctx, key_str->x );
  93. else
  94. TEST_ASSERT( 0 );
  95. TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str->x, output ) == 0 );
  96. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 );
  97. exit:
  98. mbedtls_des3_free( &ctx );
  99. }
  100. /* END_CASE */
  101. /* BEGIN_CASE */
  102. void des3_decrypt_ecb( int key_count, data_t * key_str,
  103. data_t * src_str, data_t * dst )
  104. {
  105. unsigned char output[100];
  106. mbedtls_des3_context ctx;
  107. memset(output, 0x00, 100);
  108. mbedtls_des3_init( &ctx );
  109. if( key_count == 2 )
  110. mbedtls_des3_set2key_dec( &ctx, key_str->x );
  111. else if( key_count == 3 )
  112. mbedtls_des3_set3key_dec( &ctx, key_str->x );
  113. else
  114. TEST_ASSERT( 0 );
  115. TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str->x, output ) == 0 );
  116. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 );
  117. exit:
  118. mbedtls_des3_free( &ctx );
  119. }
  120. /* END_CASE */
  121. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  122. void des3_encrypt_cbc( int key_count, data_t * key_str,
  123. data_t * iv_str, data_t * src_str,
  124. data_t * dst, int cbc_result )
  125. {
  126. unsigned char output[100];
  127. mbedtls_des3_context ctx;
  128. memset(output, 0x00, 100);
  129. mbedtls_des3_init( &ctx );
  130. if( key_count == 2 )
  131. mbedtls_des3_set2key_enc( &ctx, key_str->x );
  132. else if( key_count == 3 )
  133. mbedtls_des3_set3key_enc( &ctx, key_str->x );
  134. else
  135. TEST_ASSERT( 0 );
  136. TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
  137. if( cbc_result == 0 )
  138. {
  139. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x,
  140. src_str->len, dst->len ) == 0 );
  141. }
  142. exit:
  143. mbedtls_des3_free( &ctx );
  144. }
  145. /* END_CASE */
  146. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  147. void des3_decrypt_cbc( int key_count, data_t * key_str,
  148. data_t * iv_str, data_t * src_str,
  149. data_t * dst, int cbc_result )
  150. {
  151. unsigned char output[100];
  152. mbedtls_des3_context ctx;
  153. memset(output, 0x00, 100);
  154. mbedtls_des3_init( &ctx );
  155. if( key_count == 2 )
  156. mbedtls_des3_set2key_dec( &ctx, key_str->x );
  157. else if( key_count == 3 )
  158. mbedtls_des3_set3key_dec( &ctx, key_str->x );
  159. else
  160. TEST_ASSERT( 0 );
  161. TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
  162. if( cbc_result == 0 )
  163. {
  164. TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
  165. dst->len ) == 0 );
  166. }
  167. exit:
  168. mbedtls_des3_free( &ctx );
  169. }
  170. /* END_CASE */
  171. /* BEGIN_CASE */
  172. void des_key_parity_run( )
  173. {
  174. int i, j, cnt;
  175. unsigned char key[MBEDTLS_DES_KEY_SIZE];
  176. unsigned int parity;
  177. memset( key, 0, MBEDTLS_DES_KEY_SIZE );
  178. cnt = 0;
  179. // Iterate through all possible byte values
  180. //
  181. for( i = 0; i < 32; i++ )
  182. {
  183. for( j = 0; j < 8; j++ )
  184. key[j] = cnt++;
  185. // Set the key parity according to the table
  186. //
  187. mbedtls_des_key_set_parity( key );
  188. // Check the parity with a function
  189. //
  190. for( j = 0; j < 8; j++ )
  191. {
  192. parity = key[j] ^ ( key[j] >> 4 );
  193. parity = parity ^
  194. ( parity >> 1 ) ^
  195. ( parity >> 2 ) ^
  196. ( parity >> 3 );
  197. parity &= 1;
  198. if( parity != 1 )
  199. TEST_ASSERT( 0 );
  200. }
  201. // Check the parity with the table
  202. //
  203. TEST_ASSERT( mbedtls_des_key_check_key_parity( key ) == 0 );
  204. }
  205. }
  206. /* END_CASE */
  207. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  208. void des_selftest( )
  209. {
  210. TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 );
  211. }
  212. /* END_CASE */