bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

md5 and sha1 fixes from coreutils (plus a few of my own)


From: Paul Eggert
Subject: md5 and sha1 fixes from coreutils (plus a few of my own)
Date: Mon, 12 Sep 2005 15:06:46 -0700
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

I installed this:

2005-09-12  Paul Eggert  <address@hidden>

        Merge glibc and coreutils changes into gnulib, plus a few
        extra fixes.
        * lib/md5.c: Use #error rather than a string.
        (CYCLIC): New macro, from glibc source.  Use it instead of rol.
        * lib/md5.h (__GNUC_PREREQ, __THROW): Define if not defined already.
        (__attribute__): Define to empty for non recent-GCC.
        (__md5_buffer, __md5_finish_ctx, __md5_init_ctx, __md5_process_block):
        (__md5_process_bytes, __md5_read_ctx, __md5_stream):
        Renamed from their non-__ counterparts, with new macros replacing
        them if not _LIBC.  Add __THROW attribute.
        (rol): Remove.
        (struct md5_ctx): Align buffer if using GCC.
        * lib/sha1.h (struct sha1_ctx): Likewise.
        * lib/sha1.c (SWAP): Renamed from NOTSWAP.  All uses changed.
        The old name was backwards.
        (NOTSWAP): Remove; not used.
        (rol): New macro, moved here from md5.h.
        (sha1_process_block): Remove a FIXME that doesn't make sense.

Index: lib/md5.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/md5.c,v
retrieving revision 1.19
diff -p -u -r1.19 md5.c
--- lib/md5.c   14 May 2005 06:03:58 -0000      1.19
+++ lib/md5.c   12 Sep 2005 22:04:54 -0000
@@ -1,6 +1,6 @@
 /* md5.c - Functions to compute MD5 message digest of files or memory blocks
    according to the definition of MD5 in RFC 1321 from April 1992.
-   Copyright (C) 1995, 1996, 2001, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1995, 1996, 2001, 2003, 2004, 2005 Free Software Foundation, 
Inc.
    NOTE: The canonical source of this file is maintained with the GNU C
    Library.  Bugs can be reported to address@hidden
 
@@ -57,10 +57,8 @@
 #endif
 
 #define BLOCKSIZE 4096
-/* Ensure that BLOCKSIZE is a multiple of 64.  */
 #if BLOCKSIZE % 64 != 0
-/* FIXME-someday (soon?): use #error instead of this kludge.  */
-"invalid BLOCKSIZE"
+# error "invalid BLOCKSIZE"
 #endif
 
 /* This array contains the bytes used to pad the buffer to the next
@@ -335,15 +333,22 @@ md5_process_block (const void *buffer, s
         {                                                              \
          a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;             \
          ++words;                                                      \
-         a = rol (a, s);                                               \
+         CYCLIC (a, s);                                                \
          a += b;                                                       \
         }                                                              \
       while (0)
 
+      /* It is unfortunate that C does not provide an operator for
+        cyclic rotation.  Hope the C compiler is smart enough.  */
+#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
+
       /* Before we start, one word to the strange constants.
         They are defined in RFC 1321 as
 
-        T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64, or
+        T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
+
+        Here is an equivalent invocation using Perl:
+
         perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin 
$_))}'
        */
 
@@ -373,7 +378,7 @@ md5_process_block (const void *buffer, s
       do                                                               \
        {                                                               \
          a += f (b, c, d) + correct_words[k] + T;                      \
-         a = rol (a, s);                                               \
+         CYCLIC (a, s);                                                \
          a += b;                                                       \
        }                                                               \
       while (0)
Index: lib/md5.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/md5.h,v
retrieving revision 1.16
diff -p -u -r1.16 md5.h
--- lib/md5.h   14 May 2005 06:03:58 -0000      1.16
+++ lib/md5.h   12 Sep 2005 22:04:54 -0000
@@ -1,8 +1,6 @@
-/* md5.h - Declaration of functions and data types used for MD5 sum
-   computing library functions.
-
-   Copyright (C) 1995, 1996, 1999, 2000, 2003, 2004 Free Software
-   Foundation, Inc.
+/* Declaration of functions and data types used for MD5 sum computing
+   library functions.
+   Copyright (C) 1995-1997,1999-2005 Free Software Foundation, Inc.
 
    NOTE: The canonical source of this file is maintained with the GNU C
    Library.  Bugs can be reported to address@hidden
@@ -33,6 +31,39 @@
 # include <stdint.h>
 #endif
 
+#ifndef __GNUC_PREREQ
+# if defined __GNUC__ && defined __GNUC_MINOR__
+#  define __GNUC_PREREQ(maj, min) \
+       ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
+# else
+#  define __GNUC_PREREQ(maj, min) 0
+# endif
+#endif
+
+#ifndef __THROW
+# if defined __cplusplus && __GNUC_PREREQ (2,8)
+#  define __THROW      throw ()
+# else
+#  define __THROW
+# endif
+#endif
+
+#ifndef __attribute__
+# if ! __GNUC_PREREQ (2,8) || __STRICT_ANSI__
+#  define __attribute__(x)
+# endif
+#endif
+
+#ifndef _LIBC
+# define __md5_buffer md5_buffer
+# define __md5_finish_ctx md5_finish_ctx
+# define __md5_init_ctx md5_init_ctx
+# define __md5_process_block md5_process_block
+# define __md5_process_bytes md5_process_bytes
+# define __md5_read_ctx md5_read_ctx
+# define __md5_stream md5_stream
+#endif
+
 typedef uint32_t md5_uint32;
 
 /* Structure to save state of computation between the single steps.  */
@@ -45,7 +76,7 @@ struct md5_ctx
 
   md5_uint32 total[2];
   md5_uint32 buflen;
-  char buffer[128];
+  char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
 };
 
 /*
@@ -55,21 +86,21 @@ struct md5_ctx
 
 /* Initialize structure containing state of computation.
    (RFC 1321, 3.3: Step 3)  */
-extern void md5_init_ctx (struct md5_ctx *ctx);
+extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
 
 /* 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_process_block (const void *buffer, size_t len,
+                                struct md5_ctx *ctx) __THROW;
 
 /* 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_process_bytes (const void *buffer, size_t len,
+                                struct md5_ctx *ctx) __THROW;
 
 /* Process the remaining bytes in the buffer and put result from CTX
    in first 16 bytes following RESBUF.  The result is always in little
@@ -78,7 +109,7 @@ extern void md5_process_bytes (const voi
 
    IMPORTANT: On some systems it is required that RESBUF be correctly
    aligned for a 32 bits value.  */
-extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
+extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW;
 
 
 /* Put result from CTX in first 16 bytes following RESBUF.  The result is
@@ -87,20 +118,19 @@ extern void *md5_finish_ctx (struct md5_
 
    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);
+extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW;
 
 
 /* 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);
+extern int __md5_stream (FILE *stream, void *resblock) __THROW;
 
 /* 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_buffer (const char *buffer, size_t len,
+                          void *resblock) __THROW;
 
-#define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
-
-#endif
+#endif /* md5.h */
Index: lib/sha1.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/sha1.c,v
retrieving revision 1.3
diff -p -u -r1.3 sha1.c
--- lib/sha1.c  14 May 2005 06:03:58 -0000      1.3
+++ lib/sha1.c  12 Sep 2005 22:04:54 -0000
@@ -1,7 +1,7 @@
 /* sha1.c - Functions to compute SHA1 message digest of files or
    memory blocks according to the NIST specification FIPS-180-1.
 
-   Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the
@@ -35,26 +35,19 @@
 # include "unlocked-io.h"
 #endif
 
-/*
-  Not-swap is a macro that does an endian swap on architectures that are
-  big-endian, as SHA1 needs some data in a little-endian format
-*/
+/* SWAP does an endian swap on architectures that are little-endian,
+   as SHA1 needs some data in a big-endian form.  */
 
 #ifdef WORDS_BIGENDIAN
-# define NOTSWAP(n) (n)
-# define SWAP(n)                                                       \
-    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
+# define SWAP(n) (n)
 #else
-# define NOTSWAP(n)                                                         \
+# define SWAP(n) \
     (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
-# define SWAP(n) (n)
 #endif
 
 #define BLOCKSIZE 4096
-/* Ensure that BLOCKSIZE is a multiple of 64.  */
 #if BLOCKSIZE % 64 != 0
-/* FIXME-someday (soon?): use #error instead of this kludge.  */
-"invalid BLOCKSIZE"
+# error "invalid BLOCKSIZE"
 #endif
 
 /* This array contains the bytes used to pad the buffer to the next
@@ -88,11 +81,11 @@ sha1_init_ctx (struct sha1_ctx *ctx)
 void *
 sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
 {
-  ((md5_uint32 *) resbuf)[0] = NOTSWAP (ctx->A);
-  ((md5_uint32 *) resbuf)[1] = NOTSWAP (ctx->B);
-  ((md5_uint32 *) resbuf)[2] = NOTSWAP (ctx->C);
-  ((md5_uint32 *) resbuf)[3] = NOTSWAP (ctx->D);
-  ((md5_uint32 *) resbuf)[4] = NOTSWAP (ctx->E);
+  ((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);
+  ((md5_uint32 *) resbuf)[4] = SWAP (ctx->E);
 
   return resbuf;
 }
@@ -118,8 +111,8 @@ sha1_finish_ctx (struct sha1_ctx *ctx, v
   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 + 4] = NOTSWAP (ctx->total[0] << 3);
-  *(md5_uint32 *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) |
+  *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3);
+  *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) |
                                                    (ctx->total[0] >> 29));
 
   /* Process last bytes.  */
@@ -317,6 +310,8 @@ sha1_process_block (const void *buffer, 
   if (ctx->total[0] < len)
     ++ctx->total[1];
 
+#define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
+
 #define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
                    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
               , (x[I&0x0f] = rol(tm, 1)) )
@@ -332,10 +327,9 @@ sha1_process_block (const void *buffer, 
     {
       md5_uint32 tm;
       int t;
-      /* FIXME: see sha1.c for a better implementation.  */
       for (t = 0; t < 16; t++)
        {
-         x[t] = NOTSWAP (*words);
+         x[t] = SWAP (*words);
          words++;
        }
 
Index: lib/sha1.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/sha1.h,v
retrieving revision 1.2
diff -p -u -r1.2 sha1.h
--- lib/sha1.h  14 May 2005 06:03:58 -0000      1.2
+++ lib/sha1.h  12 Sep 2005 22:04:54 -0000
@@ -1,6 +1,6 @@
 /* Declarations of functions and data types used for SHA1 sum
    library functions.
-   Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
+   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 General Public License as published by the
@@ -33,7 +33,7 @@ struct sha1_ctx
 
   md5_uint32 total[2];
   md5_uint32 buflen;
-  char buffer[128];
+  char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
 };
 
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]