9bd53bec5a579f7bf8f592d22f2b391e44258834
[oweals/openssl.git] / crypto / rsa / rsa_locl.h
1 /*
2  * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include <openssl/rsa.h>
11 #include "internal/refcount.h"
12
13 #define RSA_MAX_PRIME_NUM 16
14 #define RSA_MIN_PRIME_SIZE 64
15 #define RSA_MIN_MODULUS_BITS    512
16
17 typedef struct rsa_prime_info_st {
18     BIGNUM *r;
19     BIGNUM *d;
20     BIGNUM *t;
21     /* save product of primes prior to this one */
22     BIGNUM *pp;
23     BN_MONT_CTX *m;
24 } RSA_PRIME_INFO;
25
26 DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
27 DEFINE_STACK_OF(RSA_PRIME_INFO)
28
29 struct rsa_st {
30     /*
31      * The first parameter is used to pickup errors where this is passed
32      * instead of an EVP_PKEY, it is set to 0
33      */
34     int pad;
35     int32_t version;
36     const RSA_METHOD *meth;
37     /* functional reference if 'meth' is ENGINE-provided */
38     ENGINE *engine;
39     BIGNUM *n;
40     BIGNUM *e;
41     BIGNUM *d;
42     BIGNUM *p;
43     BIGNUM *q;
44     BIGNUM *dmp1;
45     BIGNUM *dmq1;
46     BIGNUM *iqmp;
47     /* for multi-prime RSA, defined in RFC 8017 */
48     STACK_OF(RSA_PRIME_INFO) *prime_infos;
49     /* If a PSS only key this contains the parameter restrictions */
50     RSA_PSS_PARAMS *pss;
51     /* be careful using this if the RSA structure is shared */
52     CRYPTO_EX_DATA ex_data;
53     CRYPTO_REF_COUNT references;
54     int flags;
55     /* Used to cache montgomery values */
56     BN_MONT_CTX *_method_mod_n;
57     BN_MONT_CTX *_method_mod_p;
58     BN_MONT_CTX *_method_mod_q;
59     /*
60      * all BIGNUM values are actually in the following data, if it is not
61      * NULL
62      */
63     char *bignum_data;
64     BN_BLINDING *blinding;
65     BN_BLINDING *mt_blinding;
66     CRYPTO_RWLOCK *lock;
67 };
68
69 struct rsa_meth_st {
70     char *name;
71     int (*rsa_pub_enc) (int flen, const unsigned char *from,
72                         unsigned char *to, RSA *rsa, int padding);
73     int (*rsa_pub_dec) (int flen, const unsigned char *from,
74                         unsigned char *to, RSA *rsa, int padding);
75     int (*rsa_priv_enc) (int flen, const unsigned char *from,
76                          unsigned char *to, RSA *rsa, int padding);
77     int (*rsa_priv_dec) (int flen, const unsigned char *from,
78                          unsigned char *to, RSA *rsa, int padding);
79     /* Can be null */
80     int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
81     /* Can be null */
82     int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
83                        const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
84     /* called at new */
85     int (*init) (RSA *rsa);
86     /* called at free */
87     int (*finish) (RSA *rsa);
88     /* RSA_METHOD_FLAG_* things */
89     int flags;
90     /* may be needed! */
91     char *app_data;
92     /*
93      * New sign and verify functions: some libraries don't allow arbitrary
94      * data to be signed/verified: this allows them to be used. Note: for
95      * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
96      * *NOT* be used RSA_sign(), RSA_verify() should be used instead.
97      */
98     int (*rsa_sign) (int type,
99                      const unsigned char *m, unsigned int m_length,
100                      unsigned char *sigret, unsigned int *siglen,
101                      const RSA *rsa);
102     int (*rsa_verify) (int dtype, const unsigned char *m,
103                        unsigned int m_length, const unsigned char *sigbuf,
104                        unsigned int siglen, const RSA *rsa);
105     /*
106      * If this callback is NULL, the builtin software RSA key-gen will be
107      * used. This is for behavioural compatibility whilst the code gets
108      * rewired, but one day it would be nice to assume there are no such
109      * things as "builtin software" implementations.
110      */
111     int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
112     int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
113                                    BIGNUM *e, BN_GENCB *cb);
114 };
115
116 extern int int_rsa_verify(int dtype, const unsigned char *m,
117                           unsigned int m_len, unsigned char *rm,
118                           size_t *prm_len, const unsigned char *sigbuf,
119                           size_t siglen, RSA *rsa);
120 /* Macros to test if a pkey or ctx is for a PSS key */
121 #define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
122 #define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
123
124 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
125                                       const EVP_MD *mgf1md, int saltlen);
126 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
127                       const EVP_MD **pmgf1md, int *psaltlen);
128 /* internal function to clear and free multi-prime parameters */
129 void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
130 void rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
131 RSA_PRIME_INFO *rsa_multip_info_new(void);
132 int rsa_multip_calc_product(RSA *rsa);
133 int rsa_multip_cap(int bits);