os/ossrv/ssl/libcrypto/src/crypto/evp/digest.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/evp/digest.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,353 @@
     1.4 +/* crypto/evp/digest.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +/* ====================================================================
    1.62 + * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
    1.63 + *
    1.64 + * Redistribution and use in source and binary forms, with or without
    1.65 + * modification, are permitted provided that the following conditions
    1.66 + * are met:
    1.67 + *
    1.68 + * 1. Redistributions of source code must retain the above copyright
    1.69 + *    notice, this list of conditions and the following disclaimer. 
    1.70 + *
    1.71 + * 2. Redistributions in binary form must reproduce the above copyright
    1.72 + *    notice, this list of conditions and the following disclaimer in
    1.73 + *    the documentation and/or other materials provided with the
    1.74 + *    distribution.
    1.75 + *
    1.76 + * 3. All advertising materials mentioning features or use of this
    1.77 + *    software must display the following acknowledgment:
    1.78 + *    "This product includes software developed by the OpenSSL Project
    1.79 + *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
    1.80 + *
    1.81 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.82 + *    endorse or promote products derived from this software without
    1.83 + *    prior written permission. For written permission, please contact
    1.84 + *    openssl-core@openssl.org.
    1.85 + *
    1.86 + * 5. Products derived from this software may not be called "OpenSSL"
    1.87 + *    nor may "OpenSSL" appear in their names without prior written
    1.88 + *    permission of the OpenSSL Project.
    1.89 + *
    1.90 + * 6. Redistributions of any form whatsoever must retain the following
    1.91 + *    acknowledgment:
    1.92 + *    "This product includes software developed by the OpenSSL Project
    1.93 + *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
    1.94 + *
    1.95 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.96 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.97 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.98 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.99 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   1.100 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   1.101 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   1.102 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   1.103 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   1.104 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   1.105 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   1.106 + * OF THE POSSIBILITY OF SUCH DAMAGE.
   1.107 + * ====================================================================
   1.108 + *
   1.109 + * This product includes cryptographic software written by Eric Young
   1.110 + * (eay@cryptsoft.com).  This product includes software written by Tim
   1.111 + * Hudson (tjh@cryptsoft.com).
   1.112 + *
   1.113 + */
   1.114 +
   1.115 +#include <stdio.h>
   1.116 +#include "cryptlib.h"
   1.117 +#include <openssl/objects.h>
   1.118 +#include <openssl/evp.h>
   1.119 +#ifndef OPENSSL_NO_ENGINE
   1.120 +#include <openssl/engine.h>
   1.121 +#endif
   1.122 +
   1.123 +EXPORT_C void EVP_MD_CTX_init(EVP_MD_CTX *ctx)
   1.124 +	{
   1.125 +	memset(ctx,'\0',sizeof *ctx);
   1.126 +	}
   1.127 +
   1.128 +EXPORT_C EVP_MD_CTX *EVP_MD_CTX_create(void)
   1.129 +	{
   1.130 +	EVP_MD_CTX *ctx=OPENSSL_malloc(sizeof *ctx);
   1.131 +    if(ctx==NULL)
   1.132 +	{
   1.133 +	   return ctx;
   1.134 +	}
   1.135 +	EVP_MD_CTX_init(ctx);
   1.136 +
   1.137 +	return ctx;
   1.138 +	}
   1.139 +
   1.140 +EXPORT_C int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
   1.141 +	{
   1.142 +	EVP_MD_CTX_init(ctx);
   1.143 +	return EVP_DigestInit_ex(ctx, type, NULL);
   1.144 +	}
   1.145 +
   1.146 +EXPORT_C int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
   1.147 +	{
   1.148 +	EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED);
   1.149 +#ifndef OPENSSL_NO_ENGINE
   1.150 +	/* Whether it's nice or not, "Inits" can be used on "Final"'d contexts
   1.151 +	 * so this context may already have an ENGINE! Try to avoid releasing
   1.152 +	 * the previous handle, re-querying for an ENGINE, and having a
   1.153 +	 * reinitialisation, when it may all be unecessary. */
   1.154 +	if (ctx->engine && ctx->digest && (!type ||
   1.155 +			(type && (type->type == ctx->digest->type))))
   1.156 +		goto skip_to_init;
   1.157 +	if (type)
   1.158 +		{
   1.159 +		/* Ensure an ENGINE left lying around from last time is cleared
   1.160 +		 * (the previous check attempted to avoid this if the same
   1.161 +		 * ENGINE and EVP_MD could be used). */
   1.162 +		if(ctx->engine)
   1.163 +			ENGINE_finish(ctx->engine);
   1.164 +		if(impl)
   1.165 +			{
   1.166 +			if (!ENGINE_init(impl))
   1.167 +				{
   1.168 +				EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR);
   1.169 +				return 0;
   1.170 +				}
   1.171 +			}
   1.172 +		else
   1.173 +			/* Ask if an ENGINE is reserved for this job */
   1.174 +			impl = ENGINE_get_digest_engine(type->type);
   1.175 +		if(impl)
   1.176 +			{
   1.177 +			/* There's an ENGINE for this job ... (apparently) */
   1.178 +			const EVP_MD *d = ENGINE_get_digest(impl, type->type);
   1.179 +			if(!d)
   1.180 +				{
   1.181 +				/* Same comment from evp_enc.c */
   1.182 +				EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR);
   1.183 +				return 0;
   1.184 +				}
   1.185 +			/* We'll use the ENGINE's private digest definition */
   1.186 +			type = d;
   1.187 +			/* Store the ENGINE functional reference so we know
   1.188 +			 * 'type' came from an ENGINE and we need to release
   1.189 +			 * it when done. */
   1.190 +			ctx->engine = impl;
   1.191 +			}
   1.192 +		else
   1.193 +			ctx->engine = NULL;
   1.194 +		}
   1.195 +	else
   1.196 +	if(!ctx->digest)
   1.197 +		{
   1.198 +		EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_NO_DIGEST_SET);
   1.199 +		return 0;
   1.200 +		}
   1.201 +#endif
   1.202 +	if (ctx->digest != type)
   1.203 +		{
   1.204 +		if (ctx->digest && ctx->digest->ctx_size)
   1.205 +		  { if(ctx->md_data)
   1.206 +			  OPENSSL_free(ctx->md_data);
   1.207 +		  }
   1.208 +		ctx->digest=type;
   1.209 +		if (type->ctx_size)
   1.210 +		{
   1.211 +			ctx->md_data=OPENSSL_malloc(type->ctx_size);
   1.212 +			if(ctx->md_data==NULL)
   1.213 +			{
   1.214 +			    EVPerr(EVP_F_EVP_DIGESTINIT_EX,ERR_R_MALLOC_FAILURE);
   1.215 +			    return 0;
   1.216 +			}
   1.217 +		}
   1.218 +
   1.219 +		}
   1.220 +#ifndef OPENSSL_NO_ENGINE
   1.221 +skip_to_init:
   1.222 +#endif
   1.223 +	return ctx->digest->init(ctx);
   1.224 +	}
   1.225 +
   1.226 +EXPORT_C int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data,
   1.227 +	     size_t count)
   1.228 +	{
   1.229 +	return ctx->digest->update(ctx,data,count);
   1.230 +	}
   1.231 +
   1.232 +/* The caller can assume that this removes any secret data from the context */
   1.233 +EXPORT_C int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
   1.234 +	{
   1.235 +	int ret;
   1.236 +	ret = EVP_DigestFinal_ex(ctx, md, size);
   1.237 +	EVP_MD_CTX_cleanup(ctx);
   1.238 +	return ret;
   1.239 +	}
   1.240 +
   1.241 +/* The caller can assume that this removes any secret data from the context */
   1.242 +EXPORT_C int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
   1.243 +	{
   1.244 +	int ret;
   1.245 +
   1.246 +	OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
   1.247 +	ret=ctx->digest->final(ctx,md);
   1.248 +	if (size != NULL)
   1.249 +		*size=ctx->digest->md_size;
   1.250 +	if (ctx->digest->cleanup)
   1.251 +		{
   1.252 +		ctx->digest->cleanup(ctx);
   1.253 +		EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED);
   1.254 +		}
   1.255 +	if(ctx->md_data)	
   1.256 +	memset(ctx->md_data,0,ctx->digest->ctx_size);
   1.257 +	return ret;
   1.258 +	}
   1.259 +
   1.260 +EXPORT_C int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
   1.261 +	{
   1.262 +	EVP_MD_CTX_init(out);
   1.263 +	return EVP_MD_CTX_copy_ex(out, in);
   1.264 +	}
   1.265 +
   1.266 +EXPORT_C int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
   1.267 +	{
   1.268 +	unsigned char *tmp_buf;
   1.269 +	if ((in == NULL) || (in->digest == NULL))
   1.270 +		{
   1.271 +		EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,EVP_R_INPUT_NOT_INITIALIZED);
   1.272 +		return 0;
   1.273 +		}
   1.274 +#ifndef OPENSSL_NO_ENGINE
   1.275 +	/* Make sure it's safe to copy a digest context using an ENGINE */
   1.276 +	if (in->engine && !ENGINE_init(in->engine))
   1.277 +		{
   1.278 +		EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB);
   1.279 +		return 0;
   1.280 +		}
   1.281 +#endif
   1.282 +
   1.283 +	if (out->digest == in->digest)
   1.284 +		{
   1.285 +		tmp_buf = out->md_data;
   1.286 +	    	EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE);
   1.287 +		}
   1.288 +	else tmp_buf = NULL;
   1.289 +	EVP_MD_CTX_cleanup(out);
   1.290 +	memcpy(out,in,sizeof *out);
   1.291 +
   1.292 +	if (out->digest->ctx_size)
   1.293 +		{
   1.294 +		 if (tmp_buf) out->md_data = tmp_buf;
   1.295 +		 else out->md_data=OPENSSL_malloc(out->digest->ctx_size);
   1.296 +     if(out->md_data==NULL||in->md_data==NULL)
   1.297 +        {
   1.298 +           EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_MALLOC_FAILURE);
   1.299 +		       return 0;
   1.300 +        } 
   1.301 +     memcpy(out->md_data,in->md_data,out->digest->ctx_size);
   1.302 +		}
   1.303 +
   1.304 +	if (out->digest->copy)
   1.305 +		return out->digest->copy(out,in);
   1.306 +	
   1.307 +	return 1;
   1.308 +	}
   1.309 +
   1.310 +EXPORT_C int EVP_Digest(const void *data, size_t count,
   1.311 +		unsigned char *md, unsigned int *size, const EVP_MD *type, ENGINE *impl)
   1.312 +	{
   1.313 +	EVP_MD_CTX ctx;
   1.314 +	int ret;
   1.315 +
   1.316 +	EVP_MD_CTX_init(&ctx);
   1.317 +	EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT);
   1.318 +	ret=EVP_DigestInit_ex(&ctx, type, impl)
   1.319 +	  && EVP_DigestUpdate(&ctx, data, count)
   1.320 +	  && EVP_DigestFinal_ex(&ctx, md, size);
   1.321 +	EVP_MD_CTX_cleanup(&ctx);
   1.322 +
   1.323 +	return ret;
   1.324 +	}
   1.325 +
   1.326 +EXPORT_C void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
   1.327 +	{
   1.328 +	EVP_MD_CTX_cleanup(ctx);
   1.329 +	OPENSSL_free(ctx);
   1.330 +	}
   1.331 +
   1.332 +/* This call frees resources associated with the context */
   1.333 +EXPORT_C int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
   1.334 +	{
   1.335 +	/* Don't assume ctx->md_data was cleaned in EVP_Digest_Final,
   1.336 +	 * because sometimes only copies of the context are ever finalised.
   1.337 +	 */
   1.338 +	if (ctx->digest && ctx->digest->cleanup
   1.339 +	    && !EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED))
   1.340 +		ctx->digest->cleanup(ctx);
   1.341 +	if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
   1.342 +	    && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE))
   1.343 +		{
   1.344 +		OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size);
   1.345 +		OPENSSL_free(ctx->md_data);
   1.346 +		}
   1.347 +#ifndef OPENSSL_NO_ENGINE
   1.348 +	if(ctx->engine)
   1.349 +		/* The EVP_MD we used belongs to an ENGINE, release the
   1.350 +		 * functional reference we held for this reason. */
   1.351 +		ENGINE_finish(ctx->engine);
   1.352 +#endif
   1.353 +	memset(ctx,'\0',sizeof *ctx);
   1.354 +
   1.355 +	return 1;
   1.356 +	}