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