2 * Copyright 2020 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 <openssl/core_names.h>
11 #include "internal/param_build_set.h"
12 #include "crypto/dh.h"
15 * The intention with the "backend" source file is to offer backend functions
16 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
17 * implementations alike.
20 static int dh_ffc_params_fromdata(DH
*dh
, const OSSL_PARAM params
[])
27 ffc
= dh_get0_params(dh
);
31 ret
= ossl_ffc_params_fromdata(ffc
, params
);
33 dh_cache_named_group(dh
); /* This increments dh->dirty_cnt */
37 int dh_params_fromdata(DH
*dh
, const OSSL_PARAM params
[])
39 const OSSL_PARAM
*param_priv_len
;
42 if (!dh_ffc_params_fromdata(dh
, params
))
46 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_DH_PRIV_LEN
);
47 if (param_priv_len
!= NULL
48 && (!OSSL_PARAM_get_long(param_priv_len
, &priv_len
)
49 || !DH_set_length(dh
, priv_len
)))
55 int dh_key_fromdata(DH
*dh
, const OSSL_PARAM params
[])
57 const OSSL_PARAM
*param_priv_key
, *param_pub_key
;
58 BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
63 param_priv_key
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
64 param_pub_key
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PUB_KEY
);
67 * DH documentation says that a public key must be present if a
68 * private key is present.
69 * We want to have at least a public key either way, so we end up
70 * requiring it unconditionally.
72 if (param_priv_key
!= NULL
&& param_pub_key
== NULL
)
75 if ((param_priv_key
!= NULL
76 && !OSSL_PARAM_get_BN(param_priv_key
, &priv_key
))
77 || (param_pub_key
!= NULL
78 && !OSSL_PARAM_get_BN(param_pub_key
, &pub_key
)))
81 if (!DH_set0_key(dh
, pub_key
, priv_key
))
87 BN_clear_free(priv_key
);
92 int dh_params_todata(DH
*dh
, OSSL_PARAM_BLD
*bld
, OSSL_PARAM params
[])
94 long l
= DH_get_length(dh
);
96 if (!ossl_ffc_params_todata(dh_get0_params(dh
), bld
, params
))
99 && !ossl_param_build_set_long(bld
, params
, OSSL_PKEY_PARAM_DH_PRIV_LEN
, l
))
104 int dh_key_todata(DH
*dh
, OSSL_PARAM_BLD
*bld
, OSSL_PARAM params
[])
106 const BIGNUM
*priv
= NULL
, *pub
= NULL
;
111 DH_get0_key(dh
, &pub
, &priv
);
113 && !ossl_param_build_set_bn(bld
, params
, OSSL_PKEY_PARAM_PRIV_KEY
, priv
))
116 && !ossl_param_build_set_bn(bld
, params
, OSSL_PKEY_PARAM_PUB_KEY
, pub
))