Botan  2.7.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  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 
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 
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 }
std::string name() const override
Definition: shake.cpp:63
SHAKE_256(size_t output_bits)
Definition: shake.cpp:55
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:145
static void expand(size_t bitrate, secure_vector< uint64_t > &S, uint8_t output[], size_t output_length)
Definition: sha3.cpp:157
HashFunction * clone() const override
Definition: shake.cpp:68
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:210
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:98
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:78
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:73
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183