xref: /openbsd/lib/libcrypto/rsa/rsa_local.h (revision a21c64b7)
1 /* $OpenBSD: rsa_local.h,v 1.8 2024/01/01 15:43:02 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 __BEGIN_HIDDEN_DECLS
60 
61 #define RSA_MIN_MODULUS_BITS	512
62 
63 struct rsa_meth_st {
64 	char *name;
65 	int (*rsa_pub_enc)(int flen, const unsigned char *from,
66 	    unsigned char *to, RSA *rsa, int padding);
67 	int (*rsa_pub_dec)(int flen, const unsigned char *from,
68 	    unsigned char *to, RSA *rsa, int padding);
69 	int (*rsa_priv_enc)(int flen, const unsigned char *from,
70 	    unsigned char *to, RSA *rsa, int padding);
71 	int (*rsa_priv_dec)(int flen, const unsigned char *from,
72 	    unsigned char *to, RSA *rsa, int padding);
73 	int (*rsa_mod_exp)(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
74 	    BN_CTX *ctx); /* Can be null */
75 	int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
76 	    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); /* Can be null */
77 	int (*init)(RSA *rsa);		/* called at new */
78 	int (*finish)(RSA *rsa);	/* called at free */
79 	int flags;			/* RSA_METHOD_FLAG_* things */
80 	char *app_data;			/* may be needed! */
81 /* New sign and verify functions: some libraries don't allow arbitrary data
82  * to be signed/verified: this allows them to be used. Note: for this to work
83  * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used
84  * RSA_sign(), RSA_verify() should be used instead. Note: for backwards
85  * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER
86  * option is set in 'flags'.
87  */
88 	int (*rsa_sign)(int type, const unsigned char *m, unsigned int m_length,
89 	    unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
90 	int (*rsa_verify)(int dtype, const unsigned char *m,
91 	    unsigned int m_length, const unsigned char *sigbuf,
92 	    unsigned int siglen, const RSA *rsa);
93 /* If this callback is NULL, the builtin software RSA key-gen will be used. This
94  * is for behavioural compatibility whilst the code gets rewired, but one day
95  * it would be nice to assume there are no such things as "builtin software"
96  * implementations. */
97 	int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
98 };
99 
100 struct rsa_st {
101 	/* The first parameter is used to pickup errors where
102 	 * this is passed instead of aEVP_PKEY, it is set to 0 */
103 	int pad;
104 	long version;
105 	const RSA_METHOD *meth;
106 
107 	BIGNUM *n;
108 	BIGNUM *e;
109 	BIGNUM *d;
110 	BIGNUM *p;
111 	BIGNUM *q;
112 	BIGNUM *dmp1;
113 	BIGNUM *dmq1;
114 	BIGNUM *iqmp;
115 
116 	/* Parameter restrictions for PSS only keys. */
117 	RSA_PSS_PARAMS *pss;
118 
119 	/* be careful using this if the RSA structure is shared */
120 	CRYPTO_EX_DATA ex_data;
121 	int references;
122 	int flags;
123 
124 	/* Used to cache montgomery values */
125 	BN_MONT_CTX *_method_mod_n;
126 	BN_MONT_CTX *_method_mod_p;
127 	BN_MONT_CTX *_method_mod_q;
128 
129 	/* all BIGNUM values are actually in the following data, if it is not
130 	 * NULL */
131 	BN_BLINDING *blinding;
132 	BN_BLINDING *mt_blinding;
133 };
134 
135 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd, const EVP_MD *mgf1md,
136     int saltlen);
137 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
138     const EVP_MD **pmgf1md, int *psaltlen);
139 
140 extern int int_rsa_verify(int dtype, const unsigned char *m,
141     unsigned int m_len, unsigned char *rm, size_t *prm_len,
142     const unsigned char *sigbuf, size_t siglen, RSA *rsa);
143 
144 int RSA_padding_add_X931(unsigned char *to, int tlen,
145     const unsigned char *f, int fl);
146 int RSA_padding_check_X931(unsigned char *to, int tlen,
147     const unsigned char *f, int fl, int rsa_len);
148 int RSA_X931_hash_id(int nid);
149 
150 BN_BLINDING *BN_BLINDING_new(const BIGNUM *e, const BIGNUM *mod, BN_CTX *ctx,
151     int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
152     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), BN_MONT_CTX *m_ctx);
153 void BN_BLINDING_free(BN_BLINDING *b);
154 int BN_BLINDING_convert(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
155 int BN_BLINDING_invert(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
156 int BN_BLINDING_is_local(BN_BLINDING *b);
157 BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *ctx);
158 
159 __END_HIDDEN_DECLS
160