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