Botan 3.6.1
Crypto and TLS for C&
shake.cpp
Go to the documentation of this file.
1/*
2* SHAKE-128/256 as a hash
3* (C) 2016 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/shake.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12
13namespace Botan {
14
15SHAKE_128::SHAKE_128(size_t output_bits) : m_keccak(256, 0xF, 4), m_output_bits(output_bits) {
16 if(output_bits % 8 != 0) {
17 throw Invalid_Argument(fmt("SHAKE_128: Invalid output length {}", output_bits));
18 }
19}
20
21std::string SHAKE_128::name() const {
22 return fmt("SHAKE-128({})", m_output_bits);
23}
24
25std::unique_ptr<HashFunction> SHAKE_128::new_object() const {
26 return std::make_unique<SHAKE_128>(m_output_bits);
27}
28
29std::unique_ptr<HashFunction> SHAKE_128::copy_state() const {
30 return std::make_unique<SHAKE_128>(*this);
31}
32
33void SHAKE_128::add_data(std::span<const uint8_t> input) {
34 m_keccak.absorb(input);
35}
36
37void SHAKE_128::final_result(std::span<uint8_t> output) {
38 m_keccak.finish();
39 m_keccak.squeeze(output);
40 clear();
41}
42
43SHAKE_256::SHAKE_256(size_t output_bits) : m_keccak(512, 0xF, 4), m_output_bits(output_bits) {
44 if(output_bits % 8 != 0) {
45 throw Invalid_Argument(fmt("SHAKE_256: Invalid output length {}", output_bits));
46 }
47}
48
49std::string SHAKE_256::name() const {
50 return fmt("SHAKE-256({})", m_output_bits);
51}
52
53std::unique_ptr<HashFunction> SHAKE_256::new_object() const {
54 return std::make_unique<SHAKE_256>(m_output_bits);
55}
56
57std::unique_ptr<HashFunction> SHAKE_256::copy_state() const {
58 return std::make_unique<SHAKE_256>(*this);
59}
60
61void SHAKE_256::add_data(std::span<const uint8_t> input) {
62 m_keccak.absorb(input);
63}
64
65void SHAKE_256::final_result(std::span<uint8_t> output) {
66 m_keccak.finish();
67 m_keccak.squeeze(output);
68 clear();
69}
70
71} // namespace Botan
void squeeze(std::span< uint8_t > output)
Expand output data from the current Keccak state.
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.
std::unique_ptr< HashFunction > new_object() const override
Definition shake.cpp:25
SHAKE_128(size_t output_bits)
Definition shake.cpp:15
void clear() override
Definition shake.h:37
std::string name() const override
Definition shake.cpp:21
std::unique_ptr< HashFunction > copy_state() const override
Definition shake.cpp:29
std::unique_ptr< HashFunction > new_object() const override
Definition shake.cpp:53
void clear() override
Definition shake.h:68
SHAKE_256(size_t output_bits)
Definition shake.cpp:43
std::string name() const override
Definition shake.cpp:49
std::unique_ptr< HashFunction > copy_state() const override
Definition shake.cpp:57
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53