xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hcrypto/evp.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: evp.h,v 1.1.1.3 2014/04/24 12:45:30 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 2005 - 2008 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /* Id */
37 
38 #ifndef HEIM_EVP_H
39 #define HEIM_EVP_H 1
40 
41 #include <hcrypto/engine.h>
42 
43 /* symbol renaming */
44 #define EVP_CIPHER_CTX_block_size hc_EVP_CIPHER_CTX_block_size
45 #define EVP_CIPHER_CTX_cipher hc_EVP_CIPHER_CTX_cipher
46 #define EVP_CIPHER_CTX_cleanup hc_EVP_CIPHER_CTX_cleanup
47 #define EVP_CIPHER_CTX_flags hc_EVP_CIPHER_CTX_flags
48 #define EVP_CIPHER_CTX_get_app_data hc_EVP_CIPHER_CTX_get_app_data
49 #define EVP_CIPHER_CTX_init hc_EVP_CIPHER_CTX_init
50 #define EVP_CIPHER_CTX_iv_length hc_EVP_CIPHER_CTX_iv_length
51 #define EVP_CIPHER_CTX_key_length hc_EVP_CIPHER_CTX_key_length
52 #define EVP_CIPHER_CTX_mode hc_EVP_CIPHER_CTX_mode
53 #define EVP_CIPHER_CTX_set_app_data hc_EVP_CIPHER_CTX_set_app_data
54 #define EVP_CIPHER_CTX_set_key_length hc_EVP_CIPHER_CTX_set_key_length
55 #define EVP_CIPHER_CTX_set_padding hc_EVP_CIPHER_CTX_set_padding
56 #define EVP_CIPHER_block_size hc_EVP_CIPHER_block_size
57 #define EVP_CIPHER_iv_length hc_EVP_CIPHER_iv_length
58 #define EVP_CIPHER_key_length hc_EVP_CIPHER_key_length
59 #define EVP_Cipher hc_EVP_Cipher
60 #define EVP_CipherInit_ex hc_EVP_CipherInit_ex
61 #define EVP_CipherUpdate hc_EVP_CipherUpdate
62 #define EVP_CipherFinal_ex hc_EVP_CipherFinal_ex
63 #define EVP_Digest hc_EVP_Digest
64 #define EVP_DigestFinal_ex hc_EVP_DigestFinal_ex
65 #define EVP_DigestInit_ex hc_EVP_DigestInit_ex
66 #define EVP_DigestUpdate hc_EVP_DigestUpdate
67 #define EVP_MD_CTX_block_size hc_EVP_MD_CTX_block_size
68 #define EVP_MD_CTX_cleanup hc_EVP_MD_CTX_cleanup
69 #define EVP_MD_CTX_create hc_EVP_MD_CTX_create
70 #define EVP_MD_CTX_init hc_EVP_MD_CTX_init
71 #define EVP_MD_CTX_destroy hc_EVP_MD_CTX_destroy
72 #define EVP_MD_CTX_md hc_EVP_MD_CTX_md
73 #define EVP_MD_CTX_size hc_EVP_MD_CTX_size
74 #define EVP_MD_block_size hc_EVP_MD_block_size
75 #define EVP_MD_size hc_EVP_MD_size
76 #define EVP_aes_128_cbc hc_EVP_aes_128_cbc
77 #define EVP_aes_192_cbc hc_EVP_aes_192_cbc
78 #define EVP_aes_256_cbc hc_EVP_aes_256_cbc
79 #define EVP_aes_128_cfb8 hc_EVP_aes_128_cfb8
80 #define EVP_aes_192_cfb8 hc_EVP_aes_192_cfb8
81 #define EVP_aes_256_cfb8 hc_EVP_aes_256_cfb8
82 
83 #define EVP_des_cbc hc_EVP_des_cbc
84 #define EVP_des_ede3_cbc hc_EVP_des_ede3_cbc
85 #define EVP_enc_null hc_EVP_enc_null
86 #define EVP_md2 hc_EVP_md2
87 #define EVP_md4 hc_EVP_md4
88 #define EVP_md5 hc_EVP_md5
89 #define EVP_md_null hc_EVP_md_null
90 #define EVP_rc2_40_cbc hc_EVP_rc2_40_cbc
91 #define EVP_rc2_64_cbc hc_EVP_rc2_64_cbc
92 #define EVP_rc2_cbc hc_EVP_rc2_cbc
93 #define EVP_rc4 hc_EVP_rc4
94 #define EVP_rc4_40 hc_EVP_rc4_40
95 #define EVP_camellia_128_cbc hc_EVP_camellia_128_cbc
96 #define EVP_camellia_192_cbc hc_EVP_camellia_192_cbc
97 #define EVP_camellia_256_cbc hc_EVP_camellia_256_cbc
98 #define EVP_sha hc_EVP_sha
99 #define EVP_sha1 hc_EVP_sha1
100 #define EVP_sha256 hc_EVP_sha256
101 #define EVP_sha384 hc_EVP_sha384
102 #define EVP_sha512 hc_EVP_sha512
103 #define PKCS5_PBKDF2_HMAC_SHA1 hc_PKCS5_PBKDF2_HMAC_SHA1
104 #define EVP_BytesToKey hc_EVP_BytesToKey
105 #define EVP_get_cipherbyname hc_EVP_get_cipherbyname
106 #define	OpenSSL_add_all_algorithms hc_OpenSSL_add_all_algorithms
107 #define	OpenSSL_add_all_algorithms_conf hc_OpenSSL_add_all_algorithms_conf
108 #define	OpenSSL_add_all_algorithms_noconf hc_OpenSSL_add_all_algorithms_noconf
109 #define EVP_CIPHER_CTX_ctrl hc_EVP_CIPHER_CTX_ctrl
110 #define EVP_CIPHER_CTX_rand_key hc_EVP_CIPHER_CTX_rand_key
111 #define hcrypto_validate hc_hcrypto_validate
112 
113 /*
114  *
115  */
116 
117 typedef struct hc_EVP_MD_CTX EVP_MD_CTX;
118 typedef struct hc_evp_pkey EVP_PKEY;
119 typedef struct hc_evp_md EVP_MD;
120 typedef struct hc_CIPHER EVP_CIPHER;
121 typedef struct hc_CIPHER_CTX EVP_CIPHER_CTX;
122 
123 #define EVP_MAX_IV_LENGTH	16
124 #define EVP_MAX_BLOCK_LENGTH	32
125 
126 #define EVP_MAX_MD_SIZE		64
127 
128 struct hc_CIPHER {
129     int nid;
130     int block_size;
131     int key_len;
132     int iv_len;
133     unsigned long flags;
134     /* The lowest 3 bits is used as integer field for the mode the
135      * cipher is used in (use EVP_CIPHER.._mode() to extract the
136      * mode). The rest of the flag field is a bitfield.
137      */
138 #define EVP_CIPH_STREAM_CIPHER		0
139 #define EVP_CIPH_CBC_MODE		2
140 #define EVP_CIPH_CFB8_MODE              4
141 #define EVP_CIPH_MODE			0x7
142 
143 #define EVP_CIPH_VARIABLE_LENGTH	0x008 /* variable key length */
144 #define EVP_CIPH_ALWAYS_CALL_INIT	0x020
145 #define EVP_CIPH_RAND_KEY		0x200
146 
147     int (*init)(EVP_CIPHER_CTX*,const unsigned char*,const unsigned char*,int);
148     int (*do_cipher)(EVP_CIPHER_CTX *, unsigned char *,
149 		     const unsigned char *, unsigned int);
150     int (*cleanup)(EVP_CIPHER_CTX *);
151     int ctx_size;
152     void *set_asn1_parameters;
153     void *get_asn1_parameters;
154     int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
155 #define EVP_CTRL_RAND_KEY		0x6
156 
157     void *app_data;
158 };
159 
160 struct hc_CIPHER_CTX {
161     const EVP_CIPHER *cipher;
162     ENGINE *engine;
163     int encrypt;
164     int buf_len; /* bytes stored in buf for EVP_CipherUpdate */
165     unsigned char oiv[EVP_MAX_IV_LENGTH];
166     unsigned char iv[EVP_MAX_IV_LENGTH];
167     unsigned char buf[EVP_MAX_BLOCK_LENGTH];
168     int num;
169     void *app_data;
170     int key_len;
171     unsigned long flags;
172     void *cipher_data;
173     int final_used;
174     int block_mask;
175     unsigned char final[EVP_MAX_BLOCK_LENGTH];
176 };
177 
178 typedef int (*hc_evp_md_init)(EVP_MD_CTX *);
179 typedef int (*hc_evp_md_update)(EVP_MD_CTX *,const void *, size_t);
180 typedef int (*hc_evp_md_final)(void *, EVP_MD_CTX *);
181 typedef int (*hc_evp_md_cleanup)(EVP_MD_CTX *);
182 
183 struct hc_evp_md {
184     int hash_size;
185     int block_size;
186     int ctx_size;
187     hc_evp_md_init init;
188     hc_evp_md_update update;
189     hc_evp_md_final final;
190     hc_evp_md_cleanup cleanup;
191 };
192 
193 #if !defined(__GNUC__) && !defined(__attribute__)
194 #define __attribute__(x)
195 #endif
196 
197 #ifndef HC_DEPRECATED
198 #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
199 #define HC_DEPRECATED __attribute__((deprecated))
200 #elif defined(_MSC_VER) && (_MSC_VER>1200)
201 #define HC_DEPRECATED __declspec(deprecated)
202 #else
203 #define HC_DEPRECATED
204 #endif
205 #endif
206 
207 #ifndef HC_DEPRECATED_CRYPTO
208 #define HC_DEPRECATED_CRYPTO HC_DEPRECATED
209 #endif
210 
211 #ifdef __cplusplus
212 #define HC_CPP_BEGIN extern "C" {
213 #define HC_CPP_END }
214 #else
215 #define HC_CPP_BEGIN
216 #define HC_CPP_END
217 #endif
218 
219 HC_CPP_BEGIN
220 
221 /*
222  * Avaible crypto algs
223  */
224 
225 const EVP_MD *EVP_md_null(void);
226 HC_DEPRECATED_CRYPTO const EVP_MD *EVP_md2(void);
227 HC_DEPRECATED_CRYPTO const EVP_MD *EVP_md4(void);
228 HC_DEPRECATED_CRYPTO const EVP_MD *EVP_md5(void);
229 const EVP_MD *EVP_sha(void);
230 const EVP_MD *EVP_sha1(void);
231 const EVP_MD *EVP_sha256(void);
232 const EVP_MD *EVP_sha384(void);
233 const EVP_MD *EVP_sha512(void);
234 
235 const EVP_CIPHER * EVP_aes_128_cbc(void);
236 const EVP_CIPHER * EVP_aes_192_cbc(void);
237 const EVP_CIPHER * EVP_aes_256_cbc(void);
238 const EVP_CIPHER * EVP_aes_128_cfb8(void);
239 const EVP_CIPHER * EVP_aes_192_cfb8(void);
240 const EVP_CIPHER * EVP_aes_256_cfb8(void);
241 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_des_cbc(void);
242 const EVP_CIPHER * EVP_des_ede3_cbc(void);
243 const EVP_CIPHER * EVP_enc_null(void);
244 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_rc2_40_cbc(void);
245 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_rc2_64_cbc(void);
246 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_rc2_cbc(void);
247 const EVP_CIPHER * EVP_rc4(void);
248 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_rc4_40(void);
249 const EVP_CIPHER * EVP_camellia_128_cbc(void);
250 const EVP_CIPHER * EVP_camellia_192_cbc(void);
251 const EVP_CIPHER * EVP_camellia_256_cbc(void);
252 
253 size_t	EVP_MD_size(const EVP_MD *);
254 size_t	EVP_MD_block_size(const EVP_MD *);
255 
256 const EVP_MD *
257 	EVP_MD_CTX_md(EVP_MD_CTX *);
258 size_t	EVP_MD_CTX_size(EVP_MD_CTX *);
259 size_t	EVP_MD_CTX_block_size(EVP_MD_CTX *);
260 
261 EVP_MD_CTX *
262 	EVP_MD_CTX_create(void);
263 void	HC_DEPRECATED EVP_MD_CTX_init(EVP_MD_CTX *);
264 void	EVP_MD_CTX_destroy(EVP_MD_CTX *);
265 int	HC_DEPRECATED EVP_MD_CTX_cleanup(EVP_MD_CTX *);
266 
267 int	EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *);
268 int	EVP_DigestUpdate(EVP_MD_CTX *,const void *, size_t);
269 int	EVP_DigestFinal_ex(EVP_MD_CTX *, void *, unsigned int *);
270 int	EVP_Digest(const void *, size_t, void *, unsigned int *,
271 		   const EVP_MD *, ENGINE *);
272 /*
273  *
274  */
275 
276 const EVP_CIPHER *
277 	EVP_get_cipherbyname(const char *);
278 
279 size_t	EVP_CIPHER_block_size(const EVP_CIPHER *);
280 size_t	EVP_CIPHER_key_length(const EVP_CIPHER *);
281 size_t	EVP_CIPHER_iv_length(const EVP_CIPHER *);
282 
283 void	EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *);
284 int	EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
285 int	EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *, int);
286 int	EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
287 unsigned long
288 	EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *);
289 int	EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *);
290 
291 const EVP_CIPHER *
292 	EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *);
293 size_t	EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *);
294 size_t	EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *);
295 size_t	EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *);
296 void *	EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *);
297 void	EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *, void *);
298 
299 int	EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *, int, int, void *);
300 int	EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *, void *);
301 
302 
303 int	EVP_CipherInit_ex(EVP_CIPHER_CTX *,const EVP_CIPHER *, ENGINE *,
304 			  const void *, const void *, int);
305 int	EVP_CipherUpdate(EVP_CIPHER_CTX *, void *, int *, void *, size_t);
306 int	EVP_CipherFinal_ex(EVP_CIPHER_CTX *, void *, int *);
307 
308 int	EVP_Cipher(EVP_CIPHER_CTX *,void *,const void *,size_t);
309 
310 int	PKCS5_PBKDF2_HMAC_SHA1(const void *, size_t, const void *, size_t,
311 			       unsigned long, size_t, void *);
312 
313 int	EVP_BytesToKey(const EVP_CIPHER *, const EVP_MD *,
314 		       const void *, const void *, size_t,
315 		       unsigned int, void *, void *);
316 
317 
318 /*
319  *
320  */
321 
322 void	OpenSSL_add_all_algorithms(void);
323 void	OpenSSL_add_all_algorithms_conf(void);
324 void	OpenSSL_add_all_algorithms_noconf(void);
325 
326 void
327 hcrypto_validate(void);
328 
329 HC_CPP_END
330 
331 #endif /* HEIM_EVP_H */
332