xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hcrypto/evp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: evp.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #ifdef HAVE_CONFIG_H
37ebfedea0SLionel Sambuc #include <config.h>
38ebfedea0SLionel Sambuc #endif
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc #define HC_DEPRECATED
41ebfedea0SLionel Sambuc #define HC_DEPRECATED_CRYPTO
42ebfedea0SLionel Sambuc 
43ebfedea0SLionel Sambuc #include <sys/types.h>
44ebfedea0SLionel Sambuc #include <stdio.h>
45ebfedea0SLionel Sambuc #include <stdlib.h>
46ebfedea0SLionel Sambuc #include <string.h>
47ebfedea0SLionel Sambuc #include <assert.h>
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc #include <evp.h>
50ebfedea0SLionel Sambuc #include <evp-hcrypto.h>
51ebfedea0SLionel Sambuc #include <evp-cc.h>
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc #include <krb5/krb5-types.h>
54ebfedea0SLionel Sambuc #include <krb5/roken.h>
55ebfedea0SLionel Sambuc 
56ebfedea0SLionel Sambuc #ifndef HCRYPTO_DEF_PROVIDER
57ebfedea0SLionel Sambuc #define HCRYPTO_DEF_PROVIDER hcrypto
58ebfedea0SLionel Sambuc #endif
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc #define HC_CONCAT4(x,y,z,aa)	x ## y ## z ## aa
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc #define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc /**
66ebfedea0SLionel Sambuc  * @page page_evp EVP - generic crypto interface
67ebfedea0SLionel Sambuc  *
68ebfedea0SLionel Sambuc  * See the library functions here: @ref hcrypto_evp
69ebfedea0SLionel Sambuc  *
70ebfedea0SLionel Sambuc  * @section evp_cipher EVP Cipher
71ebfedea0SLionel Sambuc  *
72ebfedea0SLionel Sambuc  * The use of EVP_CipherInit_ex() and EVP_Cipher() is pretty easy to
73ebfedea0SLionel Sambuc  * understand forward, then EVP_CipherUpdate() and
74ebfedea0SLionel Sambuc  * EVP_CipherFinal_ex() really needs an example to explain @ref
75ebfedea0SLionel Sambuc  * example_evp_cipher.c .
76ebfedea0SLionel Sambuc  *
77ebfedea0SLionel Sambuc  * @example example_evp_cipher.c
78ebfedea0SLionel Sambuc  *
79ebfedea0SLionel Sambuc  * This is an example how to use EVP_CipherInit_ex(),
80ebfedea0SLionel Sambuc  * EVP_CipherUpdate() and EVP_CipherFinal_ex().
81ebfedea0SLionel Sambuc  */
82ebfedea0SLionel Sambuc 
83ebfedea0SLionel Sambuc struct hc_EVP_MD_CTX {
84ebfedea0SLionel Sambuc     const EVP_MD *md;
85ebfedea0SLionel Sambuc     ENGINE *engine;
86ebfedea0SLionel Sambuc     void *ptr;
87ebfedea0SLionel Sambuc };
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc /**
91ebfedea0SLionel Sambuc  * Return the output size of the message digest function.
92ebfedea0SLionel Sambuc  *
93ebfedea0SLionel Sambuc  * @param md the evp message
94ebfedea0SLionel Sambuc  *
95ebfedea0SLionel Sambuc  * @return size output size of the message digest function.
96ebfedea0SLionel Sambuc  *
97ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
98ebfedea0SLionel Sambuc  */
99ebfedea0SLionel Sambuc 
100ebfedea0SLionel Sambuc size_t
EVP_MD_size(const EVP_MD * md)101ebfedea0SLionel Sambuc EVP_MD_size(const EVP_MD *md)
102ebfedea0SLionel Sambuc {
103ebfedea0SLionel Sambuc     return md->hash_size;
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc 
106ebfedea0SLionel Sambuc /**
107ebfedea0SLionel Sambuc  * Return the blocksize of the message digest function.
108ebfedea0SLionel Sambuc  *
109ebfedea0SLionel Sambuc  * @param md the evp message
110ebfedea0SLionel Sambuc  *
111ebfedea0SLionel Sambuc  * @return size size of the message digest block size
112ebfedea0SLionel Sambuc  *
113ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
114ebfedea0SLionel Sambuc  */
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc size_t
EVP_MD_block_size(const EVP_MD * md)117ebfedea0SLionel Sambuc EVP_MD_block_size(const EVP_MD *md)
118ebfedea0SLionel Sambuc {
119ebfedea0SLionel Sambuc     return md->block_size;
120ebfedea0SLionel Sambuc }
121ebfedea0SLionel Sambuc 
122ebfedea0SLionel Sambuc /**
123ebfedea0SLionel Sambuc  * Allocate a messsage digest context object. Free with
124ebfedea0SLionel Sambuc  * EVP_MD_CTX_destroy().
125ebfedea0SLionel Sambuc  *
126ebfedea0SLionel Sambuc  * @return a newly allocated message digest context object.
127ebfedea0SLionel Sambuc  *
128ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
129ebfedea0SLionel Sambuc  */
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc EVP_MD_CTX *
EVP_MD_CTX_create(void)132ebfedea0SLionel Sambuc EVP_MD_CTX_create(void)
133ebfedea0SLionel Sambuc {
134ebfedea0SLionel Sambuc     return calloc(1, sizeof(EVP_MD_CTX));
135ebfedea0SLionel Sambuc }
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc /**
138ebfedea0SLionel Sambuc  * Initiate a messsage digest context object. Deallocate with
139ebfedea0SLionel Sambuc  * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
140ebfedea0SLionel Sambuc  *
141ebfedea0SLionel Sambuc  * @param ctx variable to initiate.
142ebfedea0SLionel Sambuc  *
143ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
144ebfedea0SLionel Sambuc  */
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc void
EVP_MD_CTX_init(EVP_MD_CTX * ctx)147ebfedea0SLionel Sambuc EVP_MD_CTX_init(EVP_MD_CTX *ctx) HC_DEPRECATED
148ebfedea0SLionel Sambuc {
149ebfedea0SLionel Sambuc     memset(ctx, 0, sizeof(*ctx));
150ebfedea0SLionel Sambuc }
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc /**
153ebfedea0SLionel Sambuc  * Free a messsage digest context object.
154ebfedea0SLionel Sambuc  *
155ebfedea0SLionel Sambuc  * @param ctx context to free.
156ebfedea0SLionel Sambuc  *
157ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
158ebfedea0SLionel Sambuc  */
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc void
EVP_MD_CTX_destroy(EVP_MD_CTX * ctx)161ebfedea0SLionel Sambuc EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
162ebfedea0SLionel Sambuc {
163ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(ctx);
164ebfedea0SLionel Sambuc     free(ctx);
165ebfedea0SLionel Sambuc }
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc /**
168ebfedea0SLionel Sambuc  * Free the resources used by the EVP_MD context.
169ebfedea0SLionel Sambuc  *
170ebfedea0SLionel Sambuc  * @param ctx the context to free the resources from.
171ebfedea0SLionel Sambuc  *
172ebfedea0SLionel Sambuc  * @return 1 on success.
173ebfedea0SLionel Sambuc  *
174ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
175ebfedea0SLionel Sambuc  */
176ebfedea0SLionel Sambuc 
177ebfedea0SLionel Sambuc int
EVP_MD_CTX_cleanup(EVP_MD_CTX * ctx)178ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) HC_DEPRECATED
179ebfedea0SLionel Sambuc {
180ebfedea0SLionel Sambuc     if (ctx->md && ctx->md->cleanup)
181ebfedea0SLionel Sambuc 	(ctx->md->cleanup)(ctx);
182ebfedea0SLionel Sambuc     else if (ctx->md)
183ebfedea0SLionel Sambuc 	memset(ctx->ptr, 0, ctx->md->ctx_size);
184ebfedea0SLionel Sambuc     ctx->md = NULL;
185ebfedea0SLionel Sambuc     ctx->engine = NULL;
186ebfedea0SLionel Sambuc     free(ctx->ptr);
187ebfedea0SLionel Sambuc     memset(ctx, 0, sizeof(*ctx));
188ebfedea0SLionel Sambuc     return 1;
189ebfedea0SLionel Sambuc }
190ebfedea0SLionel Sambuc 
191ebfedea0SLionel Sambuc /**
192ebfedea0SLionel Sambuc  * Get the EVP_MD use for a specified context.
193ebfedea0SLionel Sambuc  *
194ebfedea0SLionel Sambuc  * @param ctx the EVP_MD context to get the EVP_MD for.
195ebfedea0SLionel Sambuc  *
196ebfedea0SLionel Sambuc  * @return the EVP_MD used for the context.
197ebfedea0SLionel Sambuc  *
198ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
199ebfedea0SLionel Sambuc  */
200ebfedea0SLionel Sambuc 
201ebfedea0SLionel Sambuc const EVP_MD *
EVP_MD_CTX_md(EVP_MD_CTX * ctx)202ebfedea0SLionel Sambuc EVP_MD_CTX_md(EVP_MD_CTX *ctx)
203ebfedea0SLionel Sambuc {
204ebfedea0SLionel Sambuc     return ctx->md;
205ebfedea0SLionel Sambuc }
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc /**
208ebfedea0SLionel Sambuc  * Return the output size of the message digest function.
209ebfedea0SLionel Sambuc  *
210ebfedea0SLionel Sambuc  * @param ctx the evp message digest context
211ebfedea0SLionel Sambuc  *
212ebfedea0SLionel Sambuc  * @return size output size of the message digest function.
213ebfedea0SLionel Sambuc  *
214ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
215ebfedea0SLionel Sambuc  */
216ebfedea0SLionel Sambuc 
217ebfedea0SLionel Sambuc size_t
EVP_MD_CTX_size(EVP_MD_CTX * ctx)218ebfedea0SLionel Sambuc EVP_MD_CTX_size(EVP_MD_CTX *ctx)
219ebfedea0SLionel Sambuc {
220ebfedea0SLionel Sambuc     return EVP_MD_size(ctx->md);
221ebfedea0SLionel Sambuc }
222ebfedea0SLionel Sambuc 
223ebfedea0SLionel Sambuc /**
224ebfedea0SLionel Sambuc  * Return the blocksize of the message digest function.
225ebfedea0SLionel Sambuc  *
226ebfedea0SLionel Sambuc  * @param ctx the evp message digest context
227ebfedea0SLionel Sambuc  *
228ebfedea0SLionel Sambuc  * @return size size of the message digest block size
229ebfedea0SLionel Sambuc  *
230ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
231ebfedea0SLionel Sambuc  */
232ebfedea0SLionel Sambuc 
233ebfedea0SLionel Sambuc size_t
EVP_MD_CTX_block_size(EVP_MD_CTX * ctx)234ebfedea0SLionel Sambuc EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
235ebfedea0SLionel Sambuc {
236ebfedea0SLionel Sambuc     return EVP_MD_block_size(ctx->md);
237ebfedea0SLionel Sambuc }
238ebfedea0SLionel Sambuc 
239ebfedea0SLionel Sambuc /**
240ebfedea0SLionel Sambuc  * Init a EVP_MD_CTX for use a specific message digest and engine.
241ebfedea0SLionel Sambuc  *
242ebfedea0SLionel Sambuc  * @param ctx the message digest context to init.
243ebfedea0SLionel Sambuc  * @param md the message digest to use.
244ebfedea0SLionel Sambuc  * @param engine the engine to use, NULL to use the default engine.
245ebfedea0SLionel Sambuc  *
246ebfedea0SLionel Sambuc  * @return 1 on success.
247ebfedea0SLionel Sambuc  *
248ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
249ebfedea0SLionel Sambuc  */
250ebfedea0SLionel Sambuc 
251ebfedea0SLionel Sambuc int
EVP_DigestInit_ex(EVP_MD_CTX * ctx,const EVP_MD * md,ENGINE * engine)252ebfedea0SLionel Sambuc EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
253ebfedea0SLionel Sambuc {
254ebfedea0SLionel Sambuc     if (ctx->md != md || ctx->engine != engine) {
255ebfedea0SLionel Sambuc 	EVP_MD_CTX_cleanup(ctx);
256ebfedea0SLionel Sambuc 	ctx->md = md;
257ebfedea0SLionel Sambuc 	ctx->engine = engine;
258ebfedea0SLionel Sambuc 
259ebfedea0SLionel Sambuc 	ctx->ptr = calloc(1, md->ctx_size);
260ebfedea0SLionel Sambuc 	if (ctx->ptr == NULL)
261ebfedea0SLionel Sambuc 	    return 0;
262ebfedea0SLionel Sambuc     }
263ebfedea0SLionel Sambuc     (ctx->md->init)(ctx->ptr);
264ebfedea0SLionel Sambuc     return 1;
265ebfedea0SLionel Sambuc }
266ebfedea0SLionel Sambuc 
267ebfedea0SLionel Sambuc /**
268ebfedea0SLionel Sambuc  * Update the digest with some data.
269ebfedea0SLionel Sambuc  *
270ebfedea0SLionel Sambuc  * @param ctx the context to update
271ebfedea0SLionel Sambuc  * @param data the data to update the context with
272ebfedea0SLionel Sambuc  * @param size length of data
273ebfedea0SLionel Sambuc  *
274ebfedea0SLionel Sambuc  * @return 1 on success.
275ebfedea0SLionel Sambuc  *
276ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
277ebfedea0SLionel Sambuc  */
278ebfedea0SLionel Sambuc 
279ebfedea0SLionel Sambuc int
EVP_DigestUpdate(EVP_MD_CTX * ctx,const void * data,size_t size)280ebfedea0SLionel Sambuc EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
281ebfedea0SLionel Sambuc {
282ebfedea0SLionel Sambuc     (ctx->md->update)(ctx->ptr, data, size);
283ebfedea0SLionel Sambuc     return 1;
284ebfedea0SLionel Sambuc }
285ebfedea0SLionel Sambuc 
286ebfedea0SLionel Sambuc /**
287ebfedea0SLionel Sambuc  * Complete the message digest.
288ebfedea0SLionel Sambuc  *
289ebfedea0SLionel Sambuc  * @param ctx the context to complete.
290ebfedea0SLionel Sambuc  * @param hash the output of the message digest function. At least
291ebfedea0SLionel Sambuc  * EVP_MD_size().
292ebfedea0SLionel Sambuc  * @param size the output size of hash.
293ebfedea0SLionel Sambuc  *
294ebfedea0SLionel Sambuc  * @return 1 on success.
295ebfedea0SLionel Sambuc  *
296ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
297ebfedea0SLionel Sambuc  */
298ebfedea0SLionel Sambuc 
299ebfedea0SLionel Sambuc int
EVP_DigestFinal_ex(EVP_MD_CTX * ctx,void * hash,unsigned int * size)300ebfedea0SLionel Sambuc EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
301ebfedea0SLionel Sambuc {
302ebfedea0SLionel Sambuc     (ctx->md->final)(hash, ctx->ptr);
303ebfedea0SLionel Sambuc     if (size)
304ebfedea0SLionel Sambuc 	*size = ctx->md->hash_size;
305ebfedea0SLionel Sambuc     return 1;
306ebfedea0SLionel Sambuc }
307ebfedea0SLionel Sambuc 
308ebfedea0SLionel Sambuc /**
309ebfedea0SLionel Sambuc  * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
310ebfedea0SLionel Sambuc  * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
311ebfedea0SLionel Sambuc  * dance in one call.
312ebfedea0SLionel Sambuc  *
313ebfedea0SLionel Sambuc  * @param data the data to update the context with
314ebfedea0SLionel Sambuc  * @param dsize length of data
315ebfedea0SLionel Sambuc  * @param hash output data of at least EVP_MD_size() length.
316ebfedea0SLionel Sambuc  * @param hsize output length of hash.
317ebfedea0SLionel Sambuc  * @param md message digest to use
318ebfedea0SLionel Sambuc  * @param engine engine to use, NULL for default engine.
319ebfedea0SLionel Sambuc  *
320ebfedea0SLionel Sambuc  * @return 1 on success.
321ebfedea0SLionel Sambuc  *
322ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
323ebfedea0SLionel Sambuc  */
324ebfedea0SLionel Sambuc 
325ebfedea0SLionel Sambuc int
EVP_Digest(const void * data,size_t dsize,void * hash,unsigned int * hsize,const EVP_MD * md,ENGINE * engine)326ebfedea0SLionel Sambuc EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
327ebfedea0SLionel Sambuc 	   const EVP_MD *md, ENGINE *engine)
328ebfedea0SLionel Sambuc {
329ebfedea0SLionel Sambuc     EVP_MD_CTX *ctx;
330ebfedea0SLionel Sambuc     int ret;
331ebfedea0SLionel Sambuc 
332ebfedea0SLionel Sambuc     ctx = EVP_MD_CTX_create();
333ebfedea0SLionel Sambuc     if (ctx == NULL)
334ebfedea0SLionel Sambuc 	return 0;
335ebfedea0SLionel Sambuc     ret = EVP_DigestInit_ex(ctx, md, engine);
336ebfedea0SLionel Sambuc     if (ret != 1) {
337ebfedea0SLionel Sambuc 	EVP_MD_CTX_destroy(ctx);
338ebfedea0SLionel Sambuc 	return ret;
339ebfedea0SLionel Sambuc     }
340ebfedea0SLionel Sambuc     ret = EVP_DigestUpdate(ctx, data, dsize);
341ebfedea0SLionel Sambuc     if (ret != 1) {
342ebfedea0SLionel Sambuc 	EVP_MD_CTX_destroy(ctx);
343ebfedea0SLionel Sambuc 	return ret;
344ebfedea0SLionel Sambuc     }
345ebfedea0SLionel Sambuc     ret = EVP_DigestFinal_ex(ctx, hash, hsize);
346ebfedea0SLionel Sambuc     EVP_MD_CTX_destroy(ctx);
347ebfedea0SLionel Sambuc     return ret;
348ebfedea0SLionel Sambuc }
349ebfedea0SLionel Sambuc 
350ebfedea0SLionel Sambuc /**
351ebfedea0SLionel Sambuc  * The message digest SHA256
352ebfedea0SLionel Sambuc  *
353ebfedea0SLionel Sambuc  * @return the message digest type.
354ebfedea0SLionel Sambuc  *
355ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
356ebfedea0SLionel Sambuc  */
357ebfedea0SLionel Sambuc 
358ebfedea0SLionel Sambuc const EVP_MD *
EVP_sha256(void)359ebfedea0SLionel Sambuc EVP_sha256(void)
360ebfedea0SLionel Sambuc {
361ebfedea0SLionel Sambuc     hcrypto_validate();
362ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha256);
363ebfedea0SLionel Sambuc }
364ebfedea0SLionel Sambuc 
365ebfedea0SLionel Sambuc /**
366ebfedea0SLionel Sambuc  * The message digest SHA384
367ebfedea0SLionel Sambuc  *
368ebfedea0SLionel Sambuc  * @return the message digest type.
369ebfedea0SLionel Sambuc  *
370ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
371ebfedea0SLionel Sambuc  */
372ebfedea0SLionel Sambuc 
373ebfedea0SLionel Sambuc const EVP_MD *
EVP_sha384(void)374ebfedea0SLionel Sambuc EVP_sha384(void)
375ebfedea0SLionel Sambuc {
376ebfedea0SLionel Sambuc     hcrypto_validate();
377ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha384);
378ebfedea0SLionel Sambuc }
379ebfedea0SLionel Sambuc 
380ebfedea0SLionel Sambuc /**
381ebfedea0SLionel Sambuc  * The message digest SHA512
382ebfedea0SLionel Sambuc  *
383ebfedea0SLionel Sambuc  * @return the message digest type.
384ebfedea0SLionel Sambuc  *
385ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
386ebfedea0SLionel Sambuc  */
387ebfedea0SLionel Sambuc 
388ebfedea0SLionel Sambuc const EVP_MD *
EVP_sha512(void)389ebfedea0SLionel Sambuc EVP_sha512(void)
390ebfedea0SLionel Sambuc {
391ebfedea0SLionel Sambuc     hcrypto_validate();
392ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha512);
393ebfedea0SLionel Sambuc }
394ebfedea0SLionel Sambuc 
395ebfedea0SLionel Sambuc /**
396ebfedea0SLionel Sambuc  * The message digest SHA1
397ebfedea0SLionel Sambuc  *
398ebfedea0SLionel Sambuc  * @return the message digest type.
399ebfedea0SLionel Sambuc  *
400ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
401ebfedea0SLionel Sambuc  */
402ebfedea0SLionel Sambuc 
403ebfedea0SLionel Sambuc const EVP_MD *
EVP_sha1(void)404ebfedea0SLionel Sambuc EVP_sha1(void)
405ebfedea0SLionel Sambuc {
406ebfedea0SLionel Sambuc     hcrypto_validate();
407ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha1);
408ebfedea0SLionel Sambuc }
409ebfedea0SLionel Sambuc 
410ebfedea0SLionel Sambuc /**
411ebfedea0SLionel Sambuc  * The message digest SHA1
412ebfedea0SLionel Sambuc  *
413ebfedea0SLionel Sambuc  * @return the message digest type.
414ebfedea0SLionel Sambuc  *
415ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
416ebfedea0SLionel Sambuc  */
417ebfedea0SLionel Sambuc 
418ebfedea0SLionel Sambuc const EVP_MD *
EVP_sha(void)419ebfedea0SLionel Sambuc EVP_sha(void) HC_DEPRECATED
420ebfedea0SLionel Sambuc 
421ebfedea0SLionel Sambuc {
422ebfedea0SLionel Sambuc     hcrypto_validate();
423ebfedea0SLionel Sambuc     return EVP_sha1();
424ebfedea0SLionel Sambuc }
425ebfedea0SLionel Sambuc 
426ebfedea0SLionel Sambuc /**
427ebfedea0SLionel Sambuc  * The message digest MD5
428ebfedea0SLionel Sambuc  *
429ebfedea0SLionel Sambuc  * @return the message digest type.
430ebfedea0SLionel Sambuc  *
431ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
432ebfedea0SLionel Sambuc  */
433ebfedea0SLionel Sambuc 
434ebfedea0SLionel Sambuc const EVP_MD *
EVP_md5(void)435ebfedea0SLionel Sambuc EVP_md5(void) HC_DEPRECATED_CRYPTO
436ebfedea0SLionel Sambuc {
437ebfedea0SLionel Sambuc     hcrypto_validate();
438ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md5);
439ebfedea0SLionel Sambuc }
440ebfedea0SLionel Sambuc 
441ebfedea0SLionel Sambuc /**
442ebfedea0SLionel Sambuc  * The message digest MD4
443ebfedea0SLionel Sambuc  *
444ebfedea0SLionel Sambuc  * @return the message digest type.
445ebfedea0SLionel Sambuc  *
446ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
447ebfedea0SLionel Sambuc  */
448ebfedea0SLionel Sambuc 
449ebfedea0SLionel Sambuc const EVP_MD *
EVP_md4(void)450ebfedea0SLionel Sambuc EVP_md4(void) HC_DEPRECATED_CRYPTO
451ebfedea0SLionel Sambuc {
452ebfedea0SLionel Sambuc     hcrypto_validate();
453ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md4);
454ebfedea0SLionel Sambuc }
455ebfedea0SLionel Sambuc 
456ebfedea0SLionel Sambuc /**
457ebfedea0SLionel Sambuc  * The message digest MD2
458ebfedea0SLionel Sambuc  *
459ebfedea0SLionel Sambuc  * @return the message digest type.
460ebfedea0SLionel Sambuc  *
461ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
462ebfedea0SLionel Sambuc  */
463ebfedea0SLionel Sambuc 
464ebfedea0SLionel Sambuc const EVP_MD *
EVP_md2(void)465ebfedea0SLionel Sambuc EVP_md2(void) HC_DEPRECATED_CRYPTO
466ebfedea0SLionel Sambuc {
467ebfedea0SLionel Sambuc     hcrypto_validate();
468ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md2);
469ebfedea0SLionel Sambuc }
470ebfedea0SLionel Sambuc 
471ebfedea0SLionel Sambuc /*
472ebfedea0SLionel Sambuc  *
473ebfedea0SLionel Sambuc  */
474ebfedea0SLionel Sambuc 
475ebfedea0SLionel Sambuc static void
null_Init(void * m)476ebfedea0SLionel Sambuc null_Init (void *m)
477ebfedea0SLionel Sambuc {
478ebfedea0SLionel Sambuc }
479ebfedea0SLionel Sambuc static void
null_Update(void * m,const void * data,size_t size)480ebfedea0SLionel Sambuc null_Update (void *m, const void * data, size_t size)
481ebfedea0SLionel Sambuc {
482ebfedea0SLionel Sambuc }
483ebfedea0SLionel Sambuc static void
null_Final(void * res,void * m)484ebfedea0SLionel Sambuc null_Final(void *res, void *m)
485ebfedea0SLionel Sambuc {
486ebfedea0SLionel Sambuc }
487ebfedea0SLionel Sambuc 
488ebfedea0SLionel Sambuc /**
489ebfedea0SLionel Sambuc  * The null message digest
490ebfedea0SLionel Sambuc  *
491ebfedea0SLionel Sambuc  * @return the message digest type.
492ebfedea0SLionel Sambuc  *
493ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
494ebfedea0SLionel Sambuc  */
495ebfedea0SLionel Sambuc 
496ebfedea0SLionel Sambuc const EVP_MD *
EVP_md_null(void)497ebfedea0SLionel Sambuc EVP_md_null(void)
498ebfedea0SLionel Sambuc {
499ebfedea0SLionel Sambuc     static const struct hc_evp_md null = {
500ebfedea0SLionel Sambuc 	0,
501ebfedea0SLionel Sambuc 	0,
502ebfedea0SLionel Sambuc 	0,
503ebfedea0SLionel Sambuc 	(hc_evp_md_init)null_Init,
504ebfedea0SLionel Sambuc 	(hc_evp_md_update)null_Update,
505ebfedea0SLionel Sambuc 	(hc_evp_md_final)null_Final,
506ebfedea0SLionel Sambuc 	NULL
507ebfedea0SLionel Sambuc     };
508ebfedea0SLionel Sambuc     return &null;
509ebfedea0SLionel Sambuc }
510ebfedea0SLionel Sambuc 
511ebfedea0SLionel Sambuc /**
512ebfedea0SLionel Sambuc  * Return the block size of the cipher.
513ebfedea0SLionel Sambuc  *
514ebfedea0SLionel Sambuc  * @param c cipher to get the block size from.
515ebfedea0SLionel Sambuc  *
516ebfedea0SLionel Sambuc  * @return the block size of the cipher.
517ebfedea0SLionel Sambuc  *
518ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
519ebfedea0SLionel Sambuc  */
520ebfedea0SLionel Sambuc 
521ebfedea0SLionel Sambuc size_t
EVP_CIPHER_block_size(const EVP_CIPHER * c)522ebfedea0SLionel Sambuc EVP_CIPHER_block_size(const EVP_CIPHER *c)
523ebfedea0SLionel Sambuc {
524ebfedea0SLionel Sambuc     return c->block_size;
525ebfedea0SLionel Sambuc }
526ebfedea0SLionel Sambuc 
527ebfedea0SLionel Sambuc /**
528ebfedea0SLionel Sambuc  * Return the key size of the cipher.
529ebfedea0SLionel Sambuc  *
530ebfedea0SLionel Sambuc  * @param c cipher to get the key size from.
531ebfedea0SLionel Sambuc  *
532ebfedea0SLionel Sambuc  * @return the key size of the cipher.
533ebfedea0SLionel Sambuc  *
534ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
535ebfedea0SLionel Sambuc  */
536ebfedea0SLionel Sambuc 
537ebfedea0SLionel Sambuc size_t
EVP_CIPHER_key_length(const EVP_CIPHER * c)538ebfedea0SLionel Sambuc EVP_CIPHER_key_length(const EVP_CIPHER *c)
539ebfedea0SLionel Sambuc {
540ebfedea0SLionel Sambuc     return c->key_len;
541ebfedea0SLionel Sambuc }
542ebfedea0SLionel Sambuc 
543ebfedea0SLionel Sambuc /**
544ebfedea0SLionel Sambuc  * Return the IV size of the cipher.
545ebfedea0SLionel Sambuc  *
546ebfedea0SLionel Sambuc  * @param c cipher to get the IV size from.
547ebfedea0SLionel Sambuc  *
548ebfedea0SLionel Sambuc  * @return the IV size of the cipher.
549ebfedea0SLionel Sambuc  *
550ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
551ebfedea0SLionel Sambuc  */
552ebfedea0SLionel Sambuc 
553ebfedea0SLionel Sambuc size_t
EVP_CIPHER_iv_length(const EVP_CIPHER * c)554ebfedea0SLionel Sambuc EVP_CIPHER_iv_length(const EVP_CIPHER *c)
555ebfedea0SLionel Sambuc {
556ebfedea0SLionel Sambuc     return c->iv_len;
557ebfedea0SLionel Sambuc }
558ebfedea0SLionel Sambuc 
559ebfedea0SLionel Sambuc /**
560ebfedea0SLionel Sambuc  * Initiate a EVP_CIPHER_CTX context. Clean up with
561ebfedea0SLionel Sambuc  * EVP_CIPHER_CTX_cleanup().
562ebfedea0SLionel Sambuc  *
563ebfedea0SLionel Sambuc  * @param c the cipher initiate.
564ebfedea0SLionel Sambuc  *
565ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
566ebfedea0SLionel Sambuc  */
567ebfedea0SLionel Sambuc 
568ebfedea0SLionel Sambuc void
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX * c)569ebfedea0SLionel Sambuc EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
570ebfedea0SLionel Sambuc {
571ebfedea0SLionel Sambuc     memset(c, 0, sizeof(*c));
572ebfedea0SLionel Sambuc }
573ebfedea0SLionel Sambuc 
574ebfedea0SLionel Sambuc /**
575ebfedea0SLionel Sambuc  * Clean up the EVP_CIPHER_CTX context.
576ebfedea0SLionel Sambuc  *
577ebfedea0SLionel Sambuc  * @param c the cipher to clean up.
578ebfedea0SLionel Sambuc  *
579ebfedea0SLionel Sambuc  * @return 1 on success.
580ebfedea0SLionel Sambuc  *
581ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
582ebfedea0SLionel Sambuc  */
583ebfedea0SLionel Sambuc 
584ebfedea0SLionel Sambuc int
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX * c)585ebfedea0SLionel Sambuc EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
586ebfedea0SLionel Sambuc {
587ebfedea0SLionel Sambuc     if (c->cipher && c->cipher->cleanup)
588ebfedea0SLionel Sambuc 	c->cipher->cleanup(c);
589ebfedea0SLionel Sambuc     if (c->cipher_data) {
590ebfedea0SLionel Sambuc 	memset(c->cipher_data, 0, c->cipher->ctx_size);
591ebfedea0SLionel Sambuc 	free(c->cipher_data);
592ebfedea0SLionel Sambuc 	c->cipher_data = NULL;
593ebfedea0SLionel Sambuc     }
594ebfedea0SLionel Sambuc     return 1;
595ebfedea0SLionel Sambuc }
596ebfedea0SLionel Sambuc 
597ebfedea0SLionel Sambuc /**
598ebfedea0SLionel Sambuc  * If the cipher type supports it, change the key length
599ebfedea0SLionel Sambuc  *
600ebfedea0SLionel Sambuc  * @param c the cipher context to change the key length for
601ebfedea0SLionel Sambuc  * @param length new key length
602ebfedea0SLionel Sambuc  *
603ebfedea0SLionel Sambuc  * @return 1 on success.
604ebfedea0SLionel Sambuc  *
605ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
606ebfedea0SLionel Sambuc  */
607ebfedea0SLionel Sambuc 
608ebfedea0SLionel Sambuc int
EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX * c,int length)609ebfedea0SLionel Sambuc EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
610ebfedea0SLionel Sambuc {
611ebfedea0SLionel Sambuc     if ((c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH) && length > 0) {
612ebfedea0SLionel Sambuc 	c->key_len = length;
613ebfedea0SLionel Sambuc 	return 1;
614ebfedea0SLionel Sambuc     }
615ebfedea0SLionel Sambuc     return 0;
616ebfedea0SLionel Sambuc }
617ebfedea0SLionel Sambuc 
618ebfedea0SLionel Sambuc #if 0
619ebfedea0SLionel Sambuc int
620ebfedea0SLionel Sambuc EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
621ebfedea0SLionel Sambuc {
622ebfedea0SLionel Sambuc     return 0;
623ebfedea0SLionel Sambuc }
624ebfedea0SLionel Sambuc #endif
625ebfedea0SLionel Sambuc 
626ebfedea0SLionel Sambuc /**
627ebfedea0SLionel Sambuc  * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
628ebfedea0SLionel Sambuc  *
629ebfedea0SLionel Sambuc  * @param ctx the context to get the cipher type from.
630ebfedea0SLionel Sambuc  *
631ebfedea0SLionel Sambuc  * @return the EVP_CIPHER pointer.
632ebfedea0SLionel Sambuc  *
633ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
634ebfedea0SLionel Sambuc  */
635ebfedea0SLionel Sambuc 
636ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX * ctx)637ebfedea0SLionel Sambuc EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
638ebfedea0SLionel Sambuc {
639ebfedea0SLionel Sambuc     return ctx->cipher;
640ebfedea0SLionel Sambuc }
641ebfedea0SLionel Sambuc 
642ebfedea0SLionel Sambuc /**
643ebfedea0SLionel Sambuc  * Return the block size of the cipher context.
644ebfedea0SLionel Sambuc  *
645ebfedea0SLionel Sambuc  * @param ctx cipher context to get the block size from.
646ebfedea0SLionel Sambuc  *
647ebfedea0SLionel Sambuc  * @return the block size of the cipher context.
648ebfedea0SLionel Sambuc  *
649ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
650ebfedea0SLionel Sambuc  */
651ebfedea0SLionel Sambuc 
652ebfedea0SLionel Sambuc size_t
EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX * ctx)653ebfedea0SLionel Sambuc EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
654ebfedea0SLionel Sambuc {
655ebfedea0SLionel Sambuc     return EVP_CIPHER_block_size(ctx->cipher);
656ebfedea0SLionel Sambuc }
657ebfedea0SLionel Sambuc 
658ebfedea0SLionel Sambuc /**
659ebfedea0SLionel Sambuc  * Return the key size of the cipher context.
660ebfedea0SLionel Sambuc  *
661ebfedea0SLionel Sambuc  * @param ctx cipher context to get the key size from.
662ebfedea0SLionel Sambuc  *
663ebfedea0SLionel Sambuc  * @return the key size of the cipher context.
664ebfedea0SLionel Sambuc  *
665ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
666ebfedea0SLionel Sambuc  */
667ebfedea0SLionel Sambuc 
668ebfedea0SLionel Sambuc size_t
EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX * ctx)669ebfedea0SLionel Sambuc EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
670ebfedea0SLionel Sambuc {
671ebfedea0SLionel Sambuc     return EVP_CIPHER_key_length(ctx->cipher);
672ebfedea0SLionel Sambuc }
673ebfedea0SLionel Sambuc 
674ebfedea0SLionel Sambuc /**
675ebfedea0SLionel Sambuc  * Return the IV size of the cipher context.
676ebfedea0SLionel Sambuc  *
677ebfedea0SLionel Sambuc  * @param ctx cipher context to get the IV size from.
678ebfedea0SLionel Sambuc  *
679ebfedea0SLionel Sambuc  * @return the IV size of the cipher context.
680ebfedea0SLionel Sambuc  *
681ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
682ebfedea0SLionel Sambuc  */
683ebfedea0SLionel Sambuc 
684ebfedea0SLionel Sambuc size_t
EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX * ctx)685ebfedea0SLionel Sambuc EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
686ebfedea0SLionel Sambuc {
687ebfedea0SLionel Sambuc     return EVP_CIPHER_iv_length(ctx->cipher);
688ebfedea0SLionel Sambuc }
689ebfedea0SLionel Sambuc 
690ebfedea0SLionel Sambuc /**
691ebfedea0SLionel Sambuc  * Get the flags for an EVP_CIPHER_CTX context.
692ebfedea0SLionel Sambuc  *
693ebfedea0SLionel Sambuc  * @param ctx the EVP_CIPHER_CTX to get the flags from
694ebfedea0SLionel Sambuc  *
695ebfedea0SLionel Sambuc  * @return the flags for an EVP_CIPHER_CTX.
696ebfedea0SLionel Sambuc  *
697ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
698ebfedea0SLionel Sambuc  */
699ebfedea0SLionel Sambuc 
700ebfedea0SLionel Sambuc unsigned long
EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX * ctx)701ebfedea0SLionel Sambuc EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
702ebfedea0SLionel Sambuc {
703ebfedea0SLionel Sambuc     return ctx->cipher->flags;
704ebfedea0SLionel Sambuc }
705ebfedea0SLionel Sambuc 
706ebfedea0SLionel Sambuc /**
707ebfedea0SLionel Sambuc  * Get the mode for an EVP_CIPHER_CTX context.
708ebfedea0SLionel Sambuc  *
709ebfedea0SLionel Sambuc  * @param ctx the EVP_CIPHER_CTX to get the mode from
710ebfedea0SLionel Sambuc  *
711ebfedea0SLionel Sambuc  * @return the mode for an EVP_CIPHER_CTX.
712ebfedea0SLionel Sambuc  *
713ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
714ebfedea0SLionel Sambuc  */
715ebfedea0SLionel Sambuc 
716ebfedea0SLionel Sambuc int
EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX * ctx)717ebfedea0SLionel Sambuc EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
718ebfedea0SLionel Sambuc {
719ebfedea0SLionel Sambuc     return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
720ebfedea0SLionel Sambuc }
721ebfedea0SLionel Sambuc 
722ebfedea0SLionel Sambuc /**
723ebfedea0SLionel Sambuc  * Get the app data for an EVP_CIPHER_CTX context.
724ebfedea0SLionel Sambuc  *
725ebfedea0SLionel Sambuc  * @param ctx the EVP_CIPHER_CTX to get the app data from
726ebfedea0SLionel Sambuc  *
727ebfedea0SLionel Sambuc  * @return the app data for an EVP_CIPHER_CTX.
728ebfedea0SLionel Sambuc  *
729ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
730ebfedea0SLionel Sambuc  */
731ebfedea0SLionel Sambuc 
732ebfedea0SLionel Sambuc void *
EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX * ctx)733ebfedea0SLionel Sambuc EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
734ebfedea0SLionel Sambuc {
735ebfedea0SLionel Sambuc     return ctx->app_data;
736ebfedea0SLionel Sambuc }
737ebfedea0SLionel Sambuc 
738ebfedea0SLionel Sambuc /**
739ebfedea0SLionel Sambuc  * Set the app data for an EVP_CIPHER_CTX context.
740ebfedea0SLionel Sambuc  *
741ebfedea0SLionel Sambuc  * @param ctx the EVP_CIPHER_CTX to set the app data for
742ebfedea0SLionel Sambuc  * @param data the app data to set for an EVP_CIPHER_CTX.
743ebfedea0SLionel Sambuc  *
744ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
745ebfedea0SLionel Sambuc  */
746ebfedea0SLionel Sambuc 
747ebfedea0SLionel Sambuc void
EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX * ctx,void * data)748ebfedea0SLionel Sambuc EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
749ebfedea0SLionel Sambuc {
750ebfedea0SLionel Sambuc     ctx->app_data = data;
751ebfedea0SLionel Sambuc }
752ebfedea0SLionel Sambuc 
753ebfedea0SLionel Sambuc /**
754ebfedea0SLionel Sambuc  * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
755ebfedea0SLionel Sambuc  * Clean up with EVP_CIPHER_CTX_cleanup().
756ebfedea0SLionel Sambuc  *
757ebfedea0SLionel Sambuc  * @param ctx context to initiate
758ebfedea0SLionel Sambuc  * @param c cipher to use.
759ebfedea0SLionel Sambuc  * @param engine crypto engine to use, NULL to select default.
760ebfedea0SLionel Sambuc  * @param key the crypto key to use, NULL will use the previous value.
761ebfedea0SLionel Sambuc  * @param iv the IV to use, NULL will use the previous value.
762ebfedea0SLionel Sambuc  * @param encp non zero will encrypt, -1 use the previous value.
763ebfedea0SLionel Sambuc  *
764ebfedea0SLionel Sambuc  * @return 1 on success.
765ebfedea0SLionel Sambuc  *
766ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
767ebfedea0SLionel Sambuc  */
768ebfedea0SLionel Sambuc 
769ebfedea0SLionel Sambuc int
EVP_CipherInit_ex(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * c,ENGINE * engine,const void * key,const void * iv,int encp)770ebfedea0SLionel Sambuc EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
771ebfedea0SLionel Sambuc 		  const void *key, const void *iv, int encp)
772ebfedea0SLionel Sambuc {
773ebfedea0SLionel Sambuc     ctx->buf_len = 0;
774ebfedea0SLionel Sambuc 
775ebfedea0SLionel Sambuc     if (encp == -1)
776ebfedea0SLionel Sambuc 	encp = ctx->encrypt;
777ebfedea0SLionel Sambuc     else
778ebfedea0SLionel Sambuc 	ctx->encrypt = (encp ? 1 : 0);
779ebfedea0SLionel Sambuc 
780ebfedea0SLionel Sambuc     if (c && (c != ctx->cipher)) {
781ebfedea0SLionel Sambuc 	EVP_CIPHER_CTX_cleanup(ctx);
782ebfedea0SLionel Sambuc 	ctx->cipher = c;
783ebfedea0SLionel Sambuc 	ctx->key_len = c->key_len;
784ebfedea0SLionel Sambuc 
785ebfedea0SLionel Sambuc 	ctx->cipher_data = calloc(1, c->ctx_size);
786ebfedea0SLionel Sambuc 	if (ctx->cipher_data == NULL && c->ctx_size != 0)
787ebfedea0SLionel Sambuc 	    return 0;
788ebfedea0SLionel Sambuc 
789ebfedea0SLionel Sambuc 	/* assume block size is a multiple of 2 */
790ebfedea0SLionel Sambuc 	ctx->block_mask = EVP_CIPHER_block_size(c) - 1;
791ebfedea0SLionel Sambuc 
792ebfedea0SLionel Sambuc     } else if (ctx->cipher == NULL) {
793ebfedea0SLionel Sambuc 	/* reuse of cipher, but not any cipher ever set! */
794ebfedea0SLionel Sambuc 	return 0;
795ebfedea0SLionel Sambuc     }
796ebfedea0SLionel Sambuc 
797ebfedea0SLionel Sambuc     switch (EVP_CIPHER_CTX_mode(ctx)) {
798ebfedea0SLionel Sambuc     case EVP_CIPH_CBC_MODE:
799ebfedea0SLionel Sambuc 
800ebfedea0SLionel Sambuc 	assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
801ebfedea0SLionel Sambuc 
802ebfedea0SLionel Sambuc 	if (iv)
803ebfedea0SLionel Sambuc 	    memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
804ebfedea0SLionel Sambuc 	memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
805ebfedea0SLionel Sambuc 	break;
806ebfedea0SLionel Sambuc 
807ebfedea0SLionel Sambuc     case EVP_CIPH_STREAM_CIPHER:
808ebfedea0SLionel Sambuc 	break;
809ebfedea0SLionel Sambuc     case EVP_CIPH_CFB8_MODE:
810ebfedea0SLionel Sambuc 	if (iv)
811ebfedea0SLionel Sambuc 	    memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
812ebfedea0SLionel Sambuc 	break;
813ebfedea0SLionel Sambuc 
814ebfedea0SLionel Sambuc     default:
815ebfedea0SLionel Sambuc 	return 0;
816ebfedea0SLionel Sambuc     }
817ebfedea0SLionel Sambuc 
818ebfedea0SLionel Sambuc     if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
819ebfedea0SLionel Sambuc 	ctx->cipher->init(ctx, key, iv, encp);
820ebfedea0SLionel Sambuc 
821ebfedea0SLionel Sambuc     return 1;
822ebfedea0SLionel Sambuc }
823ebfedea0SLionel Sambuc 
824ebfedea0SLionel Sambuc /**
825ebfedea0SLionel Sambuc  * Encipher/decipher partial data
826ebfedea0SLionel Sambuc  *
827ebfedea0SLionel Sambuc  * @param ctx the cipher context.
828ebfedea0SLionel Sambuc  * @param out output data from the operation.
829ebfedea0SLionel Sambuc  * @param outlen output length
830ebfedea0SLionel Sambuc  * @param in input data to the operation.
831ebfedea0SLionel Sambuc  * @param inlen length of data.
832ebfedea0SLionel Sambuc  *
833ebfedea0SLionel Sambuc  * The output buffer length should at least be EVP_CIPHER_block_size()
834ebfedea0SLionel Sambuc  * byte longer then the input length.
835ebfedea0SLionel Sambuc  *
836ebfedea0SLionel Sambuc  * See @ref evp_cipher for an example how to use this function.
837ebfedea0SLionel Sambuc  *
838ebfedea0SLionel Sambuc  * @return 1 on success.
839ebfedea0SLionel Sambuc  *
840ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
841ebfedea0SLionel Sambuc  */
842ebfedea0SLionel Sambuc 
843ebfedea0SLionel Sambuc int
EVP_CipherUpdate(EVP_CIPHER_CTX * ctx,void * out,int * outlen,void * in,size_t inlen)844ebfedea0SLionel Sambuc EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen,
845ebfedea0SLionel Sambuc 		 void *in, size_t inlen)
846ebfedea0SLionel Sambuc {
847ebfedea0SLionel Sambuc     int ret, left, blocksize;
848ebfedea0SLionel Sambuc 
849ebfedea0SLionel Sambuc     *outlen = 0;
850ebfedea0SLionel Sambuc 
851ebfedea0SLionel Sambuc     /**
852ebfedea0SLionel Sambuc      * If there in no spare bytes in the left from last Update and the
853ebfedea0SLionel Sambuc      * input length is on the block boundery, the EVP_CipherUpdate()
854ebfedea0SLionel Sambuc      * function can take a shortcut (and preformance gain) and
855ebfedea0SLionel Sambuc      * directly encrypt the data, otherwise we hav to fix it up and
856ebfedea0SLionel Sambuc      * store extra it the EVP_CIPHER_CTX.
857ebfedea0SLionel Sambuc      */
858ebfedea0SLionel Sambuc     if (ctx->buf_len == 0 && (inlen & ctx->block_mask) == 0) {
859ebfedea0SLionel Sambuc 	ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
860ebfedea0SLionel Sambuc 	if (ret == 1)
861ebfedea0SLionel Sambuc 	    *outlen = inlen;
862ebfedea0SLionel Sambuc 	else
863ebfedea0SLionel Sambuc 	    *outlen = 0;
864ebfedea0SLionel Sambuc 	return ret;
865ebfedea0SLionel Sambuc     }
866ebfedea0SLionel Sambuc 
867ebfedea0SLionel Sambuc 
868ebfedea0SLionel Sambuc     blocksize = EVP_CIPHER_CTX_block_size(ctx);
869ebfedea0SLionel Sambuc     left = blocksize - ctx->buf_len;
870ebfedea0SLionel Sambuc     assert(left > 0);
871ebfedea0SLionel Sambuc 
872ebfedea0SLionel Sambuc     if (ctx->buf_len) {
873ebfedea0SLionel Sambuc 
874ebfedea0SLionel Sambuc 	/* if total buffer is smaller then input, store locally */
875ebfedea0SLionel Sambuc 	if (inlen < left) {
876ebfedea0SLionel Sambuc 	    memcpy(ctx->buf + ctx->buf_len, in, inlen);
877ebfedea0SLionel Sambuc 	    ctx->buf_len += inlen;
878ebfedea0SLionel Sambuc 	    return 1;
879ebfedea0SLionel Sambuc 	}
880ebfedea0SLionel Sambuc 
881ebfedea0SLionel Sambuc 	/* fill in local buffer and encrypt */
882ebfedea0SLionel Sambuc 	memcpy(ctx->buf + ctx->buf_len, in, left);
883ebfedea0SLionel Sambuc 	ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
884ebfedea0SLionel Sambuc 	memset(ctx->buf, 0, blocksize);
885ebfedea0SLionel Sambuc 	if (ret != 1)
886ebfedea0SLionel Sambuc 	    return ret;
887ebfedea0SLionel Sambuc 
888ebfedea0SLionel Sambuc 	*outlen += blocksize;
889ebfedea0SLionel Sambuc 	inlen -= left;
890ebfedea0SLionel Sambuc 	in = ((unsigned char *)in) + left;
891ebfedea0SLionel Sambuc 	out = ((unsigned char *)out) + blocksize;
892ebfedea0SLionel Sambuc 	ctx->buf_len = 0;
893ebfedea0SLionel Sambuc     }
894ebfedea0SLionel Sambuc 
895ebfedea0SLionel Sambuc     if (inlen) {
896ebfedea0SLionel Sambuc 	ctx->buf_len = (inlen & ctx->block_mask);
897ebfedea0SLionel Sambuc 	inlen &= ~ctx->block_mask;
898ebfedea0SLionel Sambuc 
899ebfedea0SLionel Sambuc 	ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
900ebfedea0SLionel Sambuc 	if (ret != 1)
901ebfedea0SLionel Sambuc 	    return ret;
902ebfedea0SLionel Sambuc 
903ebfedea0SLionel Sambuc 	*outlen += inlen;
904ebfedea0SLionel Sambuc 
905ebfedea0SLionel Sambuc 	in = ((unsigned char *)in) + inlen;
906ebfedea0SLionel Sambuc 	memcpy(ctx->buf, in, ctx->buf_len);
907ebfedea0SLionel Sambuc     }
908ebfedea0SLionel Sambuc 
909ebfedea0SLionel Sambuc     return 1;
910ebfedea0SLionel Sambuc }
911ebfedea0SLionel Sambuc 
912ebfedea0SLionel Sambuc /**
913ebfedea0SLionel Sambuc  * Encipher/decipher final data
914ebfedea0SLionel Sambuc  *
915ebfedea0SLionel Sambuc  * @param ctx the cipher context.
916ebfedea0SLionel Sambuc  * @param out output data from the operation.
917ebfedea0SLionel Sambuc  * @param outlen output length
918ebfedea0SLionel Sambuc  *
919ebfedea0SLionel Sambuc  * The input length needs to be at least EVP_CIPHER_block_size() bytes
920ebfedea0SLionel Sambuc  * long.
921ebfedea0SLionel Sambuc  *
922ebfedea0SLionel Sambuc  * See @ref evp_cipher for an example how to use this function.
923ebfedea0SLionel Sambuc  *
924ebfedea0SLionel Sambuc  * @return 1 on success.
925ebfedea0SLionel Sambuc  *
926ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
927ebfedea0SLionel Sambuc  */
928ebfedea0SLionel Sambuc 
929ebfedea0SLionel Sambuc int
EVP_CipherFinal_ex(EVP_CIPHER_CTX * ctx,void * out,int * outlen)930ebfedea0SLionel Sambuc EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, void *out, int *outlen)
931ebfedea0SLionel Sambuc {
932ebfedea0SLionel Sambuc     *outlen = 0;
933ebfedea0SLionel Sambuc 
934ebfedea0SLionel Sambuc     if (ctx->buf_len) {
935ebfedea0SLionel Sambuc 	int ret, left, blocksize;
936ebfedea0SLionel Sambuc 
937ebfedea0SLionel Sambuc 	blocksize = EVP_CIPHER_CTX_block_size(ctx);
938ebfedea0SLionel Sambuc 
939ebfedea0SLionel Sambuc 	left = blocksize - ctx->buf_len;
940ebfedea0SLionel Sambuc 	assert(left > 0);
941ebfedea0SLionel Sambuc 
942ebfedea0SLionel Sambuc 	/* zero fill local buffer */
943ebfedea0SLionel Sambuc 	memset(ctx->buf + ctx->buf_len, 0, left);
944ebfedea0SLionel Sambuc 	ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
945ebfedea0SLionel Sambuc 	memset(ctx->buf, 0, blocksize);
946ebfedea0SLionel Sambuc 	if (ret != 1)
947ebfedea0SLionel Sambuc 	    return ret;
948ebfedea0SLionel Sambuc 
949ebfedea0SLionel Sambuc 	*outlen += blocksize;
950ebfedea0SLionel Sambuc     }
951ebfedea0SLionel Sambuc 
952ebfedea0SLionel Sambuc     return 1;
953ebfedea0SLionel Sambuc }
954ebfedea0SLionel Sambuc 
955ebfedea0SLionel Sambuc /**
956ebfedea0SLionel Sambuc  * Encipher/decipher data
957ebfedea0SLionel Sambuc  *
958ebfedea0SLionel Sambuc  * @param ctx the cipher context.
959ebfedea0SLionel Sambuc  * @param out out data from the operation.
960ebfedea0SLionel Sambuc  * @param in in data to the operation.
961ebfedea0SLionel Sambuc  * @param size length of data.
962ebfedea0SLionel Sambuc  *
963ebfedea0SLionel Sambuc  * @return 1 on success.
964ebfedea0SLionel Sambuc  */
965ebfedea0SLionel Sambuc 
966ebfedea0SLionel Sambuc int
EVP_Cipher(EVP_CIPHER_CTX * ctx,void * out,const void * in,size_t size)967ebfedea0SLionel Sambuc EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
968ebfedea0SLionel Sambuc {
969ebfedea0SLionel Sambuc     return ctx->cipher->do_cipher(ctx, out, in, size);
970ebfedea0SLionel Sambuc }
971ebfedea0SLionel Sambuc 
972ebfedea0SLionel Sambuc /*
973ebfedea0SLionel Sambuc  *
974ebfedea0SLionel Sambuc  */
975ebfedea0SLionel Sambuc 
976ebfedea0SLionel Sambuc static int
enc_null_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int encp)977ebfedea0SLionel Sambuc enc_null_init(EVP_CIPHER_CTX *ctx,
978ebfedea0SLionel Sambuc 		  const unsigned char * key,
979ebfedea0SLionel Sambuc 		  const unsigned char * iv,
980ebfedea0SLionel Sambuc 		  int encp)
981ebfedea0SLionel Sambuc {
982ebfedea0SLionel Sambuc     return 1;
983ebfedea0SLionel Sambuc }
984ebfedea0SLionel Sambuc 
985ebfedea0SLionel Sambuc static int
enc_null_do_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int size)986ebfedea0SLionel Sambuc enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
987ebfedea0SLionel Sambuc 	      unsigned char *out,
988ebfedea0SLionel Sambuc 	      const unsigned char *in,
989ebfedea0SLionel Sambuc 	      unsigned int size)
990ebfedea0SLionel Sambuc {
991ebfedea0SLionel Sambuc     memmove(out, in, size);
992ebfedea0SLionel Sambuc     return 1;
993ebfedea0SLionel Sambuc }
994ebfedea0SLionel Sambuc 
995ebfedea0SLionel Sambuc static int
enc_null_cleanup(EVP_CIPHER_CTX * ctx)996ebfedea0SLionel Sambuc enc_null_cleanup(EVP_CIPHER_CTX *ctx)
997ebfedea0SLionel Sambuc {
998ebfedea0SLionel Sambuc     return 1;
999ebfedea0SLionel Sambuc }
1000ebfedea0SLionel Sambuc 
1001ebfedea0SLionel Sambuc /**
1002ebfedea0SLionel Sambuc  * The NULL cipher type, does no encryption/decryption.
1003ebfedea0SLionel Sambuc  *
1004ebfedea0SLionel Sambuc  * @return the null EVP_CIPHER pointer.
1005ebfedea0SLionel Sambuc  *
1006ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1007ebfedea0SLionel Sambuc  */
1008ebfedea0SLionel Sambuc 
1009ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_enc_null(void)1010ebfedea0SLionel Sambuc EVP_enc_null(void)
1011ebfedea0SLionel Sambuc {
1012ebfedea0SLionel Sambuc     static const EVP_CIPHER enc_null = {
1013ebfedea0SLionel Sambuc 	0,
1014ebfedea0SLionel Sambuc 	0,
1015ebfedea0SLionel Sambuc 	0,
1016ebfedea0SLionel Sambuc 	0,
1017ebfedea0SLionel Sambuc 	EVP_CIPH_CBC_MODE,
1018ebfedea0SLionel Sambuc 	enc_null_init,
1019ebfedea0SLionel Sambuc 	enc_null_do_cipher,
1020ebfedea0SLionel Sambuc 	enc_null_cleanup,
1021ebfedea0SLionel Sambuc 	0,
1022ebfedea0SLionel Sambuc 	NULL,
1023ebfedea0SLionel Sambuc 	NULL,
1024ebfedea0SLionel Sambuc 	NULL,
1025ebfedea0SLionel Sambuc 	NULL
1026ebfedea0SLionel Sambuc     };
1027ebfedea0SLionel Sambuc     return &enc_null;
1028ebfedea0SLionel Sambuc }
1029ebfedea0SLionel Sambuc 
1030ebfedea0SLionel Sambuc /**
1031ebfedea0SLionel Sambuc  * The RC2 cipher type
1032ebfedea0SLionel Sambuc  *
1033ebfedea0SLionel Sambuc  * @return the RC2 EVP_CIPHER pointer.
1034ebfedea0SLionel Sambuc  *
1035ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1036ebfedea0SLionel Sambuc  */
1037ebfedea0SLionel Sambuc 
1038ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_rc2_cbc(void)1039ebfedea0SLionel Sambuc EVP_rc2_cbc(void)
1040ebfedea0SLionel Sambuc {
1041ebfedea0SLionel Sambuc     hcrypto_validate();
1042ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_cbc);
1043ebfedea0SLionel Sambuc }
1044ebfedea0SLionel Sambuc 
1045ebfedea0SLionel Sambuc /**
1046ebfedea0SLionel Sambuc  * The RC2 cipher type
1047ebfedea0SLionel Sambuc  *
1048ebfedea0SLionel Sambuc  * @return the RC2 EVP_CIPHER pointer.
1049ebfedea0SLionel Sambuc  *
1050ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1051ebfedea0SLionel Sambuc  */
1052ebfedea0SLionel Sambuc 
1053ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_rc2_40_cbc(void)1054ebfedea0SLionel Sambuc EVP_rc2_40_cbc(void)
1055ebfedea0SLionel Sambuc {
1056ebfedea0SLionel Sambuc     hcrypto_validate();
1057ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_40_cbc);
1058ebfedea0SLionel Sambuc }
1059ebfedea0SLionel Sambuc 
1060ebfedea0SLionel Sambuc /**
1061ebfedea0SLionel Sambuc  * The RC2 cipher type
1062ebfedea0SLionel Sambuc  *
1063ebfedea0SLionel Sambuc  * @return the RC2 EVP_CIPHER pointer.
1064ebfedea0SLionel Sambuc  *
1065ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1066ebfedea0SLionel Sambuc  */
1067ebfedea0SLionel Sambuc 
1068ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_rc2_64_cbc(void)1069ebfedea0SLionel Sambuc EVP_rc2_64_cbc(void)
1070ebfedea0SLionel Sambuc {
1071ebfedea0SLionel Sambuc     hcrypto_validate();
1072ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_64_cbc);
1073ebfedea0SLionel Sambuc }
1074ebfedea0SLionel Sambuc 
1075ebfedea0SLionel Sambuc /**
1076ebfedea0SLionel Sambuc  * The RC4 cipher type
1077ebfedea0SLionel Sambuc  *
1078ebfedea0SLionel Sambuc  * @return the RC4 EVP_CIPHER pointer.
1079ebfedea0SLionel Sambuc  *
1080ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1081ebfedea0SLionel Sambuc  */
1082ebfedea0SLionel Sambuc 
1083ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_rc4(void)1084ebfedea0SLionel Sambuc EVP_rc4(void)
1085ebfedea0SLionel Sambuc {
1086ebfedea0SLionel Sambuc     hcrypto_validate();
1087ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4);
1088ebfedea0SLionel Sambuc }
1089ebfedea0SLionel Sambuc 
1090ebfedea0SLionel Sambuc /**
1091ebfedea0SLionel Sambuc  * The RC4-40 cipher type
1092ebfedea0SLionel Sambuc  *
1093ebfedea0SLionel Sambuc  * @return the RC4-40 EVP_CIPHER pointer.
1094ebfedea0SLionel Sambuc  *
1095ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1096ebfedea0SLionel Sambuc  */
1097ebfedea0SLionel Sambuc 
1098ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_rc4_40(void)1099ebfedea0SLionel Sambuc EVP_rc4_40(void)
1100ebfedea0SLionel Sambuc {
1101ebfedea0SLionel Sambuc     hcrypto_validate();
1102ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4_40);
1103ebfedea0SLionel Sambuc }
1104ebfedea0SLionel Sambuc 
1105ebfedea0SLionel Sambuc /**
1106ebfedea0SLionel Sambuc  * The DES cipher type
1107ebfedea0SLionel Sambuc  *
1108ebfedea0SLionel Sambuc  * @return the DES-CBC EVP_CIPHER pointer.
1109ebfedea0SLionel Sambuc  *
1110ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1111ebfedea0SLionel Sambuc  */
1112ebfedea0SLionel Sambuc 
1113ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_des_cbc(void)1114ebfedea0SLionel Sambuc EVP_des_cbc(void)
1115ebfedea0SLionel Sambuc {
1116ebfedea0SLionel Sambuc     hcrypto_validate();
1117ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_cbc);
1118ebfedea0SLionel Sambuc }
1119ebfedea0SLionel Sambuc 
1120ebfedea0SLionel Sambuc /**
1121ebfedea0SLionel Sambuc  * The tripple DES cipher type
1122ebfedea0SLionel Sambuc  *
1123ebfedea0SLionel Sambuc  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
1124ebfedea0SLionel Sambuc  *
1125ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1126ebfedea0SLionel Sambuc  */
1127ebfedea0SLionel Sambuc 
1128ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_des_ede3_cbc(void)1129ebfedea0SLionel Sambuc EVP_des_ede3_cbc(void)
1130ebfedea0SLionel Sambuc {
1131ebfedea0SLionel Sambuc     hcrypto_validate();
1132ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_ede3_cbc);
1133ebfedea0SLionel Sambuc }
1134ebfedea0SLionel Sambuc 
1135ebfedea0SLionel Sambuc /**
1136ebfedea0SLionel Sambuc  * The AES-128 cipher type
1137ebfedea0SLionel Sambuc  *
1138ebfedea0SLionel Sambuc  * @return the AES-128 EVP_CIPHER pointer.
1139ebfedea0SLionel Sambuc  *
1140ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1141ebfedea0SLionel Sambuc  */
1142ebfedea0SLionel Sambuc 
1143ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_aes_128_cbc(void)1144ebfedea0SLionel Sambuc EVP_aes_128_cbc(void)
1145ebfedea0SLionel Sambuc {
1146ebfedea0SLionel Sambuc     hcrypto_validate();
1147ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cbc);
1148ebfedea0SLionel Sambuc }
1149ebfedea0SLionel Sambuc 
1150ebfedea0SLionel Sambuc /**
1151ebfedea0SLionel Sambuc  * The AES-192 cipher type
1152ebfedea0SLionel Sambuc  *
1153ebfedea0SLionel Sambuc  * @return the AES-192 EVP_CIPHER pointer.
1154ebfedea0SLionel Sambuc  *
1155ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1156ebfedea0SLionel Sambuc  */
1157ebfedea0SLionel Sambuc 
1158ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_aes_192_cbc(void)1159ebfedea0SLionel Sambuc EVP_aes_192_cbc(void)
1160ebfedea0SLionel Sambuc {
1161ebfedea0SLionel Sambuc     hcrypto_validate();
1162ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cbc);
1163ebfedea0SLionel Sambuc }
1164ebfedea0SLionel Sambuc 
1165ebfedea0SLionel Sambuc /**
1166ebfedea0SLionel Sambuc  * The AES-256 cipher type
1167ebfedea0SLionel Sambuc  *
1168ebfedea0SLionel Sambuc  * @return the AES-256 EVP_CIPHER pointer.
1169ebfedea0SLionel Sambuc  *
1170ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1171ebfedea0SLionel Sambuc  */
1172ebfedea0SLionel Sambuc 
1173ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_aes_256_cbc(void)1174ebfedea0SLionel Sambuc EVP_aes_256_cbc(void)
1175ebfedea0SLionel Sambuc {
1176ebfedea0SLionel Sambuc     hcrypto_validate();
1177ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cbc);
1178ebfedea0SLionel Sambuc }
1179ebfedea0SLionel Sambuc 
1180ebfedea0SLionel Sambuc /**
1181ebfedea0SLionel Sambuc  * The AES-128 cipher type
1182ebfedea0SLionel Sambuc  *
1183ebfedea0SLionel Sambuc  * @return the AES-128 EVP_CIPHER pointer.
1184ebfedea0SLionel Sambuc  *
1185ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1186ebfedea0SLionel Sambuc  */
1187ebfedea0SLionel Sambuc 
1188ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_aes_128_cfb8(void)1189ebfedea0SLionel Sambuc EVP_aes_128_cfb8(void)
1190ebfedea0SLionel Sambuc {
1191ebfedea0SLionel Sambuc     hcrypto_validate();
1192ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cfb8);
1193ebfedea0SLionel Sambuc }
1194ebfedea0SLionel Sambuc 
1195ebfedea0SLionel Sambuc /**
1196ebfedea0SLionel Sambuc  * The AES-192 cipher type
1197ebfedea0SLionel Sambuc  *
1198ebfedea0SLionel Sambuc  * @return the AES-192 EVP_CIPHER pointer.
1199ebfedea0SLionel Sambuc  *
1200ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1201ebfedea0SLionel Sambuc  */
1202ebfedea0SLionel Sambuc 
1203ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_aes_192_cfb8(void)1204ebfedea0SLionel Sambuc EVP_aes_192_cfb8(void)
1205ebfedea0SLionel Sambuc {
1206ebfedea0SLionel Sambuc     hcrypto_validate();
1207ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cfb8);
1208ebfedea0SLionel Sambuc }
1209ebfedea0SLionel Sambuc 
1210ebfedea0SLionel Sambuc /**
1211ebfedea0SLionel Sambuc  * The AES-256 cipher type
1212ebfedea0SLionel Sambuc  *
1213ebfedea0SLionel Sambuc  * @return the AES-256 EVP_CIPHER pointer.
1214ebfedea0SLionel Sambuc  *
1215ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1216ebfedea0SLionel Sambuc  */
1217ebfedea0SLionel Sambuc 
1218ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_aes_256_cfb8(void)1219ebfedea0SLionel Sambuc EVP_aes_256_cfb8(void)
1220ebfedea0SLionel Sambuc {
1221ebfedea0SLionel Sambuc     hcrypto_validate();
1222ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cfb8);
1223ebfedea0SLionel Sambuc }
1224ebfedea0SLionel Sambuc 
1225ebfedea0SLionel Sambuc /**
1226ebfedea0SLionel Sambuc  * The Camellia-128 cipher type
1227ebfedea0SLionel Sambuc  *
1228ebfedea0SLionel Sambuc  * @return the Camellia-128 EVP_CIPHER pointer.
1229ebfedea0SLionel Sambuc  *
1230ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1231ebfedea0SLionel Sambuc  */
1232ebfedea0SLionel Sambuc 
1233ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_camellia_128_cbc(void)1234ebfedea0SLionel Sambuc EVP_camellia_128_cbc(void)
1235ebfedea0SLionel Sambuc {
1236ebfedea0SLionel Sambuc     hcrypto_validate();
1237ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_128_cbc);
1238ebfedea0SLionel Sambuc }
1239ebfedea0SLionel Sambuc 
1240ebfedea0SLionel Sambuc /**
1241ebfedea0SLionel Sambuc  * The Camellia-198 cipher type
1242ebfedea0SLionel Sambuc  *
1243ebfedea0SLionel Sambuc  * @return the Camellia-198 EVP_CIPHER pointer.
1244ebfedea0SLionel Sambuc  *
1245ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1246ebfedea0SLionel Sambuc  */
1247ebfedea0SLionel Sambuc 
1248ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_camellia_192_cbc(void)1249ebfedea0SLionel Sambuc EVP_camellia_192_cbc(void)
1250ebfedea0SLionel Sambuc {
1251ebfedea0SLionel Sambuc     hcrypto_validate();
1252ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_192_cbc);
1253ebfedea0SLionel Sambuc }
1254ebfedea0SLionel Sambuc 
1255ebfedea0SLionel Sambuc /**
1256ebfedea0SLionel Sambuc  * The Camellia-256 cipher type
1257ebfedea0SLionel Sambuc  *
1258ebfedea0SLionel Sambuc  * @return the Camellia-256 EVP_CIPHER pointer.
1259ebfedea0SLionel Sambuc  *
1260ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1261ebfedea0SLionel Sambuc  */
1262ebfedea0SLionel Sambuc 
1263ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_camellia_256_cbc(void)1264ebfedea0SLionel Sambuc EVP_camellia_256_cbc(void)
1265ebfedea0SLionel Sambuc {
1266ebfedea0SLionel Sambuc     hcrypto_validate();
1267ebfedea0SLionel Sambuc     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_256_cbc);
1268ebfedea0SLionel Sambuc }
1269ebfedea0SLionel Sambuc 
1270ebfedea0SLionel Sambuc /*
1271ebfedea0SLionel Sambuc  *
1272ebfedea0SLionel Sambuc  */
1273ebfedea0SLionel Sambuc 
1274ebfedea0SLionel Sambuc static const struct cipher_name {
1275ebfedea0SLionel Sambuc     const char *name;
1276ebfedea0SLionel Sambuc     const EVP_CIPHER *(*func)(void);
1277ebfedea0SLionel Sambuc } cipher_name[] = {
1278ebfedea0SLionel Sambuc     { "des-ede3-cbc", EVP_des_ede3_cbc },
1279ebfedea0SLionel Sambuc     { "aes-128-cbc", EVP_aes_128_cbc },
1280ebfedea0SLionel Sambuc     { "aes-192-cbc", EVP_aes_192_cbc },
1281ebfedea0SLionel Sambuc     { "aes-256-cbc", EVP_aes_256_cbc },
1282ebfedea0SLionel Sambuc     { "aes-128-cfb8", EVP_aes_128_cfb8 },
1283ebfedea0SLionel Sambuc     { "aes-192-cfb8", EVP_aes_192_cfb8 },
1284ebfedea0SLionel Sambuc     { "aes-256-cfb8", EVP_aes_256_cfb8 },
1285ebfedea0SLionel Sambuc     { "camellia-128-cbc", EVP_camellia_128_cbc },
1286ebfedea0SLionel Sambuc     { "camellia-192-cbc", EVP_camellia_192_cbc },
1287ebfedea0SLionel Sambuc     { "camellia-256-cbc", EVP_camellia_256_cbc }
1288ebfedea0SLionel Sambuc };
1289ebfedea0SLionel Sambuc 
1290ebfedea0SLionel Sambuc /**
1291ebfedea0SLionel Sambuc  * Get the cipher type using their name.
1292ebfedea0SLionel Sambuc  *
1293ebfedea0SLionel Sambuc  * @param name the name of the cipher.
1294ebfedea0SLionel Sambuc  *
1295ebfedea0SLionel Sambuc  * @return the selected EVP_CIPHER pointer or NULL if not found.
1296ebfedea0SLionel Sambuc  *
1297ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1298ebfedea0SLionel Sambuc  */
1299ebfedea0SLionel Sambuc 
1300ebfedea0SLionel Sambuc const EVP_CIPHER *
EVP_get_cipherbyname(const char * name)1301ebfedea0SLionel Sambuc EVP_get_cipherbyname(const char *name)
1302ebfedea0SLionel Sambuc {
1303ebfedea0SLionel Sambuc     int i;
1304ebfedea0SLionel Sambuc     for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
1305ebfedea0SLionel Sambuc 	if (strcasecmp(cipher_name[i].name, name) == 0)
1306ebfedea0SLionel Sambuc 	    return (*cipher_name[i].func)();
1307ebfedea0SLionel Sambuc     }
1308ebfedea0SLionel Sambuc     return NULL;
1309ebfedea0SLionel Sambuc }
1310ebfedea0SLionel Sambuc 
1311ebfedea0SLionel Sambuc 
1312ebfedea0SLionel Sambuc /*
1313ebfedea0SLionel Sambuc  *
1314ebfedea0SLionel Sambuc  */
1315ebfedea0SLionel Sambuc 
1316ebfedea0SLionel Sambuc #ifndef min
1317ebfedea0SLionel Sambuc #define min(a,b) (((a)>(b))?(b):(a))
1318ebfedea0SLionel Sambuc #endif
1319ebfedea0SLionel Sambuc 
1320ebfedea0SLionel Sambuc /**
1321ebfedea0SLionel Sambuc  * Provides a legancy string to key function, used in PEM files.
1322ebfedea0SLionel Sambuc  *
1323ebfedea0SLionel Sambuc  * New protocols should use new string to key functions like NIST
1324ebfedea0SLionel Sambuc  * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
1325ebfedea0SLionel Sambuc  *
1326ebfedea0SLionel Sambuc  * @param type type of cipher to use
1327ebfedea0SLionel Sambuc  * @param md message digest to use
1328ebfedea0SLionel Sambuc  * @param salt salt salt string, should be an binary 8 byte buffer.
1329ebfedea0SLionel Sambuc  * @param data the password/input key string.
1330ebfedea0SLionel Sambuc  * @param datalen length of data parameter.
1331ebfedea0SLionel Sambuc  * @param count iteration counter.
1332ebfedea0SLionel Sambuc  * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
1333ebfedea0SLionel Sambuc  * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
1334ebfedea0SLionel Sambuc  *
1335ebfedea0SLionel Sambuc  * @return the size of derived key.
1336ebfedea0SLionel Sambuc  *
1337ebfedea0SLionel Sambuc  * @ingroup hcrypto_evp
1338ebfedea0SLionel Sambuc  */
1339ebfedea0SLionel Sambuc 
1340ebfedea0SLionel Sambuc int
EVP_BytesToKey(const EVP_CIPHER * type,const EVP_MD * md,const void * salt,const void * data,size_t datalen,unsigned int count,void * keydata,void * ivdata)1341ebfedea0SLionel Sambuc EVP_BytesToKey(const EVP_CIPHER *type,
1342ebfedea0SLionel Sambuc 	       const EVP_MD *md,
1343ebfedea0SLionel Sambuc 	       const void *salt,
1344ebfedea0SLionel Sambuc 	       const void *data, size_t datalen,
1345ebfedea0SLionel Sambuc 	       unsigned int count,
1346ebfedea0SLionel Sambuc 	       void *keydata,
1347ebfedea0SLionel Sambuc 	       void *ivdata)
1348ebfedea0SLionel Sambuc {
1349ebfedea0SLionel Sambuc     unsigned int ivlen, keylen;
1350ebfedea0SLionel Sambuc     int first = 0;
1351ebfedea0SLionel Sambuc     unsigned int mds = 0, i;
1352ebfedea0SLionel Sambuc     unsigned char *key = keydata;
1353ebfedea0SLionel Sambuc     unsigned char *iv = ivdata;
1354ebfedea0SLionel Sambuc     unsigned char *buf;
1355ebfedea0SLionel Sambuc     EVP_MD_CTX c;
1356ebfedea0SLionel Sambuc 
1357ebfedea0SLionel Sambuc     keylen = EVP_CIPHER_key_length(type);
1358ebfedea0SLionel Sambuc     ivlen = EVP_CIPHER_iv_length(type);
1359ebfedea0SLionel Sambuc 
1360ebfedea0SLionel Sambuc     if (data == NULL)
1361ebfedea0SLionel Sambuc 	return keylen;
1362ebfedea0SLionel Sambuc 
1363ebfedea0SLionel Sambuc     buf = malloc(EVP_MD_size(md));
1364ebfedea0SLionel Sambuc     if (buf == NULL)
1365ebfedea0SLionel Sambuc 	return -1;
1366ebfedea0SLionel Sambuc 
1367ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&c);
1368ebfedea0SLionel Sambuc 
1369ebfedea0SLionel Sambuc     first = 1;
1370ebfedea0SLionel Sambuc     while (1) {
1371ebfedea0SLionel Sambuc 	EVP_DigestInit_ex(&c, md, NULL);
1372ebfedea0SLionel Sambuc 	if (!first)
1373ebfedea0SLionel Sambuc 	    EVP_DigestUpdate(&c, buf, mds);
1374ebfedea0SLionel Sambuc 	first = 0;
1375ebfedea0SLionel Sambuc 	EVP_DigestUpdate(&c,data,datalen);
1376ebfedea0SLionel Sambuc 
1377ebfedea0SLionel Sambuc #define PKCS5_SALT_LEN 8
1378ebfedea0SLionel Sambuc 
1379ebfedea0SLionel Sambuc 	if (salt)
1380ebfedea0SLionel Sambuc 	    EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
1381ebfedea0SLionel Sambuc 
1382ebfedea0SLionel Sambuc 	EVP_DigestFinal_ex(&c, buf, &mds);
1383ebfedea0SLionel Sambuc 	assert(mds == EVP_MD_size(md));
1384ebfedea0SLionel Sambuc 
1385ebfedea0SLionel Sambuc 	for (i = 1; i < count; i++) {
1386ebfedea0SLionel Sambuc 	    EVP_DigestInit_ex(&c, md, NULL);
1387ebfedea0SLionel Sambuc 	    EVP_DigestUpdate(&c, buf, mds);
1388ebfedea0SLionel Sambuc 	    EVP_DigestFinal_ex(&c, buf, &mds);
1389ebfedea0SLionel Sambuc 	    assert(mds == EVP_MD_size(md));
1390ebfedea0SLionel Sambuc 	}
1391ebfedea0SLionel Sambuc 
1392ebfedea0SLionel Sambuc 	i = 0;
1393ebfedea0SLionel Sambuc 	if (keylen) {
1394ebfedea0SLionel Sambuc 	    size_t sz = min(keylen, mds);
1395ebfedea0SLionel Sambuc 	    if (key) {
1396ebfedea0SLionel Sambuc 		memcpy(key, buf, sz);
1397ebfedea0SLionel Sambuc 		key += sz;
1398ebfedea0SLionel Sambuc 	    }
1399ebfedea0SLionel Sambuc 	    keylen -= sz;
1400ebfedea0SLionel Sambuc 	    i += sz;
1401ebfedea0SLionel Sambuc 	}
1402ebfedea0SLionel Sambuc 	if (ivlen && mds > i) {
1403ebfedea0SLionel Sambuc 	    size_t sz = min(ivlen, (mds - i));
1404ebfedea0SLionel Sambuc 	    if (iv) {
1405ebfedea0SLionel Sambuc 		memcpy(iv, &buf[i], sz);
1406ebfedea0SLionel Sambuc 		iv += sz;
1407ebfedea0SLionel Sambuc 	    }
1408ebfedea0SLionel Sambuc 	    ivlen -= sz;
1409ebfedea0SLionel Sambuc 	}
1410ebfedea0SLionel Sambuc 	if (keylen == 0 && ivlen == 0)
1411ebfedea0SLionel Sambuc 	    break;
1412ebfedea0SLionel Sambuc     }
1413ebfedea0SLionel Sambuc 
1414ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&c);
1415ebfedea0SLionel Sambuc     free(buf);
1416ebfedea0SLionel Sambuc 
1417ebfedea0SLionel Sambuc     return EVP_CIPHER_key_length(type);
1418ebfedea0SLionel Sambuc }
1419ebfedea0SLionel Sambuc 
1420ebfedea0SLionel Sambuc /**
1421ebfedea0SLionel Sambuc  * Generate a random key for the specificed EVP_CIPHER.
1422ebfedea0SLionel Sambuc  *
1423ebfedea0SLionel Sambuc  * @param ctx EVP_CIPHER_CTX type to build the key for.
1424ebfedea0SLionel Sambuc  * @param key return key, must be at least EVP_CIPHER_key_length() byte long.
1425ebfedea0SLionel Sambuc  *
1426ebfedea0SLionel Sambuc  * @return 1 for success, 0 for failure.
1427ebfedea0SLionel Sambuc  *
1428ebfedea0SLionel Sambuc  * @ingroup hcrypto_core
1429ebfedea0SLionel Sambuc  */
1430ebfedea0SLionel Sambuc 
1431ebfedea0SLionel Sambuc int
EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX * ctx,void * key)1432ebfedea0SLionel Sambuc EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, void *key)
1433ebfedea0SLionel Sambuc {
1434ebfedea0SLionel Sambuc     if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1435ebfedea0SLionel Sambuc 	return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1436ebfedea0SLionel Sambuc     if (RAND_bytes(key, ctx->key_len) != 1)
1437ebfedea0SLionel Sambuc 	return 0;
1438ebfedea0SLionel Sambuc     return 1;
1439ebfedea0SLionel Sambuc }
1440ebfedea0SLionel Sambuc 
1441ebfedea0SLionel Sambuc /**
1442ebfedea0SLionel Sambuc  * Perform a operation on a ctx
1443ebfedea0SLionel Sambuc  *
1444ebfedea0SLionel Sambuc  * @param ctx context to perform operation on.
1445ebfedea0SLionel Sambuc  * @param type type of operation.
1446ebfedea0SLionel Sambuc  * @param arg argument to operation.
1447ebfedea0SLionel Sambuc  * @param data addition data to operation.
1448ebfedea0SLionel Sambuc 
1449ebfedea0SLionel Sambuc  * @return 1 for success, 0 for failure.
1450ebfedea0SLionel Sambuc  *
1451ebfedea0SLionel Sambuc  * @ingroup hcrypto_core
1452ebfedea0SLionel Sambuc  */
1453ebfedea0SLionel Sambuc 
1454ebfedea0SLionel Sambuc int
EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX * ctx,int type,int arg,void * data)1455ebfedea0SLionel Sambuc EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *data)
1456ebfedea0SLionel Sambuc {
1457ebfedea0SLionel Sambuc     if (ctx->cipher == NULL || ctx->cipher->ctrl == NULL)
1458ebfedea0SLionel Sambuc 	return 0;
1459ebfedea0SLionel Sambuc     return (*ctx->cipher->ctrl)(ctx, type, arg, data);
1460ebfedea0SLionel Sambuc }
1461ebfedea0SLionel Sambuc 
1462ebfedea0SLionel Sambuc /**
1463ebfedea0SLionel Sambuc  * Add all algorithms to the crypto core.
1464ebfedea0SLionel Sambuc  *
1465ebfedea0SLionel Sambuc  * @ingroup hcrypto_core
1466ebfedea0SLionel Sambuc  */
1467ebfedea0SLionel Sambuc 
1468ebfedea0SLionel Sambuc void
OpenSSL_add_all_algorithms(void)1469ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms(void)
1470ebfedea0SLionel Sambuc {
1471ebfedea0SLionel Sambuc     return;
1472ebfedea0SLionel Sambuc }
1473ebfedea0SLionel Sambuc 
1474ebfedea0SLionel Sambuc /**
1475ebfedea0SLionel Sambuc  * Add all algorithms to the crypto core using configuration file.
1476ebfedea0SLionel Sambuc  *
1477ebfedea0SLionel Sambuc  * @ingroup hcrypto_core
1478ebfedea0SLionel Sambuc  */
1479ebfedea0SLionel Sambuc 
1480ebfedea0SLionel Sambuc void
OpenSSL_add_all_algorithms_conf(void)1481ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms_conf(void)
1482ebfedea0SLionel Sambuc {
1483ebfedea0SLionel Sambuc     return;
1484ebfedea0SLionel Sambuc }
1485ebfedea0SLionel Sambuc 
1486ebfedea0SLionel Sambuc /**
1487ebfedea0SLionel Sambuc  * Add all algorithms to the crypto core, but don't use the
1488ebfedea0SLionel Sambuc  * configuration file.
1489ebfedea0SLionel Sambuc  *
1490ebfedea0SLionel Sambuc  * @ingroup hcrypto_core
1491ebfedea0SLionel Sambuc  */
1492ebfedea0SLionel Sambuc 
1493ebfedea0SLionel Sambuc void
OpenSSL_add_all_algorithms_noconf(void)1494ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms_noconf(void)
1495ebfedea0SLionel Sambuc {
1496ebfedea0SLionel Sambuc     return;
1497ebfedea0SLionel Sambuc }
1498