Rename FIPS_MODE to FIPS_MODULE
[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 <openssl/rsa.h>
14 #include "internal/refcount.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 struct rsa_st {
32     /*
33      * #legacy
34      * The first field is used to pickup errors where this is passed
35      * instead of an EVP_PKEY.  It is always zero.
36      * THIS MUST REMAIN THE FIRST FIELD.
37      */
38     int dummy_zero;
39
40     OPENSSL_CTX *libctx;
41     int32_t version;
42     const RSA_METHOD *meth;
43     /* functional reference if 'meth' is ENGINE-provided */
44     ENGINE *engine;
45     BIGNUM *n;
46     BIGNUM *e;
47     BIGNUM *d;
48     BIGNUM *p;
49     BIGNUM *q;
50     BIGNUM *dmp1;
51     BIGNUM *dmq1;
52     BIGNUM *iqmp;
53     /* If a PSS only key this contains the parameter restrictions */
54     RSA_PSS_PARAMS *pss;
55 #ifndef FIPS_MODULE
56     /* for multi-prime RSA, defined in RFC 8017 */
57     STACK_OF(RSA_PRIME_INFO) *prime_infos;
58     /* Be careful using this if the RSA structure is shared */
59     CRYPTO_EX_DATA ex_data;
60 #endif
61     CRYPTO_REF_COUNT references;
62     int flags;
63     /* Used to cache montgomery values */
64     BN_MONT_CTX *_method_mod_n;
65     BN_MONT_CTX *_method_mod_p;
66     BN_MONT_CTX *_method_mod_q;
67     /*
68      * all BIGNUM values are actually in the following data, if it is not
69      * NULL
70      */
71     char *bignum_data;
72     BN_BLINDING *blinding;
73     BN_BLINDING *mt_blinding;
74     CRYPTO_RWLOCK *lock;
75
76     int dirty_cnt;
77 };
78
79 struct rsa_meth_st {
80     char *name;
81     int (*rsa_pub_enc) (int flen, const unsigned char *from,
82                         unsigned char *to, RSA *rsa, int padding);
83     int (*rsa_pub_dec) (int flen, const unsigned char *from,
84                         unsigned char *to, RSA *rsa, int padding);
85     int (*rsa_priv_enc) (int flen, const unsigned char *from,
86                          unsigned char *to, RSA *rsa, int padding);
87     int (*rsa_priv_dec) (int flen, const unsigned char *from,
88                          unsigned char *to, RSA *rsa, int padding);
89     /* Can be null */
90     int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
91     /* Can be null */
92     int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
93                        const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
94     /* called at new */
95     int (*init) (RSA *rsa);
96     /* called at free */
97     int (*finish) (RSA *rsa);
98     /* RSA_METHOD_FLAG_* things */
99     int flags;
100     /* may be needed! */
101     char *app_data;
102     /*
103      * New sign and verify functions: some libraries don't allow arbitrary
104      * data to be signed/verified: this allows them to be used. Note: for
105      * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
106      * *NOT* be used RSA_sign(), RSA_verify() should be used instead.
107      */
108     int (*rsa_sign) (int type,
109                      const unsigned char *m, unsigned int m_length,
110                      unsigned char *sigret, unsigned int *siglen,
111                      const RSA *rsa);
112     int (*rsa_verify) (int dtype, const unsigned char *m,
113                        unsigned int m_length, const unsigned char *sigbuf,
114                        unsigned int siglen, const RSA *rsa);
115     /*
116      * If this callback is NULL, the builtin software RSA key-gen will be
117      * used. This is for behavioural compatibility whilst the code gets
118      * rewired, but one day it would be nice to assume there are no such
119      * things as "builtin software" implementations.
120      */
121     int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
122     int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
123                                    BIGNUM *e, BN_GENCB *cb);
124 };
125
126 /* Macros to test if a pkey or ctx is for a PSS key */
127 #define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
128 #define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
129
130 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
131                                       const EVP_MD *mgf1md, int saltlen);
132 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
133                       const EVP_MD **pmgf1md, int *psaltlen);
134 /* internal function to clear and free multi-prime parameters */
135 void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
136 void rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
137 RSA_PRIME_INFO *rsa_multip_info_new(void);
138 int rsa_multip_calc_product(RSA *rsa);
139 int rsa_multip_cap(int bits);
140
141 int rsa_sp800_56b_validate_strength(int nbits, int strength);
142 int rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
143                            int nbits);
144 int rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
145                 BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
146                 BIGNUM *p1q1);
147
148 int rsa_check_public_exponent(const BIGNUM *e);
149 int rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx);
150 int rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx);
151 int rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx);
152 int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx);
153
154 int rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx);
155 int rsa_sp800_56b_check_public(const RSA *rsa);
156 int rsa_sp800_56b_check_private(const RSA *rsa);
157 int rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
158                                 int strength, int nbits);
159 int rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
160                                BN_GENCB *cb);
161
162 int rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
163                                         const BIGNUM *e, BN_CTX *ctx);
164 int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2,
165                                   BIGNUM *Xpout, const BIGNUM *Xp,
166                                   const BIGNUM *Xp1, const BIGNUM *Xp2,
167                                   BIGNUM *q1, BIGNUM *q2, BIGNUM *Xqout,
168                                   const BIGNUM *Xq, const BIGNUM *Xq1,
169                                   const BIGNUM *Xq2, int nbits,
170                                   const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb);
171
172 int rsa_padding_add_SSLv23_with_libctx(OPENSSL_CTX *libctx, unsigned char *to,
173                                        int tlen, const unsigned char *from,
174                                        int flen);
175 int rsa_padding_add_PKCS1_type_2_with_libctx(OPENSSL_CTX *libctx,
176                                              unsigned char *to, int tlen,
177                                              const unsigned char *from,
178                                              int flen);
179 int rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(OPENSSL_CTX *libctx,
180                                                 unsigned char *to, int tlen,
181                                                 const unsigned char *from,
182                                                 int flen,
183                                                 const unsigned char *param,
184                                                 int plen, const EVP_MD *md,
185                                                 const EVP_MD *mgf1md);
186
187 #endif /* OSSL_CRYPTO_RSA_LOCAL_H */