2 * Copyright 2006-2021 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
11 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include <openssl/ec.h>
22 #include <openssl/evp.h>
23 #include "crypto/evp.h"
25 /* EC pkey context structure */
28 /* Key and paramgen group */
32 /* Duplicate key if custom cofactor needed */
35 signed char cofactor_mode
;
36 /* KDF (if any) to use for ECDH */
38 /* Message digest to use for key derivation */
40 /* User key material */
41 unsigned char *kdf_ukm
;
43 /* KDF output length */
47 static int pkey_ec_init(EVP_PKEY_CTX
*ctx
)
51 if ((dctx
= OPENSSL_zalloc(sizeof(*dctx
))) == NULL
) {
52 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
56 dctx
->cofactor_mode
= -1;
57 dctx
->kdf_type
= EVP_PKEY_ECDH_KDF_NONE
;
62 static int pkey_ec_copy(EVP_PKEY_CTX
*dst
, const EVP_PKEY_CTX
*src
)
64 EC_PKEY_CTX
*dctx
, *sctx
;
65 if (!pkey_ec_init(dst
))
69 if (sctx
->gen_group
) {
70 dctx
->gen_group
= EC_GROUP_dup(sctx
->gen_group
);
77 dctx
->co_key
= EC_KEY_dup(sctx
->co_key
);
81 dctx
->kdf_type
= sctx
->kdf_type
;
82 dctx
->kdf_md
= sctx
->kdf_md
;
83 dctx
->kdf_outlen
= sctx
->kdf_outlen
;
85 dctx
->kdf_ukm
= OPENSSL_memdup(sctx
->kdf_ukm
, sctx
->kdf_ukmlen
);
90 dctx
->kdf_ukmlen
= sctx
->kdf_ukmlen
;
94 static void pkey_ec_cleanup(EVP_PKEY_CTX
*ctx
)
96 EC_PKEY_CTX
*dctx
= ctx
->data
;
98 EC_GROUP_free(dctx
->gen_group
);
99 EC_KEY_free(dctx
->co_key
);
100 OPENSSL_free(dctx
->kdf_ukm
);
106 static int pkey_ec_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
107 const unsigned char *tbs
, size_t tbslen
)
111 EC_PKEY_CTX
*dctx
= ctx
->data
;
112 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
113 const int sig_sz
= ECDSA_size(ec
);
115 /* ensure cast to size_t is safe */
116 if (!ossl_assert(sig_sz
> 0))
120 *siglen
= (size_t)sig_sz
;
124 if (*siglen
< (size_t)sig_sz
) {
125 ERR_raise(ERR_LIB_EC
, EC_R_BUFFER_TOO_SMALL
);
129 type
= (dctx
->md
!= NULL
) ? EVP_MD_type(dctx
->md
) : NID_sha1
;
131 ret
= ECDSA_sign(type
, tbs
, tbslen
, sig
, &sltmp
, ec
);
135 *siglen
= (size_t)sltmp
;
139 static int pkey_ec_verify(EVP_PKEY_CTX
*ctx
,
140 const unsigned char *sig
, size_t siglen
,
141 const unsigned char *tbs
, size_t tbslen
)
144 EC_PKEY_CTX
*dctx
= ctx
->data
;
145 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
148 type
= EVP_MD_type(dctx
->md
);
152 ret
= ECDSA_verify(type
, tbs
, tbslen
, sig
, siglen
, ec
);
157 #ifndef OPENSSL_NO_EC
158 static int pkey_ec_derive(EVP_PKEY_CTX
*ctx
, unsigned char *key
, size_t *keylen
)
162 const EC_POINT
*pubkey
= NULL
;
164 EC_PKEY_CTX
*dctx
= ctx
->data
;
165 if (!ctx
->pkey
|| !ctx
->peerkey
) {
166 ERR_raise(ERR_LIB_EC
, EC_R_KEYS_NOT_SET
);
170 eckey
= dctx
->co_key
? dctx
->co_key
: ctx
->pkey
->pkey
.ec
;
173 const EC_GROUP
*group
;
174 group
= EC_KEY_get0_group(eckey
);
178 *keylen
= (EC_GROUP_get_degree(group
) + 7) / 8;
181 pubkey
= EC_KEY_get0_public_key(ctx
->peerkey
->pkey
.ec
);
184 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
185 * an error, the result is truncated.
190 ret
= ECDH_compute_key(key
, outlen
, pubkey
, eckey
, 0);
197 static int pkey_ec_kdf_derive(EVP_PKEY_CTX
*ctx
,
198 unsigned char *key
, size_t *keylen
)
200 EC_PKEY_CTX
*dctx
= ctx
->data
;
201 unsigned char *ktmp
= NULL
;
204 if (dctx
->kdf_type
== EVP_PKEY_ECDH_KDF_NONE
)
205 return pkey_ec_derive(ctx
, key
, keylen
);
207 *keylen
= dctx
->kdf_outlen
;
210 if (*keylen
!= dctx
->kdf_outlen
)
212 if (!pkey_ec_derive(ctx
, NULL
, &ktmplen
))
214 if ((ktmp
= OPENSSL_malloc(ktmplen
)) == NULL
) {
215 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
218 if (!pkey_ec_derive(ctx
, ktmp
, &ktmplen
))
221 if (!ossl_ecdh_kdf_X9_63(key
, *keylen
, ktmp
, ktmplen
,
222 dctx
->kdf_ukm
, dctx
->kdf_ukmlen
, dctx
->kdf_md
,
223 ctx
->libctx
, ctx
->propquery
))
228 OPENSSL_clear_free(ktmp
, ktmplen
);
233 static int pkey_ec_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
235 EC_PKEY_CTX
*dctx
= ctx
->data
;
238 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
:
239 group
= EC_GROUP_new_by_curve_name(p1
);
241 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_CURVE
);
244 EC_GROUP_free(dctx
->gen_group
);
245 dctx
->gen_group
= group
;
248 case EVP_PKEY_CTRL_EC_PARAM_ENC
:
249 if (!dctx
->gen_group
) {
250 ERR_raise(ERR_LIB_EC
, EC_R_NO_PARAMETERS_SET
);
253 EC_GROUP_set_asn1_flag(dctx
->gen_group
, p1
);
256 #ifndef OPENSSL_NO_EC
257 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR
:
259 if (dctx
->cofactor_mode
!= -1)
260 return dctx
->cofactor_mode
;
262 EC_KEY
*ec_key
= ctx
->pkey
->pkey
.ec
;
263 return EC_KEY_get_flags(ec_key
) & EC_FLAG_COFACTOR_ECDH
? 1 : 0;
265 } else if (p1
< -1 || p1
> 1)
267 dctx
->cofactor_mode
= p1
;
269 EC_KEY
*ec_key
= ctx
->pkey
->pkey
.ec
;
272 /* If cofactor is 1 cofactor mode does nothing */
273 if (BN_is_one(ec_key
->group
->cofactor
))
276 dctx
->co_key
= EC_KEY_dup(ec_key
);
281 EC_KEY_set_flags(dctx
->co_key
, EC_FLAG_COFACTOR_ECDH
);
283 EC_KEY_clear_flags(dctx
->co_key
, EC_FLAG_COFACTOR_ECDH
);
285 EC_KEY_free(dctx
->co_key
);
291 case EVP_PKEY_CTRL_EC_KDF_TYPE
:
293 return dctx
->kdf_type
;
294 if (p1
!= EVP_PKEY_ECDH_KDF_NONE
&& p1
!= EVP_PKEY_ECDH_KDF_X9_63
)
299 case EVP_PKEY_CTRL_EC_KDF_MD
:
303 case EVP_PKEY_CTRL_GET_EC_KDF_MD
:
304 *(const EVP_MD
**)p2
= dctx
->kdf_md
;
307 case EVP_PKEY_CTRL_EC_KDF_OUTLEN
:
310 dctx
->kdf_outlen
= (size_t)p1
;
313 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN
:
314 *(int *)p2
= dctx
->kdf_outlen
;
317 case EVP_PKEY_CTRL_EC_KDF_UKM
:
318 OPENSSL_free(dctx
->kdf_ukm
);
321 dctx
->kdf_ukmlen
= p1
;
323 dctx
->kdf_ukmlen
= 0;
326 case EVP_PKEY_CTRL_GET_EC_KDF_UKM
:
327 *(unsigned char **)p2
= dctx
->kdf_ukm
;
328 return dctx
->kdf_ukmlen
;
330 case EVP_PKEY_CTRL_MD
:
331 if (EVP_MD_type((const EVP_MD
*)p2
) != NID_sha1
&&
332 EVP_MD_type((const EVP_MD
*)p2
) != NID_ecdsa_with_SHA1
&&
333 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha224
&&
334 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha256
&&
335 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha384
&&
336 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha512
&&
337 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha3_224
&&
338 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha3_256
&&
339 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha3_384
&&
340 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha3_512
&&
341 EVP_MD_type((const EVP_MD
*)p2
) != NID_sm3
) {
342 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_DIGEST_TYPE
);
348 case EVP_PKEY_CTRL_GET_MD
:
349 *(const EVP_MD
**)p2
= dctx
->md
;
352 case EVP_PKEY_CTRL_PEER_KEY
:
353 /* Default behaviour is OK */
354 case EVP_PKEY_CTRL_DIGESTINIT
:
355 case EVP_PKEY_CTRL_PKCS7_SIGN
:
356 case EVP_PKEY_CTRL_CMS_SIGN
:
365 static int pkey_ec_ctrl_str(EVP_PKEY_CTX
*ctx
,
366 const char *type
, const char *value
)
368 if (strcmp(type
, "ec_paramgen_curve") == 0) {
370 nid
= EC_curve_nist2nid(value
);
371 if (nid
== NID_undef
)
372 nid
= OBJ_sn2nid(value
);
373 if (nid
== NID_undef
)
374 nid
= OBJ_ln2nid(value
);
375 if (nid
== NID_undef
) {
376 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_CURVE
);
379 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx
, nid
);
380 } else if (strcmp(type
, "ec_param_enc") == 0) {
382 if (strcmp(value
, "explicit") == 0)
384 else if (strcmp(value
, "named_curve") == 0)
385 param_enc
= OPENSSL_EC_NAMED_CURVE
;
388 return EVP_PKEY_CTX_set_ec_param_enc(ctx
, param_enc
);
389 } else if (strcmp(type
, "ecdh_kdf_md") == 0) {
391 if ((md
= EVP_get_digestbyname(value
)) == NULL
) {
392 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_DIGEST
);
395 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx
, md
);
396 } else if (strcmp(type
, "ecdh_cofactor_mode") == 0) {
398 co_mode
= atoi(value
);
399 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx
, co_mode
);
405 static int pkey_ec_paramgen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
408 EC_PKEY_CTX
*dctx
= ctx
->data
;
411 if (dctx
->gen_group
== NULL
) {
412 ERR_raise(ERR_LIB_EC
, EC_R_NO_PARAMETERS_SET
);
418 if (!(ret
= EC_KEY_set_group(ec
, dctx
->gen_group
))
419 || !ossl_assert(ret
= EVP_PKEY_assign_EC_KEY(pkey
, ec
)))
424 static int pkey_ec_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
427 EC_PKEY_CTX
*dctx
= ctx
->data
;
430 if (ctx
->pkey
== NULL
&& dctx
->gen_group
== NULL
) {
431 ERR_raise(ERR_LIB_EC
, EC_R_NO_PARAMETERS_SET
);
437 if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey
, ec
))) {
441 /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
442 if (ctx
->pkey
!= NULL
)
443 ret
= EVP_PKEY_copy_parameters(pkey
, ctx
->pkey
);
445 ret
= EC_KEY_set_group(ec
, dctx
->gen_group
);
447 return ret
? EC_KEY_generate_key(ec
) : 0;
450 static const EVP_PKEY_METHOD ec_pkey_meth
= {
480 #ifndef OPENSSL_NO_EC
489 const EVP_PKEY_METHOD
*ossl_ec_pkey_method(void)
491 return &ec_pkey_meth
;