Coverage Report

Created: 2025-08-25 06:30

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