2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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 */
25 /* Distinguishing Identifier, ISO/IEC 15946-3 */
28 /* id_set indicates if the 'id' field is set (1) or not (0) */
32 static int pkey_sm2_init(EVP_PKEY_CTX
*ctx
)
36 if ((smctx
= OPENSSL_zalloc(sizeof(*smctx
))) == NULL
) {
37 SM2err(SM2_F_PKEY_SM2_INIT
, ERR_R_MALLOC_FAILURE
);
45 static void pkey_sm2_cleanup(EVP_PKEY_CTX
*ctx
)
47 SM2_PKEY_CTX
*smctx
= ctx
->data
;
50 EC_GROUP_free(smctx
->gen_group
);
51 OPENSSL_free(smctx
->id
);
57 static int pkey_sm2_copy(EVP_PKEY_CTX
*dst
, const EVP_PKEY_CTX
*src
)
59 SM2_PKEY_CTX
*dctx
, *sctx
;
61 if (!pkey_sm2_init(dst
))
65 if (sctx
->gen_group
!= NULL
) {
66 dctx
->gen_group
= EC_GROUP_dup(sctx
->gen_group
);
67 if (dctx
->gen_group
== NULL
) {
68 pkey_sm2_cleanup(dst
);
72 if (sctx
->id
!= NULL
) {
73 dctx
->id
= OPENSSL_malloc(sctx
->id_len
);
74 if (dctx
->id
== NULL
) {
75 SM2err(SM2_F_PKEY_SM2_COPY
, ERR_R_MALLOC_FAILURE
);
76 pkey_sm2_cleanup(dst
);
79 memcpy(dctx
->id
, sctx
->id
, sctx
->id_len
);
81 dctx
->id_len
= sctx
->id_len
;
82 dctx
->id_set
= sctx
->id_set
;
88 static int pkey_sm2_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
89 const unsigned char *tbs
, size_t tbslen
)
93 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
94 const int sig_sz
= ECDSA_size(ctx
->pkey
->pkey
.ec
);
101 *siglen
= (size_t)sig_sz
;
105 if (*siglen
< (size_t)sig_sz
) {
106 SM2err(SM2_F_PKEY_SM2_SIGN
, SM2_R_BUFFER_TOO_SMALL
);
110 ret
= sm2_sign(tbs
, tbslen
, sig
, &sltmp
, ec
);
114 *siglen
= (size_t)sltmp
;
118 static int pkey_sm2_verify(EVP_PKEY_CTX
*ctx
,
119 const unsigned char *sig
, size_t siglen
,
120 const unsigned char *tbs
, size_t tbslen
)
122 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
124 return sm2_verify(tbs
, tbslen
, sig
, siglen
, ec
);
127 static int pkey_sm2_encrypt(EVP_PKEY_CTX
*ctx
,
128 unsigned char *out
, size_t *outlen
,
129 const unsigned char *in
, size_t inlen
)
131 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
132 SM2_PKEY_CTX
*dctx
= ctx
->data
;
133 const EVP_MD
*md
= (dctx
->md
== NULL
) ? EVP_sm3() : dctx
->md
;
136 if (!sm2_ciphertext_size(ec
, md
, inlen
, outlen
))
142 return sm2_encrypt(ec
, md
, in
, inlen
, out
, outlen
);
145 static int pkey_sm2_decrypt(EVP_PKEY_CTX
*ctx
,
146 unsigned char *out
, size_t *outlen
,
147 const unsigned char *in
, size_t inlen
)
149 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
150 SM2_PKEY_CTX
*dctx
= ctx
->data
;
151 const EVP_MD
*md
= (dctx
->md
== NULL
) ? EVP_sm3() : dctx
->md
;
154 if (!sm2_plaintext_size(ec
, md
, inlen
, outlen
))
160 return sm2_decrypt(ec
, md
, in
, inlen
, out
, outlen
);
163 static int pkey_sm2_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
165 SM2_PKEY_CTX
*smctx
= ctx
->data
;
170 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
:
171 group
= EC_GROUP_new_by_curve_name(p1
);
173 SM2err(SM2_F_PKEY_SM2_CTRL
, SM2_R_INVALID_CURVE
);
176 EC_GROUP_free(smctx
->gen_group
);
177 smctx
->gen_group
= group
;
180 case EVP_PKEY_CTRL_EC_PARAM_ENC
:
181 if (smctx
->gen_group
== NULL
) {
182 SM2err(SM2_F_PKEY_SM2_CTRL
, SM2_R_NO_PARAMETERS_SET
);
185 EC_GROUP_set_asn1_flag(smctx
->gen_group
, p1
);
188 case EVP_PKEY_CTRL_MD
:
192 case EVP_PKEY_CTRL_GET_MD
:
193 *(const EVP_MD
**)p2
= smctx
->md
;
196 case EVP_PKEY_CTRL_SET1_ID
:
198 tmp_id
= OPENSSL_malloc(p1
);
199 if (tmp_id
== NULL
) {
200 SM2err(SM2_F_PKEY_SM2_CTRL
, ERR_R_MALLOC_FAILURE
);
203 memcpy(tmp_id
, p2
, p1
);
204 OPENSSL_free(smctx
->id
);
208 OPENSSL_free(smctx
->id
);
211 smctx
->id_len
= (size_t)p1
;
215 case EVP_PKEY_CTRL_GET1_ID
:
216 memcpy(p2
, smctx
->id
, smctx
->id_len
);
219 case EVP_PKEY_CTRL_GET1_ID_LEN
:
220 *(size_t *)p2
= smctx
->id_len
;
228 static int pkey_sm2_ctrl_str(EVP_PKEY_CTX
*ctx
,
229 const char *type
, const char *value
)
231 if (strcmp(type
, "ec_paramgen_curve") == 0) {
234 if (((nid
= EC_curve_nist2nid(value
)) == NID_undef
)
235 && ((nid
= OBJ_sn2nid(value
)) == NID_undef
)
236 && ((nid
= OBJ_ln2nid(value
)) == NID_undef
)) {
237 SM2err(SM2_F_PKEY_SM2_CTRL_STR
, SM2_R_INVALID_CURVE
);
240 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx
, nid
);
241 } else if (strcmp(type
, "ec_param_enc") == 0) {
244 if (strcmp(value
, "explicit") == 0)
246 else if (strcmp(value
, "named_curve") == 0)
247 param_enc
= OPENSSL_EC_NAMED_CURVE
;
250 return EVP_PKEY_CTX_set_ec_param_enc(ctx
, param_enc
);
251 } else if (strcmp(type
, "sm2_id") == 0) {
252 return pkey_sm2_ctrl(ctx
, EVP_PKEY_CTRL_SET1_ID
,
253 (int)strlen(value
), (void *)value
);
259 static int pkey_sm2_digest_custom(EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
)
261 uint8_t z
[EVP_MAX_MD_SIZE
];
262 SM2_PKEY_CTX
*smctx
= ctx
->data
;
263 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
264 const EVP_MD
*md
= EVP_MD_CTX_md(mctx
);
265 int mdlen
= EVP_MD_size(md
);
267 if (!smctx
->id_set
) {
269 * An ID value must be set. The specifications are not clear whether a
270 * NULL is allowed. We only allow it if set explicitly for maximum
273 SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM
, SM2_R_ID_NOT_SET
);
278 SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM
, SM2_R_INVALID_DIGEST
);
282 /* get hashed prefix 'z' of tbs message */
283 if (!sm2_compute_z_digest(z
, md
, smctx
->id
, smctx
->id_len
, ec
))
286 return EVP_DigestUpdate(mctx
, z
, (size_t)mdlen
);
289 const EVP_PKEY_METHOD sm2_pkey_meth
= {
327 pkey_sm2_digest_custom