sm2_sign.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2017 Ribose Inc. All Rights Reserved.
  4. * Ported from Ribose contributions from Botan.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include "internal/deprecated.h"
  12. #include "crypto/sm2.h"
  13. #include "crypto/sm2err.h"
  14. #include "crypto/ec.h" /* ossl_ec_group_do_inverse_ord() */
  15. #include "internal/numbers.h"
  16. #include <openssl/err.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/bn.h>
  19. #include <string.h>
  20. int ossl_sm2_compute_z_digest(uint8_t *out,
  21. const EVP_MD *digest,
  22. const uint8_t *id,
  23. const size_t id_len,
  24. const EC_KEY *key)
  25. {
  26. int rc = 0;
  27. const EC_GROUP *group = EC_KEY_get0_group(key);
  28. const EC_POINT *pubkey = EC_KEY_get0_public_key(key);
  29. BN_CTX *ctx = NULL;
  30. EVP_MD_CTX *hash = NULL;
  31. BIGNUM *p = NULL;
  32. BIGNUM *a = NULL;
  33. BIGNUM *b = NULL;
  34. BIGNUM *xG = NULL;
  35. BIGNUM *yG = NULL;
  36. BIGNUM *xA = NULL;
  37. BIGNUM *yA = NULL;
  38. int p_bytes = 0;
  39. uint8_t *buf = NULL;
  40. uint16_t entl = 0;
  41. uint8_t e_byte = 0;
  42. /* SM2 Signatures require a public key, check for it */
  43. if (pubkey == NULL) {
  44. ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
  45. goto done;
  46. }
  47. hash = EVP_MD_CTX_new();
  48. if (hash == NULL) {
  49. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  50. goto done;
  51. }
  52. ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
  53. if (ctx == NULL) {
  54. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  55. goto done;
  56. }
  57. p = BN_CTX_get(ctx);
  58. a = BN_CTX_get(ctx);
  59. b = BN_CTX_get(ctx);
  60. xG = BN_CTX_get(ctx);
  61. yG = BN_CTX_get(ctx);
  62. xA = BN_CTX_get(ctx);
  63. yA = BN_CTX_get(ctx);
  64. if (yA == NULL) {
  65. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  66. goto done;
  67. }
  68. if (!EVP_DigestInit(hash, digest)) {
  69. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  70. goto done;
  71. }
  72. /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
  73. if (id_len >= (UINT16_MAX / 8)) {
  74. /* too large */
  75. ERR_raise(ERR_LIB_SM2, SM2_R_ID_TOO_LARGE);
  76. goto done;
  77. }
  78. entl = (uint16_t)(8 * id_len);
  79. e_byte = entl >> 8;
  80. if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
  81. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  82. goto done;
  83. }
  84. e_byte = entl & 0xFF;
  85. if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
  86. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  87. goto done;
  88. }
  89. if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
  90. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  91. goto done;
  92. }
  93. if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
  94. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  95. goto done;
  96. }
  97. p_bytes = BN_num_bytes(p);
  98. buf = OPENSSL_zalloc(p_bytes);
  99. if (buf == NULL)
  100. goto done;
  101. if (BN_bn2binpad(a, buf, p_bytes) < 0
  102. || !EVP_DigestUpdate(hash, buf, p_bytes)
  103. || BN_bn2binpad(b, buf, p_bytes) < 0
  104. || !EVP_DigestUpdate(hash, buf, p_bytes)
  105. || !EC_POINT_get_affine_coordinates(group,
  106. EC_GROUP_get0_generator(group),
  107. xG, yG, ctx)
  108. || BN_bn2binpad(xG, buf, p_bytes) < 0
  109. || !EVP_DigestUpdate(hash, buf, p_bytes)
  110. || BN_bn2binpad(yG, buf, p_bytes) < 0
  111. || !EVP_DigestUpdate(hash, buf, p_bytes)
  112. || !EC_POINT_get_affine_coordinates(group,
  113. pubkey,
  114. xA, yA, ctx)
  115. || BN_bn2binpad(xA, buf, p_bytes) < 0
  116. || !EVP_DigestUpdate(hash, buf, p_bytes)
  117. || BN_bn2binpad(yA, buf, p_bytes) < 0
  118. || !EVP_DigestUpdate(hash, buf, p_bytes)
  119. || !EVP_DigestFinal(hash, out, NULL)) {
  120. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  121. goto done;
  122. }
  123. rc = 1;
  124. done:
  125. OPENSSL_free(buf);
  126. BN_CTX_free(ctx);
  127. EVP_MD_CTX_free(hash);
  128. return rc;
  129. }
  130. static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
  131. const EC_KEY *key,
  132. const uint8_t *id,
  133. const size_t id_len,
  134. const uint8_t *msg, size_t msg_len)
  135. {
  136. EVP_MD_CTX *hash = EVP_MD_CTX_new();
  137. const int md_size = EVP_MD_get_size(digest);
  138. uint8_t *z = NULL;
  139. BIGNUM *e = NULL;
  140. EVP_MD *fetched_digest = NULL;
  141. OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
  142. const char *propq = ossl_ec_key_get0_propq(key);
  143. if (md_size < 0) {
  144. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
  145. goto done;
  146. }
  147. if (hash == NULL) {
  148. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  149. goto done;
  150. }
  151. z = OPENSSL_zalloc(md_size);
  152. if (z == NULL)
  153. goto done;
  154. fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
  155. if (fetched_digest == NULL) {
  156. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  157. goto done;
  158. }
  159. if (!ossl_sm2_compute_z_digest(z, fetched_digest, id, id_len, key)) {
  160. /* SM2err already called */
  161. goto done;
  162. }
  163. if (!EVP_DigestInit(hash, fetched_digest)
  164. || !EVP_DigestUpdate(hash, z, md_size)
  165. || !EVP_DigestUpdate(hash, msg, msg_len)
  166. /* reuse z buffer to hold H(Z || M) */
  167. || !EVP_DigestFinal(hash, z, NULL)) {
  168. ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
  169. goto done;
  170. }
  171. e = BN_bin2bn(z, md_size, NULL);
  172. if (e == NULL)
  173. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  174. done:
  175. EVP_MD_free(fetched_digest);
  176. OPENSSL_free(z);
  177. EVP_MD_CTX_free(hash);
  178. return e;
  179. }
  180. static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
  181. {
  182. const BIGNUM *dA = EC_KEY_get0_private_key(key);
  183. const EC_GROUP *group = EC_KEY_get0_group(key);
  184. const BIGNUM *order = EC_GROUP_get0_order(group);
  185. ECDSA_SIG *sig = NULL;
  186. EC_POINT *kG = NULL;
  187. BN_CTX *ctx = NULL;
  188. BIGNUM *k = NULL;
  189. BIGNUM *rk = NULL;
  190. BIGNUM *r = NULL;
  191. BIGNUM *s = NULL;
  192. BIGNUM *x1 = NULL;
  193. BIGNUM *tmp = NULL;
  194. OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
  195. if (dA == NULL) {
  196. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_PRIVATE_KEY);
  197. goto done;
  198. }
  199. kG = EC_POINT_new(group);
  200. if (kG == NULL) {
  201. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  202. goto done;
  203. }
  204. ctx = BN_CTX_new_ex(libctx);
  205. if (ctx == NULL) {
  206. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  207. goto done;
  208. }
  209. BN_CTX_start(ctx);
  210. k = BN_CTX_get(ctx);
  211. rk = BN_CTX_get(ctx);
  212. x1 = BN_CTX_get(ctx);
  213. tmp = BN_CTX_get(ctx);
  214. if (tmp == NULL) {
  215. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  216. goto done;
  217. }
  218. /*
  219. * These values are returned and so should not be allocated out of the
  220. * context
  221. */
  222. r = BN_new();
  223. s = BN_new();
  224. if (r == NULL || s == NULL) {
  225. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  226. goto done;
  227. }
  228. /*
  229. * A3: Generate a random number k in [1,n-1] using random number generators;
  230. * A4: Compute (x1,y1)=[k]G, and convert the type of data x1 to be integer
  231. * as specified in clause 4.2.8 of GM/T 0003.1-2012;
  232. * A5: Compute r=(e+x1) mod n. If r=0 or r+k=n, then go to A3;
  233. * A6: Compute s=(1/(1+dA)*(k-r*dA)) mod n. If s=0, then go to A3;
  234. * A7: Convert the type of data (r,s) to be bit strings according to the details
  235. * in clause 4.2.2 of GM/T 0003.1-2012. Then the signature of message M is (r,s).
  236. */
  237. for (;;) {
  238. if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
  239. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  240. goto done;
  241. }
  242. if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
  243. || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
  244. ctx)
  245. || !BN_mod_add(r, e, x1, order, ctx)) {
  246. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  247. goto done;
  248. }
  249. /* try again if r == 0 or r+k == n */
  250. if (BN_is_zero(r))
  251. continue;
  252. if (!BN_add(rk, r, k)) {
  253. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  254. goto done;
  255. }
  256. if (BN_cmp(rk, order) == 0)
  257. continue;
  258. if (!BN_add(s, dA, BN_value_one())
  259. || !ossl_ec_group_do_inverse_ord(group, s, s, ctx)
  260. || !BN_mod_mul(tmp, dA, r, order, ctx)
  261. || !BN_sub(tmp, k, tmp)
  262. || !BN_mod_mul(s, s, tmp, order, ctx)) {
  263. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  264. goto done;
  265. }
  266. /* try again if s == 0 */
  267. if (BN_is_zero(s))
  268. continue;
  269. sig = ECDSA_SIG_new();
  270. if (sig == NULL) {
  271. ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
  272. goto done;
  273. }
  274. /* takes ownership of r and s */
  275. ECDSA_SIG_set0(sig, r, s);
  276. break;
  277. }
  278. done:
  279. if (sig == NULL) {
  280. BN_free(r);
  281. BN_free(s);
  282. }
  283. BN_CTX_free(ctx);
  284. EC_POINT_free(kG);
  285. return sig;
  286. }
  287. static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
  288. const BIGNUM *e)
  289. {
  290. int ret = 0;
  291. const EC_GROUP *group = EC_KEY_get0_group(key);
  292. const BIGNUM *order = EC_GROUP_get0_order(group);
  293. BN_CTX *ctx = NULL;
  294. EC_POINT *pt = NULL;
  295. BIGNUM *t = NULL;
  296. BIGNUM *x1 = NULL;
  297. const BIGNUM *r = NULL;
  298. const BIGNUM *s = NULL;
  299. OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
  300. ctx = BN_CTX_new_ex(libctx);
  301. if (ctx == NULL) {
  302. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  303. goto done;
  304. }
  305. BN_CTX_start(ctx);
  306. t = BN_CTX_get(ctx);
  307. x1 = BN_CTX_get(ctx);
  308. if (x1 == NULL) {
  309. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  310. goto done;
  311. }
  312. pt = EC_POINT_new(group);
  313. if (pt == NULL) {
  314. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  315. goto done;
  316. }
  317. /*
  318. * B1: verify whether r' in [1,n-1], verification failed if not
  319. * B2: verify whether s' in [1,n-1], verification failed if not
  320. * B3: set M'~=ZA || M'
  321. * B4: calculate e'=Hv(M'~)
  322. * B5: calculate t = (r' + s') modn, verification failed if t=0
  323. * B6: calculate the point (x1', y1')=[s']G + [t]PA
  324. * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
  325. */
  326. ECDSA_SIG_get0(sig, &r, &s);
  327. if (BN_cmp(r, BN_value_one()) < 0
  328. || BN_cmp(s, BN_value_one()) < 0
  329. || BN_cmp(order, r) <= 0
  330. || BN_cmp(order, s) <= 0) {
  331. ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
  332. goto done;
  333. }
  334. if (!BN_mod_add(t, r, s, order, ctx)) {
  335. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  336. goto done;
  337. }
  338. if (BN_is_zero(t)) {
  339. ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
  340. goto done;
  341. }
  342. if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
  343. || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
  344. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  345. goto done;
  346. }
  347. if (!BN_mod_add(t, e, x1, order, ctx)) {
  348. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  349. goto done;
  350. }
  351. if (BN_cmp(r, t) == 0)
  352. ret = 1;
  353. done:
  354. BN_CTX_end(ctx);
  355. EC_POINT_free(pt);
  356. BN_CTX_free(ctx);
  357. return ret;
  358. }
  359. ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
  360. const EVP_MD *digest,
  361. const uint8_t *id,
  362. const size_t id_len,
  363. const uint8_t *msg, size_t msg_len)
  364. {
  365. BIGNUM *e = NULL;
  366. ECDSA_SIG *sig = NULL;
  367. e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
  368. if (e == NULL) {
  369. /* SM2err already called */
  370. goto done;
  371. }
  372. sig = sm2_sig_gen(key, e);
  373. done:
  374. BN_free(e);
  375. return sig;
  376. }
  377. int ossl_sm2_do_verify(const EC_KEY *key,
  378. const EVP_MD *digest,
  379. const ECDSA_SIG *sig,
  380. const uint8_t *id,
  381. const size_t id_len,
  382. const uint8_t *msg, size_t msg_len)
  383. {
  384. BIGNUM *e = NULL;
  385. int ret = 0;
  386. e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
  387. if (e == NULL) {
  388. /* SM2err already called */
  389. goto done;
  390. }
  391. ret = sm2_sig_verify(key, sig, e);
  392. done:
  393. BN_free(e);
  394. return ret;
  395. }
  396. int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
  397. unsigned char *sig, unsigned int *siglen,
  398. EC_KEY *eckey)
  399. {
  400. BIGNUM *e = NULL;
  401. ECDSA_SIG *s = NULL;
  402. int sigleni;
  403. int ret = -1;
  404. if (sig == NULL) {
  405. ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
  406. goto done;
  407. }
  408. e = BN_bin2bn(dgst, dgstlen, NULL);
  409. if (e == NULL) {
  410. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  411. goto done;
  412. }
  413. s = sm2_sig_gen(eckey, e);
  414. if (s == NULL) {
  415. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  416. goto done;
  417. }
  418. sigleni = i2d_ECDSA_SIG(s, &sig);
  419. if (sigleni < 0) {
  420. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  421. goto done;
  422. }
  423. *siglen = (unsigned int)sigleni;
  424. ret = 1;
  425. done:
  426. ECDSA_SIG_free(s);
  427. BN_free(e);
  428. return ret;
  429. }
  430. int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
  431. const unsigned char *sig, int sig_len,
  432. EC_KEY *eckey)
  433. {
  434. ECDSA_SIG *s = NULL;
  435. BIGNUM *e = NULL;
  436. const unsigned char *p = sig;
  437. unsigned char *der = NULL;
  438. int derlen = -1;
  439. int ret = -1;
  440. s = ECDSA_SIG_new();
  441. if (s == NULL) {
  442. ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
  443. goto done;
  444. }
  445. if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
  446. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  447. goto done;
  448. }
  449. /* Ensure signature uses DER and doesn't have trailing garbage */
  450. derlen = i2d_ECDSA_SIG(s, &der);
  451. if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
  452. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  453. goto done;
  454. }
  455. e = BN_bin2bn(dgst, dgstlen, NULL);
  456. if (e == NULL) {
  457. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  458. goto done;
  459. }
  460. ret = sm2_sig_verify(eckey, s, e);
  461. done:
  462. OPENSSL_free(der);
  463. BN_free(e);
  464. ECDSA_SIG_free(s);
  465. return ret;
  466. }