sm2_sign.c 14 KB

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