/src/openssl36/providers/implementations/kem/rsa_kem.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020-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 | | /* clang-format off */ |
10 | | |
11 | | /* clang-format on */ |
12 | | |
13 | | /* |
14 | | * RSA low level APIs are deprecated for public use, but still ok for |
15 | | * internal use. |
16 | | */ |
17 | | #include "internal/deprecated.h" |
18 | | #include "internal/nelem.h" |
19 | | #include <openssl/crypto.h> |
20 | | #include <openssl/evp.h> |
21 | | #include <openssl/core_dispatch.h> |
22 | | #include <openssl/core_names.h> |
23 | | #include <openssl/rsa.h> |
24 | | #include <openssl/params.h> |
25 | | #include <openssl/err.h> |
26 | | #include <openssl/proverr.h> |
27 | | #include "crypto/rsa.h" |
28 | | #include "internal/cryptlib.h" |
29 | | #include "prov/provider_ctx.h" |
30 | | #include "prov/providercommon.h" |
31 | | #include "prov/implementations.h" |
32 | | #include "prov/securitycheck.h" |
33 | | |
34 | | static OSSL_FUNC_kem_newctx_fn rsakem_newctx; |
35 | | static OSSL_FUNC_kem_encapsulate_init_fn rsakem_encapsulate_init; |
36 | | static OSSL_FUNC_kem_encapsulate_fn rsakem_generate; |
37 | | static OSSL_FUNC_kem_decapsulate_init_fn rsakem_decapsulate_init; |
38 | | static OSSL_FUNC_kem_decapsulate_fn rsakem_recover; |
39 | | static OSSL_FUNC_kem_freectx_fn rsakem_freectx; |
40 | | static OSSL_FUNC_kem_dupctx_fn rsakem_dupctx; |
41 | | static OSSL_FUNC_kem_get_ctx_params_fn rsakem_get_ctx_params; |
42 | | static OSSL_FUNC_kem_gettable_ctx_params_fn rsakem_gettable_ctx_params; |
43 | | static OSSL_FUNC_kem_set_ctx_params_fn rsakem_set_ctx_params; |
44 | | static OSSL_FUNC_kem_settable_ctx_params_fn rsakem_settable_ctx_params; |
45 | | |
46 | | /* |
47 | | * Only the KEM for RSASVE as defined in SP800-56b r2 is implemented |
48 | | * currently. |
49 | | */ |
50 | | #define KEM_OP_UNDEFINED -1 |
51 | 0 | #define KEM_OP_RSASVE 0 |
52 | | |
53 | | /* |
54 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
55 | | * We happen to know that our KEYMGMT simply passes RSA structures, so |
56 | | * we use that here too. |
57 | | */ |
58 | | typedef struct { |
59 | | OSSL_LIB_CTX *libctx; |
60 | | RSA *rsa; |
61 | | int op; |
62 | | OSSL_FIPS_IND_DECLARE |
63 | | } PROV_RSA_CTX; |
64 | | |
65 | | static const OSSL_ITEM rsakem_opname_id_map[] = { |
66 | | { KEM_OP_RSASVE, OSSL_KEM_PARAM_OPERATION_RSASVE }, |
67 | | }; |
68 | | |
69 | | static int name2id(const char *name, const OSSL_ITEM *map, size_t sz) |
70 | 0 | { |
71 | 0 | size_t i; |
72 | |
|
73 | 0 | if (name == NULL) |
74 | 0 | return -1; |
75 | | |
76 | 0 | for (i = 0; i < sz; ++i) { |
77 | 0 | if (OPENSSL_strcasecmp(map[i].ptr, name) == 0) |
78 | 0 | return map[i].id; |
79 | 0 | } |
80 | 0 | return -1; |
81 | 0 | } |
82 | | |
83 | | static int rsakem_opname2id(const char *name) |
84 | 0 | { |
85 | 0 | return name2id(name, rsakem_opname_id_map, OSSL_NELEM(rsakem_opname_id_map)); |
86 | 0 | } |
87 | | |
88 | | static void *rsakem_newctx(void *provctx) |
89 | 0 | { |
90 | 0 | PROV_RSA_CTX *prsactx; |
91 | |
|
92 | 0 | if (!ossl_prov_is_running()) |
93 | 0 | return NULL; |
94 | | |
95 | 0 | prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX)); |
96 | 0 | if (prsactx == NULL) |
97 | 0 | return NULL; |
98 | 0 | prsactx->libctx = PROV_LIBCTX_OF(provctx); |
99 | 0 | prsactx->op = KEM_OP_RSASVE; |
100 | 0 | OSSL_FIPS_IND_INIT(prsactx) |
101 | |
|
102 | 0 | return prsactx; |
103 | 0 | } |
104 | | |
105 | | static void rsakem_freectx(void *vprsactx) |
106 | 0 | { |
107 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
108 | |
|
109 | 0 | RSA_free(prsactx->rsa); |
110 | 0 | OPENSSL_free(prsactx); |
111 | 0 | } |
112 | | |
113 | | static void *rsakem_dupctx(void *vprsactx) |
114 | 0 | { |
115 | 0 | PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx; |
116 | 0 | PROV_RSA_CTX *dstctx; |
117 | |
|
118 | 0 | if (!ossl_prov_is_running()) |
119 | 0 | return NULL; |
120 | | |
121 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
122 | 0 | if (dstctx == NULL) |
123 | 0 | return NULL; |
124 | | |
125 | 0 | *dstctx = *srcctx; |
126 | 0 | if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) { |
127 | 0 | OPENSSL_free(dstctx); |
128 | 0 | return NULL; |
129 | 0 | } |
130 | 0 | return dstctx; |
131 | 0 | } |
132 | | |
133 | | static int rsakem_init(void *vprsactx, void *vrsa, |
134 | | const OSSL_PARAM params[], int operation, |
135 | | const char *desc) |
136 | 0 | { |
137 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
138 | 0 | const BIGNUM *e = NULL; |
139 | 0 | int protect = 0; |
140 | |
|
141 | 0 | if (!ossl_prov_is_running()) |
142 | 0 | return 0; |
143 | | |
144 | 0 | if (prsactx == NULL || vrsa == NULL) |
145 | 0 | return 0; |
146 | | |
147 | 0 | if (!ossl_rsa_key_op_get_protect(vrsa, operation, &protect)) |
148 | 0 | return 0; |
149 | 0 | if (!RSA_up_ref(vrsa)) |
150 | 0 | return 0; |
151 | 0 | RSA_free(prsactx->rsa); |
152 | 0 | prsactx->rsa = vrsa; |
153 | | |
154 | | /* |
155 | | * Reject the trivial public exponent e <= 1. The FIPS module enforces the |
156 | | * full SP 800-56B §6.4.1.1 constraints via ossl_fips_ind_rsa_key_check() |
157 | | * below; non-FIPS callers wanting the complete §6.4.2 vetting can use |
158 | | * EVP_PKEY_public_check(). |
159 | | */ |
160 | 0 | RSA_get0_key(prsactx->rsa, NULL, &e, NULL); |
161 | 0 | if (e == NULL || BN_cmp(e, BN_value_one()) <= 0) { |
162 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
163 | 0 | return 0; |
164 | 0 | } |
165 | | |
166 | 0 | OSSL_FIPS_IND_SET_APPROVED(prsactx) |
167 | 0 | if (!rsakem_set_ctx_params(prsactx, params)) |
168 | 0 | return 0; |
169 | | #ifdef FIPS_MODULE |
170 | | if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx), |
171 | | OSSL_FIPS_IND_SETTABLE0, prsactx->libctx, |
172 | | prsactx->rsa, desc, protect)) |
173 | | return 0; |
174 | | #endif |
175 | 0 | return 1; |
176 | 0 | } |
177 | | |
178 | | static int rsakem_encapsulate_init(void *vprsactx, void *vrsa, |
179 | | const OSSL_PARAM params[]) |
180 | 0 | { |
181 | 0 | return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCAPSULATE, |
182 | 0 | "RSA Encapsulate Init"); |
183 | 0 | } |
184 | | |
185 | | static int rsakem_decapsulate_init(void *vprsactx, void *vrsa, |
186 | | const OSSL_PARAM params[]) |
187 | 0 | { |
188 | 0 | return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECAPSULATE, |
189 | 0 | "RSA Decapsulate Init"); |
190 | 0 | } |
191 | | |
192 | | /* clang-format off */ |
193 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
194 | | #ifndef rsakem_get_ctx_params_list |
195 | | static const OSSL_PARAM rsakem_get_ctx_params_list[] = { |
196 | | # if defined(FIPS_MODULE) |
197 | | OSSL_PARAM_int(OSSL_KEM_PARAM_FIPS_APPROVED_INDICATOR, NULL), |
198 | | # endif |
199 | | OSSL_PARAM_END |
200 | | }; |
201 | | #endif |
202 | | |
203 | | #ifndef rsakem_get_ctx_params_st |
204 | | struct rsakem_get_ctx_params_st { |
205 | | # if defined(FIPS_MODULE) |
206 | | OSSL_PARAM *ind; |
207 | | # else |
208 | | int dummy; /* unused */ |
209 | | # endif |
210 | | }; |
211 | | #endif |
212 | | |
213 | | #ifndef rsakem_get_ctx_params_decoder |
214 | | static int rsakem_get_ctx_params_decoder |
215 | | (const OSSL_PARAM *p, struct rsakem_get_ctx_params_st *r) |
216 | 0 | { |
217 | 0 | const char *s; |
218 | |
|
219 | 0 | memset(r, 0, sizeof(*r)); |
220 | 0 | if (p != NULL) |
221 | 0 | for (; (s = p->key) != NULL; p++) |
222 | | # if defined(FIPS_MODULE) |
223 | | if (ossl_likely(strcmp("fips-indicator", s + 0) == 0)) { |
224 | | /* OSSL_KEM_PARAM_FIPS_APPROVED_INDICATOR */ |
225 | | if (ossl_unlikely(r->ind != NULL)) { |
226 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
227 | | "param %s is repeated", s); |
228 | | return 0; |
229 | | } |
230 | | r->ind = (OSSL_PARAM *)p; |
231 | | } |
232 | | # else |
233 | 0 | ; |
234 | 0 | # endif |
235 | 0 | return 1; |
236 | 0 | } |
237 | | #endif |
238 | | /* End of machine generated */ |
239 | | /* clang-format on */ |
240 | | |
241 | | static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params) |
242 | 0 | { |
243 | 0 | PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx; |
244 | 0 | struct rsakem_get_ctx_params_st p; |
245 | |
|
246 | 0 | if (ctx == NULL || !rsakem_get_ctx_params_decoder(params, &p)) |
247 | 0 | return 0; |
248 | | |
249 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
250 | 0 | return 0; |
251 | 0 | return 1; |
252 | 0 | } |
253 | | |
254 | | static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *vprsactx, |
255 | | ossl_unused void *provctx) |
256 | 0 | { |
257 | 0 | return rsakem_get_ctx_params_list; |
258 | 0 | } |
259 | | |
260 | | /* clang-format off */ |
261 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
262 | | #ifndef rsakem_set_ctx_params_list |
263 | | static const OSSL_PARAM rsakem_set_ctx_params_list[] = { |
264 | | OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0), |
265 | | # if defined(FIPS_MODULE) |
266 | | OSSL_PARAM_int(OSSL_KEM_PARAM_FIPS_KEY_CHECK, NULL), |
267 | | # endif |
268 | | OSSL_PARAM_END |
269 | | }; |
270 | | #endif |
271 | | |
272 | | #ifndef rsakem_set_ctx_params_st |
273 | | struct rsakem_set_ctx_params_st { |
274 | | # if defined(FIPS_MODULE) |
275 | | OSSL_PARAM *ind_k; |
276 | | # endif |
277 | | OSSL_PARAM *op; |
278 | | }; |
279 | | #endif |
280 | | |
281 | | #ifndef rsakem_set_ctx_params_decoder |
282 | | static int rsakem_set_ctx_params_decoder |
283 | | (const OSSL_PARAM *p, struct rsakem_set_ctx_params_st *r) |
284 | 0 | { |
285 | 0 | const char *s; |
286 | |
|
287 | 0 | memset(r, 0, sizeof(*r)); |
288 | 0 | if (p != NULL) |
289 | 0 | for (; (s = p->key) != NULL; p++) |
290 | 0 | switch(s[0]) { |
291 | 0 | default: |
292 | 0 | break; |
293 | 0 | case 'k': |
294 | | # if defined(FIPS_MODULE) |
295 | | if (ossl_likely(strcmp("ey-check", s + 1) == 0)) { |
296 | | /* OSSL_KEM_PARAM_FIPS_KEY_CHECK */ |
297 | | if (ossl_unlikely(r->ind_k != NULL)) { |
298 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
299 | | "param %s is repeated", s); |
300 | | return 0; |
301 | | } |
302 | | r->ind_k = (OSSL_PARAM *)p; |
303 | | } |
304 | | # endif |
305 | 0 | break; |
306 | 0 | case 'o': |
307 | 0 | if (ossl_likely(strcmp("peration", s + 1) == 0)) { |
308 | | /* OSSL_KEM_PARAM_OPERATION */ |
309 | 0 | if (ossl_unlikely(r->op != NULL)) { |
310 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
311 | 0 | "param %s is repeated", s); |
312 | 0 | return 0; |
313 | 0 | } |
314 | 0 | r->op = (OSSL_PARAM *)p; |
315 | 0 | } |
316 | 0 | } |
317 | 0 | return 1; |
318 | 0 | } |
319 | | #endif |
320 | | /* End of machine generated */ |
321 | | /* clang-format on */ |
322 | | |
323 | | static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) |
324 | 0 | { |
325 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
326 | 0 | struct rsakem_set_ctx_params_st p; |
327 | 0 | int op; |
328 | |
|
329 | 0 | if (prsactx == NULL || !rsakem_set_ctx_params_decoder(params, &p)) |
330 | 0 | return 0; |
331 | | |
332 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0, |
333 | 0 | p.ind_k)) |
334 | 0 | return 0; |
335 | | |
336 | 0 | if (p.op != NULL) { |
337 | 0 | if (p.op->data_type != OSSL_PARAM_UTF8_STRING) |
338 | 0 | return 0; |
339 | 0 | op = rsakem_opname2id(p.op->data); |
340 | 0 | if (op < 0) |
341 | 0 | return 0; |
342 | 0 | prsactx->op = op; |
343 | 0 | } |
344 | 0 | return 1; |
345 | 0 | } |
346 | | |
347 | | static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *vprsactx, |
348 | | ossl_unused void *provctx) |
349 | 16 | { |
350 | 16 | return rsakem_set_ctx_params_list; |
351 | 16 | } |
352 | | |
353 | | /* |
354 | | * NIST.SP.800-56Br2 |
355 | | * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE). |
356 | | * |
357 | | * Generate a random in the range 1 < z < (n – 1) |
358 | | */ |
359 | | static int rsasve_gen_rand_bytes(RSA *rsa_pub, |
360 | | unsigned char *out, int outlen) |
361 | 0 | { |
362 | 0 | int ret = 0; |
363 | 0 | BN_CTX *bnctx; |
364 | 0 | BIGNUM *z, *nminus3; |
365 | |
|
366 | 0 | bnctx = BN_CTX_secure_new_ex(ossl_rsa_get0_libctx(rsa_pub)); |
367 | 0 | if (bnctx == NULL) |
368 | 0 | return 0; |
369 | | |
370 | | /* |
371 | | * Generate a random in the range 1 < z < (n – 1). |
372 | | * Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max |
373 | | * We can achieve this by adding 2.. but then we need to subtract 3 from |
374 | | * the upper bound i.e: 2 + (0 <= r < (n - 3)) |
375 | | */ |
376 | 0 | BN_CTX_start(bnctx); |
377 | 0 | nminus3 = BN_CTX_get(bnctx); |
378 | 0 | z = BN_CTX_get(bnctx); |
379 | 0 | ret = (z != NULL |
380 | 0 | && (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL) |
381 | 0 | && BN_sub_word(nminus3, 3) |
382 | 0 | && BN_priv_rand_range_ex(z, nminus3, 0, bnctx) |
383 | 0 | && BN_add_word(z, 2) |
384 | 0 | && (BN_bn2binpad(z, out, outlen) == outlen)); |
385 | 0 | BN_CTX_end(bnctx); |
386 | 0 | BN_CTX_free(bnctx); |
387 | 0 | return ret; |
388 | 0 | } |
389 | | |
390 | | /* |
391 | | * NIST.SP.800-56Br2 |
392 | | * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE). |
393 | | */ |
394 | | static int rsasve_generate(PROV_RSA_CTX *prsactx, |
395 | | unsigned char *out, size_t *outlen, |
396 | | unsigned char *secret, size_t *secretlen) |
397 | 0 | { |
398 | 0 | int ret; |
399 | 0 | size_t nlen; |
400 | | |
401 | | /* Step (1): nlen = Ceil(len(n)/8) */ |
402 | 0 | nlen = RSA_size(prsactx->rsa); |
403 | |
|
404 | 0 | if (out == NULL) { |
405 | 0 | if (nlen == 0) { |
406 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
407 | 0 | return 0; |
408 | 0 | } |
409 | 0 | if (outlen == NULL && secretlen == NULL) |
410 | 0 | return 0; |
411 | 0 | if (outlen != NULL) |
412 | 0 | *outlen = nlen; |
413 | 0 | if (secretlen != NULL) |
414 | 0 | *secretlen = nlen; |
415 | 0 | return 1; |
416 | 0 | } |
417 | | |
418 | | /* |
419 | | * If outlen is specified, then it must report the length |
420 | | * of the out buffer on input so that we can confirm |
421 | | * its size is sufficient for encapsulation |
422 | | */ |
423 | 0 | if (outlen != NULL && *outlen < nlen) { |
424 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
425 | 0 | return 0; |
426 | 0 | } |
427 | | |
428 | | /* |
429 | | * Step (2): Generate a random byte string z of nlen bytes where |
430 | | * 1 < z < n - 1 |
431 | | */ |
432 | 0 | if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, (int)nlen)) |
433 | 0 | return 0; |
434 | | |
435 | | /* Step(3): out = RSAEP((n,e), z) */ |
436 | 0 | ret = RSA_public_encrypt((int)nlen, secret, out, prsactx->rsa, |
437 | 0 | RSA_NO_PADDING); |
438 | 0 | if (ret <= 0 || ret != (int)nlen) { |
439 | 0 | OPENSSL_cleanse(secret, nlen); |
440 | 0 | return 0; |
441 | 0 | } |
442 | | |
443 | 0 | if (outlen != NULL) |
444 | 0 | *outlen = nlen; |
445 | 0 | if (secretlen != NULL) |
446 | 0 | *secretlen = nlen; |
447 | |
|
448 | 0 | return 1; |
449 | 0 | } |
450 | | |
451 | | /** |
452 | | * rsasve_recover - Recovers a secret value from ciphertext using an RSA |
453 | | * private key. Once, recovered, the secret value is considered to be a |
454 | | * shared secret. Algorithm is performed as per NIST SP 800-56B Rev 2 |
455 | | * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER). |
456 | | * |
457 | | * This function performs RSA decryption using the private key from the |
458 | | * provided RSA context (`prsactx`). It takes the input ciphertext, decrypts |
459 | | * it, and writes the decrypted message to the output buffer. |
460 | | * |
461 | | * @prsactx: The RSA context containing the private key. |
462 | | * @out: The output buffer to store the decrypted message. |
463 | | * @outlen: On input, the size of the output buffer. On successful |
464 | | * completion, the actual length of the decrypted message. |
465 | | * @in: The input buffer containing the ciphertext to be decrypted. |
466 | | * @inlen: The length of the input ciphertext in bytes. |
467 | | * |
468 | | * Returns 1 on success, or 0 on error. In case of error, appropriate |
469 | | * error messages are raised using the ERR_raise function. |
470 | | */ |
471 | | static int rsasve_recover(PROV_RSA_CTX *prsactx, |
472 | | unsigned char *out, size_t *outlen, |
473 | | const unsigned char *in, size_t inlen) |
474 | 0 | { |
475 | 0 | size_t nlen; |
476 | 0 | int ret; |
477 | | |
478 | | /* Step (1): get the byte length of n */ |
479 | 0 | nlen = RSA_size(prsactx->rsa); |
480 | |
|
481 | 0 | if (out == NULL) { |
482 | 0 | if (nlen == 0) { |
483 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
484 | 0 | return 0; |
485 | 0 | } |
486 | 0 | *outlen = nlen; |
487 | 0 | return 1; |
488 | 0 | } |
489 | | |
490 | | /* |
491 | | * Step (2): check the input ciphertext 'inlen' matches the nlen |
492 | | * and that outlen is at least nlen bytes |
493 | | */ |
494 | 0 | if (inlen != nlen) { |
495 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); |
496 | 0 | return 0; |
497 | 0 | } |
498 | | |
499 | | /* |
500 | | * If outlen is specified, then it must report the length |
501 | | * of the out buffer, so that we can confirm that it is of |
502 | | * sufficient size to hold the output of decapsulation |
503 | | */ |
504 | 0 | if (outlen != NULL && *outlen < nlen) { |
505 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
506 | 0 | return 0; |
507 | 0 | } |
508 | | |
509 | 0 | #ifndef FIPS_MODULE |
510 | | /* |
511 | | * Reject clearly degenerate ciphertexts, c in {0, 1, n-1}. |
512 | | * |
513 | | * SP 800-56B Rev 2, 7.1.2.1 requires RSADP to enforce 1 < c < n-1. In a |
514 | | * FIPS build that bound is applied by the RSADP primitive itself (see |
515 | | * crypto/rsa/rsa_ossl.c, guarded by FIPS_MODULE), where it is also needed |
516 | | * for KTS-OAEP; the primitive does not apply it in a non-FIPS build, so |
517 | | * enforce it here for RSASVE. Raise the same errors as the primitive so |
518 | | * the behaviour matches in both builds; keep the two sites in step. |
519 | | */ |
520 | 0 | { |
521 | 0 | const BIGNUM *n = RSA_get0_n(prsactx->rsa); |
522 | 0 | BIGNUM *c = BN_new(); |
523 | 0 | BIGNUM *nminus1 = BN_new(); |
524 | 0 | int reason = 0; |
525 | |
|
526 | 0 | if (n == NULL || c == NULL || nminus1 == NULL |
527 | 0 | || BN_bin2bn(in, (int)inlen, c) == NULL |
528 | 0 | || BN_copy(nminus1, n) == NULL |
529 | 0 | || !BN_sub_word(nminus1, 1)) { |
530 | 0 | BN_free(c); |
531 | 0 | BN_free(nminus1); |
532 | 0 | return 0; |
533 | 0 | } |
534 | 0 | if (BN_ucmp(c, BN_value_one()) <= 0) |
535 | 0 | reason = RSA_R_DATA_TOO_SMALL; |
536 | 0 | else if (BN_ucmp(c, nminus1) >= 0) |
537 | 0 | reason = RSA_R_DATA_TOO_LARGE_FOR_MODULUS; |
538 | 0 | BN_free(c); |
539 | 0 | BN_free(nminus1); |
540 | 0 | if (reason != 0) { |
541 | 0 | ERR_raise(ERR_LIB_RSA, reason); |
542 | 0 | return 0; |
543 | 0 | } |
544 | 0 | } |
545 | 0 | #endif |
546 | | |
547 | | /* Step (3): out = RSADP((n,d), in) */ |
548 | 0 | ret = RSA_private_decrypt((int)inlen, in, out, prsactx->rsa, RSA_NO_PADDING); |
549 | 0 | if (ret > 0 && outlen != NULL) |
550 | 0 | *outlen = ret; |
551 | 0 | return ret > 0; |
552 | 0 | } |
553 | | |
554 | | static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen, |
555 | | unsigned char *secret, size_t *secretlen) |
556 | 0 | { |
557 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
558 | |
|
559 | 0 | if (!ossl_prov_is_running()) |
560 | 0 | return 0; |
561 | | |
562 | 0 | switch (prsactx->op) { |
563 | 0 | case KEM_OP_RSASVE: |
564 | 0 | return rsasve_generate(prsactx, out, outlen, secret, secretlen); |
565 | 0 | default: |
566 | 0 | return -2; |
567 | 0 | } |
568 | 0 | } |
569 | | |
570 | | static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen, |
571 | | const unsigned char *in, size_t inlen) |
572 | 0 | { |
573 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
574 | |
|
575 | 0 | if (!ossl_prov_is_running()) |
576 | 0 | return 0; |
577 | | |
578 | 0 | switch (prsactx->op) { |
579 | 0 | case KEM_OP_RSASVE: |
580 | 0 | return rsasve_recover(prsactx, out, outlen, in, inlen); |
581 | 0 | default: |
582 | 0 | return -2; |
583 | 0 | } |
584 | 0 | } |
585 | | |
586 | | const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = { |
587 | | { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx }, |
588 | | { OSSL_FUNC_KEM_ENCAPSULATE_INIT, |
589 | | (void (*)(void))rsakem_encapsulate_init }, |
590 | | { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate }, |
591 | | { OSSL_FUNC_KEM_DECAPSULATE_INIT, |
592 | | (void (*)(void))rsakem_decapsulate_init }, |
593 | | { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover }, |
594 | | { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx }, |
595 | | { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx }, |
596 | | { OSSL_FUNC_KEM_GET_CTX_PARAMS, |
597 | | (void (*)(void))rsakem_get_ctx_params }, |
598 | | { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS, |
599 | | (void (*)(void))rsakem_gettable_ctx_params }, |
600 | | { OSSL_FUNC_KEM_SET_CTX_PARAMS, |
601 | | (void (*)(void))rsakem_set_ctx_params }, |
602 | | { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS, |
603 | | (void (*)(void))rsakem_settable_ctx_params }, |
604 | | OSSL_DISPATCH_END |
605 | | }; |