// SafeC Standard Library — SHA-512 / SHA-384 Hash (FIPS 180-4)
// Freestanding-safe. No dynamic allocation.
// Sha512Ctx conforms to hash.h's Hasher and BlockHash (generic hashing, Hmac).
#pragma once
#include <std/crypto/buf.h>
#define SHA512_DIGEST_SIZE 64 // bytes
#define SHA384_DIGEST_SIZE 48 // bytes
#define SHA512_BLOCK_SIZE 128 // bytes
namespace std {
struct Sha512Ctx {
unsigned long long h[8]; // current hash state
unsigned char buf[SHA512_BLOCK_SIZE]; // partial block buffer
// Total bytes processed. SHA-512's length field is 128 bits, but a
// single 64-bit counter is tracked here (matching every other hash
// module in this library) since no realistic input reaches 2^64 bytes
// — the length field's high 8 bytes are simply written as zero.
unsigned long long total_bytes;
int is384; // 1 = SHA-384 variant
// A fresh SHA-512 context.
static struct Sha512Ctx new();
// A fresh SHA-384 context (same struct, is384 = 1).
static struct Sha512Ctx new_384();
// Feed `len` bytes of data into the hash.
void update([]const unsigned char data);
// internal: the (reference, length) core of update()
void update_n_(const &unsigned char data, unsigned long len);
// Finalise and write the digest.
// For SHA-512: digest must be at least 64 bytes.
// For SHA-384: digest must be at least 48 bytes.
void finish([]unsigned char digest);
// internal: the (reference, length) core of finish()
void finish_n_(&unsigned char digest);
// 64 (SHA-512) or 48 (SHA-384) / SHA512_BLOCK_SIZE (hash.h's BlockHash).
unsigned long digest_size() const;
unsigned long block_size() const;
};
// Convenience: hash `len` bytes and write the 64-byte digest.
void sha512([]const unsigned char data, []unsigned char digest);
// internal: the (reference, length) core of sha512()
void sha512_n_(const &unsigned char data, unsigned long len,
&unsigned char digest);
// Convenience: hash `len` bytes and write the 48-byte digest.
void sha384([]const unsigned char data, []unsigned char digest);
// internal: the (reference, length) core of sha384()
void sha384_n_(const &unsigned char data, unsigned long len,
&unsigned char digest);
} // namespace std