diff options
author | Roland McGrath <roland@hack.frob.com> | 2011-07-09 03:17:24 -0700 |
---|---|---|
committer | Roland McGrath <roland@hack.frob.com> | 2011-07-09 03:17:24 -0700 |
commit | 32899ac4f69d4ca4856d5282464c1f9cee928c8a (patch) | |
tree | 2b45a22f79f4f3464150d5eba73a89ecab0cee22 /lib | |
parent | 02a958bc2662c1c9c2d6b663742b9c8e720e25b2 (diff) | |
download | elfutils-32899ac4f69d4ca4856d5282464c1f9cee928c8a.tar.gz |
Clean up byte order handling in md5 and sha1 code.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ChangeLog | 10 | ||||
-rw-r--r-- | lib/md5.c | 29 | ||||
-rw-r--r-- | lib/sha1.c | 26 | ||||
-rw-r--r-- | lib/system.h | 14 |
4 files changed, 54 insertions, 25 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog index 1b8b42bc..cf776884 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,13 @@ +2011-07-09 Roland McGrath <roland@hack.frob.com> + + * sha1.c (be64_copy): New function. + (sha1_finish_ctx): Use it. + * md5.c (le64_copy): New function. + (md5_finish_ctx): Use it. + * system.h (LE32, BE32): New macros, using <endian.h> and <byteswap.h>. + * md5.c (SWAP): Use LE32. + * sha1.c (SWAP): Use BE32. + 2010-06-16 Roland McGrath <roland@redhat.com> * dynamicsizehash.h (HASHTYPE): New macro. @@ -1,6 +1,6 @@ /* 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,1997,1999,2000,2001,2005 Red Hat, Inc. + Copyright (C) 1995-2011 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper <drepper@redhat.com>, 1995. @@ -29,20 +29,14 @@ # include <config.h> #endif -#include <endian.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include "md5.h" +#include "system.h" -#if __BYTE_ORDER == __BIG_ENDIAN -# define SWAP(n) \ - (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) -#else -# define SWAP(n) (n) -#endif - +#define SWAP(n) LE32 (n) /* This array contains the bytes used to pad the buffer to the next 64-byte boundary. (RFC 1321, 3.1: Step 1) */ @@ -82,6 +76,16 @@ md5_read_ctx (ctx, resbuf) return resbuf; } +static void +le64_copy (char *dest, uint64_t x) +{ + for (size_t i = 0; i < 8; ++i) + { + dest[i] = (uint8_t) x; + x >>= 8; + } +} + /* Process the remaining bytes in the internal buffer and the usual prolog according to the standard and write the result to RESBUF. @@ -105,9 +109,10 @@ md5_finish_ctx (ctx, resbuf) 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)); + const uint64_t bit_length = ((ctx->total[0] << 3) + + ((uint64_t) ((ctx->total[1] << 3) | + (ctx->total[0] >> 29)) << 32)); + le64_copy (&ctx->buffer[bytes + pad], bit_length); /* Process last bytes. */ md5_process_block (ctx->buffer, bytes + pad + 8, ctx); @@ -1,6 +1,6 @@ /* Functions to compute SHA1 message digest of files or memory blocks. according to the definition of SHA1 in FIPS 180-1 from April 1997. - Copyright (C) 2008 Red Hat, Inc. + Copyright (C) 2008-2011 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper <drepper@redhat.com>, 2008. @@ -29,20 +29,14 @@ # include <config.h> #endif -#include <endian.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include "sha1.h" +#include "system.h" -#if __BYTE_ORDER == __LITTLE_ENDIAN -# include <byteswap.h> -# define SWAP(n) bswap_32 (n) -#else -# define SWAP(n) (n) -#endif - +#define SWAP(n) BE32 (n) /* This array contains the bytes used to pad the buffer to the next 64-byte boundary. */ @@ -83,6 +77,13 @@ sha1_read_ctx (ctx, resbuf) return resbuf; } +static void +be64_copy (char *dest, uint64_t x) +{ + for (size_t i = 8; i-- > 0; x >>= 8) + dest[i] = (uint8_t) x; +} + /* Process the remaining bytes in the internal buffer and the usual prolog according to the standard and write the result to RESBUF. @@ -106,9 +107,10 @@ sha1_finish_ctx (ctx, resbuf) memcpy (&ctx->buffer[bytes], fillbuf, pad); /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(sha1_uint32 *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); - *(sha1_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3); + const uint64_t bit_length = ((ctx->total[0] << 3) + + ((uint64_t) ((ctx->total[1] << 3) | + (ctx->total[0] >> 29)) << 32)); + be64_copy (&ctx->buffer[bytes + pad], bit_length); /* Process last bytes. */ sha1_process_block (ctx->buffer, bytes + pad + 8, ctx); diff --git a/lib/system.h b/lib/system.h index 10b4734a..26954264 100644 --- a/lib/system.h +++ b/lib/system.h @@ -1,5 +1,5 @@ /* Declarations for common convenience functions. - Copyright (C) 2006, 2009 Red Hat, Inc. + Copyright (C) 2006-2011 Red Hat, Inc. This file is part of Red Hat elfutils. Red Hat elfutils is free software; you can redistribute it and/or modify @@ -51,6 +51,18 @@ #include <stddef.h> #include <stdint.h> +#include <endian.h> +#include <byteswap.h> + +#if __BYTE_ORDER == __LITTLE_ENDIAN +# define LE32(n) (n) +# define BE32(n) bswap_32 (n) +#elif __BYTE_ORDER == __BIG_ENDIAN +# define BE32(n) (n) +# define LE32(n) bswap_32 (n) +#else +# error "Unknown byte order" +#endif extern void *xmalloc (size_t) __attribute__ ((__malloc__)); extern void *xcalloc (size_t, size_t) __attribute__ ((__malloc__)); |