diff options
Diffstat (limited to 'lib/sha256.c')
-rw-r--r-- | lib/sha256.c | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/lib/sha256.c b/lib/sha256.c index 8f4bb9580..4a632c9fb 100644 --- a/lib/sha256.c +++ b/lib/sha256.c @@ -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) { |