// sha512.c
// standard C includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// own includes
#include "sha512.h"
// bring TLS support to antique compilers
#ifndef thread_local
#ifdef _MSC_VER
#define thread_local __declspec(thread) // the thread_local keyword wasn't defined before C++11 and C23
#else // a saner compiler
#define thread_local __thread // the thread_local keyword wasn't defined before C++11 and C23
#endif // _MSC_VER
#endif // !thread_local
// compiler-specific glue
#ifdef _WIN32
#ifndef __BYTE_ORDER__
#define __ORDER_BIG_ENDIAN__ 4321
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ // all Windows machines are little endian
#endif // !__BYTE_ORDER__
#ifdef _MSC_VER
#define __builtin_bswap64(x) _byteswap_uint64 ((unsigned long long) (x))
#endif // _MSC_VER
#else // !WIN32, thus POSIX
#define sprintf_s(dst,siz,...) sprintf ((dst), __VA_ARGS__)
#endif // _WIN32
static void sha512_private_transform (SHA512_CTX *context, const uint64_t *data)
{
// logical functions used in SHA-384 and SHA-512
#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) // 64-bit rotate right
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ ((x) >> 7))
#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ ((x) >> 6))
// hash constant words K for SHA-384 and SHA-512
static const uint64_t K512[80] = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
};
uint64_t a, b, c, d, e, f, g, h, s0, s1;
uint64_t T1, T2, *W512 = (uint64_t *) context->buffer;
int j;
// initialize registers with the prev. intermediate value
a = context->state[0]; b = context->state[1]; c = context->state[2]; d = context->state[3]; e = context->state[4]; f = context->state[5]; g = context->state[6]; h = context->state[7];
for (j = 0; j < 16; j++)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
W512[j] = __builtin_bswap64 (*data); // convert to host byte order
#elif // __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
W512[j] = *data;
#else // __BYTE_ORDER__ == ???
#error Please port this SHA-512 code to your exotic endianness platform. What are you compiling this on? PDP? Honeywell?
#endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
// apply the SHA-512 compression function to update a..h
T1 = h + Sigma1_512 (e) + Ch (e, f, g) + K512[j] + W512[j];
T2 = Sigma0_512 (a) + Maj (a, b, c);
// update registers
h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2;
data++;
}
for (; j < 80; j++)
{
// part of the message block expansion
s0 = W512[(j + 1) & 0x0f];
s0 = sigma0_512 (s0);
s1 = W512[(j + 14) & 0x0f];
s1 = sigma1_512 (s1);
// apply the SHA-512 compression function to update a..h
T1 = h + Sigma1_512 (e) + Ch (e, f, g) + K512[j] + (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
T2 = Sigma0_512 (a) + Maj (a, b, c);
// update registers
h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2;
}
// compute the current intermediate hash value
context->state[0] += a; context->state[1] += b; context->state[2] += c; context->state[3] += d; context->state[4] += e; context->state[5] += f; context->state[6] += g; context->state[7] += h;
// clean up
a = b = c = d = e = f = g = h = T1 = T2 = 0;
#undef sigma1_512
#undef sigma0_512
#undef Sigma1_512
#undef Sigma0_512
#undef Maj
#undef Ch
#undef S64
return;
}
void SHA512_Init (SHA512_CTX *context)
{
// initial hash value H for SHA-512
static const uint64_t sha512_initial_hash_value[8] = {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
};
memcpy (context
->state
, sha512_initial_hash_value
, SHA512_DIGEST_LENGTH
);
memset (context
->buffer
, 0, SHA512_BLOCK_LENGTH
);
context->bitcount[0] = context->bitcount[1] = 0;
}
void SHA512_Update (SHA512_CTX *context, void *datain, size_t len)
{
#define ADDINC128(w,n) do { \
(w)[0] += (uint64_t) (n); \
if ((w)[0] < (n)) \
(w)[1]++; \
} while (0) // macro for incrementally adding the unsigned 64-bit integer n to the unsigned 128-bit integer (represented using a two-element array of 64-bit words
size_t freespace, usedspace;
const uint8_t *data = (const uint8_t *) datain;
if (len == 0)
return; // calling with empty data is valid - we do nothing
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
if (usedspace > 0)
{
// calculate how much free space is available in the buffer
freespace = SHA512_BLOCK_LENGTH - usedspace;
if (len >= freespace)
{
// fill the buffer completely and process it
memcpy (&context
->buffer
[usedspace
], data
, freespace
);
ADDINC128 (context->bitcount, freespace << 3);
len -= freespace;
data += freespace;
sha512_private_transform (context, (uint64_t *) context->buffer);
}
else
{
// the buffer is not full yet
memcpy (&context
->buffer
[usedspace
], data
, len
);
ADDINC128 (context->bitcount, len << 3);
// clean up
usedspace = freespace = 0;
return;
}
}
while (len >= SHA512_BLOCK_LENGTH)
{
// process as many complete blocks as we can
sha512_private_transform (context, (uint64_t *) data);
ADDINC128 (context->bitcount, SHA512_BLOCK_LENGTH << 3);
len -= SHA512_BLOCK_LENGTH;
data += SHA512_BLOCK_LENGTH;
}
if (len > 0)
{
// save leftovers
memcpy (context
->buffer
, data
, len
);
ADDINC128 (context->bitcount, len << 3);
}
// clean up
usedspace = freespace = 0;
#undef ADDINC128
return;
}
void SHA512_Final (uint8_t digest[SHA512_DIGEST_LENGTH], SHA512_CTX *context)
{
#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
size_t usedspace;
union { uint8_t *as_bytes; uint64_t *as_uint64s; } cast_var = { NULL };
// if no digest buffer is passed, don't bother finalizing the computation
if (digest != NULL)
{
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
context->bitcount[0] = __builtin_bswap64 (context->bitcount[0]); // convert from host byte order
context->bitcount[1] = __builtin_bswap64 (context->bitcount[1]); // convert from host byte order
#endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
if (usedspace > 0)
{
// begin padding with a 1 bit
context->buffer[usedspace++] = 0x80;
if (usedspace <= SHA512_SHORT_BLOCK_LENGTH)
memset (&context
->buffer
[usedspace
], 0, SHA512_SHORT_BLOCK_LENGTH
- usedspace
); // set-up for the last transform
else
{
if (usedspace < SHA512_BLOCK_LENGTH)
memset (&context
->buffer
[usedspace
], 0, SHA512_BLOCK_LENGTH
- usedspace
);
sha512_private_transform (context, (uint64_t *) context->buffer); // do second-to-last transform
memset (context
->buffer
, 0, SHA512_BLOCK_LENGTH
- 2); // and set-up for the last transform
}
}
else // usedspace == 0
{
memset (context
->buffer
, 0, SHA512_SHORT_BLOCK_LENGTH
); // prepare for final transform
*context->buffer = 0x80; // begin padding with a 1 bit
}
// store the length of input data (in bits)
cast_var.as_bytes = context->buffer;
cast_var.as_uint64s[SHA512_SHORT_BLOCK_LENGTH / 8 + 0] = context->bitcount[1];
cast_var.as_uint64s[SHA512_SHORT_BLOCK_LENGTH / 8 + 1] = context->bitcount[0];
// final transform
sha512_private_transform (context, (uint64_t *) context->buffer);
// save the hash data for output
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
for (int j = 0; j < 8; j++)
context->state[j] = __builtin_bswap64 (context->state[j]); // convert to host byte order
#endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
memcpy (digest
, context
->state
, SHA512_DIGEST_LENGTH
);
}
// zero out state data
memset (context
, 0, sizeof (SHA512_CTX
));
#undef SHA512_SHORT_BLOCK_LENGTH
return;
}
const char *SHA512 (void *data, size_t data_len, uint8_t *digest_or_NULL)
{
// computes the SHA-512 hash of a block of data in one pass and write it to digest, or to a static buffer if NULL
// returns the STRING REPRESENTATION of digest in a statically-allocated string
static thread_local uint8_t static_digest[SHA512_DIGEST_LENGTH] = "";
static thread_local char digest_as_string[2 * SHA512_DIGEST_LENGTH + 1] = "";
SHA512_CTX ctx;
size_t byte_index;
SHA512_Init (&ctx);
SHA512_Update (&ctx, data, data_len);
if (digest_or_NULL == NULL)
digest_or_NULL = static_digest;
SHA512_Final (digest_or_NULL, &ctx);
for (byte_index = 0; byte_index < SHA512_DIGEST_LENGTH; byte_index++)
sprintf_s (&digest_as_string[2 * byte_index], 3, "%02x", digest_or_NULL[byte_index]);
return (digest_as_string);
}