[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
md5 merge from glibc, plus sha1 fixes
From: |
Paul Eggert |
Subject: |
md5 merge from glibc, plus sha1 fixes |
Date: |
Tue, 13 Sep 2005 16:33:50 -0700 |
User-agent: |
Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) |
I installed this:
2005-09-13 Paul Eggert <address@hidden>
Merge md5 from libc, and clean up some sha1 glitches.
* md5.h (__GNUC_PREREQ, __THROW, __attribute__): New macros.
(__md5_buffer): Renamed from md5_buffer. Add a macro undoing this
if _LIBC is not defined. Add __THROW.
(__md5_finish_ctx, __md5_init_ctx, __md5_process_block):
(__md5_process_bytes, __md5_read_ctx, __md5_stream): Likewise.
(struct md5_ctx): Mark buffer as being aligned.
(rol): Remove; all uses changed to CYCLIC.
* md5.c (CYCLIC): New macro. All uses of rol changed to use CYCLIC.
Redo comment to minimize changes from libc.
* sha1.h (struct sha1_ctx): Mark buffer as being aligned.
* sha1.c (SWAP): Renamed from NOTSWAP, to avoid a horrible misnaming.
All uses changed. Remove the old SWAP.
Remove obvious comment about BLOCKSIZE.
(rol): New macro, moved here from md5.h.
(sha1_process_block): Remove an incoherent FIXME comment.
Index: lib/md5.c
===================================================================
RCS file: /fetish/cu/lib/md5.c,v
retrieving revision 1.24
diff -p -u -r1.24 md5.c
--- lib/md5.c 27 Aug 2005 16:11:43 -0000 1.24
+++ lib/md5.c 13 Sep 2005 23:24:30 -0000
@@ -57,7 +57,6 @@
#endif
#define BLOCKSIZE 4096
-/* Ensure that BLOCKSIZE is a multiple of 64. */
#if BLOCKSIZE % 64 != 0
# error "invalid BLOCKSIZE"
#endif
@@ -334,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
$_))}'
*/
@@ -372,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: /fetish/cu/lib/md5.h,v
retrieving revision 1.18
diff -p -u -r1.18 md5.h
--- lib/md5.h 14 May 2005 07:58:06 -0000 1.18
+++ lib/md5.h 13 Sep 2005 23:24:30 -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: /fetish/cu/lib/sha1.c,v
retrieving revision 1.7
diff -p -u -r1.7 sha1.c
--- lib/sha1.c 27 Aug 2005 16:11:53 -0000 1.7
+++ lib/sha1.c 13 Sep 2005 23:24:31 -0000
@@ -35,23 +35,17 @@
# 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
# error "invalid BLOCKSIZE"
#endif
@@ -87,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;
}
@@ -117,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. */
@@ -316,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)) )
@@ -331,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: /fetish/cu/lib/sha1.h,v
retrieving revision 1.4
diff -p -u -r1.4 sha1.h
--- lib/sha1.h 14 May 2005 07:58:07 -0000 1.4
+++ lib/sha1.h 13 Sep 2005 23:24:31 -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))));
};
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- md5 merge from glibc, plus sha1 fixes,
Paul Eggert <=