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
SHAKE_128(size_t output_bits)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
name() const22 std::string SHAKE_128::name() const
23 {
24 return "SHAKE-128(" + std::to_string(m_output_bits) + ")";
25 }
26
clone() const27 HashFunction* SHAKE_128::clone() const
28 {
29 return new SHAKE_128(m_output_bits);
30 }
31
copy_state() const32 std::unique_ptr<HashFunction> SHAKE_128::copy_state() const
33 {
34 return std::unique_ptr<HashFunction>(new SHAKE_128(*this));
35 }
36
clear()37 void SHAKE_128::clear()
38 {
39 zeroise(m_S);
40 m_S_pos = 0;
41 }
42
add_data(const uint8_t input[],size_t length)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
final_result(uint8_t output[])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
SHAKE_256(size_t output_bits)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
name() const63 std::string SHAKE_256::name() const
64 {
65 return "SHAKE-256(" + std::to_string(m_output_bits) + ")";
66 }
67
clone() const68 HashFunction* SHAKE_256::clone() const
69 {
70 return new SHAKE_256(m_output_bits);
71 }
72
copy_state() const73 std::unique_ptr<HashFunction> SHAKE_256::copy_state() const
74 {
75 return std::unique_ptr<HashFunction>(new SHAKE_256(*this));
76 }
77
clear()78 void SHAKE_256::clear()
79 {
80 zeroise(m_S);
81 m_S_pos = 0;
82 }
83
add_data(const uint8_t input[],size_t length)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
final_result(uint8_t output[])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 }
98