155714Skris/* crypto/evp/digest.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
58109998Smarkm/* ====================================================================
59109998Smarkm * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60109998Smarkm *
61109998Smarkm * Redistribution and use in source and binary forms, with or without
62109998Smarkm * modification, are permitted provided that the following conditions
63109998Smarkm * are met:
64109998Smarkm *
65109998Smarkm * 1. Redistributions of source code must retain the above copyright
66109998Smarkm *    notice, this list of conditions and the following disclaimer.
67109998Smarkm *
68109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
69109998Smarkm *    notice, this list of conditions and the following disclaimer in
70109998Smarkm *    the documentation and/or other materials provided with the
71109998Smarkm *    distribution.
72109998Smarkm *
73109998Smarkm * 3. All advertising materials mentioning features or use of this
74109998Smarkm *    software must display the following acknowledgment:
75109998Smarkm *    "This product includes software developed by the OpenSSL Project
76109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77109998Smarkm *
78109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79109998Smarkm *    endorse or promote products derived from this software without
80109998Smarkm *    prior written permission. For written permission, please contact
81109998Smarkm *    openssl-core@openssl.org.
82109998Smarkm *
83109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
84109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
85109998Smarkm *    permission of the OpenSSL Project.
86109998Smarkm *
87109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
88109998Smarkm *    acknowledgment:
89109998Smarkm *    "This product includes software developed by the OpenSSL Project
90109998Smarkm *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91109998Smarkm *
92109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
104109998Smarkm * ====================================================================
105109998Smarkm *
106109998Smarkm * This product includes cryptographic software written by Eric Young
107109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
108109998Smarkm * Hudson (tjh@cryptsoft.com).
109109998Smarkm *
110109998Smarkm */
11155714Skris
11255714Skris#include <stdio.h>
11355714Skris#include "cryptlib.h"
11455714Skris#include <openssl/objects.h>
11555714Skris#include <openssl/evp.h>
116111147Snectar#ifndef OPENSSL_NO_ENGINE
117109998Smarkm#include <openssl/engine.h>
118111147Snectar#endif
11955714Skris
120238405Sjkim#ifdef OPENSSL_FIPS
121238405Sjkim#include <openssl/fips.h>
122238405Sjkim#endif
123238405Sjkim
124109998Smarkmvoid EVP_MD_CTX_init(EVP_MD_CTX *ctx)
12555714Skris	{
126109998Smarkm	memset(ctx,'\0',sizeof *ctx);
12755714Skris	}
12855714Skris
129109998SmarkmEVP_MD_CTX *EVP_MD_CTX_create(void)
130109998Smarkm	{
131109998Smarkm	EVP_MD_CTX *ctx=OPENSSL_malloc(sizeof *ctx);
132109998Smarkm
133205128Ssimon	if (ctx)
134205128Ssimon		EVP_MD_CTX_init(ctx);
135109998Smarkm
136109998Smarkm	return ctx;
137109998Smarkm	}
138109998Smarkm
139109998Smarkmint EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
140109998Smarkm	{
141109998Smarkm	EVP_MD_CTX_init(ctx);
142109998Smarkm	return EVP_DigestInit_ex(ctx, type, NULL);
143109998Smarkm	}
144109998Smarkm
145238405Sjkimint EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
146109998Smarkm	{
147238405Sjkim	EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED);
148111147Snectar#ifndef OPENSSL_NO_ENGINE
149238405Sjkim	/* Whether it's nice or not, "Inits" can be used on "Final"'d contexts
150238405Sjkim	 * so this context may already have an ENGINE! Try to avoid releasing
151238405Sjkim	 * the previous handle, re-querying for an ENGINE, and having a
152238405Sjkim	 * reinitialisation, when it may all be unecessary. */
153238405Sjkim	if (ctx->engine && ctx->digest && (!type ||
154238405Sjkim			(type && (type->type == ctx->digest->type))))
155238405Sjkim		goto skip_to_init;
156238405Sjkim	if (type)
157109998Smarkm		{
158109998Smarkm		/* Ensure an ENGINE left lying around from last time is cleared
159109998Smarkm		 * (the previous check attempted to avoid this if the same
160109998Smarkm		 * ENGINE and EVP_MD could be used). */
161109998Smarkm		if(ctx->engine)
162109998Smarkm			ENGINE_finish(ctx->engine);
163109998Smarkm		if(impl)
164109998Smarkm			{
165109998Smarkm			if (!ENGINE_init(impl))
166109998Smarkm				{
167238405Sjkim				EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR);
168109998Smarkm				return 0;
169109998Smarkm				}
170109998Smarkm			}
171109998Smarkm		else
172109998Smarkm			/* Ask if an ENGINE is reserved for this job */
173238405Sjkim			impl = ENGINE_get_digest_engine(type->type);
174109998Smarkm		if(impl)
175109998Smarkm			{
176109998Smarkm			/* There's an ENGINE for this job ... (apparently) */
177238405Sjkim			const EVP_MD *d = ENGINE_get_digest(impl, type->type);
178109998Smarkm			if(!d)
179109998Smarkm				{
180109998Smarkm				/* Same comment from evp_enc.c */
181238405Sjkim				EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR);
182206046Ssimon				ENGINE_finish(impl);
183109998Smarkm				return 0;
184109998Smarkm				}
185109998Smarkm			/* We'll use the ENGINE's private digest definition */
186238405Sjkim			type = d;
187109998Smarkm			/* Store the ENGINE functional reference so we know
188109998Smarkm			 * 'type' came from an ENGINE and we need to release
189109998Smarkm			 * it when done. */
190109998Smarkm			ctx->engine = impl;
191109998Smarkm			}
192109998Smarkm		else
193109998Smarkm			ctx->engine = NULL;
194109998Smarkm		}
195111147Snectar	else
196111147Snectar	if(!ctx->digest)
197109998Smarkm		{
198238405Sjkim		EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_NO_DIGEST_SET);
199109998Smarkm		return 0;
200109998Smarkm		}
201120631Snectar#endif
202109998Smarkm	if (ctx->digest != type)
203109998Smarkm		{
204109998Smarkm		if (ctx->digest && ctx->digest->ctx_size)
205109998Smarkm			OPENSSL_free(ctx->md_data);
206109998Smarkm		ctx->digest=type;
207238405Sjkim		if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size)
208205128Ssimon			{
209238405Sjkim			ctx->update = type->update;
210109998Smarkm			ctx->md_data=OPENSSL_malloc(type->ctx_size);
211238405Sjkim			if (ctx->md_data == NULL)
212205128Ssimon				{
213238405Sjkim				EVPerr(EVP_F_EVP_DIGESTINIT_EX,
214238405Sjkim							ERR_R_MALLOC_FAILURE);
215205128Ssimon				return 0;
216205128Ssimon				}
217205128Ssimon			}
218109998Smarkm		}
219111147Snectar#ifndef OPENSSL_NO_ENGINE
220238405Sjkimskip_to_init:
221111147Snectar#endif
222238405Sjkim	if (ctx->pctx)
223238405Sjkim		{
224238405Sjkim		int r;
225238405Sjkim		r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
226238405Sjkim					EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
227238405Sjkim		if (r <= 0 && (r != -2))
228238405Sjkim			return 0;
229238405Sjkim		}
230238405Sjkim	if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
231238405Sjkim		return 1;
232238405Sjkim#ifdef OPENSSL_FIPS
233238405Sjkim	if (FIPS_mode())
234238405Sjkim		{
235238405Sjkim		if (FIPS_digestinit(ctx, type))
236238405Sjkim			return 1;
237238405Sjkim		OPENSSL_free(ctx->md_data);
238238405Sjkim		ctx->md_data = NULL;
239238405Sjkim		return 0;
240238405Sjkim		}
241238405Sjkim#endif
242109998Smarkm	return ctx->digest->init(ctx);
243109998Smarkm	}
244109998Smarkm
245238405Sjkimint EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
24655714Skris	{
247194206Ssimon#ifdef OPENSSL_FIPS
248238405Sjkim	return FIPS_digestupdate(ctx, data, count);
249238405Sjkim#else
250238405Sjkim	return ctx->update(ctx,data,count);
251194206Ssimon#endif
25255714Skris	}
25355714Skris
254109998Smarkm/* The caller can assume that this removes any secret data from the context */
255109998Smarkmint EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
25655714Skris	{
257109998Smarkm	int ret;
258109998Smarkm	ret = EVP_DigestFinal_ex(ctx, md, size);
259109998Smarkm	EVP_MD_CTX_cleanup(ctx);
260109998Smarkm	return ret;
261109998Smarkm	}
262109998Smarkm
263109998Smarkm/* The caller can assume that this removes any secret data from the context */
264109998Smarkmint EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
265109998Smarkm	{
266238405Sjkim#ifdef OPENSSL_FIPS
267238405Sjkim	return FIPS_digestfinal(ctx, md, size);
268238405Sjkim#else
269109998Smarkm	int ret;
270246772Sjkim
271109998Smarkm	OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
272109998Smarkm	ret=ctx->digest->final(ctx,md);
27355714Skris	if (size != NULL)
27455714Skris		*size=ctx->digest->md_size;
275109998Smarkm	if (ctx->digest->cleanup)
276109998Smarkm		{
277109998Smarkm		ctx->digest->cleanup(ctx);
278238405Sjkim		EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED);
279109998Smarkm		}
280109998Smarkm	memset(ctx->md_data,0,ctx->digest->ctx_size);
281109998Smarkm	return ret;
282238405Sjkim#endif
28355714Skris	}
28455714Skris
285109998Smarkmint EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
286109998Smarkm	{
287109998Smarkm	EVP_MD_CTX_init(out);
288109998Smarkm	return EVP_MD_CTX_copy_ex(out, in);
289109998Smarkm	}
290109998Smarkm
291109998Smarkmint EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
292109998Smarkm	{
293127128Snectar	unsigned char *tmp_buf;
294109998Smarkm	if ((in == NULL) || (in->digest == NULL))
295109998Smarkm		{
296160814Ssimon		EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,EVP_R_INPUT_NOT_INITIALIZED);
297109998Smarkm		return 0;
298109998Smarkm		}
299111147Snectar#ifndef OPENSSL_NO_ENGINE
300109998Smarkm	/* Make sure it's safe to copy a digest context using an ENGINE */
301238405Sjkim	if (in->engine && !ENGINE_init(in->engine))
302109998Smarkm		{
303160814Ssimon		EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB);
304109998Smarkm		return 0;
305109998Smarkm		}
306111147Snectar#endif
307109998Smarkm
308127128Snectar	if (out->digest == in->digest)
309127128Snectar		{
310127128Snectar		tmp_buf = out->md_data;
311238405Sjkim	    	EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE);
312127128Snectar		}
313127128Snectar	else tmp_buf = NULL;
314109998Smarkm	EVP_MD_CTX_cleanup(out);
315109998Smarkm	memcpy(out,in,sizeof *out);
316109998Smarkm
317238405Sjkim	if (in->md_data && out->digest->ctx_size)
318109998Smarkm		{
319205128Ssimon		if (tmp_buf)
320205128Ssimon			out->md_data = tmp_buf;
321205128Ssimon		else
322205128Ssimon			{
323205128Ssimon			out->md_data=OPENSSL_malloc(out->digest->ctx_size);
324205128Ssimon			if (!out->md_data)
325205128Ssimon				{
326205128Ssimon				EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_MALLOC_FAILURE);
327205128Ssimon				return 0;
328205128Ssimon				}
329205128Ssimon			}
330109998Smarkm		memcpy(out->md_data,in->md_data,out->digest->ctx_size);
331109998Smarkm		}
332127128Snectar
333238405Sjkim	out->update = in->update;
334238405Sjkim
335238405Sjkim	if (in->pctx)
336238405Sjkim		{
337238405Sjkim		out->pctx = EVP_PKEY_CTX_dup(in->pctx);
338238405Sjkim		if (!out->pctx)
339238405Sjkim			{
340238405Sjkim			EVP_MD_CTX_cleanup(out);
341238405Sjkim			return 0;
342238405Sjkim			}
343238405Sjkim		}
344238405Sjkim
345109998Smarkm	if (out->digest->copy)
346109998Smarkm		return out->digest->copy(out,in);
347109998Smarkm
348109998Smarkm	return 1;
349109998Smarkm	}
350109998Smarkm
351160814Ssimonint EVP_Digest(const void *data, size_t count,
352109998Smarkm		unsigned char *md, unsigned int *size, const EVP_MD *type, ENGINE *impl)
353109998Smarkm	{
354109998Smarkm	EVP_MD_CTX ctx;
355109998Smarkm	int ret;
356109998Smarkm
357109998Smarkm	EVP_MD_CTX_init(&ctx);
358238405Sjkim	EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT);
359109998Smarkm	ret=EVP_DigestInit_ex(&ctx, type, impl)
360109998Smarkm	  && EVP_DigestUpdate(&ctx, data, count)
361109998Smarkm	  && EVP_DigestFinal_ex(&ctx, md, size);
362109998Smarkm	EVP_MD_CTX_cleanup(&ctx);
363109998Smarkm
364109998Smarkm	return ret;
365109998Smarkm	}
366109998Smarkm
367109998Smarkmvoid EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
368109998Smarkm	{
369279264Sdelphij	if (ctx)
370279264Sdelphij		{
371279264Sdelphij		EVP_MD_CTX_cleanup(ctx);
372279264Sdelphij		OPENSSL_free(ctx);
373279264Sdelphij		}
374109998Smarkm	}
375109998Smarkm
376109998Smarkm/* This call frees resources associated with the context */
377109998Smarkmint EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
378109998Smarkm	{
379238405Sjkim#ifndef OPENSSL_FIPS
380109998Smarkm	/* Don't assume ctx->md_data was cleaned in EVP_Digest_Final,
381109998Smarkm	 * because sometimes only copies of the context are ever finalised.
382109998Smarkm	 */
383109998Smarkm	if (ctx->digest && ctx->digest->cleanup
384238405Sjkim	    && !EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED))
385109998Smarkm		ctx->digest->cleanup(ctx);
386127128Snectar	if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
387238405Sjkim	    && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE))
388109998Smarkm		{
389109998Smarkm		OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size);
390109998Smarkm		OPENSSL_free(ctx->md_data);
391109998Smarkm		}
392238405Sjkim#endif
393238405Sjkim	if (ctx->pctx)
394238405Sjkim		EVP_PKEY_CTX_free(ctx->pctx);
395111147Snectar#ifndef OPENSSL_NO_ENGINE
396109998Smarkm	if(ctx->engine)
397109998Smarkm		/* The EVP_MD we used belongs to an ENGINE, release the
398109998Smarkm		 * functional reference we held for this reason. */
399238405Sjkim		ENGINE_finish(ctx->engine);
400111147Snectar#endif
401238405Sjkim#ifdef OPENSSL_FIPS
402238405Sjkim	FIPS_md_ctx_cleanup(ctx);
403238405Sjkim#endif
404109998Smarkm	memset(ctx,'\0',sizeof *ctx);
405109998Smarkm
406109998Smarkm	return 1;
407109998Smarkm	}
408