sm2_pmeth.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include "internal/cryptlib.h"
  10. #include <openssl/asn1t.h>
  11. #include <openssl/ec.h>
  12. #include <openssl/evp.h>
  13. #include "crypto/evp.h"
  14. #include "crypto/sm2.h"
  15. #include "crypto/sm2err.h"
  16. /* EC pkey context structure */
  17. typedef struct {
  18. /* Key and paramgen group */
  19. EC_GROUP *gen_group;
  20. /* message digest */
  21. const EVP_MD *md;
  22. /* Distinguishing Identifier, ISO/IEC 15946-3 */
  23. uint8_t *id;
  24. size_t id_len;
  25. /* id_set indicates if the 'id' field is set (1) or not (0) */
  26. int id_set;
  27. } SM2_PKEY_CTX;
  28. static int pkey_sm2_init(EVP_PKEY_CTX *ctx)
  29. {
  30. SM2_PKEY_CTX *smctx;
  31. if ((smctx = OPENSSL_zalloc(sizeof(*smctx))) == NULL) {
  32. SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE);
  33. return 0;
  34. }
  35. ctx->data = smctx;
  36. return 1;
  37. }
  38. static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx)
  39. {
  40. SM2_PKEY_CTX *smctx = ctx->data;
  41. if (smctx != NULL) {
  42. EC_GROUP_free(smctx->gen_group);
  43. OPENSSL_free(smctx->id);
  44. OPENSSL_free(smctx);
  45. ctx->data = NULL;
  46. }
  47. }
  48. static int pkey_sm2_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  49. {
  50. SM2_PKEY_CTX *dctx, *sctx;
  51. if (!pkey_sm2_init(dst))
  52. return 0;
  53. sctx = src->data;
  54. dctx = dst->data;
  55. if (sctx->gen_group != NULL) {
  56. dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
  57. if (dctx->gen_group == NULL) {
  58. pkey_sm2_cleanup(dst);
  59. return 0;
  60. }
  61. }
  62. if (sctx->id != NULL) {
  63. dctx->id = OPENSSL_malloc(sctx->id_len);
  64. if (dctx->id == NULL) {
  65. SM2err(SM2_F_PKEY_SM2_COPY, ERR_R_MALLOC_FAILURE);
  66. pkey_sm2_cleanup(dst);
  67. return 0;
  68. }
  69. memcpy(dctx->id, sctx->id, sctx->id_len);
  70. }
  71. dctx->id_len = sctx->id_len;
  72. dctx->id_set = sctx->id_set;
  73. dctx->md = sctx->md;
  74. return 1;
  75. }
  76. static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  77. const unsigned char *tbs, size_t tbslen)
  78. {
  79. int ret;
  80. unsigned int sltmp;
  81. EC_KEY *ec = ctx->pkey->pkey.ec;
  82. const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec);
  83. if (sig_sz <= 0) {
  84. return 0;
  85. }
  86. if (sig == NULL) {
  87. *siglen = (size_t)sig_sz;
  88. return 1;
  89. }
  90. if (*siglen < (size_t)sig_sz) {
  91. SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL);
  92. return 0;
  93. }
  94. ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
  95. if (ret <= 0)
  96. return ret;
  97. *siglen = (size_t)sltmp;
  98. return 1;
  99. }
  100. static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
  101. const unsigned char *sig, size_t siglen,
  102. const unsigned char *tbs, size_t tbslen)
  103. {
  104. EC_KEY *ec = ctx->pkey->pkey.ec;
  105. return sm2_verify(tbs, tbslen, sig, siglen, ec);
  106. }
  107. static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
  108. unsigned char *out, size_t *outlen,
  109. const unsigned char *in, size_t inlen)
  110. {
  111. EC_KEY *ec = ctx->pkey->pkey.ec;
  112. SM2_PKEY_CTX *dctx = ctx->data;
  113. const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
  114. if (out == NULL) {
  115. if (!sm2_ciphertext_size(ec, md, inlen, outlen))
  116. return -1;
  117. else
  118. return 1;
  119. }
  120. return sm2_encrypt(ec, md, in, inlen, out, outlen);
  121. }
  122. static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx,
  123. unsigned char *out, size_t *outlen,
  124. const unsigned char *in, size_t inlen)
  125. {
  126. EC_KEY *ec = ctx->pkey->pkey.ec;
  127. SM2_PKEY_CTX *dctx = ctx->data;
  128. const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
  129. if (out == NULL) {
  130. if (!sm2_plaintext_size(in, inlen, outlen))
  131. return -1;
  132. else
  133. return 1;
  134. }
  135. return sm2_decrypt(ec, md, in, inlen, out, outlen);
  136. }
  137. static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  138. {
  139. SM2_PKEY_CTX *smctx = ctx->data;
  140. EC_GROUP *group;
  141. uint8_t *tmp_id;
  142. switch (type) {
  143. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  144. group = EC_GROUP_new_by_curve_name(p1);
  145. if (group == NULL) {
  146. SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
  147. return 0;
  148. }
  149. EC_GROUP_free(smctx->gen_group);
  150. smctx->gen_group = group;
  151. return 1;
  152. case EVP_PKEY_CTRL_EC_PARAM_ENC:
  153. if (smctx->gen_group == NULL) {
  154. SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET);
  155. return 0;
  156. }
  157. EC_GROUP_set_asn1_flag(smctx->gen_group, p1);
  158. return 1;
  159. case EVP_PKEY_CTRL_MD:
  160. smctx->md = p2;
  161. return 1;
  162. case EVP_PKEY_CTRL_GET_MD:
  163. *(const EVP_MD **)p2 = smctx->md;
  164. return 1;
  165. case EVP_PKEY_CTRL_SET1_ID:
  166. if (p1 > 0) {
  167. tmp_id = OPENSSL_malloc(p1);
  168. if (tmp_id == NULL) {
  169. SM2err(SM2_F_PKEY_SM2_CTRL, ERR_R_MALLOC_FAILURE);
  170. return 0;
  171. }
  172. memcpy(tmp_id, p2, p1);
  173. OPENSSL_free(smctx->id);
  174. smctx->id = tmp_id;
  175. } else {
  176. /* set null-ID */
  177. OPENSSL_free(smctx->id);
  178. smctx->id = NULL;
  179. }
  180. smctx->id_len = (size_t)p1;
  181. smctx->id_set = 1;
  182. return 1;
  183. case EVP_PKEY_CTRL_GET1_ID:
  184. memcpy(p2, smctx->id, smctx->id_len);
  185. return 1;
  186. case EVP_PKEY_CTRL_GET1_ID_LEN:
  187. *(size_t *)p2 = smctx->id_len;
  188. return 1;
  189. case EVP_PKEY_CTRL_DIGESTINIT:
  190. /* nothing to be inited, this is to suppress the error... */
  191. return 1;
  192. default:
  193. return -2;
  194. }
  195. }
  196. static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
  197. const char *type, const char *value)
  198. {
  199. if (strcmp(type, "ec_paramgen_curve") == 0) {
  200. int nid = NID_undef;
  201. if (((nid = EC_curve_nist2nid(value)) == NID_undef)
  202. && ((nid = OBJ_sn2nid(value)) == NID_undef)
  203. && ((nid = OBJ_ln2nid(value)) == NID_undef)) {
  204. SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE);
  205. return 0;
  206. }
  207. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  208. } else if (strcmp(type, "ec_param_enc") == 0) {
  209. int param_enc;
  210. if (strcmp(value, "explicit") == 0)
  211. param_enc = 0;
  212. else if (strcmp(value, "named_curve") == 0)
  213. param_enc = OPENSSL_EC_NAMED_CURVE;
  214. else
  215. return -2;
  216. return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
  217. }
  218. return -2;
  219. }
  220. static int pkey_sm2_digest_custom(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  221. {
  222. uint8_t z[EVP_MAX_MD_SIZE];
  223. SM2_PKEY_CTX *smctx = ctx->data;
  224. EC_KEY *ec = ctx->pkey->pkey.ec;
  225. const EVP_MD *md = EVP_MD_CTX_md(mctx);
  226. int mdlen = EVP_MD_size(md);
  227. if (!smctx->id_set) {
  228. /*
  229. * An ID value must be set. The specifications are not clear whether a
  230. * NULL is allowed. We only allow it if set explicitly for maximum
  231. * flexibility.
  232. */
  233. SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_ID_NOT_SET);
  234. return 0;
  235. }
  236. if (mdlen < 0) {
  237. SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_INVALID_DIGEST);
  238. return 0;
  239. }
  240. /* get hashed prefix 'z' of tbs message */
  241. if (!sm2_compute_z_digest(z, md, smctx->id, smctx->id_len, ec))
  242. return 0;
  243. return EVP_DigestUpdate(mctx, z, (size_t)mdlen);
  244. }
  245. const EVP_PKEY_METHOD sm2_pkey_meth = {
  246. EVP_PKEY_SM2,
  247. 0,
  248. pkey_sm2_init,
  249. pkey_sm2_copy,
  250. pkey_sm2_cleanup,
  251. 0,
  252. 0,
  253. 0,
  254. 0,
  255. 0,
  256. pkey_sm2_sign,
  257. 0,
  258. pkey_sm2_verify,
  259. 0, 0,
  260. 0, 0, 0, 0,
  261. 0,
  262. pkey_sm2_encrypt,
  263. 0,
  264. pkey_sm2_decrypt,
  265. 0,
  266. 0,
  267. pkey_sm2_ctrl,
  268. pkey_sm2_ctrl_str,
  269. 0, 0,
  270. 0, 0, 0,
  271. pkey_sm2_digest_custom
  272. };