Botan  2.4.0
Crypto and TLS for C++11
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 
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 
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  std::vector<uint8_t> padding(SHAKE_128_BITRATE / 8 - m_S_pos);
51 
52  padding[0] = 0x1F;
53  padding[padding.size()-1] |= 0x80;
54 
55  add_data(padding.data(), padding.size());
56 
57  SHA_3::expand(SHAKE_128_BITRATE, m_S, output, output_length());
58 
59  clear();
60  }
61 
62 SHAKE_256::SHAKE_256(size_t output_bits) :
63  m_output_bits(output_bits), m_S(25), m_S_pos(0)
64  {
65  if(output_bits % 8 != 0)
66  throw Invalid_Argument("SHAKE_256: Invalid output length " +
67  std::to_string(output_bits));
68  }
69 
70 std::string SHAKE_256::name() const
71  {
72  return "SHAKE-256(" + std::to_string(m_output_bits) + ")";
73  }
74 
76  {
77  return new SHAKE_256(m_output_bits);
78  }
79 
80 std::unique_ptr<HashFunction> SHAKE_256::copy_state() const
81  {
82  return std::unique_ptr<HashFunction>(new SHAKE_256(*this));
83  }
84 
86  {
87  zeroise(m_S);
88  m_S_pos = 0;
89  }
90 
91 void SHAKE_256::add_data(const uint8_t input[], size_t length)
92  {
93  m_S_pos = SHA_3::absorb(SHAKE_256_BITRATE, m_S, m_S_pos, input, length);
94  }
95 
96 void SHAKE_256::final_result(uint8_t output[])
97  {
98  std::vector<uint8_t> padding(SHAKE_256_BITRATE / 8 - m_S_pos);
99 
100  padding[0] = 0x1F;
101  padding[padding.size()-1] |= 0x80;
102 
103  add_data(padding.data(), padding.size());
104 
105  SHA_3::expand(SHAKE_256_BITRATE, m_S, output, output_length());
106 
107  clear();
108  }
109 
110 }
std::string name() const override
Definition: shake.cpp:70
SHAKE_256(size_t output_bits)
Definition: shake.cpp:62
static void expand(size_t bitrate, secure_vector< uint64_t > &S, uint8_t output[], size_t output_length)
Definition: sha3.cpp:180
HashFunction * clone() const override
Definition: shake.cpp:75
void clear() override
Definition: shake.cpp:37
SHAKE_128(size_t output_bits)
Definition: shake.cpp:14
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
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:133
size_t output_length() const override
Definition: shake.h:31
std::string name() const override
Definition: shake.cpp:22
void clear() override
Definition: shake.cpp:85
Definition: alg_id.cpp:13
size_t output_length() const override
Definition: shake.h:63
std::unique_ptr< HashFunction > copy_state() const override
Definition: shake.cpp:32
HashFunction * clone() const override
Definition: shake.cpp:27
std::unique_ptr< HashFunction > copy_state() const override
Definition: shake.cpp:80
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:181