dig_eng.c revision 194206
112891Swpaul/* crypto/evp/digest.c */
212891Swpaul/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
312891Swpaul * All rights reserved.
412891Swpaul *
512891Swpaul * This package is an SSL implementation written
612891Swpaul * by Eric Young (eay@cryptsoft.com).
712891Swpaul * The implementation was written so as to conform with Netscapes SSL.
812891Swpaul *
912891Swpaul * This library is free for commercial and non-commercial use as long as
1012891Swpaul * the following conditions are aheared to.  The following conditions
1112891Swpaul * apply to all code found in this distribution, be it the RC4, RSA,
1212891Swpaul * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1312891Swpaul * included with this distribution is covered by the same copyright terms
1412891Swpaul * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1512891Swpaul *
1612891Swpaul * Copyright remains Eric Young's, and as such any Copyright notices in
1712891Swpaul * the code are not to be removed.
1812891Swpaul * If this package is used in a product, Eric Young should be given attribution
1912891Swpaul * as the author of the parts of the library used.
2012891Swpaul * This can be in the form of a textual message at program startup or
2112891Swpaul * in documentation (online or textual) provided with the package.
2212891Swpaul *
2312891Swpaul * Redistribution and use in source and binary forms, with or without
2412891Swpaul * modification, are permitted provided that the following conditions
2512891Swpaul * are met:
2612891Swpaul * 1. Redistributions of source code must retain the copyright
2712891Swpaul *    notice, this list of conditions and the following disclaimer.
2812891Swpaul * 2. Redistributions in binary form must reproduce the above copyright
2912891Swpaul *    notice, this list of conditions and the following disclaimer in the
3012891Swpaul *    documentation and/or other materials provided with the distribution.
3112891Swpaul * 3. All advertising materials mentioning features or use of this software
3212891Swpaul *    must display the following acknowledgement:
3312891Swpaul *    "This product includes cryptographic software written by
3430827Scharnier *     Eric Young (eay@cryptsoft.com)"
3530827Scharnier *    The word 'cryptographic' can be left out if the rouines from the library
3630827Scharnier *    being used are not cryptographic related :-).
3730827Scharnier * 4. If you include any Windows specific code (or a derivative thereof) from
3830827Scharnier *    the apps directory (application code) you must include an acknowledgement:
3920818Swpaul *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4012891Swpaul *
4130827Scharnier * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4230827Scharnier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4312891Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4412891Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4512891Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4612891Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4712891Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4812891Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4912891Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5012997Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5112891Swpaul * SUCH DAMAGE.
5212891Swpaul *
5312891Swpaul * The licence and distribution terms for any publically available version or
5412891Swpaul * derivative of this code cannot be changed.  i.e. this code cannot simply be
5528042Swpaul * copied and put under another distribution licence
5628042Swpaul * [including the GNU Public Licence.]
5728042Swpaul */
5828042Swpaul/* ====================================================================
5928042Swpaul * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
6014262Swpaul *
6114262Swpaul * Redistribution and use in source and binary forms, with or without
6214262Swpaul * modification, are permitted provided that the following conditions
6314262Swpaul * are met:
6412891Swpaul *
6512891Swpaul * 1. Redistributions of source code must retain the above copyright
6612891Swpaul *    notice, this list of conditions and the following disclaimer.
6712891Swpaul *
6812891Swpaul * 2. Redistributions in binary form must reproduce the above copyright
6912891Swpaul *    notice, this list of conditions and the following disclaimer in
7019161Swpaul *    the documentation and/or other materials provided with the
7119161Swpaul *    distribution.
7219161Swpaul *
7312891Swpaul * 3. All advertising materials mentioning features or use of this
7419161Swpaul *    software must display the following acknowledgment:
7512891Swpaul *    "This product includes software developed by the OpenSSL Project
7612891Swpaul *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
7712891Swpaul *
7812891Swpaul * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
7912891Swpaul *    endorse or promote products derived from this software without
8012891Swpaul *    prior written permission. For written permission, please contact
8112891Swpaul *    openssl-core@openssl.org.
8212891Swpaul *
8312891Swpaul * 5. Products derived from this software may not be called "OpenSSL"
8412891Swpaul *    nor may "OpenSSL" appear in their names without prior written
8512891Swpaul *    permission of the OpenSSL Project.
8612891Swpaul *
8719161Swpaul * 6. Redistributions of any form whatsoever must retain the following
8819161Swpaul *    acknowledgment:
8919161Swpaul *    "This product includes software developed by the OpenSSL Project
9012891Swpaul *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
9119161Swpaul *
9212891Swpaul * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
9312891Swpaul * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9412891Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9512891Swpaul * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
9612891Swpaul * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9712891Swpaul * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9812891Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9912891Swpaul * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10012891Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10112891Swpaul * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10212891Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10312891Swpaul * OF THE POSSIBILITY OF SUCH DAMAGE.
10412891Swpaul * ====================================================================
10512891Swpaul *
10612891Swpaul * This product includes cryptographic software written by Eric Young
10712891Swpaul * (eay@cryptsoft.com).  This product includes software written by Tim
10812891Swpaul * Hudson (tjh@cryptsoft.com).
10919161Swpaul *
11019161Swpaul */
11119161Swpaul
11212891Swpaul#include <stdio.h>
11319161Swpaul#include "cryptlib.h"
11412891Swpaul#include <openssl/objects.h>
11512891Swpaul#include <openssl/evp.h>
11612891Swpaul#ifndef OPENSSL_NO_ENGINE
11712891Swpaul#include <openssl/engine.h>
11812891Swpaul#endif
11912891Swpaul#include "evp_locl.h"
12012891Swpaul
12112891Swpaul#ifndef OPENSSL_NO_ENGINE
12212891Swpaul
12312891Swpaul#ifdef OPENSSL_FIPS
12412891Swpaul
12512891Swpaulstatic int do_evp_md_engine_full(EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl)
12612891Swpaul	{
12712891Swpaul	if (*ptype)
12812891Swpaul		{
12914304Swpaul		/* Ensure an ENGINE left lying around from last time is cleared
13014304Swpaul		 * (the previous check attempted to avoid this if the same
13119161Swpaul		 * ENGINE and EVP_MD could be used). */
13219161Swpaul		if(ctx->engine)
13319161Swpaul			ENGINE_finish(ctx->engine);
13419161Swpaul		if(impl)
13512891Swpaul			{
13619161Swpaul			if (!ENGINE_init(impl))
13712891Swpaul				{
13812891Swpaul				EVPerr(EVP_F_DO_EVP_MD_ENGINE_FULL,EVP_R_INITIALIZATION_ERROR);
13912891Swpaul				return 0;
14012891Swpaul				}
14112891Swpaul			}
14212891Swpaul		else
14312891Swpaul			/* Ask if an ENGINE is reserved for this job */
14412891Swpaul			impl = ENGINE_get_digest_engine((*ptype)->type);
14512891Swpaul		if(impl)
14620818Swpaul			{
14720818Swpaul			/* There's an ENGINE for this job ... (apparently) */
14820818Swpaul			const EVP_MD *d = ENGINE_get_digest(impl, (*ptype)->type);
14912891Swpaul			if(!d)
15012891Swpaul				{
15120818Swpaul				/* Same comment from evp_enc.c */
15220818Swpaul				EVPerr(EVP_F_DO_EVP_MD_ENGINE_FULL,EVP_R_INITIALIZATION_ERROR);
15312891Swpaul				return 0;
15412891Swpaul				}
15512891Swpaul			/* We'll use the ENGINE's private digest definition */
15612891Swpaul			*ptype = d;
15719161Swpaul			/* Store the ENGINE functional reference so we know
15819161Swpaul			 * 'type' came from an ENGINE and we need to release
15919161Swpaul			 * it when done. */
16019161Swpaul			ctx->engine = impl;
16119161Swpaul			}
16212891Swpaul		else
16319161Swpaul			ctx->engine = NULL;
16427589Swpaul		}
16520818Swpaul	else
16620818Swpaul	if(!ctx->digest)
16727589Swpaul		{
16827589Swpaul		EVPerr(EVP_F_DO_EVP_MD_ENGINE_FULL,EVP_R_NO_DIGEST_SET);
16912891Swpaul		return 0;
17012891Swpaul		}
17130827Scharnier	return 1;
17212891Swpaul	}
17312891Swpaul
17427589Swpaulvoid int_EVP_MD_init_engine_callbacks(void)
17512891Swpaul	{
17627589Swpaul	int_EVP_MD_set_engine_callbacks(
17712891Swpaul		ENGINE_init, ENGINE_finish, do_evp_md_engine_full);
17820818Swpaul	}
17920818Swpaul#endif
18012891Swpaul#endif
18112891Swpaul