summaryrefslogtreecommitdiff
path: root/lib/sha512.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2006-01-12 07:17:25 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2006-01-12 07:17:25 +0000
commit1834ba06ac55a1c627ed601e5cf7f649dbde12f0 (patch)
treef8d1a4d55a5f31b8a2fc3e5785ed74603591b93a /lib/sha512.c
parent3024d13d7eda94e23e9f9710f6d4c5fb6908ce8d (diff)
downloadcoreutils-1834ba06ac55a1c627ed601e5cf7f649dbde12f0.tar.xz
Fix commentary to match md5 better. Adjust to .h file changes.
Diffstat (limited to 'lib/sha512.c')
-rw-r--r--lib/sha512.c49
1 files changed, 21 insertions, 28 deletions
diff --git a/lib/sha512.c b/lib/sha512.c
index 11c3b1d16..79c03e216 100644
--- a/lib/sha512.c
+++ b/lib/sha512.c
@@ -34,28 +34,22 @@
# include "unlocked-io.h"
#endif
-/*
- Not-swap is a macro that does an endian swap on architectures that are
- big-endian, as SHA512 needs some data in a little-endian format
-*/
-
#ifdef WORDS_BIGENDIAN
-# define NOTSWAP(n) (n)
+# define SWAP(n) (n)
#else
-# define NOTSWAP(n) \
+# define SWAP(n) \
(((n) << 56) | (((n) & 0xff00) << 40) | (((n) & 0xff0000UL) << 24) \
- | (((n) & 0xff000000UL) << 8) | (((n) >> 8) & 0xff000000UL) \
- | (((n) >> 24) & 0xff0000UL) | (((n) >> 40) & 0xff00UL) | ((n) >> 56))
+ | (((n) & 0xff000000UL) << 8) | (((n) >> 8) & 0xff000000UL) \
+ | (((n) >> 24) & 0xff0000UL) | (((n) >> 40) & 0xff00UL) | ((n) >> 56))
#endif
#define BLOCKSIZE 4096
-/* Ensure that BLOCKSIZE is a multiple of 128. */
#if BLOCKSIZE % 128 != 0
# error "invalid BLOCKSIZE"
#endif
/* This array contains the bytes used to pad the buffer to the next
- 64-byte boundary. */
+ 128-byte boundary. */
static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ... */ };
@@ -106,8 +100,8 @@ sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
{
int i;
- for ( i=0 ; i<8 ; i++ )
- ((uint64_t *) resbuf)[i] = NOTSWAP (ctx->state[i]);
+ for (i = 0; i < 8; i++)
+ ((uint64_t *) resbuf)[i] = SWAP (ctx->state[i]);
return resbuf;
}
@@ -117,8 +111,8 @@ sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
{
int i;
- for ( i=0 ; i<6 ; i++ )
- ((uint64_t *) resbuf)[i] = NOTSWAP (ctx->state[i]);
+ for (i = 0; i < 6; i++)
+ ((uint64_t *) resbuf)[i] = SWAP (ctx->state[i]);
return resbuf;
}
@@ -133,23 +127,21 @@ sha512_conclude_ctx (struct sha512_ctx *ctx)
{
/* Take yet unprocessed bytes into account. */
uint64_t bytes = ctx->buflen;
- size_t pad;
+ size_t size = (bytes < 112) ? 128 / 8 : 128 * 2 / 8;
/* Now count remaining bytes. */
ctx->total[0] += bytes;
if (ctx->total[0] < bytes)
++ctx->total[1];
- pad = bytes >= 112 ? 128 + 112 - bytes : 112 - bytes;
- memcpy (&ctx->buffer[bytes], fillbuf, pad);
-
/* Put the 64-bit file length in *bits* at the end of the buffer. */
- *(uint64_t *) &ctx->buffer[bytes + pad + 8] = NOTSWAP (ctx->total[0] << 3);
- *(uint64_t *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) |
- (ctx->total[0] >> 61));
+ ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 61));
+ ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
+
+ memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 8 - bytes);
/* Process last bytes. */
- sha512_process_block (ctx->buffer, bytes + pad + 16, ctx);
+ sha512_process_block (ctx->buffer, size * 8, ctx);
}
void *
@@ -340,7 +332,7 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
size_t left_over = ctx->buflen;
size_t add = 256 - left_over > len ? len : 256 - left_over;
- memcpy (&ctx->buffer[left_over], buffer, add);
+ memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
ctx->buflen += add;
if (ctx->buflen > 128)
@@ -349,7 +341,8 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
ctx->buflen &= 127;
/* The regions in the following copy operation cannot overlap. */
- memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~127],
+ memcpy (ctx->buffer,
+ &((char *) ctx->buffer)[(left_over + add) & ~127],
ctx->buflen);
}
@@ -384,13 +377,13 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
{
size_t left_over = ctx->buflen;
- memcpy (&ctx->buffer[left_over], buffer, len);
+ memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
left_over += len;
if (left_over >= 128)
{
sha512_process_block (ctx->buffer, 128, ctx);
left_over -= 128;
- memcpy (ctx->buffer, &ctx->buffer[128], left_over);
+ memcpy (ctx->buffer, &ctx->buffer[16], left_over);
}
ctx->buflen = left_over;
}
@@ -469,7 +462,7 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
/* FIXME: see sha1.c for a better implementation. */
for (t = 0; t < 16; t++)
{
- x[t] = NOTSWAP (*words);
+ x[t] = SWAP (*words);
words++;
}