9#ifndef BOTAN_SPONGE_PROCESSING_H_
10#define BOTAN_SPONGE_PROCESSING_H_
12#include <botan/internal/buffer_slicer.h>
13#include <botan/internal/buffer_stuffer.h>
14#include <botan/internal/loadstor.h>
24concept SpongeLike = std::unsigned_integral<
decltype(T::word_bytes)> &&
requires(T a) {
27 { a.state() } -> std::same_as<typename T::state_t&>;
28 { a._cursor() } -> std::same_as<size_t&>;
29 { a.byte_rate() } -> std::same_as<size_t>;
34 { a.permute() } -> std::same_as<void>;
43template <SpongeLike SpongeT>
50 using word_t =
typename SpongeT::word_t;
51 constexpr static auto word_bytes = SpongeT::word_bytes;
59 std::array<uint8_t, word_bytes> partial_word_bytes{};
61 return load_le(partial_word_bytes);
69 const auto partial_word_bytes =
store_le(partial_word);
80 const auto mask = ((word_t(0) - 1) >> ((word_bytes -
length) * 8)) << (
offset * 8);
81 return (state_word & ~mask) | (partial_input_word & mask);
89template <SpongeLike SpongeT>
92 using word_t =
typename SpongeT::word_t;
93 constexpr static auto word_bytes = SpongeT::word_bytes;
106template <
typename T,
typename SpongeT,
typename ModifierT>
108 { std::invoke(fn,
word, bounds) } -> std::same_as<typename SpongeT::word_t>;
111template <
typename T,
typename SpongeT>
134template <detail::SpongeLike SpongeT>
136 size_t bytes_to_process,
139 if(bytes_to_process == 0) {
143 constexpr auto word_bytes = SpongeT::word_bytes;
144 const auto byte_rate = sponge.byte_rate();
145 auto& S = sponge.state();
146 auto& cursor = sponge._cursor();
149 const auto bytes_out_of_word_alignment =
static_cast<size_t>(cursor % word_bytes);
150 if(bytes_out_of_word_alignment > 0) {
151 const auto bytes_until_word_alignment = word_bytes - bytes_out_of_word_alignment;
152 const auto bytes_from_input = std::min(bytes_to_process, bytes_until_word_alignment);
155 S[cursor / word_bytes] = modifier_fn(S[cursor / word_bytes],
157 .offset = bytes_out_of_word_alignment,
158 .length = bytes_from_input,
160 cursor += bytes_from_input;
161 bytes_to_process -= bytes_from_input;
163 if(cursor == byte_rate) {
175 while(bytes_to_process >= word_bytes) {
178 while(bytes_to_process >= word_bytes && cursor < byte_rate) {
180 cursor += word_bytes;
181 bytes_to_process -= word_bytes;
184 if(cursor == byte_rate) {
194 if(bytes_to_process > 0) {
195 S[cursor / word_bytes] = modifier_fn(S[cursor / word_bytes],
198 .length = bytes_to_process,
200 cursor += bytes_to_process;
204template <detail::SpongeLikeWithTrivialPermute SpongeT>
206 size_t bytes_to_process,
209 sponge, bytes_to_process, [&sponge] { sponge.permute(); }, modifier_fn);
219template <detail::SpongeLike SpongeT>
221 std::span<const uint8_t> input,
223 using word_t =
typename SpongeT::word_t;
227 return state_word ^ bounds.read_from(input_slicer);
243template <detail::SpongeLike SpongeT>
245 std::span<uint8_t> output,
247 using word_t =
typename SpongeT::word_t;
251 bounds.write_into(output_stuffer, state_word);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
void copy_into(std::span< uint8_t > sink)
std::span< const uint8_t > take(const size_t count)
Helper class to ease in-place marshalling of concatenated fixed-length values.
constexpr void append(std::span< const uint8_t > buffer)
constexpr bool full() const
word_t masked_assignment(word_t, word_t full_input_word) const
word_t read_from(BufferSlicer &slicer) const
void write_into(BufferStuffer &stuffer, word_t full_word) const
word_t masked_assignment(word_t state_word, word_t partial_input_word) const
void write_into(BufferStuffer &stuffer, word_t partial_word) const
word_t read_from(BufferSlicer &slicer) const
#define BOTAN_FORCE_INLINE
BOTAN_FORCE_INLINE void process_bytes_in_sponge(SpongeT &sponge, size_t bytes_to_process, const detail::PermutationFn auto &permutation_fn, const detail::ModifierFn< SpongeT > auto &modifier_fn)
void squeeze_from_sponge(SpongeT &sponge, std::span< uint8_t > output, const detail::PermutationFn auto &permutation_fn)
constexpr auto store_le(ParamTs &&... params)
constexpr auto load_le(ParamTs &&... params)
void absorb_into_sponge(SpongeT &sponge, std::span< const uint8_t > input, const detail::PermutationFn auto &permutation_fn)
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.