Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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/shake.h>
9 #include <botan/sha3.h>
10 #include <botan/exceptn.h>
11
12 namespace Botan {
13
14 SHAKE_128::SHAKE_128(size_t output_bits) :
15 m_output_bits(output_bits), m_S(25), m_S_pos(0)
16 {
17 if(output_bits % 8 != 0)
18 throw Invalid_Argument("SHAKE_128: Invalid output length " +
19 std::to_string(output_bits));
20 }
21
22 std::string SHAKE_128::name() const
23 {
24 return "SHAKE-128(" + std::to_string(m_output_bits) + ")";
25 }
26
27 HashFunction* SHAKE_128::clone() const
28 {
29 return new SHAKE_128(m_output_bits);
30 }
31
32 std::unique_ptr<HashFunction> SHAKE_128::copy_state() const
33 {
34 return std::unique_ptr<HashFunction>(new SHAKE_128(*this));
35 }
36
37 void SHAKE_128::clear()
38 {
39 zeroise(m_S);
40 m_S_pos = 0;
41 }
42
43 void SHAKE_128::add_data(const uint8_t input[], size_t length)
44 {
45 m_S_pos = SHA_3::absorb(SHAKE_128_BITRATE, m_S, m_S_pos, input, length);
46 }
47
48 void SHAKE_128::final_result(uint8_t output[])
49 {
50 SHA_3::finish(SHAKE_128_BITRATE, m_S, m_S_pos, 0x1F, 0x80);
51 SHA_3::expand(SHAKE_128_BITRATE, m_S, output, output_length());
52 clear();
53 }
54
55 SHAKE_256::SHAKE_256(size_t output_bits) :
56 m_output_bits(output_bits), m_S(25), m_S_pos(0)
57 {
58 if(output_bits % 8 != 0)
59 throw Invalid_Argument("SHAKE_256: Invalid output length " +
60 std::to_string(output_bits));
61 }
62
63 std::string SHAKE_256::name() const
64 {
65 return "SHAKE-256(" + std::to_string(m_output_bits) + ")";
66 }
67
68 HashFunction* SHAKE_256::clone() const
69 {
70 return new SHAKE_256(m_output_bits);
71 }
72
73 std::unique_ptr<HashFunction> SHAKE_256::copy_state() const
74 {
75 return std::unique_ptr<HashFunction>(new SHAKE_256(*this));
76 }
77
78 void SHAKE_256::clear()
79 {
80 zeroise(m_S);
81 m_S_pos = 0;
82 }
83
84 void SHAKE_256::add_data(const uint8_t input[], size_t length)
85 {
86 m_S_pos = SHA_3::absorb(SHAKE_256_BITRATE, m_S, m_S_pos, input, length);
87 }
88
89 void SHAKE_256::final_result(uint8_t output[])
90 {
91 SHA_3::finish(SHAKE_256_BITRATE, m_S, m_S_pos, 0x1F, 0x80);
92 SHA_3::expand(SHAKE_256_BITRATE, m_S, output, output_length());
93
94 clear();
95 }
96
97 }
Botan::SHAKE_256::name
std::string name() const override
Definition: shake.cpp:63
Botan::SHAKE_256::SHAKE_256
SHAKE_256(size_t output_bits)
Definition: shake.cpp:55
Botan::Invalid_Argument
Definition: exceptn.h:136
Botan::SHA_3::finish
static void finish(size_t bitrate, secure_vector< uint64_t > &S, size_t S_pos, uint8_t init_pad, uint8_t fini_pad)
Definition: sha3.cpp:168
Botan::SHA_3::expand
static void expand(size_t bitrate, secure_vector< uint64_t > &S, uint8_t output[], size_t output_length)
Definition: sha3.cpp:180
Botan::SHAKE_256::clone
HashFunction * clone() const override
Definition: shake.cpp:68
Botan::SHAKE_128::clear
void clear() override
Definition: shake.cpp:37
Botan::SHAKE_128::SHAKE_128
SHAKE_128(size_t output_bits)
Definition: shake.cpp:14
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
Botan::SHA_3::absorb
static size_t absorb(size_t bitrate, secure_vector< uint64_t > &S, size_t S_pos, const uint8_t input[], size_t length)
Definition: sha3.cpp:121
Botan::SHAKE_128::output_length
size_t output_length() const override
Definition: shake.h:33
Botan::SHAKE_128::name
std::string name() const override
Definition: shake.cpp:22
Botan::SHAKE_256::clear
void clear() override
Definition: shake.cpp:78
Botan
Definition: alg_id.cpp:13
Botan::SHAKE_256::output_length
size_t output_length() const override
Definition: shake.h:65
Botan::HashFunction
Definition: hash.h:20
Botan::SHAKE_128::copy_state
std::unique_ptr< HashFunction > copy_state() const override
Definition: shake.cpp:32
Botan::SHAKE_128::clone
HashFunction * clone() const override
Definition: shake.cpp:27
Botan::SHAKE_256::copy_state
std::unique_ptr< HashFunction > copy_state() const override
Definition: shake.cpp:73
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:160