/src/openssl/providers/implementations/kem/rsa_kem.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020-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 | | /* |
11 | | * RSA low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | #include "internal/nelem.h" |
16 | | #include <openssl/crypto.h> |
17 | | #include <openssl/evp.h> |
18 | | #include <openssl/core_dispatch.h> |
19 | | #include <openssl/core_names.h> |
20 | | #include <openssl/rsa.h> |
21 | | #include <openssl/params.h> |
22 | | #include <openssl/err.h> |
23 | | #include <openssl/proverr.h> |
24 | | #include "crypto/rsa.h" |
25 | | #include "internal/cryptlib.h" |
26 | | #include "internal/fips.h" |
27 | | #include "prov/provider_ctx.h" |
28 | | #include "prov/providercommon.h" |
29 | | #include "prov/implementations.h" |
30 | | #include "prov/securitycheck.h" |
31 | | #include "providers/implementations/kem/rsa_kem.inc" |
32 | | |
33 | | static OSSL_FUNC_kem_newctx_fn rsakem_newctx; |
34 | | static OSSL_FUNC_kem_encapsulate_init_fn rsakem_encapsulate_init; |
35 | | static OSSL_FUNC_kem_encapsulate_fn rsakem_generate; |
36 | | static OSSL_FUNC_kem_decapsulate_init_fn rsakem_decapsulate_init; |
37 | | static OSSL_FUNC_kem_decapsulate_fn rsakem_recover; |
38 | | static OSSL_FUNC_kem_freectx_fn rsakem_freectx; |
39 | | static OSSL_FUNC_kem_dupctx_fn rsakem_dupctx; |
40 | | static OSSL_FUNC_kem_get_ctx_params_fn rsakem_get_ctx_params; |
41 | | static OSSL_FUNC_kem_gettable_ctx_params_fn rsakem_gettable_ctx_params; |
42 | | static OSSL_FUNC_kem_set_ctx_params_fn rsakem_set_ctx_params; |
43 | | static OSSL_FUNC_kem_settable_ctx_params_fn rsakem_settable_ctx_params; |
44 | | |
45 | | /* |
46 | | * Only the KEM for RSASVE as defined in SP800-56b r2 is implemented |
47 | | * currently. |
48 | | */ |
49 | | #define KEM_OP_UNDEFINED -1 |
50 | 0 | #define KEM_OP_RSASVE 0 |
51 | | |
52 | | /* |
53 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
54 | | * We happen to know that our KEYMGMT simply passes RSA structures, so |
55 | | * we use that here too. |
56 | | */ |
57 | | typedef struct { |
58 | | OSSL_LIB_CTX *libctx; |
59 | | RSA *rsa; |
60 | | int op; |
61 | | OSSL_FIPS_IND_DECLARE |
62 | | } PROV_RSA_CTX; |
63 | | |
64 | | static const OSSL_ITEM rsakem_opname_id_map[] = { |
65 | | { KEM_OP_RSASVE, OSSL_KEM_PARAM_OPERATION_RSASVE }, |
66 | | }; |
67 | | |
68 | | static int name2id(const char *name, const OSSL_ITEM *map, size_t sz) |
69 | 0 | { |
70 | 0 | size_t i; |
71 | |
|
72 | 0 | if (name == NULL) |
73 | 0 | return -1; |
74 | | |
75 | 0 | for (i = 0; i < sz; ++i) { |
76 | 0 | if (OPENSSL_strcasecmp(map[i].ptr, name) == 0) |
77 | 0 | return map[i].id; |
78 | 0 | } |
79 | 0 | return -1; |
80 | 0 | } |
81 | | |
82 | | static int rsakem_opname2id(const char *name) |
83 | 0 | { |
84 | 0 | return name2id(name, rsakem_opname_id_map, OSSL_NELEM(rsakem_opname_id_map)); |
85 | 0 | } |
86 | | |
87 | | static void *rsakem_newctx(void *provctx) |
88 | 0 | { |
89 | 0 | PROV_RSA_CTX *prsactx; |
90 | |
|
91 | 0 | if (!ossl_prov_is_running()) |
92 | 0 | return NULL; |
93 | | |
94 | | #ifdef FIPS_MODULE |
95 | | if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx), |
96 | | ST_ID_ASYM_CIPHER_RSA_ENC)) |
97 | | return NULL; |
98 | | #endif |
99 | | |
100 | 0 | prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX)); |
101 | 0 | if (prsactx == NULL) |
102 | 0 | return NULL; |
103 | 0 | prsactx->libctx = PROV_LIBCTX_OF(provctx); |
104 | 0 | prsactx->op = KEM_OP_RSASVE; |
105 | 0 | OSSL_FIPS_IND_INIT(prsactx) |
106 | |
|
107 | 0 | return prsactx; |
108 | 0 | } |
109 | | |
110 | | static void rsakem_freectx(void *vprsactx) |
111 | 0 | { |
112 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
113 | |
|
114 | 0 | RSA_free(prsactx->rsa); |
115 | 0 | OPENSSL_free(prsactx); |
116 | 0 | } |
117 | | |
118 | | static void *rsakem_dupctx(void *vprsactx) |
119 | 0 | { |
120 | 0 | PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx; |
121 | 0 | PROV_RSA_CTX *dstctx; |
122 | |
|
123 | 0 | if (!ossl_prov_is_running()) |
124 | 0 | return NULL; |
125 | | |
126 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
127 | 0 | if (dstctx == NULL) |
128 | 0 | return NULL; |
129 | | |
130 | 0 | *dstctx = *srcctx; |
131 | 0 | if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) { |
132 | 0 | OPENSSL_free(dstctx); |
133 | 0 | return NULL; |
134 | 0 | } |
135 | 0 | return dstctx; |
136 | 0 | } |
137 | | |
138 | | static int rsakem_init(void *vprsactx, void *vrsa, |
139 | | const OSSL_PARAM params[], int operation, |
140 | | const char *desc) |
141 | 0 | { |
142 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
143 | 0 | int protect = 0; |
144 | |
|
145 | 0 | if (!ossl_prov_is_running()) |
146 | 0 | return 0; |
147 | | |
148 | 0 | if (prsactx == NULL || vrsa == NULL) |
149 | 0 | return 0; |
150 | | |
151 | 0 | if (!ossl_rsa_key_op_get_protect(vrsa, operation, &protect)) |
152 | 0 | return 0; |
153 | 0 | if (!RSA_up_ref(vrsa)) |
154 | 0 | return 0; |
155 | 0 | RSA_free(prsactx->rsa); |
156 | 0 | prsactx->rsa = vrsa; |
157 | |
|
158 | 0 | OSSL_FIPS_IND_SET_APPROVED(prsactx) |
159 | 0 | if (!rsakem_set_ctx_params(prsactx, params)) |
160 | 0 | return 0; |
161 | | #ifdef FIPS_MODULE |
162 | | if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx), |
163 | | OSSL_FIPS_IND_SETTABLE0, prsactx->libctx, |
164 | | prsactx->rsa, desc, protect)) |
165 | | return 0; |
166 | | #endif |
167 | 0 | return 1; |
168 | 0 | } |
169 | | |
170 | | static int rsakem_encapsulate_init(void *vprsactx, void *vrsa, |
171 | | const OSSL_PARAM params[]) |
172 | 0 | { |
173 | 0 | return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCAPSULATE, |
174 | 0 | "RSA Encapsulate Init"); |
175 | 0 | } |
176 | | |
177 | | static int rsakem_decapsulate_init(void *vprsactx, void *vrsa, |
178 | | const OSSL_PARAM params[]) |
179 | 0 | { |
180 | 0 | return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECAPSULATE, |
181 | 0 | "RSA Decapsulate Init"); |
182 | 0 | } |
183 | | |
184 | | static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params) |
185 | 0 | { |
186 | 0 | PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx; |
187 | 0 | struct rsakem_get_ctx_params_st p; |
188 | |
|
189 | 0 | if (ctx == NULL || !rsakem_get_ctx_params_decoder(params, &p)) |
190 | 0 | return 0; |
191 | | |
192 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
193 | 0 | return 0; |
194 | 0 | return 1; |
195 | 0 | } |
196 | | |
197 | | static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *vprsactx, |
198 | | ossl_unused void *provctx) |
199 | 0 | { |
200 | 0 | return rsakem_get_ctx_params_list; |
201 | 0 | } |
202 | | |
203 | | static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) |
204 | 0 | { |
205 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
206 | 0 | struct rsakem_set_ctx_params_st p; |
207 | 0 | int op; |
208 | |
|
209 | 0 | if (prsactx == NULL || !rsakem_set_ctx_params_decoder(params, &p)) |
210 | 0 | return 0; |
211 | | |
212 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0, |
213 | 0 | p.ind_k)) |
214 | 0 | return 0; |
215 | | |
216 | 0 | if (p.op != NULL) { |
217 | 0 | if (p.op->data_type != OSSL_PARAM_UTF8_STRING) |
218 | 0 | return 0; |
219 | 0 | op = rsakem_opname2id(p.op->data); |
220 | 0 | if (op < 0) |
221 | 0 | return 0; |
222 | 0 | prsactx->op = op; |
223 | 0 | } |
224 | 0 | return 1; |
225 | 0 | } |
226 | | |
227 | | static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *vprsactx, |
228 | | ossl_unused void *provctx) |
229 | 0 | { |
230 | 0 | return rsakem_set_ctx_params_list; |
231 | 0 | } |
232 | | |
233 | | /* |
234 | | * NIST.SP.800-56Br2 |
235 | | * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE). |
236 | | * |
237 | | * Generate a random in the range 1 < z < (n – 1) |
238 | | */ |
239 | | static int rsasve_gen_rand_bytes(RSA *rsa_pub, |
240 | | unsigned char *out, int outlen) |
241 | 0 | { |
242 | 0 | int ret = 0; |
243 | 0 | BN_CTX *bnctx; |
244 | 0 | BIGNUM *z, *nminus3; |
245 | |
|
246 | 0 | bnctx = BN_CTX_secure_new_ex(ossl_rsa_get0_libctx(rsa_pub)); |
247 | 0 | if (bnctx == NULL) |
248 | 0 | return 0; |
249 | | |
250 | | /* |
251 | | * Generate a random in the range 1 < z < (n – 1). |
252 | | * Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max |
253 | | * We can achieve this by adding 2.. but then we need to subtract 3 from |
254 | | * the upper bound i.e: 2 + (0 <= r < (n - 3)) |
255 | | */ |
256 | 0 | BN_CTX_start(bnctx); |
257 | 0 | nminus3 = BN_CTX_get(bnctx); |
258 | 0 | z = BN_CTX_get(bnctx); |
259 | 0 | ret = (z != NULL |
260 | 0 | && (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL) |
261 | 0 | && BN_sub_word(nminus3, 3) |
262 | 0 | && BN_priv_rand_range_ex(z, nminus3, 0, bnctx) |
263 | 0 | && BN_add_word(z, 2) |
264 | 0 | && (BN_bn2binpad(z, out, outlen) == outlen)); |
265 | 0 | BN_CTX_end(bnctx); |
266 | 0 | BN_CTX_free(bnctx); |
267 | 0 | return ret; |
268 | 0 | } |
269 | | |
270 | | /* |
271 | | * NIST.SP.800-56Br2 |
272 | | * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE). |
273 | | */ |
274 | | static int rsasve_generate(PROV_RSA_CTX *prsactx, |
275 | | unsigned char *out, size_t *outlen, |
276 | | unsigned char *secret, size_t *secretlen) |
277 | 0 | { |
278 | 0 | int ret; |
279 | 0 | size_t nlen; |
280 | | |
281 | | /* Step (1): nlen = Ceil(len(n)/8) */ |
282 | 0 | nlen = RSA_size(prsactx->rsa); |
283 | |
|
284 | 0 | if (out == NULL) { |
285 | 0 | if (nlen == 0) { |
286 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
287 | 0 | return 0; |
288 | 0 | } |
289 | 0 | if (outlen == NULL && secretlen == NULL) |
290 | 0 | return 0; |
291 | 0 | if (outlen != NULL) |
292 | 0 | *outlen = nlen; |
293 | 0 | if (secretlen != NULL) |
294 | 0 | *secretlen = nlen; |
295 | 0 | return 1; |
296 | 0 | } |
297 | | |
298 | | /* |
299 | | * If outlen is specified, then it must report the length |
300 | | * of the out buffer on input so that we can confirm |
301 | | * its size is sufficient for encapsulation |
302 | | */ |
303 | 0 | if (outlen != NULL && *outlen < nlen) { |
304 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
305 | 0 | return 0; |
306 | 0 | } |
307 | | |
308 | | /* |
309 | | * Step (2): Generate a random byte string z of nlen bytes where |
310 | | * 1 < z < n - 1 |
311 | | */ |
312 | 0 | if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, (int)nlen)) |
313 | 0 | return 0; |
314 | | |
315 | | /* Step(3): out = RSAEP((n,e), z) */ |
316 | 0 | ret = RSA_public_encrypt((int)nlen, secret, out, prsactx->rsa, |
317 | 0 | RSA_NO_PADDING); |
318 | 0 | if (ret) { |
319 | 0 | ret = 1; |
320 | 0 | if (outlen != NULL) |
321 | 0 | *outlen = nlen; |
322 | 0 | if (secretlen != NULL) |
323 | 0 | *secretlen = nlen; |
324 | 0 | } else { |
325 | 0 | OPENSSL_cleanse(secret, nlen); |
326 | 0 | } |
327 | 0 | return ret; |
328 | 0 | } |
329 | | |
330 | | /** |
331 | | * rsasve_recover - Recovers a secret value from ciphertext using an RSA |
332 | | * private key. Once, recovered, the secret value is considered to be a |
333 | | * shared secret. Algorithm is performed as per |
334 | | * NIST SP 800-56B Rev 2 |
335 | | * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER). |
336 | | * |
337 | | * This function performs RSA decryption using the private key from the |
338 | | * provided RSA context (`prsactx`). It takes the input ciphertext, decrypts |
339 | | * it, and writes the decrypted message to the output buffer. |
340 | | * |
341 | | * @prsactx: The RSA context containing the private key. |
342 | | * @out: The output buffer to store the decrypted message. |
343 | | * @outlen: On input, the size of the output buffer. On successful |
344 | | * completion, the actual length of the decrypted message. |
345 | | * @in: The input buffer containing the ciphertext to be decrypted. |
346 | | * @inlen: The length of the input ciphertext in bytes. |
347 | | * |
348 | | * Returns 1 on success, or 0 on error. In case of error, appropriate |
349 | | * error messages are raised using the ERR_raise function. |
350 | | */ |
351 | | static int rsasve_recover(PROV_RSA_CTX *prsactx, |
352 | | unsigned char *out, size_t *outlen, |
353 | | const unsigned char *in, size_t inlen) |
354 | 0 | { |
355 | 0 | size_t nlen; |
356 | 0 | int ret; |
357 | | |
358 | | /* Step (1): get the byte length of n */ |
359 | 0 | nlen = RSA_size(prsactx->rsa); |
360 | |
|
361 | 0 | if (out == NULL) { |
362 | 0 | if (nlen == 0) { |
363 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
364 | 0 | return 0; |
365 | 0 | } |
366 | 0 | *outlen = nlen; |
367 | 0 | return 1; |
368 | 0 | } |
369 | | |
370 | | /* |
371 | | * Step (2): check the input ciphertext 'inlen' matches the nlen |
372 | | * and that outlen is at least nlen bytes |
373 | | */ |
374 | 0 | if (inlen != nlen) { |
375 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); |
376 | 0 | return 0; |
377 | 0 | } |
378 | | |
379 | | /* |
380 | | * If outlen is specified, then it must report the length |
381 | | * of the out buffer, so that we can confirm that it is of |
382 | | * sufficient size to hold the output of decapsulation |
383 | | */ |
384 | 0 | if (outlen != NULL && *outlen < nlen) { |
385 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
386 | 0 | return 0; |
387 | 0 | } |
388 | | |
389 | | /* Step (3): out = RSADP((n,d), in) */ |
390 | 0 | ret = RSA_private_decrypt((int)inlen, in, out, prsactx->rsa, RSA_NO_PADDING); |
391 | 0 | if (ret > 0 && outlen != NULL) |
392 | 0 | *outlen = ret; |
393 | 0 | return ret > 0; |
394 | 0 | } |
395 | | |
396 | | static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen, |
397 | | unsigned char *secret, size_t *secretlen) |
398 | 0 | { |
399 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
400 | |
|
401 | 0 | if (!ossl_prov_is_running()) |
402 | 0 | return 0; |
403 | | |
404 | 0 | switch (prsactx->op) { |
405 | 0 | case KEM_OP_RSASVE: |
406 | 0 | return rsasve_generate(prsactx, out, outlen, secret, secretlen); |
407 | 0 | default: |
408 | 0 | return -2; |
409 | 0 | } |
410 | 0 | } |
411 | | |
412 | | static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen, |
413 | | const unsigned char *in, size_t inlen) |
414 | 0 | { |
415 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
416 | |
|
417 | 0 | if (!ossl_prov_is_running()) |
418 | 0 | return 0; |
419 | | |
420 | 0 | switch (prsactx->op) { |
421 | 0 | case KEM_OP_RSASVE: |
422 | 0 | return rsasve_recover(prsactx, out, outlen, in, inlen); |
423 | 0 | default: |
424 | 0 | return -2; |
425 | 0 | } |
426 | 0 | } |
427 | | |
428 | | const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = { |
429 | | { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx }, |
430 | | { OSSL_FUNC_KEM_ENCAPSULATE_INIT, |
431 | | (void (*)(void))rsakem_encapsulate_init }, |
432 | | { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate }, |
433 | | { OSSL_FUNC_KEM_DECAPSULATE_INIT, |
434 | | (void (*)(void))rsakem_decapsulate_init }, |
435 | | { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover }, |
436 | | { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx }, |
437 | | { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx }, |
438 | | { OSSL_FUNC_KEM_GET_CTX_PARAMS, |
439 | | (void (*)(void))rsakem_get_ctx_params }, |
440 | | { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS, |
441 | | (void (*)(void))rsakem_gettable_ctx_params }, |
442 | | { OSSL_FUNC_KEM_SET_CTX_PARAMS, |
443 | | (void (*)(void))rsakem_set_ctx_params }, |
444 | | { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS, |
445 | | (void (*)(void))rsakem_settable_ctx_params }, |
446 | | OSSL_DISPATCH_END |
447 | | }; |