sm2crypttest.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/bn.h>
  15. #include <openssl/crypto.h>
  16. #include <openssl/err.h>
  17. #include <openssl/rand.h>
  18. #include "testutil.h"
  19. #ifndef OPENSSL_NO_SM2
  20. # include <openssl/sm2.h>
  21. static RAND_METHOD fake_rand;
  22. static const RAND_METHOD *saved_rand;
  23. static uint8_t *fake_rand_bytes = NULL;
  24. static size_t fake_rand_bytes_offset = 0;
  25. static int get_faked_bytes(unsigned char *buf, int num)
  26. {
  27. int i;
  28. if (fake_rand_bytes == NULL)
  29. return saved_rand->bytes(buf, num);
  30. for (i = 0; i != num; ++i)
  31. buf[i] = fake_rand_bytes[fake_rand_bytes_offset + i];
  32. fake_rand_bytes_offset += num;
  33. return 1;
  34. }
  35. static int start_fake_rand(const char *hex_bytes)
  36. {
  37. /* save old rand method */
  38. if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
  39. return 0;
  40. fake_rand = *saved_rand;
  41. /* use own random function */
  42. fake_rand.bytes = get_faked_bytes;
  43. fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
  44. fake_rand_bytes_offset = 0;
  45. /* set new RAND_METHOD */
  46. if (!TEST_true(RAND_set_rand_method(&fake_rand)))
  47. return 0;
  48. return 1;
  49. }
  50. static int restore_rand(void)
  51. {
  52. OPENSSL_free(fake_rand_bytes);
  53. fake_rand_bytes = NULL;
  54. fake_rand_bytes_offset = 0;
  55. if (!TEST_true(RAND_set_rand_method(saved_rand)))
  56. return 0;
  57. return 1;
  58. }
  59. static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
  60. const char *b_hex, const char *x_hex,
  61. const char *y_hex, const char *order_hex,
  62. const char *cof_hex)
  63. {
  64. BIGNUM *p = NULL;
  65. BIGNUM *a = NULL;
  66. BIGNUM *b = NULL;
  67. BIGNUM *g_x = NULL;
  68. BIGNUM *g_y = NULL;
  69. BIGNUM *order = NULL;
  70. BIGNUM *cof = NULL;
  71. EC_POINT *generator = NULL;
  72. EC_GROUP *group = NULL;
  73. BN_hex2bn(&p, p_hex);
  74. BN_hex2bn(&a, a_hex);
  75. BN_hex2bn(&b, b_hex);
  76. group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  77. BN_free(p);
  78. BN_free(a);
  79. BN_free(b);
  80. if (group == NULL)
  81. return NULL;
  82. generator = EC_POINT_new(group);
  83. if (generator == NULL)
  84. return NULL;
  85. BN_hex2bn(&g_x, x_hex);
  86. BN_hex2bn(&g_y, y_hex);
  87. if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
  88. 0)
  89. return NULL;
  90. BN_free(g_x);
  91. BN_free(g_y);
  92. BN_hex2bn(&order, order_hex);
  93. BN_hex2bn(&cof, cof_hex);
  94. if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
  95. return NULL;
  96. EC_POINT_free(generator);
  97. BN_free(order);
  98. BN_free(cof);
  99. return group;
  100. }
  101. static int test_sm2(const EC_GROUP *group,
  102. const EVP_MD *digest,
  103. const char *privkey_hex,
  104. const char *message,
  105. const char *k_hex, const char *ctext_hex)
  106. {
  107. const size_t msg_len = strlen(message);
  108. BIGNUM *priv = NULL;
  109. EC_KEY *key = NULL;
  110. EC_POINT *pt = NULL;
  111. unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);
  112. size_t ctext_len = 0;
  113. size_t ptext_len = 0;
  114. uint8_t *ctext = NULL;
  115. uint8_t *recovered = NULL;
  116. size_t recovered_len = msg_len;
  117. int rc = 0;
  118. BN_hex2bn(&priv, privkey_hex);
  119. key = EC_KEY_new();
  120. EC_KEY_set_group(key, group);
  121. EC_KEY_set_private_key(key, priv);
  122. pt = EC_POINT_new(group);
  123. EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
  124. EC_KEY_set_public_key(key, pt);
  125. BN_free(priv);
  126. EC_POINT_free(pt);
  127. ctext_len = SM2_ciphertext_size(key, digest, msg_len);
  128. ctext = OPENSSL_zalloc(ctext_len);
  129. if (ctext == NULL)
  130. goto done;
  131. start_fake_rand(k_hex);
  132. rc = SM2_encrypt(key, digest,
  133. (const uint8_t *)message, msg_len, ctext, &ctext_len);
  134. restore_rand();
  135. TEST_mem_eq(ctext, ctext_len, expected, ctext_len);
  136. if (rc == 0)
  137. goto done;
  138. ptext_len = SM2_plaintext_size(key, digest, ctext_len);
  139. TEST_int_eq(ptext_len, msg_len);
  140. recovered = OPENSSL_zalloc(ptext_len);
  141. if (recovered == NULL)
  142. goto done;
  143. rc = SM2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len);
  144. TEST_int_eq(recovered_len, msg_len);
  145. TEST_mem_eq(recovered, recovered_len, message, msg_len);
  146. if (rc == 0)
  147. return 0;
  148. rc = 1;
  149. done:
  150. OPENSSL_free(ctext);
  151. OPENSSL_free(recovered);
  152. OPENSSL_free(expected);
  153. EC_KEY_free(key);
  154. return rc;
  155. }
  156. static int sm2_crypt_test(void)
  157. {
  158. int rc;
  159. EC_GROUP *test_group =
  160. create_EC_group
  161. ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
  162. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
  163. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
  164. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
  165. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
  166. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
  167. "1");
  168. if (test_group == NULL)
  169. return 0;
  170. rc = test_sm2(test_group,
  171. EVP_sm3(),
  172. "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
  173. "encryption standard",
  174. "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
  175. "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
  176. "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
  177. "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
  178. "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467");
  179. if (rc == 0)
  180. return 0;
  181. /* Same test as above except using SHA-256 instead of SM3 */
  182. rc = test_sm2(test_group,
  183. EVP_sha256(),
  184. "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
  185. "encryption standard",
  186. "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
  187. "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E988E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33");
  188. if (rc == 0)
  189. return 0;
  190. EC_GROUP_free(test_group);
  191. return 1;
  192. }
  193. #endif
  194. int setup_tests(void)
  195. {
  196. #ifdef OPENSSL_NO_SM2
  197. TEST_note("SM2 is disabled.");
  198. #else
  199. ADD_TEST(sm2_crypt_test);
  200. #endif
  201. return 1;
  202. }