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
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 "crypto/evp.h"
19 /* EC pkey context structure */
22 /* Key and paramgen group */
26 /* Duplicate key if custom cofactor needed */
29 signed char cofactor_mode
;
30 /* KDF (if any) to use for ECDH */
32 /* Message digest to use for key derivation */
34 /* User key material */
35 unsigned char *kdf_ukm
;
37 /* KDF output length */
41 static int pkey_ec_init(EVP_PKEY_CTX
*ctx
)
45 if ((dctx
= OPENSSL_zalloc(sizeof(*dctx
))) == NULL
) {
46 ECerr(EC_F_PKEY_EC_INIT
, ERR_R_MALLOC_FAILURE
);
50 dctx
->cofactor_mode
= -1;
51 dctx
->kdf_type
= EVP_PKEY_ECDH_KDF_NONE
;
56 static int pkey_ec_copy(EVP_PKEY_CTX
*dst
, const EVP_PKEY_CTX
*src
)
58 EC_PKEY_CTX
*dctx
, *sctx
;
59 if (!pkey_ec_init(dst
))
63 if (sctx
->gen_group
) {
64 dctx
->gen_group
= EC_GROUP_dup(sctx
->gen_group
);
71 dctx
->co_key
= EC_KEY_dup(sctx
->co_key
);
75 dctx
->kdf_type
= sctx
->kdf_type
;
76 dctx
->kdf_md
= sctx
->kdf_md
;
77 dctx
->kdf_outlen
= sctx
->kdf_outlen
;
79 dctx
->kdf_ukm
= OPENSSL_memdup(sctx
->kdf_ukm
, sctx
->kdf_ukmlen
);
84 dctx
->kdf_ukmlen
= sctx
->kdf_ukmlen
;
88 static void pkey_ec_cleanup(EVP_PKEY_CTX
*ctx
)
90 EC_PKEY_CTX
*dctx
= ctx
->data
;
92 EC_GROUP_free(dctx
->gen_group
);
93 EC_KEY_free(dctx
->co_key
);
94 OPENSSL_free(dctx
->kdf_ukm
);
100 static int pkey_ec_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
101 const unsigned char *tbs
, size_t tbslen
)
105 EC_PKEY_CTX
*dctx
= ctx
->data
;
106 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
107 const int sig_sz
= ECDSA_size(ec
);
109 /* ensure cast to size_t is safe */
110 if (!ossl_assert(sig_sz
> 0))
114 *siglen
= (size_t)sig_sz
;
118 if (*siglen
< (size_t)sig_sz
) {
119 ECerr(EC_F_PKEY_EC_SIGN
, EC_R_BUFFER_TOO_SMALL
);
123 type
= (dctx
->md
!= NULL
) ? EVP_MD_type(dctx
->md
) : NID_sha1
;
125 ret
= ECDSA_sign(type
, tbs
, tbslen
, sig
, &sltmp
, ec
);
129 *siglen
= (size_t)sltmp
;
133 static int pkey_ec_verify(EVP_PKEY_CTX
*ctx
,
134 const unsigned char *sig
, size_t siglen
,
135 const unsigned char *tbs
, size_t tbslen
)
138 EC_PKEY_CTX
*dctx
= ctx
->data
;
139 EC_KEY
*ec
= ctx
->pkey
->pkey
.ec
;
142 type
= EVP_MD_type(dctx
->md
);
146 ret
= ECDSA_verify(type
, tbs
, tbslen
, sig
, siglen
, ec
);
151 #ifndef OPENSSL_NO_EC
152 static int pkey_ec_derive(EVP_PKEY_CTX
*ctx
, unsigned char *key
, size_t *keylen
)
156 const EC_POINT
*pubkey
= NULL
;
158 EC_PKEY_CTX
*dctx
= ctx
->data
;
159 if (!ctx
->pkey
|| !ctx
->peerkey
) {
160 ECerr(EC_F_PKEY_EC_DERIVE
, EC_R_KEYS_NOT_SET
);
164 eckey
= dctx
->co_key
? dctx
->co_key
: ctx
->pkey
->pkey
.ec
;
167 const EC_GROUP
*group
;
168 group
= EC_KEY_get0_group(eckey
);
169 *keylen
= (EC_GROUP_get_degree(group
) + 7) / 8;
172 pubkey
= EC_KEY_get0_public_key(ctx
->peerkey
->pkey
.ec
);
175 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
176 * an error, the result is truncated.
181 ret
= ECDH_compute_key(key
, outlen
, pubkey
, eckey
, 0);
188 static int pkey_ec_kdf_derive(EVP_PKEY_CTX
*ctx
,
189 unsigned char *key
, size_t *keylen
)
191 EC_PKEY_CTX
*dctx
= ctx
->data
;
192 unsigned char *ktmp
= NULL
;
195 if (dctx
->kdf_type
== EVP_PKEY_ECDH_KDF_NONE
)
196 return pkey_ec_derive(ctx
, key
, keylen
);
198 *keylen
= dctx
->kdf_outlen
;
201 if (*keylen
!= dctx
->kdf_outlen
)
203 if (!pkey_ec_derive(ctx
, NULL
, &ktmplen
))
205 if ((ktmp
= OPENSSL_malloc(ktmplen
)) == NULL
) {
206 ECerr(EC_F_PKEY_EC_KDF_DERIVE
, ERR_R_MALLOC_FAILURE
);
209 if (!pkey_ec_derive(ctx
, ktmp
, &ktmplen
))
212 if (!ecdh_KDF_X9_63(key
, *keylen
, ktmp
, ktmplen
,
213 dctx
->kdf_ukm
, dctx
->kdf_ukmlen
, dctx
->kdf_md
))
218 OPENSSL_clear_free(ktmp
, ktmplen
);
223 static int pkey_ec_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
225 EC_PKEY_CTX
*dctx
= ctx
->data
;
228 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
:
229 group
= EC_GROUP_new_by_curve_name(p1
);
231 ECerr(EC_F_PKEY_EC_CTRL
, EC_R_INVALID_CURVE
);
234 EC_GROUP_free(dctx
->gen_group
);
235 dctx
->gen_group
= group
;
238 case EVP_PKEY_CTRL_EC_PARAM_ENC
:
239 if (!dctx
->gen_group
) {
240 ECerr(EC_F_PKEY_EC_CTRL
, EC_R_NO_PARAMETERS_SET
);
243 EC_GROUP_set_asn1_flag(dctx
->gen_group
, p1
);
246 #ifndef OPENSSL_NO_EC
247 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR
:
249 if (dctx
->cofactor_mode
!= -1)
250 return dctx
->cofactor_mode
;
252 EC_KEY
*ec_key
= ctx
->pkey
->pkey
.ec
;
253 return EC_KEY_get_flags(ec_key
) & EC_FLAG_COFACTOR_ECDH
? 1 : 0;
255 } else if (p1
< -1 || p1
> 1)
257 dctx
->cofactor_mode
= p1
;
259 EC_KEY
*ec_key
= ctx
->pkey
->pkey
.ec
;
262 /* If cofactor is 1 cofactor mode does nothing */
263 if (BN_is_one(ec_key
->group
->cofactor
))
266 dctx
->co_key
= EC_KEY_dup(ec_key
);
271 EC_KEY_set_flags(dctx
->co_key
, EC_FLAG_COFACTOR_ECDH
);
273 EC_KEY_clear_flags(dctx
->co_key
, EC_FLAG_COFACTOR_ECDH
);
275 EC_KEY_free(dctx
->co_key
);
281 case EVP_PKEY_CTRL_EC_KDF_TYPE
:
283 return dctx
->kdf_type
;
284 if (p1
!= EVP_PKEY_ECDH_KDF_NONE
&& p1
!= EVP_PKEY_ECDH_KDF_X9_63
)
289 case EVP_PKEY_CTRL_EC_KDF_MD
:
293 case EVP_PKEY_CTRL_GET_EC_KDF_MD
:
294 *(const EVP_MD
**)p2
= dctx
->kdf_md
;
297 case EVP_PKEY_CTRL_EC_KDF_OUTLEN
:
300 dctx
->kdf_outlen
= (size_t)p1
;
303 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN
:
304 *(int *)p2
= dctx
->kdf_outlen
;
307 case EVP_PKEY_CTRL_EC_KDF_UKM
:
308 OPENSSL_free(dctx
->kdf_ukm
);
311 dctx
->kdf_ukmlen
= p1
;
313 dctx
->kdf_ukmlen
= 0;
316 case EVP_PKEY_CTRL_GET_EC_KDF_UKM
:
317 *(unsigned char **)p2
= dctx
->kdf_ukm
;
318 return dctx
->kdf_ukmlen
;
320 case EVP_PKEY_CTRL_MD
:
321 if (EVP_MD_type((const EVP_MD
*)p2
) != NID_sha1
&&
322 EVP_MD_type((const EVP_MD
*)p2
) != NID_ecdsa_with_SHA1
&&
323 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha224
&&
324 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha256
&&
325 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha384
&&
326 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha512
&&
327 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha3_224
&&
328 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha3_256
&&
329 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha3_384
&&
330 EVP_MD_type((const EVP_MD
*)p2
) != NID_sha3_512
&&
331 EVP_MD_type((const EVP_MD
*)p2
) != NID_sm3
) {
332 ECerr(EC_F_PKEY_EC_CTRL
, EC_R_INVALID_DIGEST_TYPE
);
338 case EVP_PKEY_CTRL_GET_MD
:
339 *(const EVP_MD
**)p2
= dctx
->md
;
342 case EVP_PKEY_CTRL_PEER_KEY
:
343 /* Default behaviour is OK */
344 case EVP_PKEY_CTRL_DIGESTINIT
:
345 case EVP_PKEY_CTRL_PKCS7_SIGN
:
346 case EVP_PKEY_CTRL_CMS_SIGN
:
355 static int pkey_ec_ctrl_str(EVP_PKEY_CTX
*ctx
,
356 const char *type
, const char *value
)
358 if (strcmp(type
, "ec_paramgen_curve") == 0) {
360 nid
= EC_curve_nist2nid(value
);
361 if (nid
== NID_undef
)
362 nid
= OBJ_sn2nid(value
);
363 if (nid
== NID_undef
)
364 nid
= OBJ_ln2nid(value
);
365 if (nid
== NID_undef
) {
366 ECerr(EC_F_PKEY_EC_CTRL_STR
, EC_R_INVALID_CURVE
);
369 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx
, nid
);
370 } else if (strcmp(type
, "ec_param_enc") == 0) {
372 if (strcmp(value
, "explicit") == 0)
374 else if (strcmp(value
, "named_curve") == 0)
375 param_enc
= OPENSSL_EC_NAMED_CURVE
;
378 return EVP_PKEY_CTX_set_ec_param_enc(ctx
, param_enc
);
379 } else if (strcmp(type
, "ecdh_kdf_md") == 0) {
381 if ((md
= EVP_get_digestbyname(value
)) == NULL
) {
382 ECerr(EC_F_PKEY_EC_CTRL_STR
, EC_R_INVALID_DIGEST
);
385 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx
, md
);
386 } else if (strcmp(type
, "ecdh_cofactor_mode") == 0) {
388 co_mode
= atoi(value
);
389 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx
, co_mode
);
395 static int pkey_ec_paramgen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
398 EC_PKEY_CTX
*dctx
= ctx
->data
;
401 if (dctx
->gen_group
== NULL
) {
402 ECerr(EC_F_PKEY_EC_PARAMGEN
, EC_R_NO_PARAMETERS_SET
);
408 if (!(ret
= EC_KEY_set_group(ec
, dctx
->gen_group
))
409 || !ossl_assert(ret
= EVP_PKEY_assign_EC_KEY(pkey
, ec
)))
414 static int pkey_ec_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
417 EC_PKEY_CTX
*dctx
= ctx
->data
;
420 if (ctx
->pkey
== NULL
&& dctx
->gen_group
== NULL
) {
421 ECerr(EC_F_PKEY_EC_KEYGEN
, EC_R_NO_PARAMETERS_SET
);
427 if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey
, ec
))) {
431 /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
432 if (ctx
->pkey
!= NULL
)
433 ret
= EVP_PKEY_copy_parameters(pkey
, ctx
->pkey
);
435 ret
= EC_KEY_set_group(ec
, dctx
->gen_group
);
437 return ret
? EC_KEY_generate_key(ec
) : 0;
440 static const EVP_PKEY_METHOD ec_pkey_meth
= {
470 #ifndef OPENSSL_NO_EC
479 const EVP_PKEY_METHOD
*ec_pkey_method(void)
481 return &ec_pkey_meth
;