/*
* FILE: sha2.c
* AUTHOR: Aaron D. Gifford <me@aarongifford.com>
- *
+ *
* Copyright (c) 2000-2001, Aaron D. Gifford
* All rights reserved.
*
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
/* #include <assert.h> */ /* assert() */
#include <stdio.h>
-#include <sysexits.h>
#include <sys/uio.h>
#include <unistd.h>
#include <inttypes.h>
-#include <endian.h>
#include "sha2.h"
*
* And for little-endian machines, add:
*
- * #define BYTE_ORDER LITTLE_ENDIAN
+ * #define BYTE_ORDER LITTLE_ENDIAN
*
* Or for big-endian machines:
*
* <machine/endian.h> where the appropriate definitions are actually
* made).
*/
-#if !defined(__BYTE_ORDER) || (__BYTE_ORDER != __LITTLE_ENDIAN && __BYTE_ORDER != __BIG_ENDIAN)
-#error Define __BYTE_ORDER to be equal to either __LITTLE_ENDIAN or __BIG_ENDIAN
-#endif
/*
* Define the following sha2_* types to types of the correct length on
/*** ENDIAN REVERSAL MACROS *******************************************/
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
#define REVERSE32(w,x) { \
sha2_word32 tmp = (w); \
tmp = (tmp >> 16) | (tmp << 16); \
(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
((tmp & 0x0000ffff0000ffffULL) << 16); \
}
-#endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#endif /* !WORDS_BIGENDIAN */
/*
* Macro for incrementally adding the unsigned 64-bit integer n to the
* library -- they are intended for private internal visibility/use
* only.
*/
+static void SHA256_Last(SHA256_CTX*);
static void SHA512_Last(SHA512_CTX*);
static void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
static void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
0x5be0cd19137e2179ULL
};
-/*
- * Constant used by SHA256/384/512_End() functions for converting the
- * digest to a readable hexadecimal character string:
- */
-static const char *sha2_hex_digits = "0123456789abcdef";
+/* Initial hash value H for SHA-224: */
+const static sha2_word32 sha224_initial_hash_value[8] = {
+ 0xc1059ed8UL,
+ 0x367cd507UL,
+ 0x3070dd17UL,
+ 0xf70e5939UL,
+ 0xffc00b31UL,
+ 0x68581511UL,
+ 0x64f98fa7UL,
+ 0xbefa4fa4UL
+};
/*** SHA-256: *********************************************************/
-void sat_SHA256_Init(SHA256_CTX* context) {
+void solv_SHA256_Init(SHA256_CTX* context) {
if (context == (SHA256_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
- MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
+ MEMSET_BZERO((char *)context->buffer, SHA256_BLOCK_LENGTH);
context->bitcount = 0;
}
/* Unrolled SHA-256 round macros: */
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
REVERSE32(*data++, W256[j]); \
j++
-#else /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#else /* !WORDS_BIGENDIAN */
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
j++
-#endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#endif /* !WORDS_BIGENDIAN */
#define ROUND256(a,b,c,d,e,f,g,h) \
s0 = W256[(j+1)&0x0f]; \
sha2_word32 T1, *W256;
int j;
- W256 = (sha2_word32*)context->buffer;
+ W256 = context->buffer;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
sha2_word32 T1, T2, *W256;
int j;
- W256 = (sha2_word32*)context->buffer;
+ W256 = context->buffer;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
j = 0;
do {
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
/* Copy data while converting to host byte order */
REVERSE32(*data++,W256[j]);
/* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
-#else /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#else /* !WORDS_BIGENDIAN */
/* Apply the SHA-256 compression function to update a..h with copy */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
-#endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#endif /* !WORDS_BIGENDIAN */
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
g = f;
s1 = sigma1_256(s1);
/* Apply the SHA-256 compression function to update a..h */
- T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
+ T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
(W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
#endif /* SHA2_UNROLL_TRANSFORM */
-void sat_SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
+void solv_SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
unsigned int freespace, usedspace;
if (len == 0) {
if (len >= freespace) {
/* Fill the buffer completely and process it */
- MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
+ MEMCPY_BCOPY(&((char *)context->buffer)[usedspace], data, freespace);
context->bitcount += freespace << 3;
len -= freespace;
data += freespace;
- SHA256_Transform(context, (sha2_word32*)context->buffer);
+ SHA256_Transform(context, context->buffer);
} else {
/* The buffer is not yet full */
- MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
+ MEMCPY_BCOPY(&((char *)context->buffer)[usedspace], data, len);
context->bitcount += len << 3;
/* Clean up: */
usedspace = freespace = 0;
}
if (len > 0) {
/* There's left-overs, so save 'em */
- MEMCPY_BCOPY(context->buffer, data, len);
+ MEMCPY_BCOPY((char *)context->buffer, data, len);
context->bitcount += len << 3;
}
/* Clean up: */
usedspace = freespace = 0;
}
-void sat_SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
- sha2_word32 *d = (sha2_word32*)digest;
+static void SHA256_Last(SHA256_CTX* context) {
unsigned int usedspace;
- /* Sanity check: */
- /* assert(context != (SHA256_CTX*)0); */
-
- /* If no digest buffer is passed, we don't bother doing this: */
- if (digest != (sha2_byte*)0) {
- usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
- /* Convert FROM host byte order */
- REVERSE64(context->bitcount,context->bitcount);
+ usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
+#ifndef WORDS_BIGENDIAN
+ /* Convert FROM host byte order */
+ REVERSE64(context->bitcount,context->bitcount);
#endif
- if (usedspace > 0) {
- /* Begin padding with a 1 bit: */
- context->buffer[usedspace++] = 0x80;
-
- if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
- /* Set-up for the last transform: */
- MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
- } else {
- if (usedspace < SHA256_BLOCK_LENGTH) {
- MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
- }
- /* Do second-to-last transform: */
- SHA256_Transform(context, (sha2_word32*)context->buffer);
-
- /* And set-up for the last transform: */
- MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
- }
- } else {
+ if (usedspace > 0) {
+ /* Begin padding with a 1 bit: */
+ ((char *)context->buffer)[usedspace++] = 0x80;
+
+ if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
/* Set-up for the last transform: */
- MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
+ MEMSET_BZERO(&((char *)context->buffer)[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
+ } else {
+ if (usedspace < SHA256_BLOCK_LENGTH) {
+ MEMSET_BZERO(&((char *)context->buffer)[usedspace], SHA256_BLOCK_LENGTH - usedspace);
+ }
+ /* Do second-to-last transform: */
+ SHA256_Transform(context, context->buffer);
- /* Begin padding with a 1 bit: */
- *context->buffer = 0x80;
+ /* And set-up for the last transform: */
+ MEMSET_BZERO((char *)context->buffer, SHA256_SHORT_BLOCK_LENGTH);
}
- /* Set the bit count: */
- *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
+ } else {
+ /* Set-up for the last transform: */
+ MEMSET_BZERO((char *)context->buffer, SHA256_SHORT_BLOCK_LENGTH);
+
+ /* Begin padding with a 1 bit: */
+ *((char *)context->buffer) = 0x80;
+ }
+ /* Set the bit count: */
+ MEMCPY_BCOPY(&((char *)context->buffer)[SHA256_SHORT_BLOCK_LENGTH], (char *)(&context->bitcount), 8);
+
+ /* Final transform: */
+ SHA256_Transform(context, context->buffer);
+}
+
+void solv_SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
+ sha2_word32 *d = (sha2_word32*)digest;
+
+ /* Sanity check: */
+ /* assert(context != (SHA256_CTX*)0); */
- /* Final transform: */
- SHA256_Transform(context, (sha2_word32*)context->buffer);
+ /* If no digest buffer is passed, we don't bother doing this: */
+ if (digest != (sha2_byte*)0) {
+ SHA256_Last(context);
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
{
/* Convert TO host byte order */
int j;
}
/* Clean up state data: */
- MEMSET_BZERO(context, sizeof(context));
- usedspace = 0;
-}
-
-char *sat_SHA256_End(SHA256_CTX* context, char buffer[]) {
- sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
- int i;
-
- /* Sanity check: */
- /* assert(context != (SHA256_CTX*)0); */
-
- if (buffer != (char*)0) {
- sat_SHA256_Final(digest, context);
-
- for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
- *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
- *buffer++ = sha2_hex_digits[*d & 0x0f];
- d++;
- }
- *buffer = (char)0;
- } else {
- MEMSET_BZERO(context, sizeof(context));
- }
- MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
- return buffer;
-}
-
-char* sat_SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
- SHA256_CTX context;
-
- sat_SHA256_Init(&context);
- sat_SHA256_Update(&context, data, len);
- return sat_SHA256_End(&context, digest);
+ MEMSET_BZERO(context, sizeof(*context));
}
/*** SHA-512: *********************************************************/
-void sat_SHA512_Init(SHA512_CTX* context) {
+void solv_SHA512_Init(SHA512_CTX* context) {
if (context == (SHA512_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
- MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
+ MEMSET_BZERO((char *)context->buffer, SHA512_BLOCK_LENGTH);
context->bitcount[0] = context->bitcount[1] = 0;
}
#ifdef SHA2_UNROLL_TRANSFORM
/* Unrolled SHA-512 round macros: */
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
REVERSE64(*data++, W512[j]); \
j++
-#else /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#else /* !WORDS_BIGENDIAN */
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
j++
-#endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#endif /* !WORDS_BIGENDIAN */
#define ROUND512(a,b,c,d,e,f,g,h) \
s0 = W512[(j+1)&0x0f]; \
static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
- sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
+ sha2_word64 T1, *W512 = context->buffer;
int j;
/* Initialize registers with the prev. intermediate value */
static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
- sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
+ sha2_word64 T1, T2, *W512 = context->buffer;
int j;
/* Initialize registers with the prev. intermediate value */
j = 0;
do {
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
/* Convert TO host byte order */
REVERSE64(*data++, W512[j]);
/* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
-#else /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#else /* !WORDS_BIGENDIAN */
/* Apply the SHA-512 compression function to update a..h with copy */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
-#endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
+#endif /* !WORDS_BIGENDIAN */
T2 = Sigma0_512(a) + Maj(a, b, c);
h = g;
g = f;
#endif /* SHA2_UNROLL_TRANSFORM */
-void sat_SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
+void solv_SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
unsigned int freespace, usedspace;
if (len == 0) {
if (len >= freespace) {
/* Fill the buffer completely and process it */
- MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
+ MEMCPY_BCOPY(&((char *)context->buffer)[usedspace], data, freespace);
ADDINC128(context->bitcount, freespace << 3);
len -= freespace;
data += freespace;
- SHA512_Transform(context, (sha2_word64*)context->buffer);
+ SHA512_Transform(context, context->buffer);
} else {
/* The buffer is not yet full */
- MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
+ MEMCPY_BCOPY(&((char *)context->buffer)[usedspace], data, len);
ADDINC128(context->bitcount, len << 3);
/* Clean up: */
usedspace = freespace = 0;
}
if (len > 0) {
/* There's left-overs, so save 'em */
- MEMCPY_BCOPY(context->buffer, data, len);
+ MEMCPY_BCOPY((char *)context->buffer, data, len);
ADDINC128(context->bitcount, len << 3);
}
/* Clean up: */
unsigned int usedspace;
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
/* Convert FROM host byte order */
REVERSE64(context->bitcount[0],context->bitcount[0]);
REVERSE64(context->bitcount[1],context->bitcount[1]);
#endif
if (usedspace > 0) {
/* Begin padding with a 1 bit: */
- context->buffer[usedspace++] = 0x80;
+ ((char *)context->buffer)[usedspace++] = 0x80;
if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
/* Set-up for the last transform: */
- MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
+ MEMSET_BZERO(&((char *)context->buffer)[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA512_BLOCK_LENGTH) {
- MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
+ MEMSET_BZERO(&((char *)context->buffer)[usedspace], SHA512_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
- SHA512_Transform(context, (sha2_word64*)context->buffer);
+ SHA512_Transform(context, context->buffer);
/* And set-up for the last transform: */
- MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
+ MEMSET_BZERO((char *)context->buffer, SHA512_BLOCK_LENGTH - 2);
}
} else {
/* Prepare for final transform: */
- MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
+ MEMSET_BZERO((char *)context->buffer, SHA512_SHORT_BLOCK_LENGTH);
/* Begin padding with a 1 bit: */
- *context->buffer = 0x80;
+ *((char *)context->buffer) = 0x80;
}
/* Store the length of input data (in bits): */
- *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
- *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
+ MEMCPY_BCOPY(&((char *)context->buffer)[SHA512_SHORT_BLOCK_LENGTH], (char *)(&context->bitcount[1]), 8);
+ MEMCPY_BCOPY(&((char *)context->buffer)[SHA512_SHORT_BLOCK_LENGTH + 8], (char *)(&context->bitcount[0]), 8);
/* Final transform: */
- SHA512_Transform(context, (sha2_word64*)context->buffer);
+ SHA512_Transform(context, context->buffer);
}
-void sat_SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
+void solv_SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
sha2_word64 *d = (sha2_word64*)digest;
/* Sanity check: */
SHA512_Last(context);
/* Save the hash data for output: */
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
{
/* Convert TO host byte order */
int j;
}
/* Zero out state data */
- MEMSET_BZERO(context, sizeof(context));
-}
-
-char *sat_SHA512_End(SHA512_CTX* context, char buffer[]) {
- sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
- int i;
-
- /* Sanity check: */
- /* assert(context != (SHA512_CTX*)0); */
-
- if (buffer != (char*)0) {
- sat_SHA512_Final(digest, context);
-
- for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
- *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
- *buffer++ = sha2_hex_digits[*d & 0x0f];
- d++;
- }
- *buffer = (char)0;
- } else {
- MEMSET_BZERO(context, sizeof(context));
- }
- MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
- return buffer;
-}
-
-char* sat_SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
- SHA512_CTX context;
-
- sat_SHA512_Init(&context);
- sat_SHA512_Update(&context, data, len);
- return sat_SHA512_End(&context, digest);
+ MEMSET_BZERO(context, sizeof(*context));
}
/*** SHA-384: *********************************************************/
-void sat_SHA384_Init(SHA384_CTX* context) {
+void solv_SHA384_Init(SHA384_CTX* context) {
if (context == (SHA384_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
- MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
+ MEMSET_BZERO((char *)context->buffer, SHA384_BLOCK_LENGTH);
context->bitcount[0] = context->bitcount[1] = 0;
}
-void sat_SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
- sat_SHA512_Update((SHA512_CTX*)context, data, len);
+void solv_SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
+ solv_SHA512_Update((SHA512_CTX*)context, data, len);
}
-void sat_SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
+void solv_SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
sha2_word64 *d = (sha2_word64*)digest;
/* Sanity check: */
SHA512_Last((SHA512_CTX*)context);
/* Save the hash data for output: */
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
{
/* Convert TO host byte order */
int j;
}
/* Zero out state data */
- MEMSET_BZERO(context, sizeof(context));
+ MEMSET_BZERO(context, sizeof(*context));
+}
+
+
+/*** SHA-224: *********************************************************/
+
+void solv_SHA224_Init(SHA224_CTX* context) {
+ if (context == (SHA224_CTX*)0) {
+ return;
+ }
+ MEMCPY_BCOPY(context->state, sha224_initial_hash_value, SHA256_DIGEST_LENGTH);
+ MEMSET_BZERO((char *)context->buffer, SHA224_BLOCK_LENGTH);
+ context->bitcount = 0;
}
-char *sat_SHA384_End(SHA384_CTX* context, char buffer[]) {
- sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
- int i;
+void solv_SHA224_Update(SHA224_CTX* context, const sha2_byte* data, size_t len) {
+ solv_SHA256_Update((SHA256_CTX*)context, data, len);
+}
+
+void solv_SHA224_Final(sha2_byte digest[], SHA224_CTX* context) {
+ sha2_word32 *d = (sha2_word32*)digest;
/* Sanity check: */
- /* assert(context != (SHA384_CTX*)0); */
+ /* assert(context != (SHA224_CTX*)0); */
- if (buffer != (char*)0) {
- sat_SHA384_Final(digest, context);
+ /* If no digest buffer is passed, we don't bother doing this: */
+ if (digest != (sha2_byte*)0) {
+ SHA256_Last(context);
- for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
- *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
- *buffer++ = sha2_hex_digits[*d & 0x0f];
- d++;
+#ifndef WORDS_BIGENDIAN
+ {
+ /* Convert TO host byte order */
+ int j;
+ for (j = 0; j < 7; j++) {
+ REVERSE32(context->state[j],context->state[j]);
+ *d++ = context->state[j];
+ }
}
- *buffer = (char)0;
- } else {
- MEMSET_BZERO(context, sizeof(context));
+#else
+ MEMCPY_BCOPY(d, context->state, SHA224_DIGEST_LENGTH);
+#endif
}
- MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
- return buffer;
-}
-char* sat_SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
- SHA384_CTX context;
-
- sat_SHA384_Init(&context);
- sat_SHA384_Update(&context, data, len);
- return sat_SHA384_End(&context, digest);
+ /* Clean up state data: */
+ MEMSET_BZERO(context, sizeof(*context));
}