From ae1bef450219bd8e9e4f3e45d63bd7bd2d107445 Mon Sep 17 00:00:00 2001 From: llsth Date: Mon, 9 Mar 2015 23:34:02 +0100 Subject: [PATCH] Reverted to original state --- sha1.c | 754 +++++++++++++++++++++++++++---------------------------- sha1.h | 16 +- sha256.c | 584 ++++++++++++++++++------------------------ sha256.h | 16 +- sha384.c | 522 +++++++++++++++++++------------------- sha384.h | 16 +- sha512.c | 516 ++++++++++++++++++------------------- sha512.h | 16 +- 8 files changed, 1173 insertions(+), 1267 deletions(-) diff --git a/sha1.c b/sha1.c index 2b049c6..513e4e0 100644 --- a/sha1.c +++ b/sha1.c @@ -55,7 +55,7 @@ #ifndef lint static const char rcsid[] = - "$Id: sha1.c 680 2003-07-25 21:57:38Z asaddi $"; + "$Id: sha1.c 680 2003-07-25 21:57:38Z asaddi $"; #endif /* !lint */ #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) @@ -95,9 +95,9 @@ static const char rcsid[] = static inline uint64_t _byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); } #endif /* WORDS_BIGENDIAN */ @@ -113,460 +113,460 @@ static inline uint64_t _byteswap64(uint64_t x) static inline uint64_t __byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); } static inline uint32_t _byteswap(int littleEndian, uint32_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP(x); + if (!littleEndian) + return x; + else + return _BYTESWAP(x); } static inline uint64_t _byteswap64(int littleEndian, uint64_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP64(x); + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); } static inline void setEndian(int *littleEndianp) { - union { - uint32_t w; - uint8_t b[4]; - } endian; + union { + uint32_t w; + uint8_t b[4]; + } endian; - endian.w = 1L; - *littleEndianp = endian.b[0] != 0; + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; } #endif /* !RUNTIME_ENDIAN */ static const uint8_t padding[64] = { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void SHA1Init (SHA1Context *sc) { #ifdef RUNTIME_ENDIAN - setEndian (&sc->littleEndian); + setEndian (&sc->littleEndian); #endif /* RUNTIME_ENDIAN */ - sc->totalLength = 0LL; - sc->hash[0] = 0x67452301L; - sc->hash[1] = 0xefcdab89L; - sc->hash[2] = 0x98badcfeL; - sc->hash[3] = 0x10325476L; - sc->hash[4] = 0xc3d2e1f0L; - sc->bufferLength = 0L; + sc->totalLength = 0LL; + sc->hash[0] = 0x67452301L; + sc->hash[1] = 0xefcdab89L; + sc->hash[2] = 0x98badcfeL; + sc->hash[3] = 0x10325476L; + sc->hash[4] = 0xc3d2e1f0L; + sc->bufferLength = 0L; } static void burnStack (int size) { - char buf[128]; + char buf[128]; - memset (buf, 0, sizeof (buf)); - size -= sizeof (buf); - if (size > 0) - burnStack (size); + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); } static void SHA1Guts (SHA1Context *sc, const uint32_t *cbuf) { - uint32_t buf[80]; - uint32_t *W, *W3, *W8, *W14, *W16; - uint32_t a, b, c, d, e, temp; - int i; + uint32_t buf[80]; + uint32_t *W, *W3, *W8, *W14, *W16; + uint32_t a, b, c, d, e, temp; + int i; - W = buf; + W = buf; - for (i = 15; i >= 0; i--) { - *(W++) = BYTESWAP(*cbuf); - cbuf++; - } + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP(*cbuf); + cbuf++; + } - W16 = &buf[0]; - W14 = &buf[2]; - W8 = &buf[8]; - W3 = &buf[13]; + W16 = &buf[0]; + W14 = &buf[2]; + W8 = &buf[8]; + W3 = &buf[13]; - for (i = 63; i >= 0; i--) { - *W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++); - *W = ROTL(*W, 1); - W++; - } + for (i = 63; i >= 0; i--) { + *W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++); + *W = ROTL(*W, 1); + W++; + } - a = sc->hash[0]; - b = sc->hash[1]; - c = sc->hash[2]; - d = sc->hash[3]; - e = sc->hash[4]; + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; - W = buf; + W = buf; #ifndef SHA1_UNROLL #define SHA1_UNROLL 20 #endif /* !SHA1_UNROLL */ #if SHA1_UNROLL == 1 - for (i = 19; i >= 0; i--) - DO_ROUND(F_0_19, K_0_19); + for (i = 19; i >= 0; i--) + DO_ROUND(F_0_19, K_0_19); - for (i = 19; i >= 0; i--) - DO_ROUND(F_20_39, K_20_39); + for (i = 19; i >= 0; i--) + DO_ROUND(F_20_39, K_20_39); - for (i = 19; i >= 0; i--) - DO_ROUND(F_40_59, K_40_59); + for (i = 19; i >= 0; i--) + DO_ROUND(F_40_59, K_40_59); - for (i = 19; i >= 0; i--) - DO_ROUND(F_60_79, K_60_79); + for (i = 19; i >= 0; i--) + DO_ROUND(F_60_79, K_60_79); #elif SHA1_UNROLL == 2 - for (i = 9; i >= 0; i--) { - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - } + for (i = 9; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } - for (i = 9; i >= 0; i--) { - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - } + for (i = 9; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } - for (i = 9; i >= 0; i--) { - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - } + for (i = 9; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } - for (i = 9; i >= 0; i--) { - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - } + for (i = 9; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } #elif SHA1_UNROLL == 4 - for (i = 4; i >= 0; i--) { - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - } + for (i = 4; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } - for (i = 4; i >= 0; i--) { - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - } + for (i = 4; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } - for (i = 4; i >= 0; i--) { - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - } + for (i = 4; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } - for (i = 4; i >= 0; i--) { - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - } + for (i = 4; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } #elif SHA1_UNROLL == 5 - for (i = 3; i >= 0; i--) { - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } - for (i = 3; i >= 0; i--) { - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } - for (i = 3; i >= 0; i--) { - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } - for (i = 3; i >= 0; i--) { - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } #elif SHA1_UNROLL == 10 - for (i = 1; i >= 0; i--) { - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } - for (i = 1; i >= 0; i--) { - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } - for (i = 1; i >= 0; i--) { - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } - for (i = 1; i >= 0; i--) { - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } #elif SHA1_UNROLL == 20 - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); #else /* SHA1_UNROLL */ #error SHA1_UNROLL must be 1, 2, 4, 5, 10 or 20! #endif - sc->hash[0] += a; - sc->hash[1] += b; - sc->hash[2] += c; - sc->hash[3] += d; - sc->hash[4] += e; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; } void SHA1Update (SHA1Context *sc, const void *vdata, uint32_t len) { - const uint8_t *data = vdata; - uint32_t bufferBytesLeft; - uint32_t bytesToCopy; - int needBurn = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + int needBurn = 0; #ifdef SHA1_FAST_COPY - if (sc->bufferLength) { - bufferBytesLeft = 64L - sc->bufferLength; + if (sc->bufferLength) { + bufferBytesLeft = 64L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - sc->totalLength += bytesToCopy * 8L; + sc->totalLength += bytesToCopy * 8L; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 64L) { - SHA1Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; - } + if (sc->bufferLength == 64L) { + SHA1Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; } + } - while (len > 63) { - sc->totalLength += 512L; + while (len > 63) { + sc->totalLength += 512L; - SHA1Guts (sc, data); - needBurn = 1; + SHA1Guts (sc, data); + needBurn = 1; - data += 64L; - len -= 64L; - } + data += 64L; + len -= 64L; + } - if (len) { - memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); - sc->totalLength += len * 8L; + sc->totalLength += len * 8L; - sc->bufferLength += len; - } + sc->bufferLength += len; + } #else /* SHA1_FAST_COPY */ - while (len) { - bufferBytesLeft = 64L - sc->bufferLength; + while (len) { + bufferBytesLeft = 64L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - sc->totalLength += bytesToCopy * 8L; + sc->totalLength += bytesToCopy * 8L; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 64L) { - SHA1Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; - } + if (sc->bufferLength == 64L) { + SHA1Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; } + } #endif /* SHA1_FAST_COPY */ - if (needBurn) - burnStack (sizeof (uint32_t[86]) + sizeof (uint32_t *[5]) + sizeof (int)); + if (needBurn) + burnStack (sizeof (uint32_t[86]) + sizeof (uint32_t *[5]) + sizeof (int)); } void SHA1Final (SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE]) { - uint32_t bytesToPad; - uint64_t lengthPad; - int i; + uint32_t bytesToPad; + uint64_t lengthPad; + int i; - bytesToPad = 120L - sc->bufferLength; - if (bytesToPad > 64L) - bytesToPad -= 64L; + bytesToPad = 120L - sc->bufferLength; + if (bytesToPad > 64L) + bytesToPad -= 64L; - lengthPad = BYTESWAP64(sc->totalLength); + lengthPad = BYTESWAP64(sc->totalLength); - SHA1Update (sc, padding, bytesToPad); - SHA1Update (sc, &lengthPad, 8L); + SHA1Update (sc, padding, bytesToPad); + SHA1Update (sc, &lengthPad, 8L); - if (hash) { - for (i = 0; i < SHA1_HASH_WORDS; i++) { + if (hash) { + for (i = 0; i < SHA1_HASH_WORDS; i++) { #ifdef SHA1_FAST_COPY - *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); + *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); #else /* SHA1_FAST_COPY */ - hash[0] = (uint8_t) (sc->hash[i] >> 24); - hash[1] = (uint8_t) (sc->hash[i] >> 16); - hash[2] = (uint8_t) (sc->hash[i] >> 8); - hash[3] = (uint8_t) sc->hash[i]; + hash[0] = (uint8_t) (sc->hash[i] >> 24); + hash[1] = (uint8_t) (sc->hash[i] >> 16); + hash[2] = (uint8_t) (sc->hash[i] >> 8); + hash[3] = (uint8_t) sc->hash[i]; #endif /* SHA1_FAST_COPY */ - hash += 4; - } + hash += 4; } + } } #ifdef SHA1_TEST @@ -578,49 +578,49 @@ SHA1Final (SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE]) int main (int argc, char *argv[]) { - SHA1Context foo; - uint8_t hash[SHA1_HASH_SIZE]; - char buf[1000]; - int i; + SHA1Context foo; + uint8_t hash[SHA1_HASH_SIZE]; + char buf[1000]; + int i; - SHA1Init (&foo); - SHA1Update (&foo, "abc", 3); - SHA1Final (&foo, hash); + SHA1Init (&foo); + SHA1Update (&foo, "abc", 3); + SHA1Final (&foo, hash); - for (i = 0; i < SHA1_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - SHA1Init (&foo); - SHA1Update (&foo, - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - 56); - SHA1Final (&foo, hash); + SHA1Init (&foo); + SHA1Update (&foo, + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + 56); + SHA1Final (&foo, hash); - for (i = 0; i < SHA1_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - SHA1Init (&foo); - memset (buf, 'a', sizeof (buf)); - for (i = 0; i < 1000; i++) - SHA1Update (&foo, buf, sizeof (buf)); - SHA1Final (&foo, hash); + SHA1Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA1Update (&foo, buf, sizeof (buf)); + SHA1Final (&foo, hash); - for (i = 0; i < SHA1_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - exit (0); + exit (0); } #endif /* SHA1_TEST */ diff --git a/sha1.h b/sha1.h index 281985e..7a6beb7 100644 --- a/sha1.h +++ b/sha1.h @@ -43,15 +43,15 @@ #define SHA1_HASH_WORDS 5 struct _SHA1Context { - uint64_t totalLength; - uint32_t hash[SHA1_HASH_WORDS]; - uint32_t bufferLength; - union { - uint32_t words[16]; - uint8_t bytes[64]; - } buffer; + uint64_t totalLength; + uint32_t hash[SHA1_HASH_WORDS]; + uint32_t bufferLength; + union { + uint32_t words[16]; + uint8_t bytes[64]; + } buffer; #ifdef RUNTIME_ENDIAN - int littleEndian; + int littleEndian; #endif /* RUNTIME_ENDIAN */ }; diff --git a/sha256.c b/sha256.c index aaab01b..ba31091 100644 --- a/sha256.c +++ b/sha256.c @@ -55,7 +55,7 @@ #ifndef lint static const char rcsid[] = - "$Id: sha256.c 680 2003-07-25 21:57:49Z asaddi $"; + "$Id: sha256.c 680 2003-07-25 21:57:49Z asaddi $"; #endif /* !lint */ #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) @@ -82,22 +82,22 @@ static const char rcsid[] = } static const uint32_t K[64] = { - 0x428a2f98L, 0x71374491L, 0xb5c0fbcfL, 0xe9b5dba5L, - 0x3956c25bL, 0x59f111f1L, 0x923f82a4L, 0xab1c5ed5L, - 0xd807aa98L, 0x12835b01L, 0x243185beL, 0x550c7dc3L, - 0x72be5d74L, 0x80deb1feL, 0x9bdc06a7L, 0xc19bf174L, - 0xe49b69c1L, 0xefbe4786L, 0x0fc19dc6L, 0x240ca1ccL, - 0x2de92c6fL, 0x4a7484aaL, 0x5cb0a9dcL, 0x76f988daL, - 0x983e5152L, 0xa831c66dL, 0xb00327c8L, 0xbf597fc7L, - 0xc6e00bf3L, 0xd5a79147L, 0x06ca6351L, 0x14292967L, - 0x27b70a85L, 0x2e1b2138L, 0x4d2c6dfcL, 0x53380d13L, - 0x650a7354L, 0x766a0abbL, 0x81c2c92eL, 0x92722c85L, - 0xa2bfe8a1L, 0xa81a664bL, 0xc24b8b70L, 0xc76c51a3L, - 0xd192e819L, 0xd6990624L, 0xf40e3585L, 0x106aa070L, - 0x19a4c116L, 0x1e376c08L, 0x2748774cL, 0x34b0bcb5L, - 0x391c0cb3L, 0x4ed8aa4aL, 0x5b9cca4fL, 0x682e6ff3L, - 0x748f82eeL, 0x78a5636fL, 0x84c87814L, 0x8cc70208L, - 0x90befffaL, 0xa4506cebL, 0xbef9a3f7L, 0xc67178f2L + 0x428a2f98L, 0x71374491L, 0xb5c0fbcfL, 0xe9b5dba5L, + 0x3956c25bL, 0x59f111f1L, 0x923f82a4L, 0xab1c5ed5L, + 0xd807aa98L, 0x12835b01L, 0x243185beL, 0x550c7dc3L, + 0x72be5d74L, 0x80deb1feL, 0x9bdc06a7L, 0xc19bf174L, + 0xe49b69c1L, 0xefbe4786L, 0x0fc19dc6L, 0x240ca1ccL, + 0x2de92c6fL, 0x4a7484aaL, 0x5cb0a9dcL, 0x76f988daL, + 0x983e5152L, 0xa831c66dL, 0xb00327c8L, 0xbf597fc7L, + 0xc6e00bf3L, 0xd5a79147L, 0x06ca6351L, 0x14292967L, + 0x27b70a85L, 0x2e1b2138L, 0x4d2c6dfcL, 0x53380d13L, + 0x650a7354L, 0x766a0abbL, 0x81c2c92eL, 0x92722c85L, + 0xa2bfe8a1L, 0xa81a664bL, 0xc24b8b70L, 0xc76c51a3L, + 0xd192e819L, 0xd6990624L, 0xf40e3585L, 0x106aa070L, + 0x19a4c116L, 0x1e376c08L, 0x2748774cL, 0x34b0bcb5L, + 0x391c0cb3L, 0x4ed8aa4aL, 0x5b9cca4fL, 0x682e6ff3L, + 0x748f82eeL, 0x78a5636fL, 0x84c87814L, 0x8cc70208L, + 0x90befffaL, 0xa4506cebL, 0xbef9a3f7L, 0xc67178f2L }; #ifndef RUNTIME_ENDIAN @@ -115,9 +115,9 @@ static const uint32_t K[64] = { static inline uint64_t _byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); } #endif /* WORDS_BIGENDIAN */ @@ -133,389 +133,295 @@ static inline uint64_t _byteswap64(uint64_t x) static inline uint64_t __byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); } static inline uint32_t _byteswap(int littleEndian, uint32_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP(x); + if (!littleEndian) + return x; + else + return _BYTESWAP(x); } static inline uint64_t _byteswap64(int littleEndian, uint64_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP64(x); + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); } static inline void setEndian(int *littleEndianp) { - union { - uint32_t w; - uint8_t b[4]; - } endian; + union { + uint32_t w; + uint8_t b[4]; + } endian; - endian.w = 1L; - *littleEndianp = endian.b[0] != 0; + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; } #endif /* !RUNTIME_ENDIAN */ static const uint8_t padding[64] = { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void SHA256Init (SHA256Context *sc) { #ifdef RUNTIME_ENDIAN - setEndian (&sc->littleEndian); + setEndian (&sc->littleEndian); #endif /* RUNTIME_ENDIAN */ - sc->totalLength = 0LL; - sc->hash[0] = 0x6a09e667L; - sc->hash[1] = 0xbb67ae85L; - sc->hash[2] = 0x3c6ef372L; - sc->hash[3] = 0xa54ff53aL; - sc->hash[4] = 0x510e527fL; - sc->hash[5] = 0x9b05688cL; - sc->hash[6] = 0x1f83d9abL; - sc->hash[7] = 0x5be0cd19L; - sc->bufferLength = 0L; + sc->totalLength = 0LL; + sc->hash[0] = 0x6a09e667L; + sc->hash[1] = 0xbb67ae85L; + sc->hash[2] = 0x3c6ef372L; + sc->hash[3] = 0xa54ff53aL; + sc->hash[4] = 0x510e527fL; + sc->hash[5] = 0x9b05688cL; + sc->hash[6] = 0x1f83d9abL; + sc->hash[7] = 0x5be0cd19L; + sc->bufferLength = 0L; } static void burnStack (int size) { - char buf[128]; + char buf[128]; - memset (buf, 0, sizeof (buf)); - size -= sizeof (buf); - if (size > 0) - burnStack (size); + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); } static void SHA256Guts (SHA256Context *sc, const uint32_t *cbuf) { - uint32_t buf[64]; - uint32_t *W, *W2, *W7, *W15, *W16; - uint32_t a, b, c, d, e, f, g, h; - uint32_t t1, t2; - const uint32_t *Kp; - int i; + uint32_t buf[64]; + uint32_t *W, *W2, *W7, *W15, *W16; + uint32_t a, b, c, d, e, f, g, h; + uint32_t t1, t2; + const uint32_t *Kp; + int i; - W = buf; + W = buf; - for (i = 15; i >= 0; i--) { - *(W++) = BYTESWAP(*cbuf); - cbuf++; - } + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP(*cbuf); + cbuf++; + } - W16 = &buf[0]; - W15 = &buf[1]; - W7 = &buf[9]; - W2 = &buf[14]; + W16 = &buf[0]; + W15 = &buf[1]; + W7 = &buf[9]; + W2 = &buf[14]; - for (i = 47; i >= 0; i--) { - *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); - W2++; - W15++; - } + for (i = 47; i >= 0; i--) { + *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); + W2++; + W15++; + } - a = sc->hash[0]; - b = sc->hash[1]; - c = sc->hash[2]; - d = sc->hash[3]; - e = sc->hash[4]; - f = sc->hash[5]; - g = sc->hash[6]; - h = sc->hash[7]; + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; + f = sc->hash[5]; + g = sc->hash[6]; + h = sc->hash[7]; - Kp = K; - W = buf; + Kp = K; + W = buf; #ifndef SHA256_UNROLL #define SHA256_UNROLL 1 #endif /* !SHA256_UNROLL */ #if SHA256_UNROLL == 1 - for (i = 63; i >= 0; i--) - DO_ROUND(); + for (i = 63; i >= 0; i--) + DO_ROUND(); #elif SHA256_UNROLL == 2 - for (i = 31; i >= 0; i--) { - DO_ROUND(); - DO_ROUND(); - } + for (i = 31; i >= 0; i--) { + DO_ROUND(); DO_ROUND(); + } #elif SHA256_UNROLL == 4 - for (i = 15; i >= 0; i--) { - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - } + for (i = 15; i >= 0; i--) { + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + } #elif SHA256_UNROLL == 8 - for (i = 7; i >= 0; i--) { - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - } + for (i = 7; i >= 0; i--) { + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + } #elif SHA256_UNROLL == 16 - for (i = 3; i >= 0; i--) { - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + } #elif SHA256_UNROLL == 32 - for (i = 1; i >= 0; i--) { - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + } #elif SHA256_UNROLL == 64 - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); - DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); #else #error "SHA256_UNROLL must be 1, 2, 4, 8, 16, 32, or 64!" #endif - sc->hash[0] += a; - sc->hash[1] += b; - sc->hash[2] += c; - sc->hash[3] += d; - sc->hash[4] += e; - sc->hash[5] += f; - sc->hash[6] += g; - sc->hash[7] += h; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; + sc->hash[5] += f; + sc->hash[6] += g; + sc->hash[7] += h; } void SHA256Update (SHA256Context *sc, const void *vdata, uint32_t len) { - const uint8_t *data = vdata; - uint32_t bufferBytesLeft; - uint32_t bytesToCopy; - int needBurn = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + int needBurn = 0; #ifdef SHA256_FAST_COPY - if (sc->bufferLength) { - bufferBytesLeft = 64L - sc->bufferLength; + if (sc->bufferLength) { + bufferBytesLeft = 64L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - sc->totalLength += bytesToCopy * 8L; + sc->totalLength += bytesToCopy * 8L; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 64L) { - SHA256Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; - } + if (sc->bufferLength == 64L) { + SHA256Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; } + } - while (len > 63L) { - sc->totalLength += 512L; + while (len > 63L) { + sc->totalLength += 512L; - SHA256Guts (sc, data); - needBurn = 1; + SHA256Guts (sc, data); + needBurn = 1; - data += 64L; - len -= 64L; - } + data += 64L; + len -= 64L; + } - if (len) { - memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); - sc->totalLength += len * 8L; + sc->totalLength += len * 8L; - sc->bufferLength += len; - } + sc->bufferLength += len; + } #else /* SHA256_FAST_COPY */ - while (len) { - bufferBytesLeft = 64L - sc->bufferLength; + while (len) { + bufferBytesLeft = 64L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - sc->totalLength += bytesToCopy * 8L; + sc->totalLength += bytesToCopy * 8L; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 64L) { - SHA256Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; - } + if (sc->bufferLength == 64L) { + SHA256Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; } + } #endif /* SHA256_FAST_COPY */ - if (needBurn) - burnStack (sizeof (uint32_t[74]) + sizeof (uint32_t *[6]) + sizeof (int)); + if (needBurn) + burnStack (sizeof (uint32_t[74]) + sizeof (uint32_t *[6]) + sizeof (int)); } void SHA256Final (SHA256Context *sc, uint8_t hash[SHA256_HASH_SIZE]) { - uint32_t bytesToPad; - uint64_t lengthPad; - int i; + uint32_t bytesToPad; + uint64_t lengthPad; + int i; - bytesToPad = 120L - sc->bufferLength; - if (bytesToPad > 64L) - bytesToPad -= 64L; + bytesToPad = 120L - sc->bufferLength; + if (bytesToPad > 64L) + bytesToPad -= 64L; - lengthPad = BYTESWAP64(sc->totalLength); + lengthPad = BYTESWAP64(sc->totalLength); - SHA256Update (sc, padding, bytesToPad); - SHA256Update (sc, &lengthPad, 8L); + SHA256Update (sc, padding, bytesToPad); + SHA256Update (sc, &lengthPad, 8L); - if (hash) { - for (i = 0; i < SHA256_HASH_WORDS; i++) { + if (hash) { + for (i = 0; i < SHA256_HASH_WORDS; i++) { #ifdef SHA256_FAST_COPY - *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); + *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); #else /* SHA256_FAST_COPY */ - hash[0] = (uint8_t) (sc->hash[i] >> 24); - hash[1] = (uint8_t) (sc->hash[i] >> 16); - hash[2] = (uint8_t) (sc->hash[i] >> 8); - hash[3] = (uint8_t) sc->hash[i]; + hash[0] = (uint8_t) (sc->hash[i] >> 24); + hash[1] = (uint8_t) (sc->hash[i] >> 16); + hash[2] = (uint8_t) (sc->hash[i] >> 8); + hash[3] = (uint8_t) sc->hash[i]; #endif /* SHA256_FAST_COPY */ - hash += 4; - } + hash += 4; } + } } #ifdef SHA256_TEST @@ -527,49 +433,49 @@ SHA256Final (SHA256Context *sc, uint8_t hash[SHA256_HASH_SIZE]) int main (int argc, char *argv[]) { - SHA256Context foo; - uint8_t hash[SHA256_HASH_SIZE]; - char buf[1000]; - int i; + SHA256Context foo; + uint8_t hash[SHA256_HASH_SIZE]; + char buf[1000]; + int i; - SHA256Init (&foo); - SHA256Update (&foo, "abc", 3); - SHA256Final (&foo, hash); + SHA256Init (&foo); + SHA256Update (&foo, "abc", 3); + SHA256Final (&foo, hash); - for (i = 0; i < SHA256_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA256_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - SHA256Init (&foo); - SHA256Update (&foo, - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - 56); - SHA256Final (&foo, hash); + SHA256Init (&foo); + SHA256Update (&foo, + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + 56); + SHA256Final (&foo, hash); - for (i = 0; i < SHA256_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA256_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - SHA256Init (&foo); - memset (buf, 'a', sizeof (buf)); - for (i = 0; i < 1000; i++) - SHA256Update (&foo, buf, sizeof (buf)); - SHA256Final (&foo, hash); + SHA256Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA256Update (&foo, buf, sizeof (buf)); + SHA256Final (&foo, hash); - for (i = 0; i < SHA256_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA256_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - exit (0); + exit (0); } #endif /* SHA256_TEST */ diff --git a/sha256.h b/sha256.h index 6c68500..76bc01c 100644 --- a/sha256.h +++ b/sha256.h @@ -43,15 +43,15 @@ #define SHA256_HASH_WORDS 8 struct _SHA256Context { - uint64_t totalLength; - uint32_t hash[SHA256_HASH_WORDS]; - uint32_t bufferLength; - union { - uint32_t words[16]; - uint8_t bytes[64]; - } buffer; + uint64_t totalLength; + uint32_t hash[SHA256_HASH_WORDS]; + uint32_t bufferLength; + union { + uint32_t words[16]; + uint8_t bytes[64]; + } buffer; #ifdef RUNTIME_ENDIAN - int littleEndian; + int littleEndian; #endif /* RUNTIME_ENDIAN */ }; diff --git a/sha384.c b/sha384.c index 9220b6a..bc5c83f 100644 --- a/sha384.c +++ b/sha384.c @@ -58,7 +58,7 @@ #ifndef lint static const char rcsid[] = - "$Id: sha384.c 680 2003-07-25 21:57:57Z asaddi $"; + "$Id: sha384.c 680 2003-07-25 21:57:57Z asaddi $"; #endif /* !lint */ #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) @@ -87,46 +87,46 @@ static const char rcsid[] = } static const uint64_t K[80] = { - 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, - 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, - 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, - 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, - 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, - 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, - 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, - 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, - 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, - 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, - 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, - 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, - 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, - 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, - 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, - 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, - 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, - 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, - 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, - 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, - 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, - 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, - 0xd192e819d6ef5218LL, 0xd69906245565a910LL, - 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, - 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, - 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, - 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, - 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, - 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, - 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, - 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, - 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, - 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, - 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, - 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, - 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, - 0x28db77f523047d84LL, 0x32caab7b40c72493LL, - 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, - 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, - 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL + 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, + 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, + 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, + 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, + 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, + 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, + 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, + 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, + 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, + 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, + 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, + 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, + 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, + 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, + 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, + 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, + 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, + 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, + 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, + 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, + 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, + 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, + 0xd192e819d6ef5218LL, 0xd69906245565a910LL, + 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, + 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, + 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, + 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, + 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, + 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, + 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, + 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, + 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, + 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, + 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, + 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, + 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, + 0x28db77f523047d84LL, 0x32caab7b40c72493LL, + 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, + 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, + 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL }; #ifndef RUNTIME_ENDIAN @@ -144,9 +144,9 @@ static const uint64_t K[80] = { static inline uint64_t _byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); } #endif /* WORDS_BIGENDIAN */ @@ -162,266 +162,266 @@ static inline uint64_t _byteswap64(uint64_t x) static inline uint64_t __byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); } static inline uint32_t _byteswap(int littleEndian, uint32_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP(x); + if (!littleEndian) + return x; + else + return _BYTESWAP(x); } static inline uint64_t _byteswap64(int littleEndian, uint64_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP64(x); + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); } static inline void setEndian(int *littleEndianp) { - union { - uint32_t w; - uint8_t b[4]; - } endian; + union { + uint32_t w; + uint8_t b[4]; + } endian; - endian.w = 1L; - *littleEndianp = endian.b[0] != 0; + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; } #endif /* !RUNTIME_ENDIAN */ static const uint8_t padding[128] = { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void SHA384Init (SHA384Context *sc) { #ifdef RUNTIME_ENDIAN - setEndian (&sc->littleEndian); + setEndian (&sc->littleEndian); #endif /* RUNTIME_ENDIAN */ - sc->totalLength[0] = 0LL; - sc->totalLength[1] = 0LL; - sc->hash[0] = 0xcbbb9d5dc1059ed8LL; - sc->hash[1] = 0x629a292a367cd507LL; - sc->hash[2] = 0x9159015a3070dd17LL; - sc->hash[3] = 0x152fecd8f70e5939LL; - sc->hash[4] = 0x67332667ffc00b31LL; - sc->hash[5] = 0x8eb44a8768581511LL; - sc->hash[6] = 0xdb0c2e0d64f98fa7LL; - sc->hash[7] = 0x47b5481dbefa4fa4LL; - sc->bufferLength = 0L; + sc->totalLength[0] = 0LL; + sc->totalLength[1] = 0LL; + sc->hash[0] = 0xcbbb9d5dc1059ed8LL; + sc->hash[1] = 0x629a292a367cd507LL; + sc->hash[2] = 0x9159015a3070dd17LL; + sc->hash[3] = 0x152fecd8f70e5939LL; + sc->hash[4] = 0x67332667ffc00b31LL; + sc->hash[5] = 0x8eb44a8768581511LL; + sc->hash[6] = 0xdb0c2e0d64f98fa7LL; + sc->hash[7] = 0x47b5481dbefa4fa4LL; + sc->bufferLength = 0L; } static void burnStack (int size) { - char buf[128]; + char buf[128]; - memset (buf, 0, sizeof (buf)); - size -= sizeof (buf); - if (size > 0) - burnStack (size); + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); } static void SHA384Guts (SHA384Context *sc, const uint64_t *cbuf) { - uint64_t buf[80]; - uint64_t *W, *W2, *W7, *W15, *W16; - uint64_t a, b, c, d, e, f, g, h; - uint64_t t1, t2; - const uint64_t *Kp; - int i; + uint64_t buf[80]; + uint64_t *W, *W2, *W7, *W15, *W16; + uint64_t a, b, c, d, e, f, g, h; + uint64_t t1, t2; + const uint64_t *Kp; + int i; - W = buf; + W = buf; - for (i = 15; i >= 0; i--) { - *(W++) = BYTESWAP64(*cbuf); - cbuf++; - } + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP64(*cbuf); + cbuf++; + } - W16 = &buf[0]; - W15 = &buf[1]; - W7 = &buf[9]; - W2 = &buf[14]; + W16 = &buf[0]; + W15 = &buf[1]; + W7 = &buf[9]; + W2 = &buf[14]; - for (i = 63; i >= 0; i--) { - *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); - W2++; - W15++; - } + for (i = 63; i >= 0; i--) { + *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); + W2++; + W15++; + } - a = sc->hash[0]; - b = sc->hash[1]; - c = sc->hash[2]; - d = sc->hash[3]; - e = sc->hash[4]; - f = sc->hash[5]; - g = sc->hash[6]; - h = sc->hash[7]; + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; + f = sc->hash[5]; + g = sc->hash[6]; + h = sc->hash[7]; - Kp = K; - W = buf; + Kp = K; + W = buf; - for (i = 79; i >= 0; i--) - DO_ROUND(); + for (i = 79; i >= 0; i--) + DO_ROUND(); - sc->hash[0] += a; - sc->hash[1] += b; - sc->hash[2] += c; - sc->hash[3] += d; - sc->hash[4] += e; - sc->hash[5] += f; - sc->hash[6] += g; - sc->hash[7] += h; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; + sc->hash[5] += f; + sc->hash[6] += g; + sc->hash[7] += h; } void SHA384Update (SHA384Context *sc, const void *vdata, uint32_t len) { - const uint8_t *data = vdata; - uint32_t bufferBytesLeft; - uint32_t bytesToCopy; - uint64_t carryCheck; - int needBurn = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + uint64_t carryCheck; + int needBurn = 0; #ifdef SHA384_FAST_COPY - if (sc->bufferLength) { - bufferBytesLeft = 128L - sc->bufferLength; + if (sc->bufferLength) { + bufferBytesLeft = 128L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += bytesToCopy * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 128L) { - SHA384Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; - } + if (sc->bufferLength == 128L) { + SHA384Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; } + } - while (len > 127) { - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += 1024L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + while (len > 127) { + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += 1024L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - SHA384Guts (sc, data); - needBurn = 1; + SHA384Guts (sc, data); + needBurn = 1; - data += 128L; - len -= 128L; - } + data += 128L; + len -= 128L; + } - if (len) { - memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += len * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += len * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += len; - } + sc->bufferLength += len; + } #else /* SHA384_FAST_COPY */ - while (len) { - bufferBytesLeft = 128L - sc->bufferLength; + while (len) { + bufferBytesLeft = 128L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += bytesToCopy * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 128L) { - SHA384Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; - } + if (sc->bufferLength == 128L) { + SHA384Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; } + } #endif /* SHA384_FAST_COPY */ - if (needBurn) - burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); + if (needBurn) + burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); } void SHA384Final (SHA384Context *sc, uint8_t hash[SHA384_HASH_SIZE]) { - uint32_t bytesToPad; - uint64_t lengthPad[2]; - int i; + uint32_t bytesToPad; + uint64_t lengthPad[2]; + int i; - bytesToPad = 240L - sc->bufferLength; - if (bytesToPad > 128L) - bytesToPad -= 128L; + bytesToPad = 240L - sc->bufferLength; + if (bytesToPad > 128L) + bytesToPad -= 128L; - lengthPad[0] = BYTESWAP64(sc->totalLength[0]); - lengthPad[1] = BYTESWAP64(sc->totalLength[1]); + lengthPad[0] = BYTESWAP64(sc->totalLength[0]); + lengthPad[1] = BYTESWAP64(sc->totalLength[1]); - SHA384Update (sc, padding, bytesToPad); - SHA384Update (sc, lengthPad, 16L); + SHA384Update (sc, padding, bytesToPad); + SHA384Update (sc, lengthPad, 16L); - if (hash) { - for (i = 0; i < SHA384_HASH_WORDS; i++) { + if (hash) { + for (i = 0; i < SHA384_HASH_WORDS; i++) { #ifdef SHA384_FAST_COPY - *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); + *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); #else /* SHA384_FAST_COPY */ - hash[0] = (uint8_t) (sc->hash[i] >> 56); - hash[1] = (uint8_t) (sc->hash[i] >> 48); - hash[2] = (uint8_t) (sc->hash[i] >> 40); - hash[3] = (uint8_t) (sc->hash[i] >> 32); - hash[4] = (uint8_t) (sc->hash[i] >> 24); - hash[5] = (uint8_t) (sc->hash[i] >> 16); - hash[6] = (uint8_t) (sc->hash[i] >> 8); - hash[7] = (uint8_t) sc->hash[i]; + hash[0] = (uint8_t) (sc->hash[i] >> 56); + hash[1] = (uint8_t) (sc->hash[i] >> 48); + hash[2] = (uint8_t) (sc->hash[i] >> 40); + hash[3] = (uint8_t) (sc->hash[i] >> 32); + hash[4] = (uint8_t) (sc->hash[i] >> 24); + hash[5] = (uint8_t) (sc->hash[i] >> 16); + hash[6] = (uint8_t) (sc->hash[i] >> 8); + hash[7] = (uint8_t) sc->hash[i]; #endif /* SHA384_FAST_COPY */ - hash += 8; - } + hash += 8; } + } } #ifdef SHA384_TEST @@ -433,56 +433,56 @@ SHA384Final (SHA384Context *sc, uint8_t hash[SHA384_HASH_SIZE]) int main (int argc, char *argv[]) { - SHA384Context foo; - uint8_t hash[SHA384_HASH_SIZE]; - char buf[1000]; - int i; + SHA384Context foo; + uint8_t hash[SHA384_HASH_SIZE]; + char buf[1000]; + int i; - SHA384Init (&foo); - SHA384Update (&foo, "abc", 3); - SHA384Final (&foo, hash); + SHA384Init (&foo); + SHA384Update (&foo, "abc", 3); + SHA384Final (&foo, hash); - for (i = 0; i < SHA384_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } - printf ("\n"); + for (i = 0; i < SHA384_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + printf ("\n"); - SHA384Init (&foo); - SHA384Update (&foo, - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" - "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - 112); - SHA384Final (&foo, hash); + SHA384Init (&foo); + SHA384Update (&foo, + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + 112); + SHA384Final (&foo, hash); - for (i = 0; i < SHA384_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } - printf ("\n"); + for (i = 0; i < SHA384_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + printf ("\n"); - SHA384Init (&foo); - memset (buf, 'a', sizeof (buf)); - for (i = 0; i < 1000; i++) - SHA384Update (&foo, buf, sizeof (buf)); - SHA384Final (&foo, hash); + SHA384Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA384Update (&foo, buf, sizeof (buf)); + SHA384Final (&foo, hash); - for (i = 0; i < SHA384_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } - printf ("\n"); + for (i = 0; i < SHA384_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + printf ("\n"); - exit (0); + exit (0); } #endif /* SHA384_TEST */ diff --git a/sha384.h b/sha384.h index 279fba5..443cd6e 100644 --- a/sha384.h +++ b/sha384.h @@ -43,15 +43,15 @@ #define SHA384_HASH_WORDS 6 struct _SHA384Context { - uint64_t totalLength[2]; - uint64_t hash[SHA384_HASH_WORDS + 2]; - uint32_t bufferLength; - union { - uint64_t words[16]; - uint8_t bytes[128]; - } buffer; + uint64_t totalLength[2]; + uint64_t hash[SHA384_HASH_WORDS + 2]; + uint32_t bufferLength; + union { + uint64_t words[16]; + uint8_t bytes[128]; + } buffer; #ifdef RUNTIME_ENDIAN - int littleEndian; + int littleEndian; #endif /* RUNTIME_ENDIAN */ }; diff --git a/sha512.c b/sha512.c index dfc7427..618389a 100644 --- a/sha512.c +++ b/sha512.c @@ -58,7 +58,7 @@ #ifndef lint static const char rcsid[] = - "$Id: sha512.c 680 2003-07-25 21:58:07Z asaddi $"; + "$Id: sha512.c 680 2003-07-25 21:58:07Z asaddi $"; #endif /* !lint */ #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) @@ -87,46 +87,46 @@ static const char rcsid[] = } static const uint64_t K[80] = { - 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, - 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, - 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, - 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, - 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, - 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, - 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, - 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, - 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, - 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, - 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, - 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, - 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, - 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, - 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, - 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, - 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, - 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, - 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, - 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, - 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, - 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, - 0xd192e819d6ef5218LL, 0xd69906245565a910LL, - 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, - 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, - 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, - 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, - 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, - 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, - 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, - 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, - 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, - 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, - 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, - 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, - 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, - 0x28db77f523047d84LL, 0x32caab7b40c72493LL, - 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, - 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, - 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL + 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, + 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, + 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, + 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, + 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, + 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, + 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, + 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, + 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, + 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, + 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, + 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, + 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, + 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, + 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, + 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, + 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, + 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, + 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, + 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, + 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, + 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, + 0xd192e819d6ef5218LL, 0xd69906245565a910LL, + 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, + 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, + 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, + 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, + 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, + 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, + 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, + 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, + 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, + 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, + 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, + 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, + 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, + 0x28db77f523047d84LL, 0x32caab7b40c72493LL, + 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, + 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, + 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL }; #ifndef RUNTIME_ENDIAN @@ -144,9 +144,9 @@ static const uint64_t K[80] = { static inline uint64_t _byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); } #endif /* WORDS_BIGENDIAN */ @@ -162,266 +162,266 @@ static inline uint64_t _byteswap64(uint64_t x) static inline uint64_t __byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); } static inline uint32_t _byteswap(int littleEndian, uint32_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP(x); + if (!littleEndian) + return x; + else + return _BYTESWAP(x); } static inline uint64_t _byteswap64(int littleEndian, uint64_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP64(x); + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); } static inline void setEndian(int *littleEndianp) { - union { - uint32_t w; - uint8_t b[4]; - } endian; + union { + uint32_t w; + uint8_t b[4]; + } endian; - endian.w = 1L; - *littleEndianp = endian.b[0] != 0; + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; } #endif /* !RUNTIME_ENDIAN */ static const uint8_t padding[128] = { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void SHA512Init (SHA512Context *sc) { #ifdef RUNTIME_ENDIAN - setEndian (&sc->littleEndian); + setEndian (&sc->littleEndian); #endif /* RUNTIME_ENDIAN */ - sc->totalLength[0] = 0LL; - sc->totalLength[1] = 0LL; - sc->hash[0] = 0x6a09e667f3bcc908LL; - sc->hash[1] = 0xbb67ae8584caa73bLL; - sc->hash[2] = 0x3c6ef372fe94f82bLL; - sc->hash[3] = 0xa54ff53a5f1d36f1LL; - sc->hash[4] = 0x510e527fade682d1LL; - sc->hash[5] = 0x9b05688c2b3e6c1fLL; - sc->hash[6] = 0x1f83d9abfb41bd6bLL; - sc->hash[7] = 0x5be0cd19137e2179LL; - sc->bufferLength = 0L; + sc->totalLength[0] = 0LL; + sc->totalLength[1] = 0LL; + sc->hash[0] = 0x6a09e667f3bcc908LL; + sc->hash[1] = 0xbb67ae8584caa73bLL; + sc->hash[2] = 0x3c6ef372fe94f82bLL; + sc->hash[3] = 0xa54ff53a5f1d36f1LL; + sc->hash[4] = 0x510e527fade682d1LL; + sc->hash[5] = 0x9b05688c2b3e6c1fLL; + sc->hash[6] = 0x1f83d9abfb41bd6bLL; + sc->hash[7] = 0x5be0cd19137e2179LL; + sc->bufferLength = 0L; } static void burnStack (int size) { - char buf[128]; + char buf[128]; - memset (buf, 0, sizeof (buf)); - size -= sizeof (buf); - if (size > 0) - burnStack (size); + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); } static void SHA512Guts (SHA512Context *sc, const uint64_t *cbuf) { - uint64_t buf[80]; - uint64_t *W, *W2, *W7, *W15, *W16; - uint64_t a, b, c, d, e, f, g, h; - uint64_t t1, t2; - const uint64_t *Kp; - int i; + uint64_t buf[80]; + uint64_t *W, *W2, *W7, *W15, *W16; + uint64_t a, b, c, d, e, f, g, h; + uint64_t t1, t2; + const uint64_t *Kp; + int i; - W = buf; + W = buf; - for (i = 15; i >= 0; i--) { - *(W++) = BYTESWAP64(*cbuf); - cbuf++; - } + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP64(*cbuf); + cbuf++; + } - W16 = &buf[0]; - W15 = &buf[1]; - W7 = &buf[9]; - W2 = &buf[14]; + W16 = &buf[0]; + W15 = &buf[1]; + W7 = &buf[9]; + W2 = &buf[14]; - for (i = 63; i >= 0; i--) { - *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); - W2++; - W15++; - } + for (i = 63; i >= 0; i--) { + *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); + W2++; + W15++; + } - a = sc->hash[0]; - b = sc->hash[1]; - c = sc->hash[2]; - d = sc->hash[3]; - e = sc->hash[4]; - f = sc->hash[5]; - g = sc->hash[6]; - h = sc->hash[7]; + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; + f = sc->hash[5]; + g = sc->hash[6]; + h = sc->hash[7]; - Kp = K; - W = buf; + Kp = K; + W = buf; - for (i = 79; i >= 0; i--) - DO_ROUND(); + for (i = 79; i >= 0; i--) + DO_ROUND(); - sc->hash[0] += a; - sc->hash[1] += b; - sc->hash[2] += c; - sc->hash[3] += d; - sc->hash[4] += e; - sc->hash[5] += f; - sc->hash[6] += g; - sc->hash[7] += h; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; + sc->hash[5] += f; + sc->hash[6] += g; + sc->hash[7] += h; } void SHA512Update (SHA512Context *sc, const void *vdata, uint32_t len) { - const uint8_t *data = vdata; - uint32_t bufferBytesLeft; - uint32_t bytesToCopy; - uint64_t carryCheck; - int needBurn = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + uint64_t carryCheck; + int needBurn = 0; #ifdef SHA512_FAST_COPY - if (sc->bufferLength) { - bufferBytesLeft = 128L - sc->bufferLength; + if (sc->bufferLength) { + bufferBytesLeft = 128L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += bytesToCopy * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 128L) { - SHA512Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; - } + if (sc->bufferLength == 128L) { + SHA512Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; } + } - while (len > 127) { - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += 1024L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + while (len > 127) { + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += 1024L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - SHA512Guts (sc, data); - needBurn = 1; + SHA512Guts (sc, data); + needBurn = 1; - data += 128L; - len -= 128L; - } + data += 128L; + len -= 128L; + } - if (len) { - memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += len * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += len * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += len; - } + sc->bufferLength += len; + } #else /* SHA512_FAST_COPY */ - while (len) { - bufferBytesLeft = 128L - sc->bufferLength; + while (len) { + bufferBytesLeft = 128L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += bytesToCopy * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 128L) { - SHA512Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; - } + if (sc->bufferLength == 128L) { + SHA512Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; } + } #endif /* SHA512_FAST_COPY */ - if (needBurn) - burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); + if (needBurn) + burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); } void SHA512Final (SHA512Context *sc, uint8_t hash[SHA512_HASH_SIZE]) { - uint32_t bytesToPad; - uint64_t lengthPad[2]; - int i; + uint32_t bytesToPad; + uint64_t lengthPad[2]; + int i; - bytesToPad = 240L - sc->bufferLength; - if (bytesToPad > 128L) - bytesToPad -= 128L; + bytesToPad = 240L - sc->bufferLength; + if (bytesToPad > 128L) + bytesToPad -= 128L; - lengthPad[0] = BYTESWAP64(sc->totalLength[0]); - lengthPad[1] = BYTESWAP64(sc->totalLength[1]); + lengthPad[0] = BYTESWAP64(sc->totalLength[0]); + lengthPad[1] = BYTESWAP64(sc->totalLength[1]); - SHA512Update (sc, padding, bytesToPad); - SHA512Update (sc, lengthPad, 16L); + SHA512Update (sc, padding, bytesToPad); + SHA512Update (sc, lengthPad, 16L); - if (hash) { - for (i = 0; i < SHA512_HASH_WORDS; i++) { + if (hash) { + for (i = 0; i < SHA512_HASH_WORDS; i++) { #ifdef SHA384_FAST_COPY - *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); + *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); #else /* SHA384_FAST_COPY */ - hash[0] = (uint8_t) (sc->hash[i] >> 56); - hash[1] = (uint8_t) (sc->hash[i] >> 48); - hash[2] = (uint8_t) (sc->hash[i] >> 40); - hash[3] = (uint8_t) (sc->hash[i] >> 32); - hash[4] = (uint8_t) (sc->hash[i] >> 24); - hash[5] = (uint8_t) (sc->hash[i] >> 16); - hash[6] = (uint8_t) (sc->hash[i] >> 8); - hash[7] = (uint8_t) sc->hash[i]; + hash[0] = (uint8_t) (sc->hash[i] >> 56); + hash[1] = (uint8_t) (sc->hash[i] >> 48); + hash[2] = (uint8_t) (sc->hash[i] >> 40); + hash[3] = (uint8_t) (sc->hash[i] >> 32); + hash[4] = (uint8_t) (sc->hash[i] >> 24); + hash[5] = (uint8_t) (sc->hash[i] >> 16); + hash[6] = (uint8_t) (sc->hash[i] >> 8); + hash[7] = (uint8_t) sc->hash[i]; #endif /* SHA384_FAST_COPY */ - hash += 8; - } + hash += 8; } + } } #ifdef SHA512_TEST @@ -433,53 +433,53 @@ SHA512Final (SHA512Context *sc, uint8_t hash[SHA512_HASH_SIZE]) int main (int argc, char *argv[]) { - SHA512Context foo; - uint8_t hash[SHA512_HASH_SIZE]; - char buf[1000]; - int i; + SHA512Context foo; + uint8_t hash[SHA512_HASH_SIZE]; + char buf[1000]; + int i; - SHA512Init (&foo); - SHA512Update (&foo, "abc", 3); - SHA512Final (&foo, hash); + SHA512Init (&foo); + SHA512Update (&foo, "abc", 3); + SHA512Final (&foo, hash); - for (i = 0; i < SHA512_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } - SHA512Init (&foo); - SHA512Update (&foo, - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" - "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - 112); - SHA512Final (&foo, hash); + SHA512Init (&foo); + SHA512Update (&foo, + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + 112); + SHA512Final (&foo, hash); - for (i = 0; i < SHA512_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } - SHA512Init (&foo); - memset (buf, 'a', sizeof (buf)); - for (i = 0; i < 1000; i++) - SHA512Update (&foo, buf, sizeof (buf)); - SHA512Final (&foo, hash); + SHA512Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA512Update (&foo, buf, sizeof (buf)); + SHA512Final (&foo, hash); - for (i = 0; i < SHA512_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } - exit (0); + exit (0); } #endif /* SHA512_TEST */ diff --git a/sha512.h b/sha512.h index 8980d7f..f590a2e 100644 --- a/sha512.h +++ b/sha512.h @@ -43,15 +43,15 @@ #define SHA512_HASH_WORDS 8 struct _SHA512Context { - uint64_t totalLength[2]; - uint64_t hash[SHA512_HASH_WORDS]; - uint32_t bufferLength; - union { - uint64_t words[16]; - uint8_t bytes[128]; - } buffer; + uint64_t totalLength[2]; + uint64_t hash[SHA512_HASH_WORDS]; + uint32_t bufferLength; + union { + uint64_t words[16]; + uint8_t bytes[128]; + } buffer; #ifdef RUNTIME_ENDIAN - int littleEndian; + int littleEndian; #endif /* RUNTIME_ENDIAN */ };