From eb90170613bec4856332c0e5e6113acfe4e7c17f Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 10 Sep 2016 19:23:22 +0000 Subject: * src/ne_md5.c: Re-order, #ifdef mostly out for OpenSSL build. * src/ne_openssl.c (init_md5_ctx, ne_md5_create_ctx, ne_md5_process_block, ne_md5_process_bytes, ne_md5_finish_ctx, ne_md5_dup_ctx, ne_md5_reset_ctx), ne_md5_destroy_ctx): Implement using EVP API. git-svn-id: http://svn.webdav.org/repos/projects/neon/trunk@1971 61a7d7f5-40b7-0310-9c16-bb0ea8cb1845 --- src/ne_md5.c | 130 +++++++++++++++++++++++++++++-------------------------- src/ne_openssl.c | 77 +++++++++++++++++++++++++++++++- 2 files changed, 144 insertions(+), 63 deletions(-) diff --git a/src/ne_md5.c b/src/ne_md5.c index 35d7109..1111c56 100644 --- a/src/ne_md5.c +++ b/src/ne_md5.c @@ -33,6 +33,14 @@ #include "ne_md5.h" #include "ne_string.h" /* for NE_ASC2HEX */ +#if SIZEOF_INT == 4 +typedef unsigned int md5_uint32; +#elif SIZEOF_LONG == 4 +typedef unsigned long md5_uint32; +#else +# error "Cannot determine unsigned 32-bit data type." +#endif + #define md5_process_block ne_md5_process_block #define md5_process_bytes ne_md5_process_bytes #define md5_finish_ctx ne_md5_finish_ctx @@ -40,6 +48,8 @@ #define md5_stream ne_md5_stream #define md5_ctx ne_md5_ctx +#ifndef HAVE_OPENSSL + #ifdef WORDS_BIGENDIAN # define SWAP(n) \ (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) @@ -47,14 +57,6 @@ # define SWAP(n) (n) #endif -#if SIZEOF_INT == 4 -typedef unsigned int md5_uint32; -#elif SIZEOF_LONG == 4 -typedef unsigned long md5_uint32; -#else -# error "Cannot determine unsigned 32-bit data type." -#endif - /* Structure to save state of computation between the single steps. */ struct md5_ctx { @@ -164,60 +166,6 @@ md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) return md5_read_ctx (ctx, resbuf); } -/* Compute MD5 message digest for bytes read from STREAM. The - resulting message digest number will be written into the 16 bytes - beginning at RESBLOCK. */ -int -md5_stream (FILE *stream, void *resblock) -{ - /* Important: BLOCKSIZE must be a multiple of 64. */ -#define BLOCKSIZE 4096 - struct md5_ctx ctx; - char buffer[BLOCKSIZE + 72]; - size_t sum; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Iterate over full file contents. */ - while (1) - { - /* We read the file in blocks of BLOCKSIZE bytes. One call of the - computation function processes the whole buffer so that with the - next round of the loop another block can be read. */ - size_t n; - sum = 0; - - /* Read block. Take care for partial reads. */ - do - { - n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); - - sum += n; - } - while (sum < BLOCKSIZE && n != 0); - if (n == 0 && ferror (stream)) - return 1; - - /* If end of file is reached, end the loop. */ - if (n == 0) - break; - - /* Process buffer with BLOCKSIZE bytes. Note that - BLOCKSIZE % 64 == 0 - */ - md5_process_block (buffer, BLOCKSIZE, &ctx); - } - - /* Add the last bytes if necessary. */ - if (sum > 0) - md5_process_bytes (buffer, sum, &ctx); - - /* Construct result in desired memory. */ - md5_finish_ctx (&ctx, resblock); - return 0; -} - void md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) { @@ -429,6 +377,64 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) ctx->D = D; } +#endif + +/* Compute MD5 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +int +md5_stream (FILE *stream, void *resblock) +{ + /* Important: BLOCKSIZE must be a multiple of 64. */ +#define BLOCKSIZE 4096 + struct ne_md5_ctx *ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + ctx = ne_md5_create_ctx (); + + /* Iterate over full file contents. */ + while (1) + { + /* We read the file in blocks of BLOCKSIZE bytes. One call of the + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ + size_t n; + sum = 0; + + /* Read block. Take care for partial reads. */ + do + { + n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + } + while (sum < BLOCKSIZE && n != 0); + if (n == 0 && ferror (stream)) + return 1; + + /* If end of file is reached, end the loop. */ + if (n == 0) + break; + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + md5_process_block (buffer, BLOCKSIZE, ctx); + } + + /* Add the last bytes if necessary. */ + if (sum > 0) + md5_process_bytes (buffer, sum, ctx); + + /* Construct result in desired memory. */ + md5_finish_ctx (ctx, resblock); + ne_md5_destroy_ctx (ctx); + + return 0; +} + /* Writes the ASCII representation of the MD5 digest into the * given buffer, which must be at least 33 characters long. */ void ne_md5_to_ascii(const unsigned char md5_buf[16], char *buffer) diff --git a/src/ne_openssl.c b/src/ne_openssl.c index b2bad39..41fdcae 100644 --- a/src/ne_openssl.c +++ b/src/ne_openssl.c @@ -35,6 +35,7 @@ #include #include #include +#include #ifdef NE_HAVE_TS_SSL #include /* for abort() */ @@ -47,7 +48,7 @@ #include "ne_string.h" #include "ne_session.h" #include "ne_internal.h" - +#include "ne_md5.h" #include "ne_private.h" #include "ne_privssl.h" @@ -1263,3 +1264,77 @@ void ne__ssl_exit(void) } #endif } + +struct ne_md5_ctx { + EVP_MD_CTX ctx; +}; + +/* Returns zero on succes, non-zero on failure. */ +static int init_md5_ctx(struct ne_md5_ctx *ctx) +{ + EVP_MD_CTX_init(&ctx->ctx); + + if (EVP_DigestInit_ex(&ctx->ctx, EVP_md5(), NULL) != 1) { + EVP_MD_CTX_cleanup(&ctx->ctx); + return 1; + } + + return 0; +} + +struct ne_md5_ctx *ne_md5_create_ctx(void) +{ +#if 1 + return NULL; +#else + struct ne_md5_ctx *ctx = ne_malloc(sizeof *ctx); + + if (init_md5_ctx(ctx)) { + ne_free(ctx); + return NULL; + } + + return ctx; +#endif +} + +void ne_md5_process_block(const void *buffer, size_t len, + struct ne_md5_ctx *ctx) +{ + EVP_DigestUpdate(&ctx->ctx, buffer, len); +} + +void ne_md5_process_bytes(const void *buffer, size_t len, + struct ne_md5_ctx *ctx) +{ + EVP_DigestUpdate(&ctx->ctx, buffer, len); +} + +void *ne_md5_finish_ctx(struct ne_md5_ctx *ctx, void *resbuf) +{ + EVP_DigestFinal(&ctx->ctx, resbuf, NULL); + + return resbuf; +} + +struct ne_md5_ctx *ne_md5_dup_ctx(struct ne_md5_ctx *ctx) +{ + struct ne_md5_ctx *r = ne_md5_create_ctx(); + + EVP_MD_CTX_copy_ex(&r->ctx, &ctx->ctx); + + return r; +} + +void ne_md5_reset_ctx(struct ne_md5_ctx *ctx) +{ + EVP_MD_CTX_cleanup(&ctx->ctx); + + init_md5_ctx(ctx); +} + +void ne_md5_destroy_ctx(struct ne_md5_ctx *ctx) +{ + EVP_MD_CTX_cleanup(&ctx->ctx); + ne_free(ctx); +} -- cgit v1.2.1