switch to much faster project nayuki sha implementations

This commit is contained in:
Thomas Fussell 2017-04-13 21:45:07 -04:00
parent 104e3bea25
commit 3b2a0ac5d6
4 changed files with 476 additions and 477 deletions

View File

@ -64,7 +64,7 @@ file(GLOB WORKSHEET_SOURCES ${XLNT_SOURCE_DIR}/worksheet/*.cpp)
file(GLOB DETAIL_HEADERS ${XLNT_SOURCE_DIR}/detail/*.hpp) file(GLOB DETAIL_HEADERS ${XLNT_SOURCE_DIR}/detail/*.hpp)
file(GLOB DETAIL_SOURCES ${XLNT_SOURCE_DIR}/detail/*.cpp) file(GLOB DETAIL_SOURCES ${XLNT_SOURCE_DIR}/detail/*.cpp)
file(GLOB DETAIL_CRYPTO_HEADERS ${XLNT_SOURCE_DIR}/detail/crypto/*.hpp) file(GLOB DETAIL_CRYPTO_HEADERS ${XLNT_SOURCE_DIR}/detail/crypto/*.hpp)
file(GLOB DETAIL_CRYPTO_SOURCES ${XLNT_SOURCE_DIR}/detail/crypto/*.cpp) file(GLOB DETAIL_CRYPTO_SOURCES ${XLNT_SOURCE_DIR}/detail/crypto/*.c*)
set(XLNT_HEADERS ${ROOT_HEADERS} ${CELL_HEADERS} ${CHARTS_HEADERS} set(XLNT_HEADERS ${ROOT_HEADERS} ${CELL_HEADERS} ${CHARTS_HEADERS}
${CHARTSHEET_HEADERS} ${DRAWING_HEADERS} ${FORMULA_HEADERS} ${CHARTSHEET_HEADERS} ${DRAWING_HEADERS} ${FORMULA_HEADERS}

View File

@ -29,499 +29,55 @@
#include <detail/crypto/sha.hpp> #include <detail/crypto/sha.hpp>
namespace { extern "C" {
class SHA1 extern void sha1_hash(const uint8_t *message, size_t len, uint32_t hash[5]);
{ extern void sha512_hash(const uint8_t *message, size_t len, uint64_t hash[8]);
public:
SHA1();
void update(const std::string &s);
void update(std::istream &is);
std::string final_();
private:
uint32_t digest[5];
std::string buffer;
uint64_t transforms;
};
static const size_t BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */
static const size_t BLOCK_BYTES = BLOCK_INTS * 4;
static void sha1_reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
{
/* SHA1 initialization constants */
digest[0] = 0x67452301;
digest[1] = 0xefcdab89;
digest[2] = 0x98badcfe;
digest[3] = 0x10325476;
digest[4] = 0xc3d2e1f0;
/* Reset counters */
buffer = "";
transforms = 0;
} }
static uint32_t sha1_rol(const uint32_t value, const size_t bits)
{
return (value << bits) | (value >> (32 - bits));
}
static uint32_t sha1_blk(const uint32_t block[BLOCK_INTS], const size_t i)
{
return sha1_rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1);
}
/*
* (R0+R1), R2, R3, R4 are the different operations used in SHA1
*/
static void sha1_R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
{
z += ((w&(x^y))^y) + block[i] + 0x5a827999 + sha1_rol(v, 5);
w = sha1_rol(w, 30);
}
static void sha1_R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
{
block[i] = sha1_blk(block, i);
z += ((w&(x^y))^y) + block[i] + 0x5a827999 + sha1_rol(v, 5);
w = sha1_rol(w, 30);
}
static void sha1_R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
{
block[i] = sha1_blk(block, i);
z += (w^x^y) + block[i] + 0x6ed9eba1 + sha1_rol(v, 5);
w = sha1_rol(w, 30);
}
static void sha1_R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
{
block[i] = sha1_blk(block, i);
z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + sha1_rol(v, 5);
w = sha1_rol(w, 30);
}
static void sha1_R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
{
block[i] = sha1_blk(block, i);
z += (w^x^y) + block[i] + 0xca62c1d6 + sha1_rol(v, 5);
w = sha1_rol(w, 30);
}
/*
* Hash a single 512-bit block. This is the core of the algorithm.
*/
static void sha1_transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms)
{
/* Copy digest[] to working vars */
uint32_t a = digest[0];
uint32_t b = digest[1];
uint32_t c = digest[2];
uint32_t d = digest[3];
uint32_t e = digest[4];
/* 4 rounds of 20 operations each. Loop unrolled. */
sha1_R0(block, a, b, c, d, e, 0);
sha1_R0(block, e, a, b, c, d, 1);
sha1_R0(block, d, e, a, b, c, 2);
sha1_R0(block, c, d, e, a, b, 3);
sha1_R0(block, b, c, d, e, a, 4);
sha1_R0(block, a, b, c, d, e, 5);
sha1_R0(block, e, a, b, c, d, 6);
sha1_R0(block, d, e, a, b, c, 7);
sha1_R0(block, c, d, e, a, b, 8);
sha1_R0(block, b, c, d, e, a, 9);
sha1_R0(block, a, b, c, d, e, 10);
sha1_R0(block, e, a, b, c, d, 11);
sha1_R0(block, d, e, a, b, c, 12);
sha1_R0(block, c, d, e, a, b, 13);
sha1_R0(block, b, c, d, e, a, 14);
sha1_R0(block, a, b, c, d, e, 15);
sha1_R1(block, e, a, b, c, d, 0);
sha1_R1(block, d, e, a, b, c, 1);
sha1_R1(block, c, d, e, a, b, 2);
sha1_R1(block, b, c, d, e, a, 3);
sha1_R2(block, a, b, c, d, e, 4);
sha1_R2(block, e, a, b, c, d, 5);
sha1_R2(block, d, e, a, b, c, 6);
sha1_R2(block, c, d, e, a, b, 7);
sha1_R2(block, b, c, d, e, a, 8);
sha1_R2(block, a, b, c, d, e, 9);
sha1_R2(block, e, a, b, c, d, 10);
sha1_R2(block, d, e, a, b, c, 11);
sha1_R2(block, c, d, e, a, b, 12);
sha1_R2(block, b, c, d, e, a, 13);
sha1_R2(block, a, b, c, d, e, 14);
sha1_R2(block, e, a, b, c, d, 15);
sha1_R2(block, d, e, a, b, c, 0);
sha1_R2(block, c, d, e, a, b, 1);
sha1_R2(block, b, c, d, e, a, 2);
sha1_R2(block, a, b, c, d, e, 3);
sha1_R2(block, e, a, b, c, d, 4);
sha1_R2(block, d, e, a, b, c, 5);
sha1_R2(block, c, d, e, a, b, 6);
sha1_R2(block, b, c, d, e, a, 7);
sha1_R3(block, a, b, c, d, e, 8);
sha1_R3(block, e, a, b, c, d, 9);
sha1_R3(block, d, e, a, b, c, 10);
sha1_R3(block, c, d, e, a, b, 11);
sha1_R3(block, b, c, d, e, a, 12);
sha1_R3(block, a, b, c, d, e, 13);
sha1_R3(block, e, a, b, c, d, 14);
sha1_R3(block, d, e, a, b, c, 15);
sha1_R3(block, c, d, e, a, b, 0);
sha1_R3(block, b, c, d, e, a, 1);
sha1_R3(block, a, b, c, d, e, 2);
sha1_R3(block, e, a, b, c, d, 3);
sha1_R3(block, d, e, a, b, c, 4);
sha1_R3(block, c, d, e, a, b, 5);
sha1_R3(block, b, c, d, e, a, 6);
sha1_R3(block, a, b, c, d, e, 7);
sha1_R3(block, e, a, b, c, d, 8);
sha1_R3(block, d, e, a, b, c, 9);
sha1_R3(block, c, d, e, a, b, 10);
sha1_R3(block, b, c, d, e, a, 11);
sha1_R4(block, a, b, c, d, e, 12);
sha1_R4(block, e, a, b, c, d, 13);
sha1_R4(block, d, e, a, b, c, 14);
sha1_R4(block, c, d, e, a, b, 15);
sha1_R4(block, b, c, d, e, a, 0);
sha1_R4(block, a, b, c, d, e, 1);
sha1_R4(block, e, a, b, c, d, 2);
sha1_R4(block, d, e, a, b, c, 3);
sha1_R4(block, c, d, e, a, b, 4);
sha1_R4(block, b, c, d, e, a, 5);
sha1_R4(block, a, b, c, d, e, 6);
sha1_R4(block, e, a, b, c, d, 7);
sha1_R4(block, d, e, a, b, c, 8);
sha1_R4(block, c, d, e, a, b, 9);
sha1_R4(block, b, c, d, e, a, 10);
sha1_R4(block, a, b, c, d, e, 11);
sha1_R4(block, e, a, b, c, d, 12);
sha1_R4(block, d, e, a, b, c, 13);
sha1_R4(block, c, d, e, a, b, 14);
sha1_R4(block, b, c, d, e, a, 15);
/* Add the working vars back into digest[] */
digest[0] += a;
digest[1] += b;
digest[2] += c;
digest[3] += d;
digest[4] += e;
/* Count the number of transformations */
transforms++;
}
static void sha1_buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS])
{
/* Convert the std::string (byte buffer) to a uint32_t array (MSB) */
for (size_t i = 0; i < BLOCK_INTS; i++)
{
block[i] = static_cast<std::uint32_t>((buffer[4*i+3] & 0xff)
| (buffer[4*i+2] & 0xff)<<8
| (buffer[4*i+1] & 0xff)<<16
| (buffer[4*i+0] & 0xff)<<24);
}
}
SHA1::SHA1()
{
sha1_reset(digest, buffer, transforms);
}
void SHA1::update(const std::string &s)
{
std::istringstream is(s);
update(is);
}
void SHA1::update(std::istream &is)
{
while (true)
{
char sbuf[BLOCK_BYTES];
is.read(sbuf, static_cast<std::streamsize>(BLOCK_BYTES - buffer.size()));
buffer.append(sbuf, static_cast<std::size_t>(is.gcount()));
if (buffer.size() != BLOCK_BYTES)
{
return;
}
uint32_t block[BLOCK_INTS];
sha1_buffer_to_block(buffer, block);
sha1_transform(digest, block, transforms);
buffer.clear();
}
}
/*
* Add padding and return the message digest.
*/
std::string SHA1::final_()
{
/* Total number of hashed bits */
uint64_t total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8;
/* Padding */
buffer.append(1, static_cast<char>(0x80u));
auto orig_size = buffer.size();
while (buffer.size() < BLOCK_BYTES)
{
buffer.append(1, '\0');
}
uint32_t block[BLOCK_INTS];
sha1_buffer_to_block(buffer, block);
if (orig_size > BLOCK_BYTES - 8)
{
sha1_transform(digest, block, transforms);
for (size_t i = 0; i < BLOCK_INTS - 2; i++)
{
block[i] = 0;
}
}
/* Append total_bits, split this uint64_t into two uint32_t */
block[BLOCK_INTS - 1] = static_cast<std::uint32_t>(total_bits);
block[BLOCK_INTS - 2] = static_cast<std::uint32_t>(total_bits >> 32);
sha1_transform(digest, block, transforms);
/* Hex std::string */
std::ostringstream result;
for (size_t i = 0; i < sizeof(digest) / sizeof(digest[0]); i++)
{
result << std::hex << std::setfill('0') << std::setw(8);
result << digest[i];
}
/* Reset for next run */
sha1_reset(digest, buffer, transforms);
return result.str();
}
struct sha512_state
{
std::uint64_t length;
std::uint64_t state[8];
std::uint32_t curlen;
unsigned char buf[128];
};
typedef std::uint32_t u32;
typedef std::uint64_t u64;
static const u64 K[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
};
static u32 min(u32 x, u32 y)
{
return x < y ? x : y;
}
static void store64(u64 x, unsigned char* y)
{
for(int i = 0; i != 8; ++i)
y[i] = (x >> ((7-i) * 8)) & 255;
}
static u64 load64(const unsigned char* y)
{
u64 res = 0;
for(int i = 0; i != 8; ++i)
res |= u64(y[i]) << ((7-i) * 8);
return res;
}
static u64 Ch(u64 x, u64 y, u64 z) { return z ^ (x & (y ^ z)); }
static u64 Maj(u64 x, u64 y, u64 z) { return ((x | y) & z) | (x & y); }
static u64 Rot(u64 x, u64 n) { return (x >> (n & 63)) | (x << (64 - (n & 63))); }
static u64 Sh(u64 x, u64 n) { return x >> n; }
static u64 Sigma0(u64 x) { return Rot(x, 28) ^ Rot(x, 34) ^ Rot(x, 39); }
static u64 Sigma1(u64 x) { return Rot(x, 14) ^ Rot(x, 18) ^ Rot(x, 41); }
static u64 Gamma0(u64 x) { return Rot(x, 1) ^ Rot(x, 8) ^ Sh(x, 7); }
static u64 Gamma1(u64 x) { return Rot(x, 19) ^ Rot(x, 61) ^ Sh(x, 6); }
static void sha_compress(sha512_state& md, const unsigned char *buf)
{
u64 S[8], W[80], t0, t1;
// Copy state into S
for(int i = 0; i < 8; i++)
S[i] = md.state[i];
// Copy the state into 1024-bits into W[0..15]
for(int i = 0; i < 16; i++)
W[i] = load64(buf + (8*i));
// Fill W[16..79]
for(int i = 16; i < 80; i++)
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
// Compress
auto RND = [&](u64 a, u64 b, u64 c, u64& d, u64 e, u64 f, u64 g, u64& h, u64 i)
{
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i];
t1 = Sigma0(a) + Maj(a, b, c);
d += t0;
h = t0 + t1;
};
for(auto i = std::uint64_t(0); i < 80; i += 8)
{
RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
}
// Feedback
for(int i = 0; i < 8; i++)
md.state[i] = md.state[i] + S[i];
}
static void sha_init(sha512_state& md)
{
md.curlen = 0;
md.length = 0;
md.state[0] = 0x6a09e667f3bcc908ULL;
md.state[1] = 0xbb67ae8584caa73bULL;
md.state[2] = 0x3c6ef372fe94f82bULL;
md.state[3] = 0xa54ff53a5f1d36f1ULL;
md.state[4] = 0x510e527fade682d1ULL;
md.state[5] = 0x9b05688c2b3e6c1fULL;
md.state[6] = 0x1f83d9abfb41bd6bULL;
md.state[7] = 0x5be0cd19137e2179ULL;
}
static void sha_process(sha512_state& md, const void* src, u32 inlen)
{
const u32 block_size = sizeof(sha512_state::buf);
auto in = static_cast<const unsigned char*>(src);
while(inlen > 0)
{
if(md.curlen == 0 && inlen >= block_size)
{
sha_compress(md, in);
md.length += block_size * 8;
in += block_size;
inlen -= block_size;
}
else
{
u32 n = min(inlen, (block_size - md.curlen));
std::memcpy(md.buf + md.curlen, in, n);
md.curlen += n;
in += n;
inlen -= n;
if(md.curlen == block_size)
{
sha_compress(md, md.buf);
md.length += 8*block_size;
md.curlen = 0;
}
}
}
}
static void sha_done(sha512_state& md, void *out)
{
// Increase the length of the message
md.length += md.curlen * 8ULL;
// Append the '1' bit
md.buf[md.curlen++] = static_cast<unsigned char>(0x80);
// If the length is currently above 112 bytes we append zeros then compress.
// Then we can fall back to padding zeros and length encoding like normal.
if(md.curlen > 112)
{
while(md.curlen < 128)
md.buf[md.curlen++] = 0;
sha_compress(md, md.buf);
md.curlen = 0;
}
// Pad upto 120 bytes of zeroes
// note: that from 112 to 120 is the 64 MSB of the length. We assume that
// you won't hash 2^64 bits of data... :-)
while(md.curlen < 120)
md.buf[md.curlen++] = 0;
// Store length
store64(md.length, md.buf+120);
sha_compress(md, md.buf);
// Copy output
for(int i = 0; i < 8; i++)
store64(md.state[i], static_cast<unsigned char*>(out)+(8*i));
}
} // namespace SHA512
namespace xlnt { namespace xlnt {
namespace detail { namespace detail {
std::vector<std::uint8_t> sha1(const std::vector<std::uint8_t> &data) std::vector<std::uint8_t> sha1(const std::vector<std::uint8_t> &data)
{ {
auto s = SHA1(); std::array<std::uint32_t, 5> hash;
s.update(std::string(data.begin(), data.end())); sha1_hash(data.data(), data.size(), hash.data());
auto hex = s.final_();
std::vector<std::uint8_t> bytes;
for (unsigned int i = 0; i < hex.length(); i += 2) std::vector<std::uint8_t> result(20, 0);
auto result_iterator = result.begin();
for (auto i : hash)
{ {
std::string byteString = hex.substr(i, 2); auto swapped = _byteswap_ulong(i);
auto byte = static_cast<std::uint8_t>(strtol(byteString.c_str(), NULL, 16)); std::copy(
bytes.push_back(byte); reinterpret_cast<std::uint8_t *>(&swapped),
reinterpret_cast<std::uint8_t *>(&swapped + 1),
&*result_iterator);
result_iterator += 4;
} }
return bytes; return result;
} }
std::vector<std::uint8_t> sha512(const std::vector<std::uint8_t> &data) std::vector<std::uint8_t> sha512(const std::vector<std::uint8_t> &data)
{ {
sha512_state md; std::array<std::uint64_t, 8> hash;
sha_init(md); sha512_hash(data.data(), data.size(), hash.data());
sha_process(md, data.data(), static_cast<std::uint32_t>(data.size()));
std::vector<std::uint8_t> result(512 / 8, 0); std::vector<std::uint8_t> result(64, 0);
sha_done(md, result.data()); auto result_iterator = result.begin();
for (auto i : hash)
{
auto swapped = _byteswap_uint64(i);
std::copy(
reinterpret_cast<std::uint8_t *>(&swapped),
reinterpret_cast<std::uint8_t *>(&swapped + 1),
&*result_iterator);
result_iterator += 8;
}
return result; return result;
} }

View File

@ -0,0 +1,178 @@
/*
* SHA-1 hash in C
*
* Copyright (c) 2014 Project Nayuki
* https://www.nayuki.io/page/fast-sha1-hash-implementation-in-x86-assembly
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#include <stdint.h>
void sha1_compress(uint32_t state[5], const uint8_t block[64]) {
#define ROTL32(x, n) (((0U + (x)) << (n)) | ((x) >> (32 - (n)))) // Assumes that x is uint32_t and 0 < n < 32
#define LOADSCHEDULE(i) \
schedule[i] = (uint32_t)block[i * 4 + 0] << 24 \
| (uint32_t)block[i * 4 + 1] << 16 \
| (uint32_t)block[i * 4 + 2] << 8 \
| (uint32_t)block[i * 4 + 3] << 0;
#define SCHEDULE(i) \
temp = schedule[(i - 3) & 0xF] ^ schedule[(i - 8) & 0xF] ^ schedule[(i - 14) & 0xF] ^ schedule[(i - 16) & 0xF]; \
schedule[i & 0xF] = ROTL32(temp, 1);
#define ROUND0a(a, b, c, d, e, i) LOADSCHEDULE(i) ROUNDTAIL(a, b, e, ((b & c) | (~b & d)) , i, 0x5A827999)
#define ROUND0b(a, b, c, d, e, i) SCHEDULE(i) ROUNDTAIL(a, b, e, ((b & c) | (~b & d)) , i, 0x5A827999)
#define ROUND1(a, b, c, d, e, i) SCHEDULE(i) ROUNDTAIL(a, b, e, (b ^ c ^ d) , i, 0x6ED9EBA1)
#define ROUND2(a, b, c, d, e, i) SCHEDULE(i) ROUNDTAIL(a, b, e, ((b & c) ^ (b & d) ^ (c & d)), i, 0x8F1BBCDC)
#define ROUND3(a, b, c, d, e, i) SCHEDULE(i) ROUNDTAIL(a, b, e, (b ^ c ^ d) , i, 0xCA62C1D6)
#define ROUNDTAIL(a, b, e, f, i, k) \
e = 0U + e + ROTL32(a, 5) + f + UINT32_C(k) + schedule[i & 0xF]; \
b = ROTL32(b, 30);
uint32_t a = state[0];
uint32_t b = state[1];
uint32_t c = state[2];
uint32_t d = state[3];
uint32_t e = state[4];
uint32_t schedule[16];
uint32_t temp;
ROUND0a(a, b, c, d, e, 0)
ROUND0a(e, a, b, c, d, 1)
ROUND0a(d, e, a, b, c, 2)
ROUND0a(c, d, e, a, b, 3)
ROUND0a(b, c, d, e, a, 4)
ROUND0a(a, b, c, d, e, 5)
ROUND0a(e, a, b, c, d, 6)
ROUND0a(d, e, a, b, c, 7)
ROUND0a(c, d, e, a, b, 8)
ROUND0a(b, c, d, e, a, 9)
ROUND0a(a, b, c, d, e, 10)
ROUND0a(e, a, b, c, d, 11)
ROUND0a(d, e, a, b, c, 12)
ROUND0a(c, d, e, a, b, 13)
ROUND0a(b, c, d, e, a, 14)
ROUND0a(a, b, c, d, e, 15)
ROUND0b(e, a, b, c, d, 16)
ROUND0b(d, e, a, b, c, 17)
ROUND0b(c, d, e, a, b, 18)
ROUND0b(b, c, d, e, a, 19)
ROUND1(a, b, c, d, e, 20)
ROUND1(e, a, b, c, d, 21)
ROUND1(d, e, a, b, c, 22)
ROUND1(c, d, e, a, b, 23)
ROUND1(b, c, d, e, a, 24)
ROUND1(a, b, c, d, e, 25)
ROUND1(e, a, b, c, d, 26)
ROUND1(d, e, a, b, c, 27)
ROUND1(c, d, e, a, b, 28)
ROUND1(b, c, d, e, a, 29)
ROUND1(a, b, c, d, e, 30)
ROUND1(e, a, b, c, d, 31)
ROUND1(d, e, a, b, c, 32)
ROUND1(c, d, e, a, b, 33)
ROUND1(b, c, d, e, a, 34)
ROUND1(a, b, c, d, e, 35)
ROUND1(e, a, b, c, d, 36)
ROUND1(d, e, a, b, c, 37)
ROUND1(c, d, e, a, b, 38)
ROUND1(b, c, d, e, a, 39)
ROUND2(a, b, c, d, e, 40)
ROUND2(e, a, b, c, d, 41)
ROUND2(d, e, a, b, c, 42)
ROUND2(c, d, e, a, b, 43)
ROUND2(b, c, d, e, a, 44)
ROUND2(a, b, c, d, e, 45)
ROUND2(e, a, b, c, d, 46)
ROUND2(d, e, a, b, c, 47)
ROUND2(c, d, e, a, b, 48)
ROUND2(b, c, d, e, a, 49)
ROUND2(a, b, c, d, e, 50)
ROUND2(e, a, b, c, d, 51)
ROUND2(d, e, a, b, c, 52)
ROUND2(c, d, e, a, b, 53)
ROUND2(b, c, d, e, a, 54)
ROUND2(a, b, c, d, e, 55)
ROUND2(e, a, b, c, d, 56)
ROUND2(d, e, a, b, c, 57)
ROUND2(c, d, e, a, b, 58)
ROUND2(b, c, d, e, a, 59)
ROUND3(a, b, c, d, e, 60)
ROUND3(e, a, b, c, d, 61)
ROUND3(d, e, a, b, c, 62)
ROUND3(c, d, e, a, b, 63)
ROUND3(b, c, d, e, a, 64)
ROUND3(a, b, c, d, e, 65)
ROUND3(e, a, b, c, d, 66)
ROUND3(d, e, a, b, c, 67)
ROUND3(c, d, e, a, b, 68)
ROUND3(b, c, d, e, a, 69)
ROUND3(a, b, c, d, e, 70)
ROUND3(e, a, b, c, d, 71)
ROUND3(d, e, a, b, c, 72)
ROUND3(c, d, e, a, b, 73)
ROUND3(b, c, d, e, a, 74)
ROUND3(a, b, c, d, e, 75)
ROUND3(e, a, b, c, d, 76)
ROUND3(d, e, a, b, c, 77)
ROUND3(c, d, e, a, b, 78)
ROUND3(b, c, d, e, a, 79)
state[0] = 0U + state[0] + a;
state[1] = 0U + state[1] + b;
state[2] = 0U + state[2] + c;
state[3] = 0U + state[3] + d;
state[4] = 0U + state[4] + e;
}
void sha1_hash(const uint8_t *message, size_t len, uint32_t hash[5]) {
hash[0] = UINT32_C(0x67452301);
hash[1] = UINT32_C(0xEFCDAB89);
hash[2] = UINT32_C(0x98BADCFE);
hash[3] = UINT32_C(0x10325476);
hash[4] = UINT32_C(0xC3D2E1F0);
#define BLOCK_SIZE 64 // In bytes
#define LENGTH_SIZE 8 // In bytes
size_t off;
for (off = 0; len - off >= BLOCK_SIZE; off += BLOCK_SIZE)
sha1_compress(hash, &message[off]);
uint8_t block[BLOCK_SIZE] = { 0 };
size_t rem = len - off;
memcpy(block, &message[off], rem);
block[rem] = 0x80;
rem++;
if (BLOCK_SIZE - rem < LENGTH_SIZE) {
sha1_compress(hash, block);
memset(block, 0, sizeof(block));
}
block[BLOCK_SIZE - 1] = (uint8_t)((len & 0x1FU) << 3);
len >>= 5;
for (int i = 1; i < LENGTH_SIZE; i++, len >>= 8)
block[BLOCK_SIZE - 1 - i] = (uint8_t)(len & 0xFFU);
sha1_compress(hash, block);
}

View File

@ -0,0 +1,265 @@
/*
* SHA-512 hash in C
*
* Copyright (c) 2016 Project Nayuki
* https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#include <stdint.h>
void sha512_compress(uint64_t state[8], const uint8_t block[128]) {
#define ROTR64(x, n) (((0U + (x)) << (64 - (n))) | ((x) >> (n))) // Assumes that x is uint64_t and 0 < n < 64
#define LOADSCHEDULE(i) \
schedule[i] = (uint64_t)block[i * 8 + 0] << 56 \
| (uint64_t)block[i * 8 + 1] << 48 \
| (uint64_t)block[i * 8 + 2] << 40 \
| (uint64_t)block[i * 8 + 3] << 32 \
| (uint64_t)block[i * 8 + 4] << 24 \
| (uint64_t)block[i * 8 + 5] << 16 \
| (uint64_t)block[i * 8 + 6] << 8 \
| (uint64_t)block[i * 8 + 7] << 0;
#define SCHEDULE(i) \
schedule[i] = 0U + schedule[i - 16] + schedule[i - 7] \
+ (ROTR64(schedule[i - 15], 1) ^ ROTR64(schedule[i - 15], 8) ^ (schedule[i - 15] >> 7)) \
+ (ROTR64(schedule[i - 2], 19) ^ ROTR64(schedule[i - 2], 61) ^ (schedule[i - 2] >> 6));
#define ROUND(a, b, c, d, e, f, g, h, i, k) \
h = 0U + h + (ROTR64(e, 14) ^ ROTR64(e, 18) ^ ROTR64(e, 41)) + (g ^ (e & (f ^ g))) + UINT64_C(k) + schedule[i]; \
d = 0U + d + h; \
h = 0U + h + (ROTR64(a, 28) ^ ROTR64(a, 34) ^ ROTR64(a, 39)) + ((a & (b | c)) | (b & c));
uint64_t schedule[80];
LOADSCHEDULE( 0)
LOADSCHEDULE( 1)
LOADSCHEDULE( 2)
LOADSCHEDULE( 3)
LOADSCHEDULE( 4)
LOADSCHEDULE( 5)
LOADSCHEDULE( 6)
LOADSCHEDULE( 7)
LOADSCHEDULE( 8)
LOADSCHEDULE( 9)
LOADSCHEDULE(10)
LOADSCHEDULE(11)
LOADSCHEDULE(12)
LOADSCHEDULE(13)
LOADSCHEDULE(14)
LOADSCHEDULE(15)
SCHEDULE(16)
SCHEDULE(17)
SCHEDULE(18)
SCHEDULE(19)
SCHEDULE(20)
SCHEDULE(21)
SCHEDULE(22)
SCHEDULE(23)
SCHEDULE(24)
SCHEDULE(25)
SCHEDULE(26)
SCHEDULE(27)
SCHEDULE(28)
SCHEDULE(29)
SCHEDULE(30)
SCHEDULE(31)
SCHEDULE(32)
SCHEDULE(33)
SCHEDULE(34)
SCHEDULE(35)
SCHEDULE(36)
SCHEDULE(37)
SCHEDULE(38)
SCHEDULE(39)
SCHEDULE(40)
SCHEDULE(41)
SCHEDULE(42)
SCHEDULE(43)
SCHEDULE(44)
SCHEDULE(45)
SCHEDULE(46)
SCHEDULE(47)
SCHEDULE(48)
SCHEDULE(49)
SCHEDULE(50)
SCHEDULE(51)
SCHEDULE(52)
SCHEDULE(53)
SCHEDULE(54)
SCHEDULE(55)
SCHEDULE(56)
SCHEDULE(57)
SCHEDULE(58)
SCHEDULE(59)
SCHEDULE(60)
SCHEDULE(61)
SCHEDULE(62)
SCHEDULE(63)
SCHEDULE(64)
SCHEDULE(65)
SCHEDULE(66)
SCHEDULE(67)
SCHEDULE(68)
SCHEDULE(69)
SCHEDULE(70)
SCHEDULE(71)
SCHEDULE(72)
SCHEDULE(73)
SCHEDULE(74)
SCHEDULE(75)
SCHEDULE(76)
SCHEDULE(77)
SCHEDULE(78)
SCHEDULE(79)
uint64_t a = state[0];
uint64_t b = state[1];
uint64_t c = state[2];
uint64_t d = state[3];
uint64_t e = state[4];
uint64_t f = state[5];
uint64_t g = state[6];
uint64_t h = state[7];
ROUND(a, b, c, d, e, f, g, h, 0, 0x428A2F98D728AE22)
ROUND(h, a, b, c, d, e, f, g, 1, 0x7137449123EF65CD)
ROUND(g, h, a, b, c, d, e, f, 2, 0xB5C0FBCFEC4D3B2F)
ROUND(f, g, h, a, b, c, d, e, 3, 0xE9B5DBA58189DBBC)
ROUND(e, f, g, h, a, b, c, d, 4, 0x3956C25BF348B538)
ROUND(d, e, f, g, h, a, b, c, 5, 0x59F111F1B605D019)
ROUND(c, d, e, f, g, h, a, b, 6, 0x923F82A4AF194F9B)
ROUND(b, c, d, e, f, g, h, a, 7, 0xAB1C5ED5DA6D8118)
ROUND(a, b, c, d, e, f, g, h, 8, 0xD807AA98A3030242)
ROUND(h, a, b, c, d, e, f, g, 9, 0x12835B0145706FBE)
ROUND(g, h, a, b, c, d, e, f, 10, 0x243185BE4EE4B28C)
ROUND(f, g, h, a, b, c, d, e, 11, 0x550C7DC3D5FFB4E2)
ROUND(e, f, g, h, a, b, c, d, 12, 0x72BE5D74F27B896F)
ROUND(d, e, f, g, h, a, b, c, 13, 0x80DEB1FE3B1696B1)
ROUND(c, d, e, f, g, h, a, b, 14, 0x9BDC06A725C71235)
ROUND(b, c, d, e, f, g, h, a, 15, 0xC19BF174CF692694)
ROUND(a, b, c, d, e, f, g, h, 16, 0xE49B69C19EF14AD2)
ROUND(h, a, b, c, d, e, f, g, 17, 0xEFBE4786384F25E3)
ROUND(g, h, a, b, c, d, e, f, 18, 0x0FC19DC68B8CD5B5)
ROUND(f, g, h, a, b, c, d, e, 19, 0x240CA1CC77AC9C65)
ROUND(e, f, g, h, a, b, c, d, 20, 0x2DE92C6F592B0275)
ROUND(d, e, f, g, h, a, b, c, 21, 0x4A7484AA6EA6E483)
ROUND(c, d, e, f, g, h, a, b, 22, 0x5CB0A9DCBD41FBD4)
ROUND(b, c, d, e, f, g, h, a, 23, 0x76F988DA831153B5)
ROUND(a, b, c, d, e, f, g, h, 24, 0x983E5152EE66DFAB)
ROUND(h, a, b, c, d, e, f, g, 25, 0xA831C66D2DB43210)
ROUND(g, h, a, b, c, d, e, f, 26, 0xB00327C898FB213F)
ROUND(f, g, h, a, b, c, d, e, 27, 0xBF597FC7BEEF0EE4)
ROUND(e, f, g, h, a, b, c, d, 28, 0xC6E00BF33DA88FC2)
ROUND(d, e, f, g, h, a, b, c, 29, 0xD5A79147930AA725)
ROUND(c, d, e, f, g, h, a, b, 30, 0x06CA6351E003826F)
ROUND(b, c, d, e, f, g, h, a, 31, 0x142929670A0E6E70)
ROUND(a, b, c, d, e, f, g, h, 32, 0x27B70A8546D22FFC)
ROUND(h, a, b, c, d, e, f, g, 33, 0x2E1B21385C26C926)
ROUND(g, h, a, b, c, d, e, f, 34, 0x4D2C6DFC5AC42AED)
ROUND(f, g, h, a, b, c, d, e, 35, 0x53380D139D95B3DF)
ROUND(e, f, g, h, a, b, c, d, 36, 0x650A73548BAF63DE)
ROUND(d, e, f, g, h, a, b, c, 37, 0x766A0ABB3C77B2A8)
ROUND(c, d, e, f, g, h, a, b, 38, 0x81C2C92E47EDAEE6)
ROUND(b, c, d, e, f, g, h, a, 39, 0x92722C851482353B)
ROUND(a, b, c, d, e, f, g, h, 40, 0xA2BFE8A14CF10364)
ROUND(h, a, b, c, d, e, f, g, 41, 0xA81A664BBC423001)
ROUND(g, h, a, b, c, d, e, f, 42, 0xC24B8B70D0F89791)
ROUND(f, g, h, a, b, c, d, e, 43, 0xC76C51A30654BE30)
ROUND(e, f, g, h, a, b, c, d, 44, 0xD192E819D6EF5218)
ROUND(d, e, f, g, h, a, b, c, 45, 0xD69906245565A910)
ROUND(c, d, e, f, g, h, a, b, 46, 0xF40E35855771202A)
ROUND(b, c, d, e, f, g, h, a, 47, 0x106AA07032BBD1B8)
ROUND(a, b, c, d, e, f, g, h, 48, 0x19A4C116B8D2D0C8)
ROUND(h, a, b, c, d, e, f, g, 49, 0x1E376C085141AB53)
ROUND(g, h, a, b, c, d, e, f, 50, 0x2748774CDF8EEB99)
ROUND(f, g, h, a, b, c, d, e, 51, 0x34B0BCB5E19B48A8)
ROUND(e, f, g, h, a, b, c, d, 52, 0x391C0CB3C5C95A63)
ROUND(d, e, f, g, h, a, b, c, 53, 0x4ED8AA4AE3418ACB)
ROUND(c, d, e, f, g, h, a, b, 54, 0x5B9CCA4F7763E373)
ROUND(b, c, d, e, f, g, h, a, 55, 0x682E6FF3D6B2B8A3)
ROUND(a, b, c, d, e, f, g, h, 56, 0x748F82EE5DEFB2FC)
ROUND(h, a, b, c, d, e, f, g, 57, 0x78A5636F43172F60)
ROUND(g, h, a, b, c, d, e, f, 58, 0x84C87814A1F0AB72)
ROUND(f, g, h, a, b, c, d, e, 59, 0x8CC702081A6439EC)
ROUND(e, f, g, h, a, b, c, d, 60, 0x90BEFFFA23631E28)
ROUND(d, e, f, g, h, a, b, c, 61, 0xA4506CEBDE82BDE9)
ROUND(c, d, e, f, g, h, a, b, 62, 0xBEF9A3F7B2C67915)
ROUND(b, c, d, e, f, g, h, a, 63, 0xC67178F2E372532B)
ROUND(a, b, c, d, e, f, g, h, 64, 0xCA273ECEEA26619C)
ROUND(h, a, b, c, d, e, f, g, 65, 0xD186B8C721C0C207)
ROUND(g, h, a, b, c, d, e, f, 66, 0xEADA7DD6CDE0EB1E)
ROUND(f, g, h, a, b, c, d, e, 67, 0xF57D4F7FEE6ED178)
ROUND(e, f, g, h, a, b, c, d, 68, 0x06F067AA72176FBA)
ROUND(d, e, f, g, h, a, b, c, 69, 0x0A637DC5A2C898A6)
ROUND(c, d, e, f, g, h, a, b, 70, 0x113F9804BEF90DAE)
ROUND(b, c, d, e, f, g, h, a, 71, 0x1B710B35131C471B)
ROUND(a, b, c, d, e, f, g, h, 72, 0x28DB77F523047D84)
ROUND(h, a, b, c, d, e, f, g, 73, 0x32CAAB7B40C72493)
ROUND(g, h, a, b, c, d, e, f, 74, 0x3C9EBE0A15C9BEBC)
ROUND(f, g, h, a, b, c, d, e, 75, 0x431D67C49C100D4C)
ROUND(e, f, g, h, a, b, c, d, 76, 0x4CC5D4BECB3E42B6)
ROUND(d, e, f, g, h, a, b, c, 77, 0x597F299CFC657E2A)
ROUND(c, d, e, f, g, h, a, b, 78, 0x5FCB6FAB3AD6FAEC)
ROUND(b, c, d, e, f, g, h, a, 79, 0x6C44198C4A475817)
state[0] = 0U + state[0] + a;
state[1] = 0U + state[1] + b;
state[2] = 0U + state[2] + c;
state[3] = 0U + state[3] + d;
state[4] = 0U + state[4] + e;
state[5] = 0U + state[5] + f;
state[6] = 0U + state[6] + g;
state[7] = 0U + state[7] + h;
}
void sha512_hash(const uint8_t *message, size_t len, uint64_t hash[8]) {
hash[0] = UINT64_C(0x6A09E667F3BCC908);
hash[1] = UINT64_C(0xBB67AE8584CAA73B);
hash[2] = UINT64_C(0x3C6EF372FE94F82B);
hash[3] = UINT64_C(0xA54FF53A5F1D36F1);
hash[4] = UINT64_C(0x510E527FADE682D1);
hash[5] = UINT64_C(0x9B05688C2B3E6C1F);
hash[6] = UINT64_C(0x1F83D9ABFB41BD6B);
hash[7] = UINT64_C(0x5BE0CD19137E2179);
#define BLOCK_SIZE 128 // In bytes
#define LENGTH_SIZE 16 // In bytes
size_t off;
for (off = 0; len - off >= BLOCK_SIZE; off += BLOCK_SIZE)
sha512_compress(hash, &message[off]);
uint8_t block[BLOCK_SIZE] = { 0 };
size_t rem = len - off;
memcpy(block, &message[off], rem);
block[rem] = 0x80;
rem++;
if (BLOCK_SIZE - rem < LENGTH_SIZE) {
sha512_compress(hash, block);
memset(block, 0, sizeof(block));
}
block[BLOCK_SIZE - 1] = (uint8_t)((len & 0x1FU) << 3);
len >>= 5;
for (int i = 1; i < LENGTH_SIZE; i++, len >>= 8)
block[BLOCK_SIZE - 1 - i] = (uint8_t)(len & 0xFFU);
sha512_compress(hash, block);
}