]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/exchange/ecx_exch.c
Fix some conversion from size_t to const int errors
[thirdparty/openssl.git] / providers / implementations / exchange / ecx_exch.c
CommitLineData
6f7d2135 1/*
7ed6de99 2 * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
6f7d2135
MC
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>
23c48d94 11#include <openssl/core_dispatch.h>
6f7d2135
MC
12#include <openssl/core_names.h>
13#include <openssl/params.h>
14#include <openssl/err.h>
2741128e 15#include <openssl/proverr.h>
6f7d2135
MC
16#include "internal/cryptlib.h"
17#include "crypto/ecx.h"
18#include "prov/implementations.h"
ca94057f 19#include "prov/providercommon.h"
f6a296c3 20#include "prov/securitycheck.h"
6f7d2135 21
363b1e5d
DMSP
22static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
23static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
c37e2176 24static OSSL_FUNC_keyexch_init_fn x25519_init;
25static OSSL_FUNC_keyexch_init_fn x448_init;
363b1e5d
DMSP
26static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
27static OSSL_FUNC_keyexch_derive_fn ecx_derive;
28static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
29static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
c37e2176 30static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecx_gettable_ctx_params;
31static OSSL_FUNC_keyexch_get_ctx_params_fn ecx_get_ctx_params;
6f7d2135
MC
32
33/*
34 * What's passed as an actual key is defined by the KEYMGMT interface.
35 * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
36 * we use that here too.
37 */
38
39typedef struct {
40 size_t keylen;
41 ECX_KEY *key;
42 ECX_KEY *peerkey;
43} PROV_ECX_CTX;
44
45static void *ecx_newctx(void *provctx, size_t keylen)
46{
ca94057f 47 PROV_ECX_CTX *ctx;
6f7d2135 48
ca94057f
P
49 if (!ossl_prov_is_running())
50 return NULL;
51
52 ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
e077455e 53 if (ctx == NULL)
6f7d2135 54 return NULL;
6f7d2135
MC
55
56 ctx->keylen = keylen;
57
58 return ctx;
59}
60
61static void *x25519_newctx(void *provctx)
62{
63 return ecx_newctx(provctx, X25519_KEYLEN);
64}
65
66static void *x448_newctx(void *provctx)
67{
68 return ecx_newctx(provctx, X448_KEYLEN);
69}
70
c37e2176 71static int ecx_init(void *vecxctx, void *vkey, const char *algname)
6f7d2135
MC
72{
73 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
74 ECX_KEY *key = vkey;
75
ca94057f
P
76 if (!ossl_prov_is_running())
77 return 0;
78
6f7d2135
MC
79 if (ecxctx == NULL
80 || key == NULL
81 || key->keylen != ecxctx->keylen
32ab57cb 82 || !ossl_ecx_key_up_ref(key)) {
6f7d2135
MC
83 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
84 return 0;
85 }
86
32ab57cb 87 ossl_ecx_key_free(ecxctx->key);
6f7d2135
MC
88 ecxctx->key = key;
89
c37e2176 90#ifdef FIPS_MODULE
91 if (!ossl_FIPS_IND_callback(key->libctx, algname, "Init"))
92 return 0;
93#endif
6f7d2135
MC
94 return 1;
95}
96
c37e2176 97static int x25519_init(void *vecxctx, void *vkey,
98 ossl_unused const OSSL_PARAM params[])
99{
100 return ecx_init(vecxctx, vkey, "X25519");
101}
102
103static int x448_init(void *vecxctx, void *vkey,
104 ossl_unused const OSSL_PARAM params[])
105{
106 return ecx_init(vecxctx, vkey, "X448");
107}
108
6f7d2135
MC
109static int ecx_set_peer(void *vecxctx, void *vkey)
110{
111 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
112 ECX_KEY *key = vkey;
113
ca94057f
P
114 if (!ossl_prov_is_running())
115 return 0;
116
6f7d2135
MC
117 if (ecxctx == NULL
118 || key == NULL
119 || key->keylen != ecxctx->keylen
32ab57cb 120 || !ossl_ecx_key_up_ref(key)) {
6f7d2135
MC
121 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
122 return 0;
123 }
32ab57cb 124 ossl_ecx_key_free(ecxctx->peerkey);
6f7d2135
MC
125 ecxctx->peerkey = key;
126
127 return 1;
128}
129
130static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
131 size_t outlen)
132{
133 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
134
ca94057f
P
135 if (!ossl_prov_is_running())
136 return 0;
78c44b05 137 return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen,
138 secret, secretlen, outlen);
6f7d2135
MC
139}
140
141static void ecx_freectx(void *vecxctx)
142{
143 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
144
32ab57cb
SL
145 ossl_ecx_key_free(ecxctx->key);
146 ossl_ecx_key_free(ecxctx->peerkey);
6f7d2135
MC
147
148 OPENSSL_free(ecxctx);
149}
150
151static void *ecx_dupctx(void *vecxctx)
152{
153 PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
154 PROV_ECX_CTX *dstctx;
155
ca94057f
P
156 if (!ossl_prov_is_running())
157 return NULL;
158
6f7d2135 159 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
e077455e 160 if (dstctx == NULL)
6f7d2135 161 return NULL;
6f7d2135
MC
162
163 *dstctx = *srcctx;
32ab57cb 164 if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
6f7d2135
MC
165 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
166 OPENSSL_free(dstctx);
167 return NULL;
168 }
169
32ab57cb 170 if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
6f7d2135 171 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
32ab57cb 172 ossl_ecx_key_free(dstctx->key);
6f7d2135
MC
173 OPENSSL_free(dstctx);
174 return NULL;
175 }
176
177 return dstctx;
178}
179
c37e2176 180static const OSSL_PARAM *ecx_gettable_ctx_params(ossl_unused void *vctx,
181 ossl_unused void *provctx)
182{
183 static const OSSL_PARAM known_gettable_ctx_params[] = {
184 OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
185 OSSL_PARAM_END
186 };
187 return known_gettable_ctx_params;
188}
189
190static int ecx_get_ctx_params(ossl_unused void *vctx, OSSL_PARAM params[])
191{
192#ifdef FIPS_MODULE
193 int approved = 0;
194 OSSL_PARAM *p = OSSL_PARAM_locate(params,
195 OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR);
196
197 if (p != NULL && !OSSL_PARAM_set_int(p, approved))
198 return 0;
199#endif
200 return 1;
201}
202
1be63951 203const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
6f7d2135 204 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
c37e2176 205 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x25519_init },
6f7d2135
MC
206 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
207 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
208 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
209 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
c37e2176 210 { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params },
211 { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
212 (void (*)(void))ecx_gettable_ctx_params },
1e6bd31e 213 OSSL_DISPATCH_END
6f7d2135
MC
214};
215
1be63951 216const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
6f7d2135 217 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
c37e2176 218 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x448_init },
6f7d2135
MC
219 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
220 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
221 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
222 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
c37e2176 223 { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params },
224 { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
225 (void (*)(void))ecx_gettable_ctx_params },
1e6bd31e 226 OSSL_DISPATCH_END
6f7d2135 227};