Coverage Report

Created: 2026-08-18 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
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
    const BIGNUM *e = NULL;
144
0
    int protect = 0;
145
146
0
    if (!ossl_prov_is_running())
147
0
        return 0;
148
149
0
    if (prsactx == NULL || vrsa == NULL)
150
0
        return 0;
151
152
0
    if (!ossl_rsa_key_op_get_protect(vrsa, operation, &protect))
153
0
        return 0;
154
0
    if (!RSA_up_ref(vrsa))
155
0
        return 0;
156
0
    RSA_free(prsactx->rsa);
157
0
    prsactx->rsa = vrsa;
158
159
    /*
160
     * Reject the trivial public exponent e <= 1. The FIPS module enforces the
161
     * full SP 800-56B §6.4.1.1 constraints via ossl_fips_ind_rsa_key_check()
162
     * below; non-FIPS callers wanting the complete §6.4.2 vetting can use
163
     * EVP_PKEY_public_check().
164
     */
165
0
    RSA_get0_key(prsactx->rsa, NULL, &e, NULL);
166
0
    if (e == NULL || BN_cmp(e, BN_value_one()) <= 0) {
167
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
168
0
        return 0;
169
0
    }
170
171
0
    OSSL_FIPS_IND_SET_APPROVED(prsactx)
172
0
    if (!rsakem_set_ctx_params(prsactx, params))
173
0
        return 0;
174
#ifdef FIPS_MODULE
175
    if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx),
176
            OSSL_FIPS_IND_SETTABLE0, prsactx->libctx,
177
            prsactx->rsa, desc, protect))
178
        return 0;
179
#endif
180
0
    return 1;
181
0
}
182
183
static int rsakem_encapsulate_init(void *vprsactx, void *vrsa,
184
    const OSSL_PARAM params[])
185
0
{
186
0
    return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCAPSULATE,
187
0
        "RSA Encapsulate Init");
188
0
}
189
190
static int rsakem_decapsulate_init(void *vprsactx, void *vrsa,
191
    const OSSL_PARAM params[])
192
0
{
193
0
    return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECAPSULATE,
194
0
        "RSA Decapsulate Init");
195
0
}
196
197
static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
198
0
{
199
0
    PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx;
200
0
    struct rsakem_get_ctx_params_st p;
201
202
0
    if (ctx == NULL || !rsakem_get_ctx_params_decoder(params, &p))
203
0
        return 0;
204
205
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
206
0
        return 0;
207
0
    return 1;
208
0
}
209
210
static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *vprsactx,
211
    ossl_unused void *provctx)
212
0
{
213
0
    return rsakem_get_ctx_params_list;
214
0
}
215
216
static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
217
0
{
218
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
219
0
    struct rsakem_set_ctx_params_st p;
220
0
    int op;
221
222
0
    if (prsactx == NULL || !rsakem_set_ctx_params_decoder(params, &p))
223
0
        return 0;
224
225
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0,
226
0
            p.ind_k))
227
0
        return 0;
228
229
0
    if (p.op != NULL) {
230
0
        if (p.op->data_type != OSSL_PARAM_UTF8_STRING)
231
0
            return 0;
232
0
        op = rsakem_opname2id(p.op->data);
233
0
        if (op < 0)
234
0
            return 0;
235
0
        prsactx->op = op;
236
0
    }
237
0
    return 1;
238
0
}
239
240
static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *vprsactx,
241
    ossl_unused void *provctx)
242
0
{
243
0
    return rsakem_set_ctx_params_list;
244
0
}
245
246
/*
247
 * NIST.SP.800-56Br2
248
 * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
249
 *
250
 * Generate a random in the range 1 < z < (n – 1)
251
 */
252
static int rsasve_gen_rand_bytes(RSA *rsa_pub,
253
    unsigned char *out, int outlen)
254
0
{
255
0
    int ret = 0;
256
0
    BN_CTX *bnctx;
257
0
    BIGNUM *z, *nminus3;
258
259
0
    bnctx = BN_CTX_secure_new_ex(ossl_rsa_get0_libctx(rsa_pub));
260
0
    if (bnctx == NULL)
261
0
        return 0;
262
263
    /*
264
     * Generate a random in the range 1 < z < (n – 1).
265
     * Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max
266
     * We can achieve this by adding 2.. but then we need to subtract 3 from
267
     * the upper bound i.e: 2 + (0 <= r < (n - 3))
268
     */
269
0
    BN_CTX_start(bnctx);
270
0
    nminus3 = BN_CTX_get(bnctx);
271
0
    z = BN_CTX_get(bnctx);
272
0
    ret = (z != NULL
273
0
        && (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL)
274
0
        && BN_sub_word(nminus3, 3)
275
0
        && BN_priv_rand_range_ex(z, nminus3, 0, bnctx)
276
0
        && BN_add_word(z, 2)
277
0
        && (BN_bn2binpad(z, out, outlen) == outlen));
278
0
    BN_CTX_end(bnctx);
279
0
    BN_CTX_free(bnctx);
280
0
    return ret;
281
0
}
282
283
/*
284
 * NIST.SP.800-56Br2
285
 * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
286
 */
287
static int rsasve_generate(PROV_RSA_CTX *prsactx,
288
    unsigned char *out, size_t *outlen,
289
    unsigned char *secret, size_t *secretlen)
290
0
{
291
0
    int ret;
292
0
    size_t nlen;
293
294
    /* Step (1): nlen = Ceil(len(n)/8) */
295
0
    nlen = RSA_size(prsactx->rsa);
296
297
0
    if (out == NULL) {
298
0
        if (nlen == 0) {
299
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
300
0
            return 0;
301
0
        }
302
0
        if (outlen == NULL && secretlen == NULL)
303
0
            return 0;
304
0
        if (outlen != NULL)
305
0
            *outlen = nlen;
306
0
        if (secretlen != NULL)
307
0
            *secretlen = nlen;
308
0
        return 1;
309
0
    }
310
311
    /*
312
     * If outlen is specified, then it must report the length
313
     * of the out buffer on input so that we can confirm
314
     * its size is sufficient for encapsulation
315
     */
316
0
    if (outlen != NULL && *outlen < nlen) {
317
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
318
0
        return 0;
319
0
    }
320
321
    /*
322
     * Step (2): Generate a random byte string z of nlen bytes where
323
     *            1 < z < n - 1
324
     */
325
0
    if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, (int)nlen))
326
0
        return 0;
327
328
    /* Step(3): out = RSAEP((n,e), z) */
329
0
    ret = RSA_public_encrypt((int)nlen, secret, out, prsactx->rsa,
330
0
        RSA_NO_PADDING);
331
0
    if (ret <= 0 || ret != (int)nlen) {
332
0
        OPENSSL_cleanse(secret, nlen);
333
0
        return 0;
334
0
    }
335
336
0
    if (outlen != NULL)
337
0
        *outlen = nlen;
338
0
    if (secretlen != NULL)
339
0
        *secretlen = nlen;
340
341
0
    return 1;
342
0
}
343
344
/**
345
 * rsasve_recover - Recovers a secret value from ciphertext using an RSA
346
 * private key.  Once, recovered, the secret value is considered to be a
347
 * shared secret.  Algorithm is performed as per
348
 * NIST SP 800-56B Rev 2
349
 * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER).
350
 *
351
 * This function performs RSA decryption using the private key from the
352
 * provided RSA context (`prsactx`). It takes the input ciphertext, decrypts
353
 * it, and writes the decrypted message to the output buffer.
354
 *
355
 * @prsactx:      The RSA context containing the private key.
356
 * @out:          The output buffer to store the decrypted message.
357
 * @outlen:       On input, the size of the output buffer. On successful
358
 *                completion, the actual length of the decrypted message.
359
 * @in:           The input buffer containing the ciphertext to be decrypted.
360
 * @inlen:        The length of the input ciphertext in bytes.
361
 *
362
 * Returns 1 on success, or 0 on error. In case of error, appropriate
363
 * error messages are raised using the ERR_raise function.
364
 */
365
static int rsasve_recover(PROV_RSA_CTX *prsactx,
366
    unsigned char *out, size_t *outlen,
367
    const unsigned char *in, size_t inlen)
368
0
{
369
0
    size_t nlen;
370
0
    int ret;
371
372
    /* Step (1): get the byte length of n */
373
0
    nlen = RSA_size(prsactx->rsa);
374
375
0
    if (out == NULL) {
376
0
        if (nlen == 0) {
377
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
378
0
            return 0;
379
0
        }
380
0
        *outlen = nlen;
381
0
        return 1;
382
0
    }
383
384
    /*
385
     * Step (2): check the input ciphertext 'inlen' matches the nlen
386
     * and that outlen is at least nlen bytes
387
     */
388
0
    if (inlen != nlen) {
389
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
390
0
        return 0;
391
0
    }
392
393
    /*
394
     * If outlen is specified, then it must report the length
395
     * of the out buffer, so that we can confirm that it is of
396
     * sufficient size to hold the output of decapsulation
397
     */
398
0
    if (outlen != NULL && *outlen < nlen) {
399
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
400
0
        return 0;
401
0
    }
402
403
0
#ifndef FIPS_MODULE
404
    /*
405
     * Reject clearly degenerate ciphertexts, c in {0, 1, n-1}.
406
     *
407
     * SP 800-56B Rev 2, 7.1.2.1 requires RSADP to enforce 1 < c < n-1.  In a
408
     * FIPS build that bound is applied by the RSADP primitive itself (see
409
     * crypto/rsa/rsa_ossl.c, guarded by FIPS_MODULE), where it is also needed
410
     * for KTS-OAEP; the primitive does not apply it in a non-FIPS build, so
411
     * enforce it here for RSASVE.  Raise the same errors as the primitive so
412
     * the behaviour matches in both builds; keep the two sites in step.
413
     */
414
0
    {
415
0
        const BIGNUM *n = RSA_get0_n(prsactx->rsa);
416
0
        BIGNUM *c = BN_new();
417
0
        BIGNUM *nminus1 = BN_new();
418
0
        int reason = 0;
419
420
0
        if (n == NULL || c == NULL || nminus1 == NULL
421
0
            || BN_bin2bn(in, (int)inlen, c) == NULL
422
0
            || BN_copy(nminus1, n) == NULL
423
0
            || !BN_sub_word(nminus1, 1)) {
424
0
            BN_free(c);
425
0
            BN_free(nminus1);
426
0
            return 0;
427
0
        }
428
0
        if (BN_ucmp(c, BN_value_one()) <= 0)
429
0
            reason = RSA_R_DATA_TOO_SMALL;
430
0
        else if (BN_ucmp(c, nminus1) >= 0)
431
0
            reason = RSA_R_DATA_TOO_LARGE_FOR_MODULUS;
432
0
        BN_free(c);
433
0
        BN_free(nminus1);
434
0
        if (reason != 0) {
435
0
            ERR_raise(ERR_LIB_RSA, reason);
436
0
            return 0;
437
0
        }
438
0
    }
439
0
#endif
440
441
    /* Step (3): out = RSADP((n,d), in) */
442
0
    ret = RSA_private_decrypt((int)inlen, in, out, prsactx->rsa, RSA_NO_PADDING);
443
0
    if (ret > 0 && outlen != NULL)
444
0
        *outlen = ret;
445
0
    return ret > 0;
446
0
}
447
448
static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen,
449
    unsigned char *secret, size_t *secretlen)
450
0
{
451
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
452
453
0
    if (!ossl_prov_is_running())
454
0
        return 0;
455
456
0
    switch (prsactx->op) {
457
0
    case KEM_OP_RSASVE:
458
0
        return rsasve_generate(prsactx, out, outlen, secret, secretlen);
459
0
    default:
460
0
        return -2;
461
0
    }
462
0
}
463
464
static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen,
465
    const unsigned char *in, size_t inlen)
466
0
{
467
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
468
469
0
    if (!ossl_prov_is_running())
470
0
        return 0;
471
472
0
    switch (prsactx->op) {
473
0
    case KEM_OP_RSASVE:
474
0
        return rsasve_recover(prsactx, out, outlen, in, inlen);
475
0
    default:
476
0
        return -2;
477
0
    }
478
0
}
479
480
const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = {
481
    { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx },
482
    { OSSL_FUNC_KEM_ENCAPSULATE_INIT,
483
        (void (*)(void))rsakem_encapsulate_init },
484
    { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate },
485
    { OSSL_FUNC_KEM_DECAPSULATE_INIT,
486
        (void (*)(void))rsakem_decapsulate_init },
487
    { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover },
488
    { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx },
489
    { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx },
490
    { OSSL_FUNC_KEM_GET_CTX_PARAMS,
491
        (void (*)(void))rsakem_get_ctx_params },
492
    { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS,
493
        (void (*)(void))rsakem_gettable_ctx_params },
494
    { OSSL_FUNC_KEM_SET_CTX_PARAMS,
495
        (void (*)(void))rsakem_set_ctx_params },
496
    { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,
497
        (void (*)(void))rsakem_settable_ctx_params },
498
    OSSL_DISPATCH_END
499
};