2 * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/crypto.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/core_names.h>
13 #include <openssl/params.h>
14 #include <openssl/err.h>
15 #include <openssl/proverr.h>
16 #include "internal/cryptlib.h"
17 #include "crypto/ecx.h"
18 #include "prov/implementations.h"
19 #include "prov/providercommon.h"
20 #include "prov/securitycheck.h"
22 static OSSL_FUNC_keyexch_newctx_fn x25519_newctx
;
23 static OSSL_FUNC_keyexch_newctx_fn x448_newctx
;
24 static OSSL_FUNC_keyexch_init_fn x25519_init
;
25 static OSSL_FUNC_keyexch_init_fn x448_init
;
26 static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer
;
27 static OSSL_FUNC_keyexch_derive_fn ecx_derive
;
28 static OSSL_FUNC_keyexch_freectx_fn ecx_freectx
;
29 static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx
;
30 static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecx_gettable_ctx_params
;
31 static OSSL_FUNC_keyexch_get_ctx_params_fn ecx_get_ctx_params
;
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.
45 static void *ecx_newctx(void *provctx
, size_t keylen
)
49 if (!ossl_prov_is_running())
52 ctx
= OPENSSL_zalloc(sizeof(PROV_ECX_CTX
));
61 static void *x25519_newctx(void *provctx
)
63 return ecx_newctx(provctx
, X25519_KEYLEN
);
66 static void *x448_newctx(void *provctx
)
68 return ecx_newctx(provctx
, X448_KEYLEN
);
71 static int ecx_init(void *vecxctx
, void *vkey
, const char *algname
)
73 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
76 if (!ossl_prov_is_running())
81 || key
->keylen
!= ecxctx
->keylen
82 || !ossl_ecx_key_up_ref(key
)) {
83 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
87 ossl_ecx_key_free(ecxctx
->key
);
91 if (!ossl_FIPS_IND_callback(key
->libctx
, algname
, "Init"))
97 static int x25519_init(void *vecxctx
, void *vkey
,
98 ossl_unused
const OSSL_PARAM params
[])
100 return ecx_init(vecxctx
, vkey
, "X25519");
103 static int x448_init(void *vecxctx
, void *vkey
,
104 ossl_unused
const OSSL_PARAM params
[])
106 return ecx_init(vecxctx
, vkey
, "X448");
109 static int ecx_set_peer(void *vecxctx
, void *vkey
)
111 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
114 if (!ossl_prov_is_running())
119 || key
->keylen
!= ecxctx
->keylen
120 || !ossl_ecx_key_up_ref(key
)) {
121 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
124 ossl_ecx_key_free(ecxctx
->peerkey
);
125 ecxctx
->peerkey
= key
;
130 static int ecx_derive(void *vecxctx
, unsigned char *secret
, size_t *secretlen
,
133 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
135 if (!ossl_prov_is_running())
137 return ossl_ecx_compute_key(ecxctx
->peerkey
, ecxctx
->key
, ecxctx
->keylen
,
138 secret
, secretlen
, outlen
);
141 static void ecx_freectx(void *vecxctx
)
143 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
145 ossl_ecx_key_free(ecxctx
->key
);
146 ossl_ecx_key_free(ecxctx
->peerkey
);
148 OPENSSL_free(ecxctx
);
151 static void *ecx_dupctx(void *vecxctx
)
153 PROV_ECX_CTX
*srcctx
= (PROV_ECX_CTX
*)vecxctx
;
154 PROV_ECX_CTX
*dstctx
;
156 if (!ossl_prov_is_running())
159 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
164 if (dstctx
->key
!= NULL
&& !ossl_ecx_key_up_ref(dstctx
->key
)) {
165 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
166 OPENSSL_free(dstctx
);
170 if (dstctx
->peerkey
!= NULL
&& !ossl_ecx_key_up_ref(dstctx
->peerkey
)) {
171 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
172 ossl_ecx_key_free(dstctx
->key
);
173 OPENSSL_free(dstctx
);
180 static const OSSL_PARAM
*ecx_gettable_ctx_params(ossl_unused
void *vctx
,
181 ossl_unused
void *provctx
)
183 static const OSSL_PARAM known_gettable_ctx_params
[] = {
184 OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
187 return known_gettable_ctx_params
;
190 static int ecx_get_ctx_params(ossl_unused
void *vctx
, OSSL_PARAM params
[])
194 OSSL_PARAM
*p
= OSSL_PARAM_locate(params
,
195 OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR
);
197 if (p
!= NULL
&& !OSSL_PARAM_set_int(p
, approved
))
203 const OSSL_DISPATCH ossl_x25519_keyexch_functions
[] = {
204 { OSSL_FUNC_KEYEXCH_NEWCTX
, (void (*)(void))x25519_newctx
},
205 { OSSL_FUNC_KEYEXCH_INIT
, (void (*)(void))x25519_init
},
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
},
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
},
216 const OSSL_DISPATCH ossl_x448_keyexch_functions
[] = {
217 { OSSL_FUNC_KEYEXCH_NEWCTX
, (void (*)(void))x448_newctx
},
218 { OSSL_FUNC_KEYEXCH_INIT
, (void (*)(void))x448_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
},