Use read/write locking on Windows
[openssl.git] / crypto / rsa / rsa_local.h
1 /*
2  * Copyright 2006-2020 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 #define RSA_MIN_MODULUS_BITS    512
18
19 typedef struct rsa_prime_info_st {
20     BIGNUM *r;
21     BIGNUM *d;
22     BIGNUM *t;
23     /* save product of primes prior to this one */
24     BIGNUM *pp;
25     BN_MONT_CTX *m;
26 } RSA_PRIME_INFO;
27
28 DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
29 DEFINE_STACK_OF(RSA_PRIME_INFO)
30
31 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
32 struct rsa_acvp_test_st {
33     /* optional inputs */
34     BIGNUM *Xp1;
35     BIGNUM *Xp2;
36     BIGNUM *Xq1;
37     BIGNUM *Xq2;
38     BIGNUM *Xp;
39     BIGNUM *Xq;
40
41     /* optional outputs */
42     BIGNUM *p1;
43     BIGNUM *p2;
44     BIGNUM *q1;
45     BIGNUM *q2;
46 };
47 #endif
48
49 struct rsa_st {
50     /*
51      * #legacy
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.
55      */
56     int dummy_zero;
57
58     OSSL_LIB_CTX *libctx;
59     int32_t version;
60     const RSA_METHOD *meth;
61     /* functional reference if 'meth' is ENGINE-provided */
62     ENGINE *engine;
63     BIGNUM *n;
64     BIGNUM *e;
65     BIGNUM *d;
66     BIGNUM *p;
67     BIGNUM *q;
68     BIGNUM *dmp1;
69     BIGNUM *dmq1;
70     BIGNUM *iqmp;
71
72     /*
73      * If a PSS only key this contains the parameter restrictions.
74      * There are two structures for the same thing, used in different cases.
75      */
76     /* This is used uniquely by OpenSSL provider implementations. */
77     RSA_PSS_PARAMS_30 pss_params;
78
79 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
80     RSA_ACVP_TEST *acvp_test;
81 #endif
82
83 #ifndef FIPS_MODULE
84     /* This is used uniquely by rsa_ameth.c and rsa_pmeth.c. */
85     RSA_PSS_PARAMS *pss;
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;
90 #endif
91     CRYPTO_REF_COUNT references;
92     int flags;
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;
97     /*
98      * all BIGNUM values are actually in the following data, if it is not
99      * NULL
100      */
101     char *bignum_data;
102     BN_BLINDING *blinding;
103     BN_BLINDING *mt_blinding;
104     CRYPTO_RWLOCK *lock;
105
106     int dirty_cnt;
107 };
108
109 struct rsa_meth_st {
110     char *name;
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);
119     /* Can be null */
120     int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
121     /* Can be null */
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);
124     /* called at new */
125     int (*init) (RSA *rsa);
126     /* called at free */
127     int (*finish) (RSA *rsa);
128     /* RSA_METHOD_FLAG_* things */
129     int flags;
130     /* may be needed! */
131     char *app_data;
132     /*
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.
137      */
138     int (*rsa_sign) (int type,
139                      const unsigned char *m, unsigned int m_length,
140                      unsigned char *sigret, unsigned int *siglen,
141                      const RSA *rsa);
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);
145     /*
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.
150      */
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);
154 };
155
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)
159
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);
170
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,
173                                 int nbits);
174 int ossl_rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
175                      BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
176                      BIGNUM *p1q1);
177
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);
183
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,
190                                     BN_GENCB *cb);
191
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,
196                                        BN_GENCB *cb);
197
198 int ossl_rsa_padding_add_PKCS1_type_2_ex(OSSL_LIB_CTX *libctx, unsigned char *to,
199                                          int tlen, const unsigned char *from,
200                                          int flen);
201
202 #endif /* OSSL_CRYPTO_RSA_LOCAL_H */