/src/openssl40/providers/implementations/kem/mlx_kem.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2026 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 | | #include <string.h> |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/crypto.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/evp.h> |
16 | | #include <openssl/obj_mac.h> |
17 | | #include <openssl/params.h> |
18 | | #include <openssl/proverr.h> |
19 | | #include <openssl/rand.h> |
20 | | #include "internal/cryptlib.h" |
21 | | #include "prov/implementations.h" |
22 | | #include "prov/names.h" |
23 | | #include "prov/mlx_kem.h" |
24 | | #include "prov/provider_ctx.h" |
25 | | #include "prov/providercommon.h" |
26 | | #include "providers/implementations/kem/mlx_kem.inc" |
27 | | |
28 | | static OSSL_FUNC_kem_newctx_fn mlx_kem_newctx; |
29 | | static OSSL_FUNC_kem_freectx_fn mlx_kem_freectx; |
30 | | static OSSL_FUNC_kem_encapsulate_init_fn mlx_kem_encapsulate_init; |
31 | | static OSSL_FUNC_kem_encapsulate_fn mlx_kem_encapsulate; |
32 | | static OSSL_FUNC_kem_decapsulate_init_fn mlx_kem_decapsulate_init; |
33 | | static OSSL_FUNC_kem_decapsulate_fn mlx_kem_decapsulate; |
34 | | static OSSL_FUNC_kem_set_ctx_params_fn mlx_kem_set_ctx_params; |
35 | | static OSSL_FUNC_kem_settable_ctx_params_fn mlx_kem_settable_ctx_params; |
36 | | #ifdef FIPS_MODULE |
37 | | static OSSL_FUNC_kem_get_ctx_params_fn mlx_kem_get_ctx_params; |
38 | | static OSSL_FUNC_kem_gettable_ctx_params_fn mlx_kem_gettable_ctx_params; |
39 | | #endif |
40 | | |
41 | | typedef struct { |
42 | | OSSL_LIB_CTX *libctx; |
43 | | MLX_KEY *key; |
44 | | int op; |
45 | | } PROV_MLX_KEM_CTX; |
46 | | |
47 | | static void *mlx_kem_newctx(void *provctx) |
48 | 51 | { |
49 | 51 | PROV_MLX_KEM_CTX *ctx; |
50 | | |
51 | 51 | if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) |
52 | 0 | return NULL; |
53 | | |
54 | 51 | ctx->libctx = PROV_LIBCTX_OF(provctx); |
55 | 51 | ctx->key = NULL; |
56 | 51 | ctx->op = 0; |
57 | 51 | return ctx; |
58 | 51 | } |
59 | | |
60 | | static void mlx_kem_freectx(void *vctx) |
61 | 51 | { |
62 | 51 | OPENSSL_free(vctx); |
63 | 51 | } |
64 | | |
65 | | static int mlx_kem_init(void *vctx, int op, void *key, |
66 | | ossl_unused const OSSL_PARAM params[]) |
67 | 51 | { |
68 | 51 | PROV_MLX_KEM_CTX *ctx = vctx; |
69 | | |
70 | 51 | if (!ossl_prov_is_running()) |
71 | 0 | return 0; |
72 | 51 | ctx->key = key; |
73 | 51 | ctx->op = op; |
74 | 51 | return 1; |
75 | 51 | } |
76 | | |
77 | | static int |
78 | | mlx_kem_encapsulate_init(void *vctx, void *vkey, const OSSL_PARAM params[]) |
79 | 18 | { |
80 | 18 | MLX_KEY *key = vkey; |
81 | | |
82 | 18 | if (!mlx_kem_have_pubkey(key)) { |
83 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
84 | 0 | return 0; |
85 | 0 | } |
86 | 18 | return mlx_kem_init(vctx, EVP_PKEY_OP_ENCAPSULATE, key, params); |
87 | 18 | } |
88 | | |
89 | | static int |
90 | | mlx_kem_decapsulate_init(void *vctx, void *vkey, const OSSL_PARAM params[]) |
91 | 33 | { |
92 | 33 | MLX_KEY *key = vkey; |
93 | | |
94 | 33 | if (!mlx_kem_have_prvkey(key)) { |
95 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
96 | 0 | return 0; |
97 | 0 | } |
98 | 33 | return mlx_kem_init(vctx, EVP_PKEY_OP_DECAPSULATE, key, params); |
99 | 33 | } |
100 | | |
101 | | static const OSSL_PARAM *mlx_kem_settable_ctx_params(ossl_unused void *vctx, |
102 | | ossl_unused void *provctx) |
103 | 5 | { |
104 | 5 | static const OSSL_PARAM params[] = { OSSL_PARAM_END }; |
105 | | |
106 | 5 | return params; |
107 | 5 | } |
108 | | |
109 | | static int |
110 | | mlx_kem_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
111 | 0 | { |
112 | 0 | return 1; |
113 | 0 | } |
114 | | |
115 | | #ifdef FIPS_MODULE |
116 | | static const OSSL_PARAM *mlx_kem_gettable_ctx_params(ossl_unused void *vctx, |
117 | | ossl_unused void *provctx) |
118 | | { |
119 | | return mlx_kem_get_ctx_params_list; |
120 | | } |
121 | | |
122 | | static int mlx_kem_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
123 | | { |
124 | | PROV_MLX_KEM_CTX *ctx = vctx; |
125 | | struct mlx_kem_get_ctx_params_st p; |
126 | | int approved; |
127 | | |
128 | | if (ctx == NULL || !mlx_kem_get_ctx_params_decoder(params, &p)) |
129 | | return 0; |
130 | | |
131 | | if (p.ind != NULL) { |
132 | | if (ctx->key == NULL || ctx->key->xinfo == NULL) |
133 | | return 0; |
134 | | approved = strcmp(ctx->key->xinfo->algorithm_name, SN_X448) != 0; |
135 | | if (!OSSL_PARAM_set_int(p.ind, approved)) |
136 | | return 0; |
137 | | } |
138 | | return 1; |
139 | | } |
140 | | #endif |
141 | | |
142 | | static int mlx_kem_encapsulate(void *vctx, unsigned char *ctext, size_t *clen, |
143 | | unsigned char *shsec, size_t *slen) |
144 | 36 | { |
145 | 36 | MLX_KEY *key = ((PROV_MLX_KEM_CTX *)vctx)->key; |
146 | 36 | EVP_PKEY_CTX *ctx = NULL; |
147 | 36 | EVP_PKEY *xkey = NULL; |
148 | 36 | size_t encap_clen; |
149 | 36 | size_t encap_slen; |
150 | 36 | uint8_t *cbuf; |
151 | 36 | uint8_t *sbuf; |
152 | 36 | int ml_kem_slot = key->xinfo->ml_kem_slot; |
153 | 36 | int ret = 0; |
154 | | |
155 | 36 | if (!mlx_kem_have_pubkey(key)) { |
156 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
157 | 0 | return 0; |
158 | 0 | } |
159 | 36 | encap_clen = key->minfo->ctext_bytes + key->xinfo->pubkey_bytes; |
160 | 36 | encap_slen = ML_KEM_SHARED_SECRET_BYTES + key->xinfo->shsec_bytes; |
161 | | |
162 | 36 | if (ctext == NULL) { |
163 | 18 | if (clen == NULL && slen == NULL) |
164 | 0 | return 0; |
165 | 18 | if (clen != NULL) |
166 | 18 | *clen = encap_clen; |
167 | 18 | if (slen != NULL) |
168 | 18 | *slen = encap_slen; |
169 | 18 | return 1; |
170 | 18 | } |
171 | 18 | if (shsec == NULL) { |
172 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_NULL_OUTPUT_BUFFER, |
173 | 0 | "null shared-secret output buffer"); |
174 | 0 | return 0; |
175 | 0 | } |
176 | | |
177 | 18 | if (clen == NULL) { |
178 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_NULL_LENGTH_POINTER, |
179 | 0 | "null ciphertext input/output length pointer"); |
180 | 0 | return 0; |
181 | 18 | } else if (*clen < encap_clen) { |
182 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL, |
183 | 0 | "ciphertext buffer too small"); |
184 | 0 | return 0; |
185 | 18 | } else { |
186 | 18 | *clen = encap_clen; |
187 | 18 | } |
188 | | |
189 | 18 | if (slen == NULL) { |
190 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_NULL_LENGTH_POINTER, |
191 | 0 | "null shared secret input/output length pointer"); |
192 | 0 | return 0; |
193 | 18 | } else if (*slen < encap_slen) { |
194 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL, |
195 | 0 | "shared-secret buffer too small"); |
196 | 0 | return 0; |
197 | 18 | } else { |
198 | 18 | *slen = encap_slen; |
199 | 18 | } |
200 | | |
201 | | /* ML-KEM encapsulation */ |
202 | 18 | encap_clen = key->minfo->ctext_bytes; |
203 | 18 | encap_slen = ML_KEM_SHARED_SECRET_BYTES; |
204 | 18 | cbuf = ctext + ml_kem_slot * key->xinfo->pubkey_bytes; |
205 | 18 | sbuf = shsec + ml_kem_slot * key->xinfo->shsec_bytes; |
206 | 18 | ctx = EVP_PKEY_CTX_new_from_pkey(key->libctx, key->mkey, key->propq); |
207 | 18 | if (ctx == NULL |
208 | 18 | || EVP_PKEY_encapsulate_init(ctx, NULL) <= 0 |
209 | 18 | || EVP_PKEY_encapsulate(ctx, cbuf, &encap_clen, sbuf, &encap_slen) <= 0) |
210 | 0 | goto end; |
211 | 18 | if (encap_clen != key->minfo->ctext_bytes) { |
212 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
213 | 0 | "unexpected %s ciphertext output size: %lu", |
214 | 0 | key->minfo->algorithm_name, (unsigned long)encap_clen); |
215 | 0 | goto end; |
216 | 0 | } |
217 | 18 | if (encap_slen != ML_KEM_SHARED_SECRET_BYTES) { |
218 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
219 | 0 | "unexpected %s shared secret output size: %lu", |
220 | 0 | key->minfo->algorithm_name, (unsigned long)encap_slen); |
221 | 0 | goto end; |
222 | 0 | } |
223 | 18 | EVP_PKEY_CTX_free(ctx); |
224 | | |
225 | | /*- |
226 | | * ECDHE encapsulation |
227 | | * |
228 | | * Generate own ephemeral private key and add its public key to ctext. |
229 | | * |
230 | | * Note, we could support a settable parameter that sets an extant ECDH |
231 | | * keypair as the keys to use in encap, making it possible to reuse the |
232 | | * same (TLS client) ECDHE keypair for both the classical EC keyshare and a |
233 | | * corresponding ECDHE + ML-KEM keypair. But the TLS layer would then need |
234 | | * know that this is a hybrid, and that it can partly reuse the same keys |
235 | | * as another group for which a keyshare will be sent. Deferred until we |
236 | | * support generating multiple keyshares, there's a workable keyshare |
237 | | * prediction specification, and the optimisation is justified. |
238 | | */ |
239 | 18 | cbuf = ctext + (1 - ml_kem_slot) * key->minfo->ctext_bytes; |
240 | 18 | encap_clen = key->xinfo->pubkey_bytes; |
241 | 18 | ctx = EVP_PKEY_CTX_new_from_pkey(key->libctx, key->xkey, key->propq); |
242 | 18 | if (ctx == NULL |
243 | 18 | || EVP_PKEY_keygen_init(ctx) <= 0 |
244 | 18 | || EVP_PKEY_keygen(ctx, &xkey) <= 0 |
245 | 18 | || EVP_PKEY_get_octet_string_param(xkey, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
246 | 18 | cbuf, encap_clen, &encap_clen) |
247 | 18 | <= 0) |
248 | 0 | goto end; |
249 | 18 | if (encap_clen != key->xinfo->pubkey_bytes) { |
250 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
251 | 0 | "unexpected %s public key output size: %lu", |
252 | 0 | key->xinfo->algorithm_name, (unsigned long)encap_clen); |
253 | 0 | goto end; |
254 | 0 | } |
255 | 18 | EVP_PKEY_CTX_free(ctx); |
256 | | |
257 | | /* Derive the ECDH shared secret */ |
258 | 18 | encap_slen = key->xinfo->shsec_bytes; |
259 | 18 | sbuf = shsec + (1 - ml_kem_slot) * ML_KEM_SHARED_SECRET_BYTES; |
260 | 18 | ctx = EVP_PKEY_CTX_new_from_pkey(key->libctx, xkey, key->propq); |
261 | 18 | if (ctx == NULL |
262 | 18 | || EVP_PKEY_derive_init(ctx) <= 0 |
263 | 18 | || EVP_PKEY_derive_set_peer(ctx, key->xkey) <= 0 |
264 | 18 | || EVP_PKEY_derive(ctx, sbuf, &encap_slen) <= 0) |
265 | 2 | goto end; |
266 | 16 | if (encap_slen != key->xinfo->shsec_bytes) { |
267 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
268 | 0 | "unexpected %s shared secret output size: %lu", |
269 | 0 | key->xinfo->algorithm_name, (unsigned long)encap_slen); |
270 | 0 | goto end; |
271 | 0 | } |
272 | | |
273 | 16 | ret = 1; |
274 | 18 | end: |
275 | | /* Erase any partial shared secret on failure */ |
276 | 18 | if (ret == 0) |
277 | 2 | OPENSSL_cleanse(shsec, |
278 | 2 | ML_KEM_SHARED_SECRET_BYTES + key->xinfo->shsec_bytes); |
279 | 18 | EVP_PKEY_free(xkey); |
280 | 18 | EVP_PKEY_CTX_free(ctx); |
281 | 18 | return ret; |
282 | 16 | } |
283 | | |
284 | | static int mlx_kem_decapsulate(void *vctx, uint8_t *shsec, size_t *slen, |
285 | | const uint8_t *ctext, size_t clen) |
286 | 66 | { |
287 | 66 | MLX_KEY *key = ((PROV_MLX_KEM_CTX *)vctx)->key; |
288 | 66 | EVP_PKEY_CTX *ctx = NULL; |
289 | 66 | EVP_PKEY *xkey = NULL; |
290 | 66 | const uint8_t *cbuf; |
291 | 66 | uint8_t *sbuf; |
292 | 66 | size_t decap_slen = ML_KEM_SHARED_SECRET_BYTES + key->xinfo->shsec_bytes; |
293 | 66 | size_t decap_clen = key->minfo->ctext_bytes + key->xinfo->pubkey_bytes; |
294 | 66 | int ml_kem_slot = key->xinfo->ml_kem_slot; |
295 | 66 | int ret = 0; |
296 | | |
297 | 66 | if (!mlx_kem_have_prvkey(key)) { |
298 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
299 | 0 | return 0; |
300 | 0 | } |
301 | | |
302 | 66 | if (shsec == NULL) { |
303 | 33 | if (slen == NULL) |
304 | 0 | return 0; |
305 | 33 | *slen = decap_slen; |
306 | 33 | return 1; |
307 | 33 | } |
308 | | |
309 | | /* For now tolerate newly-deprecated NULL length pointers. */ |
310 | 33 | if (slen == NULL) { |
311 | 0 | slen = &decap_slen; |
312 | 33 | } else if (*slen < decap_slen) { |
313 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL, |
314 | 0 | "shared-secret buffer too small"); |
315 | 0 | return 0; |
316 | 33 | } else { |
317 | 33 | *slen = decap_slen; |
318 | 33 | } |
319 | 33 | if (clen != decap_clen) { |
320 | 2 | ERR_raise_data(ERR_LIB_PROV, PROV_R_WRONG_CIPHERTEXT_SIZE, |
321 | 2 | "wrong decapsulation input ciphertext size: %lu", |
322 | 2 | (unsigned long)clen); |
323 | 2 | return 0; |
324 | 2 | } |
325 | | |
326 | | /* ML-KEM decapsulation */ |
327 | 31 | decap_clen = key->minfo->ctext_bytes; |
328 | 31 | decap_slen = ML_KEM_SHARED_SECRET_BYTES; |
329 | 31 | cbuf = ctext + ml_kem_slot * key->xinfo->pubkey_bytes; |
330 | 31 | sbuf = shsec + ml_kem_slot * key->xinfo->shsec_bytes; |
331 | 31 | ctx = EVP_PKEY_CTX_new_from_pkey(key->libctx, key->mkey, key->propq); |
332 | 31 | if (ctx == NULL |
333 | 31 | || EVP_PKEY_decapsulate_init(ctx, NULL) <= 0 |
334 | 31 | || EVP_PKEY_decapsulate(ctx, sbuf, &decap_slen, cbuf, decap_clen) <= 0) |
335 | 0 | goto end; |
336 | 31 | if (decap_slen != ML_KEM_SHARED_SECRET_BYTES) { |
337 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
338 | 0 | "unexpected %s shared secret output size: %lu", |
339 | 0 | key->minfo->algorithm_name, (unsigned long)decap_slen); |
340 | 0 | goto end; |
341 | 0 | } |
342 | 31 | EVP_PKEY_CTX_free(ctx); |
343 | | |
344 | | /* ECDH decapsulation */ |
345 | 31 | decap_clen = key->xinfo->pubkey_bytes; |
346 | 31 | decap_slen = key->xinfo->shsec_bytes; |
347 | 31 | cbuf = ctext + (1 - ml_kem_slot) * key->minfo->ctext_bytes; |
348 | 31 | sbuf = shsec + (1 - ml_kem_slot) * ML_KEM_SHARED_SECRET_BYTES; |
349 | 31 | ctx = EVP_PKEY_CTX_new_from_pkey(key->libctx, key->xkey, key->propq); |
350 | 31 | if (ctx == NULL |
351 | 31 | || (xkey = EVP_PKEY_new()) == NULL |
352 | 31 | || EVP_PKEY_copy_parameters(xkey, key->xkey) <= 0 |
353 | 31 | || EVP_PKEY_set1_encoded_public_key(xkey, cbuf, decap_clen) <= 0 |
354 | 31 | || EVP_PKEY_derive_init(ctx) <= 0 |
355 | 31 | || EVP_PKEY_derive_set_peer(ctx, xkey) <= 0 |
356 | 31 | || EVP_PKEY_derive(ctx, sbuf, &decap_slen) <= 0) |
357 | 4 | goto end; |
358 | 27 | if (decap_slen != key->xinfo->shsec_bytes) { |
359 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
360 | 0 | "unexpected %s shared secret output size: %lu", |
361 | 0 | key->xinfo->algorithm_name, (unsigned long)decap_slen); |
362 | 0 | goto end; |
363 | 0 | } |
364 | | |
365 | 27 | ret = 1; |
366 | 31 | end: |
367 | | /* Erase any partial shared secret on failure */ |
368 | 31 | if (ret == 0) |
369 | 4 | OPENSSL_cleanse(shsec, |
370 | 4 | ML_KEM_SHARED_SECRET_BYTES + key->xinfo->shsec_bytes); |
371 | 31 | EVP_PKEY_CTX_free(ctx); |
372 | 31 | EVP_PKEY_free(xkey); |
373 | 31 | return ret; |
374 | 27 | } |
375 | | |
376 | | const OSSL_DISPATCH ossl_mlx_kem_asym_kem_functions[] = { |
377 | | { OSSL_FUNC_KEM_NEWCTX, (OSSL_FUNC)mlx_kem_newctx }, |
378 | | { OSSL_FUNC_KEM_ENCAPSULATE_INIT, (OSSL_FUNC)mlx_kem_encapsulate_init }, |
379 | | { OSSL_FUNC_KEM_ENCAPSULATE, (OSSL_FUNC)mlx_kem_encapsulate }, |
380 | | { OSSL_FUNC_KEM_DECAPSULATE_INIT, (OSSL_FUNC)mlx_kem_decapsulate_init }, |
381 | | { OSSL_FUNC_KEM_DECAPSULATE, (OSSL_FUNC)mlx_kem_decapsulate }, |
382 | | { OSSL_FUNC_KEM_FREECTX, (OSSL_FUNC)mlx_kem_freectx }, |
383 | | { OSSL_FUNC_KEM_SET_CTX_PARAMS, (OSSL_FUNC)mlx_kem_set_ctx_params }, |
384 | | { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS, (OSSL_FUNC)mlx_kem_settable_ctx_params }, |
385 | | #ifdef FIPS_MODULE |
386 | | { OSSL_FUNC_KEM_GET_CTX_PARAMS, (OSSL_FUNC)mlx_kem_get_ctx_params }, |
387 | | { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS, |
388 | | (OSSL_FUNC)mlx_kem_gettable_ctx_params }, |
389 | | #endif |
390 | | OSSL_DISPATCH_END |
391 | | }; |