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