mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-22 23:48:08 -05:00
Merge branch 'main' of https://github.com/geode-sdk/geode
This commit is contained in:
commit
b0d72b0c79
9 changed files with 428 additions and 762 deletions
|
@ -55,6 +55,7 @@ file(GLOB SOURCES CONFIGURE_DEPENDS
|
||||||
src/ui/internal/info/*.cpp
|
src/ui/internal/info/*.cpp
|
||||||
src/ui/internal/list/*.cpp
|
src/ui/internal/list/*.cpp
|
||||||
src/ui/internal/settings/*.cpp
|
src/ui/internal/settings/*.cpp
|
||||||
|
hash/hash.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
# Obj-c sources
|
# Obj-c sources
|
||||||
|
|
|
@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
|
||||||
|
|
||||||
project(GeodeChecksum VERSION 1.0)
|
project(GeodeChecksum VERSION 1.0)
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} hash.cpp)
|
add_executable(${PROJECT_NAME} main.cpp hash.cpp)
|
||||||
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
|
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME} PUBLIC ghc_filesystem)
|
target_link_libraries(${PROJECT_NAME} PUBLIC ghc_filesystem)
|
||||||
|
|
|
@ -1,11 +1,44 @@
|
||||||
#include <iostream>
|
|
||||||
#include "hash.hpp"
|
#include "hash.hpp"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
// shh, its fine :-)
|
||||||
if (argc < 2 || !ghc::filesystem::exists(argv[1])) {
|
#include "sha3.cpp"
|
||||||
std::cout << "Usage: \"checksum <file>\"\n";
|
|
||||||
return 1;
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <ciso646>
|
||||||
|
#include "picosha2.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
template <class Func>
|
||||||
|
void readBuffered(std::ifstream& stream, Func func) {
|
||||||
|
constexpr size_t BUF_SIZE = 4096;
|
||||||
|
stream.exceptions(std::ios_base::badbit);
|
||||||
|
|
||||||
|
std::vector<uint8_t> buffer(BUF_SIZE);
|
||||||
|
while (true) {
|
||||||
|
stream.read(reinterpret_cast<char*>(buffer.data()), BUF_SIZE);
|
||||||
|
size_t amt = stream ? BUF_SIZE : stream.gcount();
|
||||||
|
func(buffer.data(), amt);
|
||||||
|
if (!stream) break;
|
||||||
}
|
}
|
||||||
std::cout << calculateHash(argv[1]) << std::endl;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string calculateSHA3_256(ghc::filesystem::path const& path) {
|
||||||
|
std::ifstream file(path, std::ios::binary);
|
||||||
|
SHA3 sha;
|
||||||
|
readBuffered(file, [&](const void* data, size_t amt) {
|
||||||
|
sha.add(data, amt);
|
||||||
|
});
|
||||||
|
return sha.getHash();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string calculateSHA256(ghc::filesystem::path const& path) {
|
||||||
|
std::vector<uint8_t> hash(picosha2::k_digest_size);
|
||||||
|
std::ifstream file(path, std::ios::binary);
|
||||||
|
picosha2::hash256(file, hash.begin(), hash.end());
|
||||||
|
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string calculateHash(ghc::filesystem::path const& path) {
|
||||||
|
return calculateSHA3_256(path);
|
||||||
|
}
|
|
@ -1,27 +1,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
|
||||||
#include <ciso646>
|
|
||||||
#include "picosha3.h"
|
|
||||||
#include "picosha2.h"
|
|
||||||
#include <vector>
|
|
||||||
#include <ghc/filesystem.hpp>
|
#include <ghc/filesystem.hpp>
|
||||||
|
|
||||||
static std::string calculateSHA3_256(ghc::filesystem::path const& path) {
|
std::string calculateSHA3_256(ghc::filesystem::path const& path);
|
||||||
std::vector<uint8_t> s(picosha3::bits_to_bytes(256));
|
|
||||||
auto sha3_256 = picosha3::get_sha3_generator<256>();
|
|
||||||
std::ifstream file(path, std::ios::binary);
|
|
||||||
return sha3_256.get_hex_string(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string calculateSHA256(ghc::filesystem::path const& path) {
|
std::string calculateSHA256(ghc::filesystem::path const& path);
|
||||||
std::vector<uint8_t> hash(picosha2::k_digest_size);
|
|
||||||
std::ifstream file(path, std::ios::binary);
|
|
||||||
picosha2::hash256(file, hash.begin(), hash.end());
|
|
||||||
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string calculateHash(ghc::filesystem::path const& path) {
|
std::string calculateHash(ghc::filesystem::path const& path);
|
||||||
return calculateSHA3_256(path);
|
|
||||||
}
|
|
||||||
|
|
11
loader/hash/main.cpp
Normal file
11
loader/hash/main.cpp
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include "hash.hpp"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if (argc < 2 || !ghc::filesystem::exists(argv[1])) {
|
||||||
|
std::cout << "Usage: \"checksum <file>\"\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::cout << calculateHash(argv[1]) << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -1,361 +0,0 @@
|
||||||
#ifndef PICOSHA3_H
|
|
||||||
#define PICOSHA3_H
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cassert>
|
|
||||||
#include <fstream>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
namespace picosha3 {
|
|
||||||
constexpr size_t bits_to_bytes(size_t bits) { return bits / 8; };
|
|
||||||
constexpr static size_t b_bytes = bits_to_bytes(1600);
|
|
||||||
constexpr static uint64_t RC[24] = {
|
|
||||||
0x0000000000000001ull, 0x0000000000008082ull, 0x800000000000808Aull,
|
|
||||||
0x8000000080008000ull, 0x000000000000808Bull, 0x0000000080000001ull,
|
|
||||||
0x8000000080008081ull, 0x8000000000008009ull, 0x000000000000008Aull,
|
|
||||||
0x0000000000000088ull, 0x0000000080008009ull, 0x000000008000000Aull,
|
|
||||||
0x000000008000808Bull, 0x800000000000008Bull, 0x8000000000008089ull,
|
|
||||||
0x8000000000008003ull, 0x8000000000008002ull, 0x8000000000000080ull,
|
|
||||||
0x000000000000800Aull, 0x800000008000000Aull, 0x8000000080008081ull,
|
|
||||||
0x8000000000008080ull, 0x0000000080000001ull, 0x8000000080008008ull};
|
|
||||||
|
|
||||||
using byte_t = uint8_t;
|
|
||||||
using state_t = std::array<std::array<uint64_t, 5>, 5>;
|
|
||||||
|
|
||||||
inline void theta(state_t& A) {
|
|
||||||
uint64_t C[5] = {0, 0, 0, 0, 0};
|
|
||||||
for(size_t x = 0; x < 5; ++x) {
|
|
||||||
C[x] = A[x][0] ^ A[x][1] ^ A[x][2] ^ A[x][3] ^ A[x][4];
|
|
||||||
};
|
|
||||||
uint64_t D[5] = {0, 0, 0, 0, 0};
|
|
||||||
D[0] = C[4] ^ (C[1] << 1 | C[1] >> (64 - 1));
|
|
||||||
D[1] = C[0] ^ (C[2] << 1 | C[2] >> (64 - 1));
|
|
||||||
D[2] = C[1] ^ (C[3] << 1 | C[3] >> (64 - 1));
|
|
||||||
D[3] = C[2] ^ (C[4] << 1 | C[4] >> (64 - 1));
|
|
||||||
D[4] = C[3] ^ (C[0] << 1 | C[0] >> (64 - 1));
|
|
||||||
for(size_t x = 0; x < 5; ++x) {
|
|
||||||
for(size_t y = 0; y < 5; ++y) {
|
|
||||||
A[x][y] ^= D[x];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void rho(state_t& A) {
|
|
||||||
size_t x{1};
|
|
||||||
size_t y{0};
|
|
||||||
for(size_t t = 0; t < 24; ++t) {
|
|
||||||
size_t offset = ((t + 1) * (t + 2) / 2) % 64;
|
|
||||||
A[x][y] = (A[x][y] << offset) | (A[x][y] >> (64 - offset));
|
|
||||||
size_t tmp{y};
|
|
||||||
y = (2 * x + 3 * y) % 5;
|
|
||||||
x = tmp;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void pi(state_t& A) {
|
|
||||||
state_t tmp{A};
|
|
||||||
for(size_t x = 0; x < 5; ++x) {
|
|
||||||
for(size_t y = 0; y < 5; ++y) {
|
|
||||||
A[x][y] = tmp[(x + 3 * y) % 5][x];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void chi(state_t& A) {
|
|
||||||
state_t tmp{A};
|
|
||||||
for(size_t x = 0; x < 5; ++x) {
|
|
||||||
for(size_t y = 0; y < 5; ++y) {
|
|
||||||
A[x][y] =
|
|
||||||
tmp[x][y] ^ (~(tmp[(x + 1) % 5][y]) & tmp[(x + 2) % 5][y]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void iota(state_t& A, size_t round_index) {
|
|
||||||
A[0][0] ^= RC[round_index];
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void keccak_p(state_t& A) {
|
|
||||||
for(size_t round_index = 0; round_index < 24; ++round_index) {
|
|
||||||
theta(A);
|
|
||||||
rho(A);
|
|
||||||
pi(A);
|
|
||||||
chi(A);
|
|
||||||
iota(A, round_index);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
inline void next(size_t& x, size_t& y, size_t& i) {
|
|
||||||
if(++i != 8) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
i = 0;
|
|
||||||
if(++x != 5) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
x = 0;
|
|
||||||
if(++y != 5) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
template <typename InIter>
|
|
||||||
void absorb(InIter first, InIter last, state_t& A) {
|
|
||||||
size_t x = 0;
|
|
||||||
size_t y = 0;
|
|
||||||
size_t i = 0;
|
|
||||||
for(; first != last && y < 5; ++first) {
|
|
||||||
auto tmp = static_cast<uint64_t>(*first);
|
|
||||||
A[x][y] ^= (tmp << (i * 8));
|
|
||||||
next(x, y, i);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename InContainer>
|
|
||||||
void absorb(const InContainer& src, state_t& A) {
|
|
||||||
absorb(src.cbegin(), src.cend(), A);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename OutIter>
|
|
||||||
OutIter squeeze(const state_t& A, OutIter first, OutIter last,
|
|
||||||
size_t rate_bytes) {
|
|
||||||
size_t x = 0;
|
|
||||||
size_t y = 0;
|
|
||||||
size_t i = 0;
|
|
||||||
for(size_t read_bytes = 0;
|
|
||||||
first != last && y < 5 && read_bytes < rate_bytes;
|
|
||||||
++read_bytes, ++first) {
|
|
||||||
auto tmp = static_cast<uint64_t>(A[x][y]);
|
|
||||||
auto p = reinterpret_cast<byte_t*>(&tmp);
|
|
||||||
*first = *(p + i);
|
|
||||||
next(x, y, i);
|
|
||||||
}
|
|
||||||
return first;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename OutContainer>
|
|
||||||
typename OutContainer::iterator
|
|
||||||
squeeze(const state_t& A, OutContainer& dest, size_t rate_bytes) {
|
|
||||||
return squeeze(A, dest.begin(), dest.end(), rate_bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class PaddingType {
|
|
||||||
SHA,
|
|
||||||
SHAKE,
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename InIter>
|
|
||||||
std::string bytes_to_hex_string(InIter first, InIter last) {
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << std::hex;
|
|
||||||
for(; first != last; ++first) {
|
|
||||||
ss << std::setw(2) << std::setfill('0')
|
|
||||||
<< static_cast<uint64_t>(*first);
|
|
||||||
}
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename InContainer>
|
|
||||||
std::string bytes_to_hex_string(const InContainer& src) {
|
|
||||||
return bytes_to_hex_string(src.cbegin(), src.cend());
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t rate_bytes, size_t d_bytes, PaddingType padding_type>
|
|
||||||
class HashGenerator {
|
|
||||||
public:
|
|
||||||
HashGenerator()
|
|
||||||
: buffer_{}, buffer_pos_{buffer_.begin()}, A_{}, hash_{},
|
|
||||||
is_finished_{false} {}
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
clear_state();
|
|
||||||
clear_buffer();
|
|
||||||
is_finished_ = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename InIter>
|
|
||||||
void process(InIter first, InIter last) {
|
|
||||||
static_assert(
|
|
||||||
sizeof(typename std::iterator_traits<InIter>::value_type) == 1,
|
|
||||||
"The size of input iterator value_type must be one byte.");
|
|
||||||
|
|
||||||
for(; first != last; ++first) {
|
|
||||||
*buffer_pos_ = *first;
|
|
||||||
if(++buffer_pos_ == buffer_.end()) {
|
|
||||||
absorb(buffer_, A_);
|
|
||||||
keccak_p(A_);
|
|
||||||
clear_buffer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
void finish() {
|
|
||||||
add_padding();
|
|
||||||
absorb(buffer_, A_);
|
|
||||||
keccak_p(A_);
|
|
||||||
squeeze_();
|
|
||||||
is_finished_ = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename OutIter>
|
|
||||||
void get_hash_bytes(OutIter first, OutIter last) {
|
|
||||||
if(!is_finished_) {
|
|
||||||
throw std::runtime_error("Not finished!");
|
|
||||||
}
|
|
||||||
std::copy(hash_.cbegin(), hash_.cend(), first);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename OutCotainer>
|
|
||||||
void get_hash_bytes(OutCotainer& dest) {
|
|
||||||
get_hash_bytes(dest.begin(), dest.end());
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename InIter, typename OutIter>
|
|
||||||
void operator()(InIter in_first, InIter in_last, OutIter out_first,
|
|
||||||
OutIter out_last) {
|
|
||||||
static_assert(
|
|
||||||
sizeof(typename std::iterator_traits<InIter>::value_type) == 1,
|
|
||||||
"The size of input iterator value_type must be one byte.");
|
|
||||||
static_assert(
|
|
||||||
sizeof(typename std::iterator_traits<OutIter>::value_type) == 1,
|
|
||||||
"The size of output iterator value_type must be one byte.");
|
|
||||||
process(in_first, in_last);
|
|
||||||
finish();
|
|
||||||
std::copy(hash_.cbegin(), hash_.cend(), out_first);
|
|
||||||
clear();
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename InIter, typename OutCotainer>
|
|
||||||
void operator()(InIter in_first, InIter in_last, OutCotainer& dest) {
|
|
||||||
operator()(in_first, in_last, dest.begin(), dest.end());
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename InContainer, typename OutIter>
|
|
||||||
void operator()(const InContainer& src, OutIter out_first,
|
|
||||||
OutIter out_last) {
|
|
||||||
operator()(src.cbegin(), src.cend(), out_first, out_last);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename InContainer, typename OutContainer>
|
|
||||||
void operator()(const InContainer& src, OutContainer& dest) {
|
|
||||||
operator()(src.cbegin(), src.cend(), dest.begin(), dest.end());
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename OutIter>
|
|
||||||
void operator()(std::ifstream& ifs, OutIter out_first,
|
|
||||||
OutIter out_last) {
|
|
||||||
auto in_first = std::istreambuf_iterator<char>(ifs);
|
|
||||||
auto in_last = std::istreambuf_iterator<char>();
|
|
||||||
operator()(in_first, in_last, out_first, out_last);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename OutCotainer>
|
|
||||||
void operator()(std::ifstream& ifs, OutCotainer& dest) {
|
|
||||||
operator()(ifs, dest.begin(), dest.end());
|
|
||||||
};
|
|
||||||
|
|
||||||
std::string get_hex_string() {
|
|
||||||
if(!is_finished_) {
|
|
||||||
throw std::runtime_error("Not finished!");
|
|
||||||
}
|
|
||||||
return bytes_to_hex_string(hash_);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename InIter>
|
|
||||||
std::string get_hex_string(InIter in_first, InIter in_last) {
|
|
||||||
process(in_first, in_last);
|
|
||||||
finish();
|
|
||||||
auto hash = get_hex_string();
|
|
||||||
clear();
|
|
||||||
return hash;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename InContainer>
|
|
||||||
std::string get_hex_string(const InContainer& src) {
|
|
||||||
return get_hex_string(src.cbegin(), src.cend());
|
|
||||||
};
|
|
||||||
|
|
||||||
std::string get_hex_string(std::ifstream& ifs) {
|
|
||||||
auto in_first = std::istreambuf_iterator<char>(ifs);
|
|
||||||
auto in_last = std::istreambuf_iterator<char>();
|
|
||||||
return get_hex_string(in_first, in_last);
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
|
||||||
void clear_buffer() {
|
|
||||||
buffer_.fill(0);
|
|
||||||
buffer_pos_ = buffer_.begin();
|
|
||||||
};
|
|
||||||
|
|
||||||
void clear_state() {
|
|
||||||
for(auto& row : A_) {
|
|
||||||
row.fill(0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
void add_padding() {
|
|
||||||
const auto q =
|
|
||||||
buffer_.size() - std::distance(buffer_pos_, buffer_.begin());
|
|
||||||
|
|
||||||
if(padding_type == PaddingType::SHA) {
|
|
||||||
if(q == 1) {
|
|
||||||
*buffer_pos_ = 0x86;
|
|
||||||
} else {
|
|
||||||
*buffer_pos_ = 0x06;
|
|
||||||
buffer_.back() = 0x80;
|
|
||||||
}
|
|
||||||
} else if(padding_type == PaddingType::SHAKE) {
|
|
||||||
if(q == 1) {
|
|
||||||
*buffer_pos_ = 0x9F;
|
|
||||||
} else {
|
|
||||||
*buffer_pos_ = 0x1F;
|
|
||||||
buffer_.back() = 0x80;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
void squeeze_() {
|
|
||||||
auto first = hash_.begin();
|
|
||||||
auto last = hash_.end();
|
|
||||||
first = squeeze(A_, first, last, rate_bytes);
|
|
||||||
while(first != last) {
|
|
||||||
keccak_p(A_);
|
|
||||||
first = squeeze(A_, first, last, rate_bytes);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::array<byte_t, rate_bytes> buffer_;
|
|
||||||
typename decltype(buffer_)::iterator buffer_pos_;
|
|
||||||
state_t A_;
|
|
||||||
std::array<byte_t, d_bytes> hash_;
|
|
||||||
bool is_finished_;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <size_t d_bits>
|
|
||||||
auto get_sha3_generator() {
|
|
||||||
static_assert(
|
|
||||||
d_bits == 224 or d_bits == 256 or d_bits == 384 or d_bits == 512,
|
|
||||||
"SHA3 only accepts digest message length 224, 256 384 or 512 bits.");
|
|
||||||
constexpr auto d_bytes = bits_to_bytes(d_bits);
|
|
||||||
constexpr auto capacity_bytes = d_bytes * 2;
|
|
||||||
constexpr auto rate_bytes = b_bytes - capacity_bytes;
|
|
||||||
return HashGenerator<rate_bytes, d_bytes, PaddingType::SHA>{};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t strength_bits, size_t d_bits>
|
|
||||||
auto get_shake_generator() {
|
|
||||||
static_assert(strength_bits == 128 or strength_bits == 256,
|
|
||||||
"SHAKE only accepts strength 128 or 256 bits.");
|
|
||||||
constexpr auto strength_bytes = bits_to_bytes(strength_bits);
|
|
||||||
constexpr auto capacity_bytes = strength_bytes * 2;
|
|
||||||
constexpr auto rate_bytes = b_bytes - capacity_bytes;
|
|
||||||
constexpr auto d_bytes = bits_to_bytes(d_bits);
|
|
||||||
return HashGenerator<rate_bytes, d_bytes, PaddingType::SHAKE>{};
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace picosha3
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,373 +0,0 @@
|
||||||
Mozilla Public License Version 2.0
|
|
||||||
==================================
|
|
||||||
|
|
||||||
1. Definitions
|
|
||||||
--------------
|
|
||||||
|
|
||||||
1.1. "Contributor"
|
|
||||||
means each individual or legal entity that creates, contributes to
|
|
||||||
the creation of, or owns Covered Software.
|
|
||||||
|
|
||||||
1.2. "Contributor Version"
|
|
||||||
means the combination of the Contributions of others (if any) used
|
|
||||||
by a Contributor and that particular Contributor's Contribution.
|
|
||||||
|
|
||||||
1.3. "Contribution"
|
|
||||||
means Covered Software of a particular Contributor.
|
|
||||||
|
|
||||||
1.4. "Covered Software"
|
|
||||||
means Source Code Form to which the initial Contributor has attached
|
|
||||||
the notice in Exhibit A, the Executable Form of such Source Code
|
|
||||||
Form, and Modifications of such Source Code Form, in each case
|
|
||||||
including portions thereof.
|
|
||||||
|
|
||||||
1.5. "Incompatible With Secondary Licenses"
|
|
||||||
means
|
|
||||||
|
|
||||||
(a) that the initial Contributor has attached the notice described
|
|
||||||
in Exhibit B to the Covered Software; or
|
|
||||||
|
|
||||||
(b) that the Covered Software was made available under the terms of
|
|
||||||
version 1.1 or earlier of the License, but not also under the
|
|
||||||
terms of a Secondary License.
|
|
||||||
|
|
||||||
1.6. "Executable Form"
|
|
||||||
means any form of the work other than Source Code Form.
|
|
||||||
|
|
||||||
1.7. "Larger Work"
|
|
||||||
means a work that combines Covered Software with other material, in
|
|
||||||
a separate file or files, that is not Covered Software.
|
|
||||||
|
|
||||||
1.8. "License"
|
|
||||||
means this document.
|
|
||||||
|
|
||||||
1.9. "Licensable"
|
|
||||||
means having the right to grant, to the maximum extent possible,
|
|
||||||
whether at the time of the initial grant or subsequently, any and
|
|
||||||
all of the rights conveyed by this License.
|
|
||||||
|
|
||||||
1.10. "Modifications"
|
|
||||||
means any of the following:
|
|
||||||
|
|
||||||
(a) any file in Source Code Form that results from an addition to,
|
|
||||||
deletion from, or modification of the contents of Covered
|
|
||||||
Software; or
|
|
||||||
|
|
||||||
(b) any new file in Source Code Form that contains any Covered
|
|
||||||
Software.
|
|
||||||
|
|
||||||
1.11. "Patent Claims" of a Contributor
|
|
||||||
means any patent claim(s), including without limitation, method,
|
|
||||||
process, and apparatus claims, in any patent Licensable by such
|
|
||||||
Contributor that would be infringed, but for the grant of the
|
|
||||||
License, by the making, using, selling, offering for sale, having
|
|
||||||
made, import, or transfer of either its Contributions or its
|
|
||||||
Contributor Version.
|
|
||||||
|
|
||||||
1.12. "Secondary License"
|
|
||||||
means either the GNU General Public License, Version 2.0, the GNU
|
|
||||||
Lesser General Public License, Version 2.1, the GNU Affero General
|
|
||||||
Public License, Version 3.0, or any later versions of those
|
|
||||||
licenses.
|
|
||||||
|
|
||||||
1.13. "Source Code Form"
|
|
||||||
means the form of the work preferred for making modifications.
|
|
||||||
|
|
||||||
1.14. "You" (or "Your")
|
|
||||||
means an individual or a legal entity exercising rights under this
|
|
||||||
License. For legal entities, "You" includes any entity that
|
|
||||||
controls, is controlled by, or is under common control with You. For
|
|
||||||
purposes of this definition, "control" means (a) the power, direct
|
|
||||||
or indirect, to cause the direction or management of such entity,
|
|
||||||
whether by contract or otherwise, or (b) ownership of more than
|
|
||||||
fifty percent (50%) of the outstanding shares or beneficial
|
|
||||||
ownership of such entity.
|
|
||||||
|
|
||||||
2. License Grants and Conditions
|
|
||||||
--------------------------------
|
|
||||||
|
|
||||||
2.1. Grants
|
|
||||||
|
|
||||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
|
||||||
non-exclusive license:
|
|
||||||
|
|
||||||
(a) under intellectual property rights (other than patent or trademark)
|
|
||||||
Licensable by such Contributor to use, reproduce, make available,
|
|
||||||
modify, display, perform, distribute, and otherwise exploit its
|
|
||||||
Contributions, either on an unmodified basis, with Modifications, or
|
|
||||||
as part of a Larger Work; and
|
|
||||||
|
|
||||||
(b) under Patent Claims of such Contributor to make, use, sell, offer
|
|
||||||
for sale, have made, import, and otherwise transfer either its
|
|
||||||
Contributions or its Contributor Version.
|
|
||||||
|
|
||||||
2.2. Effective Date
|
|
||||||
|
|
||||||
The licenses granted in Section 2.1 with respect to any Contribution
|
|
||||||
become effective for each Contribution on the date the Contributor first
|
|
||||||
distributes such Contribution.
|
|
||||||
|
|
||||||
2.3. Limitations on Grant Scope
|
|
||||||
|
|
||||||
The licenses granted in this Section 2 are the only rights granted under
|
|
||||||
this License. No additional rights or licenses will be implied from the
|
|
||||||
distribution or licensing of Covered Software under this License.
|
|
||||||
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
|
||||||
Contributor:
|
|
||||||
|
|
||||||
(a) for any code that a Contributor has removed from Covered Software;
|
|
||||||
or
|
|
||||||
|
|
||||||
(b) for infringements caused by: (i) Your and any other third party's
|
|
||||||
modifications of Covered Software, or (ii) the combination of its
|
|
||||||
Contributions with other software (except as part of its Contributor
|
|
||||||
Version); or
|
|
||||||
|
|
||||||
(c) under Patent Claims infringed by Covered Software in the absence of
|
|
||||||
its Contributions.
|
|
||||||
|
|
||||||
This License does not grant any rights in the trademarks, service marks,
|
|
||||||
or logos of any Contributor (except as may be necessary to comply with
|
|
||||||
the notice requirements in Section 3.4).
|
|
||||||
|
|
||||||
2.4. Subsequent Licenses
|
|
||||||
|
|
||||||
No Contributor makes additional grants as a result of Your choice to
|
|
||||||
distribute the Covered Software under a subsequent version of this
|
|
||||||
License (see Section 10.2) or under the terms of a Secondary License (if
|
|
||||||
permitted under the terms of Section 3.3).
|
|
||||||
|
|
||||||
2.5. Representation
|
|
||||||
|
|
||||||
Each Contributor represents that the Contributor believes its
|
|
||||||
Contributions are its original creation(s) or it has sufficient rights
|
|
||||||
to grant the rights to its Contributions conveyed by this License.
|
|
||||||
|
|
||||||
2.6. Fair Use
|
|
||||||
|
|
||||||
This License is not intended to limit any rights You have under
|
|
||||||
applicable copyright doctrines of fair use, fair dealing, or other
|
|
||||||
equivalents.
|
|
||||||
|
|
||||||
2.7. Conditions
|
|
||||||
|
|
||||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
|
|
||||||
in Section 2.1.
|
|
||||||
|
|
||||||
3. Responsibilities
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
3.1. Distribution of Source Form
|
|
||||||
|
|
||||||
All distribution of Covered Software in Source Code Form, including any
|
|
||||||
Modifications that You create or to which You contribute, must be under
|
|
||||||
the terms of this License. You must inform recipients that the Source
|
|
||||||
Code Form of the Covered Software is governed by the terms of this
|
|
||||||
License, and how they can obtain a copy of this License. You may not
|
|
||||||
attempt to alter or restrict the recipients' rights in the Source Code
|
|
||||||
Form.
|
|
||||||
|
|
||||||
3.2. Distribution of Executable Form
|
|
||||||
|
|
||||||
If You distribute Covered Software in Executable Form then:
|
|
||||||
|
|
||||||
(a) such Covered Software must also be made available in Source Code
|
|
||||||
Form, as described in Section 3.1, and You must inform recipients of
|
|
||||||
the Executable Form how they can obtain a copy of such Source Code
|
|
||||||
Form by reasonable means in a timely manner, at a charge no more
|
|
||||||
than the cost of distribution to the recipient; and
|
|
||||||
|
|
||||||
(b) You may distribute such Executable Form under the terms of this
|
|
||||||
License, or sublicense it under different terms, provided that the
|
|
||||||
license for the Executable Form does not attempt to limit or alter
|
|
||||||
the recipients' rights in the Source Code Form under this License.
|
|
||||||
|
|
||||||
3.3. Distribution of a Larger Work
|
|
||||||
|
|
||||||
You may create and distribute a Larger Work under terms of Your choice,
|
|
||||||
provided that You also comply with the requirements of this License for
|
|
||||||
the Covered Software. If the Larger Work is a combination of Covered
|
|
||||||
Software with a work governed by one or more Secondary Licenses, and the
|
|
||||||
Covered Software is not Incompatible With Secondary Licenses, this
|
|
||||||
License permits You to additionally distribute such Covered Software
|
|
||||||
under the terms of such Secondary License(s), so that the recipient of
|
|
||||||
the Larger Work may, at their option, further distribute the Covered
|
|
||||||
Software under the terms of either this License or such Secondary
|
|
||||||
License(s).
|
|
||||||
|
|
||||||
3.4. Notices
|
|
||||||
|
|
||||||
You may not remove or alter the substance of any license notices
|
|
||||||
(including copyright notices, patent notices, disclaimers of warranty,
|
|
||||||
or limitations of liability) contained within the Source Code Form of
|
|
||||||
the Covered Software, except that You may alter any license notices to
|
|
||||||
the extent required to remedy known factual inaccuracies.
|
|
||||||
|
|
||||||
3.5. Application of Additional Terms
|
|
||||||
|
|
||||||
You may choose to offer, and to charge a fee for, warranty, support,
|
|
||||||
indemnity or liability obligations to one or more recipients of Covered
|
|
||||||
Software. However, You may do so only on Your own behalf, and not on
|
|
||||||
behalf of any Contributor. You must make it absolutely clear that any
|
|
||||||
such warranty, support, indemnity, or liability obligation is offered by
|
|
||||||
You alone, and You hereby agree to indemnify every Contributor for any
|
|
||||||
liability incurred by such Contributor as a result of warranty, support,
|
|
||||||
indemnity or liability terms You offer. You may include additional
|
|
||||||
disclaimers of warranty and limitations of liability specific to any
|
|
||||||
jurisdiction.
|
|
||||||
|
|
||||||
4. Inability to Comply Due to Statute or Regulation
|
|
||||||
---------------------------------------------------
|
|
||||||
|
|
||||||
If it is impossible for You to comply with any of the terms of this
|
|
||||||
License with respect to some or all of the Covered Software due to
|
|
||||||
statute, judicial order, or regulation then You must: (a) comply with
|
|
||||||
the terms of this License to the maximum extent possible; and (b)
|
|
||||||
describe the limitations and the code they affect. Such description must
|
|
||||||
be placed in a text file included with all distributions of the Covered
|
|
||||||
Software under this License. Except to the extent prohibited by statute
|
|
||||||
or regulation, such description must be sufficiently detailed for a
|
|
||||||
recipient of ordinary skill to be able to understand it.
|
|
||||||
|
|
||||||
5. Termination
|
|
||||||
--------------
|
|
||||||
|
|
||||||
5.1. The rights granted under this License will terminate automatically
|
|
||||||
if You fail to comply with any of its terms. However, if You become
|
|
||||||
compliant, then the rights granted under this License from a particular
|
|
||||||
Contributor are reinstated (a) provisionally, unless and until such
|
|
||||||
Contributor explicitly and finally terminates Your grants, and (b) on an
|
|
||||||
ongoing basis, if such Contributor fails to notify You of the
|
|
||||||
non-compliance by some reasonable means prior to 60 days after You have
|
|
||||||
come back into compliance. Moreover, Your grants from a particular
|
|
||||||
Contributor are reinstated on an ongoing basis if such Contributor
|
|
||||||
notifies You of the non-compliance by some reasonable means, this is the
|
|
||||||
first time You have received notice of non-compliance with this License
|
|
||||||
from such Contributor, and You become compliant prior to 30 days after
|
|
||||||
Your receipt of the notice.
|
|
||||||
|
|
||||||
5.2. If You initiate litigation against any entity by asserting a patent
|
|
||||||
infringement claim (excluding declaratory judgment actions,
|
|
||||||
counter-claims, and cross-claims) alleging that a Contributor Version
|
|
||||||
directly or indirectly infringes any patent, then the rights granted to
|
|
||||||
You by any and all Contributors for the Covered Software under Section
|
|
||||||
2.1 of this License shall terminate.
|
|
||||||
|
|
||||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
|
|
||||||
end user license agreements (excluding distributors and resellers) which
|
|
||||||
have been validly granted by You or Your distributors under this License
|
|
||||||
prior to termination shall survive termination.
|
|
||||||
|
|
||||||
************************************************************************
|
|
||||||
* *
|
|
||||||
* 6. Disclaimer of Warranty *
|
|
||||||
* ------------------------- *
|
|
||||||
* *
|
|
||||||
* Covered Software is provided under this License on an "as is" *
|
|
||||||
* basis, without warranty of any kind, either expressed, implied, or *
|
|
||||||
* statutory, including, without limitation, warranties that the *
|
|
||||||
* Covered Software is free of defects, merchantable, fit for a *
|
|
||||||
* particular purpose or non-infringing. The entire risk as to the *
|
|
||||||
* quality and performance of the Covered Software is with You. *
|
|
||||||
* Should any Covered Software prove defective in any respect, You *
|
|
||||||
* (not any Contributor) assume the cost of any necessary servicing, *
|
|
||||||
* repair, or correction. This disclaimer of warranty constitutes an *
|
|
||||||
* essential part of this License. No use of any Covered Software is *
|
|
||||||
* authorized under this License except under this disclaimer. *
|
|
||||||
* *
|
|
||||||
************************************************************************
|
|
||||||
|
|
||||||
************************************************************************
|
|
||||||
* *
|
|
||||||
* 7. Limitation of Liability *
|
|
||||||
* -------------------------- *
|
|
||||||
* *
|
|
||||||
* Under no circumstances and under no legal theory, whether tort *
|
|
||||||
* (including negligence), contract, or otherwise, shall any *
|
|
||||||
* Contributor, or anyone who distributes Covered Software as *
|
|
||||||
* permitted above, be liable to You for any direct, indirect, *
|
|
||||||
* special, incidental, or consequential damages of any character *
|
|
||||||
* including, without limitation, damages for lost profits, loss of *
|
|
||||||
* goodwill, work stoppage, computer failure or malfunction, or any *
|
|
||||||
* and all other commercial damages or losses, even if such party *
|
|
||||||
* shall have been informed of the possibility of such damages. This *
|
|
||||||
* limitation of liability shall not apply to liability for death or *
|
|
||||||
* personal injury resulting from such party's negligence to the *
|
|
||||||
* extent applicable law prohibits such limitation. Some *
|
|
||||||
* jurisdictions do not allow the exclusion or limitation of *
|
|
||||||
* incidental or consequential damages, so this exclusion and *
|
|
||||||
* limitation may not apply to You. *
|
|
||||||
* *
|
|
||||||
************************************************************************
|
|
||||||
|
|
||||||
8. Litigation
|
|
||||||
-------------
|
|
||||||
|
|
||||||
Any litigation relating to this License may be brought only in the
|
|
||||||
courts of a jurisdiction where the defendant maintains its principal
|
|
||||||
place of business and such litigation shall be governed by laws of that
|
|
||||||
jurisdiction, without reference to its conflict-of-law provisions.
|
|
||||||
Nothing in this Section shall prevent a party's ability to bring
|
|
||||||
cross-claims or counter-claims.
|
|
||||||
|
|
||||||
9. Miscellaneous
|
|
||||||
----------------
|
|
||||||
|
|
||||||
This License represents the complete agreement concerning the subject
|
|
||||||
matter hereof. If any provision of this License is held to be
|
|
||||||
unenforceable, such provision shall be reformed only to the extent
|
|
||||||
necessary to make it enforceable. Any law or regulation which provides
|
|
||||||
that the language of a contract shall be construed against the drafter
|
|
||||||
shall not be used to construe this License against a Contributor.
|
|
||||||
|
|
||||||
10. Versions of the License
|
|
||||||
---------------------------
|
|
||||||
|
|
||||||
10.1. New Versions
|
|
||||||
|
|
||||||
Mozilla Foundation is the license steward. Except as provided in Section
|
|
||||||
10.3, no one other than the license steward has the right to modify or
|
|
||||||
publish new versions of this License. Each version will be given a
|
|
||||||
distinguishing version number.
|
|
||||||
|
|
||||||
10.2. Effect of New Versions
|
|
||||||
|
|
||||||
You may distribute the Covered Software under the terms of the version
|
|
||||||
of the License under which You originally received the Covered Software,
|
|
||||||
or under the terms of any subsequent version published by the license
|
|
||||||
steward.
|
|
||||||
|
|
||||||
10.3. Modified Versions
|
|
||||||
|
|
||||||
If you create software not governed by this License, and you want to
|
|
||||||
create a new license for such software, you may create and use a
|
|
||||||
modified version of this License if you rename the license and remove
|
|
||||||
any references to the name of the license steward (except to note that
|
|
||||||
such modified license differs from this License).
|
|
||||||
|
|
||||||
10.4. Distributing Source Code Form that is Incompatible With Secondary
|
|
||||||
Licenses
|
|
||||||
|
|
||||||
If You choose to distribute Source Code Form that is Incompatible With
|
|
||||||
Secondary Licenses under the terms of this version of the License, the
|
|
||||||
notice described in Exhibit B of this License must be attached.
|
|
||||||
|
|
||||||
Exhibit A - Source Code Form License Notice
|
|
||||||
-------------------------------------------
|
|
||||||
|
|
||||||
This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
||||||
|
|
||||||
If it is not possible or desirable to put the notice in a particular
|
|
||||||
file, then You may include the notice in a location (such as a LICENSE
|
|
||||||
file in a relevant directory) where a recipient would be likely to look
|
|
||||||
for such a notice.
|
|
||||||
|
|
||||||
You may add additional accurate notices of copyright ownership.
|
|
||||||
|
|
||||||
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
|
||||||
---------------------------------------------------------
|
|
||||||
|
|
||||||
This Source Code Form is "Incompatible With Secondary Licenses", as
|
|
||||||
defined by the Mozilla Public License, v. 2.0.
|
|
299
loader/hash/sha3.cpp
Normal file
299
loader/hash/sha3.cpp
Normal file
|
@ -0,0 +1,299 @@
|
||||||
|
// //////////////////////////////////////////////////////////
|
||||||
|
// sha3.cpp
|
||||||
|
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
|
||||||
|
// see http://create.stephan-brumme.com/disclaimer.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "sha3.h"
|
||||||
|
|
||||||
|
#include <bit>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/// same as reset()
|
||||||
|
SHA3::SHA3(Bits bits)
|
||||||
|
: m_blockSize(200 - 2 * (bits / 8)),
|
||||||
|
m_bits(bits)
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// restart
|
||||||
|
void SHA3::reset()
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < StateSize; i++)
|
||||||
|
m_hash[i] = 0;
|
||||||
|
|
||||||
|
m_numBytes = 0;
|
||||||
|
m_bufferSize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// constants and local helper functions
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
const unsigned int Rounds = 24;
|
||||||
|
const uint64_t XorMasks[Rounds] =
|
||||||
|
{
|
||||||
|
0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
|
||||||
|
0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
|
||||||
|
0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
|
||||||
|
0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
|
||||||
|
0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
|
||||||
|
0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
|
||||||
|
0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
|
||||||
|
0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
|
||||||
|
};
|
||||||
|
|
||||||
|
/// rotate left and wrap around to the right
|
||||||
|
inline uint64_t rotateLeft(uint64_t x, uint8_t numBits)
|
||||||
|
{
|
||||||
|
return (x << numBits) | (x >> (64 - numBits));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// convert litte vs big endian
|
||||||
|
inline uint64_t swap(uint64_t x)
|
||||||
|
{
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
return __builtin_bswap64(x);
|
||||||
|
#endif
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
return _byteswap_uint64(x);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return (x >> 56) |
|
||||||
|
((x >> 40) & 0x000000000000FF00ULL) |
|
||||||
|
((x >> 24) & 0x0000000000FF0000ULL) |
|
||||||
|
((x >> 8) & 0x00000000FF000000ULL) |
|
||||||
|
((x << 8) & 0x000000FF00000000ULL) |
|
||||||
|
((x << 24) & 0x0000FF0000000000ULL) |
|
||||||
|
((x << 40) & 0x00FF000000000000ULL) |
|
||||||
|
(x << 56);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint64_t littleEndian(uint64_t x) {
|
||||||
|
if constexpr (std::endian::native == std::endian::little) {
|
||||||
|
return x;
|
||||||
|
} else {
|
||||||
|
return swap(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// return x % 5 for 0 <= x <= 9
|
||||||
|
unsigned int mod5(unsigned int x)
|
||||||
|
{
|
||||||
|
if (x < 5)
|
||||||
|
return x;
|
||||||
|
|
||||||
|
return x - 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// process a full block
|
||||||
|
void SHA3::processBlock(const void* data)
|
||||||
|
{
|
||||||
|
const uint64_t* data64 = (const uint64_t*) data;
|
||||||
|
// mix data into state
|
||||||
|
for (unsigned int i = 0; i < m_blockSize / 8; i++)
|
||||||
|
m_hash[i] ^= littleEndian(data64[i]);
|
||||||
|
|
||||||
|
// re-compute state
|
||||||
|
for (unsigned int round = 0; round < Rounds; round++)
|
||||||
|
{
|
||||||
|
// Theta
|
||||||
|
uint64_t coefficients[5];
|
||||||
|
for (unsigned int i = 0; i < 5; i++)
|
||||||
|
coefficients[i] = m_hash[i] ^ m_hash[i + 5] ^ m_hash[i + 10] ^ m_hash[i + 15] ^ m_hash[i + 20];
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
uint64_t one = coefficients[mod5(i + 4)] ^ rotateLeft(coefficients[mod5(i + 1)], 1);
|
||||||
|
m_hash[i ] ^= one;
|
||||||
|
m_hash[i + 5] ^= one;
|
||||||
|
m_hash[i + 10] ^= one;
|
||||||
|
m_hash[i + 15] ^= one;
|
||||||
|
m_hash[i + 20] ^= one;
|
||||||
|
}
|
||||||
|
|
||||||
|
// temporary
|
||||||
|
uint64_t one;
|
||||||
|
|
||||||
|
// Rho Pi
|
||||||
|
uint64_t last = m_hash[1];
|
||||||
|
one = m_hash[10]; m_hash[10] = rotateLeft(last, 1); last = one;
|
||||||
|
one = m_hash[ 7]; m_hash[ 7] = rotateLeft(last, 3); last = one;
|
||||||
|
one = m_hash[11]; m_hash[11] = rotateLeft(last, 6); last = one;
|
||||||
|
one = m_hash[17]; m_hash[17] = rotateLeft(last, 10); last = one;
|
||||||
|
one = m_hash[18]; m_hash[18] = rotateLeft(last, 15); last = one;
|
||||||
|
one = m_hash[ 3]; m_hash[ 3] = rotateLeft(last, 21); last = one;
|
||||||
|
one = m_hash[ 5]; m_hash[ 5] = rotateLeft(last, 28); last = one;
|
||||||
|
one = m_hash[16]; m_hash[16] = rotateLeft(last, 36); last = one;
|
||||||
|
one = m_hash[ 8]; m_hash[ 8] = rotateLeft(last, 45); last = one;
|
||||||
|
one = m_hash[21]; m_hash[21] = rotateLeft(last, 55); last = one;
|
||||||
|
one = m_hash[24]; m_hash[24] = rotateLeft(last, 2); last = one;
|
||||||
|
one = m_hash[ 4]; m_hash[ 4] = rotateLeft(last, 14); last = one;
|
||||||
|
one = m_hash[15]; m_hash[15] = rotateLeft(last, 27); last = one;
|
||||||
|
one = m_hash[23]; m_hash[23] = rotateLeft(last, 41); last = one;
|
||||||
|
one = m_hash[19]; m_hash[19] = rotateLeft(last, 56); last = one;
|
||||||
|
one = m_hash[13]; m_hash[13] = rotateLeft(last, 8); last = one;
|
||||||
|
one = m_hash[12]; m_hash[12] = rotateLeft(last, 25); last = one;
|
||||||
|
one = m_hash[ 2]; m_hash[ 2] = rotateLeft(last, 43); last = one;
|
||||||
|
one = m_hash[20]; m_hash[20] = rotateLeft(last, 62); last = one;
|
||||||
|
one = m_hash[14]; m_hash[14] = rotateLeft(last, 18); last = one;
|
||||||
|
one = m_hash[22]; m_hash[22] = rotateLeft(last, 39); last = one;
|
||||||
|
one = m_hash[ 9]; m_hash[ 9] = rotateLeft(last, 61); last = one;
|
||||||
|
one = m_hash[ 6]; m_hash[ 6] = rotateLeft(last, 20); last = one;
|
||||||
|
m_hash[ 1] = rotateLeft(last, 44);
|
||||||
|
|
||||||
|
// Chi
|
||||||
|
for (unsigned int j = 0; j < StateSize; j += 5)
|
||||||
|
{
|
||||||
|
// temporaries
|
||||||
|
uint64_t one = m_hash[j];
|
||||||
|
uint64_t two = m_hash[j + 1];
|
||||||
|
|
||||||
|
m_hash[j] ^= m_hash[j + 2] & ~two;
|
||||||
|
m_hash[j + 1] ^= m_hash[j + 3] & ~m_hash[j + 2];
|
||||||
|
m_hash[j + 2] ^= m_hash[j + 4] & ~m_hash[j + 3];
|
||||||
|
m_hash[j + 3] ^= one & ~m_hash[j + 4];
|
||||||
|
m_hash[j + 4] ^= two & ~one;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iota
|
||||||
|
m_hash[0] ^= XorMasks[round];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// add arbitrary number of bytes
|
||||||
|
void SHA3::add(const void* data, size_t numBytes)
|
||||||
|
{
|
||||||
|
const uint8_t* current = (const uint8_t*) data;
|
||||||
|
|
||||||
|
// copy data to buffer
|
||||||
|
if (m_bufferSize > 0)
|
||||||
|
{
|
||||||
|
while (numBytes > 0 && m_bufferSize < m_blockSize)
|
||||||
|
{
|
||||||
|
m_buffer[m_bufferSize++] = *current++;
|
||||||
|
numBytes--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// full buffer
|
||||||
|
if (m_bufferSize == m_blockSize)
|
||||||
|
{
|
||||||
|
processBlock((void*)m_buffer);
|
||||||
|
m_numBytes += m_blockSize;
|
||||||
|
m_bufferSize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// no more data ?
|
||||||
|
if (numBytes == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// process full blocks
|
||||||
|
while (numBytes >= m_blockSize)
|
||||||
|
{
|
||||||
|
processBlock(current);
|
||||||
|
current += m_blockSize;
|
||||||
|
m_numBytes += m_blockSize;
|
||||||
|
numBytes -= m_blockSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
// keep remaining bytes in buffer
|
||||||
|
while (numBytes > 0)
|
||||||
|
{
|
||||||
|
m_buffer[m_bufferSize++] = *current++;
|
||||||
|
numBytes--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// process everything left in the internal buffer
|
||||||
|
void SHA3::processBuffer()
|
||||||
|
{
|
||||||
|
// add padding
|
||||||
|
size_t offset = m_bufferSize;
|
||||||
|
// add a "1" byte
|
||||||
|
m_buffer[offset++] = 0x06;
|
||||||
|
// fill with zeros
|
||||||
|
while (offset < m_blockSize)
|
||||||
|
m_buffer[offset++] = 0;
|
||||||
|
|
||||||
|
// and add a single set bit
|
||||||
|
m_buffer[offset - 1] |= 0x80;
|
||||||
|
|
||||||
|
processBlock(m_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// return latest hash as 16 hex characters
|
||||||
|
std::string SHA3::getHash()
|
||||||
|
{
|
||||||
|
// save hash state
|
||||||
|
uint64_t oldHash[StateSize];
|
||||||
|
for (unsigned int i = 0; i < StateSize; i++)
|
||||||
|
oldHash[i] = m_hash[i];
|
||||||
|
|
||||||
|
// process remaining bytes
|
||||||
|
processBuffer();
|
||||||
|
|
||||||
|
// convert hash to string
|
||||||
|
static const char dec2hex[16 + 1] = "0123456789abcdef";
|
||||||
|
|
||||||
|
// number of significant elements in hash (uint64_t)
|
||||||
|
unsigned int hashLength = m_bits / 64;
|
||||||
|
|
||||||
|
std::string result;
|
||||||
|
result.reserve(m_bits / 4);
|
||||||
|
for (unsigned int i = 0; i < hashLength; i++)
|
||||||
|
for (unsigned int j = 0; j < 8; j++) // 64 bits => 8 bytes
|
||||||
|
{
|
||||||
|
// convert a byte to hex
|
||||||
|
unsigned char oneByte = (unsigned char) (m_hash[i] >> (8 * j));
|
||||||
|
result += dec2hex[oneByte >> 4];
|
||||||
|
result += dec2hex[oneByte & 15];
|
||||||
|
}
|
||||||
|
|
||||||
|
// SHA3-224's last entry in m_hash provides only 32 bits instead of 64 bits
|
||||||
|
unsigned int remainder = m_bits - hashLength * 64;
|
||||||
|
unsigned int processed = 0;
|
||||||
|
while (processed < remainder)
|
||||||
|
{
|
||||||
|
// convert a byte to hex
|
||||||
|
unsigned char oneByte = (unsigned char) (m_hash[hashLength] >> processed);
|
||||||
|
result += dec2hex[oneByte >> 4];
|
||||||
|
result += dec2hex[oneByte & 15];
|
||||||
|
|
||||||
|
processed += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
// restore state
|
||||||
|
for (unsigned int i = 0; i < StateSize; i++)
|
||||||
|
m_hash[i] = oldHash[i];
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// compute SHA3 of a memory block
|
||||||
|
std::string SHA3::operator()(const void* data, size_t numBytes)
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
add(data, numBytes);
|
||||||
|
return getHash();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// compute SHA3 of a string, excluding final zero
|
||||||
|
std::string SHA3::operator()(const std::string& text)
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
add(text.c_str(), text.size());
|
||||||
|
return getHash();
|
||||||
|
}
|
73
loader/hash/sha3.h
Normal file
73
loader/hash/sha3.h
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
// //////////////////////////////////////////////////////////
|
||||||
|
// sha3.h
|
||||||
|
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
|
||||||
|
// see http://create.stephan-brumme.com/disclaimer.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
//#include "hash.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
/// compute SHA3 hash
|
||||||
|
/** Usage:
|
||||||
|
SHA3 sha3;
|
||||||
|
std::string myHash = sha3("Hello World"); // std::string
|
||||||
|
std::string myHash2 = sha3("How are you", 11); // arbitrary data, 11 bytes
|
||||||
|
|
||||||
|
// or in a streaming fashion:
|
||||||
|
|
||||||
|
SHA3 sha3;
|
||||||
|
while (more data available)
|
||||||
|
sha3.add(pointer to fresh data, number of new bytes);
|
||||||
|
std::string myHash3 = sha3.getHash();
|
||||||
|
*/
|
||||||
|
class SHA3 //: public Hash
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// algorithm variants
|
||||||
|
enum Bits { Bits224 = 224, Bits256 = 256, Bits384 = 384, Bits512 = 512 };
|
||||||
|
|
||||||
|
/// same as reset()
|
||||||
|
explicit SHA3(Bits bits = Bits256);
|
||||||
|
|
||||||
|
/// compute hash of a memory block
|
||||||
|
std::string operator()(const void* data, size_t numBytes);
|
||||||
|
/// compute hash of a string, excluding final zero
|
||||||
|
std::string operator()(const std::string& text);
|
||||||
|
|
||||||
|
/// add arbitrary number of bytes
|
||||||
|
void add(const void* data, size_t numBytes);
|
||||||
|
|
||||||
|
/// return latest hash as hex characters
|
||||||
|
std::string getHash();
|
||||||
|
|
||||||
|
/// restart
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
private:
|
||||||
|
/// process a full block
|
||||||
|
void processBlock(const void* data);
|
||||||
|
/// process everything left in the internal buffer
|
||||||
|
void processBuffer();
|
||||||
|
|
||||||
|
/// 1600 bits, stored as 25x64 bit, BlockSize is no more than 1152 bits (Keccak224)
|
||||||
|
enum { StateSize = 1600 / (8 * 8),
|
||||||
|
MaxBlockSize = 200 - 2 * (224 / 8) };
|
||||||
|
|
||||||
|
/// hash
|
||||||
|
uint64_t m_hash[StateSize];
|
||||||
|
/// size of processed data in bytes
|
||||||
|
uint64_t m_numBytes;
|
||||||
|
/// block size (less or equal to MaxBlockSize)
|
||||||
|
size_t m_blockSize;
|
||||||
|
/// valid bytes in m_buffer
|
||||||
|
size_t m_bufferSize;
|
||||||
|
/// bytes not processed yet
|
||||||
|
uint8_t m_buffer[MaxBlockSize];
|
||||||
|
/// variant
|
||||||
|
Bits m_bits;
|
||||||
|
};
|
Loading…
Reference in a new issue