sl@0: /* crypto/evp/digest.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: #include sl@0: #endif sl@0: sl@0: EXPORT_C void EVP_MD_CTX_init(EVP_MD_CTX *ctx) sl@0: { sl@0: memset(ctx,'\0',sizeof *ctx); sl@0: } sl@0: sl@0: EXPORT_C EVP_MD_CTX *EVP_MD_CTX_create(void) sl@0: { sl@0: EVP_MD_CTX *ctx=OPENSSL_malloc(sizeof *ctx); sl@0: if(ctx==NULL) sl@0: { sl@0: return ctx; sl@0: } sl@0: EVP_MD_CTX_init(ctx); sl@0: sl@0: return ctx; sl@0: } sl@0: sl@0: EXPORT_C int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) sl@0: { sl@0: EVP_MD_CTX_init(ctx); sl@0: return EVP_DigestInit_ex(ctx, type, NULL); sl@0: } sl@0: sl@0: EXPORT_C int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) sl@0: { sl@0: EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts sl@0: * so this context may already have an ENGINE! Try to avoid releasing sl@0: * the previous handle, re-querying for an ENGINE, and having a sl@0: * reinitialisation, when it may all be unecessary. */ sl@0: if (ctx->engine && ctx->digest && (!type || sl@0: (type && (type->type == ctx->digest->type)))) sl@0: goto skip_to_init; sl@0: if (type) sl@0: { sl@0: /* Ensure an ENGINE left lying around from last time is cleared sl@0: * (the previous check attempted to avoid this if the same sl@0: * ENGINE and EVP_MD could be used). */ sl@0: if(ctx->engine) sl@0: ENGINE_finish(ctx->engine); sl@0: if(impl) sl@0: { sl@0: if (!ENGINE_init(impl)) sl@0: { sl@0: EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR); sl@0: return 0; sl@0: } sl@0: } sl@0: else sl@0: /* Ask if an ENGINE is reserved for this job */ sl@0: impl = ENGINE_get_digest_engine(type->type); sl@0: if(impl) sl@0: { sl@0: /* There's an ENGINE for this job ... (apparently) */ sl@0: const EVP_MD *d = ENGINE_get_digest(impl, type->type); sl@0: if(!d) sl@0: { sl@0: /* Same comment from evp_enc.c */ sl@0: EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR); sl@0: return 0; sl@0: } sl@0: /* We'll use the ENGINE's private digest definition */ sl@0: type = d; sl@0: /* Store the ENGINE functional reference so we know sl@0: * 'type' came from an ENGINE and we need to release sl@0: * it when done. */ sl@0: ctx->engine = impl; sl@0: } sl@0: else sl@0: ctx->engine = NULL; sl@0: } sl@0: else sl@0: if(!ctx->digest) sl@0: { sl@0: EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_NO_DIGEST_SET); sl@0: return 0; sl@0: } sl@0: #endif sl@0: if (ctx->digest != type) sl@0: { sl@0: if (ctx->digest && ctx->digest->ctx_size) sl@0: { if(ctx->md_data) sl@0: OPENSSL_free(ctx->md_data); sl@0: } sl@0: ctx->digest=type; sl@0: if (type->ctx_size) sl@0: { sl@0: ctx->md_data=OPENSSL_malloc(type->ctx_size); sl@0: if(ctx->md_data==NULL) sl@0: { sl@0: EVPerr(EVP_F_EVP_DIGESTINIT_EX,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: } sl@0: sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: skip_to_init: sl@0: #endif sl@0: return ctx->digest->init(ctx); sl@0: } sl@0: sl@0: EXPORT_C int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, sl@0: size_t count) sl@0: { sl@0: return ctx->digest->update(ctx,data,count); sl@0: } sl@0: sl@0: /* The caller can assume that this removes any secret data from the context */ sl@0: EXPORT_C int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) sl@0: { sl@0: int ret; sl@0: ret = EVP_DigestFinal_ex(ctx, md, size); sl@0: EVP_MD_CTX_cleanup(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: /* The caller can assume that this removes any secret data from the context */ sl@0: EXPORT_C int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) sl@0: { sl@0: int ret; sl@0: sl@0: OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); sl@0: ret=ctx->digest->final(ctx,md); sl@0: if (size != NULL) sl@0: *size=ctx->digest->md_size; sl@0: if (ctx->digest->cleanup) sl@0: { sl@0: ctx->digest->cleanup(ctx); sl@0: EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); sl@0: } sl@0: if(ctx->md_data) sl@0: memset(ctx->md_data,0,ctx->digest->ctx_size); sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) sl@0: { sl@0: EVP_MD_CTX_init(out); sl@0: return EVP_MD_CTX_copy_ex(out, in); sl@0: } sl@0: sl@0: EXPORT_C int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) sl@0: { sl@0: unsigned char *tmp_buf; sl@0: if ((in == NULL) || (in->digest == NULL)) sl@0: { sl@0: EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,EVP_R_INPUT_NOT_INITIALIZED); sl@0: return 0; sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: /* Make sure it's safe to copy a digest context using an ENGINE */ sl@0: if (in->engine && !ENGINE_init(in->engine)) sl@0: { sl@0: EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB); sl@0: return 0; sl@0: } sl@0: #endif sl@0: sl@0: if (out->digest == in->digest) sl@0: { sl@0: tmp_buf = out->md_data; sl@0: EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE); sl@0: } sl@0: else tmp_buf = NULL; sl@0: EVP_MD_CTX_cleanup(out); sl@0: memcpy(out,in,sizeof *out); sl@0: sl@0: if (out->digest->ctx_size) sl@0: { sl@0: if (tmp_buf) out->md_data = tmp_buf; sl@0: else out->md_data=OPENSSL_malloc(out->digest->ctx_size); sl@0: if(out->md_data==NULL||in->md_data==NULL) sl@0: { sl@0: EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: memcpy(out->md_data,in->md_data,out->digest->ctx_size); sl@0: } sl@0: sl@0: if (out->digest->copy) sl@0: return out->digest->copy(out,in); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int EVP_Digest(const void *data, size_t count, sl@0: unsigned char *md, unsigned int *size, const EVP_MD *type, ENGINE *impl) sl@0: { sl@0: EVP_MD_CTX ctx; sl@0: int ret; sl@0: sl@0: EVP_MD_CTX_init(&ctx); sl@0: EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT); sl@0: ret=EVP_DigestInit_ex(&ctx, type, impl) sl@0: && EVP_DigestUpdate(&ctx, data, count) sl@0: && EVP_DigestFinal_ex(&ctx, md, size); sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) sl@0: { sl@0: EVP_MD_CTX_cleanup(ctx); sl@0: OPENSSL_free(ctx); sl@0: } sl@0: sl@0: /* This call frees resources associated with the context */ sl@0: EXPORT_C int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) sl@0: { sl@0: /* Don't assume ctx->md_data was cleaned in EVP_Digest_Final, sl@0: * because sometimes only copies of the context are ever finalised. sl@0: */ sl@0: if (ctx->digest && ctx->digest->cleanup sl@0: && !EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED)) sl@0: ctx->digest->cleanup(ctx); sl@0: if (ctx->digest && ctx->digest->ctx_size && ctx->md_data sl@0: && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) sl@0: { sl@0: OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size); sl@0: OPENSSL_free(ctx->md_data); sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: if(ctx->engine) sl@0: /* The EVP_MD we used belongs to an ENGINE, release the sl@0: * functional reference we held for this reason. */ sl@0: ENGINE_finish(ctx->engine); sl@0: #endif sl@0: memset(ctx,'\0',sizeof *ctx); sl@0: sl@0: return 1; sl@0: }