/src/openssl/providers/implementations/kem/ml_kem_kem.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-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 | | |
10 | | #include <string.h> |
11 | | #include <openssl/crypto.h> |
12 | | #include <openssl/evp.h> |
13 | | #include <openssl/core_dispatch.h> |
14 | | #include <openssl/core_names.h> |
15 | | #include <openssl/params.h> |
16 | | #include <openssl/err.h> |
17 | | #include <openssl/proverr.h> |
18 | | #include "crypto/ml_kem.h" |
19 | | #include "internal/cryptlib.h" |
20 | | #include "internal/fips.h" |
21 | | #include "prov/provider_ctx.h" |
22 | | #include "prov/implementations.h" |
23 | | #include "prov/securitycheck.h" |
24 | | #include "prov/providercommon.h" |
25 | | #include "providers/implementations/kem/ml_kem_kem.inc" |
26 | | |
27 | | static OSSL_FUNC_kem_newctx_fn ml_kem_newctx; |
28 | | static OSSL_FUNC_kem_freectx_fn ml_kem_freectx; |
29 | | static OSSL_FUNC_kem_encapsulate_init_fn ml_kem_encapsulate_init; |
30 | | static OSSL_FUNC_kem_encapsulate_fn ml_kem_encapsulate; |
31 | | static OSSL_FUNC_kem_decapsulate_init_fn ml_kem_decapsulate_init; |
32 | | static OSSL_FUNC_kem_decapsulate_fn ml_kem_decapsulate; |
33 | | static OSSL_FUNC_kem_set_ctx_params_fn ml_kem_set_ctx_params; |
34 | | static OSSL_FUNC_kem_settable_ctx_params_fn ml_kem_settable_ctx_params; |
35 | | |
36 | | typedef struct { |
37 | | ML_KEM_KEY *key; |
38 | | uint8_t entropy_buf[ML_KEM_RANDOM_BYTES]; |
39 | | uint8_t *entropy; |
40 | | int op; |
41 | | } PROV_ML_KEM_CTX; |
42 | | |
43 | | static void *ml_kem_newctx(void *provctx) |
44 | 0 | { |
45 | 0 | PROV_ML_KEM_CTX *ctx; |
46 | |
|
47 | 0 | if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) |
48 | 0 | return NULL; |
49 | | |
50 | | #ifdef FIPS_MODULE |
51 | | if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx), |
52 | | ST_ID_KEM_ML_KEM)) |
53 | | return NULL; |
54 | | #endif |
55 | | |
56 | 0 | ctx->key = NULL; |
57 | 0 | ctx->entropy = NULL; |
58 | 0 | ctx->op = 0; |
59 | 0 | return ctx; |
60 | 0 | } |
61 | | |
62 | | static void ml_kem_freectx(void *vctx) |
63 | 0 | { |
64 | 0 | PROV_ML_KEM_CTX *ctx = vctx; |
65 | |
|
66 | 0 | if (ctx->entropy != NULL) |
67 | 0 | OPENSSL_cleanse(ctx->entropy, ML_KEM_RANDOM_BYTES); |
68 | 0 | OPENSSL_free(ctx); |
69 | 0 | } |
70 | | |
71 | | static int ml_kem_init(void *vctx, int op, void *key, |
72 | | const OSSL_PARAM params[]) |
73 | 0 | { |
74 | 0 | PROV_ML_KEM_CTX *ctx = vctx; |
75 | |
|
76 | 0 | if (!ossl_prov_is_running()) |
77 | 0 | return 0; |
78 | 0 | ctx->key = key; |
79 | 0 | ctx->op = op; |
80 | 0 | if (ctx->entropy != NULL) { |
81 | 0 | OPENSSL_cleanse(ctx->entropy, ML_KEM_RANDOM_BYTES); |
82 | 0 | ctx->entropy = NULL; |
83 | 0 | } |
84 | 0 | return ml_kem_set_ctx_params(vctx, params); |
85 | 0 | } |
86 | | |
87 | | static int ml_kem_encapsulate_init(void *vctx, void *vkey, |
88 | | const OSSL_PARAM params[]) |
89 | 0 | { |
90 | 0 | ML_KEM_KEY *key = vkey; |
91 | |
|
92 | 0 | if (!ossl_ml_kem_have_pubkey(key)) { |
93 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
94 | 0 | return 0; |
95 | 0 | } |
96 | 0 | return ml_kem_init(vctx, EVP_PKEY_OP_ENCAPSULATE, key, params); |
97 | 0 | } |
98 | | |
99 | | static int ml_kem_decapsulate_init(void *vctx, void *vkey, |
100 | | const OSSL_PARAM params[]) |
101 | 0 | { |
102 | 0 | ML_KEM_KEY *key = vkey; |
103 | |
|
104 | 0 | if (!ossl_ml_kem_have_prvkey(key)) { |
105 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
106 | 0 | return 0; |
107 | 0 | } |
108 | 0 | return ml_kem_init(vctx, EVP_PKEY_OP_DECAPSULATE, key, params); |
109 | 0 | } |
110 | | |
111 | | static int ml_kem_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
112 | 0 | { |
113 | 0 | PROV_ML_KEM_CTX *ctx = vctx; |
114 | 0 | struct ml_kem_set_ctx_params_st p; |
115 | |
|
116 | 0 | if (ctx == NULL || !ml_kem_set_ctx_params_decoder(params, &p)) |
117 | 0 | return 0; |
118 | | |
119 | | /* Encapsulation ephemeral input key material "ikmE" */ |
120 | 0 | if (ctx->op == EVP_PKEY_OP_ENCAPSULATE && p.ikme != NULL) { |
121 | 0 | size_t len = ML_KEM_RANDOM_BYTES; |
122 | |
|
123 | 0 | ctx->entropy = ctx->entropy_buf; |
124 | 0 | if (OSSL_PARAM_get_octet_string(p.ikme, (void **)&ctx->entropy, |
125 | 0 | len, &len) |
126 | 0 | && len == ML_KEM_RANDOM_BYTES) |
127 | 0 | return 1; |
128 | | |
129 | | /* Possibly, but much less likely wrong type */ |
130 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH); |
131 | 0 | ctx->entropy = NULL; |
132 | 0 | return 0; |
133 | 0 | } |
134 | | |
135 | 0 | return 1; |
136 | 0 | } |
137 | | |
138 | | static const OSSL_PARAM *ml_kem_settable_ctx_params(ossl_unused void *vctx, |
139 | | ossl_unused void *provctx) |
140 | 0 | { |
141 | 0 | return ml_kem_set_ctx_params_list; |
142 | 0 | } |
143 | | |
144 | | static int ml_kem_encapsulate(void *vctx, unsigned char *ctext, size_t *clen, |
145 | | unsigned char *shsec, size_t *slen) |
146 | 0 | { |
147 | 0 | PROV_ML_KEM_CTX *ctx = vctx; |
148 | 0 | ML_KEM_KEY *key = ctx->key; |
149 | 0 | const ML_KEM_VINFO *v; |
150 | 0 | size_t encap_clen; |
151 | 0 | size_t encap_slen; |
152 | 0 | int ret = 0; |
153 | |
|
154 | 0 | if (!ossl_ml_kem_have_pubkey(key)) { |
155 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
156 | 0 | goto end; |
157 | 0 | } |
158 | 0 | v = ossl_ml_kem_key_vinfo(key); |
159 | 0 | encap_clen = v->ctext_bytes; |
160 | 0 | encap_slen = ML_KEM_SHARED_SECRET_BYTES; |
161 | |
|
162 | 0 | if (ctext == NULL) { |
163 | 0 | if (clen == NULL && slen == NULL) |
164 | 0 | return 0; |
165 | 0 | if (clen != NULL) |
166 | 0 | *clen = encap_clen; |
167 | 0 | if (slen != NULL) |
168 | 0 | *slen = encap_slen; |
169 | 0 | return 1; |
170 | 0 | } |
171 | 0 | if (shsec == NULL) { |
172 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_NULL_OUTPUT_BUFFER, |
173 | 0 | "NULL shared-secret buffer"); |
174 | 0 | goto end; |
175 | 0 | } |
176 | | |
177 | 0 | 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 | goto end; |
181 | 0 | } 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 | goto end; |
185 | 0 | } else { |
186 | 0 | *clen = encap_clen; |
187 | 0 | } |
188 | | |
189 | 0 | 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 | goto end; |
193 | 0 | } 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 | goto end; |
197 | 0 | } else { |
198 | 0 | *slen = encap_slen; |
199 | 0 | } |
200 | | |
201 | 0 | if (ctx->entropy != NULL) |
202 | 0 | ret = ossl_ml_kem_encap_seed(ctext, encap_clen, shsec, encap_slen, |
203 | 0 | ctx->entropy, ML_KEM_RANDOM_BYTES, key); |
204 | 0 | else |
205 | 0 | ret = ossl_ml_kem_encap_rand(ctext, encap_clen, shsec, encap_slen, key); |
206 | |
|
207 | 0 | end: |
208 | | /* |
209 | | * One shot entropy, each encapsulate call must either provide a new |
210 | | * "ikmE", or else will use a random value. If a caller sets an explicit |
211 | | * ikmE once for testing, and later performs multiple encapsulations |
212 | | * without again calling encapsulate_init(), these should not share the |
213 | | * original entropy. |
214 | | */ |
215 | 0 | if (ctx->entropy != NULL) { |
216 | 0 | OPENSSL_cleanse(ctx->entropy, ML_KEM_RANDOM_BYTES); |
217 | 0 | ctx->entropy = NULL; |
218 | 0 | } |
219 | 0 | return ret; |
220 | 0 | } |
221 | | |
222 | | static int ml_kem_decapsulate(void *vctx, uint8_t *shsec, size_t *slen, |
223 | | const uint8_t *ctext, size_t clen) |
224 | 0 | { |
225 | 0 | PROV_ML_KEM_CTX *ctx = vctx; |
226 | 0 | ML_KEM_KEY *key = ctx->key; |
227 | 0 | size_t decap_slen = ML_KEM_SHARED_SECRET_BYTES; |
228 | |
|
229 | 0 | if (!ossl_ml_kem_have_prvkey(key)) { |
230 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
231 | 0 | return 0; |
232 | 0 | } |
233 | | |
234 | 0 | if (shsec == NULL) { |
235 | 0 | if (slen == NULL) |
236 | 0 | return 0; |
237 | 0 | *slen = ML_KEM_SHARED_SECRET_BYTES; |
238 | 0 | return 1; |
239 | 0 | } |
240 | | |
241 | | /* For now tolerate newly-deprecated NULL length pointers. */ |
242 | 0 | if (slen == NULL) { |
243 | 0 | slen = &decap_slen; |
244 | 0 | } else if (*slen < decap_slen) { |
245 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL, |
246 | 0 | "shared-secret buffer too small"); |
247 | 0 | return 0; |
248 | 0 | } else { |
249 | 0 | *slen = decap_slen; |
250 | 0 | } |
251 | | |
252 | | /* ML-KEM decap handles incorrect ciphertext lengths internally */ |
253 | 0 | return ossl_ml_kem_decap(shsec, decap_slen, ctext, clen, key); |
254 | 0 | } |
255 | | |
256 | | const OSSL_DISPATCH ossl_ml_kem_asym_kem_functions[] = { |
257 | | { OSSL_FUNC_KEM_NEWCTX, (OSSL_FUNC)ml_kem_newctx }, |
258 | | { OSSL_FUNC_KEM_ENCAPSULATE_INIT, (OSSL_FUNC)ml_kem_encapsulate_init }, |
259 | | { OSSL_FUNC_KEM_ENCAPSULATE, (OSSL_FUNC)ml_kem_encapsulate }, |
260 | | { OSSL_FUNC_KEM_DECAPSULATE_INIT, (OSSL_FUNC)ml_kem_decapsulate_init }, |
261 | | { OSSL_FUNC_KEM_DECAPSULATE, (OSSL_FUNC)ml_kem_decapsulate }, |
262 | | { OSSL_FUNC_KEM_FREECTX, (OSSL_FUNC)ml_kem_freectx }, |
263 | | { OSSL_FUNC_KEM_SET_CTX_PARAMS, (OSSL_FUNC)ml_kem_set_ctx_params }, |
264 | | { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS, (OSSL_FUNC)ml_kem_settable_ctx_params }, |
265 | | OSSL_DISPATCH_END |
266 | | }; |