Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/core/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME crypto
PRIVATE_HEADERS sha256.h sha1.h fnv128.h uuid.h crc32.h base64.h
SOURCES crypto_sha256.cc crypto_sha1.cc crypto_fnv128.cc
crypto_uuid.cc crypto_crc32.cc crypto_base64.cc)
PRIVATE_HEADERS sha256.h sha384.h sha512.h sha1.h fnv128.h uuid.h crc32.h
base64.h
SOURCES crypto_sha256.cc crypto_sha384.cc crypto_sha512.cc crypto_sha2_64.h
crypto_sha1.cc crypto_fnv128.cc crypto_uuid.cc crypto_crc32.cc
crypto_base64.cc)

if(SOURCEMETA_CORE_CRYPTO_USE_SYSTEM_OPENSSL)
target_compile_definitions(sourcemeta_core_crypto
Expand Down
2 changes: 1 addition & 1 deletion src/core/crypto/crypto_sha1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cstdint> // std::uint32_t, std::uint64_t

#if defined(SOURCEMETA_CORE_CRYPTO_USE_SYSTEM_OPENSSL)
#include <openssl/evp.h> // EVP_MD_CTX_new, EVP_DigestInit_ex, EVP_sha1, EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_MD_CTX_free
#include <openssl/evp.h> // EVP_*
#include <stdexcept> // std::runtime_error
#elif defined(__APPLE__)
#include <CommonCrypto/CommonDigest.h> // CC_SHA1*, CC_LONG
Expand Down
76 changes: 34 additions & 42 deletions src/core/crypto/crypto_sha256.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cstdint> // std::uint32_t, std::uint64_t

#if defined(SOURCEMETA_CORE_CRYPTO_USE_SYSTEM_OPENSSL)
#include <openssl/evp.h> // EVP_MD_CTX_new, EVP_DigestInit_ex, EVP_sha256, EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_MD_CTX_free
#include <openssl/evp.h> // EVP_*
#include <stdexcept> // std::runtime_error
#elif defined(__APPLE__)
#include <CommonCrypto/CommonDigest.h> // CC_SHA256*, CC_LONG
Expand All @@ -22,6 +22,7 @@
#include <limits> // std::numeric_limits
#include <stdexcept> // std::runtime_error
#else
#include <cstddef> // std::size_t
#include <cstring> // std::memcpy
#endif

Expand All @@ -35,7 +36,8 @@ constexpr std::array<char, 17> HEX_DIGITS{{'0', '1', '2', '3', '4', '5', '6',

namespace sourcemeta::core {

auto sha256(const std::string_view input) -> std::string {
auto sha256_digest(const std::string_view input)
-> std::array<std::uint8_t, 32> {
auto *context = EVP_MD_CTX_new();
if (context == nullptr) {
throw std::runtime_error("Could not allocate OpenSSL digest context");
Expand All @@ -47,23 +49,15 @@ auto sha256(const std::string_view input) -> std::string {
throw std::runtime_error("Could not compute SHA-256 digest");
}

std::array<unsigned char, 32> digest{};
std::array<std::uint8_t, 32> digest{};
unsigned int length = 0;
if (EVP_DigestFinal_ex(context, digest.data(), &length) != 1) {
EVP_MD_CTX_free(context);
throw std::runtime_error("Could not finalize SHA-256 digest");
}

EVP_MD_CTX_free(context);

std::string result;
result.reserve(64);
for (std::uint64_t index = 0; index < 32u; ++index) {
result.push_back(HEX_DIGITS[(digest[index] >> 4u) & 0x0fu]);
result.push_back(HEX_DIGITS[digest[index] & 0x0fu]);
}

return result;
return digest;
}

} // namespace sourcemeta::core
Expand All @@ -72,7 +66,8 @@ auto sha256(const std::string_view input) -> std::string {

namespace sourcemeta::core {

auto sha256(const std::string_view input) -> std::string {
auto sha256_digest(const std::string_view input)
-> std::array<std::uint8_t, 32> {
CC_SHA256_CTX context;
CC_SHA256_Init(&context);

Expand All @@ -90,17 +85,9 @@ auto sha256(const std::string_view input) -> std::string {
remaining_size -= chunk_size;
}

std::array<unsigned char, CC_SHA256_DIGEST_LENGTH> digest{};
std::array<std::uint8_t, CC_SHA256_DIGEST_LENGTH> digest{};
CC_SHA256_Final(digest.data(), &context);

std::string result;
result.reserve(64);
for (std::uint64_t index = 0; index < 32u; ++index) {
result.push_back(HEX_DIGITS[(digest[index] >> 4u) & 0x0fu]);
result.push_back(HEX_DIGITS[digest[index] & 0x0fu]);
}

return result;
return digest;
}

} // namespace sourcemeta::core
Expand All @@ -109,7 +96,8 @@ auto sha256(const std::string_view input) -> std::string {

namespace sourcemeta::core {

auto sha256(const std::string_view input) -> std::string {
auto sha256_digest(const std::string_view input)
-> std::array<std::uint8_t, 32> {
BCRYPT_ALG_HANDLE algorithm{nullptr};
if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(
&algorithm, BCRYPT_SHA256_ALGORITHM, nullptr, 0))) {
Expand Down Expand Up @@ -140,7 +128,7 @@ auto sha256(const std::string_view input) -> std::string {
remaining_size -= chunk_size;
}

std::array<unsigned char, 32> digest{};
std::array<std::uint8_t, 32> digest{};
if (success) {
success = BCRYPT_SUCCESS(BCryptFinishHash(
hash, digest.data(), static_cast<ULONG>(digest.size()), 0));
Expand All @@ -152,14 +140,7 @@ auto sha256(const std::string_view input) -> std::string {
throw std::runtime_error("Could not compute the CNG SHA-256 digest");
}

std::string result;
result.reserve(64);
for (std::uint64_t index = 0; index < 32u; ++index) {
result.push_back(HEX_DIGITS[(digest[index] >> 4u) & 0x0fu]);
result.push_back(HEX_DIGITS[digest[index] & 0x0fu]);
}

return result;
return digest;
}

} // namespace sourcemeta::core
Expand Down Expand Up @@ -276,7 +257,8 @@ inline auto sha256_process_block(const unsigned char *block,

namespace sourcemeta::core {

auto sha256(const std::string_view input) -> std::string {
auto sha256_digest(const std::string_view input)
-> std::array<std::uint8_t, 32> {
// Initial hash values: first 32 bits of the fractional parts of the
// square roots of the first 8 primes (FIPS 180-4 Section 5.3.3)
std::array<std::uint32_t, 8> state{};
Expand Down Expand Up @@ -331,17 +313,15 @@ auto sha256(const std::string_view input) -> std::string {
sha256_process_block(final_block.data() + 64u, state);
}

std::string result;
result.reserve(64);
for (std::uint64_t state_index = 0u; state_index < 8u; ++state_index) {
const auto value = state[state_index];
for (std::uint64_t nibble = 0u; nibble < 8u; ++nibble) {
const auto shift = 28u - nibble * 4u;
result.push_back(HEX_DIGITS[(value >> shift) & 0x0fu]);
std::array<std::uint8_t, 32> digest{};
for (std::size_t word_index = 0u; word_index < 8u; ++word_index) {
for (std::size_t byte_index = 0u; byte_index < 4u; ++byte_index) {
digest[(word_index * 4u) + byte_index] = static_cast<std::uint8_t>(
(state[word_index] >> (8u * (3u - byte_index))) & 0xffu);
}
}

return result;
return digest;
}

} // namespace sourcemeta::core
Expand All @@ -350,6 +330,18 @@ auto sha256(const std::string_view input) -> std::string {

namespace sourcemeta::core {

auto sha256(const std::string_view input) -> std::string {
const auto digest{sha256_digest(input)};
std::string result;
result.reserve(64);
for (const auto byte : digest) {
result.push_back(HEX_DIGITS[(byte >> 4u) & 0x0fu]);
result.push_back(HEX_DIGITS[byte & 0x0fu]);
}

return result;
}

auto sha256(const std::string_view input, std::ostream &output) -> void {
const auto result = sha256(input);
output.write(result.data(), static_cast<std::streamsize>(result.size()));
Expand Down
200 changes: 200 additions & 0 deletions src/core/crypto/crypto_sha2_64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#ifndef SOURCEMETA_CORE_CRYPTO_SHA2_64_H_
#define SOURCEMETA_CORE_CRYPTO_SHA2_64_H_

// Shared FIPS 180-4 core for the hash functions built on 64-bit words,
// used only by the fallback implementations

#include <array> // std::array
#include <cstddef> // std::size_t
#include <cstdint> // std::uint8_t, std::uint64_t
#include <cstring> // std::memcpy
#include <string_view> // std::string_view

namespace sourcemeta::core {

// The count must be between 1 and 63, as the complementary shift is
// undefined otherwise
inline constexpr auto sha2_64_rotate_right(std::uint64_t value,
std::uint64_t count) noexcept
-> std::uint64_t {
return (value >> count) | (value << (64u - count));
Comment thread
jviotti marked this conversation as resolved.
}

// FIPS 180-4 Section 4.1.3 logical functions
inline constexpr auto sha2_64_big_sigma_0(std::uint64_t value) noexcept
-> std::uint64_t {
return sha2_64_rotate_right(value, 28u) ^ sha2_64_rotate_right(value, 34u) ^
sha2_64_rotate_right(value, 39u);
}

inline constexpr auto sha2_64_big_sigma_1(std::uint64_t value) noexcept
-> std::uint64_t {
return sha2_64_rotate_right(value, 14u) ^ sha2_64_rotate_right(value, 18u) ^
sha2_64_rotate_right(value, 41u);
}

inline constexpr auto sha2_64_small_sigma_0(std::uint64_t value) noexcept
-> std::uint64_t {
return sha2_64_rotate_right(value, 1u) ^ sha2_64_rotate_right(value, 8u) ^
(value >> 7u);
}

inline constexpr auto sha2_64_small_sigma_1(std::uint64_t value) noexcept
-> std::uint64_t {
return sha2_64_rotate_right(value, 19u) ^ sha2_64_rotate_right(value, 61u) ^
(value >> 6u);
}

// Equivalent to (x & y) ^ (~x & z) but avoids a bitwise NOT
inline constexpr auto sha2_64_choice(std::uint64_t x, std::uint64_t y,
std::uint64_t z) noexcept
-> std::uint64_t {
return z ^ (x & (y ^ z));
}

inline constexpr auto sha2_64_majority(std::uint64_t x, std::uint64_t y,
std::uint64_t z) noexcept
-> std::uint64_t {
return (x & y) ^ (x & z) ^ (y & z);
}

inline auto sha2_64_process_block(const std::uint8_t *block,
std::array<std::uint64_t, 8> &state) noexcept
-> void {
// First 64 bits of the fractional parts of the cube roots
// of the first 80 prime numbers (FIPS 180-4 Section 4.2.3)
static constexpr std::array<std::uint64_t, 80> round_constants = {
{0x428a2f98d728ae22U, 0x7137449123ef65cdU, 0xb5c0fbcfec4d3b2fU,
0xe9b5dba58189dbbcU, 0x3956c25bf348b538U, 0x59f111f1b605d019U,
0x923f82a4af194f9bU, 0xab1c5ed5da6d8118U, 0xd807aa98a3030242U,
0x12835b0145706fbeU, 0x243185be4ee4b28cU, 0x550c7dc3d5ffb4e2U,
0x72be5d74f27b896fU, 0x80deb1fe3b1696b1U, 0x9bdc06a725c71235U,
0xc19bf174cf692694U, 0xe49b69c19ef14ad2U, 0xefbe4786384f25e3U,
0x0fc19dc68b8cd5b5U, 0x240ca1cc77ac9c65U, 0x2de92c6f592b0275U,
0x4a7484aa6ea6e483U, 0x5cb0a9dcbd41fbd4U, 0x76f988da831153b5U,
0x983e5152ee66dfabU, 0xa831c66d2db43210U, 0xb00327c898fb213fU,
0xbf597fc7beef0ee4U, 0xc6e00bf33da88fc2U, 0xd5a79147930aa725U,
0x06ca6351e003826fU, 0x142929670a0e6e70U, 0x27b70a8546d22ffcU,
0x2e1b21385c26c926U, 0x4d2c6dfc5ac42aedU, 0x53380d139d95b3dfU,
0x650a73548baf63deU, 0x766a0abb3c77b2a8U, 0x81c2c92e47edaee6U,
0x92722c851482353bU, 0xa2bfe8a14cf10364U, 0xa81a664bbc423001U,
0xc24b8b70d0f89791U, 0xc76c51a30654be30U, 0xd192e819d6ef5218U,
0xd69906245565a910U, 0xf40e35855771202aU, 0x106aa07032bbd1b8U,
0x19a4c116b8d2d0c8U, 0x1e376c085141ab53U, 0x2748774cdf8eeb99U,
0x34b0bcb5e19b48a8U, 0x391c0cb3c5c95a63U, 0x4ed8aa4ae3418acbU,
0x5b9cca4f7763e373U, 0x682e6ff3d6b2b8a3U, 0x748f82ee5defb2fcU,
0x78a5636f43172f60U, 0x84c87814a1f0ab72U, 0x8cc702081a6439ecU,
0x90befffa23631e28U, 0xa4506cebde82bde9U, 0xbef9a3f7b2c67915U,
0xc67178f2e372532bU, 0xca273eceea26619cU, 0xd186b8c721c0c207U,
0xeada7dd6cde0eb1eU, 0xf57d4f7fee6ed178U, 0x06f067aa72176fbaU,
0x0a637dc5a2c898a6U, 0x113f9804bef90daeU, 0x1b710b35131c471bU,
0x28db77f523047d84U, 0x32caab7b40c72493U, 0x3c9ebe0a15c9bebcU,
0x431d67c49c100d4cU, 0x4cc5d4becb3e42b6U, 0x597f299cfc657e2aU,
0x5fcb6fab3ad6faecU, 0x6c44198c4a475817U}};

// Decode 16 big-endian 64-bit words from the block
std::array<std::uint64_t, 80> schedule;
for (std::uint64_t word_index = 0; word_index < 16u; ++word_index) {
const std::uint64_t byte_index = word_index * 8u;
schedule[word_index] =
(static_cast<std::uint64_t>(block[byte_index]) << 56u) |
(static_cast<std::uint64_t>(block[byte_index + 1u]) << 48u) |
(static_cast<std::uint64_t>(block[byte_index + 2u]) << 40u) |
(static_cast<std::uint64_t>(block[byte_index + 3u]) << 32u) |
(static_cast<std::uint64_t>(block[byte_index + 4u]) << 24u) |
(static_cast<std::uint64_t>(block[byte_index + 5u]) << 16u) |
(static_cast<std::uint64_t>(block[byte_index + 6u]) << 8u) |
static_cast<std::uint64_t>(block[byte_index + 7u]);
}

// Extend the message schedule (FIPS 180-4 Section 6.4.2 step 1)
for (std::uint64_t index = 16u; index < 80u; ++index) {
schedule[index] =
sha2_64_small_sigma_1(schedule[index - 2u]) + schedule[index - 7u] +
sha2_64_small_sigma_0(schedule[index - 15u]) + schedule[index - 16u];
}

auto working = state;

// Compression function (FIPS 180-4 Section 6.4.2 step 3)
for (std::uint64_t round_index = 0u; round_index < 80u; ++round_index) {
const auto temporary_1 =
working[7] + sha2_64_big_sigma_1(working[4]) +
sha2_64_choice(working[4], working[5], working[6]) +
round_constants[round_index] + schedule[round_index];
const auto temporary_2 =
sha2_64_big_sigma_0(working[0]) +
sha2_64_majority(working[0], working[1], working[2]);

working[7] = working[6];
working[6] = working[5];
working[5] = working[4];
working[4] = working[3] + temporary_1;
working[3] = working[2];
working[2] = working[1];
working[1] = working[0];
working[0] = temporary_1 + temporary_2;
}

for (std::uint64_t index = 0u; index < 8u; ++index) {
state[index] += working[index];
}
}

inline auto sha2_64_hash(const std::string_view input,
std::array<std::uint64_t, 8> &state) -> void {
const auto *const input_bytes =
reinterpret_cast<const std::uint8_t *>(input.data());
const std::size_t input_length = input.size();

// Process all full 128-byte blocks directly from the input (streaming)
std::size_t processed_bytes = 0u;
while (input_length - processed_bytes >= 128u) {
sha2_64_process_block(input_bytes + processed_bytes, state);
processed_bytes += 128u;
}

// Prepare the final block(s) (one or two 128-byte blocks)
std::array<std::uint8_t, 256> final_block{};
const std::size_t remaining_bytes = input_length - processed_bytes;
if (remaining_bytes > 0u) {
std::memcpy(final_block.data(), input_bytes + processed_bytes,
remaining_bytes);
}

// Append the 0x80 byte after the message data
final_block[remaining_bytes] = 0x80u;

// Append length in bits as a big-endian 128-bit value at the end of the
// padding (FIPS 180-4 Section 5.1.2). The bit length of any input is at
// most 67 bits wide, so the upper word carries the bits that the 8x
// multiplication would otherwise overflow
const std::uint64_t message_length_bits_high =
static_cast<std::uint64_t>(input_length) >> 61u;
const std::uint64_t message_length_bits_low =
static_cast<std::uint64_t>(input_length) << 3u;

if (remaining_bytes < 112u) {
for (std::uint64_t index = 0u; index < 8u; ++index) {
final_block[112u + index] = static_cast<std::uint8_t>(
(message_length_bits_high >> (8u * (7u - index))) & 0xffu);
final_block[120u + index] = static_cast<std::uint8_t>(
(message_length_bits_low >> (8u * (7u - index))) & 0xffu);
}
sha2_64_process_block(final_block.data(), state);
} else {
for (std::uint64_t index = 0u; index < 8u; ++index) {
final_block[128u + 112u + index] = static_cast<std::uint8_t>(
(message_length_bits_high >> (8u * (7u - index))) & 0xffu);
final_block[128u + 120u + index] = static_cast<std::uint8_t>(
(message_length_bits_low >> (8u * (7u - index))) & 0xffu);
}

sha2_64_process_block(final_block.data(), state);
sha2_64_process_block(final_block.data() + 128u, state);
}
}

} // namespace sourcemeta::core

#endif
Loading
Loading