xref: /openbsd-src/lib/libcrypto/rsa/rsa_local.h (revision 07ac5809962bf446b7e83b72bc1437e1f234567e)
1*07ac5809Stb /* $OpenBSD: rsa_local.h,v 1.10 2025/01/05 15:39:12 tb Exp $ */
22e070e51Stb /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
32e070e51Stb  * All rights reserved.
42e070e51Stb  *
52e070e51Stb  * This package is an SSL implementation written
62e070e51Stb  * by Eric Young (eay@cryptsoft.com).
72e070e51Stb  * The implementation was written so as to conform with Netscapes SSL.
82e070e51Stb  *
92e070e51Stb  * This library is free for commercial and non-commercial use as long as
102e070e51Stb  * the following conditions are aheared to.  The following conditions
112e070e51Stb  * apply to all code found in this distribution, be it the RC4, RSA,
122e070e51Stb  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
132e070e51Stb  * included with this distribution is covered by the same copyright terms
142e070e51Stb  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
152e070e51Stb  *
162e070e51Stb  * Copyright remains Eric Young's, and as such any Copyright notices in
172e070e51Stb  * the code are not to be removed.
182e070e51Stb  * If this package is used in a product, Eric Young should be given attribution
192e070e51Stb  * as the author of the parts of the library used.
202e070e51Stb  * This can be in the form of a textual message at program startup or
212e070e51Stb  * in documentation (online or textual) provided with the package.
222e070e51Stb  *
232e070e51Stb  * Redistribution and use in source and binary forms, with or without
242e070e51Stb  * modification, are permitted provided that the following conditions
252e070e51Stb  * are met:
262e070e51Stb  * 1. Redistributions of source code must retain the copyright
272e070e51Stb  *    notice, this list of conditions and the following disclaimer.
282e070e51Stb  * 2. Redistributions in binary form must reproduce the above copyright
292e070e51Stb  *    notice, this list of conditions and the following disclaimer in the
302e070e51Stb  *    documentation and/or other materials provided with the distribution.
312e070e51Stb  * 3. All advertising materials mentioning features or use of this software
322e070e51Stb  *    must display the following acknowledgement:
332e070e51Stb  *    "This product includes cryptographic software written by
342e070e51Stb  *     Eric Young (eay@cryptsoft.com)"
352e070e51Stb  *    The word 'cryptographic' can be left out if the rouines from the library
362e070e51Stb  *    being used are not cryptographic related :-).
372e070e51Stb  * 4. If you include any Windows specific code (or a derivative thereof) from
382e070e51Stb  *    the apps directory (application code) you must include an acknowledgement:
392e070e51Stb  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
402e070e51Stb  *
412e070e51Stb  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
422e070e51Stb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
432e070e51Stb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
442e070e51Stb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
452e070e51Stb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
462e070e51Stb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
472e070e51Stb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
482e070e51Stb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
492e070e51Stb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
502e070e51Stb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
512e070e51Stb  * SUCH DAMAGE.
522e070e51Stb  *
532e070e51Stb  * The licence and distribution terms for any publically available version or
542e070e51Stb  * derivative of this code cannot be changed.  i.e. this code cannot simply be
552e070e51Stb  * copied and put under another distribution licence
562e070e51Stb  * [including the GNU Public Licence.]
572e070e51Stb  */
58c9675a23Stb 
59c9675a23Stb __BEGIN_HIDDEN_DECLS
60c9675a23Stb 
61c9675a23Stb #define RSA_MIN_MODULUS_BITS	512
62c9675a23Stb 
63c9675a23Stb struct rsa_meth_st {
64c9675a23Stb 	char *name;
65c9675a23Stb 	int (*rsa_pub_enc)(int flen, const unsigned char *from,
66c9675a23Stb 	    unsigned char *to, RSA *rsa, int padding);
67c9675a23Stb 	int (*rsa_pub_dec)(int flen, const unsigned char *from,
68c9675a23Stb 	    unsigned char *to, RSA *rsa, int padding);
69c9675a23Stb 	int (*rsa_priv_enc)(int flen, const unsigned char *from,
70c9675a23Stb 	    unsigned char *to, RSA *rsa, int padding);
71c9675a23Stb 	int (*rsa_priv_dec)(int flen, const unsigned char *from,
72c9675a23Stb 	    unsigned char *to, RSA *rsa, int padding);
73c9675a23Stb 	int (*rsa_mod_exp)(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
74c9675a23Stb 	    BN_CTX *ctx); /* Can be null */
75c9675a23Stb 	int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
76c9675a23Stb 	    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); /* Can be null */
77c9675a23Stb 	int (*init)(RSA *rsa);		/* called at new */
78c9675a23Stb 	int (*finish)(RSA *rsa);	/* called at free */
79c9675a23Stb 	int flags;			/* RSA_METHOD_FLAG_* things */
80c9675a23Stb 	char *app_data;			/* may be needed! */
81c9675a23Stb /* New sign and verify functions: some libraries don't allow arbitrary data
82c9675a23Stb  * to be signed/verified: this allows them to be used. Note: for this to work
83c9675a23Stb  * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used
84*07ac5809Stb  * RSA_sign(), RSA_verify() should be used instead.
85c9675a23Stb  */
86c9675a23Stb 	int (*rsa_sign)(int type, const unsigned char *m, unsigned int m_length,
87c9675a23Stb 	    unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
88c9675a23Stb 	int (*rsa_verify)(int dtype, const unsigned char *m,
89c9675a23Stb 	    unsigned int m_length, const unsigned char *sigbuf,
90c9675a23Stb 	    unsigned int siglen, const RSA *rsa);
91c9675a23Stb /* If this callback is NULL, the builtin software RSA key-gen will be used. This
92c9675a23Stb  * is for behavioural compatibility whilst the code gets rewired, but one day
93c9675a23Stb  * it would be nice to assume there are no such things as "builtin software"
94c9675a23Stb  * implementations. */
95c9675a23Stb 	int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
96c9675a23Stb };
97c9675a23Stb 
98c9675a23Stb struct rsa_st {
99c9675a23Stb 	long version;
100c9675a23Stb 	const RSA_METHOD *meth;
101c9675a23Stb 
102c9675a23Stb 	BIGNUM *n;
103c9675a23Stb 	BIGNUM *e;
104c9675a23Stb 	BIGNUM *d;
105c9675a23Stb 	BIGNUM *p;
106c9675a23Stb 	BIGNUM *q;
107c9675a23Stb 	BIGNUM *dmp1;
108c9675a23Stb 	BIGNUM *dmq1;
109c9675a23Stb 	BIGNUM *iqmp;
110c9675a23Stb 
111c9675a23Stb 	/* Parameter restrictions for PSS only keys. */
112c9675a23Stb 	RSA_PSS_PARAMS *pss;
113c9675a23Stb 
114c9675a23Stb 	/* be careful using this if the RSA structure is shared */
115c9675a23Stb 	CRYPTO_EX_DATA ex_data;
116c9675a23Stb 	int references;
117c9675a23Stb 	int flags;
118c9675a23Stb 
119c9675a23Stb 	/* Used to cache montgomery values */
120c9675a23Stb 	BN_MONT_CTX *_method_mod_n;
121c9675a23Stb 	BN_MONT_CTX *_method_mod_p;
122c9675a23Stb 	BN_MONT_CTX *_method_mod_q;
123c9675a23Stb 
124c9675a23Stb 	/* all BIGNUM values are actually in the following data, if it is not
125c9675a23Stb 	 * NULL */
126c9675a23Stb 	BN_BLINDING *blinding;
127c9675a23Stb 	BN_BLINDING *mt_blinding;
128c9675a23Stb };
129c9675a23Stb 
130c9675a23Stb RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd, const EVP_MD *mgf1md,
131c9675a23Stb     int saltlen);
132c9675a23Stb int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
133c9675a23Stb     const EVP_MD **pmgf1md, int *psaltlen);
134c9675a23Stb 
135c9675a23Stb extern int int_rsa_verify(int dtype, const unsigned char *m,
136c9675a23Stb     unsigned int m_len, unsigned char *rm, size_t *prm_len,
137c9675a23Stb     const unsigned char *sigbuf, size_t siglen, RSA *rsa);
138c9675a23Stb 
1394349b8b3Stb int RSA_padding_add_X931(unsigned char *to, int tlen,
1404349b8b3Stb     const unsigned char *f, int fl);
1414349b8b3Stb int RSA_padding_check_X931(unsigned char *to, int tlen,
1424349b8b3Stb     const unsigned char *f, int fl, int rsa_len);
1434349b8b3Stb int RSA_X931_hash_id(int nid);
1444349b8b3Stb 
145f2606938Stb BN_BLINDING *BN_BLINDING_new(const BIGNUM *e, const BIGNUM *mod, BN_CTX *ctx,
146f2606938Stb     int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
147f2606938Stb     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), BN_MONT_CTX *m_ctx);
148f2606938Stb void BN_BLINDING_free(BN_BLINDING *b);
149f2606938Stb int BN_BLINDING_convert(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
150f2606938Stb int BN_BLINDING_invert(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
151226d4252Stb int BN_BLINDING_is_local(BN_BLINDING *b);
152aa88ce08Stb BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *ctx);
153aa88ce08Stb 
154c9675a23Stb __END_HIDDEN_DECLS
155