sm2_pmeth.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright 2006-2020 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. * ECDSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "internal/cryptlib.h"
  15. #include <openssl/asn1t.h>
  16. #include <openssl/ec.h>
  17. #include <openssl/evp.h>
  18. #include "crypto/evp.h"
  19. #include "crypto/sm2.h"
  20. #include "crypto/sm2err.h"
  21. /* EC pkey context structure */
  22. typedef struct {
  23. /* message digest */
  24. const EVP_MD *md;
  25. /* Distinguishing Identifier, ISO/IEC 15946-3, FIPS 196 */
  26. uint8_t *id;
  27. size_t id_len;
  28. /* id_set indicates if the 'id' field is set (1) or not (0) */
  29. int id_set;
  30. } SM2_PKEY_CTX;
  31. static int pkey_sm2_init(EVP_PKEY_CTX *ctx)
  32. {
  33. SM2_PKEY_CTX *smctx;
  34. if ((smctx = OPENSSL_zalloc(sizeof(*smctx))) == NULL) {
  35. SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE);
  36. return 0;
  37. }
  38. ctx->data = smctx;
  39. return 1;
  40. }
  41. static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx)
  42. {
  43. SM2_PKEY_CTX *smctx = ctx->data;
  44. if (smctx != NULL) {
  45. OPENSSL_free(smctx->id);
  46. OPENSSL_free(smctx);
  47. ctx->data = NULL;
  48. }
  49. }
  50. static int pkey_sm2_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
  51. {
  52. SM2_PKEY_CTX *dctx, *sctx;
  53. if (!pkey_sm2_init(dst))
  54. return 0;
  55. sctx = src->data;
  56. dctx = dst->data;
  57. if (sctx->id != NULL) {
  58. dctx->id = OPENSSL_malloc(sctx->id_len);
  59. if (dctx->id == NULL) {
  60. SM2err(SM2_F_PKEY_SM2_COPY, ERR_R_MALLOC_FAILURE);
  61. pkey_sm2_cleanup(dst);
  62. return 0;
  63. }
  64. memcpy(dctx->id, sctx->id, sctx->id_len);
  65. }
  66. dctx->id_len = sctx->id_len;
  67. dctx->id_set = sctx->id_set;
  68. dctx->md = sctx->md;
  69. return 1;
  70. }
  71. static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  72. const unsigned char *tbs, size_t tbslen)
  73. {
  74. int ret;
  75. unsigned int sltmp;
  76. EC_KEY *ec = ctx->pkey->pkey.ec;
  77. const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec);
  78. if (sig_sz <= 0) {
  79. return 0;
  80. }
  81. if (sig == NULL) {
  82. *siglen = (size_t)sig_sz;
  83. return 1;
  84. }
  85. if (*siglen < (size_t)sig_sz) {
  86. SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL);
  87. return 0;
  88. }
  89. ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
  90. if (ret <= 0)
  91. return ret;
  92. *siglen = (size_t)sltmp;
  93. return 1;
  94. }
  95. static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
  96. const unsigned char *sig, size_t siglen,
  97. const unsigned char *tbs, size_t tbslen)
  98. {
  99. EC_KEY *ec = ctx->pkey->pkey.ec;
  100. return sm2_verify(tbs, tbslen, sig, siglen, ec);
  101. }
  102. static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
  103. unsigned char *out, size_t *outlen,
  104. const unsigned char *in, size_t inlen)
  105. {
  106. EC_KEY *ec = ctx->pkey->pkey.ec;
  107. SM2_PKEY_CTX *dctx = ctx->data;
  108. const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
  109. if (out == NULL) {
  110. if (!sm2_ciphertext_size(ec, md, inlen, outlen))
  111. return -1;
  112. else
  113. return 1;
  114. }
  115. return sm2_encrypt(ec, md, in, inlen, out, outlen);
  116. }
  117. static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx,
  118. unsigned char *out, size_t *outlen,
  119. const unsigned char *in, size_t inlen)
  120. {
  121. EC_KEY *ec = ctx->pkey->pkey.ec;
  122. SM2_PKEY_CTX *dctx = ctx->data;
  123. const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
  124. if (out == NULL) {
  125. if (!sm2_plaintext_size(ec, md, inlen, outlen))
  126. return -1;
  127. else
  128. return 1;
  129. }
  130. return sm2_decrypt(ec, md, in, inlen, out, outlen);
  131. }
  132. static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  133. {
  134. SM2_PKEY_CTX *smctx = ctx->data;
  135. uint8_t *tmp_id;
  136. switch (type) {
  137. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  138. /*
  139. * This control could be removed, which would signal it being
  140. * unsupported. However, that means that when the caller uses
  141. * the correct curve, it may interpret the unsupported signal
  142. * as an error, so it's better to accept the control, check the
  143. * value and return a corresponding value.
  144. */
  145. if (p1 != NID_sm2) {
  146. SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
  147. return 0;
  148. }
  149. return 1;
  150. case EVP_PKEY_CTRL_MD:
  151. smctx->md = p2;
  152. return 1;
  153. case EVP_PKEY_CTRL_GET_MD:
  154. *(const EVP_MD **)p2 = smctx->md;
  155. return 1;
  156. case EVP_PKEY_CTRL_SET1_ID:
  157. if (p1 > 0) {
  158. tmp_id = OPENSSL_malloc(p1);
  159. if (tmp_id == NULL) {
  160. SM2err(SM2_F_PKEY_SM2_CTRL, ERR_R_MALLOC_FAILURE);
  161. return 0;
  162. }
  163. memcpy(tmp_id, p2, p1);
  164. OPENSSL_free(smctx->id);
  165. smctx->id = tmp_id;
  166. } else {
  167. /* set null-ID */
  168. OPENSSL_free(smctx->id);
  169. smctx->id = NULL;
  170. }
  171. smctx->id_len = (size_t)p1;
  172. smctx->id_set = 1;
  173. return 1;
  174. case EVP_PKEY_CTRL_GET1_ID:
  175. memcpy(p2, smctx->id, smctx->id_len);
  176. return 1;
  177. case EVP_PKEY_CTRL_GET1_ID_LEN:
  178. *(size_t *)p2 = smctx->id_len;
  179. return 1;
  180. case EVP_PKEY_CTRL_DIGESTINIT:
  181. /* nothing to be inited, this is to suppress the error... */
  182. return 1;
  183. default:
  184. return -2;
  185. }
  186. }
  187. static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
  188. const char *type, const char *value)
  189. {
  190. uint8_t *hex_id;
  191. long hex_len = 0;
  192. int ret = 0;
  193. if (strcmp(type, "ec_paramgen_curve") == 0) {
  194. int nid = NID_undef;
  195. if (((nid = EC_curve_nist2nid(value)) == NID_undef)
  196. && ((nid = OBJ_sn2nid(value)) == NID_undef)
  197. && ((nid = OBJ_ln2nid(value)) == NID_undef)) {
  198. SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE);
  199. return 0;
  200. }
  201. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  202. } else if (strcmp(type, "ec_param_enc") == 0) {
  203. int param_enc;
  204. if (strcmp(value, "explicit") == 0)
  205. param_enc = 0;
  206. else if (strcmp(value, "named_curve") == 0)
  207. param_enc = OPENSSL_EC_NAMED_CURVE;
  208. else
  209. return -2;
  210. return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
  211. } else if (strcmp(type, "distid") == 0) {
  212. return pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID,
  213. (int)strlen(value), (void *)value);
  214. } else if (strcmp(type, "hexdistid") == 0) {
  215. hex_id = OPENSSL_hexstr2buf((const char *)value, &hex_len);
  216. if (hex_id == NULL) {
  217. SM2err(SM2_F_PKEY_SM2_CTRL_STR, ERR_R_PASSED_INVALID_ARGUMENT);
  218. return 0;
  219. }
  220. ret = pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID, (int)hex_len,
  221. (void *)hex_id);
  222. OPENSSL_free(hex_id);
  223. return ret;
  224. }
  225. return -2;
  226. }
  227. static int pkey_sm2_digest_custom(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  228. {
  229. uint8_t z[EVP_MAX_MD_SIZE];
  230. SM2_PKEY_CTX *smctx = ctx->data;
  231. EC_KEY *ec = ctx->pkey->pkey.ec;
  232. const EVP_MD *md = EVP_MD_CTX_md(mctx);
  233. int mdlen = EVP_MD_size(md);
  234. if (!smctx->id_set) {
  235. /*
  236. * An ID value must be set. The specifications are not clear whether a
  237. * NULL is allowed. We only allow it if set explicitly for maximum
  238. * flexibility.
  239. */
  240. SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_ID_NOT_SET);
  241. return 0;
  242. }
  243. if (mdlen < 0) {
  244. SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_INVALID_DIGEST);
  245. return 0;
  246. }
  247. /* get hashed prefix 'z' of tbs message */
  248. if (!sm2_compute_z_digest(z, md, smctx->id, smctx->id_len, ec))
  249. return 0;
  250. return EVP_DigestUpdate(mctx, z, (size_t)mdlen);
  251. }
  252. static int pkey_sm2_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  253. {
  254. EC_KEY *ec = NULL;
  255. int ret;
  256. ec = EC_KEY_new_by_curve_name(NID_sm2);
  257. if (ec == NULL)
  258. return 0;
  259. if (!ossl_assert(ret = EVP_PKEY_assign_EC_KEY(pkey, ec)))
  260. EC_KEY_free(ec);
  261. return ret;
  262. }
  263. static int pkey_sm2_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  264. {
  265. EC_KEY *ec = NULL;
  266. ec = EC_KEY_new_by_curve_name(NID_sm2);
  267. if (ec == NULL)
  268. return 0;
  269. if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey, ec))) {
  270. EC_KEY_free(ec);
  271. return 0;
  272. }
  273. /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
  274. if (ctx->pkey != NULL
  275. && !EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  276. return 0;
  277. return EC_KEY_generate_key(ec);
  278. }
  279. static const EVP_PKEY_METHOD sm2_pkey_meth = {
  280. EVP_PKEY_SM2,
  281. 0,
  282. pkey_sm2_init,
  283. pkey_sm2_copy,
  284. pkey_sm2_cleanup,
  285. 0,
  286. pkey_sm2_paramgen,
  287. 0,
  288. pkey_sm2_keygen,
  289. 0,
  290. pkey_sm2_sign,
  291. 0,
  292. pkey_sm2_verify,
  293. 0, 0,
  294. 0, 0, 0, 0,
  295. 0,
  296. pkey_sm2_encrypt,
  297. 0,
  298. pkey_sm2_decrypt,
  299. 0,
  300. 0,
  301. pkey_sm2_ctrl,
  302. pkey_sm2_ctrl_str,
  303. 0, 0,
  304. 0, 0, 0,
  305. pkey_sm2_digest_custom
  306. };
  307. const EVP_PKEY_METHOD *sm2_pkey_method(void)
  308. {
  309. return &sm2_pkey_meth;
  310. }