Botan 3.10.0
Crypto and TLS for C&
keccak_perm.h
Go to the documentation of this file.
1/*
2* Keccak Permutation
3* (C) 2010,2016 Jack Lloyd
4* (C) 2023 Falko Strenzke
5* (C) 2023,2025 René Meusel - Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#ifndef BOTAN_KECCAK_PERM_H_
11#define BOTAN_KECCAK_PERM_H_
12
13#include <botan/internal/sponge.h>
14#include <span>
15#include <string>
16
17namespace Botan {
18
20 uint64_t padding; /// The padding bits in little-endian order
21 uint8_t bit_len; /// The number of relevant bits in 'padding'
22
23 /// NIST FIPS 202 Section 6.1
24 static constexpr KeccakPadding sha3() { return {.padding = 0b10 /* little-endian */, .bit_len = 2}; }
25
26 /// NIST FIPS 202 Section 6.2
27 static constexpr KeccakPadding shake() { return {.padding = 0b1111, .bit_len = 4}; }
28
29 /// NIST SP.800-185 Section 3.3
30 static constexpr KeccakPadding cshake() { return {.padding = 0b00, .bit_len = 2}; }
31
32 /// Keccak submission, prior to the introduction of an algorithm specific padding
33 static constexpr KeccakPadding keccak1600() { return {.padding = 0, .bit_len = 0}; }
34};
35
36/**
37* KECCAK FIPS
38*
39* This file implements Keccak[c] which is specified by NIST FIPS 202 [1], where
40* "c" is the variable capacity of this hash primitive. Keccak[c] is not a
41* general purpose hash function, but used as the basic primitive for algorithms
42* such as SHA-3 and KMAC. This is not to be confused with the "informal" general purpose hash
43* function which is referred to as "Keccak" and apparently refers to the final
44* submission version of the Keccak submission in the SHA-3 contest, possibly
45* what is released by NIST under the name "KECCAK - Final Algorithm Package" [2].
46* See also the file keccak.h for the details how the keccak hash function is defined
47* in terms of the Keccak[c] – a detail which cannot be found in [1].
48*
49*
50*
51* [1] FIPS PUB 202 – FEDERAL INFORMATION PROCESSING STANDARDS PUBLICATION – SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions
52* https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf#page=28
53* [2] https://csrc.nist.gov/projects/hash-functions/sha-3-project
54*/
55class Keccak_Permutation final : public Sponge<25, uint64_t> {
56 public:
61
62 public:
63 /**
64 * @brief Instantiate a Keccak permutation
65 *
66 * @param config Keccak parameter configuration
67 */
68 constexpr explicit Keccak_Permutation(Config config) :
69 Sponge({.bit_rate = state_bits() - config.capacity_bits, .initial_state = {}}), m_padding(config.padding) {}
70
71 void clear();
72 std::string provider() const;
73
74 /**
75 * @brief Absorb input data into the Keccak sponge
76 *
77 * This method can be called multiple times with arbitrary-length buffers.
78 *
79 * @param input the input data
80 */
81 void absorb(std::span<const uint8_t> input);
82
83 /**
84 * @brief Expand output data from the current Keccak state
85 *
86 * This method can be called multiple times with arbitrary-length buffers.
87 *
88 * @param output the designated output memory
89 */
90 void squeeze(std::span<uint8_t> output);
91
92 /**
93 * @brief Add final padding (as provided in the constructor) and permute
94 */
95 void finish();
96
97 /**
98 * The Keccak permutation function
99 */
100 void permute();
101
102 private:
103#if defined(BOTAN_HAS_KECCAK_PERM_BMI2)
104 void permute_bmi2();
105#endif
106
107 private:
108 KeccakPadding m_padding;
109};
110
111} // namespace Botan
112
113#endif
void squeeze(std::span< uint8_t > output)
Expand output data from the current Keccak state.
std::string provider() const
constexpr Keccak_Permutation(Config config)
Instantiate a Keccak permutation.
Definition keccak_perm.h:68
void absorb(std::span< const uint8_t > input)
Absorb input data into the Keccak sponge.
void finish()
Add final padding (as provided in the constructor) and permute.
static constexpr size_t state_bits()
Definition sponge.h:46
constexpr Sponge(Config config)
Definition sponge.h:38
static constexpr KeccakPadding sha3()
The number of relevant bits in 'padding'.
Definition keccak_perm.h:24
static constexpr KeccakPadding shake()
NIST FIPS 202 Section 6.2.
Definition keccak_perm.h:27
static constexpr KeccakPadding cshake()
NIST SP.800-185 Section 3.3.
Definition keccak_perm.h:30
uint8_t bit_len
The padding bits in little-endian order.
Definition keccak_perm.h:21
static constexpr KeccakPadding keccak1600()
Keccak submission, prior to the introduction of an algorithm specific padding.
Definition keccak_perm.h:33