sm2_sign.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright 2017-2020 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 "crypto/sm2.h"
  12. #include "crypto/sm2err.h"
  13. #include "crypto/ec.h" /* ec_group_do_inverse_ord() */
  14. #include "internal/numbers.h"
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/err.h>
  18. #include <openssl/bn.h>
  19. #include <string.h>
  20. int 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(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_size(digest);
  130. uint8_t *z = NULL;
  131. BIGNUM *e = NULL;
  132. EVP_MD *fetched_digest = NULL;
  133. OSSL_LIB_CTX *libctx = ec_key_get_libctx(key);
  134. const char *propq = 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_name(digest), propq);
  145. if (fetched_digest == NULL) {
  146. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  147. goto done;
  148. }
  149. if (!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 = 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. for (;;) {
  211. if (!BN_priv_rand_range_ex(k, order, ctx)) {
  212. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  213. goto done;
  214. }
  215. if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
  216. || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
  217. ctx)
  218. || !BN_mod_add(r, e, x1, order, ctx)) {
  219. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  220. goto done;
  221. }
  222. /* try again if r == 0 or r+k == n */
  223. if (BN_is_zero(r))
  224. continue;
  225. if (!BN_add(rk, r, k)) {
  226. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  227. goto done;
  228. }
  229. if (BN_cmp(rk, order) == 0)
  230. continue;
  231. if (!BN_add(s, dA, BN_value_one())
  232. || !ec_group_do_inverse_ord(group, s, s, ctx)
  233. || !BN_mod_mul(tmp, dA, r, order, ctx)
  234. || !BN_sub(tmp, k, tmp)
  235. || !BN_mod_mul(s, s, tmp, order, ctx)) {
  236. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  237. goto done;
  238. }
  239. sig = ECDSA_SIG_new();
  240. if (sig == NULL) {
  241. ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
  242. goto done;
  243. }
  244. /* takes ownership of r and s */
  245. ECDSA_SIG_set0(sig, r, s);
  246. break;
  247. }
  248. done:
  249. if (sig == NULL) {
  250. BN_free(r);
  251. BN_free(s);
  252. }
  253. BN_CTX_free(ctx);
  254. EC_POINT_free(kG);
  255. return sig;
  256. }
  257. static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
  258. const BIGNUM *e)
  259. {
  260. int ret = 0;
  261. const EC_GROUP *group = EC_KEY_get0_group(key);
  262. const BIGNUM *order = EC_GROUP_get0_order(group);
  263. BN_CTX *ctx = NULL;
  264. EC_POINT *pt = NULL;
  265. BIGNUM *t = NULL;
  266. BIGNUM *x1 = NULL;
  267. const BIGNUM *r = NULL;
  268. const BIGNUM *s = NULL;
  269. OSSL_LIB_CTX *libctx = ec_key_get_libctx(key);
  270. ctx = BN_CTX_new_ex(libctx);
  271. pt = EC_POINT_new(group);
  272. if (ctx == NULL || pt == NULL) {
  273. ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
  274. goto done;
  275. }
  276. BN_CTX_start(ctx);
  277. t = BN_CTX_get(ctx);
  278. x1 = BN_CTX_get(ctx);
  279. if (x1 == NULL) {
  280. ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
  281. goto done;
  282. }
  283. /*
  284. * B1: verify whether r' in [1,n-1], verification failed if not
  285. * B2: verify whether s' in [1,n-1], verification failed if not
  286. * B3: set M'~=ZA || M'
  287. * B4: calculate e'=Hv(M'~)
  288. * B5: calculate t = (r' + s') modn, verification failed if t=0
  289. * B6: calculate the point (x1', y1')=[s']G + [t]PA
  290. * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
  291. */
  292. ECDSA_SIG_get0(sig, &r, &s);
  293. if (BN_cmp(r, BN_value_one()) < 0
  294. || BN_cmp(s, BN_value_one()) < 0
  295. || BN_cmp(order, r) <= 0
  296. || BN_cmp(order, s) <= 0) {
  297. ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
  298. goto done;
  299. }
  300. if (!BN_mod_add(t, r, s, order, ctx)) {
  301. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  302. goto done;
  303. }
  304. if (BN_is_zero(t)) {
  305. ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
  306. goto done;
  307. }
  308. if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
  309. || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
  310. ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
  311. goto done;
  312. }
  313. if (!BN_mod_add(t, e, x1, order, ctx)) {
  314. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  315. goto done;
  316. }
  317. if (BN_cmp(r, t) == 0)
  318. ret = 1;
  319. done:
  320. EC_POINT_free(pt);
  321. BN_CTX_free(ctx);
  322. return ret;
  323. }
  324. ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
  325. const EVP_MD *digest,
  326. const uint8_t *id,
  327. const size_t id_len,
  328. const uint8_t *msg, size_t msg_len)
  329. {
  330. BIGNUM *e = NULL;
  331. ECDSA_SIG *sig = NULL;
  332. e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
  333. if (e == NULL) {
  334. /* SM2err already called */
  335. goto done;
  336. }
  337. sig = sm2_sig_gen(key, e);
  338. done:
  339. BN_free(e);
  340. return sig;
  341. }
  342. int sm2_do_verify(const EC_KEY *key,
  343. const EVP_MD *digest,
  344. const ECDSA_SIG *sig,
  345. const uint8_t *id,
  346. const size_t id_len,
  347. const uint8_t *msg, size_t msg_len)
  348. {
  349. BIGNUM *e = NULL;
  350. int ret = 0;
  351. e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
  352. if (e == NULL) {
  353. /* SM2err already called */
  354. goto done;
  355. }
  356. ret = sm2_sig_verify(key, sig, e);
  357. done:
  358. BN_free(e);
  359. return ret;
  360. }
  361. int sm2_internal_sign(const unsigned char *dgst, int dgstlen,
  362. unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
  363. {
  364. BIGNUM *e = NULL;
  365. ECDSA_SIG *s = NULL;
  366. int sigleni;
  367. int ret = -1;
  368. e = BN_bin2bn(dgst, dgstlen, NULL);
  369. if (e == NULL) {
  370. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  371. goto done;
  372. }
  373. s = sm2_sig_gen(eckey, e);
  374. if (s == NULL) {
  375. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  376. goto done;
  377. }
  378. sigleni = i2d_ECDSA_SIG(s, &sig);
  379. if (sigleni < 0) {
  380. ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
  381. goto done;
  382. }
  383. *siglen = (unsigned int)sigleni;
  384. ret = 1;
  385. done:
  386. ECDSA_SIG_free(s);
  387. BN_free(e);
  388. return ret;
  389. }
  390. int sm2_internal_verify(const unsigned char *dgst, int dgstlen,
  391. const unsigned char *sig, int sig_len, EC_KEY *eckey)
  392. {
  393. ECDSA_SIG *s = NULL;
  394. BIGNUM *e = NULL;
  395. const unsigned char *p = sig;
  396. unsigned char *der = NULL;
  397. int derlen = -1;
  398. int ret = -1;
  399. s = ECDSA_SIG_new();
  400. if (s == NULL) {
  401. ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
  402. goto done;
  403. }
  404. if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
  405. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  406. goto done;
  407. }
  408. /* Ensure signature uses DER and doesn't have trailing garbage */
  409. derlen = i2d_ECDSA_SIG(s, &der);
  410. if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
  411. ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
  412. goto done;
  413. }
  414. e = BN_bin2bn(dgst, dgstlen, NULL);
  415. if (e == NULL) {
  416. ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
  417. goto done;
  418. }
  419. ret = sm2_sig_verify(eckey, s, e);
  420. done:
  421. OPENSSL_free(der);
  422. BN_free(e);
  423. ECDSA_SIG_free(s);
  424. return ret;
  425. }