Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
/* 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
    int protect = 0;
139
140
0
    if (!ossl_prov_is_running())
141
0
        return 0;
142
143
0
    if (prsactx == NULL || vrsa == NULL)
144
0
        return 0;
145
146
0
    if (!ossl_rsa_key_op_get_protect(vrsa, operation, &protect))
147
0
        return 0;
148
0
    if (!RSA_up_ref(vrsa))
149
0
        return 0;
150
0
    RSA_free(prsactx->rsa);
151
0
    prsactx->rsa = vrsa;
152
153
0
    OSSL_FIPS_IND_SET_APPROVED(prsactx)
154
0
    if (!rsakem_set_ctx_params(prsactx, params))
155
0
        return 0;
156
#ifdef FIPS_MODULE
157
    if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx),
158
            OSSL_FIPS_IND_SETTABLE0, prsactx->libctx,
159
            prsactx->rsa, desc, protect))
160
        return 0;
161
#endif
162
0
    return 1;
163
0
}
164
165
static int rsakem_encapsulate_init(void *vprsactx, void *vrsa,
166
    const OSSL_PARAM params[])
167
0
{
168
0
    return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCAPSULATE,
169
0
        "RSA Encapsulate Init");
170
0
}
171
172
static int rsakem_decapsulate_init(void *vprsactx, void *vrsa,
173
    const OSSL_PARAM params[])
174
0
{
175
0
    return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECAPSULATE,
176
0
        "RSA Decapsulate Init");
177
0
}
178
179
/* clang-format off */
180
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
181
#ifndef rsakem_get_ctx_params_list
182
static const OSSL_PARAM rsakem_get_ctx_params_list[] = {
183
# if defined(FIPS_MODULE)
184
    OSSL_PARAM_int(OSSL_KEM_PARAM_FIPS_APPROVED_INDICATOR, NULL),
185
# endif
186
    OSSL_PARAM_END
187
};
188
#endif
189
190
#ifndef rsakem_get_ctx_params_st
191
struct rsakem_get_ctx_params_st {
192
# if defined(FIPS_MODULE)
193
    OSSL_PARAM *ind;
194
# else
195
    int dummy; /* unused */
196
# endif
197
};
198
#endif
199
200
#ifndef rsakem_get_ctx_params_decoder
201
static int rsakem_get_ctx_params_decoder
202
    (const OSSL_PARAM *p, struct rsakem_get_ctx_params_st *r)
203
0
{
204
0
    const char *s;
205
206
0
    memset(r, 0, sizeof(*r));
207
0
    if (p != NULL)
208
0
        for (; (s = p->key) != NULL; p++)
209
# if defined(FIPS_MODULE)
210
            if (ossl_likely(strcmp("fips-indicator", s + 0) == 0)) {
211
                /* OSSL_KEM_PARAM_FIPS_APPROVED_INDICATOR */
212
                if (ossl_unlikely(r->ind != NULL)) {
213
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
214
                                   "param %s is repeated", s);
215
                    return 0;
216
                }
217
                r->ind = (OSSL_PARAM *)p;
218
            }
219
# else
220
0
            ;
221
0
# endif
222
0
    return 1;
223
0
}
224
#endif
225
/* End of machine generated */
226
/* clang-format on */
227
228
static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
229
0
{
230
0
    PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx;
231
0
    struct rsakem_get_ctx_params_st p;
232
233
0
    if (ctx == NULL || !rsakem_get_ctx_params_decoder(params, &p))
234
0
        return 0;
235
236
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
237
0
        return 0;
238
0
    return 1;
239
0
}
240
241
static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *vprsactx,
242
    ossl_unused void *provctx)
243
0
{
244
0
    return rsakem_get_ctx_params_list;
245
0
}
246
247
/* clang-format off */
248
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
249
#ifndef rsakem_set_ctx_params_list
250
static const OSSL_PARAM rsakem_set_ctx_params_list[] = {
251
    OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0),
252
# if defined(FIPS_MODULE)
253
    OSSL_PARAM_int(OSSL_KEM_PARAM_FIPS_KEY_CHECK, NULL),
254
# endif
255
    OSSL_PARAM_END
256
};
257
#endif
258
259
#ifndef rsakem_set_ctx_params_st
260
struct rsakem_set_ctx_params_st {
261
# if defined(FIPS_MODULE)
262
    OSSL_PARAM *ind_k;
263
# endif
264
    OSSL_PARAM *op;
265
};
266
#endif
267
268
#ifndef rsakem_set_ctx_params_decoder
269
static int rsakem_set_ctx_params_decoder
270
    (const OSSL_PARAM *p, struct rsakem_set_ctx_params_st *r)
271
0
{
272
0
    const char *s;
273
274
0
    memset(r, 0, sizeof(*r));
275
0
    if (p != NULL)
276
0
        for (; (s = p->key) != NULL; p++)
277
0
            switch(s[0]) {
278
0
            default:
279
0
                break;
280
0
            case 'k':
281
# if defined(FIPS_MODULE)
282
                if (ossl_likely(strcmp("ey-check", s + 1) == 0)) {
283
                    /* OSSL_KEM_PARAM_FIPS_KEY_CHECK */
284
                    if (ossl_unlikely(r->ind_k != NULL)) {
285
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
286
                                       "param %s is repeated", s);
287
                        return 0;
288
                    }
289
                    r->ind_k = (OSSL_PARAM *)p;
290
                }
291
# endif
292
0
                break;
293
0
            case 'o':
294
0
                if (ossl_likely(strcmp("peration", s + 1) == 0)) {
295
                    /* OSSL_KEM_PARAM_OPERATION */
296
0
                    if (ossl_unlikely(r->op != NULL)) {
297
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
298
0
                                       "param %s is repeated", s);
299
0
                        return 0;
300
0
                    }
301
0
                    r->op = (OSSL_PARAM *)p;
302
0
                }
303
0
            }
304
0
    return 1;
305
0
}
306
#endif
307
/* End of machine generated */
308
/* clang-format on */
309
310
static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
311
0
{
312
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
313
0
    struct rsakem_set_ctx_params_st p;
314
0
    int op;
315
316
0
    if (prsactx == NULL || !rsakem_set_ctx_params_decoder(params, &p))
317
0
        return 0;
318
319
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0,
320
0
            p.ind_k))
321
0
        return 0;
322
323
0
    if (p.op != NULL) {
324
0
        if (p.op->data_type != OSSL_PARAM_UTF8_STRING)
325
0
            return 0;
326
0
        op = rsakem_opname2id(p.op->data);
327
0
        if (op < 0)
328
0
            return 0;
329
0
        prsactx->op = op;
330
0
    }
331
0
    return 1;
332
0
}
333
334
static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *vprsactx,
335
    ossl_unused void *provctx)
336
15
{
337
15
    return rsakem_set_ctx_params_list;
338
15
}
339
340
/*
341
 * NIST.SP.800-56Br2
342
 * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
343
 *
344
 * Generate a random in the range 1 < z < (n – 1)
345
 */
346
static int rsasve_gen_rand_bytes(RSA *rsa_pub,
347
    unsigned char *out, int outlen)
348
0
{
349
0
    int ret = 0;
350
0
    BN_CTX *bnctx;
351
0
    BIGNUM *z, *nminus3;
352
353
0
    bnctx = BN_CTX_secure_new_ex(ossl_rsa_get0_libctx(rsa_pub));
354
0
    if (bnctx == NULL)
355
0
        return 0;
356
357
    /*
358
     * Generate a random in the range 1 < z < (n – 1).
359
     * Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max
360
     * We can achieve this by adding 2.. but then we need to subtract 3 from
361
     * the upper bound i.e: 2 + (0 <= r < (n - 3))
362
     */
363
0
    BN_CTX_start(bnctx);
364
0
    nminus3 = BN_CTX_get(bnctx);
365
0
    z = BN_CTX_get(bnctx);
366
0
    ret = (z != NULL
367
0
        && (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL)
368
0
        && BN_sub_word(nminus3, 3)
369
0
        && BN_priv_rand_range_ex(z, nminus3, 0, bnctx)
370
0
        && BN_add_word(z, 2)
371
0
        && (BN_bn2binpad(z, out, outlen) == outlen));
372
0
    BN_CTX_end(bnctx);
373
0
    BN_CTX_free(bnctx);
374
0
    return ret;
375
0
}
376
377
/*
378
 * NIST.SP.800-56Br2
379
 * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
380
 */
381
static int rsasve_generate(PROV_RSA_CTX *prsactx,
382
    unsigned char *out, size_t *outlen,
383
    unsigned char *secret, size_t *secretlen)
384
0
{
385
0
    int ret;
386
0
    size_t nlen;
387
388
    /* Step (1): nlen = Ceil(len(n)/8) */
389
0
    nlen = RSA_size(prsactx->rsa);
390
391
0
    if (out == NULL) {
392
0
        if (nlen == 0) {
393
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
394
0
            return 0;
395
0
        }
396
0
        if (outlen == NULL && secretlen == NULL)
397
0
            return 0;
398
0
        if (outlen != NULL)
399
0
            *outlen = nlen;
400
0
        if (secretlen != NULL)
401
0
            *secretlen = nlen;
402
0
        return 1;
403
0
    }
404
405
    /*
406
     * If outlen is specified, then it must report the length
407
     * of the out buffer on input so that we can confirm
408
     * its size is sufficient for encapsulation
409
     */
410
0
    if (outlen != NULL && *outlen < nlen) {
411
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
412
0
        return 0;
413
0
    }
414
415
    /*
416
     * Step (2): Generate a random byte string z of nlen bytes where
417
     *            1 < z < n - 1
418
     */
419
0
    if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, (int)nlen))
420
0
        return 0;
421
422
    /* Step(3): out = RSAEP((n,e), z) */
423
0
    ret = RSA_public_encrypt((int)nlen, secret, out, prsactx->rsa,
424
0
        RSA_NO_PADDING);
425
0
    if (ret) {
426
0
        ret = 1;
427
0
        if (outlen != NULL)
428
0
            *outlen = nlen;
429
0
        if (secretlen != NULL)
430
0
            *secretlen = nlen;
431
0
    } else {
432
0
        OPENSSL_cleanse(secret, nlen);
433
0
    }
434
0
    return ret;
435
0
}
436
437
/**
438
 * rsasve_recover - Recovers a secret value from ciphertext using an RSA
439
 * private key.  Once, recovered, the secret value is considered to be a
440
 * shared secret.  Algorithm is performed as per NIST SP 800-56B Rev 2
441
 * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER).
442
 *
443
 * This function performs RSA decryption using the private key from the
444
 * provided RSA context (`prsactx`). It takes the input ciphertext, decrypts
445
 * it, and writes the decrypted message to the output buffer.
446
 *
447
 * @prsactx:      The RSA context containing the private key.
448
 * @out:          The output buffer to store the decrypted message.
449
 * @outlen:       On input, the size of the output buffer. On successful
450
 *                completion, the actual length of the decrypted message.
451
 * @in:           The input buffer containing the ciphertext to be decrypted.
452
 * @inlen:        The length of the input ciphertext in bytes.
453
 *
454
 * Returns 1 on success, or 0 on error. In case of error, appropriate
455
 * error messages are raised using the ERR_raise function.
456
 */
457
static int rsasve_recover(PROV_RSA_CTX *prsactx,
458
    unsigned char *out, size_t *outlen,
459
    const unsigned char *in, size_t inlen)
460
0
{
461
0
    size_t nlen;
462
0
    int ret;
463
464
    /* Step (1): get the byte length of n */
465
0
    nlen = RSA_size(prsactx->rsa);
466
467
0
    if (out == NULL) {
468
0
        if (nlen == 0) {
469
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
470
0
            return 0;
471
0
        }
472
0
        *outlen = nlen;
473
0
        return 1;
474
0
    }
475
476
    /*
477
     * Step (2): check the input ciphertext 'inlen' matches the nlen
478
     * and that outlen is at least nlen bytes
479
     */
480
0
    if (inlen != nlen) {
481
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
482
0
        return 0;
483
0
    }
484
485
    /*
486
     * If outlen is specified, then it must report the length
487
     * of the out buffer, so that we can confirm that it is of
488
     * sufficient size to hold the output of decapsulation
489
     */
490
0
    if (outlen != NULL && *outlen < nlen) {
491
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
492
0
        return 0;
493
0
    }
494
495
    /* Step (3): out = RSADP((n,d), in) */
496
0
    ret = RSA_private_decrypt((int)inlen, in, out, prsactx->rsa, RSA_NO_PADDING);
497
0
    if (ret > 0 && outlen != NULL)
498
0
        *outlen = ret;
499
0
    return ret > 0;
500
0
}
501
502
static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen,
503
    unsigned char *secret, size_t *secretlen)
504
0
{
505
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
506
507
0
    if (!ossl_prov_is_running())
508
0
        return 0;
509
510
0
    switch (prsactx->op) {
511
0
    case KEM_OP_RSASVE:
512
0
        return rsasve_generate(prsactx, out, outlen, secret, secretlen);
513
0
    default:
514
0
        return -2;
515
0
    }
516
0
}
517
518
static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen,
519
    const unsigned char *in, size_t inlen)
520
0
{
521
0
    PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
522
523
0
    if (!ossl_prov_is_running())
524
0
        return 0;
525
526
0
    switch (prsactx->op) {
527
0
    case KEM_OP_RSASVE:
528
0
        return rsasve_recover(prsactx, out, outlen, in, inlen);
529
0
    default:
530
0
        return -2;
531
0
    }
532
0
}
533
534
const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = {
535
    { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx },
536
    { OSSL_FUNC_KEM_ENCAPSULATE_INIT,
537
        (void (*)(void))rsakem_encapsulate_init },
538
    { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate },
539
    { OSSL_FUNC_KEM_DECAPSULATE_INIT,
540
        (void (*)(void))rsakem_decapsulate_init },
541
    { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover },
542
    { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx },
543
    { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx },
544
    { OSSL_FUNC_KEM_GET_CTX_PARAMS,
545
        (void (*)(void))rsakem_get_ctx_params },
546
    { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS,
547
        (void (*)(void))rsakem_gettable_ctx_params },
548
    { OSSL_FUNC_KEM_SET_CTX_PARAMS,
549
        (void (*)(void))rsakem_set_ctx_params },
550
    { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,
551
        (void (*)(void))rsakem_settable_ctx_params },
552
    OSSL_DISPATCH_END
553
};