Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/exchange/ecx_exch.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2025 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
/* clang-format off */
10
11
/* clang-format on */
12
13
#include <openssl/crypto.h>
14
#include <openssl/core_dispatch.h>
15
#include <openssl/core_names.h>
16
#include <openssl/params.h>
17
#include <openssl/err.h>
18
#include <openssl/proverr.h>
19
#include "internal/cryptlib.h"
20
#include "crypto/ecx.h"
21
#include "prov/implementations.h"
22
#include "prov/providercommon.h"
23
#include "prov/securitycheck.h"
24
25
static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
26
static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
27
static OSSL_FUNC_keyexch_init_fn x25519_init;
28
static OSSL_FUNC_keyexch_init_fn x448_init;
29
static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
30
static OSSL_FUNC_keyexch_derive_fn ecx_derive;
31
static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
32
static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
33
static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecx_gettable_ctx_params;
34
static OSSL_FUNC_keyexch_get_ctx_params_fn ecx_get_ctx_params;
35
36
/*
37
 * What's passed as an actual key is defined by the KEYMGMT interface.
38
 * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
39
 * we use that here too.
40
 */
41
42
typedef struct {
43
    size_t keylen;
44
    ECX_KEY *key;
45
    ECX_KEY *peerkey;
46
} PROV_ECX_CTX;
47
48
static void *ecx_newctx(void *provctx, size_t keylen)
49
32.2k
{
50
32.2k
    PROV_ECX_CTX *ctx;
51
52
32.2k
    if (!ossl_prov_is_running())
53
0
        return NULL;
54
55
32.2k
    ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
56
32.2k
    if (ctx == NULL)
57
0
        return NULL;
58
59
32.2k
    ctx->keylen = keylen;
60
61
32.2k
    return ctx;
62
32.2k
}
63
64
static void *x25519_newctx(void *provctx)
65
32.2k
{
66
32.2k
    return ecx_newctx(provctx, X25519_KEYLEN);
67
32.2k
}
68
69
static void *x448_newctx(void *provctx)
70
33
{
71
33
    return ecx_newctx(provctx, X448_KEYLEN);
72
33
}
73
74
static int ecx_init(void *vecxctx, void *vkey, const char *algname)
75
32.2k
{
76
32.2k
    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
77
32.2k
    ECX_KEY *key = vkey;
78
79
32.2k
    if (!ossl_prov_is_running())
80
0
        return 0;
81
82
32.2k
    if (ecxctx == NULL
83
32.2k
        || key == NULL
84
32.2k
        || key->keylen != ecxctx->keylen
85
32.2k
        || !ossl_ecx_key_up_ref(key)) {
86
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
87
0
        return 0;
88
0
    }
89
90
32.2k
    ossl_ecx_key_free(ecxctx->key);
91
32.2k
    ecxctx->key = key;
92
93
#ifdef FIPS_MODULE
94
    if (!ossl_FIPS_IND_callback(key->libctx, algname, "Init"))
95
        return 0;
96
#endif
97
32.2k
    return 1;
98
32.2k
}
99
100
static int x25519_init(void *vecxctx, void *vkey,
101
    ossl_unused const OSSL_PARAM params[])
102
24.7k
{
103
24.7k
    return ecx_init(vecxctx, vkey, "X25519");
104
24.7k
}
105
106
static int x448_init(void *vecxctx, void *vkey,
107
    ossl_unused const OSSL_PARAM params[])
108
22
{
109
22
    return ecx_init(vecxctx, vkey, "X448");
110
22
}
111
112
static int ecx_set_peer(void *vecxctx, void *vkey)
113
32.2k
{
114
32.2k
    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
115
32.2k
    ECX_KEY *key = vkey;
116
117
32.2k
    if (!ossl_prov_is_running())
118
0
        return 0;
119
120
32.2k
    if (ecxctx == NULL
121
32.2k
        || key == NULL
122
32.2k
        || key->keylen != ecxctx->keylen
123
32.2k
        || !ossl_ecx_key_up_ref(key)) {
124
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
125
0
        return 0;
126
0
    }
127
32.2k
    ossl_ecx_key_free(ecxctx->peerkey);
128
32.2k
    ecxctx->peerkey = key;
129
130
32.2k
    return 1;
131
32.2k
}
132
133
static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
134
    size_t outlen)
135
62.6k
{
136
62.6k
    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
137
138
62.6k
    if (!ossl_prov_is_running())
139
0
        return 0;
140
62.6k
    return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen,
141
62.6k
        secret, secretlen, outlen);
142
62.6k
}
143
144
static void ecx_freectx(void *vecxctx)
145
32.2k
{
146
32.2k
    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
147
148
32.2k
    ossl_ecx_key_free(ecxctx->key);
149
32.2k
    ossl_ecx_key_free(ecxctx->peerkey);
150
151
32.2k
    OPENSSL_free(ecxctx);
152
32.2k
}
153
154
static void *ecx_dupctx(void *vecxctx)
155
0
{
156
0
    PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
157
0
    PROV_ECX_CTX *dstctx;
158
159
0
    if (!ossl_prov_is_running())
160
0
        return NULL;
161
162
0
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
163
0
    if (dstctx == NULL)
164
0
        return NULL;
165
166
0
    *dstctx = *srcctx;
167
0
    if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
168
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
169
0
        OPENSSL_free(dstctx);
170
0
        return NULL;
171
0
    }
172
173
0
    if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
174
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
175
0
        ossl_ecx_key_free(dstctx->key);
176
0
        OPENSSL_free(dstctx);
177
0
        return NULL;
178
0
    }
179
180
0
    return dstctx;
181
0
}
182
183
#ifdef FIPS_MODULE
184
/* clang-format off */
185
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
186
#ifndef ecx_get_ctx_params_list
187
static const OSSL_PARAM ecx_get_ctx_params_list[] = {
188
# if defined(FIPS_MODULE)
189
    OSSL_PARAM_int(OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR, NULL),
190
# endif
191
    OSSL_PARAM_END
192
};
193
#endif
194
195
#ifndef ecx_get_ctx_params_st
196
struct ecx_get_ctx_params_st {
197
# if defined(FIPS_MODULE)
198
    OSSL_PARAM *ind;
199
# else
200
    int dummy; /* unused */
201
# endif
202
};
203
#endif
204
205
#ifndef ecx_get_ctx_params_decoder
206
static int ecx_get_ctx_params_decoder
207
    (const OSSL_PARAM *p, struct ecx_get_ctx_params_st *r)
208
{
209
    const char *s;
210
211
    memset(r, 0, sizeof(*r));
212
    if (p != NULL)
213
        for (; (s = p->key) != NULL; p++)
214
# if defined(FIPS_MODULE)
215
            if (ossl_likely(strcmp("fips-indicator", s + 0) == 0)) {
216
                /* OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR */
217
                if (ossl_unlikely(r->ind != NULL)) {
218
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
219
                                   "param %s is repeated", s);
220
                    return 0;
221
                }
222
                r->ind = (OSSL_PARAM *)p;
223
            }
224
# else
225
            ;
226
# endif
227
    return 1;
228
}
229
#endif
230
/* End of machine generated */
231
/* clang-format on */
232
#endif
233
234
static const OSSL_PARAM *ecx_gettable_ctx_params(ossl_unused void *vctx,
235
    ossl_unused void *provctx)
236
0
{
237
#ifdef FIPS_MODULE
238
    return ecx_get_ctx_params_list;
239
#else
240
0
    static OSSL_PARAM params[] = { OSSL_PARAM_END };
241
242
0
    return params;
243
0
#endif
244
0
}
245
246
static int ecx_get_ctx_params(ossl_unused void *vctx, OSSL_PARAM params[])
247
0
{
248
#ifdef FIPS_MODULE
249
    int approved = 0;
250
    struct ecx_get_ctx_params_st p;
251
252
    if (vctx == NULL || !ecx_get_ctx_params_decoder(params, &p))
253
        return 0;
254
255
    if (p.ind != NULL && !OSSL_PARAM_set_int(p.ind, approved))
256
        return 0;
257
#endif
258
0
    return 1;
259
0
}
260
261
const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
262
    { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
263
    { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x25519_init },
264
    { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
265
    { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
266
    { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
267
    { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
268
    { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params },
269
    { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
270
        (void (*)(void))ecx_gettable_ctx_params },
271
    OSSL_DISPATCH_END
272
};
273
274
const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
275
    { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
276
    { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x448_init },
277
    { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
278
    { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
279
    { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
280
    { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
281
    { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params },
282
    { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
283
        (void (*)(void))ecx_gettable_ctx_params },
284
    OSSL_DISPATCH_END
285
};