Botan 3.10.0
Crypto and TLS for C&
cshake_xof.cpp
Go to the documentation of this file.
1/*
2 * cSHAKE-128 and cSHAKE-256 as XOFs
3 *
4 * (C) 2016-2023 Jack Lloyd
5 * 2022-2023 René Meusel - Rohde & Schwarz Cybersecurity
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9
10#include <botan/internal/cshake_xof.h>
11
12#include <botan/internal/keccak_helpers.h>
13#include <botan/internal/mem_utils.h>
14
15namespace Botan {
16
17cSHAKE_XOF::cSHAKE_XOF(size_t capacity, std::vector<uint8_t> function_name) :
18 m_keccak({.capacity_bits = capacity, .padding = KeccakPadding::cshake()}),
19 m_function_name(std::move(function_name)),
20 m_output_generated(false) {
21 BOTAN_ASSERT_NOMSG(capacity == 256 || capacity == 512);
22}
23
24cSHAKE_XOF::cSHAKE_XOF(size_t capacity, std::span<const uint8_t> function_name) :
25 cSHAKE_XOF(capacity, std::vector<uint8_t>{function_name.begin(), function_name.end()}) {}
26
27cSHAKE_XOF::cSHAKE_XOF(size_t capacity, std::string_view function_name) :
29
30void cSHAKE_XOF::reset() {
31 m_keccak.clear();
32 m_output_generated = false;
33}
34
35std::string cSHAKE_XOF::provider() const {
36 return m_keccak.provider();
37}
38
39size_t cSHAKE_XOF::block_size() const {
40 return m_keccak.byte_rate();
41}
42
43bool cSHAKE_XOF::valid_salt_length(size_t salt_length) const {
44 // NIST SP.800-185 Section 3.2
45 // When N and S are both empty strings, cSHAKE(X, L, N, S) is equivalent to
46 // SHAKE as defined in FIPS 202.
47 //
48 // We don't implement the fallback case where N and S are empty. Hence, if
49 // the function name N was defined as 'empty', a salt must be provided.
50 return m_function_name.size() + salt_length > 0;
51}
52
53void cSHAKE_XOF::start_msg(std::span<const uint8_t> salt, std::span<const uint8_t> key) {
54 BOTAN_STATE_CHECK(!m_output_generated);
55 BOTAN_ASSERT_NOMSG(key.empty());
56 keccak_absorb_padded_strings_encoding(*this, block_size(), m_function_name, salt);
57}
58
59void cSHAKE_XOF::add_data(std::span<const uint8_t> input) {
60 BOTAN_STATE_CHECK(!m_output_generated);
61 m_keccak.absorb(input);
62}
63
64void cSHAKE_XOF::generate_bytes(std::span<uint8_t> output) {
65 if(!m_output_generated) {
66 m_output_generated = true;
67 m_keccak.finish();
68 }
69
70 m_keccak.squeeze(output);
71}
72
73} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
void absorb(std::span< const uint8_t > input)
Absorb input data into the Keccak sponge.
T output(size_t bytes)
Definition xof.h:153
size_t block_size() const final
cSHAKE_XOF(size_t capacity, std::vector< uint8_t > function_name)
const std::vector< uint8_t > & function_name() const
Definition cshake_xof.h:46
bool valid_salt_length(size_t salt_length) const final
std::string provider() const final
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:28
size_t keccak_absorb_padded_strings_encoding(T &sink, size_t padding_mod, Ts... byte_strings)
static constexpr KeccakPadding cshake()
NIST SP.800-185 Section 3.3.
Definition keccak_perm.h:30