From 9ad949a760233d4da6efaaa94be48b0afe53440e Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Sat, 6 Sep 2025 19:02:03 -0500 Subject: [PATCH] sha512 first pass --- source/common/crypto/md5.cpp | 264 ---------------------------- source/common/crypto/md5.h | 47 ----- source/common/crypto/sha512.cpp | 300 +++++++++++++++++--------------- source/common/crypto/sha512.h | 51 +++--- 4 files changed, 182 insertions(+), 480 deletions(-) delete mode 100644 source/common/crypto/md5.cpp delete mode 100644 source/common/crypto/md5.h diff --git a/source/common/crypto/md5.cpp b/source/common/crypto/md5.cpp deleted file mode 100644 index 7968980..0000000 --- a/source/common/crypto/md5.cpp +++ /dev/null @@ -1,264 +0,0 @@ -// Copyright (C) 2007-2025 EQ2EMulator -// Licensed under GPL v3 -#include /* for memcpy() */ -#include "md5.h" -#include "../MiscFunctions.h" -#include "../seperator.h" - -MD5::MD5() { - memset(pMD5, 0, 16); -} - -MD5::MD5(const uchar* buf, uint32 len) { - Generate(buf, len, pMD5); -} - -MD5::MD5(const char* buf, uint32 len) { - Generate((const uchar*) buf, len, pMD5); -} - -MD5::MD5(const int8 buf[16]) { - Set(buf); -} - -MD5::MD5(const char* iMD5String) { - Set(iMD5String); -} - -void MD5::Generate(const char* iString) { - Generate((const uchar*) iString, strlen(iString)); -} - -void MD5::Generate(const int8* buf, uint32 len) { - Generate(buf, len, pMD5); -} - -bool MD5::Set(const int8 buf[16]) { - memcpy(pMD5, buf, 16); - return true; -} - -bool MD5::Set(const char* iMD5String) { - char tmp[5] = { '0', 'x', 0, 0, 0 }; - for (int i=0; i<16; i++) { - tmp[2] = iMD5String[i*2]; - tmp[3] = iMD5String[(i*2) + 1]; - if (!Seperator::IsHexNumber(tmp)) - return false; - pMD5[i] = hextoi(tmp); - } - return true; -} - -MD5::operator const char* () { - snprintf(pMD5String, sizeof(pMD5String), "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", pMD5[0], pMD5[1], pMD5[2], pMD5[3], pMD5[4], pMD5[5], pMD5[6], pMD5[7], pMD5[8], pMD5[9], pMD5[10], pMD5[11], pMD5[12], pMD5[13], pMD5[14], pMD5[15]); - return pMD5String; -} - -bool MD5::operator== (const MD5& iMD5) { - if (memcmp(pMD5, iMD5.pMD5, 16) == 0) - return true; - else - return false; -} - -bool MD5::operator== (const int8* iMD5) { - if (memcmp(pMD5, iMD5, 16) == 0) - return true; - else - return false; -} - -bool MD5::operator== (const char* iMD5String) { - char tmp[5] = { '0', 'x', 0, 0, 0 }; - for (int i=0; i<16; i++) { - tmp[2] = iMD5String[i*2]; - tmp[3] = iMD5String[(i*2) + 1]; - if (pMD5[i] != hextoi(tmp)) - return false; - } - return true; -} - -MD5& MD5::operator= (const MD5& iMD5) { - memcpy(pMD5, iMD5.pMD5, 16); - return *this; -} - -MD5* MD5::operator= (const MD5* iMD5) { - memcpy(pMD5, iMD5->pMD5, 16); - return this; -} - -/* Byte-swap an array of words to little-endian. (Byte-sex independent) */ -void MD5::byteSwap(uint32 *buf, uint32 words) { - int8 *p = (int8 *)buf; - do { - *buf++ = (uint32)((uint32)p[3]<<8 | p[2]) << 16 | - ((uint32)p[1]<<8 | p[0]); - p += 4; - } while (--words); -} - -void MD5::Generate(const int8* buf, uint32 len, int8 digest[16]) { - MD5Context ctx; - Init(&ctx); - Update(&ctx, buf, len); - Final(digest, &ctx); -} - -/* Start MD5 accumulation. */ -void MD5::Init(struct MD5Context *ctx) { - ctx->hash[0] = 0x67452301; - ctx->hash[1] = 0xefcdab89; - ctx->hash[2] = 0x98badcfe; - ctx->hash[3] = 0x10325476; - ctx->bytes[1] = ctx->bytes[0] = 0; -} - -/* Update ctx to reflect the addition of another buffer full of bytes. */ -void MD5::Update(struct MD5Context *ctx, int8 const *buf, uint32 len) { - uint32 t = ctx->bytes[0]; - if ((ctx->bytes[0] = t + len) < t) /* Update 64-bit byte count */ - ctx->bytes[1]++; /* Carry from low to high */ - - - - t = 64 - (t & 0x3f); /* Bytes available in ctx->input (>= 1) */ - if (t > len) { - memcpy((int8*)ctx->input+64-t, buf, len); - return; - } - /* First chunk is an odd size */ - memcpy((int8*)ctx->input+64-t, buf, t); - byteSwap(ctx->input, 16); - Transform(ctx->hash, ctx->input); - buf += t; - len -= t; - /* Process data in 64-byte chunks */ - while (len >= 64) { - memcpy(ctx->input, buf, 64); - byteSwap(ctx->input, 16); - Transform(ctx->hash, ctx->input); - buf += 64; - len -= 64; - } - /* Buffer any remaining bytes of data */ - memcpy(ctx->input, buf, len); -} - -/* Final wrapup - pad to 64-byte boundary with the bit pattern -* 1 0* (64-bit count of bits processed, LSB-first) */ -void MD5::Final(int8 digest[16], MD5Context *ctx) { - int count = ctx->bytes[0] & 0x3F; /* Bytes mod 64 */ - int8 *p = (int8*)ctx->input + count; - /* Set the first byte of padding to 0x80. There is always room. */ - *p++ = 0x80; - /* Bytes of zero padding needed to make 56 bytes (-8..55) */ - count = 56 - 1 - count; - if (count < 0) { /* Padding forces an extra block */ - memset(p, 0, count+8); - byteSwap(ctx->input, 16); - Transform(ctx->hash, ctx->input); - p = (int8*)ctx->input; - count = 56; - } - memset(p, 0, count); - byteSwap(ctx->input, 14); - /* Append 8 bytes of length in *bits* and transform */ - ctx->input[14] = ctx->bytes[0] << 3; - - ctx->input[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; - Transform(ctx->hash, ctx->input); - byteSwap(ctx->hash, 4); - memcpy(digest, ctx->hash, 16); - memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ -} - -/* The four core functions */ -#define F1(x, y, z) (z ^ (x & (y ^ z))) -#define F2(x, y, z) F1(z, x, y) -#define F3(x, y, z) (x ^ y ^ z) -#define F4(x, y, z) (y ^ (x | ~z)) -/* This is the central step in the MD5 algorithm. */ -#define MD5STEP(f,w,x,y,z,in,s) (w += f(x,y,z)+in, w = (w<>(32-s)) + x) - - - -/* The heart of the MD5 algorithm. */ -void MD5::Transform(uint32 hash[4], const uint32 input[16]) { - uint32 a = hash[0], b = hash[1], c = hash[2], d = hash[3]; - - MD5STEP(F1, a, b, c, d, input[ 0]+0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, input[ 1]+0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, input[ 2]+0x242070db, 17); - MD5STEP(F1, b, c, d, a, input[ 3]+0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, input[ 4]+0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, input[ 5]+0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, input[ 6]+0xa8304613, 17); - MD5STEP(F1, b, c, d, a, input[ 7]+0xfd469501, 22); - MD5STEP(F1, a, b, c, d, input[ 8]+0x698098d8, 7); - MD5STEP(F1, d, a, b, c, input[ 9]+0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, input[10]+0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, input[11]+0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, input[12]+0x6b901122, 7); - MD5STEP(F1, d, a, b, c, input[13]+0xfd987193, 12); - MD5STEP(F1, c, d, a, b, input[14]+0xa679438e, 17); - MD5STEP(F1, b, c, d, a, input[15]+0x49b40821, 22); - - MD5STEP(F2, a, b, c, d, input[ 1]+0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, input[ 6]+0xc040b340, 9); - MD5STEP(F2, c, d, a, b, input[11]+0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, input[ 0]+0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, input[ 5]+0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, input[10]+0x02441453, 9); - MD5STEP(F2, c, d, a, b, input[15]+0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, input[ 4]+0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, input[ 9]+0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, input[14]+0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, input[ 3]+0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, input[ 8]+0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, input[13]+0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, input[ 2]+0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, input[ 7]+0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, input[12]+0x8d2a4c8a, 20); - - - - - MD5STEP(F3, a, b, c, d, input[ 5]+0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, input[ 8]+0x8771f681, 11); - MD5STEP(F3, c, d, a, b, input[11]+0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, input[14]+0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, input[ 1]+0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, input[ 4]+0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, input[ 7]+0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, input[10]+0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, input[13]+0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, input[ 0]+0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, input[ 3]+0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, input[ 6]+0x04881d05, 23); - MD5STEP(F3, a, b, c, d, input[ 9]+0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, input[12]+0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, input[15]+0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, input[ 2]+0xc4ac5665, 23); - - MD5STEP(F4, a, b, c, d, input[ 0]+0xf4292244, 6); - MD5STEP(F4, d, a, b, c, input[ 7]+0x432aff97, 10); - MD5STEP(F4, c, d, a, b, input[14]+0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, input[ 5]+0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, input[12]+0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, input[ 3]+0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, input[10]+0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, input[ 1]+0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, input[ 8]+0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, input[15]+0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, input[ 6]+0xa3014314, 15); - MD5STEP(F4, b, c, d, a, input[13]+0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, input[ 4]+0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, input[11]+0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, input[ 2]+0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, input[ 9]+0xeb86d391, 21); - - hash[0] += a; hash[1] += b; hash[2] += c; hash[3] += d; -} diff --git a/source/common/crypto/md5.h b/source/common/crypto/md5.h deleted file mode 100644 index 8497ca3..0000000 --- a/source/common/crypto/md5.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) 2007-2025 EQ2EMulator -// Licensed under GPL v3 -#ifndef MD5_H -#define MD5_H -#include "../common/types.h" - - -class MD5 { -public: - struct MD5Context { - uint32 hash[4]; - uint32 bytes[2]; - uint32 input[16]; - }; - static void Generate(const int8* buf, uint32 len, int8 digest[16]); - - static void Init(struct MD5Context *context); - static void Update(struct MD5Context *context, const int8 *buf, uint32 len); - static void Final(int8 digest[16], struct MD5Context *context); - - MD5(); - MD5(const uchar* buf, uint32 len); - MD5(const char* buf, uint32 len); - MD5(const int8 buf[16]); - MD5(const char* iMD5String); - - void Generate(const char* iString); - void Generate(const int8* buf, uint32 len); - bool Set(const int8 buf[16]); - bool Set(const char* iMD5String); - - bool operator== (const MD5& iMD5); - bool operator== (const int8 iMD5[16]); - bool operator== (const char* iMD5String); - - MD5& operator= (const MD5& iMD5); - MD5* operator= (const MD5* iMD5); - MD5* operator= (const int8* iMD5); - operator const char* (); -protected: - int8 pMD5[16]; -private: - static void byteSwap(uint32 *buf, uint32 words); - static void Transform(uint32 hash[4], const int32 input[16]); - char pMD5String[33]; -}; -#endif diff --git a/source/common/crypto/sha512.cpp b/source/common/crypto/sha512.cpp index 10b9592..6658798 100644 --- a/source/common/crypto/sha512.cpp +++ b/source/common/crypto/sha512.cpp @@ -1,155 +1,165 @@ -#include -#include +// Copyright (C) 2007-2025 EQ2EMulator +// Licensed under GPL v3 + #include "sha512.h" - -const unsigned long long SHA512::sha512_k[80] = //ULL = uint64 - {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}; - -void SHA512::transform(const unsigned char *message, unsigned int block_nb) +#include +#include +#include + +const std::array SHA512::sha512_k{ + 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 +}; + +void SHA512::transform(const unsigned char* message, unsigned int block_nb) noexcept { - uint64 w[80]; - uint64 wv[8]; - uint64 t1, t2; - const unsigned char *sub_block; - int i, j; - for (i = 0; i < (int) block_nb; i++) { - sub_block = message + (i << 7); - for (j = 0; j < 16; j++) { - SHA2_PACK64(&sub_block[j << 3], &w[j]); - } - for (j = 16; j < 80; j++) { - w[j] = SHA512_F4(w[j - 2]) + w[j - 7] + SHA512_F3(w[j - 15]) + w[j - 16]; - } - for (j = 0; j < 8; j++) { - wv[j] = m_h[j]; - } - for (j = 0; j < 80; j++) { - t1 = wv[7] + SHA512_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6]) - + sha512_k[j] + w[j]; - t2 = SHA512_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]); - wv[7] = wv[6]; - wv[6] = wv[5]; - wv[5] = wv[4]; - wv[4] = wv[3] + t1; - wv[3] = wv[2]; - wv[2] = wv[1]; - wv[1] = wv[0]; - wv[0] = t1 + t2; - } - for (j = 0; j < 8; j++) { - m_h[j] += wv[j]; - } - - } + std::array w{}; + std::array wv{}; + uint64 t1, t2; + const unsigned char* sub_block; + + for (unsigned int i = 0; i < block_nb; ++i) { + sub_block = message + (i << 7); + + for (int j = 0; j < 16; ++j) { + SHA2_PACK64(&sub_block[j << 3], &w[j]); + } + + for (int j = 16; j < 80; ++j) { + w[j] = SHA512_F4(w[j - 2]) + w[j - 7] + SHA512_F3(w[j - 15]) + w[j - 16]; + } + + for (int j = 0; j < 8; ++j) { + wv[j] = m_h[j]; + } + + for (int j = 0; j < 80; ++j) { + t1 = wv[7] + SHA512_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6]) + sha512_k[j] + w[j]; + t2 = SHA512_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]); + wv[7] = wv[6]; + wv[6] = wv[5]; + wv[5] = wv[4]; + wv[4] = wv[3] + t1; + wv[3] = wv[2]; + wv[2] = wv[1]; + wv[1] = wv[0]; + wv[0] = t1 + t2; + } + + for (int j = 0; j < 8; ++j) { + m_h[j] += wv[j]; + } + } } - -void SHA512::init() + +void SHA512::init() noexcept { - m_h[0] = 0x6a09e667f3bcc908ULL; - m_h[1] = 0xbb67ae8584caa73bULL; - m_h[2] = 0x3c6ef372fe94f82bULL; - m_h[3] = 0xa54ff53a5f1d36f1ULL; - m_h[4] = 0x510e527fade682d1ULL; - m_h[5] = 0x9b05688c2b3e6c1fULL; - m_h[6] = 0x1f83d9abfb41bd6bULL; - m_h[7] = 0x5be0cd19137e2179ULL; - m_len = 0; - m_tot_len = 0; + m_h[0] = 0x6a09e667f3bcc908ULL; + m_h[1] = 0xbb67ae8584caa73bULL; + m_h[2] = 0x3c6ef372fe94f82bULL; + m_h[3] = 0xa54ff53a5f1d36f1ULL; + m_h[4] = 0x510e527fade682d1ULL; + m_h[5] = 0x9b05688c2b3e6c1fULL; + m_h[6] = 0x1f83d9abfb41bd6bULL; + m_h[7] = 0x5be0cd19137e2179ULL; + m_len = 0; + m_tot_len = 0; } - -void SHA512::update(const unsigned char *message, unsigned int len) + +void SHA512::update(const unsigned char* message, unsigned int len) noexcept { - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char *shifted_message; - tmp_len = SHA384_512_BLOCK_SIZE - m_len; - rem_len = len < tmp_len ? len : tmp_len; - memcpy(&m_block[m_len], message, rem_len); - if (m_len + len < SHA384_512_BLOCK_SIZE) { - m_len += len; - return; - } - new_len = len - rem_len; - block_nb = new_len / SHA384_512_BLOCK_SIZE; - shifted_message = message + rem_len; - transform(m_block, 1); - transform(shifted_message, block_nb); - rem_len = new_len % SHA384_512_BLOCK_SIZE; - memcpy(m_block, &shifted_message[block_nb << 7], rem_len); - m_len = rem_len; - m_tot_len += (block_nb + 1) << 7; + unsigned int block_nb; + unsigned int new_len, rem_len, tmp_len; + const unsigned char* shifted_message; + + tmp_len = SHA384_512_BLOCK_SIZE - m_len; + rem_len = len < tmp_len ? len : tmp_len; + std::memcpy(&m_block[m_len], message, rem_len); + + if (m_len + len < SHA384_512_BLOCK_SIZE) { + m_len += len; + return; + } + + new_len = len - rem_len; + block_nb = new_len / SHA384_512_BLOCK_SIZE; + shifted_message = message + rem_len; + transform(m_block.data(), 1); + transform(shifted_message, block_nb); + rem_len = new_len % SHA384_512_BLOCK_SIZE; + std::memcpy(m_block.data(), &shifted_message[block_nb << 7], rem_len); + m_len = rem_len; + m_tot_len += (block_nb + 1) << 7; } - -void SHA512::final(unsigned char *digest) + +void SHA512::final(unsigned char* digest) noexcept { - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; - int i; - block_nb = 1 + ((SHA384_512_BLOCK_SIZE - 17) - < (m_len % SHA384_512_BLOCK_SIZE)); - len_b = (m_tot_len + m_len) << 3; - pm_len = block_nb << 7; - memset(m_block + m_len, 0, pm_len - m_len); - m_block[m_len] = 0x80; - SHA2_UNPACK32(len_b, m_block + pm_len - 4); - transform(m_block, block_nb); - for (i = 0 ; i < 8; i++) { - SHA2_UNPACK64(m_h[i], &digest[i << 3]); - } + unsigned int block_nb; + unsigned int pm_len; + unsigned int len_b; + + block_nb = 1 + ((SHA384_512_BLOCK_SIZE - 17) < (m_len % SHA384_512_BLOCK_SIZE)); + len_b = (m_tot_len + m_len) << 3; + pm_len = block_nb << 7; + std::memset(m_block.data() + m_len, 0, pm_len - m_len); + m_block[m_len] = 0x80; + SHA2_UNPACK32(len_b, m_block.data() + pm_len - 4); + transform(m_block.data(), block_nb); + + for (int i = 0; i < 8; ++i) { + SHA2_UNPACK64(m_h[i], &digest[i << 3]); + } } - -std::string sha512(std::string input) + +std::string sha512(std::string_view input) noexcept { - unsigned char digest[SHA512::DIGEST_SIZE]; - memset(digest,0,SHA512::DIGEST_SIZE); - SHA512 ctx = SHA512(); - ctx.init(); - ctx.update((unsigned char*)input.c_str(), input.length()); - ctx.final(digest); - - char buf[2*SHA512::DIGEST_SIZE+1]; - buf[2*SHA512::DIGEST_SIZE] = 0; - for (int i = 0; i < SHA512::DIGEST_SIZE; i++) - sprintf(buf+i*2, "%02x", digest[i]); - return std::string(buf); -} \ No newline at end of file + std::array digest{}; + SHA512 ctx; + ctx.init(); + ctx.update(reinterpret_cast(input.data()), static_cast(input.length())); + ctx.final(digest.data()); + + std::array buf{}; + for (int i = 0; i < SHA512::DIGEST_SIZE; ++i) { + std::sprintf(buf.data() + i * 2, "%02x", digest[i]); + } + return std::string{buf.data()}; +} diff --git a/source/common/crypto/sha512.h b/source/common/crypto/sha512.h index 72ce5a8..8197077 100644 --- a/source/common/crypto/sha512.h +++ b/source/common/crypto/sha512.h @@ -1,33 +1,36 @@ -#ifndef SHA512_H -#define SHA512_H +#pragma once + +#include +#include #include - +#include + class SHA512 { -protected: - typedef unsigned char uint8; - typedef unsigned int uint32; - typedef unsigned long long uint64; - - const static uint64 sha512_k[]; - static const unsigned int SHA384_512_BLOCK_SIZE = (1024/8); - public: - void init(); - void update(const unsigned char *message, unsigned int len); - void final(unsigned char *digest); - static const unsigned int DIGEST_SIZE = ( 512 / 8); - + static constexpr unsigned int DIGEST_SIZE = 512 / 8; + static constexpr unsigned int SHA384_512_BLOCK_SIZE = 1024 / 8; + + void init() noexcept; + void update(const unsigned char* message, unsigned int len) noexcept; + void final(unsigned char* digest) noexcept; + protected: - void transform(const unsigned char *message, unsigned int block_nb); - unsigned int m_tot_len; - unsigned int m_len; - unsigned char m_block[2 * SHA384_512_BLOCK_SIZE]; - uint64 m_h[8]; + using uint8 = std::uint8_t; + using uint32 = std::uint32_t; + using uint64 = std::uint64_t; + + static const std::array sha512_k; + + void transform(const unsigned char* message, unsigned int block_nb) noexcept; + + unsigned int m_tot_len{}; + unsigned int m_len{}; + std::array m_block{}; + std::array m_h{}; }; - - -std::string sha512(std::string input); + +[[nodiscard]] std::string sha512(std::string_view input) noexcept; #define SHA2_SHFR(x, n) (x >> n) #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))