2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/ec.h>
13 #include <openssl/evp.h>
14 #include "internal/evp_int.h"
15 #include "internal/sm2.h"
16 #include "internal/sm2err.h"
18 /* EC pkey context structure */
21 /* Key and paramgen group */
27 static int pkey_sm2_init(EVP_PKEY_CTX *ctx)
31 if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL) {
32 SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE);
40 static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx)
42 SM2_PKEY_CTX *dctx = ctx->data;
45 EC_GROUP_free(dctx->gen_group);
51 static int pkey_sm2_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
53 SM2_PKEY_CTX *dctx, *sctx;
55 if (!pkey_sm2_init(dst))
59 if (sctx->gen_group != NULL) {
60 dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
61 if (dctx->gen_group == NULL) {
62 pkey_sm2_cleanup(dst);
71 static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
72 const unsigned char *tbs, size_t tbslen)
76 EC_KEY *ec = ctx->pkey->pkey.ec;
77 const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec);
84 *siglen = (size_t)sig_sz;
88 if (*siglen < (size_t)sig_sz) {
89 SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL);
93 ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
97 *siglen = (size_t)sltmp;
101 static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
102 const unsigned char *sig, size_t siglen,
103 const unsigned char *tbs, size_t tbslen)
105 EC_KEY *ec = ctx->pkey->pkey.ec;
107 return sm2_verify(tbs, tbslen, sig, siglen, ec);
110 static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
111 unsigned char *out, size_t *outlen,
112 const unsigned char *in, size_t inlen)
114 EC_KEY *ec = ctx->pkey->pkey.ec;
115 SM2_PKEY_CTX *dctx = ctx->data;
116 const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
119 if (!sm2_ciphertext_size(ec, md, inlen, outlen))
125 return sm2_encrypt(ec, md, in, inlen, out, outlen);
128 static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx,
129 unsigned char *out, size_t *outlen,
130 const unsigned char *in, size_t inlen)
132 EC_KEY *ec = ctx->pkey->pkey.ec;
133 SM2_PKEY_CTX *dctx = ctx->data;
134 const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
137 if (!sm2_plaintext_size(ec, md, inlen, outlen))
143 return sm2_decrypt(ec, md, in, inlen, out, outlen);
146 static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
148 SM2_PKEY_CTX *dctx = ctx->data;
152 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
153 group = EC_GROUP_new_by_curve_name(p1);
155 SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
158 EC_GROUP_free(dctx->gen_group);
159 dctx->gen_group = group;
162 case EVP_PKEY_CTRL_EC_PARAM_ENC:
163 if (dctx->gen_group == NULL) {
164 SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET);
167 EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
170 case EVP_PKEY_CTRL_MD:
174 case EVP_PKEY_CTRL_GET_MD:
175 *(const EVP_MD **)p2 = dctx->md;
184 static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
185 const char *type, const char *value)
187 if (strcmp(type, "ec_paramgen_curve") == 0) {
190 if (((nid = EC_curve_nist2nid(value)) == NID_undef)
191 && ((nid = OBJ_sn2nid(value)) == NID_undef)
192 && ((nid = OBJ_ln2nid(value)) == NID_undef)) {
193 SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE);
196 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
197 } else if (strcmp(type, "ec_param_enc") == 0) {
200 if (strcmp(value, "explicit") == 0)
202 else if (strcmp(value, "named_curve") == 0)
203 param_enc = OPENSSL_EC_NAMED_CURVE;
206 return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
212 const EVP_PKEY_METHOD sm2_pkey_meth = {