sm2_sign.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Copyright 2017-2023 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. kG = EC_POINT_new(group);
  196. if (kG == NULL) {
  197. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  198. goto done;
  199. }
  200. ctx = BN_CTX_new_ex(libctx);
  201. if (ctx == NULL) {
  202. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  203. goto done;
  204. }
  205. BN_CTX_start(ctx);
  206. k = BN_CTX_get(ctx);
  207. rk = BN_CTX_get(ctx);
  208. x1 = BN_CTX_get(ctx);
  209. tmp = BN_CTX_get(ctx);
  210. if (tmp == NULL) {
  211. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  212. goto done;
  213. }
  214. /*
  215. * These values are returned and so should not be allocated out of the
  216. * context
  217. */
  218. r = BN_new();
  219. s = BN_new();
  220. if (r == NULL || s == NULL) {
  221. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  222. goto done;
  223. }
  224. /*
  225. * A3: Generate a random number k in [1,n-1] using random number generators;
  226. * A4: Compute (x1,y1)=[k]G, and convert the type of data x1 to be integer
  227. * as specified in clause 4.2.8 of GM/T 0003.1-2012;
  228. * A5: Compute r=(e+x1) mod n. If r=0 or r+k=n, then go to A3;
  229. * A6: Compute s=(1/(1+dA)*(k-r*dA)) mod n. If s=0, then go to A3;
  230. * A7: Convert the type of data (r,s) to be bit strings according to the details
  231. * in clause 4.2.2 of GM/T 0003.1-2012. Then the signature of message M is (r,s).
  232. */
  233. for (;;) {
  234. if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
  235. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  236. goto done;
  237. }
  238. if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
  239. || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
  240. ctx)
  241. || !BN_mod_add(r, e, x1, order, ctx)) {
  242. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  243. goto done;
  244. }
  245. /* try again if r == 0 or r+k == n */
  246. if (BN_is_zero(r))
  247. continue;
  248. if (!BN_add(rk, r, k)) {
  249. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  250. goto done;
  251. }
  252. if (BN_cmp(rk, order) == 0)
  253. continue;
  254. if (!BN_add(s, dA, BN_value_one())
  255. || !ossl_ec_group_do_inverse_ord(group, s, s, ctx)
  256. || !BN_mod_mul(tmp, dA, r, order, ctx)
  257. || !BN_sub(tmp, k, tmp)
  258. || !BN_mod_mul(s, s, tmp, order, ctx)) {
  259. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  260. goto done;
  261. }
  262. /* try again if s == 0 */
  263. if (BN_is_zero(s))
  264. continue;
  265. sig = ECDSA_SIG_new();
  266. if (sig == NULL) {
  267. ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
  268. goto done;
  269. }
  270. /* takes ownership of r and s */
  271. ECDSA_SIG_set0(sig, r, s);
  272. break;
  273. }
  274. done:
  275. if (sig == NULL) {
  276. BN_free(r);
  277. BN_free(s);
  278. }
  279. BN_CTX_free(ctx);
  280. EC_POINT_free(kG);
  281. return sig;
  282. }
  283. static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
  284. const BIGNUM *e)
  285. {
  286. int ret = 0;
  287. const EC_GROUP *group = EC_KEY_get0_group(key);
  288. const BIGNUM *order = EC_GROUP_get0_order(group);
  289. BN_CTX *ctx = NULL;
  290. EC_POINT *pt = NULL;
  291. BIGNUM *t = NULL;
  292. BIGNUM *x1 = NULL;
  293. const BIGNUM *r = NULL;
  294. const BIGNUM *s = NULL;
  295. OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
  296. ctx = BN_CTX_new_ex(libctx);
  297. pt = EC_POINT_new(group);
  298. if (ctx == NULL || pt == NULL) {
  299. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  300. goto done;
  301. }
  302. BN_CTX_start(ctx);
  303. t = BN_CTX_get(ctx);
  304. x1 = BN_CTX_get(ctx);
  305. if (x1 == NULL) {
  306. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  307. goto done;
  308. }
  309. /*
  310. * B1: verify whether r' in [1,n-1], verification failed if not
  311. * B2: verify whether s' in [1,n-1], verification failed if not
  312. * B3: set M'~=ZA || M'
  313. * B4: calculate e'=Hv(M'~)
  314. * B5: calculate t = (r' + s') modn, verification failed if t=0
  315. * B6: calculate the point (x1', y1')=[s']G + [t]PA
  316. * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
  317. */
  318. ECDSA_SIG_get0(sig, &r, &s);
  319. if (BN_cmp(r, BN_value_one()) < 0
  320. || BN_cmp(s, BN_value_one()) < 0
  321. || BN_cmp(order, r) <= 0
  322. || BN_cmp(order, s) <= 0) {
  323. ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
  324. goto done;
  325. }
  326. if (!BN_mod_add(t, r, s, order, ctx)) {
  327. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  328. goto done;
  329. }
  330. if (BN_is_zero(t)) {
  331. ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
  332. goto done;
  333. }
  334. if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
  335. || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
  336. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  337. goto done;
  338. }
  339. if (!BN_mod_add(t, e, x1, order, ctx)) {
  340. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  341. goto done;
  342. }
  343. if (BN_cmp(r, t) == 0)
  344. ret = 1;
  345. done:
  346. BN_CTX_end(ctx);
  347. EC_POINT_free(pt);
  348. BN_CTX_free(ctx);
  349. return ret;
  350. }
  351. ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
  352. const EVP_MD *digest,
  353. const uint8_t *id,
  354. const size_t id_len,
  355. const uint8_t *msg, size_t msg_len)
  356. {
  357. BIGNUM *e = NULL;
  358. ECDSA_SIG *sig = NULL;
  359. e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
  360. if (e == NULL) {
  361. /* SM2err already called */
  362. goto done;
  363. }
  364. sig = sm2_sig_gen(key, e);
  365. done:
  366. BN_free(e);
  367. return sig;
  368. }
  369. int ossl_sm2_do_verify(const EC_KEY *key,
  370. const EVP_MD *digest,
  371. const ECDSA_SIG *sig,
  372. const uint8_t *id,
  373. const size_t id_len,
  374. const uint8_t *msg, size_t msg_len)
  375. {
  376. BIGNUM *e = NULL;
  377. int ret = 0;
  378. e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
  379. if (e == NULL) {
  380. /* SM2err already called */
  381. goto done;
  382. }
  383. ret = sm2_sig_verify(key, sig, e);
  384. done:
  385. BN_free(e);
  386. return ret;
  387. }
  388. int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
  389. unsigned char *sig, unsigned int *siglen,
  390. EC_KEY *eckey)
  391. {
  392. BIGNUM *e = NULL;
  393. ECDSA_SIG *s = NULL;
  394. int sigleni;
  395. int ret = -1;
  396. if (sig == NULL) {
  397. ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
  398. goto done;
  399. }
  400. e = BN_bin2bn(dgst, dgstlen, NULL);
  401. if (e == NULL) {
  402. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  403. goto done;
  404. }
  405. s = sm2_sig_gen(eckey, e);
  406. if (s == NULL) {
  407. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  408. goto done;
  409. }
  410. sigleni = i2d_ECDSA_SIG(s, &sig);
  411. if (sigleni < 0) {
  412. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  413. goto done;
  414. }
  415. *siglen = (unsigned int)sigleni;
  416. ret = 1;
  417. done:
  418. ECDSA_SIG_free(s);
  419. BN_free(e);
  420. return ret;
  421. }
  422. int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
  423. const unsigned char *sig, int sig_len,
  424. EC_KEY *eckey)
  425. {
  426. ECDSA_SIG *s = NULL;
  427. BIGNUM *e = NULL;
  428. const unsigned char *p = sig;
  429. unsigned char *der = NULL;
  430. int derlen = -1;
  431. int ret = -1;
  432. s = ECDSA_SIG_new();
  433. if (s == NULL) {
  434. ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
  435. goto done;
  436. }
  437. if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
  438. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  439. goto done;
  440. }
  441. /* Ensure signature uses DER and doesn't have trailing garbage */
  442. derlen = i2d_ECDSA_SIG(s, &der);
  443. if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
  444. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  445. goto done;
  446. }
  447. e = BN_bin2bn(dgst, dgstlen, NULL);
  448. if (e == NULL) {
  449. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  450. goto done;
  451. }
  452. ret = sm2_sig_verify(eckey, s, e);
  453. done:
  454. OPENSSL_free(der);
  455. BN_free(e);
  456. ECDSA_SIG_free(s);
  457. return ret;
  458. }