SHA-3 hash
Loading...
Searching...
No Matches
sha3.hpp
Go to the documentation of this file.
1
7#ifndef SHA_3_IMPL_HPP
8#define SHA_3_IMPL_HPP
9
10#include <cstring>
11#include <utility>
12
13#include "constant.hh"
14
20#define UNROLL(A, F) \
21 [&]<uint64_t... I>(std::index_sequence<I...>) __attribute__((always_inline)) { \
22 ((A), ...); \
23 } (std::make_index_sequence<(F)>());
24
34template <std::size_t m_mode>
35class SHA3 {
36 alignas(32) uint64_t s[b >> 6] = {0};
37 public:
41 static constexpr auto block_size() {
42 return (b - (m_mode << 1)) >> 3;
43 }
44
53 void update(const void* __restrict in, const uint64_t sz) {
54 alignas(32) uint64_t c[5], t[25];
55 for (uint64_t i{}; i < sz; i += block_size()) {
56 UNROLL(s[I] ^= ((const uint64_t* __restrict)&((char*)in)[i])[I], block_size() >> 3);
57 for (uint64_t rnd{}; rnd < nr; rnd++) {
58 UNROLL(c[I] = s[I] ^ s[I + 5] ^ s[I + 10] ^ s[I + 15] ^ s[I + 20], 5);
59 UNROLL(s[I] ^= (c[(I + 1) % 5] << 1 | c[(I + 1) % 5] >> 63) ^ c[(I + 4) % 5], 25);
60 UNROLL(t[I] = s[pi[I]] << rot[pi[I]] | s[pi[I]] >> (64 - rot[pi[I]]), 25);
61 UNROLL(s[I] = (~t[I / 5 * 5 + (I + 1) % 5] & t[I / 5 * 5 + (I + 2) % 5]) ^ t[I], 25);
62 s[0] ^= rc[rnd];
63 }
64 }
65 }
66
75 void finalize(void* __restrict out, const void* __restrict in, const uint64_t sz) {
76 uint64_t resid = sz % block_size();
77 update(in, sz - resid);
78
79 alignas(32) uint8_t buff[block_size()] = {0};
80 buff[block_size() - 1] = 0x80;
81 buff[resid] |= 0x06;
82 memcpy(buff, &((char*)in)[sz - resid], resid);
83 update(buff, block_size());
84 memcpy(out, s, m_mode >> 3);
85 memset(s, 0, sizeof(s));
86 }
87
91 auto operator()(auto&&... args) {
92 return finalize(std::forward<decltype(args)>(args)...);
93 }
94};
95
99template <std::size_t m_mode>
100void sha3(auto&&... args) {
101 SHA3<m_mode>{}(std::forward<decltype(args)>(args)...);
102}
103
104#endif // SHA_3_IMPL_HPP
SHA-3 hash main class.
Definition sha3.hpp:35
auto operator()(auto &&... args)
wrapper for calling a class as a functor
Definition sha3.hpp:91
void finalize(void *__restrict out, const void *__restrict in, const uint64_t sz)
complete the hash calculation with the remaining data
Definition sha3.hpp:75
static constexpr auto block_size()
calculate block size based on selected hash mode
Definition sha3.hpp:41
void update(const void *__restrict in, const uint64_t sz)
update the hash state with intermediate chunk
Definition sha3.hpp:53
Constants for SHA-3 hash functions.
constexpr uint8_t pi[]
pi substitution array
Definition constant.hh:33
constexpr uint64_t rc[]
round constants
Definition constant.hh:16
constexpr uint8_t rot[]
rotate (permutation) array
Definition constant.hh:27
constexpr uint64_t nr
number of transform rounds (= 24 from standard)
Definition constant.hh:35
constexpr uint64_t b
internal register (state) bitwidth
Definition constant.hh:36
void sha3(auto &&... args)
wrapper for calling a SHA-3 class as a function
Definition sha3.hpp:100
#define UNROLL(A, F)
macro for compile-time for unroll
Definition sha3.hpp:20