sm2sigtest.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2017 Ribose Inc. All Rights Reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <openssl/bio.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/bn.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. #include <openssl/rand.h>
  19. #include "testutil.h"
  20. #ifndef OPENSSL_NO_SM2
  21. # include <openssl/sm2.h>
  22. static RAND_METHOD fake_rand;
  23. static const RAND_METHOD *saved_rand;
  24. static uint8_t *fake_rand_bytes = NULL;
  25. static size_t fake_rand_bytes_offset = 0;
  26. static int get_faked_bytes(unsigned char *buf, int num)
  27. {
  28. int i;
  29. if (fake_rand_bytes == NULL)
  30. return saved_rand->bytes(buf, num);
  31. for (i = 0; i != num; ++i)
  32. buf[i] = fake_rand_bytes[fake_rand_bytes_offset + i];
  33. fake_rand_bytes_offset += num;
  34. return 1;
  35. }
  36. static int start_fake_rand(const char *hex_bytes)
  37. {
  38. /* save old rand method */
  39. if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
  40. return 0;
  41. fake_rand = *saved_rand;
  42. /* use own random function */
  43. fake_rand.bytes = get_faked_bytes;
  44. fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
  45. fake_rand_bytes_offset = 0;
  46. /* set new RAND_METHOD */
  47. if (!TEST_true(RAND_set_rand_method(&fake_rand)))
  48. return 0;
  49. return 1;
  50. }
  51. static int restore_rand(void)
  52. {
  53. OPENSSL_free(fake_rand_bytes);
  54. fake_rand_bytes = NULL;
  55. fake_rand_bytes_offset = 0;
  56. if (!TEST_true(RAND_set_rand_method(saved_rand)))
  57. return 0;
  58. return 1;
  59. }
  60. static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
  61. const char *b_hex, const char *x_hex,
  62. const char *y_hex, const char *order_hex,
  63. const char *cof_hex)
  64. {
  65. BIGNUM *p = NULL;
  66. BIGNUM *a = NULL;
  67. BIGNUM *b = NULL;
  68. BIGNUM *g_x = NULL;
  69. BIGNUM *g_y = NULL;
  70. BIGNUM *order = NULL;
  71. BIGNUM *cof = NULL;
  72. EC_POINT *generator = NULL;
  73. EC_GROUP *group = NULL;
  74. BN_hex2bn(&p, p_hex);
  75. BN_hex2bn(&a, a_hex);
  76. BN_hex2bn(&b, b_hex);
  77. group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  78. BN_free(p);
  79. BN_free(a);
  80. BN_free(b);
  81. if (group == NULL)
  82. return NULL;
  83. generator = EC_POINT_new(group);
  84. if (generator == NULL)
  85. return NULL;
  86. BN_hex2bn(&g_x, x_hex);
  87. BN_hex2bn(&g_y, y_hex);
  88. if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
  89. 0)
  90. return NULL;
  91. BN_free(g_x);
  92. BN_free(g_y);
  93. BN_hex2bn(&order, order_hex);
  94. BN_hex2bn(&cof, cof_hex);
  95. if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
  96. return NULL;
  97. EC_POINT_free(generator);
  98. BN_free(order);
  99. BN_free(cof);
  100. return group;
  101. }
  102. static int test_sm2(const EC_GROUP *group,
  103. const char *userid,
  104. const char *privkey_hex,
  105. const char *message,
  106. const char *k_hex, const char *r_hex, const char *s_hex)
  107. {
  108. const size_t msg_len = strlen(message);
  109. int ok = -1;
  110. BIGNUM *priv = NULL;
  111. EC_POINT *pt = NULL;
  112. EC_KEY *key = NULL;
  113. ECDSA_SIG *sig = NULL;
  114. const BIGNUM *sig_r = NULL;
  115. const BIGNUM *sig_s = NULL;
  116. BIGNUM *r = NULL;
  117. BIGNUM *s = NULL;
  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. start_fake_rand(k_hex);
  126. sig = SM2_do_sign(key, EVP_sm3(), userid, (const uint8_t *)message, msg_len);
  127. restore_rand();
  128. if (sig == NULL)
  129. return 0;
  130. ECDSA_SIG_get0(sig, &sig_r, &sig_s);
  131. BN_hex2bn(&r, r_hex);
  132. BN_hex2bn(&s, s_hex);
  133. if (BN_cmp(r, sig_r) != 0) {
  134. printf("Signature R mismatch: ");
  135. BN_print_fp(stdout, r);
  136. printf(" != ");
  137. BN_print_fp(stdout, sig_r);
  138. printf("\n");
  139. ok = 0;
  140. }
  141. if (BN_cmp(s, sig_s) != 0) {
  142. printf("Signature S mismatch: ");
  143. BN_print_fp(stdout, s);
  144. printf(" != ");
  145. BN_print_fp(stdout, sig_s);
  146. printf("\n");
  147. ok = 0;
  148. }
  149. ok = SM2_do_verify(key, EVP_sm3(), sig, userid, (const uint8_t *)message, msg_len);
  150. ECDSA_SIG_free(sig);
  151. EC_POINT_free(pt);
  152. EC_KEY_free(key);
  153. BN_free(priv);
  154. BN_free(r);
  155. BN_free(s);
  156. return ok;
  157. }
  158. static int sm2_sig_test(void)
  159. {
  160. int rc = 0;
  161. /* From draft-shen-sm2-ecdsa-02 */
  162. EC_GROUP *test_group =
  163. create_EC_group
  164. ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
  165. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
  166. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
  167. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
  168. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
  169. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
  170. "1");
  171. if (test_group == NULL)
  172. return 0;
  173. rc = test_sm2(test_group,
  174. "ALICE123@YAHOO.COM",
  175. "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
  176. "message digest",
  177. "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F",
  178. "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
  179. "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7");
  180. EC_GROUP_free(test_group);
  181. return rc;
  182. }
  183. #endif
  184. int setup_tests(void)
  185. {
  186. #ifdef OPENSSL_NO_SM2
  187. TEST_note("SM2 is disabled.");
  188. #else
  189. ADD_TEST(sm2_sig_test);
  190. #endif
  191. return 1;
  192. }