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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/ec.h>
16 #include <openssl/evp.h>
17 #include "internal/evp_int.h"
19 #if !defined(OPENSSL_NO_SM2)
20 # include <openssl/sm2.h>
23 /* EC pkey context structure */
26 /* Key and paramgen group */
30 /* Duplicate key if custom cofactor needed */
33 signed char cofactor_mode
;
34 /* KDF (if any) to use for ECDH */
36 /* Message digest to use for key derivation */
38 /* User key material */
39 unsigned char *kdf_ukm
;
41 /* KDF output length */
45 static int pkey_ec_init(EVP_PKEY_CTX
*ctx
)
49 if ((dctx
= OPENSSL_zalloc(sizeof(*dctx
))) == NULL
) {
50 ECerr(EC_F_PKEY_EC_INIT
, ERR_R_MALLOC_FAILURE
);
54 dctx
->cofactor_mode
= -1;
55 dctx
->kdf_type
= EVP_PKEY_ECDH_KDF_NONE
;
60 static int pkey_ec_copy(EVP_PKEY_CTX
*dst
, EVP_PKEY_CTX
*src
)
62 EC_PKEY_CTX
*dctx
, *sctx
;
63 if (!pkey_ec_init(dst
))
67 if (sctx
->gen_group
) {
68 dctx
->gen_group
= EC_GROUP_dup(sctx
->gen_group
);
75 dctx
->co_key
= EC_KEY_dup(sctx
->co_key
);
79 dctx
->kdf_type
= sctx
->kdf_type
;
80 dctx
->kdf_md
= sctx
->kdf_md
;
81 dctx
->kdf_outlen
= sctx
->kdf_outlen
;
83 dctx
->kdf_ukm
= OPENSSL_memdup(sctx
->kdf_ukm
, sctx
->kdf_ukmlen
);
88 dctx
->kdf_ukmlen
= sctx
->kdf_ukmlen
;
92 static void pkey_ec_cleanup(EVP_PKEY_CTX
*ctx
)
94 EC_PKEY_CTX
*dctx
= ctx
->data
;
96 EC_GROUP_free(dctx
->gen_group
);
97 EC_KEY_free(dctx
->co_key
);
98 OPENSSL_free(dctx
->kdf_ukm
);
103 static int pkey_ec_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
104 const unsigned char *tbs
, size_t tbslen
)
108 EC_PKEY_CTX
*dctx
= ctx
->data
;
109 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
110 const int ec_nid
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
113 *siglen
= ECDSA_size(ec
);
115 } else if (*siglen
< (size_t)ECDSA_size(ec
)) {
116 ECerr(EC_F_PKEY_EC_SIGN
, EC_R_BUFFER_TOO_SMALL
);
121 type
= EVP_MD_type(dctx
->md
);
125 if (ec_nid
== NID_sm2
) {
126 #if defined(OPENSSL_NO_SM2)
129 ret
= SM2_sign(type
, tbs
, tbslen
, sig
, &sltmp
, ec
);
132 ret
= ECDSA_sign(type
, tbs
, tbslen
, sig
, &sltmp
, ec
);
137 *siglen
= (size_t)sltmp
;
141 static int pkey_ec_verify(EVP_PKEY_CTX
*ctx
,
142 const unsigned char *sig
, size_t siglen
,
143 const unsigned char *tbs
, size_t tbslen
)
146 EC_PKEY_CTX
*dctx
= ctx
->data
;
147 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
148 const int ec_nid
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
151 type
= EVP_MD_type(dctx
->md
);
155 if (ec_nid
== NID_sm2
) {
156 #if defined(OPENSSL_NO_SM2)
159 ret
= SM2_verify(type
, tbs
, tbslen
, sig
, siglen
, ec
);
162 ret
= ECDSA_verify(type
, tbs
, tbslen
, sig
, siglen
, ec
);
168 #ifndef OPENSSL_NO_EC
169 static int pkey_ec_derive(EVP_PKEY_CTX
*ctx
, unsigned char *key
, size_t *keylen
)
173 const EC_POINT
*pubkey
= NULL
;
175 EC_PKEY_CTX
*dctx
= ctx
->data
;
176 if (!ctx
->pkey
|| !ctx
->peerkey
) {
177 ECerr(EC_F_PKEY_EC_DERIVE
, EC_R_KEYS_NOT_SET
);
181 eckey
= dctx
->co_key
? dctx
->co_key
: ctx
->pkey
->pkey
.ec
;
184 const EC_GROUP
*group
;
185 group
= EC_KEY_get0_group(eckey
);
186 *keylen
= (EC_GROUP_get_degree(group
) + 7) / 8;
189 pubkey
= EC_KEY_get0_public_key(ctx
->peerkey
->pkey
.ec
);
192 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
193 * an error, the result is truncated.
198 ret
= ECDH_compute_key(key
, outlen
, pubkey
, eckey
, 0);
205 static int pkey_ecies_encrypt(EVP_PKEY_CTX
*ctx
,
206 unsigned char *out
, size_t *outlen
,
207 const unsigned char *in
, size_t inlen
)
210 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
211 const int ec_nid
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
213 if (ec_nid
== NID_sm2
) {
214 # if defined(OPENSSL_NO_SM2)
218 EC_PKEY_CTX
*dctx
= ctx
->data
;
221 md_type
= EVP_MD_type(dctx
->md
);
226 *outlen
= SM2_ciphertext_size(ec
, EVP_get_digestbynid(md_type
),
231 ret
= SM2_encrypt(ec
, EVP_get_digestbynid(md_type
),
232 in
, inlen
, out
, outlen
);
236 /* standard ECIES not implemented */
243 static int pkey_ecies_decrypt(EVP_PKEY_CTX
*ctx
,
244 unsigned char *out
, size_t *outlen
,
245 const unsigned char *in
, size_t inlen
)
248 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
249 const int ec_nid
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
251 if (ec_nid
== NID_sm2
) {
252 # if defined(OPENSSL_NO_SM2)
256 EC_PKEY_CTX
*dctx
= ctx
->data
;
259 md_type
= EVP_MD_type(dctx
->md
);
264 *outlen
= SM2_plaintext_size(ec
, EVP_get_digestbynid(md_type
),
269 ret
= SM2_decrypt(ec
, EVP_get_digestbynid(md_type
),
270 in
, inlen
, out
, outlen
);
274 /* standard ECIES not implemented */
281 static int pkey_ec_kdf_derive(EVP_PKEY_CTX
*ctx
,
282 unsigned char *key
, size_t *keylen
)
284 EC_PKEY_CTX
*dctx
= ctx
->data
;
285 unsigned char *ktmp
= NULL
;
288 if (dctx
->kdf_type
== EVP_PKEY_ECDH_KDF_NONE
)
289 return pkey_ec_derive(ctx
, key
, keylen
);
291 *keylen
= dctx
->kdf_outlen
;
294 if (*keylen
!= dctx
->kdf_outlen
)
296 if (!pkey_ec_derive(ctx
, NULL
, &ktmplen
))
298 if ((ktmp
= OPENSSL_malloc(ktmplen
)) == NULL
) {
299 ECerr(EC_F_PKEY_EC_KDF_DERIVE
, ERR_R_MALLOC_FAILURE
);
302 if (!pkey_ec_derive(ctx
, ktmp
, &ktmplen
))
305 if (!ECDH_KDF_X9_62(key
, *keylen
, ktmp
, ktmplen
,
306 dctx
->kdf_ukm
, dctx
->kdf_ukmlen
, dctx
->kdf_md
))
311 OPENSSL_clear_free(ktmp
, ktmplen
);
316 static int pkey_ec_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
318 EC_PKEY_CTX
*dctx
= ctx
->data
;
321 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
:
322 group
= EC_GROUP_new_by_curve_name(p1
);
324 ECerr(EC_F_PKEY_EC_CTRL
, EC_R_INVALID_CURVE
);
327 EC_GROUP_free(dctx
->gen_group
);
328 dctx
->gen_group
= group
;
331 case EVP_PKEY_CTRL_EC_PARAM_ENC
:
332 if (!dctx
->gen_group
) {
333 ECerr(EC_F_PKEY_EC_CTRL
, EC_R_NO_PARAMETERS_SET
);
336 EC_GROUP_set_asn1_flag(dctx
->gen_group
, p1
);
339 #ifndef OPENSSL_NO_EC
340 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR
:
342 if (dctx
->cofactor_mode
!= -1)
343 return dctx
->cofactor_mode
;
345 EC_KEY
*ec_key
= ctx
->pkey
->pkey
.ec
;
346 return EC_KEY_get_flags(ec_key
) & EC_FLAG_COFACTOR_ECDH
? 1 : 0;
348 } else if (p1
< -1 || p1
> 1)
350 dctx
->cofactor_mode
= p1
;
352 EC_KEY
*ec_key
= ctx
->pkey
->pkey
.ec
;
355 /* If cofactor is 1 cofactor mode does nothing */
356 if (BN_is_one(ec_key
->group
->cofactor
))
359 dctx
->co_key
= EC_KEY_dup(ec_key
);
364 EC_KEY_set_flags(dctx
->co_key
, EC_FLAG_COFACTOR_ECDH
);
366 EC_KEY_clear_flags(dctx
->co_key
, EC_FLAG_COFACTOR_ECDH
);
368 EC_KEY_free(dctx
->co_key
);
374 case EVP_PKEY_CTRL_EC_KDF_TYPE
:
376 return dctx
->kdf_type
;
377 if (p1
!= EVP_PKEY_ECDH_KDF_NONE
&& p1
!= EVP_PKEY_ECDH_KDF_X9_62
)
382 case EVP_PKEY_CTRL_EC_KDF_MD
:
386 case EVP_PKEY_CTRL_GET_EC_KDF_MD
:
387 *(const EVP_MD
**)p2
= dctx
->kdf_md
;
390 case EVP_PKEY_CTRL_EC_KDF_OUTLEN
:
393 dctx
->kdf_outlen
= (size_t)p1
;
396 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN
:
397 *(int *)p2
= dctx
->kdf_outlen
;
400 case EVP_PKEY_CTRL_EC_KDF_UKM
:
401 OPENSSL_free(dctx
->kdf_ukm
);
404 dctx
->kdf_ukmlen
= p1
;
406 dctx
->kdf_ukmlen
= 0;
409 case EVP_PKEY_CTRL_GET_EC_KDF_UKM
:
410 *(unsigned char **)p2
= dctx
->kdf_ukm
;
411 return dctx
->kdf_ukmlen
;
413 case EVP_PKEY_CTRL_MD
:
414 if (EVP_MD_type((const EVP_MD
*)p2
) != NID_sha1
&&
415 EVP_MD_type((const EVP_MD
*)p2
) != NID_ecdsa_with_SHA1
&&
416 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha224
&&
417 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha256
&&
418 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha384
&&
419 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha512
&&
420 EVP_MD_type((const EVP_MD
*)p2
) != NID_sm3
) {
421 ECerr(EC_F_PKEY_EC_CTRL
, EC_R_INVALID_DIGEST_TYPE
);
427 case EVP_PKEY_CTRL_GET_MD
:
428 *(const EVP_MD
**)p2
= dctx
->md
;
431 case EVP_PKEY_CTRL_PEER_KEY
:
432 /* Default behaviour is OK */
433 case EVP_PKEY_CTRL_DIGESTINIT
:
434 case EVP_PKEY_CTRL_PKCS7_SIGN
:
435 case EVP_PKEY_CTRL_CMS_SIGN
:
444 static int pkey_ec_ctrl_str(EVP_PKEY_CTX
*ctx
,
445 const char *type
, const char *value
)
447 if (strcmp(type
, "ec_paramgen_curve") == 0) {
449 nid
= EC_curve_nist2nid(value
);
450 if (nid
== NID_undef
)
451 nid
= OBJ_sn2nid(value
);
452 if (nid
== NID_undef
)
453 nid
= OBJ_ln2nid(value
);
454 if (nid
== NID_undef
) {
455 ECerr(EC_F_PKEY_EC_CTRL_STR
, EC_R_INVALID_CURVE
);
458 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx
, nid
);
459 } else if (strcmp(type
, "ec_param_enc") == 0) {
461 if (strcmp(value
, "explicit") == 0)
463 else if (strcmp(value
, "named_curve") == 0)
464 param_enc
= OPENSSL_EC_NAMED_CURVE
;
467 return EVP_PKEY_CTX_set_ec_param_enc(ctx
, param_enc
);
468 } else if (strcmp(type
, "ecdh_kdf_md") == 0) {
470 if ((md
= EVP_get_digestbyname(value
)) == NULL
) {
471 ECerr(EC_F_PKEY_EC_CTRL_STR
, EC_R_INVALID_DIGEST
);
474 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx
, md
);
475 } else if (strcmp(type
, "ecdh_cofactor_mode") == 0) {
477 co_mode
= atoi(value
);
478 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx
, co_mode
);
484 static int pkey_ec_paramgen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
487 EC_PKEY_CTX
*dctx
= ctx
->data
;
489 if (dctx
->gen_group
== NULL
) {
490 ECerr(EC_F_PKEY_EC_PARAMGEN
, EC_R_NO_PARAMETERS_SET
);
496 ret
= EC_KEY_set_group(ec
, dctx
->gen_group
);
498 EVP_PKEY_assign_EC_KEY(pkey
, ec
);
504 static int pkey_ec_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
507 EC_PKEY_CTX
*dctx
= ctx
->data
;
508 if (ctx
->pkey
== NULL
&& dctx
->gen_group
== NULL
) {
509 ECerr(EC_F_PKEY_EC_KEYGEN
, EC_R_NO_PARAMETERS_SET
);
515 EVP_PKEY_assign_EC_KEY(pkey
, ec
);
517 /* Note: if error return, pkey is freed by parent routine */
518 if (!EVP_PKEY_copy_parameters(pkey
, ctx
->pkey
))
521 if (!EC_KEY_set_group(ec
, dctx
->gen_group
))
524 return EC_KEY_generate_key(pkey
->pkey
.ec
);
527 const EVP_PKEY_METHOD ec_pkey_meth
= {
557 #ifndef OPENSSL_NO_EC