sm2_internal_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /*
  10. * Low level APIs are deprecated for public use, but still ok for internal use.
  11. */
  12. #include "internal/deprecated.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <openssl/bio.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/crypto.h>
  20. #include <openssl/err.h>
  21. #include <openssl/rand.h>
  22. #include "testutil.h"
  23. #ifndef OPENSSL_NO_SM2
  24. # include "crypto/sm2.h"
  25. static RAND_METHOD fake_rand;
  26. static const RAND_METHOD *saved_rand;
  27. static uint8_t *fake_rand_bytes = NULL;
  28. static size_t fake_rand_bytes_offset = 0;
  29. static size_t fake_rand_size = 0;
  30. static int get_faked_bytes(unsigned char *buf, int num)
  31. {
  32. if (fake_rand_bytes == NULL)
  33. return saved_rand->bytes(buf, num);
  34. if (!TEST_size_t_gt(fake_rand_size, 0))
  35. return 0;
  36. while (num-- > 0) {
  37. if (fake_rand_bytes_offset >= fake_rand_size)
  38. fake_rand_bytes_offset = 0;
  39. *buf++ = fake_rand_bytes[fake_rand_bytes_offset++];
  40. }
  41. return 1;
  42. }
  43. static int start_fake_rand(const char *hex_bytes)
  44. {
  45. /* save old rand method */
  46. if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
  47. return 0;
  48. fake_rand = *saved_rand;
  49. /* use own random function */
  50. fake_rand.bytes = get_faked_bytes;
  51. fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
  52. fake_rand_bytes_offset = 0;
  53. fake_rand_size = strlen(hex_bytes) / 2;
  54. /* set new RAND_METHOD */
  55. if (!TEST_true(RAND_set_rand_method(&fake_rand)))
  56. return 0;
  57. return 1;
  58. }
  59. static int restore_rand(void)
  60. {
  61. OPENSSL_free(fake_rand_bytes);
  62. fake_rand_bytes = NULL;
  63. fake_rand_bytes_offset = 0;
  64. if (!TEST_true(RAND_set_rand_method(saved_rand)))
  65. return 0;
  66. return 1;
  67. }
  68. static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
  69. const char *b_hex, const char *x_hex,
  70. const char *y_hex, const char *order_hex,
  71. const char *cof_hex)
  72. {
  73. BIGNUM *p = NULL;
  74. BIGNUM *a = NULL;
  75. BIGNUM *b = NULL;
  76. BIGNUM *g_x = NULL;
  77. BIGNUM *g_y = NULL;
  78. BIGNUM *order = NULL;
  79. BIGNUM *cof = NULL;
  80. EC_POINT *generator = NULL;
  81. EC_GROUP *group = NULL;
  82. int ok = 0;
  83. if (!TEST_true(BN_hex2bn(&p, p_hex))
  84. || !TEST_true(BN_hex2bn(&a, a_hex))
  85. || !TEST_true(BN_hex2bn(&b, b_hex)))
  86. goto done;
  87. group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  88. if (!TEST_ptr(group))
  89. goto done;
  90. generator = EC_POINT_new(group);
  91. if (!TEST_ptr(generator))
  92. goto done;
  93. if (!TEST_true(BN_hex2bn(&g_x, x_hex))
  94. || !TEST_true(BN_hex2bn(&g_y, y_hex))
  95. || !TEST_true(EC_POINT_set_affine_coordinates(group, generator, g_x,
  96. g_y, NULL)))
  97. goto done;
  98. if (!TEST_true(BN_hex2bn(&order, order_hex))
  99. || !TEST_true(BN_hex2bn(&cof, cof_hex))
  100. || !TEST_true(EC_GROUP_set_generator(group, generator, order, cof)))
  101. goto done;
  102. ok = 1;
  103. done:
  104. BN_free(p);
  105. BN_free(a);
  106. BN_free(b);
  107. BN_free(g_x);
  108. BN_free(g_y);
  109. EC_POINT_free(generator);
  110. BN_free(order);
  111. BN_free(cof);
  112. if (!ok) {
  113. EC_GROUP_free(group);
  114. group = NULL;
  115. }
  116. return group;
  117. }
  118. static int test_sm2_crypt(const EC_GROUP *group,
  119. const EVP_MD *digest,
  120. const char *privkey_hex,
  121. const char *message,
  122. const char *k_hex, const char *ctext_hex)
  123. {
  124. const size_t msg_len = strlen(message);
  125. BIGNUM *priv = NULL;
  126. EC_KEY *key = NULL;
  127. EC_POINT *pt = NULL;
  128. unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);
  129. size_t ctext_len = 0;
  130. size_t ptext_len = 0;
  131. uint8_t *ctext = NULL;
  132. uint8_t *recovered = NULL;
  133. size_t recovered_len = msg_len;
  134. int rc = 0;
  135. if (!TEST_ptr(expected)
  136. || !TEST_true(BN_hex2bn(&priv, privkey_hex)))
  137. goto done;
  138. key = EC_KEY_new();
  139. if (!TEST_ptr(key)
  140. || !TEST_true(EC_KEY_set_group(key, group))
  141. || !TEST_true(EC_KEY_set_private_key(key, priv)))
  142. goto done;
  143. pt = EC_POINT_new(group);
  144. if (!TEST_ptr(pt)
  145. || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))
  146. || !TEST_true(EC_KEY_set_public_key(key, pt))
  147. || !TEST_true(sm2_ciphertext_size(key, digest, msg_len, &ctext_len)))
  148. goto done;
  149. ctext = OPENSSL_zalloc(ctext_len);
  150. if (!TEST_ptr(ctext))
  151. goto done;
  152. start_fake_rand(k_hex);
  153. if (!TEST_true(sm2_encrypt(key, digest, (const uint8_t *)message, msg_len,
  154. ctext, &ctext_len))) {
  155. restore_rand();
  156. goto done;
  157. }
  158. restore_rand();
  159. if (!TEST_mem_eq(ctext, ctext_len, expected, ctext_len))
  160. goto done;
  161. if (!TEST_true(sm2_plaintext_size(key, digest, ctext_len, &ptext_len))
  162. || !TEST_int_eq(ptext_len, msg_len))
  163. goto done;
  164. recovered = OPENSSL_zalloc(ptext_len);
  165. if (!TEST_ptr(recovered)
  166. || !TEST_true(sm2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len))
  167. || !TEST_int_eq(recovered_len, msg_len)
  168. || !TEST_mem_eq(recovered, recovered_len, message, msg_len))
  169. goto done;
  170. rc = 1;
  171. done:
  172. BN_free(priv);
  173. EC_POINT_free(pt);
  174. OPENSSL_free(ctext);
  175. OPENSSL_free(recovered);
  176. OPENSSL_free(expected);
  177. EC_KEY_free(key);
  178. return rc;
  179. }
  180. static int sm2_crypt_test(void)
  181. {
  182. int testresult = 0;
  183. EC_GROUP *test_group =
  184. create_EC_group
  185. ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
  186. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
  187. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
  188. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
  189. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
  190. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
  191. "1");
  192. if (!TEST_ptr(test_group))
  193. goto done;
  194. if (!test_sm2_crypt(
  195. test_group,
  196. EVP_sm3(),
  197. "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
  198. "encryption standard",
  199. "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
  200. "0092e8ff62146873c258557548500ab2df2a365e0609ab67640a1f6d57d7b17820"
  201. "008349312695a3e1d2f46905f39a766487f2432e95d6be0cb009fe8c69fd8825a7",
  202. "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
  203. "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
  204. "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
  205. "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467"))
  206. goto done;
  207. /* Same test as above except using SHA-256 instead of SM3 */
  208. if (!test_sm2_crypt(
  209. test_group,
  210. EVP_sha256(),
  211. "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
  212. "encryption standard",
  213. "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
  214. "003da18008784352192d70f22c26c243174a447ba272fec64163dd4742bae8bc98"
  215. "00df17605cf304e9dd1dfeb90c015e93b393a6f046792f790a6fa4228af67d9588",
  216. "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F"
  217. "6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84"
  218. "400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E9"
  219. "88E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33"))
  220. goto done;
  221. testresult = 1;
  222. done:
  223. EC_GROUP_free(test_group);
  224. return testresult;
  225. }
  226. static int test_sm2_sign(const EC_GROUP *group,
  227. const char *userid,
  228. const char *privkey_hex,
  229. const char *message,
  230. const char *k_hex,
  231. const char *r_hex,
  232. const char *s_hex)
  233. {
  234. const size_t msg_len = strlen(message);
  235. int ok = 0;
  236. BIGNUM *priv = NULL;
  237. EC_POINT *pt = NULL;
  238. EC_KEY *key = NULL;
  239. ECDSA_SIG *sig = NULL;
  240. const BIGNUM *sig_r = NULL;
  241. const BIGNUM *sig_s = NULL;
  242. BIGNUM *r = NULL;
  243. BIGNUM *s = NULL;
  244. if (!TEST_true(BN_hex2bn(&priv, privkey_hex)))
  245. goto done;
  246. key = EC_KEY_new();
  247. if (!TEST_ptr(key)
  248. || !TEST_true(EC_KEY_set_group(key, group))
  249. || !TEST_true(EC_KEY_set_private_key(key, priv)))
  250. goto done;
  251. pt = EC_POINT_new(group);
  252. if (!TEST_ptr(pt)
  253. || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))
  254. || !TEST_true(EC_KEY_set_public_key(key, pt)))
  255. goto done;
  256. start_fake_rand(k_hex);
  257. sig = sm2_do_sign(key, EVP_sm3(), (const uint8_t *)userid, strlen(userid),
  258. (const uint8_t *)message, msg_len);
  259. if (!TEST_ptr(sig)) {
  260. restore_rand();
  261. goto done;
  262. }
  263. restore_rand();
  264. ECDSA_SIG_get0(sig, &sig_r, &sig_s);
  265. if (!TEST_true(BN_hex2bn(&r, r_hex))
  266. || !TEST_true(BN_hex2bn(&s, s_hex))
  267. || !TEST_BN_eq(r, sig_r)
  268. || !TEST_BN_eq(s, sig_s))
  269. goto done;
  270. ok = sm2_do_verify(key, EVP_sm3(), sig, (const uint8_t *)userid,
  271. strlen(userid), (const uint8_t *)message, msg_len);
  272. /* We goto done whether this passes or fails */
  273. TEST_true(ok);
  274. done:
  275. ECDSA_SIG_free(sig);
  276. EC_POINT_free(pt);
  277. EC_KEY_free(key);
  278. BN_free(priv);
  279. BN_free(r);
  280. BN_free(s);
  281. return ok;
  282. }
  283. static int sm2_sig_test(void)
  284. {
  285. int testresult = 0;
  286. /* From draft-shen-sm2-ecdsa-02 */
  287. EC_GROUP *test_group =
  288. create_EC_group
  289. ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
  290. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
  291. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
  292. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
  293. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
  294. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
  295. "1");
  296. if (!TEST_ptr(test_group))
  297. goto done;
  298. if (!TEST_true(test_sm2_sign(
  299. test_group,
  300. "ALICE123@YAHOO.COM",
  301. "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
  302. "message digest",
  303. "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F"
  304. "007c47811054c6f99613a578eb8453706ccb96384fe7df5c171671e760bfa8be3a",
  305. "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
  306. "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7")))
  307. goto done;
  308. testresult = 1;
  309. done:
  310. EC_GROUP_free(test_group);
  311. return testresult;
  312. }
  313. #endif
  314. int setup_tests(void)
  315. {
  316. #ifdef OPENSSL_NO_SM2
  317. TEST_note("SM2 is disabled.");
  318. #else
  319. ADD_TEST(sm2_crypt_test);
  320. ADD_TEST(sm2_sig_test);
  321. #endif
  322. return 1;
  323. }