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{-
10use OpenSSL::paramnames qw(produce_param_decoder);
11-}
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
25static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
26static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
27static OSSL_FUNC_keyexch_init_fn x25519_init;
28static OSSL_FUNC_keyexch_init_fn x448_init;
29static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
30static OSSL_FUNC_keyexch_derive_fn ecx_derive;
31static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
32static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
33static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecx_gettable_ctx_params;
34static 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
42typedef struct {
43    size_t keylen;
44    ECX_KEY *key;
45    ECX_KEY *peerkey;
46} PROV_ECX_CTX;
47
48static void *ecx_newctx(void *provctx, size_t keylen)
49{
50    PROV_ECX_CTX *ctx;
51
52    if (!ossl_prov_is_running())
53        return NULL;
54
55    ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
56    if (ctx == NULL)
57        return NULL;
58
59    ctx->keylen = keylen;
60
61    return ctx;
62}
63
64static void *x25519_newctx(void *provctx)
65{
66    return ecx_newctx(provctx, X25519_KEYLEN);
67}
68
69static void *x448_newctx(void *provctx)
70{
71    return ecx_newctx(provctx, X448_KEYLEN);
72}
73
74static int ecx_init(void *vecxctx, void *vkey, const char *algname)
75{
76    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
77    ECX_KEY *key = vkey;
78
79    if (!ossl_prov_is_running())
80        return 0;
81
82    if (ecxctx == NULL
83            || key == NULL
84            || key->keylen != ecxctx->keylen
85            || !ossl_ecx_key_up_ref(key)) {
86        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
87        return 0;
88    }
89
90    ossl_ecx_key_free(ecxctx->key);
91    ecxctx->key = key;
92
93#ifdef FIPS_MODULE
94    if (!ossl_FIPS_IND_callback(key->libctx, algname, "Init"))
95        return 0;
96#endif
97    return 1;
98}
99
100static int x25519_init(void *vecxctx, void *vkey,
101                       ossl_unused const OSSL_PARAM params[])
102{
103    return ecx_init(vecxctx, vkey, "X25519");
104}
105
106static int x448_init(void *vecxctx, void *vkey,
107                     ossl_unused const OSSL_PARAM params[])
108{
109    return ecx_init(vecxctx, vkey, "X448");
110}
111
112static int ecx_set_peer(void *vecxctx, void *vkey)
113{
114    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
115    ECX_KEY *key = vkey;
116
117    if (!ossl_prov_is_running())
118        return 0;
119
120    if (ecxctx == NULL
121            || key == NULL
122            || key->keylen != ecxctx->keylen
123            || !ossl_ecx_key_up_ref(key)) {
124        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
125        return 0;
126    }
127    ossl_ecx_key_free(ecxctx->peerkey);
128    ecxctx->peerkey = key;
129
130    return 1;
131}
132
133static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
134                      size_t outlen)
135{
136    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
137
138    if (!ossl_prov_is_running())
139        return 0;
140    return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen,
141                                secret, secretlen, outlen);
142}
143
144static void ecx_freectx(void *vecxctx)
145{
146    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
147
148    ossl_ecx_key_free(ecxctx->key);
149    ossl_ecx_key_free(ecxctx->peerkey);
150
151    OPENSSL_free(ecxctx);
152}
153
154static void *ecx_dupctx(void *vecxctx)
155{
156    PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
157    PROV_ECX_CTX *dstctx;
158
159    if (!ossl_prov_is_running())
160        return NULL;
161
162    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
163    if (dstctx == NULL)
164        return NULL;
165
166    *dstctx = *srcctx;
167    if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
168        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
169        OPENSSL_free(dstctx);
170        return NULL;
171    }
172
173    if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
174        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
175        ossl_ecx_key_free(dstctx->key);
176        OPENSSL_free(dstctx);
177        return NULL;
178    }
179
180    return dstctx;
181}
182
183#ifdef FIPS_MODULE
184{- produce_param_decoder('ecx_get_ctx_params',
185                         (['ALG_PARAM_FIPS_APPROVED_INDICATOR', 'ind', 'int', 'fips'],
186                         )); -}
187#endif
188
189static const OSSL_PARAM *ecx_gettable_ctx_params(ossl_unused void *vctx,
190                                                 ossl_unused void *provctx)
191{
192#ifdef FIPS_MODULE
193    return ecx_get_ctx_params_list;
194#else
195    static OSSL_PARAM params[] = { OSSL_PARAM_END };
196
197    return params;
198#endif
199}
200
201static int ecx_get_ctx_params(ossl_unused void *vctx, OSSL_PARAM params[])
202{
203#ifdef FIPS_MODULE
204    int approved = 0;
205    struct ecx_get_ctx_params_st p;
206
207    if (vctx == NULL || !ecx_get_ctx_params_decoder(params, &p))
208        return 0;
209
210    if (p.ind != NULL && !OSSL_PARAM_set_int(p.ind, approved))
211        return 0;
212#endif
213    return 1;
214}
215
216const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
217    { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
218    { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x25519_init },
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 },
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 },
226    OSSL_DISPATCH_END
227};
228
229const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
230    { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
231    { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x448_init },
232    { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
233    { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
234    { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
235    { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
236    { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params },
237    { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
238      (void (*)(void))ecx_gettable_ctx_params },
239    OSSL_DISPATCH_END
240};
241