da9d2ea4b19ab279f3c8f5c9216f490d88e510f2
[jelmer/samba4-debian.git] / source / heimdal / lib / des / rsa.h
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: rsa.h,v 1.2 2006/01/13 15:26:52 lha Exp $
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_new hc_RSA_new
44 #define RSA_new_method hc_RSA_new_method
45 #define RSA_free hc_RSA_free
46 #define RSA_up_ref hc_RSA_up_ref
47 #define RSA_set_default_method hc_RSA_set_default_method
48 #define RSA_get_default_method hc_RSA_get_default_method
49 #define RSA_set_method hc_RSA_set_method
50 #define RSA_get_method hc_RSA_get_method
51 #define RSA_set_app_data hc_RSA_set_app_data
52 #define RSA_get_app_data hc_RSA_get_app_data
53 #define RSA_check_key hc_RSA_check_key
54 #define RSA_size hc_RSA_size
55 #define RSA_public_encrypt hc_RSA_public_encrypt
56 #define RSA_public_decrypt hc_RSA_public_decrypt
57 #define RSA_private_encrypt hc_RSA_private_encrypt
58 #define RSA_private_decrypt hc_RSA_private_decrypt
59 #define RSA_sign hc_RSA_sign
60 #define RSA_verify hc_RSA_verify
61 #define d2i_RSAPrivateKey hc_d2i_RSAPrivateKey
62
63 /*
64  *
65  */
66
67 typedef struct RSA RSA;
68 typedef struct RSA_METHOD RSA_METHOD;
69
70 #include <hcrypto/bn.h>
71 #include <hcrypto/engine.h>
72
73 struct RSA_METHOD {
74     const char *name;
75     int (*rsa_pub_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
76     int (*rsa_pub_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
77     int (*rsa_priv_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
78     int (*rsa_priv_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
79     void *rsa_mod_exp;
80     void *bn_mod_exp;
81     int (*init)(RSA *rsa);
82     int (*finish)(RSA *rsa);
83     int flags;
84     char *app_data;
85     int (*rsa_sign)(int, const unsigned char *, unsigned int,
86                     unsigned char *, unsigned int *, const RSA *);
87     int (*rsa_verify)(int, const unsigned char *, unsigned int,
88                       unsigned char *, unsigned int, const RSA *);
89     int (*rsa_keygen)(RSA *, int, BIGNUM *, BN_GENCB *);
90 };
91
92 struct RSA {
93     int pad;
94     long version;
95     const RSA_METHOD *meth;
96     void *engine;
97     BIGNUM *n;
98     BIGNUM *e;
99     BIGNUM *d;
100     BIGNUM *p;
101     BIGNUM *q;
102     BIGNUM *dmp1;
103     BIGNUM *dmq1;
104     BIGNUM *iqmp;
105     struct rsa_CRYPTO_EX_DATA {
106         void *sk;
107         int dummy;
108     } ex_data;
109     int references;
110     int flags;
111     void *_method_mod_n;
112     void *_method_mod_p;
113     void *_method_mod_q;
114
115     char *bignum_data;
116     void *blinding;
117     void *mt_blinding;
118 };
119
120 #define RSA_FLAG_SIGN_VER               0x40
121
122 #define RSA_PKCS1_PADDING               1
123 #define RSA_PKCS1_PADDING_SIZE          11
124
125 /*
126  *
127  */
128
129 const RSA_METHOD *RSA_null_method(void);
130
131 /*
132  *
133  */
134
135 RSA *   RSA_new(void);
136 RSA *   RSA_new_method(ENGINE *);
137 void    RSA_free(RSA *);
138 int     RSA_up_ref(RSA *);
139
140 void    RSA_set_default_method(const RSA_METHOD *);
141 const RSA_METHOD * RSA_get_default_method(void);
142
143 const RSA_METHOD * RSA_get_method(const RSA *);
144 int RSA_set_method(RSA *, const RSA_METHOD *);
145
146 int     RSA_set_app_data(RSA *, void *arg);
147 void *  RSA_get_app_data(RSA *);
148
149 int     RSA_check_key(const RSA *);
150 int     RSA_size(const RSA *);
151
152 int     RSA_public_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
153 int     RSA_private_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
154 int     RSA_public_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
155 int     RSA_private_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
156
157 int RSA_sign(int, const unsigned char *, unsigned int,
158              unsigned char *, unsigned int *, RSA *);
159 int RSA_verify(int, const unsigned char *, unsigned int,
160                unsigned char *, unsigned int, RSA *);
161
162 RSA *   d2i_RSAPrivateKey(RSA *, const unsigned char **, size_t);
163
164 #endif /* _HEIM_RSA_H */