1254721Semaste/* crypto/evp/bio_md.c */ 2254721Semaste/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3254721Semaste * All rights reserved. 4254721Semaste * 5254721Semaste * This package is an SSL implementation written 6254721Semaste * by Eric Young (eay@cryptsoft.com). 7254721Semaste * The implementation was written so as to conform with Netscapes SSL. 8254721Semaste * 9254721Semaste * This library is free for commercial and non-commercial use as long as 10254721Semaste * the following conditions are aheared to. The following conditions 11254721Semaste * apply to all code found in this distribution, be it the RC4, RSA, 12254721Semaste * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13254721Semaste * included with this distribution is covered by the same copyright terms 14254721Semaste * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15254721Semaste * 16254721Semaste * Copyright remains Eric Young's, and as such any Copyright notices in 17254721Semaste * the code are not to be removed. 18254721Semaste * If this package is used in a product, Eric Young should be given attribution 19254721Semaste * as the author of the parts of the library used. 20254721Semaste * This can be in the form of a textual message at program startup or 21254721Semaste * in documentation (online or textual) provided with the package. 22254721Semaste * 23254721Semaste * Redistribution and use in source and binary forms, with or without 24254721Semaste * modification, are permitted provided that the following conditions 25254721Semaste * are met: 26254721Semaste * 1. Redistributions of source code must retain the copyright 27254721Semaste * notice, this list of conditions and the following disclaimer. 28254721Semaste * 2. Redistributions in binary form must reproduce the above copyright 29254721Semaste * notice, this list of conditions and the following disclaimer in the 30254721Semaste * documentation and/or other materials provided with the distribution. 31254721Semaste * 3. All advertising materials mentioning features or use of this software 32254721Semaste * must display the following acknowledgement: 33254721Semaste * "This product includes cryptographic software written by 34254721Semaste * Eric Young (eay@cryptsoft.com)" 35254721Semaste * The word 'cryptographic' can be left out if the rouines from the library 36254721Semaste * being used are not cryptographic related :-). 37254721Semaste * 4. If you include any Windows specific code (or a derivative thereof) from 38254721Semaste * the apps directory (application code) you must include an acknowledgement: 39254721Semaste * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40254721Semaste * 41254721Semaste * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44254721Semaste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51254721Semaste * SUCH DAMAGE. 52254721Semaste * 53254721Semaste * The licence and distribution terms for any publically available version or 54254721Semaste * derivative of this code cannot be changed. i.e. this code cannot simply be 55254721Semaste * copied and put under another distribution licence 56254721Semaste * [including the GNU Public Licence.] 57254721Semaste */ 58254721Semaste 59254721Semaste#include <stdio.h> 60254721Semaste#include <errno.h> 61254721Semaste#include "cryptlib.h" 62254721Semaste#include <openssl/buffer.h> 63254721Semaste#include <openssl/evp.h> 64254721Semaste 65254721Semaste/* 66254721Semaste * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest 67254721Semaste */ 68254721Semaste 69254721Semastestatic int md_write(BIO *h, char const *buf, int num); 70254721Semastestatic int md_read(BIO *h, char *buf, int size); 71254721Semaste/* 72254721Semaste * static int md_puts(BIO *h, const char *str); 73254721Semaste */ 74254721Semastestatic int md_gets(BIO *h, char *str, int size); 75254721Semastestatic long md_ctrl(BIO *h, int cmd, long arg1, void *arg2); 76254721Semastestatic int md_new(BIO *h); 77254721Semastestatic int md_free(BIO *data); 78254721Semastestatic long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); 79254721Semaste 80254721Semastestatic BIO_METHOD methods_md = { 81254721Semaste BIO_TYPE_MD, "message digest", 82254721Semaste md_write, 83254721Semaste md_read, 84254721Semaste NULL, /* md_puts, */ 85254721Semaste md_gets, 86254721Semaste md_ctrl, 87254721Semaste md_new, 88254721Semaste md_free, 89254721Semaste md_callback_ctrl, 90254721Semaste}; 91254721Semaste 92254721SemasteBIO_METHOD *BIO_f_md(void) 93254721Semaste{ 94254721Semaste return (&methods_md); 95254721Semaste} 96254721Semaste 97254721Semastestatic int md_new(BIO *bi) 98254721Semaste{ 99254721Semaste EVP_MD_CTX *ctx; 100254721Semaste 101254721Semaste ctx = EVP_MD_CTX_create(); 102254721Semaste if (ctx == NULL) 103254721Semaste return (0); 104254721Semaste 105254721Semaste bi->init = 0; 106254721Semaste bi->ptr = (char *)ctx; 107254721Semaste bi->flags = 0; 108254721Semaste return (1); 109254721Semaste} 110254721Semaste 111254721Semastestatic int md_free(BIO *a) 112254721Semaste{ 113254721Semaste if (a == NULL) 114254721Semaste return (0); 115254721Semaste EVP_MD_CTX_destroy(a->ptr); 116254721Semaste a->ptr = NULL; 117254721Semaste a->init = 0; 118254721Semaste a->flags = 0; 119254721Semaste return (1); 120254721Semaste} 121254721Semaste 122254721Semastestatic int md_read(BIO *b, char *out, int outl) 123254721Semaste{ 124254721Semaste int ret = 0; 125254721Semaste EVP_MD_CTX *ctx; 126254721Semaste 127254721Semaste if (out == NULL) 128254721Semaste return (0); 129254721Semaste ctx = b->ptr; 130254721Semaste 131254721Semaste if ((ctx == NULL) || (b->next_bio == NULL)) 132254721Semaste return (0); 133254721Semaste 134254721Semaste ret = BIO_read(b->next_bio, out, outl); 135254721Semaste if (b->init) { 136254721Semaste if (ret > 0) { 137254721Semaste if (EVP_DigestUpdate(ctx, (unsigned char *)out, 138254721Semaste (unsigned int)ret) <= 0) 139254721Semaste return (-1); 140254721Semaste } 141254721Semaste } 142254721Semaste BIO_clear_retry_flags(b); 143254721Semaste BIO_copy_next_retry(b); 144254721Semaste return (ret); 145254721Semaste} 146254721Semaste 147254721Semastestatic int md_write(BIO *b, const char *in, int inl) 148254721Semaste{ 149254721Semaste int ret = 0; 150254721Semaste EVP_MD_CTX *ctx; 151254721Semaste 152254721Semaste if ((in == NULL) || (inl <= 0)) 153254721Semaste return (0); 154254721Semaste ctx = b->ptr; 155254721Semaste 156254721Semaste if ((ctx != NULL) && (b->next_bio != NULL)) 157254721Semaste ret = BIO_write(b->next_bio, in, inl); 158254721Semaste if (b->init) { 159254721Semaste if (ret > 0) { 160254721Semaste if (!EVP_DigestUpdate(ctx, (const unsigned char *)in, 161254721Semaste (unsigned int)ret)) { 162254721Semaste BIO_clear_retry_flags(b); 163254721Semaste return 0; 164254721Semaste } 165254721Semaste } 166254721Semaste } 167254721Semaste if (b->next_bio != NULL) { 168254721Semaste BIO_clear_retry_flags(b); 169254721Semaste BIO_copy_next_retry(b); 170254721Semaste } 171254721Semaste return (ret); 172254721Semaste} 173254721Semaste 174254721Semastestatic long md_ctrl(BIO *b, int cmd, long num, void *ptr) 175254721Semaste{ 176254721Semaste EVP_MD_CTX *ctx, *dctx, **pctx; 177254721Semaste const EVP_MD **ppmd; 178254721Semaste EVP_MD *md; 179254721Semaste long ret = 1; 180254721Semaste BIO *dbio; 181254721Semaste 182254721Semaste ctx = b->ptr; 183254721Semaste 184254721Semaste switch (cmd) { 185254721Semaste case BIO_CTRL_RESET: 186254721Semaste if (b->init) 187254721Semaste ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL); 188254721Semaste else 189254721Semaste ret = 0; 190254721Semaste if (ret > 0) 191254721Semaste ret = BIO_ctrl(b->next_bio, cmd, num, ptr); 192254721Semaste break; 193254721Semaste case BIO_C_GET_MD: 194254721Semaste if (b->init) { 195254721Semaste ppmd = ptr; 196254721Semaste *ppmd = ctx->digest; 197254721Semaste } else 198254721Semaste ret = 0; 199254721Semaste break; 200254721Semaste case BIO_C_GET_MD_CTX: 201254721Semaste pctx = ptr; 202254721Semaste *pctx = ctx; 203254721Semaste b->init = 1; 204254721Semaste break; 205254721Semaste case BIO_C_SET_MD_CTX: 206254721Semaste if (b->init) 207254721Semaste b->ptr = ptr; 208254721Semaste else 209254721Semaste ret = 0; 210254721Semaste break; 211254721Semaste case BIO_C_DO_STATE_MACHINE: 212254721Semaste BIO_clear_retry_flags(b); 213254721Semaste ret = BIO_ctrl(b->next_bio, cmd, num, ptr); 214254721Semaste BIO_copy_next_retry(b); 215254721Semaste break; 216254721Semaste 217254721Semaste case BIO_C_SET_MD: 218254721Semaste md = ptr; 219254721Semaste ret = EVP_DigestInit_ex(ctx, md, NULL); 220254721Semaste if (ret > 0) 221254721Semaste b->init = 1; 222254721Semaste break; 223254721Semaste case BIO_CTRL_DUP: 224254721Semaste dbio = ptr; 225254721Semaste dctx = dbio->ptr; 226254721Semaste if (!EVP_MD_CTX_copy_ex(dctx, ctx)) 227254721Semaste return 0; 228254721Semaste b->init = 1; 229254721Semaste break; 230254721Semaste default: 231254721Semaste ret = BIO_ctrl(b->next_bio, cmd, num, ptr); 232254721Semaste break; 233254721Semaste } 234254721Semaste return (ret); 235254721Semaste} 236254721Semaste 237254721Semastestatic long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 238254721Semaste{ 239254721Semaste long ret = 1; 240254721Semaste 241254721Semaste if (b->next_bio == NULL) 242254721Semaste return (0); 243254721Semaste switch (cmd) { 244254721Semaste default: 245254721Semaste ret = BIO_callback_ctrl(b->next_bio, cmd, fp); 246254721Semaste break; 247254721Semaste } 248254721Semaste return (ret); 249254721Semaste} 250254721Semaste 251254721Semastestatic int md_gets(BIO *bp, char *buf, int size) 252254721Semaste{ 253254721Semaste EVP_MD_CTX *ctx; 254254721Semaste unsigned int ret; 255254721Semaste 256254721Semaste ctx = bp->ptr; 257254721Semaste if (size < ctx->digest->md_size) 258254721Semaste return (0); 259254721Semaste if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0) 260254721Semaste return -1; 261254721Semaste 262254721Semaste return ((int)ret); 263254721Semaste} 264254721Semaste 265254721Semaste/*- 266254721Semastestatic int md_puts(bp,str) 267254721SemasteBIO *bp; 268254721Semastechar *str; 269254721Semaste { 270254721Semaste return(-1); 271254721Semaste } 272254721Semaste*/ 273254721Semaste