1/*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/*
35 * $Id$
36 */
37
38#ifndef _HEIM_RSA_H
39#define _HEIM_RSA_H 1
40
41/* symbol renaming */
42#define RSA_null_method hc_RSA_null_method
43#define RSA_imath_method hc_RSA_imath_method
44#define RSA_cdsa_method hc_RSA_cdsa_method
45#define RSA_sf_method hc_RSA_sf_method
46#define RSA_tfm_method hc_RSA_tfm_method
47#define RSA_ltm_method hc_RSA_ltm_method
48#define RSA_gmp_method hc_RSA_gmp_method
49#define RSA_tfm_method hc_RSA_tfm_method
50#define RSA_new hc_RSA_new
51#define RSA_new_method hc_RSA_new_method
52#define RSA_free hc_RSA_free
53#define RSA_up_ref hc_RSA_up_ref
54#define RSA_set_default_method hc_RSA_set_default_method
55#define RSA_get_default_method hc_RSA_get_default_method
56#define RSA_set_method hc_RSA_set_method
57#define RSA_get_method hc_RSA_get_method
58#define RSA_set_app_data hc_RSA_set_app_data
59#define RSA_get_app_data hc_RSA_get_app_data
60#define RSA_check_key hc_RSA_check_key
61#define RSA_size hc_RSA_size
62#define RSA_public_encrypt hc_RSA_public_encrypt
63#define RSA_public_decrypt hc_RSA_public_decrypt
64#define RSA_private_encrypt hc_RSA_private_encrypt
65#define RSA_private_decrypt hc_RSA_private_decrypt
66#define RSA_sign hc_RSA_sign
67#define RSA_verify hc_RSA_verify
68#define RSA_generate_key_ex hc_RSA_generate_key_ex
69#define d2i_RSAPrivateKey hc_d2i_RSAPrivateKey
70#define i2d_RSAPrivateKey hc_i2d_RSAPrivateKey
71#define i2d_RSAPublicKey hc_i2d_RSAPublicKey
72#define d2i_RSAPublicKey hc_d2i_RSAPublicKey
73
74/*
75 *
76 */
77
78typedef struct RSA RSA;
79typedef struct RSA_METHOD RSA_METHOD;
80
81#include <hcrypto/bn.h>
82#include <hcrypto/engine.h>
83
84struct RSA_METHOD {
85    const char *name;
86    int (*rsa_pub_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
87    int (*rsa_pub_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
88    int (*rsa_priv_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
89    int (*rsa_priv_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
90    void *rsa_mod_exp;
91    void *bn_mod_exp;
92    int (*init)(RSA *rsa);
93    int (*finish)(RSA *rsa);
94    int flags;
95    char *app_data;
96    int (*rsa_sign)(int, const unsigned char *, unsigned int,
97		    unsigned char *, unsigned int *, const RSA *);
98    int (*rsa_verify)(int, const unsigned char *, unsigned int,
99		      unsigned char *, unsigned int, const RSA *);
100    int (*rsa_keygen)(RSA *, int, BIGNUM *, BN_GENCB *);
101};
102
103struct RSA {
104    int pad;
105    long version;
106    const RSA_METHOD *meth;
107    void *engine;
108    BIGNUM *n;
109    BIGNUM *e;
110    BIGNUM *d;
111    BIGNUM *p;
112    BIGNUM *q;
113    BIGNUM *dmp1;
114    BIGNUM *dmq1;
115    BIGNUM *iqmp;
116    struct rsa_CRYPTO_EX_DATA {
117	void *sk;
118	int dummy;
119    } ex_data;
120    int references;
121    int flags;
122    void *_method_mod_n;
123    void *_method_mod_p;
124    void *_method_mod_q;
125
126    char *bignum_data;
127    void *blinding;
128    void *mt_blinding;
129};
130
131#define RSA_FLAG_NO_BLINDING		0x0080
132
133#define RSA_PKCS1_PADDING		1
134#define RSA_PKCS1_OAEP_PADDING		4
135#define RSA_PKCS1_PADDING_SIZE		11
136
137/*
138 *
139 */
140
141const RSA_METHOD *RSA_null_method(void);
142const RSA_METHOD *RSA_gmp_method(void);
143const RSA_METHOD *RSA_sf_method(void);
144const RSA_METHOD *RSA_cdsa_method(void);
145const RSA_METHOD *RSA_tfm_method(void);
146const RSA_METHOD *RSA_ltm_method(void);
147
148/*
149 *
150 */
151
152RSA *	RSA_new(void);
153RSA *	RSA_new_method(ENGINE *);
154void	RSA_free(RSA *);
155int	RSA_up_ref(RSA *);
156
157void	RSA_set_default_method(const RSA_METHOD *);
158const RSA_METHOD * RSA_get_default_method(void);
159
160const RSA_METHOD * RSA_get_method(const RSA *);
161int RSA_set_method(RSA *, const RSA_METHOD *);
162
163int	RSA_set_app_data(RSA *, void *arg);
164void *	RSA_get_app_data(const RSA *);
165
166int	RSA_check_key(const RSA *);
167int	RSA_size(const RSA *);
168
169int	RSA_public_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
170int	RSA_private_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
171int	RSA_public_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
172int	RSA_private_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
173
174int RSA_sign(int, const unsigned char *, unsigned int,
175	     unsigned char *, unsigned int *, RSA *);
176int RSA_verify(int, const unsigned char *, unsigned int,
177	       unsigned char *, unsigned int, RSA *);
178
179int	RSA_generate_key_ex(RSA *, int, BIGNUM *, BN_GENCB *);
180
181RSA *	d2i_RSAPrivateKey(RSA *, const unsigned char **, size_t);
182int	i2d_RSAPrivateKey(RSA *, unsigned char **);
183
184int	i2d_RSAPublicKey(RSA *, unsigned char **);
185RSA *	d2i_RSAPublicKey(RSA *, const unsigned char **, size_t);
186
187#endif /* _HEIM_RSA_H */
188