From 3faecdb353e60e7feb0f45b484ea935a8170750c Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 10 Sep 2017 17:21:19 -0700 Subject: [PATCH] Add SHA1 algorithm implementation. (#32) Largely based on https://github.com/vog/sha1 with some customizations and optimizations. --- src/main/native/cpp/support/sha1.cpp | 330 +++++++++++++++++++++++++ src/main/native/include/support/sha1.h | 55 +++++ src/test/native/cpp/sha1Test.cpp | 93 +++++++ 3 files changed, 478 insertions(+) create mode 100644 src/main/native/cpp/support/sha1.cpp create mode 100644 src/main/native/include/support/sha1.h create mode 100644 src/test/native/cpp/sha1Test.cpp diff --git a/src/main/native/cpp/support/sha1.cpp b/src/main/native/cpp/support/sha1.cpp new file mode 100644 index 0000000000..4154f43fc2 --- /dev/null +++ b/src/main/native/cpp/support/sha1.cpp @@ -0,0 +1,330 @@ +/* + sha1.cpp - source code of + + ============ + SHA-1 in C++ + ============ + + 100% Public Domain. + + Original C Code + -- Steve Reid + Small changes to fit into bglibs + -- Bruce Guenter + Translation to simpler C++ Code + -- Volker Grabsch + Safety fixes + -- Eugene Hopkinson +*/ + +#include "support/sha1.h" + +#include "llvm/SmallVector.h" +#include "llvm/StringExtras.h" +#include "llvm/raw_ostream.h" +#include "support/raw_istream.h" + +using namespace wpi; + +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 reset(uint32_t digest[], size_t &buf_size, uint64_t &transforms) +{ + /* SHA1 initialization constants */ + digest[0] = 0x67452301; + digest[1] = 0xefcdab89; + digest[2] = 0x98badcfe; + digest[3] = 0x10325476; + digest[4] = 0xc3d2e1f0; + + /* Reset counters */ + buf_size = 0; + transforms = 0; +} + + +static uint32_t rol(const uint32_t value, const size_t bits) +{ + return (value << bits) | (value >> (32 - bits)); +} + + +static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i) +{ + return 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 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 + rol(v, 5); + w = rol(w, 30); +} + + +static void 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] = blk(block, i); + z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5); + w = rol(w, 30); +} + + +static void 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] = blk(block, i); + z += (w^x^y) + block[i] + 0x6ed9eba1 + rol(v, 5); + w = rol(w, 30); +} + + +static void 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] = blk(block, i); + z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rol(v, 5); + w = rol(w, 30); +} + + +static void 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] = blk(block, i); + z += (w^x^y) + block[i] + 0xca62c1d6 + rol(v, 5); + w = rol(w, 30); +} + + +/* + * Hash a single 512-bit block. This is the core of the algorithm. + */ + +static void 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. */ + R0(block, a, b, c, d, e, 0); + R0(block, e, a, b, c, d, 1); + R0(block, d, e, a, b, c, 2); + R0(block, c, d, e, a, b, 3); + R0(block, b, c, d, e, a, 4); + R0(block, a, b, c, d, e, 5); + R0(block, e, a, b, c, d, 6); + R0(block, d, e, a, b, c, 7); + R0(block, c, d, e, a, b, 8); + R0(block, b, c, d, e, a, 9); + R0(block, a, b, c, d, e, 10); + R0(block, e, a, b, c, d, 11); + R0(block, d, e, a, b, c, 12); + R0(block, c, d, e, a, b, 13); + R0(block, b, c, d, e, a, 14); + R0(block, a, b, c, d, e, 15); + R1(block, e, a, b, c, d, 0); + R1(block, d, e, a, b, c, 1); + R1(block, c, d, e, a, b, 2); + R1(block, b, c, d, e, a, 3); + R2(block, a, b, c, d, e, 4); + R2(block, e, a, b, c, d, 5); + R2(block, d, e, a, b, c, 6); + R2(block, c, d, e, a, b, 7); + R2(block, b, c, d, e, a, 8); + R2(block, a, b, c, d, e, 9); + R2(block, e, a, b, c, d, 10); + R2(block, d, e, a, b, c, 11); + R2(block, c, d, e, a, b, 12); + R2(block, b, c, d, e, a, 13); + R2(block, a, b, c, d, e, 14); + R2(block, e, a, b, c, d, 15); + R2(block, d, e, a, b, c, 0); + R2(block, c, d, e, a, b, 1); + R2(block, b, c, d, e, a, 2); + R2(block, a, b, c, d, e, 3); + R2(block, e, a, b, c, d, 4); + R2(block, d, e, a, b, c, 5); + R2(block, c, d, e, a, b, 6); + R2(block, b, c, d, e, a, 7); + R3(block, a, b, c, d, e, 8); + R3(block, e, a, b, c, d, 9); + R3(block, d, e, a, b, c, 10); + R3(block, c, d, e, a, b, 11); + R3(block, b, c, d, e, a, 12); + R3(block, a, b, c, d, e, 13); + R3(block, e, a, b, c, d, 14); + R3(block, d, e, a, b, c, 15); + R3(block, c, d, e, a, b, 0); + R3(block, b, c, d, e, a, 1); + R3(block, a, b, c, d, e, 2); + R3(block, e, a, b, c, d, 3); + R3(block, d, e, a, b, c, 4); + R3(block, c, d, e, a, b, 5); + R3(block, b, c, d, e, a, 6); + R3(block, a, b, c, d, e, 7); + R3(block, e, a, b, c, d, 8); + R3(block, d, e, a, b, c, 9); + R3(block, c, d, e, a, b, 10); + R3(block, b, c, d, e, a, 11); + R4(block, a, b, c, d, e, 12); + R4(block, e, a, b, c, d, 13); + R4(block, d, e, a, b, c, 14); + R4(block, c, d, e, a, b, 15); + R4(block, b, c, d, e, a, 0); + R4(block, a, b, c, d, e, 1); + R4(block, e, a, b, c, d, 2); + R4(block, d, e, a, b, c, 3); + R4(block, c, d, e, a, b, 4); + R4(block, b, c, d, e, a, 5); + R4(block, a, b, c, d, e, 6); + R4(block, e, a, b, c, d, 7); + R4(block, d, e, a, b, c, 8); + R4(block, c, d, e, a, b, 9); + R4(block, b, c, d, e, a, 10); + R4(block, a, b, c, d, e, 11); + R4(block, e, a, b, c, d, 12); + R4(block, d, e, a, b, c, 13); + R4(block, c, d, e, a, b, 14); + 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 buffer_to_block(const unsigned char* 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] = (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() +{ + reset(digest, buf_size, transforms); +} + + +void SHA1::Update(llvm::StringRef s) +{ + raw_mem_istream is(s); + Update(is); +} + + +void SHA1::Update(raw_istream &is) +{ + while (true) + { + buf_size += is.readsome(&buffer[buf_size], BLOCK_BYTES - buf_size); + if (buf_size != BLOCK_BYTES) + { + return; + } + uint32_t block[BLOCK_INTS]; + buffer_to_block(buffer, block); + transform(digest, block, transforms); + buf_size = 0; + } +} + + +/* + * Add padding and return the message digest. + */ + +static void finalize(uint32_t digest[], unsigned char* buffer, size_t& buf_size, uint64_t &transforms, llvm::raw_ostream& os) +{ + /* Total number of hashed bits */ + uint64_t total_bits = (transforms*BLOCK_BYTES + buf_size) * 8; + + /* Padding */ + buffer[buf_size++] = 0x80; + for (size_t i = buf_size; i < BLOCK_BYTES; ++i) + { + buffer[i] = 0x00; + } + + uint32_t block[BLOCK_INTS]; + buffer_to_block(buffer, block); + + if (buf_size > BLOCK_BYTES - 8) + { + 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] = total_bits; + block[BLOCK_INTS - 2] = (total_bits >> 32); + transform(digest, block, transforms); + + /* Hex string */ + static const char *const LUT = "0123456789abcdef"; + for (size_t i = 0; i < 5; i++) + { + uint32_t v = digest[i]; + os << LUT[(v >> 28) & 0xf] << LUT[(v >> 24) & 0xf] + << LUT[(v >> 20) & 0xf] << LUT[(v >> 16) & 0xf] + << LUT[(v >> 12) & 0xf] << LUT[(v >> 8) & 0xf] + << LUT[(v >> 4) & 0xf] << LUT[(v >> 0) & 0xf]; + } + + /* Reset for next run */ + reset(digest, buf_size, transforms); +} + + +std::string SHA1::Final() +{ + std::string out; + llvm::raw_string_ostream os(out); + + finalize(digest, buffer, buf_size, transforms, os); + + return os.str(); +} + + +llvm::StringRef SHA1::Final(llvm::SmallVectorImpl& buf) +{ + llvm::raw_svector_ostream os(buf); + + finalize(digest, buffer, buf_size, transforms, os); + + return os.str(); +} + + +std::string SHA1::FromFile(llvm::StringRef filename) +{ + std::error_code ec; + raw_fd_istream stream(filename, ec); + SHA1 checksum; + checksum.Update(stream); + return checksum.Final(); +} diff --git a/src/main/native/include/support/sha1.h b/src/main/native/include/support/sha1.h new file mode 100644 index 0000000000..f156f888f5 --- /dev/null +++ b/src/main/native/include/support/sha1.h @@ -0,0 +1,55 @@ +/* + sha1.hpp - header of + + ============ + SHA-1 in C++ + ============ + + 100% Public Domain. + + Original C Code + -- Steve Reid + Small changes to fit into bglibs + -- Bruce Guenter + Translation to simpler C++ Code + -- Volker Grabsch + Safety fixes + -- Eugene Hopkinson +*/ + +#ifndef SHA1_HPP +#define SHA1_HPP + + +#include +#include +#include "llvm/StringRef.h" + +namespace llvm { +template class SmallVectorImpl; +} + +namespace wpi { + +class raw_istream; + +class SHA1 +{ +public: + SHA1(); + void Update(llvm::StringRef s); + void Update(raw_istream &is); + std::string Final(); + llvm::StringRef Final(llvm::SmallVectorImpl& buf); + static std::string FromFile(llvm::StringRef filename); + +private: + uint32_t digest[5]; + unsigned char buffer[64]; + size_t buf_size; + uint64_t transforms; +}; + +} // namespace wpi + +#endif /* SHA1_HPP */ diff --git a/src/test/native/cpp/sha1Test.cpp b/src/test/native/cpp/sha1Test.cpp new file mode 100644 index 0000000000..5f71fe0a9b --- /dev/null +++ b/src/test/native/cpp/sha1Test.cpp @@ -0,0 +1,93 @@ +/* + test_sha1.cpp - test program of + + ============ + SHA-1 in C++ + ============ + + 100% Public Domain. + + Original C Code + -- Steve Reid + Small changes to fit into bglibs + -- Bruce Guenter + Translation to simpler C++ Code + -- Volker Grabsch +*/ + +#include "support/sha1.h" + +#include "gtest/gtest.h" + +#include + +namespace wpi { + +/* + * The 3 test vectors from FIPS PUB 180-1 + */ + +TEST(SHA1Test, Standard1) +{ + SHA1 checksum; + checksum.Update("abc"); + ASSERT_EQ(checksum.Final(), "a9993e364706816aba3e25717850c26c9cd0d89d"); +} + +TEST(SHA1Test, Standard2) +{ + SHA1 checksum; + checksum.Update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); + ASSERT_EQ(checksum.Final(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1"); +} + +TEST(SHA1Test, Standard3) +{ + SHA1 checksum; + // A million repetitions of 'a' + for (int i = 0; i < 1000000/200; ++i) + { + checksum.Update("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + ); + } + ASSERT_EQ(checksum.Final(), "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); +} + + +/* + * Other tests + */ + +TEST(SHA1Test, OtherNoString) +{ + SHA1 checksum; + ASSERT_EQ(checksum.Final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); +} + +TEST(SHA1Test, OtherEmptyString) +{ + SHA1 checksum; + checksum.Update(""); + ASSERT_EQ(checksum.Final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); +} + +TEST(SHA1Test, OtherABCDE) +{ + SHA1 checksum; + checksum.Update("abcde"); + ASSERT_EQ(checksum.Final(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); +} + +TEST(SHA1Test, Concurrent) +{ + // Two concurrent checksum calculations + SHA1 checksum1, checksum2; + checksum1.Update("abc"); + ASSERT_EQ(checksum2.Final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); /* "" */ + ASSERT_EQ(checksum1.Final(), "a9993e364706816aba3e25717850c26c9cd0d89d"); /* "abc" */ +} + +} // namespace wpi