| 1 |
/* |
| 2 |
* Copyright 2006-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 |
|
| 10 |
#ifndef OSSL_CRYPTO_RSA_LOCAL_H |
| 11 |
#define OSSL_CRYPTO_RSA_LOCAL_H |
| 12 |
|
| 13 |
#include "internal/refcount.h" |
| 14 |
#include "crypto/rsa.h" |
| 15 |
|
| 16 |
#define RSA_MAX_PRIME_NUM 5 |
| 17 |
|
| 18 |
typedef struct rsa_prime_info_st { |
| 19 |
BIGNUM *r; |
| 20 |
BIGNUM *d; |
| 21 |
BIGNUM *t; |
| 22 |
/* save product of primes prior to this one */ |
| 23 |
BIGNUM *pp; |
| 24 |
BN_MONT_CTX *m; |
| 25 |
} RSA_PRIME_INFO; |
| 26 |
|
| 27 |
DECLARE_ASN1_ITEM(RSA_PRIME_INFO) |
| 28 |
DEFINE_STACK_OF(RSA_PRIME_INFO) |
| 29 |
|
| 30 |
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS) |
| 31 |
struct rsa_acvp_test_st { |
| 32 |
/* optional inputs */ |
| 33 |
BIGNUM *Xp1; |
| 34 |
BIGNUM *Xp2; |
| 35 |
BIGNUM *Xq1; |
| 36 |
BIGNUM *Xq2; |
| 37 |
BIGNUM *Xp; |
| 38 |
BIGNUM *Xq; |
| 39 |
|
| 40 |
/* optional outputs */ |
| 41 |
BIGNUM *p1; |
| 42 |
BIGNUM *p2; |
| 43 |
BIGNUM *q1; |
| 44 |
BIGNUM *q2; |
| 45 |
}; |
| 46 |
#endif |
| 47 |
|
| 48 |
struct rsa_st { |
| 49 |
/* |
| 50 |
* #legacy |
| 51 |
* The first field is used to pickup errors where this is passed |
| 52 |
* instead of an EVP_PKEY. It is always zero. |
| 53 |
* THIS MUST REMAIN THE FIRST FIELD. |
| 54 |
*/ |
| 55 |
int dummy_zero; |
| 56 |
|
| 57 |
OSSL_LIB_CTX *libctx; |
| 58 |
int32_t version; |
| 59 |
const RSA_METHOD *meth; |
| 60 |
/* functional reference if 'meth' is ENGINE-provided */ |
| 61 |
ENGINE *engine; |
| 62 |
BIGNUM *n; |
| 63 |
BIGNUM *e; |
| 64 |
BIGNUM *d; |
| 65 |
BIGNUM *p; |
| 66 |
BIGNUM *q; |
| 67 |
BIGNUM *dmp1; |
| 68 |
BIGNUM *dmq1; |
| 69 |
BIGNUM *iqmp; |
| 70 |
|
| 71 |
/* |
| 72 |
* If a PSS only key this contains the parameter restrictions. |
| 73 |
* There are two structures for the same thing, used in different cases. |
| 74 |
*/ |
| 75 |
/* This is used uniquely by OpenSSL provider implementations. */ |
| 76 |
RSA_PSS_PARAMS_30 pss_params; |
| 77 |
|
| 78 |
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS) |
| 79 |
RSA_ACVP_TEST *acvp_test; |
| 80 |
#endif |
| 81 |
|
| 82 |
#ifndef FIPS_MODULE |
| 83 |
/* This is used uniquely by rsa_ameth.c and rsa_pmeth.c. */ |
| 84 |
RSA_PSS_PARAMS *pss; |
| 85 |
/* for multi-prime RSA, defined in RFC 8017 */ |
| 86 |
STACK_OF(RSA_PRIME_INFO) *prime_infos; |
| 87 |
/* Be careful using this if the RSA structure is shared */ |
| 88 |
CRYPTO_EX_DATA ex_data; |
| 89 |
#endif |
| 90 |
CRYPTO_REF_COUNT references; |
| 91 |
int flags; |
| 92 |
/* Used to cache montgomery values */ |
| 93 |
BN_MONT_CTX *_method_mod_n; |
| 94 |
BN_MONT_CTX *_method_mod_p; |
| 95 |
BN_MONT_CTX *_method_mod_q; |
| 96 |
void *blindings_sa; |
| 97 |
CRYPTO_RWLOCK *lock; |
| 98 |
|
| 99 |
int dirty_cnt; |
| 100 |
}; |
| 101 |
|
| 102 |
struct rsa_meth_st { |
| 103 |
char *name; |
| 104 |
int (*rsa_pub_enc)(int flen, const unsigned char *from, |
| 105 |
unsigned char *to, RSA *rsa, int padding); |
| 106 |
int (*rsa_pub_dec)(int flen, const unsigned char *from, |
| 107 |
unsigned char *to, RSA *rsa, int padding); |
| 108 |
int (*rsa_priv_enc)(int flen, const unsigned char *from, |
| 109 |
unsigned char *to, RSA *rsa, int padding); |
| 110 |
int (*rsa_priv_dec)(int flen, const unsigned char *from, |
| 111 |
unsigned char *to, RSA *rsa, int padding); |
| 112 |
/* Can be null */ |
| 113 |
int (*rsa_mod_exp)(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx); |
| 114 |
/* Can be null */ |
| 115 |
int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
| 116 |
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); |
| 117 |
/* called at new */ |
| 118 |
int (*init)(RSA *rsa); |
| 119 |
/* called at free */ |
| 120 |
int (*finish)(RSA *rsa); |
| 121 |
/* RSA_METHOD_FLAG_* things */ |
| 122 |
int flags; |
| 123 |
/* may be needed! */ |
| 124 |
char *app_data; |
| 125 |
/* |
| 126 |
* New sign and verify functions: some libraries don't allow arbitrary |
| 127 |
* data to be signed/verified: this allows them to be used. Note: for |
| 128 |
* this to work the RSA_public_decrypt() and RSA_private_encrypt() should |
| 129 |
* *NOT* be used. RSA_sign(), RSA_verify() should be used instead. |
| 130 |
*/ |
| 131 |
int (*rsa_sign)(int type, |
| 132 |
const unsigned char *m, unsigned int m_length, |
| 133 |
unsigned char *sigret, unsigned int *siglen, |
| 134 |
const RSA *rsa); |
| 135 |
int (*rsa_verify)(int dtype, const unsigned char *m, |
| 136 |
unsigned int m_length, const unsigned char *sigbuf, |
| 137 |
unsigned int siglen, const RSA *rsa); |
| 138 |
/* |
| 139 |
* If this callback is NULL, the builtin software RSA key-gen will be |
| 140 |
* used. This is for behavioural compatibility whilst the code gets |
| 141 |
* rewired, but one day it would be nice to assume there are no such |
| 142 |
* things as "builtin software" implementations. |
| 143 |
*/ |
| 144 |
int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb); |
| 145 |
int (*rsa_multi_prime_keygen)(RSA *rsa, int bits, int primes, |
| 146 |
BIGNUM *e, BN_GENCB *cb); |
| 147 |
}; |
| 148 |
|
| 149 |
/* Macros to test if a pkey or ctx is for a PSS key */ |
| 150 |
#define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS) |
| 151 |
#define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS) |
| 152 |
int ossl_rsa_multiprime_derive(RSA *rsa, int bits, int primes, |
| 153 |
BIGNUM *e_value, |
| 154 |
STACK_OF(BIGNUM) *factors, STACK_OF(BIGNUM) *exps, |
| 155 |
STACK_OF(BIGNUM) *coeffs); |
| 156 |
|
| 157 |
RSA_PSS_PARAMS *ossl_rsa_pss_params_create(const EVP_MD *sigmd, |
| 158 |
const EVP_MD *mgf1md, int saltlen); |
| 159 |
int ossl_rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd, |
| 160 |
const EVP_MD **pmgf1md, int *psaltlen); |
| 161 |
/* internal function to clear and free multi-prime parameters */ |
| 162 |
void ossl_rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo); |
| 163 |
void ossl_rsa_multip_info_free(RSA_PRIME_INFO *pinfo); |
| 164 |
RSA_PRIME_INFO *ossl_rsa_multip_info_new(void); |
| 165 |
int ossl_rsa_multip_calc_product(RSA *rsa); |
| 166 |
int ossl_rsa_multip_cap(int bits); |
| 167 |
|
| 168 |
int ossl_rsa_sp800_56b_validate_strength(int nbits, int strength); |
| 169 |
int ossl_rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q, |
| 170 |
int nbits); |
| 171 |
int ossl_rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q, |
| 172 |
BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1, |
| 173 |
BIGNUM *p1q1); |
| 174 |
|
| 175 |
int ossl_rsa_check_public_exponent(const BIGNUM *e); |
| 176 |
int ossl_rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx); |
| 177 |
int ossl_rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx); |
| 178 |
int ossl_rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx); |
| 179 |
int ossl_rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx); |
| 180 |
|
| 181 |
int ossl_rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx); |
| 182 |
int ossl_rsa_sp800_56b_check_public(const RSA *rsa); |
| 183 |
int ossl_rsa_sp800_56b_check_private(const RSA *rsa); |
| 184 |
int ossl_rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed, |
| 185 |
int strength, int nbits); |
| 186 |
int ossl_rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed, |
| 187 |
BN_GENCB *cb); |
| 188 |
|
| 189 |
int ossl_rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits, |
| 190 |
const BIGNUM *e, BN_CTX *ctx); |
| 191 |
int ossl_rsa_fips186_4_gen_prob_primes(RSA *rsa, RSA_ACVP_TEST *test, |
| 192 |
int nbits, const BIGNUM *e, BN_CTX *ctx, |
| 193 |
BN_GENCB *cb); |
| 194 |
|
| 195 |
int ossl_rsa_padding_add_PKCS1_type_2_ex(OSSL_LIB_CTX *libctx, unsigned char *to, |
| 196 |
int tlen, const unsigned char *from, |
| 197 |
int flen); |
| 198 |
void ossl_rsa_free_blinding(RSA *rsa); |
| 199 |
void *ossl_rsa_alloc_blinding(void); |
| 200 |
|
| 201 |
#endif /* OSSL_CRYPTO_RSA_LOCAL_H */ |