Providers: move default kdfs,macs
[openssl.git] / providers / implementations / exchange / dh_exch.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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"
16
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;
23 static OSSL_OP_keyexch_set_ctx_params_fn dh_set_ctx_params;
24 static OSSL_OP_keyexch_settable_ctx_params_fn dh_settable_ctx_params;
25
26 /*
27  * What's passed as an actual key is defined by the KEYMGMT interface.
28  * We happen to know that our KEYMGMT simply passes DH structures, so
29  * we use that here too.
30  */
31
32 typedef struct {
33     DH *dh;
34     DH *dhpeer;
35     unsigned int pad : 1;
36 } PROV_DH_CTX;
37
38 static void *dh_newctx(void *provctx)
39 {
40     return OPENSSL_zalloc(sizeof(PROV_DH_CTX));
41 }
42
43 static int dh_init(void *vpdhctx, void *vdh)
44 {
45     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
46
47     if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
48         return 0;
49     DH_free(pdhctx->dh);
50     pdhctx->dh = vdh;
51     return 1;
52 }
53
54 static int dh_set_peer(void *vpdhctx, void *vdh)
55 {
56     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
57
58     if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
59         return 0;
60     DH_free(pdhctx->dhpeer);
61     pdhctx->dhpeer = vdh;
62     return 1;
63 }
64
65 static int dh_derive(void *vpdhctx, unsigned char *secret, size_t *secretlen,
66                      size_t outlen)
67 {
68     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
69     int ret;
70     size_t dhsize;
71     const BIGNUM *pub_key = NULL;
72
73     /* TODO(3.0): Add errors to stack */
74     if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL)
75         return 0;
76
77     dhsize = (size_t)DH_size(pdhctx->dh);
78     if (secret == NULL) {
79         *secretlen = dhsize;
80         return 1;
81     }
82     if (outlen < dhsize)
83         return 0;
84
85     DH_get0_key(pdhctx->dhpeer, &pub_key, NULL);
86     ret = (pdhctx->pad) ? DH_compute_key_padded(secret, pub_key, pdhctx->dh)
87                         : DH_compute_key(secret, pub_key, pdhctx->dh);
88     if (ret <= 0)
89         return 0;
90
91     *secretlen = ret;
92     return 1;
93 }
94
95 static void dh_freectx(void *vpdhctx)
96 {
97     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
98
99     DH_free(pdhctx->dh);
100     DH_free(pdhctx->dhpeer);
101
102     OPENSSL_free(pdhctx);
103 }
104
105 static void *dh_dupctx(void *vpdhctx)
106 {
107     PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx;
108     PROV_DH_CTX *dstctx;
109
110     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
111     if (dstctx == NULL)
112         return NULL;
113
114     *dstctx = *srcctx;
115     if (dstctx->dh != NULL && !DH_up_ref(dstctx->dh)) {
116         OPENSSL_free(dstctx);
117         return NULL;
118     }
119
120     if (dstctx->dhpeer != NULL && !DH_up_ref(dstctx->dhpeer)) {
121         DH_free(dstctx->dh);
122         OPENSSL_free(dstctx);
123         return NULL;
124     }
125
126     return dstctx;
127 }
128
129 static int dh_set_ctx_params(void *vpdhctx, const OSSL_PARAM params[])
130 {
131     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
132     const OSSL_PARAM *p;
133     unsigned int pad;
134
135     if (pdhctx == NULL || params == NULL)
136         return 0;
137
138     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_PAD);
139     if (p == NULL || !OSSL_PARAM_get_uint(p, &pad))
140         return 0;
141     pdhctx->pad = pad ? 1 : 0;
142     return 1;
143 }
144
145 static const OSSL_PARAM known_settable_ctx_params[] = {
146     OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_PAD, NULL),
147     OSSL_PARAM_END
148 };
149
150 static const OSSL_PARAM *dh_settable_ctx_params(void)
151 {
152     return known_settable_ctx_params;
153 }
154
155 const OSSL_DISPATCH dh_keyexch_functions[] = {
156     { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx },
157     { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init },
158     { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive },
159     { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer },
160     { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx },
161     { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx },
162     { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))dh_set_ctx_params },
163     { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS,
164       (void (*)(void))dh_settable_ctx_params },
165     { 0, NULL }
166 };