2 * Copyright 2019 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/crypto.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/dh.h>
14 #include <openssl/params.h>
15 #include "internal/provider_algs.h"
17 static OSSL_OP_keyexch_newctx_fn dh_newctx
;
18 static OSSL_OP_keyexch_init_fn dh_init
;
19 static OSSL_OP_keyexch_set_peer_fn dh_set_peer
;
20 static OSSL_OP_keyexch_derive_fn dh_derive
;
21 static OSSL_OP_keyexch_freectx_fn dh_freectx
;
22 static OSSL_OP_keyexch_dupctx_fn dh_dupctx
;
31 static void *dh_newctx(void *provctx
)
33 return OPENSSL_zalloc(sizeof(PROV_DH_CTX
));
36 static DH
*param_to_dh(OSSL_PARAM params
[], int priv
)
40 BIGNUM
*p
= NULL
, *g
= NULL
, *pub_key
= NULL
, *priv_key
= NULL
;
45 paramptr
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DH_P
);
47 || !OSSL_PARAM_get_BN(paramptr
, &p
))
50 paramptr
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DH_G
);
51 if (paramptr
== NULL
|| !OSSL_PARAM_get_BN(paramptr
, &g
))
54 if (!DH_set0_pqg(dh
, p
, NULL
, g
))
58 paramptr
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DH_PUB_KEY
);
59 if (paramptr
== NULL
|| !OSSL_PARAM_get_BN(paramptr
, &pub_key
))
62 /* Private key is optional */
64 paramptr
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DH_PRIV_KEY
);
66 || (priv_key
= BN_secure_new()) == NULL
67 || !OSSL_PARAM_get_BN(paramptr
, &priv_key
))
71 if (!DH_set0_key(dh
, pub_key
, priv_key
))
85 static int dh_init(void *vpdhctx
, OSSL_PARAM params
[])
87 PROV_DH_CTX
*pdhctx
= (PROV_DH_CTX
*)vpdhctx
;
90 pdhctx
->dh
= param_to_dh(params
, 1);
92 return pdhctx
->dh
!= NULL
;
95 static int dh_set_peer(void *vpdhctx
, OSSL_PARAM params
[])
97 PROV_DH_CTX
*pdhctx
= (PROV_DH_CTX
*)vpdhctx
;
99 DH_free(pdhctx
->dhpeer
);
100 pdhctx
->dhpeer
= param_to_dh(params
, 0);
102 return pdhctx
->dhpeer
!= NULL
;
105 static int dh_derive(void *vpdhctx
, unsigned char *key
, size_t *keylen
,
108 PROV_DH_CTX
*pdhctx
= (PROV_DH_CTX
*)vpdhctx
;
111 const BIGNUM
*pub_key
= NULL
;
113 /* TODO(3.0): Add errors to stack */
114 if (pdhctx
->dh
== NULL
|| pdhctx
->dhpeer
== NULL
)
117 dhsize
= (size_t)DH_size(pdhctx
->dh
);
125 DH_get0_key(pdhctx
->dhpeer
, &pub_key
, NULL
);
126 ret
= (pdhctx
->pad
) ? DH_compute_key_padded(key
, pub_key
, pdhctx
->dh
)
127 : DH_compute_key(key
, pub_key
, pdhctx
->dh
);
135 static void dh_freectx(void *vpdhctx
)
137 PROV_DH_CTX
*pdhctx
= (PROV_DH_CTX
*)vpdhctx
;
140 DH_free(pdhctx
->dhpeer
);
142 OPENSSL_free(pdhctx
);
145 static void *dh_dupctx(void *vpdhctx
)
147 PROV_DH_CTX
*srcctx
= (PROV_DH_CTX
*)vpdhctx
;
150 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
153 if (dstctx
->dh
!= NULL
&& !DH_up_ref(dstctx
->dh
)) {
154 OPENSSL_free(dstctx
);
158 if (dstctx
->dhpeer
!= NULL
&& !DH_up_ref(dstctx
->dhpeer
)) {
160 OPENSSL_free(dstctx
);
167 static int dh_set_params(void *vpdhctx
, OSSL_PARAM params
[])
169 PROV_DH_CTX
*pdhctx
= (PROV_DH_CTX
*)vpdhctx
;
173 if (pdhctx
== NULL
|| params
== NULL
)
176 p
= OSSL_PARAM_locate(params
, OSSL_EXCHANGE_PARAM_PAD
);
177 if (p
== NULL
|| !OSSL_PARAM_get_int(p
, &pad
))
185 const OSSL_DISPATCH dh_functions
[] = {
186 { OSSL_FUNC_KEYEXCH_NEWCTX
, (void (*)(void))dh_newctx
},
187 { OSSL_FUNC_KEYEXCH_INIT
, (void (*)(void))dh_init
},
188 { OSSL_FUNC_KEYEXCH_DERIVE
, (void (*)(void))dh_derive
},
189 { OSSL_FUNC_KEYEXCH_SET_PEER
, (void (*)(void))dh_set_peer
},
190 { OSSL_FUNC_KEYEXCH_FREECTX
, (void (*)(void))dh_freectx
},
191 { OSSL_FUNC_KEYEXCH_DUPCTX
, (void (*)(void))dh_dupctx
},
192 { OSSL_FUNC_KEYEXCH_SET_PARAMS
, (void (*)(void))dh_set_params
},