2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef OSSL_CRYPTO_RSA_LOCAL_H
11 #define OSSL_CRYPTO_RSA_LOCAL_H
13 #include "internal/refcount.h"
14 #include "crypto/rsa.h"
16 #define RSA_MAX_PRIME_NUM 5
17 #define RSA_MIN_MODULUS_BITS 512
19 typedef struct rsa_prime_info_st
{
23 /* save product of primes prior to this one */
28 DECLARE_ASN1_ITEM(RSA_PRIME_INFO
)
29 DEFINE_STACK_OF(RSA_PRIME_INFO
)
31 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
32 struct rsa_acvp_test_st
{
41 /* optional outputs */
52 * The first field is used to pickup errors where this is passed
53 * instead of an EVP_PKEY. It is always zero.
54 * THIS MUST REMAIN THE FIRST FIELD.
60 const RSA_METHOD
*meth
;
61 /* functional reference if 'meth' is ENGINE-provided */
73 * If a PSS only key this contains the parameter restrictions.
74 * There are two structures for the same thing, used in different cases.
76 /* This is used uniquely by OpenSSL provider implementations. */
77 RSA_PSS_PARAMS_30 pss_params
;
79 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
80 RSA_ACVP_TEST
*acvp_test
;
84 /* This is used uniquely by rsa_ameth.c and rsa_pmeth.c. */
86 /* for multi-prime RSA, defined in RFC 8017 */
87 STACK_OF(RSA_PRIME_INFO
) *prime_infos
;
88 /* Be careful using this if the RSA structure is shared */
89 CRYPTO_EX_DATA ex_data
;
91 CRYPTO_REF_COUNT references
;
93 /* Used to cache montgomery values */
94 BN_MONT_CTX
*_method_mod_n
;
95 BN_MONT_CTX
*_method_mod_p
;
96 BN_MONT_CTX
*_method_mod_q
;
98 * all BIGNUM values are actually in the following data, if it is not
102 BN_BLINDING
*blinding
;
103 BN_BLINDING
*mt_blinding
;
111 int (*rsa_pub_enc
) (int flen
, const unsigned char *from
,
112 unsigned char *to
, RSA
*rsa
, int padding
);
113 int (*rsa_pub_dec
) (int flen
, const unsigned char *from
,
114 unsigned char *to
, RSA
*rsa
, int padding
);
115 int (*rsa_priv_enc
) (int flen
, const unsigned char *from
,
116 unsigned char *to
, RSA
*rsa
, int padding
);
117 int (*rsa_priv_dec
) (int flen
, const unsigned char *from
,
118 unsigned char *to
, RSA
*rsa
, int padding
);
120 int (*rsa_mod_exp
) (BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
);
122 int (*bn_mod_exp
) (BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
123 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
125 int (*init
) (RSA
*rsa
);
127 int (*finish
) (RSA
*rsa
);
128 /* RSA_METHOD_FLAG_* things */
133 * New sign and verify functions: some libraries don't allow arbitrary
134 * data to be signed/verified: this allows them to be used. Note: for
135 * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
136 * *NOT* be used. RSA_sign(), RSA_verify() should be used instead.
138 int (*rsa_sign
) (int type
,
139 const unsigned char *m
, unsigned int m_length
,
140 unsigned char *sigret
, unsigned int *siglen
,
142 int (*rsa_verify
) (int dtype
, const unsigned char *m
,
143 unsigned int m_length
, const unsigned char *sigbuf
,
144 unsigned int siglen
, const RSA
*rsa
);
146 * If this callback is NULL, the builtin software RSA key-gen will be
147 * used. This is for behavioural compatibility whilst the code gets
148 * rewired, but one day it would be nice to assume there are no such
149 * things as "builtin software" implementations.
151 int (*rsa_keygen
) (RSA
*rsa
, int bits
, BIGNUM
*e
, BN_GENCB
*cb
);
152 int (*rsa_multi_prime_keygen
) (RSA
*rsa
, int bits
, int primes
,
153 BIGNUM
*e
, BN_GENCB
*cb
);
156 /* Macros to test if a pkey or ctx is for a PSS key */
157 #define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
158 #define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
160 RSA_PSS_PARAMS
*rsa_pss_params_create(const EVP_MD
*sigmd
,
161 const EVP_MD
*mgf1md
, int saltlen
);
162 int rsa_pss_get_param(const RSA_PSS_PARAMS
*pss
, const EVP_MD
**pmd
,
163 const EVP_MD
**pmgf1md
, int *psaltlen
);
164 /* internal function to clear and free multi-prime parameters */
165 void rsa_multip_info_free_ex(RSA_PRIME_INFO
*pinfo
);
166 void rsa_multip_info_free(RSA_PRIME_INFO
*pinfo
);
167 RSA_PRIME_INFO
*rsa_multip_info_new(void);
168 int rsa_multip_calc_product(RSA
*rsa
);
169 int rsa_multip_cap(int bits
);
171 int ossl_rsa_sp800_56b_validate_strength(int nbits
, int strength
);
172 int ossl_rsa_check_pminusq_diff(BIGNUM
*diff
, const BIGNUM
*p
, const BIGNUM
*q
,
174 int ossl_rsa_get_lcm(BN_CTX
*ctx
, const BIGNUM
*p
, const BIGNUM
*q
,
175 BIGNUM
*lcm
, BIGNUM
*gcd
, BIGNUM
*p1
, BIGNUM
*q1
,
178 int ossl_rsa_check_public_exponent(const BIGNUM
*e
);
179 int ossl_rsa_check_private_exponent(const RSA
*rsa
, int nbits
, BN_CTX
*ctx
);
180 int ossl_rsa_check_prime_factor(BIGNUM
*p
, BIGNUM
*e
, int nbits
, BN_CTX
*ctx
);
181 int ossl_rsa_check_prime_factor_range(const BIGNUM
*p
, int nbits
, BN_CTX
*ctx
);
182 int ossl_rsa_check_crt_components(const RSA
*rsa
, BN_CTX
*ctx
);
184 int ossl_rsa_sp800_56b_pairwise_test(RSA
*rsa
, BN_CTX
*ctx
);
185 int ossl_rsa_sp800_56b_check_public(const RSA
*rsa
);
186 int ossl_rsa_sp800_56b_check_private(const RSA
*rsa
);
187 int ossl_rsa_sp800_56b_check_keypair(const RSA
*rsa
, const BIGNUM
*efixed
,
188 int strength
, int nbits
);
189 int ossl_rsa_sp800_56b_generate_key(RSA
*rsa
, int nbits
, const BIGNUM
*efixed
,
192 int ossl_rsa_sp800_56b_derive_params_from_pq(RSA
*rsa
, int nbits
,
193 const BIGNUM
*e
, BN_CTX
*ctx
);
194 int ossl_rsa_fips186_4_gen_prob_primes(RSA
*rsa
, RSA_ACVP_TEST
*test
,
195 int nbits
, const BIGNUM
*e
, BN_CTX
*ctx
,
198 int ossl_rsa_padding_add_PKCS1_type_2_ex(OSSL_LIB_CTX
*libctx
, unsigned char *to
,
199 int tlen
, const unsigned char *from
,
202 #endif /* OSSL_CRYPTO_RSA_LOCAL_H */