c8f8f80f800f9b22d9d59cbacb6859a995b0f920
[metze/samba/wip.git] / source / heimdal / lib / hcrypto / evp.h
1 /*
2  * Copyright (c) 2005 - 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 /* $Id: evp.h 23141 2008-04-29 05:47:04Z lha $ */
35
36 #ifndef HEIM_EVP_H
37 #define HEIM_EVP_H 1
38
39 #include <hcrypto/engine.h>
40
41 /* symbol renaming */
42 #define EVP_CIPHER_CTX_block_size hc_EVP_CIPHER_CTX_block_size
43 #define EVP_CIPHER_CTX_cipher hc_EVP_CIPHER_CTX_cipher
44 #define EVP_CIPHER_CTX_cleanup hc_EVP_CIPHER_CTX_cleanup
45 #define EVP_CIPHER_CTX_flags hc_EVP_CIPHER_CTX_flags
46 #define EVP_CIPHER_CTX_get_app_data hc_EVP_CIPHER_CTX_get_app_data
47 #define EVP_CIPHER_CTX_init hc_EVP_CIPHER_CTX_init
48 #define EVP_CIPHER_CTX_iv_length hc_EVP_CIPHER_CTX_iv_length
49 #define EVP_CIPHER_CTX_key_length hc_EVP_CIPHER_CTX_key_length
50 #define EVP_CIPHER_CTX_mode hc_EVP_CIPHER_CTX_mode
51 #define EVP_CIPHER_CTX_set_app_data hc_EVP_CIPHER_CTX_set_app_data
52 #define EVP_CIPHER_CTX_set_key_length hc_EVP_CIPHER_CTX_set_key_length
53 #define EVP_CIPHER_CTX_set_padding hc_EVP_CIPHER_CTX_set_padding
54 #define EVP_CIPHER_block_size hc_EVP_CIPHER_block_size
55 #define EVP_CIPHER_iv_length hc_EVP_CIPHER_iv_length
56 #define EVP_CIPHER_key_length hc_EVP_CIPHER_key_length
57 #define EVP_Cipher hc_EVP_Cipher
58 #define EVP_CipherInit_ex hc_EVP_CipherInit_ex
59 #define EVP_Digest hc_EVP_Digest
60 #define EVP_DigestFinal_ex hc_EVP_DigestFinal_ex
61 #define EVP_DigestInit_ex hc_EVP_DigestInit_ex
62 #define EVP_DigestUpdate hc_EVP_DigestUpdate
63 #define EVP_MD_CTX_block_size hc_EVP_MD_CTX_block_size
64 #define EVP_MD_CTX_cleanup hc_EVP_MD_CTX_cleanup
65 #define EVP_MD_CTX_create hc_EVP_MD_CTX_create
66 #define EVP_MD_CTX_init hc_EVP_MD_CTX_init
67 #define EVP_MD_CTX_destroy hc_EVP_MD_CTX_destroy
68 #define EVP_MD_CTX_md hc_EVP_MD_CTX_md
69 #define EVP_MD_CTX_size hc_EVP_MD_CTX_size
70 #define EVP_MD_block_size hc_EVP_MD_block_size
71 #define EVP_MD_size hc_EVP_MD_size
72 #define EVP_aes_128_cbc hc_EVP_aes_128_cbc
73 #define EVP_aes_192_cbc hc_EVP_aes_192_cbc
74 #define EVP_aes_256_cbc hc_EVP_aes_256_cbc
75 #define EVP_des_ede3_cbc hc_EVP_des_ede3_cbc
76 #define EVP_enc_null hc_EVP_enc_null
77 #define EVP_md2 hc_EVP_md2
78 #define EVP_md4 hc_EVP_md4
79 #define EVP_md5 hc_EVP_md5
80 #define EVP_md_null hc_EVP_md_null
81 #define EVP_rc2_40_cbc hc_EVP_rc2_40_cbc
82 #define EVP_rc2_64_cbc hc_EVP_rc2_64_cbc
83 #define EVP_rc2_cbc hc_EVP_rc2_cbc
84 #define EVP_rc4 hc_EVP_rc4
85 #define EVP_rc4_40 hc_EVP_rc4_40
86 #define EVP_camellia_128_cbc hc_EVP_camellia_128_cbc
87 #define EVP_camellia_192_cbc hc_EVP_camellia_192_cbc
88 #define EVP_camellia_256_cbc hc_EVP_camellia_256_cbc
89 #define EVP_sha hc_EVP_sha
90 #define EVP_sha1 hc_EVP_sha1
91 #define EVP_sha256 hc_EVP_sha256
92 #define PKCS5_PBKDF2_HMAC_SHA1 hc_PKCS5_PBKDF2_HMAC_SHA1
93 #define EVP_BytesToKey hc_EVP_BytesToKey
94 #define EVP_get_cipherbyname hc_EVP_get_cipherbyname
95 #define OpenSSL_add_all_algorithms hc_OpenSSL_add_all_algorithms
96 #define OpenSSL_add_all_algorithms_conf hc_OpenSSL_add_all_algorithms_conf
97 #define OpenSSL_add_all_algorithms_noconf hc_OpenSSL_add_all_algorithms_noconf
98
99 /*
100  *
101  */
102
103 typedef struct hc_EVP_MD_CTX EVP_MD_CTX;
104 typedef struct hc_evp_pkey EVP_PKEY;
105 typedef struct hc_evp_md EVP_MD;
106 typedef struct hc_CIPHER EVP_CIPHER;
107 typedef struct hc_CIPHER_CTX EVP_CIPHER_CTX;
108
109 #define EVP_MAX_IV_LENGTH       16
110 #define EVP_MAX_BLOCK_LENGTH    32
111
112 #define EVP_MAX_MD_SIZE         64
113
114 struct hc_CIPHER {
115     int nid;
116     int block_size;
117     int key_len;
118     int iv_len;
119     unsigned long flags;
120     /* The lowest 3 bits is used as integer field for the mode the
121      * cipher is used in (use EVP_CIPHER.._mode() to extract the
122      * mode). The rest of the flag field is a bitfield.
123      */
124 #define EVP_CIPH_CBC_MODE               2
125 #define EVP_CIPH_MODE                   0x7
126
127 #define EVP_CIPH_ALWAYS_CALL_INIT       0x20
128
129     int (*init)(EVP_CIPHER_CTX*,const unsigned char*,const unsigned char*,int);
130     int (*do_cipher)(EVP_CIPHER_CTX *, unsigned char *,
131                      const unsigned char *, unsigned int);
132     int (*cleanup)(EVP_CIPHER_CTX *);
133     int ctx_size;
134     void *set_asn1_parameters;
135     void *get_asn1_parameters;
136     void *ctrl;
137     void *app_data;
138 };
139
140 struct hc_CIPHER_CTX {
141     const EVP_CIPHER *cipher;
142     ENGINE *engine;
143     int encrypt;
144     int buf_len;
145     unsigned char oiv[EVP_MAX_IV_LENGTH];
146     unsigned char iv[EVP_MAX_IV_LENGTH];
147     unsigned char buf[EVP_MAX_BLOCK_LENGTH];
148     int num;
149     void *app_data;
150     int key_len;
151     unsigned long flags;
152     void *cipher_data;
153     int final_used;
154     int block_mask;
155     unsigned char final[EVP_MAX_BLOCK_LENGTH];
156 };
157
158 #if !defined(__GNUC__) && !defined(__attribute__)
159 #define __attribute__(x)
160 #endif
161
162 #ifndef HC_DEPRECATED
163 #define HC_DEPRECATED __attribute__((deprecated))
164 #endif
165
166 #ifdef  __cplusplus
167 extern "C" {
168 #endif
169
170 /*
171  * Avaible crypto algs
172  */
173
174 const EVP_MD *EVP_md_null(void);
175 const EVP_MD *EVP_md2(void);
176 const EVP_MD *EVP_md4(void);
177 const EVP_MD *EVP_md5(void);
178 const EVP_MD *EVP_sha(void);
179 const EVP_MD *EVP_sha1(void);
180 const EVP_MD *EVP_sha256(void);
181
182 const EVP_CIPHER * EVP_aes_128_cbc(void);
183 const EVP_CIPHER * EVP_aes_192_cbc(void);
184 const EVP_CIPHER * EVP_aes_256_cbc(void);
185 const EVP_CIPHER * EVP_des_ede3_cbc(void);
186 const EVP_CIPHER * EVP_enc_null(void);
187 const EVP_CIPHER * EVP_rc2_40_cbc(void);
188 const EVP_CIPHER * EVP_rc2_64_cbc(void);
189 const EVP_CIPHER * EVP_rc2_cbc(void);
190 const EVP_CIPHER * EVP_rc4(void);
191 const EVP_CIPHER * EVP_rc4_40(void);
192 const EVP_CIPHER * EVP_camellia_128_cbc(void);
193 const EVP_CIPHER * EVP_camellia_192_cbc(void);
194 const EVP_CIPHER * EVP_camellia_256_cbc(void);
195
196 /*
197  *
198  */
199
200 size_t  EVP_MD_size(const EVP_MD *);
201 size_t  EVP_MD_block_size(const EVP_MD *);
202
203 const EVP_MD *
204         EVP_MD_CTX_md(EVP_MD_CTX *);
205 size_t  EVP_MD_CTX_size(EVP_MD_CTX *);
206 size_t  EVP_MD_CTX_block_size(EVP_MD_CTX *);
207
208 EVP_MD_CTX *
209         EVP_MD_CTX_create(void);
210 void    HC_DEPRECATED EVP_MD_CTX_init(EVP_MD_CTX *);
211 void    EVP_MD_CTX_destroy(EVP_MD_CTX *);
212 int     HC_DEPRECATED EVP_MD_CTX_cleanup(EVP_MD_CTX *);
213
214 int     EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *);
215 int     EVP_DigestUpdate(EVP_MD_CTX *,const void *, size_t);
216 int     EVP_DigestFinal_ex(EVP_MD_CTX *, void *, unsigned int *);
217 int     EVP_Digest(const void *, size_t, void *, unsigned int *, 
218                    const EVP_MD *, ENGINE *);
219 /*
220  *
221  */
222
223 const EVP_CIPHER *
224         EVP_get_cipherbyname(const char *);
225
226 size_t  EVP_CIPHER_block_size(const EVP_CIPHER *);
227 size_t  EVP_CIPHER_key_length(const EVP_CIPHER *);
228 size_t  EVP_CIPHER_iv_length(const EVP_CIPHER *);
229
230 void    EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *);
231 int     EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
232 int     EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *, int);
233 int     EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
234 unsigned long
235         EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *);
236 int     EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *);
237
238 const EVP_CIPHER *
239         EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *);
240 size_t  EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *);
241 size_t  EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *);
242 size_t  EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *);
243 void *  EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *);
244 void    EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *, void *);
245
246 int     EVP_CipherInit_ex(EVP_CIPHER_CTX *,const EVP_CIPHER *, ENGINE *,
247                           const void *, const void *, int);
248
249 int     EVP_Cipher(EVP_CIPHER_CTX *,void *,const void *,size_t);
250
251 int     PKCS5_PBKDF2_HMAC_SHA1(const void *, size_t, const void *, size_t,
252                                unsigned long, size_t, void *);
253
254 int     EVP_BytesToKey(const EVP_CIPHER *, const EVP_MD *, 
255                        const void *, const void *, size_t,
256                        unsigned int, void *, void *);
257
258
259 /*
260  *
261  */
262
263 void    OpenSSL_add_all_algorithms(void);
264 void    OpenSSL_add_all_algorithms_conf(void);
265 void    OpenSSL_add_all_algorithms_noconf(void);
266
267 #ifdef  __cplusplus
268 }
269 #endif
270
271 #endif /* HEIM_EVP_H */