155714Skris/* crypto/evp/bio_md.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 */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <errno.h> 6155714Skris#include "cryptlib.h" 6255714Skris#include <openssl/buffer.h> 6355714Skris#include <openssl/evp.h> 6455714Skris 6555714Skris/* BIO_put and BIO_get both add to the digest, 6655714Skris * BIO_gets returns the digest */ 6755714Skris 6868651Skrisstatic int md_write(BIO *h, char const *buf, int num); 6968651Skrisstatic int md_read(BIO *h, char *buf, int size); 7068651Skris/*static int md_puts(BIO *h, const char *str); */ 7168651Skrisstatic int md_gets(BIO *h, char *str, int size); 7268651Skrisstatic long md_ctrl(BIO *h, int cmd, long arg1, void *arg2); 7355714Skrisstatic int md_new(BIO *h); 7455714Skrisstatic int md_free(BIO *data); 7568651Skrisstatic long md_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); 7659191Skris 7755714Skrisstatic BIO_METHOD methods_md= 7855714Skris { 7955714Skris BIO_TYPE_MD,"message digest", 8055714Skris md_write, 8155714Skris md_read, 8255714Skris NULL, /* md_puts, */ 8355714Skris md_gets, 8455714Skris md_ctrl, 8555714Skris md_new, 8655714Skris md_free, 8759191Skris md_callback_ctrl, 8855714Skris }; 8955714Skris 9055714SkrisBIO_METHOD *BIO_f_md(void) 9155714Skris { 9255714Skris return(&methods_md); 9355714Skris } 9455714Skris 9555714Skrisstatic int md_new(BIO *bi) 9655714Skris { 9755714Skris EVP_MD_CTX *ctx; 9855714Skris 99109998Smarkm ctx=EVP_MD_CTX_create(); 10055714Skris if (ctx == NULL) return(0); 10155714Skris 10255714Skris bi->init=0; 10355714Skris bi->ptr=(char *)ctx; 10455714Skris bi->flags=0; 10555714Skris return(1); 10655714Skris } 10755714Skris 10855714Skrisstatic int md_free(BIO *a) 10955714Skris { 11055714Skris if (a == NULL) return(0); 111109998Smarkm EVP_MD_CTX_destroy(a->ptr); 11255714Skris a->ptr=NULL; 11355714Skris a->init=0; 11455714Skris a->flags=0; 11555714Skris return(1); 11655714Skris } 11755714Skris 11855714Skrisstatic int md_read(BIO *b, char *out, int outl) 11955714Skris { 12055714Skris int ret=0; 12155714Skris EVP_MD_CTX *ctx; 12255714Skris 12355714Skris if (out == NULL) return(0); 124109998Smarkm ctx=b->ptr; 12555714Skris 12655714Skris if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 12755714Skris 12855714Skris ret=BIO_read(b->next_bio,out,outl); 12955714Skris if (b->init) 13055714Skris { 13155714Skris if (ret > 0) 13255714Skris { 133238405Sjkim if (EVP_DigestUpdate(ctx,(unsigned char *)out, 134238405Sjkim (unsigned int)ret)<=0) return (-1); 13555714Skris } 13655714Skris } 13755714Skris BIO_clear_retry_flags(b); 13855714Skris BIO_copy_next_retry(b); 13955714Skris return(ret); 14055714Skris } 14155714Skris 14268651Skrisstatic int md_write(BIO *b, const char *in, int inl) 14355714Skris { 14455714Skris int ret=0; 14555714Skris EVP_MD_CTX *ctx; 14655714Skris 14755714Skris if ((in == NULL) || (inl <= 0)) return(0); 148109998Smarkm ctx=b->ptr; 14955714Skris 15055714Skris if ((ctx != NULL) && (b->next_bio != NULL)) 15155714Skris ret=BIO_write(b->next_bio,in,inl); 15255714Skris if (b->init) 15355714Skris { 15455714Skris if (ret > 0) 15555714Skris { 156238405Sjkim if (!EVP_DigestUpdate(ctx,(const unsigned char *)in, 157238405Sjkim (unsigned int)ret)) 158238405Sjkim { 159238405Sjkim BIO_clear_retry_flags(b); 160238405Sjkim return 0; 161238405Sjkim } 16255714Skris } 16355714Skris } 164238405Sjkim if(b->next_bio != NULL) 165238405Sjkim { 166238405Sjkim BIO_clear_retry_flags(b); 167238405Sjkim BIO_copy_next_retry(b); 168238405Sjkim } 16955714Skris return(ret); 17055714Skris } 17155714Skris 17268651Skrisstatic long md_ctrl(BIO *b, int cmd, long num, void *ptr) 17355714Skris { 17455714Skris EVP_MD_CTX *ctx,*dctx,**pctx; 17555714Skris const EVP_MD **ppmd; 17655714Skris EVP_MD *md; 17755714Skris long ret=1; 17855714Skris BIO *dbio; 17955714Skris 180109998Smarkm ctx=b->ptr; 18155714Skris 18255714Skris switch (cmd) 18355714Skris { 18455714Skris case BIO_CTRL_RESET: 18555714Skris if (b->init) 186142425Snectar ret = EVP_DigestInit_ex(ctx,ctx->digest, NULL); 18755714Skris else 18855714Skris ret=0; 189142425Snectar if (ret > 0) 190142425Snectar ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 19155714Skris break; 19255714Skris case BIO_C_GET_MD: 19355714Skris if (b->init) 19455714Skris { 195109998Smarkm ppmd=ptr; 19655714Skris *ppmd=ctx->digest; 19755714Skris } 19855714Skris else 19955714Skris ret=0; 20055714Skris break; 20155714Skris case BIO_C_GET_MD_CTX: 202194206Ssimon pctx=ptr; 203194206Ssimon *pctx=ctx; 204238405Sjkim b->init = 1; 20555714Skris break; 206167612Ssimon case BIO_C_SET_MD_CTX: 207167612Ssimon if (b->init) 208167612Ssimon b->ptr=ptr; 209167612Ssimon else 210167612Ssimon ret=0; 211167612Ssimon break; 21255714Skris case BIO_C_DO_STATE_MACHINE: 21355714Skris BIO_clear_retry_flags(b); 21455714Skris ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 21555714Skris BIO_copy_next_retry(b); 21655714Skris break; 21755714Skris 21855714Skris case BIO_C_SET_MD: 219109998Smarkm md=ptr; 220142425Snectar ret = EVP_DigestInit_ex(ctx,md, NULL); 221142425Snectar if (ret > 0) 222142425Snectar b->init=1; 22355714Skris break; 22455714Skris case BIO_CTRL_DUP: 225109998Smarkm dbio=ptr; 226109998Smarkm dctx=dbio->ptr; 227238405Sjkim if (!EVP_MD_CTX_copy_ex(dctx,ctx)) 228238405Sjkim return 0; 22955714Skris b->init=1; 23055714Skris break; 23155714Skris default: 23255714Skris ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 23355714Skris break; 23455714Skris } 23555714Skris return(ret); 23655714Skris } 23755714Skris 23868651Skrisstatic long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 23959191Skris { 24059191Skris long ret=1; 24159191Skris 24259191Skris if (b->next_bio == NULL) return(0); 24359191Skris switch (cmd) 24459191Skris { 24559191Skris default: 24659191Skris ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 24759191Skris break; 24859191Skris } 24959191Skris return(ret); 25059191Skris } 25159191Skris 25255714Skrisstatic int md_gets(BIO *bp, char *buf, int size) 25355714Skris { 25455714Skris EVP_MD_CTX *ctx; 25555714Skris unsigned int ret; 25655714Skris 25755714Skris 258109998Smarkm ctx=bp->ptr; 25955714Skris if (size < ctx->digest->md_size) 26055714Skris return(0); 261238405Sjkim if (EVP_DigestFinal_ex(ctx,(unsigned char *)buf,&ret)<=0) 262238405Sjkim return -1; 263238405Sjkim 26455714Skris return((int)ret); 26555714Skris } 26655714Skris 26755714Skris/* 26855714Skrisstatic int md_puts(bp,str) 26955714SkrisBIO *bp; 27055714Skrischar *str; 27155714Skris { 27255714Skris return(-1); 27355714Skris } 27455714Skris*/ 27555714Skris 276