test_suite_des.function 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. TEST_ASSERT(mbedtls_des_setkey_enc(&ctx, key_str->x) == 0);
  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. TEST_ASSERT(mbedtls_des_setkey_dec(&ctx, key_str->x) == 0);
  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. TEST_ASSERT(mbedtls_des_setkey_enc(&ctx, key_str->x) == 0);
  51. TEST_ASSERT(mbedtls_des_crypt_cbc(&ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x,
  52. src_str->x, output) == cbc_result);
  53. if (cbc_result == 0) {
  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. TEST_ASSERT(mbedtls_des_setkey_dec(&ctx, key_str->x) == 0);
  71. TEST_ASSERT(mbedtls_des_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x,
  72. src_str->x, output) == cbc_result);
  73. if (cbc_result == 0) {
  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. TEST_ASSERT(mbedtls_des3_set2key_enc(&ctx, key_str->x) == 0);
  91. } else if (key_count == 3) {
  92. TEST_ASSERT(mbedtls_des3_set3key_enc(&ctx, key_str->x) == 0);
  93. } else {
  94. TEST_ASSERT(0);
  95. }
  96. TEST_ASSERT(mbedtls_des3_crypt_ecb(&ctx, src_str->x, output) == 0);
  97. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
  98. exit:
  99. mbedtls_des3_free(&ctx);
  100. }
  101. /* END_CASE */
  102. /* BEGIN_CASE */
  103. void des3_decrypt_ecb(int key_count, data_t *key_str,
  104. data_t *src_str, data_t *dst)
  105. {
  106. unsigned char output[100];
  107. mbedtls_des3_context ctx;
  108. memset(output, 0x00, 100);
  109. mbedtls_des3_init(&ctx);
  110. if (key_count == 2) {
  111. TEST_ASSERT(mbedtls_des3_set2key_dec(&ctx, key_str->x) == 0);
  112. } else if (key_count == 3) {
  113. TEST_ASSERT(mbedtls_des3_set3key_dec(&ctx, key_str->x) == 0);
  114. } else {
  115. TEST_ASSERT(0);
  116. }
  117. TEST_ASSERT(mbedtls_des3_crypt_ecb(&ctx, src_str->x, output) == 0);
  118. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
  119. exit:
  120. mbedtls_des3_free(&ctx);
  121. }
  122. /* END_CASE */
  123. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  124. void des3_encrypt_cbc(int key_count, data_t *key_str,
  125. data_t *iv_str, data_t *src_str,
  126. data_t *dst, int cbc_result)
  127. {
  128. unsigned char output[100];
  129. mbedtls_des3_context ctx;
  130. memset(output, 0x00, 100);
  131. mbedtls_des3_init(&ctx);
  132. if (key_count == 2) {
  133. TEST_ASSERT(mbedtls_des3_set2key_enc(&ctx, key_str->x) == 0);
  134. } else if (key_count == 3) {
  135. TEST_ASSERT(mbedtls_des3_set3key_enc(&ctx, key_str->x) == 0);
  136. } else {
  137. TEST_ASSERT(0);
  138. }
  139. TEST_ASSERT(mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x,
  140. src_str->x, output) == cbc_result);
  141. if (cbc_result == 0) {
  142. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
  143. src_str->len, dst->len) == 0);
  144. }
  145. exit:
  146. mbedtls_des3_free(&ctx);
  147. }
  148. /* END_CASE */
  149. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  150. void des3_decrypt_cbc(int key_count, data_t *key_str,
  151. data_t *iv_str, data_t *src_str,
  152. data_t *dst, int cbc_result)
  153. {
  154. unsigned char output[100];
  155. mbedtls_des3_context ctx;
  156. memset(output, 0x00, 100);
  157. mbedtls_des3_init(&ctx);
  158. if (key_count == 2) {
  159. TEST_ASSERT(mbedtls_des3_set2key_dec(&ctx, key_str->x) == 0);
  160. } else if (key_count == 3) {
  161. TEST_ASSERT(mbedtls_des3_set3key_dec(&ctx, key_str->x) == 0);
  162. } else {
  163. TEST_ASSERT(0);
  164. }
  165. TEST_ASSERT(mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x,
  166. src_str->x, output) == cbc_result);
  167. if (cbc_result == 0) {
  168. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
  169. dst->len) == 0);
  170. }
  171. exit:
  172. mbedtls_des3_free(&ctx);
  173. }
  174. /* END_CASE */
  175. /* BEGIN_CASE */
  176. void des_key_parity_run()
  177. {
  178. int i, j, cnt;
  179. unsigned char key[MBEDTLS_DES_KEY_SIZE];
  180. unsigned int parity;
  181. memset(key, 0, MBEDTLS_DES_KEY_SIZE);
  182. cnt = 0;
  183. // Iterate through all possible byte values
  184. //
  185. for (i = 0; i < 32; i++) {
  186. for (j = 0; j < 8; j++) {
  187. key[j] = cnt++;
  188. }
  189. // Set the key parity according to the table
  190. //
  191. mbedtls_des_key_set_parity(key);
  192. // Check the parity with a function
  193. //
  194. for (j = 0; j < 8; j++) {
  195. parity = key[j] ^ (key[j] >> 4);
  196. parity = parity ^
  197. (parity >> 1) ^
  198. (parity >> 2) ^
  199. (parity >> 3);
  200. parity &= 1;
  201. if (parity != 1) {
  202. TEST_ASSERT(0);
  203. }
  204. }
  205. // Check the parity with the table
  206. //
  207. TEST_ASSERT(mbedtls_des_key_check_key_parity(key) == 0);
  208. }
  209. }
  210. /* END_CASE */
  211. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  212. void des_selftest()
  213. {
  214. TEST_ASSERT(mbedtls_des_self_test(1) == 0);
  215. }
  216. /* END_CASE */