sm2_pmeth.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2006-2018 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 "internal/evp_int.h"
  14. #include "internal/sm2.h"
  15. #include "internal/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. } SM2_PKEY_CTX;
  23. static int pkey_sm2_init(EVP_PKEY_CTX *ctx)
  24. {
  25. SM2_PKEY_CTX *dctx;
  26. if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL) {
  27. SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE);
  28. return 0;
  29. }
  30. ctx->data = dctx;
  31. return 1;
  32. }
  33. static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx)
  34. {
  35. SM2_PKEY_CTX *dctx = ctx->data;
  36. if (dctx != NULL) {
  37. EC_GROUP_free(dctx->gen_group);
  38. OPENSSL_free(dctx);
  39. ctx->data = NULL;
  40. }
  41. }
  42. static int pkey_sm2_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  43. {
  44. SM2_PKEY_CTX *dctx, *sctx;
  45. if (!pkey_sm2_init(dst))
  46. return 0;
  47. sctx = src->data;
  48. dctx = dst->data;
  49. if (sctx->gen_group != NULL) {
  50. dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
  51. if (dctx->gen_group == NULL) {
  52. pkey_sm2_cleanup(dst);
  53. return 0;
  54. }
  55. }
  56. dctx->md = sctx->md;
  57. return 1;
  58. }
  59. static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  60. const unsigned char *tbs, size_t tbslen)
  61. {
  62. int ret;
  63. unsigned int sltmp;
  64. EC_KEY *ec = ctx->pkey->pkey.ec;
  65. const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec);
  66. if (sig_sz <= 0) {
  67. return 0;
  68. }
  69. if (sig == NULL) {
  70. *siglen = (size_t)sig_sz;
  71. return 1;
  72. }
  73. if (*siglen < (size_t)sig_sz) {
  74. SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL);
  75. return 0;
  76. }
  77. ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
  78. if (ret <= 0)
  79. return ret;
  80. *siglen = (size_t)sltmp;
  81. return 1;
  82. }
  83. static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
  84. const unsigned char *sig, size_t siglen,
  85. const unsigned char *tbs, size_t tbslen)
  86. {
  87. EC_KEY *ec = ctx->pkey->pkey.ec;
  88. return sm2_verify(tbs, tbslen, sig, siglen, ec);
  89. }
  90. static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
  91. unsigned char *out, size_t *outlen,
  92. const unsigned char *in, size_t inlen)
  93. {
  94. EC_KEY *ec = ctx->pkey->pkey.ec;
  95. SM2_PKEY_CTX *dctx = ctx->data;
  96. const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
  97. if (out == NULL) {
  98. if (!sm2_ciphertext_size(ec, md, inlen, outlen))
  99. return -1;
  100. else
  101. return 1;
  102. }
  103. return sm2_encrypt(ec, md, in, inlen, out, outlen);
  104. }
  105. static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx,
  106. unsigned char *out, size_t *outlen,
  107. const unsigned char *in, size_t inlen)
  108. {
  109. EC_KEY *ec = ctx->pkey->pkey.ec;
  110. SM2_PKEY_CTX *dctx = ctx->data;
  111. const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
  112. if (out == NULL) {
  113. if (!sm2_plaintext_size(ec, md, inlen, outlen))
  114. return -1;
  115. else
  116. return 1;
  117. }
  118. return sm2_decrypt(ec, md, in, inlen, out, outlen);
  119. }
  120. static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  121. {
  122. SM2_PKEY_CTX *dctx = ctx->data;
  123. EC_GROUP *group;
  124. switch (type) {
  125. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  126. group = EC_GROUP_new_by_curve_name(p1);
  127. if (group == NULL) {
  128. SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
  129. return 0;
  130. }
  131. EC_GROUP_free(dctx->gen_group);
  132. dctx->gen_group = group;
  133. return 1;
  134. case EVP_PKEY_CTRL_EC_PARAM_ENC:
  135. if (dctx->gen_group == NULL) {
  136. SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET);
  137. return 0;
  138. }
  139. EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
  140. return 1;
  141. case EVP_PKEY_CTRL_MD:
  142. dctx->md = p2;
  143. return 1;
  144. case EVP_PKEY_CTRL_GET_MD:
  145. *(const EVP_MD **)p2 = dctx->md;
  146. return 1;
  147. default:
  148. return -2;
  149. }
  150. }
  151. static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
  152. const char *type, const char *value)
  153. {
  154. if (strcmp(type, "ec_paramgen_curve") == 0) {
  155. int nid = NID_undef;
  156. if (((nid = EC_curve_nist2nid(value)) == NID_undef)
  157. && ((nid = OBJ_sn2nid(value)) == NID_undef)
  158. && ((nid = OBJ_ln2nid(value)) == NID_undef)) {
  159. SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE);
  160. return 0;
  161. }
  162. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  163. } else if (strcmp(type, "ec_param_enc") == 0) {
  164. int param_enc;
  165. if (strcmp(value, "explicit") == 0)
  166. param_enc = 0;
  167. else if (strcmp(value, "named_curve") == 0)
  168. param_enc = OPENSSL_EC_NAMED_CURVE;
  169. else
  170. return -2;
  171. return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
  172. }
  173. return -2;
  174. }
  175. const EVP_PKEY_METHOD sm2_pkey_meth = {
  176. EVP_PKEY_SM2,
  177. 0,
  178. pkey_sm2_init,
  179. pkey_sm2_copy,
  180. pkey_sm2_cleanup,
  181. 0,
  182. 0,
  183. 0,
  184. 0,
  185. 0,
  186. pkey_sm2_sign,
  187. 0,
  188. pkey_sm2_verify,
  189. 0, 0,
  190. 0, 0, 0, 0,
  191. 0,
  192. pkey_sm2_encrypt,
  193. 0,
  194. pkey_sm2_decrypt,
  195. 0,
  196. 0,
  197. pkey_sm2_ctrl,
  198. pkey_sm2_ctrl_str
  199. };