diff options
author | Simon Josefsson <simon@josefsson.org> | 2005-10-18 23:09:55 +0000 |
---|---|---|
committer | Simon Josefsson <simon@josefsson.org> | 2005-10-18 23:09:55 +0000 |
commit | 31c60687fe4f674770be628bec20d6b2f3aadbb2 (patch) | |
tree | 211d92510a80d3e1e19094990d1ce1e9be9f147d | |
parent | ffb3938725768678c65c9e82d63f9f4949554114 (diff) | |
download | gnutls-31c60687fe4f674770be628bec20d6b2f3aadbb2.tar.gz |
Add gc-md4.
-rw-r--r-- | gl/Makefile.am | 2 | ||||
-rw-r--r-- | gl/gc-gnulib.c | 18 | ||||
-rw-r--r-- | gl/gc-libgcrypt.c | 46 | ||||
-rw-r--r-- | gl/gc.h | 2 | ||||
-rw-r--r-- | gl/m4/gc-md4.m4 | 14 | ||||
-rw-r--r-- | gl/m4/gnulib-cache.m4 | 4 | ||||
-rw-r--r-- | gl/m4/gnulib-comp.m4 | 5 | ||||
-rw-r--r-- | gl/m4/md4.m4 | 14 | ||||
-rw-r--r-- | gl/md4.c | 381 | ||||
-rw-r--r-- | gl/md4.h | 88 |
10 files changed, 571 insertions, 3 deletions
diff --git a/gl/Makefile.am b/gl/Makefile.am index db77019540..8bf1bc253c 100644 --- a/gl/Makefile.am +++ b/gl/Makefile.am @@ -8,7 +8,7 @@ # the same distribution terms as the rest of that program. # # Generated by gnulib-tool. -# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=gl --m4-base=gl/m4 --aux-dir=. --lgpl --libtool --macro-prefix=gl gc gc-hmac-md5 gc-md5 gc-pbkdf2-sha1 gc-sha1 getline getpass inet_ntop memmem memmove minmax readline snprintf socklen +# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=gl --m4-base=gl/m4 --aux-dir=. --lgpl --libtool --macro-prefix=gl gc gc-hmac-md5 gc-md4 gc-md5 gc-pbkdf2-sha1 gc-sha1 getline getpass inet_ntop memmem memmove minmax readline snprintf socklen AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies diff --git a/gl/gc-gnulib.c b/gl/gc-gnulib.c index 9d6cfebd6f..ff2d1c85c0 100644 --- a/gl/gc-gnulib.c +++ b/gl/gc-gnulib.c @@ -37,6 +37,9 @@ #include <fcntl.h> #include <errno.h> +#ifdef GC_USE_MD4 +# include "md4.h" +#endif #ifdef GC_USE_MD5 # include "md5.h" #endif @@ -149,6 +152,12 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) { switch (hash) { +#ifdef GC_USE_MD4 + case GC_MD4: + md4_buffer (in, inlen, resbuf); + break; +#endif + #ifdef GC_USE_MD5 case GC_MD5: md5_buffer (in, inlen, resbuf); @@ -168,6 +177,15 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) return GC_OK; } +#ifdef GC_USE_MD4 +Gc_rc +gc_md4 (const void *in, size_t inlen, void *resbuf) +{ + md4_buffer (in, inlen, resbuf); + return GC_OK; +} +#endif + #ifdef GC_USE_MD5 Gc_rc gc_md5 (const void *in, size_t inlen, void *resbuf) diff --git a/gl/gc-libgcrypt.c b/gl/gc-libgcrypt.c index 7c1ffa0497..810bd8d7ec 100644 --- a/gl/gc-libgcrypt.c +++ b/gl/gc-libgcrypt.c @@ -222,6 +222,10 @@ gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle) switch (hash) { + case GC_MD4: + gcryalg = GCRY_MD_MD4; + break; + case GC_MD5: gcryalg = GCRY_MD_MD5; break; @@ -278,6 +282,10 @@ gc_hash_digest_length (Gc_hash hash) switch (hash) { + case GC_MD4: + gcryalg = GCRY_MD_MD4; + break; + case GC_MD5: gcryalg = GCRY_MD_MD5; break; @@ -333,6 +341,12 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) switch (hash) { +#ifdef GC_USE_MD4 + case GC_MD4: + gcryalg = GCRY_MD_MD4; + break; +#endif + #ifdef GC_USE_MD5 case GC_MD5: gcryalg = GCRY_MD_MD5; @@ -362,6 +376,38 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) /* One-call interface. */ +#ifdef GC_USE_MD4 +Gc_rc +gc_md4 (const void *in, size_t inlen, void *resbuf) +{ + size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4); + gcry_md_hd_t hd; + gpg_error_t err; + unsigned char *p; + + assert (outlen == GC_MD4_DIGEST_SIZE); + + err = gcry_md_open (&hd, GCRY_MD_MD4, 0); + if (err != GPG_ERR_NO_ERROR) + return GC_INVALID_HASH; + + gcry_md_write (hd, in, inlen); + + p = gcry_md_read (hd, GCRY_MD_MD4); + if (p == NULL) + { + gcry_md_close (hd); + return GC_INVALID_HASH; + } + + memcpy (resbuf, p, outlen); + + gcry_md_close (hd); + + return GC_OK; +} +#endif + #ifdef GC_USE_MD5 Gc_rc gc_md5 (const void *in, size_t inlen, void *resbuf) @@ -41,6 +41,7 @@ typedef enum Gc_rc Gc_rc; /* Hash types. */ enum Gc_hash { + GC_MD4, GC_MD5, GC_SHA1, GC_MD2, @@ -56,6 +57,7 @@ typedef enum Gc_hash_mode Gc_hash_mode; typedef void *gc_hash_handle; +#define GC_MD4_DIGEST_SIZE 16 #define GC_MD5_DIGEST_SIZE 16 #define GC_SHA1_DIGEST_SIZE 20 diff --git a/gl/m4/gc-md4.m4 b/gl/m4/gc-md4.m4 new file mode 100644 index 0000000000..b72dbbb0ee --- /dev/null +++ b/gl/m4/gc-md4.m4 @@ -0,0 +1,14 @@ +# gc-md4.m4 serial 1 +dnl Copyright (C) 2005 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_GC_MD4], +[ + AC_REQUIRE([gl_GC]) + AC_DEFINE(GC_USE_MD4, 1, [Define to if you want to support MD4 through GC.]) + if test "$ac_cv_libgcrypt" != yes; then + gl_MD4 + fi +]) diff --git a/gl/m4/gnulib-cache.m4 b/gl/m4/gnulib-cache.m4 index 767b0e2702..ded84cfe7b 100644 --- a/gl/m4/gnulib-cache.m4 +++ b/gl/m4/gnulib-cache.m4 @@ -14,10 +14,10 @@ # Specification in the form of a command-line invocation: -# gnulib-tool --import --dir=. --lib=libgnu --source-base=gl --m4-base=gl/m4 --aux-dir=. --lgpl --libtool --macro-prefix=gl gc gc-hmac-md5 gc-md5 gc-pbkdf2-sha1 gc-sha1 getline getpass inet_ntop memmem memmove minmax readline snprintf socklen +# gnulib-tool --import --dir=. --lib=libgnu --source-base=gl --m4-base=gl/m4 --aux-dir=. --lgpl --libtool --macro-prefix=gl gc gc-hmac-md5 gc-md4 gc-md5 gc-pbkdf2-sha1 gc-sha1 getline getpass inet_ntop memmem memmove minmax readline snprintf socklen # Specification in the form of a few gnulib-tool.m4 macro invocations: -gl_MODULES([gc gc-hmac-md5 gc-md5 gc-pbkdf2-sha1 gc-sha1 getline getpass inet_ntop memmem memmove minmax readline snprintf socklen]) +gl_MODULES([gc gc-hmac-md5 gc-md4 gc-md5 gc-pbkdf2-sha1 gc-sha1 getline getpass inet_ntop memmem memmove minmax readline snprintf socklen]) gl_AVOID([]) gl_SOURCE_BASE([gl]) gl_M4_BASE([gl/m4]) diff --git a/gl/m4/gnulib-comp.m4 b/gl/m4/gnulib-comp.m4 index 48153ebfa0..f9bc88242f 100644 --- a/gl/m4/gnulib-comp.m4 +++ b/gl/m4/gnulib-comp.m4 @@ -29,6 +29,7 @@ AC_DEFUN([gl_INIT], gl_GC gl_GC_HMAC_MD5 gl_GC_HMAC_SHA1 + gl_GC_MD4 gl_GC_MD5 gl_GC_PBKDF2_SHA1 gl_GC_SHA1 @@ -71,6 +72,8 @@ AC_DEFUN([gl_FILE_LIST], [ lib/hmac.h lib/inet_ntop.c lib/inet_ntop.h + lib/md4.c + lib/md4.h lib/md5.c lib/md5.h lib/memmem.c @@ -99,6 +102,7 @@ AC_DEFUN([gl_FILE_LIST], [ m4/eoverflow.m4 m4/gc-hmac-md5.m4 m4/gc-hmac-sha1.m4 + m4/gc-md4.m4 m4/gc-md5.m4 m4/gc-pbkdf2-sha1.m4 m4/gc-sha1.m4 @@ -117,6 +121,7 @@ AC_DEFUN([gl_FILE_LIST], [ m4/lib-prefix.m4 m4/longdouble.m4 m4/longlong.m4 + m4/md4.m4 m4/md5.m4 m4/memmem.m4 m4/memmove.m4 diff --git a/gl/m4/md4.m4 b/gl/m4/md4.m4 new file mode 100644 index 0000000000..1df2e3e82b --- /dev/null +++ b/gl/m4/md4.m4 @@ -0,0 +1,14 @@ +# md4.m4 serial 1 +dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_MD4], +[ + AC_LIBSOURCES([md4.c, md4.h]) + AC_LIBOBJ([md4]) + + dnl Prerequisites of lib/md4.c. + AC_REQUIRE([AC_C_BIGENDIAN]) +]) diff --git a/gl/md4.c b/gl/md4.c new file mode 100644 index 0000000000..e1cecd0b22 --- /dev/null +++ b/gl/md4.c @@ -0,0 +1,381 @@ +/* Functions to compute MD4 message digest of files or memory blocks. + according to the definition of MD4 in RFC 1320 from April 1992. + Copyright (C) 1995,1996,1997,1999,2000,2001,2002,2003,2005 + Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2.1, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Adapted by Simon Josefsson from gnulib md5.? and Libgcrypt + cipher/md4.c . */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "md4.h" + +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> + +#if USE_UNLOCKED_IO +# include "unlocked-io.h" +#endif + +#ifdef WORDS_BIGENDIAN +# define SWAP(n) \ + (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) +#else +# define SWAP(n) (n) +#endif + +#define BLOCKSIZE 4096 +#if BLOCKSIZE % 64 != 0 +# error "invalid BLOCKSIZE" +#endif + +/* This array contains the bytes used to pad the buffer to the next + 64-byte boundary. (RFC 1320, 3.1: Step 1) */ +static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; + + +/* Initialize structure containing state of computation. + (RFC 1320, 3.3: Step 3) */ +void +md4_init_ctx (struct md4_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 * +md4_read_ctx (const struct md4_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); + + 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 * +md4_finish_ctx (struct md4_ctx *ctx, void *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. */ + md4_process_block (ctx->buffer, bytes + pad + 8, ctx); + + return md4_read_ctx (ctx, resbuf); +} + +/* Compute MD4 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +int +md4_stream (FILE * stream, void *resblock) +{ + struct md4_ctx ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + md4_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. */ + while (1) + { + n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + + if (sum == BLOCKSIZE) + break; + + if (n == 0) + { + /* Check for the error flag IFF N == 0, so that we don't + exit the loop after a partial read due to e.g., EAGAIN + or EWOULDBLOCK. */ + if (ferror (stream)) + return 1; + goto process_partial_block; + } + + /* We've read at least one byte, so ignore errors. But always + check for EOF, since feof may be true even though N > 0. + Otherwise, we could end up calling fread after EOF. */ + if (feof (stream)) + goto process_partial_block; + } + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + md4_process_block (buffer, BLOCKSIZE, &ctx); + } + +process_partial_block:; + + /* Process any remaining bytes. */ + if (sum > 0) + md4_process_bytes (buffer, sum, &ctx); + + /* Construct result in desired memory. */ + md4_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 * +md4_buffer (const char *buffer, size_t len, void *resblock) +{ + struct md4_ctx ctx; + + /* Initialize the computation context. */ + md4_init_ctx (&ctx); + + /* Process whole buffer but last len % 64 bytes. */ + md4_process_bytes (buffer, len, &ctx); + + /* Put result in desired memory area. */ + return md4_finish_ctx (&ctx, resblock); +} + +void +md4_process_bytes (const void *buffer, size_t len, struct md4_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) + { + md4_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) (((uintptr_t) p) % __alignof__ (uint32_t) != 0) +# else +# define alignof(type) offsetof (struct { char c; type x; }, x) +# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) +# endif + if (UNALIGNED_P (buffer)) + while (len > 64) + { + md4_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); + buffer = (const char *) buffer + 64; + len -= 64; + } + else +#endif + { + md4_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) + { + md4_process_block (ctx->buffer, 64, ctx); + left_over -= 64; + memcpy (ctx->buffer, &ctx->buffer[64], left_over); + } + ctx->buflen = left_over; + } +} + +/* --- Code below is the primary difference between md5.c and md4.c --- */ + +/* MD4 round constants */ +#define K1 0x5a827999L +#define K2 0x6ed9eba1L + +/* Round functions. */ +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) ) +#define R1(a,b,c,d,k,s) a=rol(a+F(b,c,d)+x[k],s); +#define R2(a,b,c,d,k,s) a=rol(a+G(b,c,d)+x[k]+K1,s); +#define R3(a,b,c,d,k,s) a=rol(a+H(b,c,d)+x[k]+K2,s); + +/* Process LEN bytes of BUFFER, accumulating context into CTX. + It is assumed that LEN % 64 == 0. */ + +void +md4_process_block (const void *buffer, size_t len, struct md4_ctx *ctx) +{ + const uint32_t *words = buffer; + size_t nwords = len / sizeof (uint32_t); + const uint32_t *endp = words + nwords; + uint32_t x[16]; + 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 1320 specifies the possible + length of the file up to 2^64 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; + + /* Process all bytes in the buffer with 64 bytes in each round of + the loop. */ + while (words < endp) + { + uint32_t tm; + int t; + for (t = 0; t < 16; t++) + { + x[t] = SWAP (*words); + words++; + } + + /* Round 1. */ + R1 (A, B, C, D, 0, 3); + R1 (D, A, B, C, 1, 7); + R1 (C, D, A, B, 2, 11); + R1 (B, C, D, A, 3, 19); + R1 (A, B, C, D, 4, 3); + R1 (D, A, B, C, 5, 7); + R1 (C, D, A, B, 6, 11); + R1 (B, C, D, A, 7, 19); + R1 (A, B, C, D, 8, 3); + R1 (D, A, B, C, 9, 7); + R1 (C, D, A, B, 10, 11); + R1 (B, C, D, A, 11, 19); + R1 (A, B, C, D, 12, 3); + R1 (D, A, B, C, 13, 7); + R1 (C, D, A, B, 14, 11); + R1 (B, C, D, A, 15, 19); + + /* Round 2. */ + R2 (A, B, C, D, 0, 3); + R2 (D, A, B, C, 4, 5); + R2 (C, D, A, B, 8, 9); + R2 (B, C, D, A, 12, 13); + R2 (A, B, C, D, 1, 3); + R2 (D, A, B, C, 5, 5); + R2 (C, D, A, B, 9, 9); + R2 (B, C, D, A, 13, 13); + R2 (A, B, C, D, 2, 3); + R2 (D, A, B, C, 6, 5); + R2 (C, D, A, B, 10, 9); + R2 (B, C, D, A, 14, 13); + R2 (A, B, C, D, 3, 3); + R2 (D, A, B, C, 7, 5); + R2 (C, D, A, B, 11, 9); + R2 (B, C, D, A, 15, 13); + + /* Round 3. */ + R3 (A, B, C, D, 0, 3); + R3 (D, A, B, C, 8, 9); + R3 (C, D, A, B, 4, 11); + R3 (B, C, D, A, 12, 15); + R3 (A, B, C, D, 2, 3); + R3 (D, A, B, C, 10, 9); + R3 (C, D, A, B, 6, 11); + R3 (B, C, D, A, 14, 15); + R3 (A, B, C, D, 1, 3); + R3 (D, A, B, C, 9, 9); + R3 (C, D, A, B, 5, 11); + R3 (B, C, D, A, 13, 15); + R3 (A, B, C, D, 3, 3); + R3 (D, A, B, C, 11, 9); + R3 (C, D, A, B, 7, 11); + R3 (B, C, D, A, 15, 15); + + A = ctx->A += A; + B = ctx->B += B; + C = ctx->C += C; + D = ctx->D += D; + } +} diff --git a/gl/md4.h b/gl/md4.h new file mode 100644 index 0000000000..f9417ef8e9 --- /dev/null +++ b/gl/md4.h @@ -0,0 +1,88 @@ +/* Declarations of functions and data types used for MD4 sum + library functions. + Copyright (C) 2000, 2001, 2003, 2005 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2.1, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifndef MD4_H +# define MD4_H 1 + +# include <stdio.h> +# include <stdint.h> + +#define MD4_DIGEST_SIZE 16 + +/* Structure to save state of computation between the single steps. */ +struct md4_ctx +{ + uint32_t A; + uint32_t B; + uint32_t C; + uint32_t D; + + uint32_t total[2]; + uint32_t buflen; + char buffer[128] __attribute__ ((__aligned__ (__alignof__ (uint32_t)))); +}; + + +/* Initialize structure containing state of computation. */ +extern void md4_init_ctx (struct md4_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 md4_process_block (const void *buffer, size_t len, + struct md4_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 md4_process_bytes (const void *buffer, size_t len, + struct md4_ctx *ctx); + +/* Process the remaining bytes in the buffer and 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 be correctly + aligned for a 32 bits value. */ +extern void *md4_finish_ctx (struct md4_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 *md4_read_ctx (const struct md4_ctx *ctx, void *resbuf); + + +/* Compute MD4 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +extern int md4_stream (FILE * stream, void *resblock); + +/* Compute MD4 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 *md4_buffer (const char *buffer, size_t len, void *resblock); + +#endif |