156083Skris/* crypto/rsa/rsa.h */
256083Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
356083Skris * All rights reserved.
456083Skris *
556083Skris * This package is an SSL implementation written
656083Skris * by Eric Young (eay@cryptsoft.com).
756083Skris * The implementation was written so as to conform with Netscapes SSL.
8280297Sjkim *
956083Skris * This library is free for commercial and non-commercial use as long as
1056083Skris * the following conditions are aheared to.  The following conditions
1156083Skris * apply to all code found in this distribution, be it the RC4, RSA,
1256083Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1356083Skris * included with this distribution is covered by the same copyright terms
1456083Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280297Sjkim *
1656083Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1756083Skris * the code are not to be removed.
1856083Skris * If this package is used in a product, Eric Young should be given attribution
1956083Skris * as the author of the parts of the library used.
2056083Skris * This can be in the form of a textual message at program startup or
2156083Skris * in documentation (online or textual) provided with the package.
22280297Sjkim *
2356083Skris * Redistribution and use in source and binary forms, with or without
2456083Skris * modification, are permitted provided that the following conditions
2556083Skris * are met:
2656083Skris * 1. Redistributions of source code must retain the copyright
2756083Skris *    notice, this list of conditions and the following disclaimer.
2856083Skris * 2. Redistributions in binary form must reproduce the above copyright
2956083Skris *    notice, this list of conditions and the following disclaimer in the
3056083Skris *    documentation and/or other materials provided with the distribution.
3156083Skris * 3. All advertising materials mentioning features or use of this software
3256083Skris *    must display the following acknowledgement:
3356083Skris *    "This product includes cryptographic software written by
3456083Skris *     Eric Young (eay@cryptsoft.com)"
3556083Skris *    The word 'cryptographic' can be left out if the rouines from the library
3656083Skris *    being used are not cryptographic related :-).
37280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3856083Skris *    the apps directory (application code) you must include an acknowledgement:
3956083Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280297Sjkim *
4156083Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4256083Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4356083Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4456083Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4556083Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4656083Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4756083Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4856083Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4956083Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5056083Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5156083Skris * SUCH DAMAGE.
52280297Sjkim *
5356083Skris * The licence and distribution terms for any publically available version or
5456083Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5556083Skris * copied and put under another distribution licence
5656083Skris * [including the GNU Public Licence.]
5756083Skris */
5856083Skris
5956083Skris#ifndef HEADER_RSA_H
60280297Sjkim# define HEADER_RSA_H
6156083Skris
62280297Sjkim# include <openssl/asn1.h>
63110007Smarkm
64280297Sjkim# ifndef OPENSSL_NO_BIO
65280297Sjkim#  include <openssl/bio.h>
66280297Sjkim# endif
67280297Sjkim# include <openssl/crypto.h>
68280297Sjkim# include <openssl/ossl_typ.h>
69280297Sjkim# ifndef OPENSSL_NO_DEPRECATED
70280297Sjkim#  include <openssl/bn.h>
71280297Sjkim# endif
7256083Skris
73280297Sjkim# ifdef OPENSSL_NO_RSA
74280297Sjkim#  error RSA is disabled.
75280297Sjkim# endif
7656083Skris
7768654Skris#ifdef  __cplusplus
7868654Skrisextern "C" {
7968654Skris#endif
8068654Skris
81160817Ssimon/* Declared already in ossl_typ.h */
82160817Ssimon/* typedef struct rsa_st RSA; */
83160817Ssimon/* typedef struct rsa_meth_st RSA_METHOD; */
8456083Skris
85280297Sjkimstruct rsa_meth_st {
86280297Sjkim    const char *name;
87280297Sjkim    int (*rsa_pub_enc) (int flen, const unsigned char *from,
88280297Sjkim                        unsigned char *to, RSA *rsa, int padding);
89280297Sjkim    int (*rsa_pub_dec) (int flen, const unsigned char *from,
90280297Sjkim                        unsigned char *to, RSA *rsa, int padding);
91280297Sjkim    int (*rsa_priv_enc) (int flen, const unsigned char *from,
92280297Sjkim                         unsigned char *to, RSA *rsa, int padding);
93280297Sjkim    int (*rsa_priv_dec) (int flen, const unsigned char *from,
94280297Sjkim                         unsigned char *to, RSA *rsa, int padding);
95280297Sjkim    /* Can be null */
96280297Sjkim    int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
97280297Sjkim    /* Can be null */
98280297Sjkim    int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
99280297Sjkim                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
100280297Sjkim    /* called at new */
101280297Sjkim    int (*init) (RSA *rsa);
102280297Sjkim    /* called at free */
103280297Sjkim    int (*finish) (RSA *rsa);
104280297Sjkim    /* RSA_METHOD_FLAG_* things */
105280297Sjkim    int flags;
106280297Sjkim    /* may be needed! */
107280297Sjkim    char *app_data;
108280297Sjkim    /*
109280297Sjkim     * New sign and verify functions: some libraries don't allow arbitrary
110280297Sjkim     * data to be signed/verified: this allows them to be used. Note: for
111280297Sjkim     * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
112280297Sjkim     * *NOT* be used RSA_sign(), RSA_verify() should be used instead. Note:
113280297Sjkim     * for backwards compatibility this functionality is only enabled if the
114280297Sjkim     * RSA_FLAG_SIGN_VER option is set in 'flags'.
115280297Sjkim     */
116280297Sjkim    int (*rsa_sign) (int type,
117280297Sjkim                     const unsigned char *m, unsigned int m_length,
118280297Sjkim                     unsigned char *sigret, unsigned int *siglen,
119280297Sjkim                     const RSA *rsa);
120280297Sjkim    int (*rsa_verify) (int dtype, const unsigned char *m,
121280297Sjkim                       unsigned int m_length, const unsigned char *sigbuf,
122280297Sjkim                       unsigned int siglen, const RSA *rsa);
123280297Sjkim    /*
124280297Sjkim     * If this callback is NULL, the builtin software RSA key-gen will be
125280297Sjkim     * used. This is for behavioural compatibility whilst the code gets
126280297Sjkim     * rewired, but one day it would be nice to assume there are no such
127280297Sjkim     * things as "builtin software" implementations.
128280297Sjkim     */
129280297Sjkim    int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
130280297Sjkim};
131100931Snectar
132280297Sjkimstruct rsa_st {
133280297Sjkim    /*
134280297Sjkim     * The first parameter is used to pickup errors where this is passed
135280297Sjkim     * instead of aEVP_PKEY, it is set to 0
136280297Sjkim     */
137280297Sjkim    int pad;
138280297Sjkim    long version;
139280297Sjkim    const RSA_METHOD *meth;
140280297Sjkim    /* functional reference if 'meth' is ENGINE-provided */
141280297Sjkim    ENGINE *engine;
142280297Sjkim    BIGNUM *n;
143280297Sjkim    BIGNUM *e;
144280297Sjkim    BIGNUM *d;
145280297Sjkim    BIGNUM *p;
146280297Sjkim    BIGNUM *q;
147280297Sjkim    BIGNUM *dmp1;
148280297Sjkim    BIGNUM *dmq1;
149280297Sjkim    BIGNUM *iqmp;
150280297Sjkim    /* be careful using this if the RSA structure is shared */
151280297Sjkim    CRYPTO_EX_DATA ex_data;
152280297Sjkim    int references;
153280297Sjkim    int flags;
154280297Sjkim    /* Used to cache montgomery values */
155280297Sjkim    BN_MONT_CTX *_method_mod_n;
156280297Sjkim    BN_MONT_CTX *_method_mod_p;
157280297Sjkim    BN_MONT_CTX *_method_mod_q;
158280297Sjkim    /*
159280297Sjkim     * all BIGNUM values are actually in the following data, if it is not
160280297Sjkim     * NULL
161280297Sjkim     */
162280297Sjkim    char *bignum_data;
163280297Sjkim    BN_BLINDING *blinding;
164280297Sjkim    BN_BLINDING *mt_blinding;
165280297Sjkim};
16656083Skris
167280297Sjkim# ifndef OPENSSL_RSA_MAX_MODULUS_BITS
168280297Sjkim#  define OPENSSL_RSA_MAX_MODULUS_BITS   16384
169280297Sjkim# endif
17056083Skris
171280297Sjkim# ifndef OPENSSL_RSA_SMALL_MODULUS_BITS
172280297Sjkim#  define OPENSSL_RSA_SMALL_MODULUS_BITS 3072
173280297Sjkim# endif
174280297Sjkim# ifndef OPENSSL_RSA_MAX_PUBEXP_BITS
17556083Skris
176280297Sjkim/* exponent limit enforced for "large" modulus only */
177280297Sjkim#  define OPENSSL_RSA_MAX_PUBEXP_BITS    64
178280297Sjkim# endif
179162914Ssimon
180280297Sjkim# define RSA_3   0x3L
181280297Sjkim# define RSA_F4  0x10001L
182162914Ssimon
183280297Sjkim# define RSA_METHOD_FLAG_NO_CHECK        0x0001/* don't check pub/private
184280297Sjkim                                                * match */
18556083Skris
186280297Sjkim# define RSA_FLAG_CACHE_PUBLIC           0x0002
187280297Sjkim# define RSA_FLAG_CACHE_PRIVATE          0x0004
188280297Sjkim# define RSA_FLAG_BLINDING               0x0008
189280297Sjkim# define RSA_FLAG_THREAD_SAFE            0x0010
190280297Sjkim/*
191280297Sjkim * This flag means the private key operations will be handled by rsa_mod_exp
19256083Skris * and that they do not depend on the private key components being present:
193280297Sjkim * for example a key stored in external hardware. Without this flag
194280297Sjkim * bn_mod_exp gets called when private key components are absent.
19556083Skris */
196280297Sjkim# define RSA_FLAG_EXT_PKEY               0x0020
19756083Skris
198280297Sjkim/*
199280297Sjkim * This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify
200280297Sjkim * functions.
20159194Skris */
202280297Sjkim# define RSA_FLAG_SIGN_VER               0x0040
20359194Skris
204280297Sjkim/*
205280297Sjkim * new with 0.9.6j and 0.9.7b; the built-in
206280297Sjkim * RSA implementation now uses blinding by
207280297Sjkim * default (ignoring RSA_FLAG_BLINDING),
208280297Sjkim * but other engines might not need it
209280297Sjkim */
210280297Sjkim# define RSA_FLAG_NO_BLINDING            0x0080
211280297Sjkim/*
212280297Sjkim * new with 0.9.8f; the built-in RSA
213280297Sjkim * implementation now uses constant time
214280297Sjkim * operations by default in private key operations,
215280297Sjkim * e.g., constant time modular exponentiation,
216280297Sjkim * modular inverse without leaking branches,
217280297Sjkim * division without leaking branches. This
218280297Sjkim * flag disables these constant time
219280297Sjkim * operations and results in faster RSA
220280297Sjkim * private key operations.
221280297Sjkim */
222280297Sjkim# define RSA_FLAG_NO_CONSTTIME           0x0100
223280297Sjkim# ifdef OPENSSL_USE_DEPRECATED
224280297Sjkim/* deprecated name for the flag*/
225280297Sjkim/*
226280297Sjkim * new with 0.9.7h; the built-in RSA
227280297Sjkim * implementation now uses constant time
228280297Sjkim * modular exponentiation for secret exponents
229280297Sjkim * by default. This flag causes the
230280297Sjkim * faster variable sliding window method to
231280297Sjkim * be used for all exponents.
232280297Sjkim */
233280297Sjkim#  define RSA_FLAG_NO_EXP_CONSTTIME RSA_FLAG_NO_CONSTTIME
234280297Sjkim# endif
235120635Snectar
236280297Sjkim# define EVP_PKEY_CTX_set_rsa_padding(ctx, pad) \
237280297Sjkim        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING, \
238280297Sjkim                                pad, NULL)
239194206Ssimon
240280297Sjkim# define EVP_PKEY_CTX_get_rsa_padding(ctx, ppad) \
241280297Sjkim        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, \
242280297Sjkim                                EVP_PKEY_CTRL_GET_RSA_PADDING, 0, ppad)
243238405Sjkim
244280297Sjkim# define EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, len) \
245280297Sjkim        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, \
246280297Sjkim                                (EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY), \
247280297Sjkim                                EVP_PKEY_CTRL_RSA_PSS_SALTLEN, \
248280297Sjkim                                len, NULL)
249238405Sjkim
250280297Sjkim# define EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, plen) \
251280297Sjkim        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, \
252280297Sjkim                                (EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY), \
253280297Sjkim                                EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, \
254280297Sjkim                                0, plen)
255238405Sjkim
256280297Sjkim# define EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) \
257280297Sjkim        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN, \
258280297Sjkim                                EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL)
259238405Sjkim
260280297Sjkim# define EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp) \
261280297Sjkim        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN, \
262280297Sjkim                                EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp)
263238405Sjkim
264280297Sjkim# define  EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md)  \
265290207Sjkim                EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, \
266290207Sjkim                        EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, \
267280297Sjkim                                EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md)
268238405Sjkim
269290207Sjkim# define  EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md)  \
270290207Sjkim                EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,  \
271290207Sjkim                                EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md)
272290207Sjkim
273280297Sjkim# define  EVP_PKEY_CTX_get_rsa_mgf1_md(ctx, pmd) \
274290207Sjkim                EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, \
275290207Sjkim                        EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, \
276280297Sjkim                                EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)pmd)
277238405Sjkim
278290207Sjkim# define  EVP_PKEY_CTX_get_rsa_oaep_md(ctx, pmd) \
279290207Sjkim                EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,  \
280290207Sjkim                                EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)pmd)
281290207Sjkim
282290207Sjkim# define  EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, l, llen) \
283290207Sjkim                EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,  \
284290207Sjkim                                EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen, (void *)l)
285290207Sjkim
286290207Sjkim# define  EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, l)       \
287290207Sjkim                EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,  \
288290207Sjkim                                EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, (void *)l)
289290207Sjkim
290280297Sjkim# define EVP_PKEY_CTRL_RSA_PADDING       (EVP_PKEY_ALG_CTRL + 1)
291280297Sjkim# define EVP_PKEY_CTRL_RSA_PSS_SALTLEN   (EVP_PKEY_ALG_CTRL + 2)
292238405Sjkim
293280297Sjkim# define EVP_PKEY_CTRL_RSA_KEYGEN_BITS   (EVP_PKEY_ALG_CTRL + 3)
294280297Sjkim# define EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP (EVP_PKEY_ALG_CTRL + 4)
295280297Sjkim# define EVP_PKEY_CTRL_RSA_MGF1_MD       (EVP_PKEY_ALG_CTRL + 5)
296238405Sjkim
297280297Sjkim# define EVP_PKEY_CTRL_GET_RSA_PADDING           (EVP_PKEY_ALG_CTRL + 6)
298280297Sjkim# define EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN       (EVP_PKEY_ALG_CTRL + 7)
299280297Sjkim# define EVP_PKEY_CTRL_GET_RSA_MGF1_MD           (EVP_PKEY_ALG_CTRL + 8)
300238405Sjkim
301290207Sjkim# define EVP_PKEY_CTRL_RSA_OAEP_MD       (EVP_PKEY_ALG_CTRL + 9)
302290207Sjkim# define EVP_PKEY_CTRL_RSA_OAEP_LABEL    (EVP_PKEY_ALG_CTRL + 10)
303290207Sjkim
304290207Sjkim# define EVP_PKEY_CTRL_GET_RSA_OAEP_MD   (EVP_PKEY_ALG_CTRL + 11)
305290207Sjkim# define EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 12)
306290207Sjkim
307280297Sjkim# define RSA_PKCS1_PADDING       1
308280297Sjkim# define RSA_SSLV23_PADDING      2
309280297Sjkim# define RSA_NO_PADDING          3
310280297Sjkim# define RSA_PKCS1_OAEP_PADDING  4
311280297Sjkim# define RSA_X931_PADDING        5
312238405Sjkim/* EVP_PKEY_ only */
313280297Sjkim# define RSA_PKCS1_PSS_PADDING   6
31456083Skris
315280297Sjkim# define RSA_PKCS1_PADDING_SIZE  11
316110007Smarkm
317280297Sjkim# define RSA_set_app_data(s,arg)         RSA_set_ex_data(s,0,arg)
318280297Sjkim# define RSA_get_app_data(s)             RSA_get_ex_data(s,0)
31956083Skris
320280297SjkimRSA *RSA_new(void);
321280297SjkimRSA *RSA_new_method(ENGINE *engine);
322280297Sjkimint RSA_size(const RSA *rsa);
323160817Ssimon
324160817Ssimon/* Deprecated version */
325280297Sjkim# ifndef OPENSSL_NO_DEPRECATED
326280297SjkimRSA *RSA_generate_key(int bits, unsigned long e, void
327280297Sjkim                       (*callback) (int, int, void *), void *cb_arg);
328280297Sjkim# endif                         /* !defined(OPENSSL_NO_DEPRECATED) */
329160817Ssimon
330160817Ssimon/* New version */
331280297Sjkimint RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
332160817Ssimon
333280297Sjkimint RSA_check_key(const RSA *);
334280297Sjkim        /* next 4 return -1 on error */
335280297Sjkimint RSA_public_encrypt(int flen, const unsigned char *from,
336280297Sjkim                       unsigned char *to, RSA *rsa, int padding);
337280297Sjkimint RSA_private_encrypt(int flen, const unsigned char *from,
338280297Sjkim                        unsigned char *to, RSA *rsa, int padding);
339280297Sjkimint RSA_public_decrypt(int flen, const unsigned char *from,
340280297Sjkim                       unsigned char *to, RSA *rsa, int padding);
341280297Sjkimint RSA_private_decrypt(int flen, const unsigned char *from,
342280297Sjkim                        unsigned char *to, RSA *rsa, int padding);
343280297Sjkimvoid RSA_free(RSA *r);
344110007Smarkm/* "up" the RSA object's reference count */
345280297Sjkimint RSA_up_ref(RSA *r);
34656083Skris
347280297Sjkimint RSA_flags(const RSA *r);
34856083Skris
349110007Smarkmvoid RSA_set_default_method(const RSA_METHOD *meth);
350110007Smarkmconst RSA_METHOD *RSA_get_default_method(void);
351110007Smarkmconst RSA_METHOD *RSA_get_method(const RSA *rsa);
352110007Smarkmint RSA_set_method(RSA *rsa, const RSA_METHOD *meth);
35356083Skris
35456083Skris/* This function needs the memory locking malloc callbacks to be installed */
35556083Skrisint RSA_memory_lock(RSA *r);
35656083Skris
35756083Skris/* these are the actual SSLeay RSA functions */
358110007Smarkmconst RSA_METHOD *RSA_PKCS1_SSLeay(void);
35956083Skris
360110007Smarkmconst RSA_METHOD *RSA_null_method(void);
36159194Skris
362110007SmarkmDECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey)
363110007SmarkmDECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey)
364110007Smarkm
365280297Sjkimtypedef struct rsa_pss_params_st {
366280297Sjkim    X509_ALGOR *hashAlgorithm;
367280297Sjkim    X509_ALGOR *maskGenAlgorithm;
368280297Sjkim    ASN1_INTEGER *saltLength;
369280297Sjkim    ASN1_INTEGER *trailerField;
370280297Sjkim} RSA_PSS_PARAMS;
371238405Sjkim
372238405SjkimDECLARE_ASN1_FUNCTIONS(RSA_PSS_PARAMS)
373238405Sjkim
374290207Sjkimtypedef struct rsa_oaep_params_st {
375290207Sjkim    X509_ALGOR *hashFunc;
376290207Sjkim    X509_ALGOR *maskGenFunc;
377290207Sjkim    X509_ALGOR *pSourceFunc;
378290207Sjkim} RSA_OAEP_PARAMS;
379290207Sjkim
380290207SjkimDECLARE_ASN1_FUNCTIONS(RSA_OAEP_PARAMS)
381290207Sjkim
382280297Sjkim# ifndef OPENSSL_NO_FP_API
383280297Sjkimint RSA_print_fp(FILE *fp, const RSA *r, int offset);
384280297Sjkim# endif
38556083Skris
386280297Sjkim# ifndef OPENSSL_NO_BIO
387280297Sjkimint RSA_print(BIO *bp, const RSA *r, int offset);
388280297Sjkim# endif
38956083Skris
390280297Sjkim# ifndef OPENSSL_NO_RC4
391160817Ssimonint i2d_RSA_NET(const RSA *a, unsigned char **pp,
392280297Sjkim                int (*cb) (char *buf, int len, const char *prompt,
393280297Sjkim                           int verify), int sgckey);
394160817SsimonRSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length,
395280297Sjkim                 int (*cb) (char *buf, int len, const char *prompt,
396280297Sjkim                            int verify), int sgckey);
39768654Skris
398160817Ssimonint i2d_Netscape_RSA(const RSA *a, unsigned char **pp,
399280297Sjkim                     int (*cb) (char *buf, int len, const char *prompt,
400280297Sjkim                                int verify));
401160817SsimonRSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length,
402280297Sjkim                      int (*cb) (char *buf, int len, const char *prompt,
403280297Sjkim                                 int verify));
404280297Sjkim# endif
40556083Skris
406280297Sjkim/*
407280297Sjkim * The following 2 functions sign and verify a X509_SIG ASN1 object inside
408280297Sjkim * PKCS#1 padded RSA encryption
409280297Sjkim */
410110007Smarkmint RSA_sign(int type, const unsigned char *m, unsigned int m_length,
411280297Sjkim             unsigned char *sigret, unsigned int *siglen, RSA *rsa);
412110007Smarkmint RSA_verify(int type, const unsigned char *m, unsigned int m_length,
413280297Sjkim               const unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
41456083Skris
415280297Sjkim/*
416280297Sjkim * The following 2 function sign and verify a ASN1_OCTET_STRING object inside
417280297Sjkim * PKCS#1 padded RSA encryption
418280297Sjkim */
419110007Smarkmint RSA_sign_ASN1_OCTET_STRING(int type,
420280297Sjkim                               const unsigned char *m, unsigned int m_length,
421280297Sjkim                               unsigned char *sigret, unsigned int *siglen,
422280297Sjkim                               RSA *rsa);
423280297Sjkimint RSA_verify_ASN1_OCTET_STRING(int type, const unsigned char *m,
424280297Sjkim                                 unsigned int m_length, unsigned char *sigbuf,
425280297Sjkim                                 unsigned int siglen, RSA *rsa);
42656083Skris
42756083Skrisint RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
42856083Skrisvoid RSA_blinding_off(RSA *rsa);
429160817SsimonBN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *ctx);
43056083Skris
431280297Sjkimint RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
432280297Sjkim                                 const unsigned char *f, int fl);
433280297Sjkimint RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
434280297Sjkim                                   const unsigned char *f, int fl,
435280297Sjkim                                   int rsa_len);
436280297Sjkimint RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
437280297Sjkim                                 const unsigned char *f, int fl);
438280297Sjkimint RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
439280297Sjkim                                   const unsigned char *f, int fl,
440280297Sjkim                                   int rsa_len);
441280297Sjkimint PKCS1_MGF1(unsigned char *mask, long len, const unsigned char *seed,
442280297Sjkim               long seedlen, const EVP_MD *dgst);
443280297Sjkimint RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
444280297Sjkim                               const unsigned char *f, int fl,
445280297Sjkim                               const unsigned char *p, int pl);
446280297Sjkimint RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
447280297Sjkim                                 const unsigned char *f, int fl, int rsa_len,
448280297Sjkim                                 const unsigned char *p, int pl);
449290207Sjkimint RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
450290207Sjkim                                    const unsigned char *from, int flen,
451290207Sjkim                                    const unsigned char *param, int plen,
452290207Sjkim                                    const EVP_MD *md, const EVP_MD *mgf1md);
453290207Sjkimint RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
454290207Sjkim                                      const unsigned char *from, int flen,
455290207Sjkim                                      int num, const unsigned char *param,
456290207Sjkim                                      int plen, const EVP_MD *md,
457290207Sjkim                                      const EVP_MD *mgf1md);
458280297Sjkimint RSA_padding_add_SSLv23(unsigned char *to, int tlen,
459280297Sjkim                           const unsigned char *f, int fl);
460280297Sjkimint RSA_padding_check_SSLv23(unsigned char *to, int tlen,
461280297Sjkim                             const unsigned char *f, int fl, int rsa_len);
462280297Sjkimint RSA_padding_add_none(unsigned char *to, int tlen, const unsigned char *f,
463280297Sjkim                         int fl);
464280297Sjkimint RSA_padding_check_none(unsigned char *to, int tlen,
465280297Sjkim                           const unsigned char *f, int fl, int rsa_len);
466280297Sjkimint RSA_padding_add_X931(unsigned char *to, int tlen, const unsigned char *f,
467280297Sjkim                         int fl);
468280297Sjkimint RSA_padding_check_X931(unsigned char *to, int tlen,
469280297Sjkim                           const unsigned char *f, int fl, int rsa_len);
470160817Ssimonint RSA_X931_hash_id(int nid);
47156083Skris
472160817Ssimonint RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
473280297Sjkim                         const EVP_MD *Hash, const unsigned char *EM,
474280297Sjkim                         int sLen);
475160817Ssimonint RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
476280297Sjkim                              const unsigned char *mHash, const EVP_MD *Hash,
477280297Sjkim                              int sLen);
478160817Ssimon
479238405Sjkimint RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
480280297Sjkim                              const EVP_MD *Hash, const EVP_MD *mgf1Hash,
481280297Sjkim                              const unsigned char *EM, int sLen);
482238405Sjkim
483238405Sjkimint RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
484280297Sjkim                                   const unsigned char *mHash,
485280297Sjkim                                   const EVP_MD *Hash, const EVP_MD *mgf1Hash,
486280297Sjkim                                   int sLen);
487238405Sjkim
48859194Skrisint RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
489280297Sjkim                         CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
490280297Sjkimint RSA_set_ex_data(RSA *r, int idx, void *arg);
491110007Smarkmvoid *RSA_get_ex_data(const RSA *r, int idx);
49257971Skris
493110007SmarkmRSA *RSAPublicKey_dup(RSA *rsa);
494110007SmarkmRSA *RSAPrivateKey_dup(RSA *rsa);
495110007Smarkm
496280297Sjkim/*
497280297Sjkim * If this flag is set the RSA method is FIPS compliant and can be used in
498280297Sjkim * FIPS mode. This is set in the validated module method. If an application
499280297Sjkim * sets this flag in its own methods it is its responsibility to ensure the
500280297Sjkim * result is compliant.
501238405Sjkim */
502238405Sjkim
503280297Sjkim# define RSA_FLAG_FIPS_METHOD                    0x0400
504238405Sjkim
505280297Sjkim/*
506280297Sjkim * If this flag is set the operations normally disabled in FIPS mode are
507238405Sjkim * permitted it is then the applications responsibility to ensure that the
508238405Sjkim * usage is compliant.
509238405Sjkim */
510238405Sjkim
511280297Sjkim# define RSA_FLAG_NON_FIPS_ALLOW                 0x0400
512280297Sjkim/*
513280297Sjkim * Application has decided PRNG is good enough to generate a key: don't
514238405Sjkim * check.
515238405Sjkim */
516280297Sjkim# define RSA_FLAG_CHECKED                        0x0800
517238405Sjkim
51856083Skris/* BEGIN ERROR CODES */
519280297Sjkim/*
520280297Sjkim * The following lines are auto generated by the script mkerr.pl. Any changes
52156083Skris * made after this point may be overwritten when the script is next run.
52256083Skris */
52389840Skrisvoid ERR_load_RSA_strings(void);
52456083Skris
52556083Skris/* Error codes for the RSA functions. */
52656083Skris
52756083Skris/* Function codes. */
528280297Sjkim# define RSA_F_CHECK_PADDING_MD                           140
529280297Sjkim# define RSA_F_DO_RSA_PRINT                               146
530280297Sjkim# define RSA_F_INT_RSA_VERIFY                             145
531280297Sjkim# define RSA_F_MEMORY_LOCK                                100
532280297Sjkim# define RSA_F_OLD_RSA_PRIV_DECODE                        147
533280297Sjkim# define RSA_F_PKEY_RSA_CTRL                              143
534280297Sjkim# define RSA_F_PKEY_RSA_CTRL_STR                          144
535280297Sjkim# define RSA_F_PKEY_RSA_SIGN                              142
536280297Sjkim# define RSA_F_PKEY_RSA_VERIFY                            154
537280297Sjkim# define RSA_F_PKEY_RSA_VERIFYRECOVER                     141
538290207Sjkim# define RSA_F_RSA_ALGOR_TO_MD                            157
539280297Sjkim# define RSA_F_RSA_BUILTIN_KEYGEN                         129
540280297Sjkim# define RSA_F_RSA_CHECK_KEY                              123
541290207Sjkim# define RSA_F_RSA_CMS_DECRYPT                            158
542280297Sjkim# define RSA_F_RSA_EAY_PRIVATE_DECRYPT                    101
543280297Sjkim# define RSA_F_RSA_EAY_PRIVATE_ENCRYPT                    102
544280297Sjkim# define RSA_F_RSA_EAY_PUBLIC_DECRYPT                     103
545280297Sjkim# define RSA_F_RSA_EAY_PUBLIC_ENCRYPT                     104
546280297Sjkim# define RSA_F_RSA_GENERATE_KEY                           105
547280297Sjkim# define RSA_F_RSA_GENERATE_KEY_EX                        155
548280297Sjkim# define RSA_F_RSA_ITEM_VERIFY                            156
549280297Sjkim# define RSA_F_RSA_MEMORY_LOCK                            130
550290207Sjkim# define RSA_F_RSA_MGF1_TO_MD                             159
551280297Sjkim# define RSA_F_RSA_NEW_METHOD                             106
552280297Sjkim# define RSA_F_RSA_NULL                                   124
553280297Sjkim# define RSA_F_RSA_NULL_MOD_EXP                           131
554280297Sjkim# define RSA_F_RSA_NULL_PRIVATE_DECRYPT                   132
555280297Sjkim# define RSA_F_RSA_NULL_PRIVATE_ENCRYPT                   133
556280297Sjkim# define RSA_F_RSA_NULL_PUBLIC_DECRYPT                    134
557280297Sjkim# define RSA_F_RSA_NULL_PUBLIC_ENCRYPT                    135
558280297Sjkim# define RSA_F_RSA_PADDING_ADD_NONE                       107
559280297Sjkim# define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP                 121
560290207Sjkim# define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1            160
561280297Sjkim# define RSA_F_RSA_PADDING_ADD_PKCS1_PSS                  125
562280297Sjkim# define RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1             148
563280297Sjkim# define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1               108
564280297Sjkim# define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2               109
565280297Sjkim# define RSA_F_RSA_PADDING_ADD_SSLV23                     110
566280297Sjkim# define RSA_F_RSA_PADDING_ADD_X931                       127
567280297Sjkim# define RSA_F_RSA_PADDING_CHECK_NONE                     111
568280297Sjkim# define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP               122
569290207Sjkim# define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1          161
570280297Sjkim# define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1             112
571280297Sjkim# define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2             113
572280297Sjkim# define RSA_F_RSA_PADDING_CHECK_SSLV23                   114
573280297Sjkim# define RSA_F_RSA_PADDING_CHECK_X931                     128
574280297Sjkim# define RSA_F_RSA_PRINT                                  115
575280297Sjkim# define RSA_F_RSA_PRINT_FP                               116
576280297Sjkim# define RSA_F_RSA_PRIVATE_DECRYPT                        150
577280297Sjkim# define RSA_F_RSA_PRIVATE_ENCRYPT                        151
578280297Sjkim# define RSA_F_RSA_PRIV_DECODE                            137
579280297Sjkim# define RSA_F_RSA_PRIV_ENCODE                            138
580290207Sjkim# define RSA_F_RSA_PSS_TO_CTX                             162
581280297Sjkim# define RSA_F_RSA_PUBLIC_DECRYPT                         152
582280297Sjkim# define RSA_F_RSA_PUBLIC_ENCRYPT                         153
583280297Sjkim# define RSA_F_RSA_PUB_DECODE                             139
584280297Sjkim# define RSA_F_RSA_SETUP_BLINDING                         136
585280297Sjkim# define RSA_F_RSA_SIGN                                   117
586280297Sjkim# define RSA_F_RSA_SIGN_ASN1_OCTET_STRING                 118
587280297Sjkim# define RSA_F_RSA_VERIFY                                 119
588280297Sjkim# define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING               120
589280297Sjkim# define RSA_F_RSA_VERIFY_PKCS1_PSS                       126
590280297Sjkim# define RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1                  149
59156083Skris
59256083Skris/* Reason codes. */
593280297Sjkim# define RSA_R_ALGORITHM_MISMATCH                         100
594280297Sjkim# define RSA_R_BAD_E_VALUE                                101
595280297Sjkim# define RSA_R_BAD_FIXED_HEADER_DECRYPT                   102
596280297Sjkim# define RSA_R_BAD_PAD_BYTE_COUNT                         103
597280297Sjkim# define RSA_R_BAD_SIGNATURE                              104
598280297Sjkim# define RSA_R_BLOCK_TYPE_IS_NOT_01                       106
599280297Sjkim# define RSA_R_BLOCK_TYPE_IS_NOT_02                       107
600280297Sjkim# define RSA_R_DATA_GREATER_THAN_MOD_LEN                  108
601280297Sjkim# define RSA_R_DATA_TOO_LARGE                             109
602280297Sjkim# define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE                110
603280297Sjkim# define RSA_R_DATA_TOO_LARGE_FOR_MODULUS                 132
604280297Sjkim# define RSA_R_DATA_TOO_SMALL                             111
605280297Sjkim# define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE                122
606290207Sjkim# define RSA_R_DIGEST_DOES_NOT_MATCH                      166
607280297Sjkim# define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY                 112
608280297Sjkim# define RSA_R_DMP1_NOT_CONGRUENT_TO_D                    124
609280297Sjkim# define RSA_R_DMQ1_NOT_CONGRUENT_TO_D                    125
610280297Sjkim# define RSA_R_D_E_NOT_CONGRUENT_TO_1                     123
611280297Sjkim# define RSA_R_FIRST_OCTET_INVALID                        133
612280297Sjkim# define RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE        144
613290207Sjkim# define RSA_R_INVALID_DIGEST                             160
614280297Sjkim# define RSA_R_INVALID_DIGEST_LENGTH                      143
615280297Sjkim# define RSA_R_INVALID_HEADER                             137
616280297Sjkim# define RSA_R_INVALID_KEYBITS                            145
617290207Sjkim# define RSA_R_INVALID_LABEL                              161
618280297Sjkim# define RSA_R_INVALID_MESSAGE_LENGTH                     131
619280297Sjkim# define RSA_R_INVALID_MGF1_MD                            156
620290207Sjkim# define RSA_R_INVALID_OAEP_PARAMETERS                    162
621280297Sjkim# define RSA_R_INVALID_PADDING                            138
622280297Sjkim# define RSA_R_INVALID_PADDING_MODE                       141
623280297Sjkim# define RSA_R_INVALID_PSS_PARAMETERS                     149
624280297Sjkim# define RSA_R_INVALID_PSS_SALTLEN                        146
625280297Sjkim# define RSA_R_INVALID_SALT_LENGTH                        150
626280297Sjkim# define RSA_R_INVALID_TRAILER                            139
627280297Sjkim# define RSA_R_INVALID_X931_DIGEST                        142
628280297Sjkim# define RSA_R_IQMP_NOT_INVERSE_OF_Q                      126
629280297Sjkim# define RSA_R_KEY_SIZE_TOO_SMALL                         120
630280297Sjkim# define RSA_R_LAST_OCTET_INVALID                         134
631280297Sjkim# define RSA_R_MODULUS_TOO_LARGE                          105
632280297Sjkim# define RSA_R_NON_FIPS_RSA_METHOD                        157
633280297Sjkim# define RSA_R_NO_PUBLIC_EXPONENT                         140
634280297Sjkim# define RSA_R_NULL_BEFORE_BLOCK_MISSING                  113
635280297Sjkim# define RSA_R_N_DOES_NOT_EQUAL_P_Q                       127
636280297Sjkim# define RSA_R_OAEP_DECODING_ERROR                        121
637280297Sjkim# define RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE         158
638280297Sjkim# define RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE   148
639280297Sjkim# define RSA_R_PADDING_CHECK_FAILED                       114
640280297Sjkim# define RSA_R_PKCS_DECODING_ERROR                        159
641280297Sjkim# define RSA_R_P_NOT_PRIME                                128
642280297Sjkim# define RSA_R_Q_NOT_PRIME                                129
643280297Sjkim# define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED               130
644280297Sjkim# define RSA_R_SLEN_CHECK_FAILED                          136
645280297Sjkim# define RSA_R_SLEN_RECOVERY_FAILED                       135
646280297Sjkim# define RSA_R_SSLV3_ROLLBACK_ATTACK                      115
647280297Sjkim# define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116
648280297Sjkim# define RSA_R_UNKNOWN_ALGORITHM_TYPE                     117
649290207Sjkim# define RSA_R_UNKNOWN_DIGEST                             163
650280297Sjkim# define RSA_R_UNKNOWN_MASK_DIGEST                        151
651280297Sjkim# define RSA_R_UNKNOWN_PADDING_TYPE                       118
652280297Sjkim# define RSA_R_UNKNOWN_PSS_DIGEST                         152
653290207Sjkim# define RSA_R_UNSUPPORTED_ENCRYPTION_TYPE                164
654290207Sjkim# define RSA_R_UNSUPPORTED_LABEL_SOURCE                   165
655280297Sjkim# define RSA_R_UNSUPPORTED_MASK_ALGORITHM                 153
656280297Sjkim# define RSA_R_UNSUPPORTED_MASK_PARAMETER                 154
657280297Sjkim# define RSA_R_UNSUPPORTED_SIGNATURE_TYPE                 155
658280297Sjkim# define RSA_R_VALUE_MISSING                              147
659280297Sjkim# define RSA_R_WRONG_SIGNATURE_LENGTH                     119
66056083Skris
66156083Skris#ifdef  __cplusplus
66256083Skris}
66356083Skris#endif
66456083Skris#endif
665