From: Jonathan McDowell Date: Tue, 18 Oct 2011 14:20:43 +0000 (-0700) Subject: Use nettle for hashing when available rather than internal MD5/SHA1 routines X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/commitdiff_plain/4860ab5af538fd20c7e92e01b883dbbc556863a5?hp=a241edc87bed49d6bc5dc3c4c99786a921ae7c8a Use nettle for hashing when available rather than internal MD5/SHA1 routines Change the internal MD5/SHA1 routines to match nettle's name and calling convention and add suitable autoconf bits to auto-select nettle if it's available, otherwise fall back to the internal routines as usual. Not so much of an issue for MD5/SHA1 (though we might end up with more optimised routines in some instances), but allows easier use of other hashing/crypto functions in the future. --- diff --git a/Makefile.in b/Makefile.in index 1fb16d9..447894b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -9,7 +9,7 @@ LDFLAGS += @LDFLAGS@ # Can be "pg" for Postgresql, "file" for flat files or "db2" for pksd db2 style. DBTYPE = @DBTYPE@ # -LIBS = @LIBS@ +LIBS = @LIBS@ @NETTLE_LIBS@ DB4LIBS = @DB4LIBS@ #MAKEDEPEND = makedepend -f- -- MAKEDEPEND = $(CC) -MM @@ -18,8 +18,11 @@ exec_prefix ?= @exec_prefix@ PROGS = add lookup hashquery gpgwww onak splitkeys onak-mail.pl stripkey CORE_OBJS = armor.o charfuncs.o decodekey.o getcgi.o hash.o marshal.o \ - keyid.o keyindex.o ll.o mem.o onak-conf.o parsekey.o sha1.o md5.o \ + keyid.o keyindex.o ll.o mem.o onak-conf.o parsekey.o \ log.o photoid.o wordlist.o cleanup.o merge.o sendsync.o keyarray.o +ifeq (x@NETTLE_LIBS@, x) +CORE_OBJS += md5.o sha1.o +endif SRCS = armor.c parsekey.c merge.c keyid.c md5.c sha1.c main.c getcgi.c mem.c \ keyindex.c stats.c lookup.c add.c keydb_$(DBTYPE).c ll.c hash.c \ gpgwww.c onak-conf.c charfuncs.c sendsync.c log.c photoid.c \ diff --git a/configure.ac b/configure.ac index 76462ad..f2bdfd1 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,10 @@ AC_PROG_CC AC_C_BIGENDIAN +m4_include([m4/ax_lib_nettle.m4]) + +AX_LIB_NETTLE(auto) + dnl We should always have these backends available. backends="file fs keyd" diff --git a/debian/control b/debian/control index b3c8de2..f8bb920 100644 --- a/debian/control +++ b/debian/control @@ -3,7 +3,7 @@ Section: net Priority: optional Maintainer: Jonathan McDowell Uploaders: Ross Burton -Build-Depends: debhelper (>= 7), cdbs, libdb-dev +Build-Depends: debhelper (>= 7), cdbs, libdb-dev, nettle-dev Standards-Version: 3.9.2.0 Homepage: http://www.earth.li/projectpurple/progs/onak.html Vcs-Bzr: http://www.earth.li/~noodles/bzr/onak/mainline diff --git a/keyid.c b/keyid.c index a7051f2..c75f0a1 100644 --- a/keyid.c +++ b/keyid.c @@ -21,14 +21,21 @@ #include #include +#include "config.h" #include "keyid.h" #include "keystructs.h" #include "log.h" #include "parsekey.h" -#include "md5.h" #include "mem.h" #include "merge.h" + +#ifdef HAVE_NETTLE +#include +#include +#else +#include "md5.h" #include "sha1.h" +#endif /** @@ -54,7 +61,7 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet, unsigned char *fingerprint, size_t *len) { - SHA1_CTX sha_ctx; + struct sha1_ctx sha_ctx; struct md5_ctx md5_context; unsigned char c; size_t modlen, explen; @@ -67,41 +74,40 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet, switch (packet->data[0]) { case 2: case 3: - md5_init_ctx(&md5_context); + md5_init(&md5_context); /* * MD5 the modulus and exponent. */ modlen = ((packet->data[8] << 8) + packet->data[9] + 7) >> 3; - md5_process_bytes(&packet->data[10], modlen, &md5_context); + md5_update(&md5_context, modlen, &packet->data[10]); explen = ((packet->data[10+modlen] << 8) + packet->data[11+modlen] + 7) >> 3; - md5_process_bytes(&packet->data[12 + modlen], explen, - &md5_context); + md5_update(&md5_context, explen, &packet->data[12 + modlen]); - md5_finish_ctx(&md5_context, fingerprint); *len = 16; + md5_digest(&md5_context, *len, fingerprint); break; case 4: - SHA1Init(&sha_ctx); + sha1_init(&sha_ctx); /* * TODO: Can this be 0x99? Are all public key packets old * format with 2 bytes of length data? */ c = 0x99; - SHA1Update(&sha_ctx, &c, sizeof(c)); + sha1_update(&sha_ctx, sizeof(c), &c); c = packet->length >> 8; - SHA1Update(&sha_ctx, &c, sizeof(c)); + sha1_update(&sha_ctx, sizeof(c), &c); c = packet->length & 0xFF; - SHA1Update(&sha_ctx, &c, sizeof(c)); - SHA1Update(&sha_ctx, packet->data, - packet->length); - SHA1Final(fingerprint, &sha_ctx); + sha1_update(&sha_ctx, sizeof(c), &c); + sha1_update(&sha_ctx, packet->length, + packet->data); *len = 20; + sha1_digest(&sha_ctx, *len, fingerprint); break; default: @@ -212,20 +218,20 @@ void get_skshash(struct openpgp_publickey *key, struct skshash *hash) key->next = next; packets = sortpackets(packets); - md5_init_ctx(&md5_context); + md5_init(&md5_context); for (curpacket = packets; curpacket != NULL; curpacket = curpacket->next) { tmp = htonl(curpacket->packet->tag); - md5_process_bytes(&tmp, sizeof(tmp), &md5_context); + md5_update(&md5_context, sizeof(tmp), (void *) &tmp); tmp = htonl(curpacket->packet->length); - md5_process_bytes(&tmp, sizeof(tmp), &md5_context); - md5_process_bytes(curpacket->packet->data, + md5_update(&md5_context, sizeof(tmp), (void *) &tmp); + md5_update(&md5_context, curpacket->packet->length, - &md5_context); + curpacket->packet->data); } - md5_finish_ctx(&md5_context, &hash->hash); + md5_digest(&md5_context, 16, (uint8_t *) &hash->hash); free_packet_list(packets); } diff --git a/m4/ax_lib_nettle.m4 b/m4/ax_lib_nettle.m4 new file mode 100644 index 0000000..fa54263 --- /dev/null +++ b/m4/ax_lib_nettle.m4 @@ -0,0 +1,80 @@ +# =========================================================================== +# http://www.gnu.org/software/autoconf-archive/ax_lib_nettle.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_LIB_NETTLE([yes|no|auto]) +# +# DESCRIPTION +# +# Searches for the 'nettle' library with the --with... option. +# +# If found, define HAVE_NETTLE and macro NETTLE_LIBS. Also defines +# NETTLE_WITH_ for the algorithms found available. Possible +# algorithms: AES ARCTWO BLOWFISH CAST128 DES DES3 SERPENT TWOFISH MD2 MD4 +# MD5 SHA1 SHA256. +# +# The argument is used if no --with...-nettle option is set. Value "yes" +# requires the configuration by default. Value "no" does not require it by +# default. Value "auto" configures the library only if available. +# +# See also AX_LIB_BEECRYPT, AX_LIB_CRYPTO, and AX_LIB_GCRYPT. +# +# LICENSE +# +# Copyright (c) 2009 Fabien Coelho +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +#serial 7 + +# AX_CHECK_NETTLE_ALGO([name],[function]) +AC_DEFUN([AX_CHECK_NETTLE_ALGO],[ + AC_CHECK_LIB([nettle], [nettle_$2], + AC_DEFINE([NETTLE_WITH_$1],[1],[Algorithm $1 in nettle library])) +]) + +# AX_LIB_NETTLE([yes|no|auto]) +AC_DEFUN([AX_LIB_NETTLE],[ + AC_MSG_CHECKING([whether nettle is enabled]) + AC_ARG_WITH([nettle],[ --with-nettle require nettle library + --without-nettle disable nettle library],[ + AC_MSG_RESULT([$withval]) + ax_with_nettle=$withval + ],[ + AC_MSG_RESULT([$1]) + ax_with_nettle=$1 + ]) + if test "$ax_with_nettle" = "yes" -o "$ax_with_nettle" = "auto" ; then + AC_CHECK_HEADERS([nettle/nettle-meta.h],[ + AC_CHECK_LIB([nettle],[nettle_base64_encode_final],[ + AC_DEFINE([HAVE_NETTLE],[1],[Nettle library is available]) + HAVE_NETTLE=1 + AC_SUBST([NETTLE_LIBS],[-lnettle]) + # ciphers + AX_CHECK_NETTLE_ALGO([AES],[aes_encrypt]) + AX_CHECK_NETTLE_ALGO([ARCTWO],[arctwo_encrypt]) + AX_CHECK_NETTLE_ALGO([BLOWFISH],[blowfish_encrypt]) + AX_CHECK_NETTLE_ALGO([CAST128],[cast128_encrypt]) + AX_CHECK_NETTLE_ALGO([DES],[des_encrypt]) + AX_CHECK_NETTLE_ALGO([DES3],[des3_encrypt]) + AX_CHECK_NETTLE_ALGO([SERPENT],[serpent_encrypt]) + AX_CHECK_NETTLE_ALGO([TWOFISH],[twofish_encrypt]) + # digests + AX_CHECK_NETTLE_ALGO([MD2],[md2_digest]) + AX_CHECK_NETTLE_ALGO([MD4],[md4_digest]) + AX_CHECK_NETTLE_ALGO([MD5],[md5_digest]) + AX_CHECK_NETTLE_ALGO([SHA1],[sha1_digest]) + AX_CHECK_NETTLE_ALGO([SHA256],[sha256_digest]) + ]) + ]) + # complain only if explicitely required + if test "$ax_with_nettle" = "yes" -a "x$HAVE_NETTLE" = "x" ; then + AC_MSG_ERROR([cannot configure required nettle library]) + fi + fi +]) diff --git a/md5.c b/md5.c index e18f224..aec1921 100644 --- a/md5.c +++ b/md5.c @@ -41,227 +41,6 @@ 64-byte boundary. (RFC 1321, 3.1: Step 1) */ static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; - -/* Initialize structure containing state of computation. - (RFC 1321, 3.3: Step 3) */ -void -md5_init_ctx (ctx) - struct md5_ctx *ctx; -{ - ctx->A = 0x67452301; - ctx->B = 0xefcdab89; - ctx->C = 0x98badcfe; - ctx->D = 0x10325476; - - ctx->total[0] = ctx->total[1] = 0; - ctx->buflen = 0; -} - -/* Put result from CTX in first 16 bytes following RESBUF. The result - must be in little endian byte order. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -void * -md5_read_ctx (ctx, resbuf) - const struct md5_ctx *ctx; - void *resbuf; -{ - ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); - ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); - ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); - ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); - - return resbuf; -} - -/* Process the remaining bytes in the internal buffer and the usual - prolog according to the standard and write the result to RESBUF. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -void * -md5_finish_ctx (ctx, resbuf) - struct md5_ctx *ctx; - void *resbuf; -{ - /* Take yet unprocessed bytes into account. */ - md5_uint32 bytes = ctx->buflen; - size_t pad; - - /* Now count remaining bytes. */ - ctx->total[0] += bytes; - if (ctx->total[0] < bytes) - ++ctx->total[1]; - - pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; - memcpy (&ctx->buffer[bytes], fillbuf, pad); - - /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); - *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); - - /* Process last bytes. */ - md5_process_block (ctx->buffer, bytes + pad + 8, ctx); - - 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 (stream, resblock) - 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; -} - -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -void * -md5_buffer (buffer, len, resblock) - const char *buffer; - size_t len; - void *resblock; -{ - struct md5_ctx ctx; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Process whole buffer but last len % 64 bytes. */ - md5_process_bytes (buffer, len, &ctx); - - /* Put result in desired memory area. */ - return md5_finish_ctx (&ctx, resblock); -} - - -void -md5_process_bytes (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; -{ - /* When we already have some bits in our internal buffer concatenate - both inputs first. */ - if (ctx->buflen != 0) - { - size_t left_over = ctx->buflen; - size_t add = 128 - left_over > len ? len : 128 - left_over; - - memcpy (&ctx->buffer[left_over], buffer, add); - ctx->buflen += add; - - if (ctx->buflen > 64) - { - md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); - - ctx->buflen &= 63; - /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], - ctx->buflen); - } - - buffer = (const char *) buffer + add; - len -= add; - } - - /* Process available complete blocks. */ - if (len >= 64) - { -#if !_STRING_ARCH_unaligned -/* To check alignment gcc has an appropriate operator. Other - compilers don't. */ -# if __GNUC__ >= 2 -# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0) -# else -# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0) -# endif - if (UNALIGNED_P (buffer)) - while (len > 64) - { - md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); - buffer = (const char *) buffer + 64; - len -= 64; - } - else -#endif - { - md5_process_block (buffer, len & ~63, ctx); - buffer = (const char *) buffer + (len & ~63); - len &= 63; - } - } - - /* Move remaining bytes in internal buffer. */ - if (len > 0) - { - size_t left_over = ctx->buflen; - - memcpy (&ctx->buffer[left_over], buffer, len); - left_over += len; - if (left_over >= 64) - { - md5_process_block (ctx->buffer, 64, ctx); - left_over -= 64; - memcpy (ctx->buffer, &ctx->buffer[64], left_over); - } - ctx->buflen = left_over; - } -} - - /* These are the four functions used in the four steps of the MD5 algorithm and defined in the RFC 1321. The first function is a little bit optimized (as found in Colin Plumbs public domain implementation). */ @@ -280,14 +59,14 @@ md5_process_block (buffer, len, ctx) size_t len; struct md5_ctx *ctx; { - md5_uint32 correct_words[16]; - const md5_uint32 *words = buffer; - size_t nwords = len / sizeof (md5_uint32); - const md5_uint32 *endp = words + nwords; - md5_uint32 A = ctx->A; - md5_uint32 B = ctx->B; - md5_uint32 C = ctx->C; - md5_uint32 D = ctx->D; + uint32_t correct_words[16]; + const uint32_t *words = buffer; + size_t nwords = len / sizeof (uint32_t); + const uint32_t *endp = words + nwords; + uint32_t A = ctx->A; + uint32_t B = ctx->B; + uint32_t C = ctx->C; + uint32_t D = ctx->D; /* First increment the byte count. RFC 1321 specifies the possible length of the file up to 2^64 bits. Here we only compute the @@ -300,11 +79,11 @@ md5_process_block (buffer, len, ctx) the loop. */ while (words < endp) { - md5_uint32 *cwp = correct_words; - md5_uint32 A_save = A; - md5_uint32 B_save = B; - md5_uint32 C_save = C; - md5_uint32 D_save = D; + uint32_t *cwp = correct_words; + uint32_t A_save = A; + uint32_t B_save = B; + uint32_t C_save = C; + uint32_t D_save = D; /* First round: using the given function, the context and a constant the next context is computed. Because the algorithms processing @@ -431,3 +210,138 @@ md5_process_block (buffer, len, ctx) ctx->C = C; ctx->D = D; } + +/* Initialize structure containing state of computation. + (RFC 1321, 3.3: Step 3) */ +void +md5_init(struct md5_ctx *ctx) +{ + ctx->A = 0x67452301; + ctx->B = 0xefcdab89; + ctx->C = 0x98badcfe; + ctx->D = 0x10325476; + + ctx->total[0] = ctx->total[1] = 0; + ctx->buflen = 0; +} + +/* Put result from CTX in first 16 bytes following RESBUF. The result + must be in little endian byte order. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +void +md5_read_ctx (ctx, resbuf) + const struct md5_ctx *ctx; + void *resbuf; +{ + ((uint32_t *) resbuf)[0] = SWAP (ctx->A); + ((uint32_t *) resbuf)[1] = SWAP (ctx->B); + ((uint32_t *) resbuf)[2] = SWAP (ctx->C); + ((uint32_t *) resbuf)[3] = SWAP (ctx->D); +} + +/* Process the remaining bytes in the internal buffer and the usual + prolog according to the standard and write the result to RESBUF. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +void +md5_digest(struct md5_ctx *ctx, unsigned length, uint8_t *resbuf) +{ + /* Take yet unprocessed bytes into account. */ + uint32_t bytes = ctx->buflen; + size_t pad; + + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; + + pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; + memcpy (&ctx->buffer[bytes], fillbuf, pad); + + /* Put the 64-bit file length in *bits* at the end of the buffer. */ + *(uint32_t *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); + *(uint32_t *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | + (ctx->total[0] >> 29)); + + /* Process last bytes. */ + md5_process_block (ctx->buffer, bytes + pad + 8, ctx); + + md5_read_ctx(ctx, resbuf); + + return; +} + +void +md5_update(struct md5_ctx *ctx, unsigned length, + const uint8_t *buffer) +{ + /* When we already have some bits in our internal buffer concatenate + both inputs first. */ + if (ctx->buflen != 0) + { + size_t left_over = ctx->buflen; + size_t add = 128 - left_over > length ? length : 128 - left_over; + + memcpy (&ctx->buffer[left_over], buffer, add); + ctx->buflen += add; + + if (ctx->buflen > 64) + { + md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); + + ctx->buflen &= 63; + /* The regions in the following copy operation cannot overlap. */ + memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], + ctx->buflen); + } + + buffer = (const uint8_t *) buffer + add; + length -= add; + } + + /* Process available complete blocks. */ + if (length >= 64) + { +#if !_STRING_ARCH_unaligned +/* To check alignment gcc has an appropriate operator. Other + compilers don't. */ +# if __GNUC__ >= 2 +# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (uint32_t) != 0) +# else +# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (uint32_t) != 0) +# endif + if (UNALIGNED_P (buffer)) + while (length > 64) + { + md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); + buffer = (const uint8_t *) buffer + 64; + length -= 64; + } + else +#endif + { + md5_process_block (buffer, length & ~63, ctx); + buffer = (const uint8_t *) buffer + (length & ~63); + length &= 63; + } + } + + /* Move remaining bytes in internal buffer. */ + if (length > 0) + { + size_t left_over = ctx->buflen; + + memcpy (&ctx->buffer[left_over], buffer, length); + left_over += length; + if (left_over >= 64) + { + md5_process_block (ctx->buffer, 64, ctx); + left_over -= 64; + memcpy (ctx->buffer, &ctx->buffer[64], left_over); + } + ctx->buflen = left_over; + } +} diff --git a/md5.h b/md5.h index 57eba21..92f45db 100644 --- a/md5.h +++ b/md5.h @@ -22,21 +22,21 @@ #define _MD5_H 1 #include +#include #include -typedef u_int32_t md5_uint32; typedef size_t md5_uintptr; /* Structure to save state of computation between the single steps. */ struct md5_ctx { char buffer[128]; - md5_uint32 A; - md5_uint32 B; - md5_uint32 C; - md5_uint32 D; + uint32_t A; + uint32_t B; + uint32_t C; + uint32_t D; - md5_uint32 total[2]; - md5_uint32 buflen; + uint32_t total[2]; + uint32_t buflen; }; /* @@ -46,21 +46,14 @@ struct md5_ctx /* Initialize structure containing state of computation. (RFC 1321, 3.3: Step 3) */ -extern void md5_init_ctx (struct md5_ctx *ctx); - -/* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is necessary that LEN is a multiple of 64!!! */ -extern void md5_process_block (const void *buffer, size_t len, - struct md5_ctx *ctx); +extern void md5_init(struct md5_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 64. */ -extern void md5_process_bytes (const void *buffer, size_t len, - struct md5_ctx *ctx); +extern void md5_update(struct md5_ctx *ctx, unsigned length, + const uint8_t *buffer); /* Process the remaining bytes in the buffer and put result from CTX in first 16 bytes following RESBUF. The result is always in little @@ -69,28 +62,6 @@ extern void md5_process_bytes (const void *buffer, size_t len, IMPORTANT: On some systems it is required that RESBUF is correctly aligned for a 32 bits value. */ -extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf); - - -/* Put result from CTX in first 16 bytes following RESBUF. The result is - always in little endian byte order, so that a byte-wise output yields - to the wanted ASCII representation of the message digest. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -extern void *md5_read_ctx (const struct md5_ctx *ctx, void *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. */ -extern int md5_stream (FILE *stream, void *resblock); - -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -extern void *md5_buffer (const char *buffer, size_t len, - void *resblock); +extern void md5_digest(struct md5_ctx *ctx, unsigned length, uint8_t *resbuf); #endif /* md5.h */ diff --git a/sha1.c b/sha1.c index 604cd61..859440e 100644 --- a/sha1.c +++ b/sha1.c @@ -65,7 +65,7 @@ CHAR64LONG16 block[1]; /* use array to appear as a pointer */ */ CHAR64LONG16* block = (const CHAR64LONG16*)buffer; #endif - /* Copy context->state[] to working vars */ + /* Copy ctx->state[] to working vars */ a = state[0]; b = state[1]; c = state[2]; @@ -92,7 +92,7 @@ CHAR64LONG16* block = (const CHAR64LONG16*)buffer; R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); - /* Add the working vars back into context.state[] */ + /* Add the working vars back into ctx.state[] */ state[0] += a; state[1] += b; state[2] += c; @@ -103,53 +103,53 @@ CHAR64LONG16* block = (const CHAR64LONG16*)buffer; /* SHA1Init - Initialize new context */ -void SHA1Init(SHA1_CTX* context) +void sha1_init(struct sha1_ctx *ctx) { /* SHA1 initialization constants */ - context->state[0] = 0x67452301; - context->state[1] = 0xEFCDAB89; - context->state[2] = 0x98BADCFE; - context->state[3] = 0x10325476; - context->state[4] = 0xC3D2E1F0; - context->count[0] = context->count[1] = 0; + ctx->state[0] = 0x67452301; + ctx->state[1] = 0xEFCDAB89; + ctx->state[2] = 0x98BADCFE; + ctx->state[3] = 0x10325476; + ctx->state[4] = 0xC3D2E1F0; + ctx->count[0] = ctx->count[1] = 0; } /* Run your data through this. */ -void SHA1Update(SHA1_CTX* context, const unsigned char* data, u_int32_t len) +void sha1_update(struct sha1_ctx *ctx, unsigned length, const uint8_t *data) { u_int32_t i; u_int32_t j; - j = context->count[0]; - if ((context->count[0] += len << 3) < j) - context->count[1]++; - context->count[1] += (len>>29); + j = ctx->count[0]; + if ((ctx->count[0] += length << 3) < j) + ctx->count[1]++; + ctx->count[1] += (length >> 29); j = (j >> 3) & 63; - if ((j + len) > 63) { - memcpy(&context->buffer[j], data, (i = 64-j)); - SHA1Transform(context->state, context->buffer); - for ( ; i + 63 < len; i += 64) { - SHA1Transform(context->state, &data[i]); + if ((j + length) > 63) { + memcpy(&ctx->buffer[j], data, (i = 64-j)); + SHA1Transform(ctx->state, ctx->buffer); + for ( ; i + 63 < length; i += 64) { + SHA1Transform(ctx->state, &data[i]); } j = 0; } else i = 0; - memcpy(&context->buffer[j], &data[i], len - i); + memcpy(&ctx->buffer[j], &data[i], length - i); } /* Add padding and return the message digest. */ -void SHA1Final(unsigned char digest[20], SHA1_CTX* context) +void sha1_digest(struct sha1_ctx *ctx, unsigned length, uint8_t *digest) { unsigned i; unsigned char finalcount[8]; unsigned char c; #if 0 /* untested "improvement" by DHR */ - /* Convert context->count to a sequence of bytes + /* Convert ctx->count to a sequence of bytes * in finalcount. Second element first, but * big-endian order within element. * But we do it all backwards. @@ -158,7 +158,7 @@ unsigned char c; for (i = 0; i < 2; i++) { - u_int32_t t = context->count[i]; + u_int32_t t = ctx->count[i]; int j; for (j = 0; j < 4; t >>= 8, j++) @@ -166,19 +166,19 @@ unsigned char c; } #else for (i = 0; i < 8; i++) { - finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] + finalcount[i] = (unsigned char)((ctx->count[(i >= 4 ? 0 : 1)] >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ } #endif c = 0200; - SHA1Update(context, &c, 1); - while ((context->count[0] & 504) != 448) { + sha1_update(ctx, 1, &c); + while ((ctx->count[0] & 504) != 448) { c = 0000; - SHA1Update(context, &c, 1); + sha1_update(ctx, 1, &c); } - SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ + sha1_update(ctx, 8, finalcount); /* Should cause a SHA1Transform() */ for (i = 0; i < 20; i++) { digest[i] = (unsigned char) - ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + ((ctx->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); } } diff --git a/sha1.h b/sha1.h index ea0b4db..487160a 100644 --- a/sha1.h +++ b/sha1.h @@ -9,18 +9,18 @@ #ifndef _SHA1_H # define _SHA1_H +#include #include -typedef struct { - u_int32_t state[5]; - u_int32_t count[2]; +struct sha1_ctx { + uint32_t state[5]; + uint32_t count[2]; unsigned char buffer[64]; -} SHA1_CTX; +}; -void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64]); -void SHA1Init(SHA1_CTX* context); -void SHA1Update(SHA1_CTX* context, const unsigned char* data, u_int32_t len); -void SHA1Final(unsigned char digest[20], SHA1_CTX* context); +void sha1_init(struct sha1_ctx *ctx); +void sha1_update(struct sha1_ctx *ctx, unsigned length, const uint8_t *data); +void sha1_digest(struct sha1_ctx *ctx, unsigned length, uint8_t *digest); # define SHA1_Transform SHA1Transform # define SHA1_Init SHA1Init