2 * Copyright 2020-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 low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include <openssl/crypto.h>
18 #include <openssl/evp.h>
19 #include <openssl/core_dispatch.h>
20 #include <openssl/core_names.h>
21 #include <openssl/ec.h>
22 #include <openssl/params.h>
23 #include <openssl/err.h>
24 #include "prov/provider_ctx.h"
25 #include "prov/providercommon.h"
26 #include "prov/implementations.h"
27 #include "prov/securitycheck.h"
28 #include "crypto/ec.h" /* ecdh_KDF_X9_63() */
30 static OSSL_FUNC_keyexch_newctx_fn ecdh_newctx
;
31 static OSSL_FUNC_keyexch_init_fn ecdh_init
;
32 static OSSL_FUNC_keyexch_set_peer_fn ecdh_set_peer
;
33 static OSSL_FUNC_keyexch_derive_fn ecdh_derive
;
34 static OSSL_FUNC_keyexch_freectx_fn ecdh_freectx
;
35 static OSSL_FUNC_keyexch_dupctx_fn ecdh_dupctx
;
36 static OSSL_FUNC_keyexch_set_ctx_params_fn ecdh_set_ctx_params
;
37 static OSSL_FUNC_keyexch_settable_ctx_params_fn ecdh_settable_ctx_params
;
38 static OSSL_FUNC_keyexch_get_ctx_params_fn ecdh_get_ctx_params
;
39 static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecdh_gettable_ctx_params
;
42 PROV_ECDH_KDF_NONE
= 0,
47 * What's passed as an actual key is defined by the KEYMGMT interface.
48 * We happen to know that our KEYMGMT simply passes EC_KEY structures, so
49 * we use that here too.
63 * . -1 use cofactor mode set for k
70 /* KDF (if any) to use for ECDH */
71 enum kdf_type kdf_type
;
72 /* Message digest to use for key derivation */
74 /* User key material */
75 unsigned char *kdf_ukm
;
77 /* KDF output length */
82 void *ecdh_newctx(void *provctx
)
86 if (!ossl_prov_is_running())
89 pectx
= OPENSSL_zalloc(sizeof(*pectx
));
93 pectx
->libctx
= PROV_LIBCTX_OF(provctx
);
94 pectx
->cofactor_mode
= -1;
95 pectx
->kdf_type
= PROV_ECDH_KDF_NONE
;
101 int ecdh_init(void *vpecdhctx
, void *vecdh
)
103 PROV_ECDH_CTX
*pecdhctx
= (PROV_ECDH_CTX
*)vpecdhctx
;
105 if (!ossl_prov_is_running()
108 || !EC_KEY_up_ref(vecdh
))
110 EC_KEY_free(pecdhctx
->k
);
112 pecdhctx
->cofactor_mode
= -1;
113 pecdhctx
->kdf_type
= PROV_ECDH_KDF_NONE
;
114 return ec_check_key(vecdh
, 1);
118 int ecdh_set_peer(void *vpecdhctx
, void *vecdh
)
120 PROV_ECDH_CTX
*pecdhctx
= (PROV_ECDH_CTX
*)vpecdhctx
;
122 if (!ossl_prov_is_running()
125 || !EC_KEY_up_ref(vecdh
))
127 EC_KEY_free(pecdhctx
->peerk
);
128 pecdhctx
->peerk
= vecdh
;
129 return ec_check_key(vecdh
, 1);
133 void ecdh_freectx(void *vpecdhctx
)
135 PROV_ECDH_CTX
*pecdhctx
= (PROV_ECDH_CTX
*)vpecdhctx
;
137 EC_KEY_free(pecdhctx
->k
);
138 EC_KEY_free(pecdhctx
->peerk
);
140 EVP_MD_free(pecdhctx
->kdf_md
);
141 OPENSSL_clear_free(pecdhctx
->kdf_ukm
, pecdhctx
->kdf_ukmlen
);
143 OPENSSL_free(pecdhctx
);
147 void *ecdh_dupctx(void *vpecdhctx
)
149 PROV_ECDH_CTX
*srcctx
= (PROV_ECDH_CTX
*)vpecdhctx
;
150 PROV_ECDH_CTX
*dstctx
;
152 if (!ossl_prov_is_running())
155 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
161 /* clear all pointers */
164 dstctx
->peerk
= NULL
;
165 dstctx
->kdf_md
= NULL
;
166 dstctx
->kdf_ukm
= NULL
;
168 /* up-ref all ref-counted objects referenced in dstctx */
170 if (srcctx
->k
!= NULL
&& !EC_KEY_up_ref(srcctx
->k
))
173 dstctx
->k
= srcctx
->k
;
175 if (srcctx
->peerk
!= NULL
&& !EC_KEY_up_ref(srcctx
->peerk
))
178 dstctx
->peerk
= srcctx
->peerk
;
180 if (srcctx
->kdf_md
!= NULL
&& !EVP_MD_up_ref(srcctx
->kdf_md
))
183 dstctx
->kdf_md
= srcctx
->kdf_md
;
185 /* Duplicate UKM data if present */
186 if (srcctx
->kdf_ukm
!= NULL
&& srcctx
->kdf_ukmlen
> 0) {
187 dstctx
->kdf_ukm
= OPENSSL_memdup(srcctx
->kdf_ukm
,
189 if (dstctx
->kdf_ukm
== NULL
)
196 ecdh_freectx(dstctx
);
201 int ecdh_set_ctx_params(void *vpecdhctx
, const OSSL_PARAM params
[])
203 char name
[80] = { '\0' }; /* should be big enough */
205 PROV_ECDH_CTX
*pectx
= (PROV_ECDH_CTX
*)vpecdhctx
;
208 if (pectx
== NULL
|| params
== NULL
)
211 p
= OSSL_PARAM_locate_const(params
, OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE
);
215 if (!OSSL_PARAM_get_int(p
, &mode
))
218 if (mode
< -1 || mode
> 1)
221 pectx
->cofactor_mode
= mode
;
224 p
= OSSL_PARAM_locate_const(params
, OSSL_EXCHANGE_PARAM_KDF_TYPE
);
227 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(name
)))
231 pectx
->kdf_type
= PROV_ECDH_KDF_NONE
;
232 else if (strcmp(name
, OSSL_KDF_NAME_X963KDF
) == 0)
233 pectx
->kdf_type
= PROV_ECDH_KDF_X9_63
;
238 p
= OSSL_PARAM_locate_const(params
, OSSL_EXCHANGE_PARAM_KDF_DIGEST
);
240 char mdprops
[80] = { '\0' }; /* should be big enough */
243 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(name
)))
247 p
= OSSL_PARAM_locate_const(params
,
248 OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS
);
251 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdprops
)))
255 EVP_MD_free(pectx
->kdf_md
);
256 pectx
->kdf_md
= EVP_MD_fetch(pectx
->libctx
, name
, mdprops
);
257 if (!digest_is_allowed(pectx
->kdf_md
)) {
258 EVP_MD_free(pectx
->kdf_md
);
259 pectx
->kdf_md
= NULL
;
261 if (pectx
->kdf_md
== NULL
)
265 p
= OSSL_PARAM_locate_const(params
, OSSL_EXCHANGE_PARAM_KDF_OUTLEN
);
269 if (!OSSL_PARAM_get_size_t(p
, &outlen
))
271 pectx
->kdf_outlen
= outlen
;
274 p
= OSSL_PARAM_locate_const(params
, OSSL_EXCHANGE_PARAM_KDF_UKM
);
276 void *tmp_ukm
= NULL
;
279 if (!OSSL_PARAM_get_octet_string(p
, &tmp_ukm
, 0, &tmp_ukmlen
))
281 OPENSSL_free(pectx
->kdf_ukm
);
282 pectx
->kdf_ukm
= tmp_ukm
;
283 pectx
->kdf_ukmlen
= tmp_ukmlen
;
289 static const OSSL_PARAM known_settable_ctx_params
[] = {
290 OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE
, NULL
),
291 OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE
, NULL
, 0),
292 OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST
, NULL
, 0),
293 OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS
, NULL
, 0),
294 OSSL_PARAM_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN
, NULL
),
295 OSSL_PARAM_octet_string(OSSL_EXCHANGE_PARAM_KDF_UKM
, NULL
, 0),
300 const OSSL_PARAM
*ecdh_settable_ctx_params(ossl_unused
void *provctx
)
302 return known_settable_ctx_params
;
306 int ecdh_get_ctx_params(void *vpecdhctx
, OSSL_PARAM params
[])
308 PROV_ECDH_CTX
*pectx
= (PROV_ECDH_CTX
*)vpecdhctx
;
311 if (pectx
== NULL
|| params
== NULL
)
314 p
= OSSL_PARAM_locate(params
, OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE
);
316 int mode
= pectx
->cofactor_mode
;
319 /* check what is the default for pecdhctx->k */
320 mode
= EC_KEY_get_flags(pectx
->k
) & EC_FLAG_COFACTOR_ECDH
? 1 : 0;
323 if (!OSSL_PARAM_set_int(p
, mode
))
327 p
= OSSL_PARAM_locate(params
, OSSL_EXCHANGE_PARAM_KDF_TYPE
);
329 const char *kdf_type
= NULL
;
331 switch (pectx
->kdf_type
) {
332 case PROV_ECDH_KDF_NONE
:
335 case PROV_ECDH_KDF_X9_63
:
336 kdf_type
= OSSL_KDF_NAME_X963KDF
;
342 if (!OSSL_PARAM_set_utf8_string(p
, kdf_type
))
346 p
= OSSL_PARAM_locate(params
, OSSL_EXCHANGE_PARAM_KDF_DIGEST
);
348 && !OSSL_PARAM_set_utf8_string(p
, pectx
->kdf_md
== NULL
350 : EVP_MD_name(pectx
->kdf_md
))){
354 p
= OSSL_PARAM_locate(params
, OSSL_EXCHANGE_PARAM_KDF_OUTLEN
);
355 if (p
!= NULL
&& !OSSL_PARAM_set_size_t(p
, pectx
->kdf_outlen
))
358 p
= OSSL_PARAM_locate(params
, OSSL_EXCHANGE_PARAM_KDF_UKM
);
360 !OSSL_PARAM_set_octet_ptr(p
, pectx
->kdf_ukm
, pectx
->kdf_ukmlen
))
366 static const OSSL_PARAM known_gettable_ctx_params
[] = {
367 OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE
, NULL
),
368 OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE
, NULL
, 0),
369 OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST
, NULL
, 0),
370 OSSL_PARAM_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN
, NULL
),
371 OSSL_PARAM_DEFN(OSSL_EXCHANGE_PARAM_KDF_UKM
, OSSL_PARAM_OCTET_PTR
,
377 const OSSL_PARAM
*ecdh_gettable_ctx_params(ossl_unused
void *provctx
)
379 return known_gettable_ctx_params
;
383 size_t ecdh_size(const EC_KEY
*k
)
386 const EC_GROUP
*group
;
389 || (group
= EC_KEY_get0_group(k
)) == NULL
)
392 degree
= EC_GROUP_get_degree(group
);
394 return (degree
+ 7) / 8;
398 int ecdh_plain_derive(void *vpecdhctx
, unsigned char *secret
,
399 size_t *psecretlen
, size_t outlen
)
401 PROV_ECDH_CTX
*pecdhctx
= (PROV_ECDH_CTX
*)vpecdhctx
;
403 size_t ecdhsize
, size
;
404 const EC_POINT
*ppubkey
= NULL
;
405 EC_KEY
*privk
= NULL
;
406 const EC_GROUP
*group
;
407 const BIGNUM
*cofactor
;
408 int key_cofactor_mode
;
410 if (pecdhctx
->k
== NULL
|| pecdhctx
->peerk
== NULL
) {
411 ERR_raise(ERR_LIB_PROV
, EC_R_KEYS_NOT_SET
);
415 ecdhsize
= ecdh_size(pecdhctx
->k
);
416 if (secret
== NULL
) {
417 *psecretlen
= ecdhsize
;
421 if ((group
= EC_KEY_get0_group(pecdhctx
->k
)) == NULL
422 || (cofactor
= EC_GROUP_get0_cofactor(group
)) == NULL
)
426 * NB: unlike PKCS#3 DH, if outlen is less than maximum size this is not
427 * an error, the result is truncated.
429 size
= outlen
< ecdhsize
? outlen
: ecdhsize
;
432 * The ctx->cofactor_mode flag has precedence over the
433 * cofactor_mode flag set on ctx->k.
435 * - if ctx->cofactor_mode == -1, use ctx->k directly
436 * - if ctx->cofactor_mode == key_cofactor_mode, use ctx->k directly
437 * - if ctx->cofactor_mode != key_cofactor_mode:
438 * - if ctx->k->cofactor == 1, the cofactor_mode flag is irrelevant, use
440 * - if ctx->k->cofactor != 1, use a duplicate of ctx->k with the flag
441 * set to ctx->cofactor_mode
444 (EC_KEY_get_flags(pecdhctx
->k
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
445 if (pecdhctx
->cofactor_mode
!= -1
446 && pecdhctx
->cofactor_mode
!= key_cofactor_mode
447 && !BN_is_one(cofactor
)) {
448 if ((privk
= EC_KEY_dup(pecdhctx
->k
)) == NULL
)
451 if (pecdhctx
->cofactor_mode
== 1)
452 EC_KEY_set_flags(privk
, EC_FLAG_COFACTOR_ECDH
);
454 EC_KEY_clear_flags(privk
, EC_FLAG_COFACTOR_ECDH
);
459 ppubkey
= EC_KEY_get0_public_key(pecdhctx
->peerk
);
461 retlen
= ECDH_compute_key(secret
, size
, ppubkey
, privk
, NULL
);
466 *psecretlen
= retlen
;
470 if (privk
!= pecdhctx
->k
)
476 int ecdh_X9_63_kdf_derive(void *vpecdhctx
, unsigned char *secret
,
477 size_t *psecretlen
, size_t outlen
)
479 PROV_ECDH_CTX
*pecdhctx
= (PROV_ECDH_CTX
*)vpecdhctx
;
480 unsigned char *stmp
= NULL
;
484 if (secret
== NULL
) {
485 *psecretlen
= pecdhctx
->kdf_outlen
;
489 if (pecdhctx
->kdf_outlen
> outlen
)
491 if (!ecdh_plain_derive(vpecdhctx
, NULL
, &stmplen
, 0))
493 if ((stmp
= OPENSSL_secure_malloc(stmplen
)) == NULL
) {
494 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
497 if (!ecdh_plain_derive(vpecdhctx
, stmp
, &stmplen
, stmplen
))
501 if (!ecdh_KDF_X9_63(secret
, pecdhctx
->kdf_outlen
,
504 pecdhctx
->kdf_ukmlen
,
506 pecdhctx
->libctx
, NULL
))
508 *psecretlen
= pecdhctx
->kdf_outlen
;
512 OPENSSL_secure_clear_free(stmp
, stmplen
);
517 int ecdh_derive(void *vpecdhctx
, unsigned char *secret
,
518 size_t *psecretlen
, size_t outlen
)
520 PROV_ECDH_CTX
*pecdhctx
= (PROV_ECDH_CTX
*)vpecdhctx
;
522 switch (pecdhctx
->kdf_type
) {
523 case PROV_ECDH_KDF_NONE
:
524 return ecdh_plain_derive(vpecdhctx
, secret
, psecretlen
, outlen
);
525 case PROV_ECDH_KDF_X9_63
:
526 return ecdh_X9_63_kdf_derive(vpecdhctx
, secret
, psecretlen
, outlen
);
533 const OSSL_DISPATCH ecossl_dh_keyexch_functions
[] = {
534 { OSSL_FUNC_KEYEXCH_NEWCTX
, (void (*)(void))ecdh_newctx
},
535 { OSSL_FUNC_KEYEXCH_INIT
, (void (*)(void))ecdh_init
},
536 { OSSL_FUNC_KEYEXCH_DERIVE
, (void (*)(void))ecdh_derive
},
537 { OSSL_FUNC_KEYEXCH_SET_PEER
, (void (*)(void))ecdh_set_peer
},
538 { OSSL_FUNC_KEYEXCH_FREECTX
, (void (*)(void))ecdh_freectx
},
539 { OSSL_FUNC_KEYEXCH_DUPCTX
, (void (*)(void))ecdh_dupctx
},
540 { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS
, (void (*)(void))ecdh_set_ctx_params
},
541 { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS
,
542 (void (*)(void))ecdh_settable_ctx_params
},
543 { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS
, (void (*)(void))ecdh_get_ctx_params
},
544 { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS
,
545 (void (*)(void))ecdh_gettable_ctx_params
},