Path: blob/main/crypto/openssl/providers/implementations/kem/rsa_kem.c
48383 views
/*1* Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* RSA low level APIs are deprecated for public use, but still ok for11* internal use.12*/13#include "internal/deprecated.h"14#include "internal/nelem.h"15#include <openssl/crypto.h>16#include <openssl/evp.h>17#include <openssl/core_dispatch.h>18#include <openssl/core_names.h>19#include <openssl/rsa.h>20#include <openssl/params.h>21#include <openssl/err.h>22#include <openssl/proverr.h>23#include "crypto/rsa.h"24#include "prov/provider_ctx.h"25#include "prov/providercommon.h"26#include "prov/implementations.h"27#include "prov/securitycheck.h"2829static OSSL_FUNC_kem_newctx_fn rsakem_newctx;30static OSSL_FUNC_kem_encapsulate_init_fn rsakem_encapsulate_init;31static OSSL_FUNC_kem_encapsulate_fn rsakem_generate;32static OSSL_FUNC_kem_decapsulate_init_fn rsakem_decapsulate_init;33static OSSL_FUNC_kem_decapsulate_fn rsakem_recover;34static OSSL_FUNC_kem_freectx_fn rsakem_freectx;35static OSSL_FUNC_kem_dupctx_fn rsakem_dupctx;36static OSSL_FUNC_kem_get_ctx_params_fn rsakem_get_ctx_params;37static OSSL_FUNC_kem_gettable_ctx_params_fn rsakem_gettable_ctx_params;38static OSSL_FUNC_kem_set_ctx_params_fn rsakem_set_ctx_params;39static OSSL_FUNC_kem_settable_ctx_params_fn rsakem_settable_ctx_params;4041/*42* Only the KEM for RSASVE as defined in SP800-56b r2 is implemented43* currently.44*/45#define KEM_OP_UNDEFINED -146#define KEM_OP_RSASVE 04748/*49* What's passed as an actual key is defined by the KEYMGMT interface.50* We happen to know that our KEYMGMT simply passes RSA structures, so51* we use that here too.52*/53typedef struct {54OSSL_LIB_CTX *libctx;55RSA *rsa;56int op;57OSSL_FIPS_IND_DECLARE58} PROV_RSA_CTX;5960static const OSSL_ITEM rsakem_opname_id_map[] = {61{ KEM_OP_RSASVE, OSSL_KEM_PARAM_OPERATION_RSASVE },62};6364static int name2id(const char *name, const OSSL_ITEM *map, size_t sz)65{66size_t i;6768if (name == NULL)69return -1;7071for (i = 0; i < sz; ++i) {72if (OPENSSL_strcasecmp(map[i].ptr, name) == 0)73return map[i].id;74}75return -1;76}7778static int rsakem_opname2id(const char *name)79{80return name2id(name, rsakem_opname_id_map, OSSL_NELEM(rsakem_opname_id_map));81}8283static void *rsakem_newctx(void *provctx)84{85PROV_RSA_CTX *prsactx;8687if (!ossl_prov_is_running())88return NULL;8990prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));91if (prsactx == NULL)92return NULL;93prsactx->libctx = PROV_LIBCTX_OF(provctx);94prsactx->op = KEM_OP_RSASVE;95OSSL_FIPS_IND_INIT(prsactx)9697return prsactx;98}99100static void rsakem_freectx(void *vprsactx)101{102PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;103104RSA_free(prsactx->rsa);105OPENSSL_free(prsactx);106}107108static void *rsakem_dupctx(void *vprsactx)109{110PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;111PROV_RSA_CTX *dstctx;112113if (!ossl_prov_is_running())114return NULL;115116dstctx = OPENSSL_zalloc(sizeof(*srcctx));117if (dstctx == NULL)118return NULL;119120*dstctx = *srcctx;121if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {122OPENSSL_free(dstctx);123return NULL;124}125return dstctx;126}127128static int rsakem_init(void *vprsactx, void *vrsa,129const OSSL_PARAM params[], int operation,130const char *desc)131{132PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;133int protect = 0;134135if (!ossl_prov_is_running())136return 0;137138if (prsactx == NULL || vrsa == NULL)139return 0;140141if (!ossl_rsa_key_op_get_protect(vrsa, operation, &protect))142return 0;143if (!RSA_up_ref(vrsa))144return 0;145RSA_free(prsactx->rsa);146prsactx->rsa = vrsa;147148OSSL_FIPS_IND_SET_APPROVED(prsactx)149if (!rsakem_set_ctx_params(prsactx, params))150return 0;151#ifdef FIPS_MODULE152if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx),153OSSL_FIPS_IND_SETTABLE0, prsactx->libctx,154prsactx->rsa, desc, protect))155return 0;156#endif157return 1;158}159160static int rsakem_encapsulate_init(void *vprsactx, void *vrsa,161const OSSL_PARAM params[])162{163return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCAPSULATE,164"RSA Encapsulate Init");165}166167static int rsakem_decapsulate_init(void *vprsactx, void *vrsa,168const OSSL_PARAM params[])169{170return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECAPSULATE,171"RSA Decapsulate Init");172}173174static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params)175{176PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx;177178if (ctx == NULL)179return 0;180181if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, params))182return 0;183return 1;184}185186static const OSSL_PARAM known_gettable_rsakem_ctx_params[] = {187OSSL_FIPS_IND_GETTABLE_CTX_PARAM()188OSSL_PARAM_END189};190191static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *vprsactx,192ossl_unused void *provctx)193{194return known_gettable_rsakem_ctx_params;195}196197static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])198{199PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;200const OSSL_PARAM *p;201int op;202203if (prsactx == NULL)204return 0;205if (ossl_param_is_empty(params))206return 1;207208if (!OSSL_FIPS_IND_SET_CTX_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0, params,209OSSL_KEM_PARAM_FIPS_KEY_CHECK))210return 0;211p = OSSL_PARAM_locate_const(params, OSSL_KEM_PARAM_OPERATION);212if (p != NULL) {213if (p->data_type != OSSL_PARAM_UTF8_STRING)214return 0;215op = rsakem_opname2id(p->data);216if (op < 0)217return 0;218prsactx->op = op;219}220return 1;221}222223static const OSSL_PARAM known_settable_rsakem_ctx_params[] = {224OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0),225OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KEM_PARAM_FIPS_KEY_CHECK)226OSSL_PARAM_END227};228229static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *vprsactx,230ossl_unused void *provctx)231{232return known_settable_rsakem_ctx_params;233}234235/*236* NIST.SP.800-56Br2237* 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).238*239* Generate a random in the range 1 < z < (n – 1)240*/241static int rsasve_gen_rand_bytes(RSA *rsa_pub,242unsigned char *out, int outlen)243{244int ret = 0;245BN_CTX *bnctx;246BIGNUM *z, *nminus3;247248bnctx = BN_CTX_secure_new_ex(ossl_rsa_get0_libctx(rsa_pub));249if (bnctx == NULL)250return 0;251252/*253* Generate a random in the range 1 < z < (n – 1).254* Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max255* We can achieve this by adding 2.. but then we need to subtract 3 from256* the upper bound i.e: 2 + (0 <= r < (n - 3))257*/258BN_CTX_start(bnctx);259nminus3 = BN_CTX_get(bnctx);260z = BN_CTX_get(bnctx);261ret = (z != NULL262&& (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL)263&& BN_sub_word(nminus3, 3)264&& BN_priv_rand_range_ex(z, nminus3, 0, bnctx)265&& BN_add_word(z, 2)266&& (BN_bn2binpad(z, out, outlen) == outlen));267BN_CTX_end(bnctx);268BN_CTX_free(bnctx);269return ret;270}271272/*273* NIST.SP.800-56Br2274* 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).275*/276static int rsasve_generate(PROV_RSA_CTX *prsactx,277unsigned char *out, size_t *outlen,278unsigned char *secret, size_t *secretlen)279{280int ret;281size_t nlen;282283/* Step (1): nlen = Ceil(len(n)/8) */284nlen = RSA_size(prsactx->rsa);285286if (out == NULL) {287if (nlen == 0) {288ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);289return 0;290}291if (outlen == NULL && secretlen == NULL)292return 0;293if (outlen != NULL)294*outlen = nlen;295if (secretlen != NULL)296*secretlen = nlen;297return 1;298}299300/*301* If outlen is specified, then it must report the length302* of the out buffer on input so that we can confirm303* its size is sufficent for encapsulation304*/305if (outlen != NULL && *outlen < nlen) {306ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);307return 0;308}309310/*311* Step (2): Generate a random byte string z of nlen bytes where312* 1 < z < n - 1313*/314if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, nlen))315return 0;316317/* Step(3): out = RSAEP((n,e), z) */318ret = RSA_public_encrypt(nlen, secret, out, prsactx->rsa, RSA_NO_PADDING);319if (ret) {320ret = 1;321if (outlen != NULL)322*outlen = nlen;323if (secretlen != NULL)324*secretlen = nlen;325} else {326OPENSSL_cleanse(secret, nlen);327}328return ret;329}330331/**332* rsasve_recover - Recovers a secret value from ciphertext using an RSA333* private key. Once, recovered, the secret value is considered to be a334* shared secret. Algorithm is preformed as per335* NIST SP 800-56B Rev 2336* 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER).337*338* This function performs RSA decryption using the private key from the339* provided RSA context (`prsactx`). It takes the input ciphertext, decrypts340* it, and writes the decrypted message to the output buffer.341*342* @prsactx: The RSA context containing the private key.343* @out: The output buffer to store the decrypted message.344* @outlen: On input, the size of the output buffer. On successful345* completion, the actual length of the decrypted message.346* @in: The input buffer containing the ciphertext to be decrypted.347* @inlen: The length of the input ciphertext in bytes.348*349* Returns 1 on success, or 0 on error. In case of error, appropriate350* error messages are raised using the ERR_raise function.351*/352static int rsasve_recover(PROV_RSA_CTX *prsactx,353unsigned char *out, size_t *outlen,354const unsigned char *in, size_t inlen)355{356size_t nlen;357int ret;358359/* Step (1): get the byte length of n */360nlen = RSA_size(prsactx->rsa);361362if (out == NULL) {363if (nlen == 0) {364ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);365return 0;366}367*outlen = nlen;368return 1;369}370371/*372* Step (2): check the input ciphertext 'inlen' matches the nlen373* and that outlen is at least nlen bytes374*/375if (inlen != nlen) {376ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);377return 0;378}379380/*381* If outlen is specified, then it must report the length382* of the out buffer, so that we can confirm that it is of383* sufficient size to hold the output of decapsulation384*/385if (outlen != NULL && *outlen < nlen) {386ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);387return 0;388}389390/* Step (3): out = RSADP((n,d), in) */391ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, RSA_NO_PADDING);392if (ret > 0 && outlen != NULL)393*outlen = ret;394return ret > 0;395}396397static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen,398unsigned char *secret, size_t *secretlen)399{400PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;401402if (!ossl_prov_is_running())403return 0;404405switch (prsactx->op) {406case KEM_OP_RSASVE:407return rsasve_generate(prsactx, out, outlen, secret, secretlen);408default:409return -2;410}411}412413static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen,414const unsigned char *in, size_t inlen)415{416PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;417418if (!ossl_prov_is_running())419return 0;420421switch (prsactx->op) {422case KEM_OP_RSASVE:423return rsasve_recover(prsactx, out, outlen, in, inlen);424default:425return -2;426}427}428429const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = {430{ OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx },431{ OSSL_FUNC_KEM_ENCAPSULATE_INIT,432(void (*)(void))rsakem_encapsulate_init },433{ OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate },434{ OSSL_FUNC_KEM_DECAPSULATE_INIT,435(void (*)(void))rsakem_decapsulate_init },436{ OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover },437{ OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx },438{ OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx },439{ OSSL_FUNC_KEM_GET_CTX_PARAMS,440(void (*)(void))rsakem_get_ctx_params },441{ OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS,442(void (*)(void))rsakem_gettable_ctx_params },443{ OSSL_FUNC_KEM_SET_CTX_PARAMS,444(void (*)(void))rsakem_set_ctx_params },445{ OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,446(void (*)(void))rsakem_settable_ctx_params },447OSSL_DISPATCH_END448};449450451