roken: getuserinfo WIN32 fix username string termination
[heimdal.git] / lib / hcrypto / evp.h
blob8893362db23e7516b8d69ea02a50a630a676fdd4
1 /*
2 * Copyright (c) 2005 - 2016 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 /* $Id$ */
36 #ifndef HEIM_EVP_H
37 #define HEIM_EVP_H 1
39 #include <hcrypto/engine.h>
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_CipherUpdate hc_EVP_CipherUpdate
60 #define EVP_CipherFinal_ex hc_EVP_CipherFinal_ex
61 #define EVP_Digest hc_EVP_Digest
62 #define EVP_DigestFinal_ex hc_EVP_DigestFinal_ex
63 #define EVP_DigestInit_ex hc_EVP_DigestInit_ex
64 #define EVP_DigestUpdate hc_EVP_DigestUpdate
65 #define EVP_MD_CTX_block_size hc_EVP_MD_CTX_block_size
66 #define EVP_MD_CTX_cleanup hc_EVP_MD_CTX_cleanup
67 #define EVP_MD_CTX_create hc_EVP_MD_CTX_create
68 #define EVP_MD_CTX_init hc_EVP_MD_CTX_init
69 #define EVP_MD_CTX_destroy hc_EVP_MD_CTX_destroy
70 #define EVP_MD_CTX_md hc_EVP_MD_CTX_md
71 #define EVP_MD_CTX_size hc_EVP_MD_CTX_size
72 #define EVP_MD_block_size hc_EVP_MD_block_size
73 #define EVP_MD_size hc_EVP_MD_size
74 #define EVP_aes_128_cbc hc_EVP_aes_128_cbc
75 #define EVP_aes_192_cbc hc_EVP_aes_192_cbc
76 #define EVP_aes_256_cbc hc_EVP_aes_256_cbc
77 #define EVP_aes_128_cfb8 hc_EVP_aes_128_cfb8
78 #define EVP_aes_192_cfb8 hc_EVP_aes_192_cfb8
79 #define EVP_aes_256_cfb8 hc_EVP_aes_256_cfb8
81 #define EVP_des_cbc hc_EVP_des_cbc
82 #define EVP_des_ede3_cbc hc_EVP_des_ede3_cbc
83 #define EVP_enc_null hc_EVP_enc_null
84 #define EVP_md2 hc_EVP_md2
85 #define EVP_md4 hc_EVP_md4
86 #define EVP_md5 hc_EVP_md5
87 #define EVP_md_null hc_EVP_md_null
88 #define EVP_rc2_40_cbc hc_EVP_rc2_40_cbc
89 #define EVP_rc2_64_cbc hc_EVP_rc2_64_cbc
90 #define EVP_rc2_cbc hc_EVP_rc2_cbc
91 #define EVP_rc4 hc_EVP_rc4
92 #define EVP_rc4_40 hc_EVP_rc4_40
93 #define EVP_camellia_128_cbc hc_EVP_camellia_128_cbc
94 #define EVP_camellia_192_cbc hc_EVP_camellia_192_cbc
95 #define EVP_camellia_256_cbc hc_EVP_camellia_256_cbc
96 #define EVP_sha hc_EVP_sha
97 #define EVP_sha1 hc_EVP_sha1
98 #define EVP_sha256 hc_EVP_sha256
99 #define EVP_sha384 hc_EVP_sha384
100 #define EVP_sha512 hc_EVP_sha512
101 #define PKCS5_PBKDF2_HMAC hc_PKCS5_PBKDF2_HMAC
102 #define PKCS5_PBKDF2_HMAC_SHA1 hc_PKCS5_PBKDF2_HMAC_SHA1
103 #define EVP_BytesToKey hc_EVP_BytesToKey
104 #define EVP_get_cipherbyname hc_EVP_get_cipherbyname
105 #define OpenSSL_add_all_algorithms hc_OpenSSL_add_all_algorithms
106 #define OpenSSL_add_all_algorithms_conf hc_OpenSSL_add_all_algorithms_conf
107 #define OpenSSL_add_all_algorithms_noconf hc_OpenSSL_add_all_algorithms_noconf
108 #define EVP_CIPHER_CTX_ctrl hc_EVP_CIPHER_CTX_ctrl
109 #define EVP_CIPHER_CTX_rand_key hc_EVP_CIPHER_CTX_rand_key
110 #define hcrypto_validate hc_hcrypto_validate
112 /* Type name renaming */
113 #define EVP_MD_CTX hc_EVP_MD_CTX
114 #define EVP_PKEY hc_EVP_PKEY
115 #define EVP_MD hc_EVP_MD
116 #define EVP_CIPHER hc_EVP_CIPHER
117 #define EVP_CIPHER_CTX hc_EVP_CIPHER_CTX
119 /* Constant renaming */
120 #define EVP_CIPH_STREAM_CIPHER hc_EVP_CIPH_STREAM_CIPHER
121 #define EVP_CIPH_CBC_MODE hc_EVP_CIPH_CBC_MODE
122 #define EVP_CIPH_CFB8_MODE hc_EVP_CIPH_CFB8_MODE
123 #define EVP_CIPH_MODE hc_EVP_CIPH_MODE
124 #define EVP_CIPH_CTRL_INIT hc_EVP_CIPH_CTRL_INIT
126 #define EVP_CIPH_VARIABLE_LENGTH hc_EVP_CIPH_VARIABLE_LENGTH
127 #define EVP_CIPH_ALWAYS_CALL_INIT hc_EVP_CIPH_ALWAYS_CALL_INIT
128 #define EVP_CIPH_RAND_KEY hc_EVP_CIPH_RAND_KEY
130 #define EVP_CTRL_INIT hc_EVP_CTRL_INIT
137 typedef struct hc_EVP_MD_CTX EVP_MD_CTX;
138 typedef struct hc_evp_pkey EVP_PKEY;
139 typedef struct hc_evp_md EVP_MD;
140 typedef struct hc_CIPHER EVP_CIPHER;
141 typedef struct hc_CIPHER_CTX EVP_CIPHER_CTX;
143 #define EVP_MAX_IV_LENGTH 16
144 #define EVP_MAX_BLOCK_LENGTH 32
146 #define EVP_MAX_MD_SIZE 64
148 struct hc_CIPHER {
149 int nid;
150 int block_size;
151 int key_len;
152 int iv_len;
153 unsigned long flags;
154 /* The lowest 3 bits is used as integer field for the mode the
155 * cipher is used in (use EVP_CIPHER.._mode() to extract the
156 * mode). The rest of the flag field is a bitfield.
158 #define hc_EVP_CIPH_STREAM_CIPHER 0
159 #define hc_EVP_CIPH_CBC_MODE 2
160 #define hc_EVP_CIPH_CFB8_MODE 4
161 #define hc_EVP_CIPH_MODE 0x7
162 #define hc_EVP_CIPH_CTRL_INIT 0x40
164 #define hc_EVP_CTRL_INIT 0x0
166 #define hc_EVP_CIPH_VARIABLE_LENGTH 0x008 /* variable key length */
167 #define hc_EVP_CIPH_ALWAYS_CALL_INIT 0x020 /* backend maintains own cipherstate */
168 #define hc_EVP_CIPH_RAND_KEY 0x200
170 int (*init)(EVP_CIPHER_CTX*,const unsigned char*,const unsigned char*,int);
171 int (*do_cipher)(EVP_CIPHER_CTX *, unsigned char *,
172 const unsigned char *, unsigned int);
173 int (*cleanup)(EVP_CIPHER_CTX *);
174 int ctx_size;
175 int (*set_asn1_parameters)(void);
176 int (*get_asn1_parameters)(void);
177 int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
178 #define EVP_CTRL_RAND_KEY 0x6
180 void *app_data;
183 struct hc_CIPHER_CTX {
184 const EVP_CIPHER *cipher;
185 ENGINE *engine;
186 int encrypt;
187 int buf_len; /* bytes stored in buf for EVP_CipherUpdate */
188 unsigned char oiv[EVP_MAX_IV_LENGTH];
189 unsigned char iv[EVP_MAX_IV_LENGTH];
190 unsigned char buf[EVP_MAX_BLOCK_LENGTH];
191 int num;
192 void *app_data;
193 int key_len;
194 unsigned long flags;
195 void *cipher_data;
196 int final_used;
197 int block_mask;
198 unsigned char final[EVP_MAX_BLOCK_LENGTH];
202 * LIES. It's not an EVP_MD_CTX that gets passed to these functions
203 * here in hcrypto, but an object of ctx_size.
205 typedef int (*hc_evp_md_init)(EVP_MD_CTX *);
206 typedef int (*hc_evp_md_update)(EVP_MD_CTX *,const void *, size_t);
207 typedef int (*hc_evp_md_final)(void *, EVP_MD_CTX *);
208 typedef int (*hc_evp_md_cleanup)(EVP_MD_CTX *);
210 struct hc_evp_md {
211 int hash_size;
212 int block_size;
213 int ctx_size;
214 hc_evp_md_init init;
215 hc_evp_md_update update;
216 hc_evp_md_final final;
217 hc_evp_md_cleanup cleanup;
220 #if !defined(__GNUC__) && !defined(__attribute__)
221 #define __attribute__(x)
222 #endif
224 #ifndef HC_DEPRECATED
225 #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
226 #define HC_DEPRECATED __attribute__((deprecated))
227 #elif defined(_MSC_VER) && (_MSC_VER>1200)
228 #define HC_DEPRECATED __declspec(deprecated)
229 #else
230 #define HC_DEPRECATED
231 #endif
232 #endif
234 #ifndef HC_DEPRECATED_CRYPTO
235 #define HC_DEPRECATED_CRYPTO HC_DEPRECATED
236 #endif
238 #ifdef __cplusplus
239 #define HC_CPP_BEGIN extern "C" {
240 #define HC_CPP_END }
241 #else
242 #define HC_CPP_BEGIN
243 #define HC_CPP_END
244 #endif
246 HC_CPP_BEGIN
249 * Avaible crypto algs
252 const EVP_MD *EVP_md_null(void);
253 HC_DEPRECATED_CRYPTO const EVP_MD *EVP_md2(void);
254 HC_DEPRECATED_CRYPTO const EVP_MD *EVP_md4(void);
255 HC_DEPRECATED_CRYPTO const EVP_MD *EVP_md5(void);
256 const EVP_MD *EVP_sha(void);
257 const EVP_MD *EVP_sha1(void);
258 const EVP_MD *EVP_sha256(void);
259 const EVP_MD *EVP_sha384(void);
260 const EVP_MD *EVP_sha512(void);
262 const EVP_CIPHER * EVP_aes_128_cbc(void);
263 const EVP_CIPHER * EVP_aes_192_cbc(void);
264 const EVP_CIPHER * EVP_aes_256_cbc(void);
265 const EVP_CIPHER * EVP_aes_128_cfb8(void);
266 const EVP_CIPHER * EVP_aes_192_cfb8(void);
267 const EVP_CIPHER * EVP_aes_256_cfb8(void);
268 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_des_cbc(void);
269 const EVP_CIPHER * EVP_des_ede3_cbc(void);
270 const EVP_CIPHER * EVP_enc_null(void);
271 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_rc2_40_cbc(void);
272 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_rc2_64_cbc(void);
273 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_rc2_cbc(void);
274 const EVP_CIPHER * EVP_rc4(void);
275 HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_rc4_40(void);
276 const EVP_CIPHER * EVP_camellia_128_cbc(void);
277 const EVP_CIPHER * EVP_camellia_192_cbc(void);
278 const EVP_CIPHER * EVP_camellia_256_cbc(void);
280 size_t EVP_MD_size(const EVP_MD *);
281 size_t EVP_MD_block_size(const EVP_MD *);
283 const EVP_MD *
284 EVP_MD_CTX_md(EVP_MD_CTX *);
285 size_t EVP_MD_CTX_size(EVP_MD_CTX *);
286 size_t EVP_MD_CTX_block_size(EVP_MD_CTX *);
288 EVP_MD_CTX *
289 EVP_MD_CTX_create(void);
290 void HC_DEPRECATED EVP_MD_CTX_init(EVP_MD_CTX *);
291 void EVP_MD_CTX_destroy(EVP_MD_CTX *);
292 int HC_DEPRECATED EVP_MD_CTX_cleanup(EVP_MD_CTX *);
294 int EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *);
295 int EVP_DigestUpdate(EVP_MD_CTX *,const void *, size_t);
296 int EVP_DigestFinal_ex(EVP_MD_CTX *, void *, unsigned int *);
297 int EVP_Digest(const void *, size_t, void *, unsigned int *,
298 const EVP_MD *, ENGINE *);
303 const EVP_CIPHER *
304 EVP_get_cipherbyname(const char *);
306 size_t EVP_CIPHER_block_size(const EVP_CIPHER *);
307 size_t EVP_CIPHER_key_length(const EVP_CIPHER *);
308 size_t EVP_CIPHER_iv_length(const EVP_CIPHER *);
310 void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *);
311 int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
312 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *, int);
313 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
314 unsigned long
315 EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *);
316 int EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *);
318 const EVP_CIPHER *
319 EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *);
320 size_t EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *);
321 size_t EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *);
322 size_t EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *);
323 void * EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *);
324 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *, void *);
326 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *, int, int, void *);
327 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *, void *);
330 int EVP_CipherInit_ex(EVP_CIPHER_CTX *,const EVP_CIPHER *, ENGINE *,
331 const void *, const void *, int);
332 int EVP_CipherUpdate(EVP_CIPHER_CTX *, void *, int *, void *, size_t);
333 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *, void *, int *);
335 int EVP_Cipher(EVP_CIPHER_CTX *,void *,const void *,size_t);
337 int PKCS5_PBKDF2_HMAC(const void *, size_t, const void *, size_t,
338 unsigned long, const EVP_MD *, size_t, void *);
340 int PKCS5_PBKDF2_HMAC_SHA1(const void *, size_t, const void *, size_t,
341 unsigned long, size_t, void *);
343 int EVP_BytesToKey(const EVP_CIPHER *, const EVP_MD *,
344 const void *, const void *, size_t,
345 unsigned int, void *, void *);
352 void OpenSSL_add_all_algorithms(void);
353 void OpenSSL_add_all_algorithms_conf(void);
354 void OpenSSL_add_all_algorithms_noconf(void);
356 void
357 hcrypto_validate(void);
359 HC_CPP_END
361 #endif /* HEIM_EVP_H */