8#include <botan/internal/sha3.h>
9#include <botan/internal/sha3_round.h>
10#include <botan/internal/loadstor.h>
11#include <botan/internal/cpuid.h>
12#include <botan/internal/fmt.h>
13#include <botan/exceptn.h>
20#if defined(BOTAN_HAS_SHA3_BMI2)
23 return permute_bmi2(A);
27 static const uint64_t RC[24] = {
28 0x0000000000000001, 0x0000000000008082, 0x800000000000808A,
29 0x8000000080008000, 0x000000000000808B, 0x0000000080000001,
30 0x8000000080008081, 0x8000000000008009, 0x000000000000008A,
31 0x0000000000000088, 0x0000000080008009, 0x000000008000000A,
32 0x000000008000808B, 0x800000000000008B, 0x8000000000008089,
33 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
34 0x000000000000800A, 0x800000008000000A, 0x8000000080008081,
35 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
40 for(
size_t i = 0; i != 24; i += 2)
50 const uint8_t input[],
size_t length)
54 size_t to_take = std::min(length, bitrate / 8 - S_pos);
58 while(to_take && S_pos % 8)
60 S[S_pos / 8] ^=
static_cast<uint64_t
>(input[0]) << (8 * (S_pos % 8));
67 while(to_take && to_take % 8 == 0)
77 S[S_pos / 8] ^=
static_cast<uint64_t
>(input[0]) << (8 * (S_pos % 8));
84 if(S_pos == bitrate / 8)
97 uint8_t init_pad, uint8_t fini_pad)
99 BOTAN_ARG_CHECK(bitrate % 64 == 0,
"SHA-3 bitrate must be multiple of 64");
101 S[S_pos / 8] ^=
static_cast<uint64_t
>(init_pad) << (8 * (S_pos % 8));
102 S[(bitrate / 64) - 1] ^=
static_cast<uint64_t
>(fini_pad) << 56;
109 uint8_t output[],
size_t output_length)
111 BOTAN_ARG_CHECK(bitrate % 64 == 0,
"SHA-3 bitrate must be multiple of 64");
113 const size_t byterate = bitrate / 8;
132 m_output_bits(output_bits),
133 m_bitrate(1600 - 2*output_bits),
139 if(output_bits != 224 && output_bits != 256 &&
140 output_bits != 384 && output_bits != 512)
148 return fmt(
"SHA-3({})", m_output_bits);
153#if defined(BOTAN_HAS_SHA3_BMI2)
154 if(CPUID::has_bmi2())
165 return std::make_unique<SHA_3>(*
this);
170 return std::make_unique<SHA_3>(m_output_bits);
179void SHA_3::add_data(
const uint8_t input[],
size_t length)
181 m_S_pos =
SHA_3::absorb(m_bitrate, m_S, m_S_pos, input, length);
184void SHA_3::final_result(uint8_t output[])
#define BOTAN_ARG_CHECK(expr, msg)
static void permute(uint64_t A[25])
SHA_3(size_t output_bits)
std::unique_ptr< HashFunction > new_object() const override
static void finish(size_t bitrate, secure_vector< uint64_t > &S, size_t S_pos, uint8_t init_pad, uint8_t fini_pad)
std::string provider() const override
static size_t absorb(size_t bitrate, secure_vector< uint64_t > &S, size_t S_pos, const uint8_t input[], size_t length)
static void expand(size_t bitrate, secure_vector< uint64_t > &S, uint8_t output[], size_t output_length)
std::string name() const override
size_t output_length() const override
std::unique_ptr< HashFunction > copy_state() const override
void zeroise(std::vector< T, Alloc > &vec)
std::string fmt(std::string_view format, const T &... args)
void copy_out_vec_le(uint8_t out[], size_t out_bytes, const std::vector< T, Alloc > &in)
void SHA3_round(uint64_t T[25], const uint64_t A[25], uint64_t RC)
constexpr uint64_t load_le< uint64_t >(const uint8_t in[], size_t off)
std::vector< T, secure_allocator< T > > secure_vector