]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Remove alignment constraint from the sha*_read_ctx functions.
authorJim Meyering <meyering@redhat.com>
Thu, 31 Jan 2008 12:51:10 +0000 (13:51 +0100)
committerJim Meyering <meyering@redhat.com>
Thu, 31 Jan 2008 13:45:02 +0000 (14:45 +0100)
* lib/sha256.c (set_uint32): New function.
(sha256_read_ctx, sha224_read_ctx): Use it.
* lib/sha512.c (set_uint64): New function.
(sha512_read_ctx, sha384_read_ctx): Use it.
* lib/sha256.h: Remove warning about alignment constraint.
* lib/sha512.h: Likewise.
Prompted by similar changes in gnulib's sha1 and md[45] modules.

ChangeLog
lib/sha256.c
lib/sha256.h
lib/sha512.c
lib/sha512.h

index 214fa02bf3f8bb6105fa2ac01c828b29bca57e3a..d2c615aabae11ff2c6aa233fb35ee31e353d4063 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2008-01-31  Jim Meyering  <meyering@redhat.com>
 
+       Remove alignment constraint from the sha*_read_ctx functions.
+       * lib/sha256.c (set_uint32): New function.
+       (sha256_read_ctx, sha224_read_ctx): Use it.
+       * lib/sha512.c (set_uint64): New function.
+       (sha512_read_ctx, sha384_read_ctx): Use it.
+       * lib/sha256.h: Remove warning about alignment constraint.
+       * lib/sha512.h: Likewise.
+       Prompted by similar changes in gnulib's sha1 and md[45] modules.
+
        Adapt to new version of vc-list-files.
        * tests/check.mk (vc_exe_in_TESTS): Adapt to new constraint
        that vc-list-files be run only from $(top_srcdir).
index 8f4bb95805cb31ad977ed32569bdb5c54dcdd649..4a632c9fbdc7d192a0412b7356868e1124aab690 100644 (file)
@@ -1,7 +1,7 @@
 /* sha256.c - Functions to compute SHA256 and SHA224 message digest of files or
    memory blocks according to the NIST specification FIPS-180-2.
 
-   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006, 2008 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
@@ -85,18 +85,25 @@ sha224_init_ctx (struct sha256_ctx *ctx)
   ctx->buflen = 0;
 }
 
-/* Put result from CTX in first 32 bytes following RESBUF.  The result
-   must be in little endian byte order.
+/* Copy the value from v into the memory location pointed to by *cp,
+   If your architecture allows unaligned access this is equivalent to
+   * (uint32_t *) cp = v  */
+static inline void
+set_uint32 (char *cp, uint32_t v)
+{
+  memcpy (cp, &v, sizeof v);
+}
 
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32-bit value.  */
+/* Put result from CTX in first 32 bytes following RESBUF.  The result
+   must be in little endian byte order.  */
 void *
 sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
 {
   int i;
+  char *r = resbuf;
 
   for (i = 0; i < 8; i++)
-    ((uint32_t *) resbuf)[i] = SWAP (ctx->state[i]);
+    set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
 
   return resbuf;
 }
@@ -105,18 +112,16 @@ void *
 sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
 {
   int i;
+  char *r = resbuf;
 
   for (i = 0; i < 7; i++)
-    ((uint32_t *) resbuf)[i] = SWAP (ctx->state[i]);
+    set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
 
   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-bit value.  */
+   prolog according to the standard and write the result to RESBUF.  */
 static void
 sha256_conclude_ctx (struct sha256_ctx *ctx)
 {
index 32f26bab9511296d4ad7adf853ab1d33708ecbc7..9fd83c94cf5d36275a55938a6ae989d2bfac0577 100644 (file)
@@ -1,6 +1,6 @@
 /* Declarations of functions and data types used for SHA256 and SHA224 sum
    library functions.
-   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006, 2008 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
@@ -53,20 +53,14 @@ extern void sha256_process_bytes (const void *buffer, size_t len,
 /* Process the remaining bytes in the buffer and put result from CTX
    in first 32 (28) 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.  */
+   ASCII representation of the message digest.  */
 extern void *sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf);
 extern void *sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf);
 
 
 /* Put result from CTX in first 32 (28) 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.  */
+   to the wanted ASCII representation of the message digest.  */
 extern void *sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf);
 extern void *sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf);
 
index 230296e19a00b28794f448baa4021b2178faff62..e0109f80fafddeba6c237c658fa023a189582efb 100644 (file)
@@ -1,7 +1,7 @@
 /* sha512.c - Functions to compute SHA512 and SHA384 message digest of files or
    memory blocks according to the NIST specification FIPS-180-2.
 
-   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006, 2008 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
@@ -92,18 +92,25 @@ sha384_init_ctx (struct sha512_ctx *ctx)
   ctx->buflen = 0;
 }
 
-/* Put result from CTX in first 64 bytes following RESBUF.  The result
-   must be in little endian byte order.
+/* Copy the value from V into the memory location pointed to by *CP,
+   If your architecture allows unaligned access, this is equivalent to
+   * (__typeof__ (v) *) cp = v  */
+static inline void
+set_uint64 (char *cp, u64 v)
+{
+  memcpy (cp, &v, sizeof v);
+}
 
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 64-bit value.  */
+/* Put result from CTX in first 64 bytes following RESBUF.
+   The result must be in little endian byte order.  */
 void *
 sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
 {
   int i;
+  char *r = resbuf;
 
   for (i = 0; i < 8; i++)
-    ((u64 *) resbuf)[i] = SWAP (ctx->state[i]);
+    set_uint64 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
 
   return resbuf;
 }
@@ -112,18 +119,16 @@ void *
 sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
 {
   int i;
+  char *r = resbuf;
 
   for (i = 0; i < 6; i++)
-    ((u64 *) resbuf)[i] = SWAP (ctx->state[i]);
+    set_uint64 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
 
   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 64-bit value.  */
+   prolog according to the standard and write the result to RESBUF.  */
 static void
 sha512_conclude_ctx (struct sha512_ctx *ctx)
 {
index cc321650c51f63ad6069033179a6e2ceb8bd5343..7ee3ac4f86179f661a823e6c81a8319bbc90e874 100644 (file)
@@ -1,6 +1,6 @@
 /* Declarations of functions and data types used for SHA512 and SHA384 sum
    library functions.
-   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006, 2008 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
@@ -54,10 +54,7 @@ extern void sha512_process_bytes (const void *buffer, size_t len,
 /* Process the remaining bytes in the buffer and put result from CTX
    in first 64 (48) 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 64 bits value.  */
+   ASCII representation of the message digest.  */
 extern void *sha512_finish_ctx (struct sha512_ctx *ctx, void *resbuf);
 extern void *sha384_finish_ctx (struct sha512_ctx *ctx, void *resbuf);