sm2_internal_test.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright 2017-2021 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 fake_random_generate_cb get_faked_bytes;
  26. static OSSL_PROVIDER *fake_rand = NULL;
  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, size_t num,
  31. ossl_unused const char *name,
  32. ossl_unused EVP_RAND_CTX *ctx)
  33. {
  34. if (!TEST_ptr(fake_rand_bytes) || !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. OPENSSL_free(fake_rand_bytes);
  46. fake_rand_bytes_offset = 0;
  47. fake_rand_size = strlen(hex_bytes) / 2;
  48. if (!TEST_ptr(fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL)))
  49. return 0;
  50. /* use own random function */
  51. fake_rand_set_public_private_callbacks(NULL, get_faked_bytes);
  52. return 1;
  53. }
  54. static void restore_rand(void)
  55. {
  56. fake_rand_set_public_private_callbacks(NULL, NULL);
  57. OPENSSL_free(fake_rand_bytes);
  58. fake_rand_bytes = NULL;
  59. fake_rand_bytes_offset = 0;
  60. }
  61. static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
  62. const char *b_hex, const char *x_hex,
  63. const char *y_hex, const char *order_hex,
  64. const char *cof_hex)
  65. {
  66. BIGNUM *p = NULL;
  67. BIGNUM *a = NULL;
  68. BIGNUM *b = NULL;
  69. BIGNUM *g_x = NULL;
  70. BIGNUM *g_y = NULL;
  71. BIGNUM *order = NULL;
  72. BIGNUM *cof = NULL;
  73. EC_POINT *generator = NULL;
  74. EC_GROUP *group = NULL;
  75. int ok = 0;
  76. if (!TEST_true(BN_hex2bn(&p, p_hex))
  77. || !TEST_true(BN_hex2bn(&a, a_hex))
  78. || !TEST_true(BN_hex2bn(&b, b_hex)))
  79. goto done;
  80. group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
  81. if (!TEST_ptr(group))
  82. goto done;
  83. generator = EC_POINT_new(group);
  84. if (!TEST_ptr(generator))
  85. goto done;
  86. if (!TEST_true(BN_hex2bn(&g_x, x_hex))
  87. || !TEST_true(BN_hex2bn(&g_y, y_hex))
  88. || !TEST_true(EC_POINT_set_affine_coordinates(group, generator, g_x,
  89. g_y, NULL)))
  90. goto done;
  91. if (!TEST_true(BN_hex2bn(&order, order_hex))
  92. || !TEST_true(BN_hex2bn(&cof, cof_hex))
  93. || !TEST_true(EC_GROUP_set_generator(group, generator, order, cof)))
  94. goto done;
  95. ok = 1;
  96. done:
  97. BN_free(p);
  98. BN_free(a);
  99. BN_free(b);
  100. BN_free(g_x);
  101. BN_free(g_y);
  102. EC_POINT_free(generator);
  103. BN_free(order);
  104. BN_free(cof);
  105. if (!ok) {
  106. EC_GROUP_free(group);
  107. group = NULL;
  108. }
  109. return group;
  110. }
  111. static int test_sm2_crypt(const EC_GROUP *group,
  112. const EVP_MD *digest,
  113. const char *privkey_hex,
  114. const char *message,
  115. const char *k_hex, const char *ctext_hex)
  116. {
  117. const size_t msg_len = strlen(message);
  118. BIGNUM *priv = NULL;
  119. EC_KEY *key = NULL;
  120. EC_POINT *pt = NULL;
  121. unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);
  122. size_t ctext_len = 0;
  123. size_t ptext_len = 0;
  124. uint8_t *ctext = NULL;
  125. uint8_t *recovered = NULL;
  126. size_t recovered_len = msg_len;
  127. int rc = 0;
  128. if (!TEST_ptr(expected)
  129. || !TEST_true(BN_hex2bn(&priv, privkey_hex)))
  130. goto done;
  131. key = EC_KEY_new();
  132. if (!TEST_ptr(key)
  133. || !TEST_true(EC_KEY_set_group(key, group))
  134. || !TEST_true(EC_KEY_set_private_key(key, priv)))
  135. goto done;
  136. pt = EC_POINT_new(group);
  137. if (!TEST_ptr(pt)
  138. || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))
  139. || !TEST_true(EC_KEY_set_public_key(key, pt))
  140. || !TEST_true(ossl_sm2_ciphertext_size(key, digest, msg_len,
  141. &ctext_len)))
  142. goto done;
  143. ctext = OPENSSL_zalloc(ctext_len);
  144. if (!TEST_ptr(ctext))
  145. goto done;
  146. start_fake_rand(k_hex);
  147. if (!TEST_true(ossl_sm2_encrypt(key, digest,
  148. (const uint8_t *)message, msg_len,
  149. ctext, &ctext_len))) {
  150. restore_rand();
  151. goto done;
  152. }
  153. restore_rand();
  154. if (!TEST_mem_eq(ctext, ctext_len, expected, ctext_len))
  155. goto done;
  156. if (!TEST_true(ossl_sm2_plaintext_size(ctext, ctext_len, &ptext_len))
  157. || !TEST_int_eq(ptext_len, msg_len))
  158. goto done;
  159. recovered = OPENSSL_zalloc(ptext_len);
  160. if (!TEST_ptr(recovered)
  161. || !TEST_true(ossl_sm2_decrypt(key, digest, ctext, ctext_len,
  162. recovered, &recovered_len))
  163. || !TEST_int_eq(recovered_len, msg_len)
  164. || !TEST_mem_eq(recovered, recovered_len, message, msg_len))
  165. goto done;
  166. rc = 1;
  167. done:
  168. BN_free(priv);
  169. EC_POINT_free(pt);
  170. OPENSSL_free(ctext);
  171. OPENSSL_free(recovered);
  172. OPENSSL_free(expected);
  173. EC_KEY_free(key);
  174. return rc;
  175. }
  176. static int sm2_crypt_test(void)
  177. {
  178. int testresult = 0;
  179. EC_GROUP *gm_group = NULL;
  180. EC_GROUP *test_group =
  181. create_EC_group
  182. ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
  183. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
  184. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
  185. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
  186. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
  187. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
  188. "1");
  189. if (!TEST_ptr(test_group))
  190. goto done;
  191. if (!test_sm2_crypt(
  192. test_group,
  193. EVP_sm3(),
  194. "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
  195. "encryption standard",
  196. "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
  197. "0092e8ff62146873c258557548500ab2df2a365e0609ab67640a1f6d57d7b17820"
  198. "008349312695a3e1d2f46905f39a766487f2432e95d6be0cb009fe8c69fd8825a7",
  199. "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
  200. "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
  201. "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
  202. "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467"))
  203. goto done;
  204. /* Same test as above except using SHA-256 instead of SM3 */
  205. if (!test_sm2_crypt(
  206. test_group,
  207. EVP_sha256(),
  208. "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
  209. "encryption standard",
  210. "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
  211. "003da18008784352192d70f22c26c243174a447ba272fec64163dd4742bae8bc98"
  212. "00df17605cf304e9dd1dfeb90c015e93b393a6f046792f790a6fa4228af67d9588",
  213. "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F"
  214. "6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84"
  215. "400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E9"
  216. "88E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33"))
  217. goto done;
  218. /* From Annex C in both GM/T0003.5-2012 and GB/T 32918.5-2016.*/
  219. gm_group = create_EC_group(
  220. "fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff",
  221. "fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc",
  222. "28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93",
  223. "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7",
  224. "bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0",
  225. "fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123",
  226. "1");
  227. if (!TEST_ptr(gm_group))
  228. goto done;
  229. if (!test_sm2_crypt(
  230. gm_group,
  231. EVP_sm3(),
  232. /* privkey (from which the encrypting public key is derived) */
  233. "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8",
  234. /* plaintext message */
  235. "encryption standard",
  236. /* ephemeral nonce k */
  237. "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21",
  238. /*
  239. * expected ciphertext, the field values are from GM/T 0003.5-2012
  240. * (Annex C), but serialized following the ASN.1 format specified
  241. * in GM/T 0009-2012 (Sec. 7.2).
  242. */
  243. "307C" /* SEQUENCE, 0x7c bytes */
  244. "0220" /* INTEGER, 0x20 bytes */
  245. "04EBFC718E8D1798620432268E77FEB6415E2EDE0E073C0F4F640ECD2E149A73"
  246. "0221" /* INTEGER, 0x21 bytes */
  247. "00" /* leading 00 due to DER for pos. int with topmost bit set */
  248. "E858F9D81E5430A57B36DAAB8F950A3C64E6EE6A63094D99283AFF767E124DF0"
  249. "0420" /* OCTET STRING, 0x20 bytes */
  250. "59983C18F809E262923C53AEC295D30383B54E39D609D160AFCB1908D0BD8766"
  251. "0413" /* OCTET STRING, 0x13 bytes */
  252. "21886CA989CA9C7D58087307CA93092D651EFA"))
  253. goto done;
  254. testresult = 1;
  255. done:
  256. EC_GROUP_free(test_group);
  257. EC_GROUP_free(gm_group);
  258. return testresult;
  259. }
  260. static int test_sm2_sign(const EC_GROUP *group,
  261. const char *userid,
  262. const char *privkey_hex,
  263. const char *message,
  264. const char *k_hex,
  265. const char *r_hex,
  266. const char *s_hex,
  267. int omit_pubkey)
  268. {
  269. const size_t msg_len = strlen(message);
  270. int ok = 0;
  271. BIGNUM *priv = NULL;
  272. EC_POINT *pt = NULL;
  273. EC_KEY *key = NULL;
  274. ECDSA_SIG *sig = NULL;
  275. const BIGNUM *sig_r = NULL;
  276. const BIGNUM *sig_s = NULL;
  277. BIGNUM *r = NULL;
  278. BIGNUM *s = NULL;
  279. if (!TEST_true(BN_hex2bn(&priv, privkey_hex)))
  280. goto done;
  281. key = EC_KEY_new();
  282. if (!TEST_ptr(key)
  283. || !TEST_true(EC_KEY_set_group(key, group))
  284. || !TEST_true(EC_KEY_set_private_key(key, priv)))
  285. goto done;
  286. if (omit_pubkey == 0) {
  287. pt = EC_POINT_new(group);
  288. if (!TEST_ptr(pt)
  289. || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))
  290. || !TEST_true(EC_KEY_set_public_key(key, pt)))
  291. goto done;
  292. }
  293. start_fake_rand(k_hex);
  294. sig = ossl_sm2_do_sign(key, EVP_sm3(), (const uint8_t *)userid,
  295. strlen(userid), (const uint8_t *)message, msg_len);
  296. if (!TEST_ptr(sig)) {
  297. restore_rand();
  298. goto done;
  299. }
  300. restore_rand();
  301. ECDSA_SIG_get0(sig, &sig_r, &sig_s);
  302. if (!TEST_true(BN_hex2bn(&r, r_hex))
  303. || !TEST_true(BN_hex2bn(&s, s_hex))
  304. || !TEST_BN_eq(r, sig_r)
  305. || !TEST_BN_eq(s, sig_s))
  306. goto done;
  307. ok = ossl_sm2_do_verify(key, EVP_sm3(), sig, (const uint8_t *)userid,
  308. strlen(userid), (const uint8_t *)message, msg_len);
  309. /* We goto done whether this passes or fails */
  310. TEST_true(ok);
  311. done:
  312. ECDSA_SIG_free(sig);
  313. EC_POINT_free(pt);
  314. EC_KEY_free(key);
  315. BN_free(priv);
  316. BN_free(r);
  317. BN_free(s);
  318. return ok;
  319. }
  320. static int sm2_sig_test(void)
  321. {
  322. int testresult = 0;
  323. EC_GROUP *gm_group = NULL;
  324. /* From draft-shen-sm2-ecdsa-02 */
  325. EC_GROUP *test_group =
  326. create_EC_group
  327. ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
  328. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
  329. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
  330. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
  331. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
  332. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
  333. "1");
  334. if (!TEST_ptr(test_group))
  335. goto done;
  336. if (!TEST_true(test_sm2_sign(
  337. test_group,
  338. "ALICE123@YAHOO.COM",
  339. "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
  340. "message digest",
  341. "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F"
  342. "007c47811054c6f99613a578eb8453706ccb96384fe7df5c171671e760bfa8be3a",
  343. "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
  344. "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7", 0)))
  345. goto done;
  346. /* From Annex A in both GM/T0003.5-2012 and GB/T 32918.5-2016.*/
  347. gm_group = create_EC_group(
  348. "fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff",
  349. "fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc",
  350. "28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93",
  351. "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7",
  352. "bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0",
  353. "fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123",
  354. "1");
  355. if (!TEST_ptr(gm_group))
  356. goto done;
  357. if (!TEST_true(test_sm2_sign(
  358. gm_group,
  359. /* the default ID specified in GM/T 0009-2012 (Sec. 10).*/
  360. SM2_DEFAULT_USERID,
  361. /* privkey */
  362. "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8",
  363. /* plaintext message */
  364. "message digest",
  365. /* ephemeral nonce k */
  366. "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21",
  367. /* expected signature, the field values are from GM/T 0003.5-2012,
  368. Annex A. */
  369. /* signature R, 0x20 bytes */
  370. "F5A03B0648D2C4630EEAC513E1BB81A15944DA3827D5B74143AC7EACEEE720B3",
  371. /* signature S, 0x20 bytes */
  372. "B1B6AA29DF212FD8763182BC0D421CA1BB9038FD1F7F42D4840B69C485BBC1AA", 0)))
  373. goto done;
  374. /* Make sure we fail if we omit the public portion of the key */
  375. if (!TEST_false(test_sm2_sign(
  376. gm_group,
  377. /* the default ID specified in GM/T 0009-2012 (Sec. 10).*/
  378. SM2_DEFAULT_USERID,
  379. /* privkey */
  380. "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8",
  381. /* plaintext message */
  382. "message digest",
  383. /* ephemeral nonce k */
  384. "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21",
  385. /* expected signature, the field values are from GM/T 0003.5-2012,
  386. Annex A. */
  387. /* signature R, 0x20 bytes */
  388. "F5A03B0648D2C4630EEAC513E1BB81A15944DA3827D5B74143AC7EACEEE720B3",
  389. /* signature S, 0x20 bytes */
  390. "B1B6AA29DF212FD8763182BC0D421CA1BB9038FD1F7F42D4840B69C485BBC1AA", 1)))
  391. goto done;
  392. testresult = 1;
  393. done:
  394. EC_GROUP_free(test_group);
  395. EC_GROUP_free(gm_group);
  396. return testresult;
  397. }
  398. #endif
  399. int setup_tests(void)
  400. {
  401. #ifdef OPENSSL_NO_SM2
  402. TEST_note("SM2 is disabled.");
  403. #else
  404. fake_rand = fake_rand_start(NULL);
  405. if (fake_rand == NULL)
  406. return 0;
  407. ADD_TEST(sm2_crypt_test);
  408. ADD_TEST(sm2_sig_test);
  409. #endif
  410. return 1;
  411. }
  412. void cleanup_tests(void)
  413. {
  414. #ifndef OPENSSL_NO_SM2
  415. fake_rand_finish(fake_rand);
  416. #endif
  417. }