[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Alignment issue with sha1 code from gnulib
From: |
Jim Meyering |
Subject: |
Re: Alignment issue with sha1 code from gnulib |
Date: |
Wed, 30 Jan 2008 13:31:22 +0100 |
David Shaw <address@hidden> wrote:
> Peter Palfrader reported a bug against the sha1 code in paperkey, but
> that code actually comes from gnulib, so I'm referring it to you.
>
> The issue comes up (as noted in the comment) if resbuf is not 32-bit
> aligned. Rather than requiring all programs that use the gnulib sha1
> code to align their result buffer, Peter's patch seems to make more
> sense in that it "just works" on any platform.
>
> David
>
> ----- Forwarded message from Peter Palfrader <address@hidden> -----
>
> This seems to fix the issue:
>
> -sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
> +sha1_read_ctx (const struct sha1_ctx *ctx, char *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);
> - ((uint32_t *) resbuf)[4] = SWAP (ctx->E);
> + set_uint32(&resbuf[0*4], SWAP (ctx->A));
> + set_uint32(&resbuf[1*4], SWAP (ctx->B));
> + set_uint32(&resbuf[2*4], SWAP (ctx->C));
> + set_uint32(&resbuf[3*4], SWAP (ctx->D));
> + set_uint32(&resbuf[4*4], SWAP (ctx->E));
Thanks for the suggestion. It looks like a good one.
However, I don't want to change the type of the resbuf parameter.
Here's the change I'm considering:
diff --git a/lib/sha1.c b/lib/sha1.c
index 035f898..d9e99d8 100644
--- a/lib/sha1.c
+++ b/lib/sha1.c
@@ -67,28 +67,32 @@ sha1_init_ctx (struct sha1_ctx *ctx)
ctx->buflen = 0;
}
-/* Put result from CTX in first 20 bytes following RESBUF. The result
- must be in little endian byte order.
+/* Copy the 4 byte 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 */
+void
+set_uint32 (char *cp, uint32_t v)
+{
+ memcpy (cp, &v, 4);
+}
- IMPORTANT: On some systems it is required that RESBUF is correctly
- aligned for a 32-bit value. */
+/* Put result from CTX in first 20 bytes following RESBUF. The result
+ must be in little endian byte order. */
void *
sha1_read_ctx (const struct sha1_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);
- ((uint32_t *) resbuf)[4] = SWAP (ctx->E);
+ char *r = resbuf;
+ set_uint32 (r + 0*4, SWAP (ctx->A));
+ set_uint32 (r + 1*4, SWAP (ctx->B));
+ set_uint32 (r + 2*4, SWAP (ctx->C));
+ set_uint32 (r + 3*4, SWAP (ctx->D));
+ set_uint32 (r + 4*4, SWAP (ctx->E));
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. */
void *
sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
{
diff --git a/lib/sha1.h b/lib/sha1.h
index 9545f0b..9e55974 100644
--- a/lib/sha1.h
+++ b/lib/sha1.h
@@ -1,6 +1,7 @@
/* Declarations of functions and data types used for SHA1 sum
library functions.
- Copyright (C) 2000, 2001, 2003, 2005, 2006 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2001, 2003, 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 the
@@ -57,19 +58,13 @@ extern void sha1_process_bytes (const void *buffer, size_t
len,
/* Process the remaining bytes in the buffer and put result from CTX
in first 20 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 *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
/* Put result from CTX in first 20 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 *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
- Alignment issue with sha1 code from gnulib, David Shaw, 2008/01/29
- Re: Alignment issue with sha1 code from gnulib,
Jim Meyering <=
- Re: Alignment issue with sha1 code from gnulib, Jim Meyering, 2008/01/30
- Re: Alignment issue with sha1 code from gnulib, Bruno Haible, 2008/01/30
- Re: Alignment issue with sha1 code from gnulib, Jim Meyering, 2008/01/31
- Re: Alignment issue with sha1 code from gnulib, Simon Josefsson, 2008/01/31
- Re: Alignment issue with sha1 code from gnulib, Bruno Haible, 2008/01/31
- Re: Alignment issue with sha1 code from gnulib, Jim Meyering, 2008/01/31
- Re: Alignment issue with sha1 code from gnulib, Simon Josefsson, 2008/01/31
- Re: Alignment issue with sha1 code from gnulib, Simon Josefsson, 2008/01/31
- Re: Alignment issue with sha1 code from gnulib, Simon Josefsson, 2008/01/31
Re: Alignment issue with sha1 code from gnulib, Jim Meyering, 2008/01/31