Coverage Report

Created: 2026-09-12 06:55

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