598fcb746bbebcd8366885aedd201e51b0cd717e
[openssl.git] / crypto / rsa / rsa_locl.h
1 /*
2  * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL licenses, (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * https://www.openssl.org/source/license.html
8  * or in the file LICENSE in the source distribution.
9  */
10
11 #include <openssl/rsa.h>
12
13 struct rsa_st {
14     /*
15      * The first parameter is used to pickup errors where this is passed
16      * instead of aEVP_PKEY, it is set to 0
17      */
18     int pad;
19     long version;
20     const RSA_METHOD *meth;
21     /* functional reference if 'meth' is ENGINE-provided */
22     ENGINE *engine;
23     BIGNUM *n;
24     BIGNUM *e;
25     BIGNUM *d;
26     BIGNUM *p;
27     BIGNUM *q;
28     BIGNUM *dmp1;
29     BIGNUM *dmq1;
30     BIGNUM *iqmp;
31     /* be careful using this if the RSA structure is shared */
32     CRYPTO_EX_DATA ex_data;
33     int references;
34     int flags;
35     /* Used to cache montgomery values */
36     BN_MONT_CTX *_method_mod_n;
37     BN_MONT_CTX *_method_mod_p;
38     BN_MONT_CTX *_method_mod_q;
39     /*
40      * all BIGNUM values are actually in the following data, if it is not
41      * NULL
42      */
43     char *bignum_data;
44     BN_BLINDING *blinding;
45     BN_BLINDING *mt_blinding;
46     CRYPTO_RWLOCK *lock;
47 };
48
49 struct rsa_meth_st {
50     char *name;
51     int (*rsa_pub_enc) (int flen, const unsigned char *from,
52                         unsigned char *to, RSA *rsa, int padding);
53     int (*rsa_pub_dec) (int flen, const unsigned char *from,
54                         unsigned char *to, RSA *rsa, int padding);
55     int (*rsa_priv_enc) (int flen, const unsigned char *from,
56                          unsigned char *to, RSA *rsa, int padding);
57     int (*rsa_priv_dec) (int flen, const unsigned char *from,
58                          unsigned char *to, RSA *rsa, int padding);
59     /* Can be null */
60     int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
61     /* Can be null */
62     int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
63                        const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
64     /* called at new */
65     int (*init) (RSA *rsa);
66     /* called at free */
67     int (*finish) (RSA *rsa);
68     /* RSA_METHOD_FLAG_* things */
69     int flags;
70     /* may be needed! */
71     char *app_data;
72     /*
73      * New sign and verify functions: some libraries don't allow arbitrary
74      * data to be signed/verified: this allows them to be used. Note: for
75      * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
76      * *NOT* be used RSA_sign(), RSA_verify() should be used instead.
77      */
78     int (*rsa_sign) (int type,
79                      const unsigned char *m, unsigned int m_length,
80                      unsigned char *sigret, unsigned int *siglen,
81                      const RSA *rsa);
82     int (*rsa_verify) (int dtype, const unsigned char *m,
83                        unsigned int m_length, const unsigned char *sigbuf,
84                        unsigned int siglen, const RSA *rsa);
85     /*
86      * If this callback is NULL, the builtin software RSA key-gen will be
87      * used. This is for behavioural compatibility whilst the code gets
88      * rewired, but one day it would be nice to assume there are no such
89      * things as "builtin software" implementations.
90      */
91     int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
92 };
93
94 extern int int_rsa_verify(int dtype, const unsigned char *m,
95                           unsigned int m_len, unsigned char *rm,
96                           size_t *prm_len, const unsigned char *sigbuf,
97                           size_t siglen, RSA *rsa);