Botan  2.4.0
Crypto and TLS for C++11
keccak.cpp
Go to the documentation of this file.
1 /*
2 * Keccak
3 * (C) 2010,2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/keccak.h>
9 #include <botan/sha3.h>
10 #include <botan/exceptn.h>
11 
12 namespace Botan {
13 
14 std::unique_ptr<HashFunction> Keccak_1600::copy_state() const
15  {
16  return std::unique_ptr<HashFunction>(new Keccak_1600(*this));
17  }
18 
19 Keccak_1600::Keccak_1600(size_t output_bits) :
20  m_output_bits(output_bits),
21  m_bitrate(1600 - 2*output_bits),
22  m_S(25),
23  m_S_pos(0)
24  {
25  // We only support the parameters for the SHA-3 proposal
26 
27  if(output_bits != 224 && output_bits != 256 &&
28  output_bits != 384 && output_bits != 512)
29  throw Invalid_Argument("Keccak_1600: Invalid output length " +
30  std::to_string(output_bits));
31  }
32 
33 std::string Keccak_1600::name() const
34  {
35  return "Keccak-1600(" + std::to_string(m_output_bits) + ")";
36  }
37 
39  {
40  return new Keccak_1600(m_output_bits);
41  }
42 
44  {
45  zeroise(m_S);
46  m_S_pos = 0;
47  }
48 
49 void Keccak_1600::add_data(const uint8_t input[], size_t length)
50  {
51  m_S_pos = SHA_3::absorb(m_bitrate, m_S, m_S_pos, input, length);
52  }
53 
54 void Keccak_1600::final_result(uint8_t output[])
55  {
56  std::vector<uint8_t> padding(m_bitrate / 8 - m_S_pos);
57 
58  padding[0] = 0x01;
59  padding[padding.size()-1] |= 0x80;
60 
61  add_data(padding.data(), padding.size());
62 
63  /*
64  * We never have to run the permutation again because we only support
65  * limited output lengths
66  */
67  for(size_t i = 0; i != m_output_bits/8; ++i)
68  output[i] = get_byte(7 - (i % 8), m_S[i/8]);
69 
70  clear();
71  }
72 
73 }
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
HashFunction * clone() const override
Definition: keccak.cpp:38
std::unique_ptr< HashFunction > copy_state() const override
Definition: keccak.cpp:14
std::string name() const override
Definition: keccak.cpp:33
Definition: alg_id.cpp:13
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39
Keccak_1600(size_t output_bits=512)
Definition: keccak.cpp:19
void clear() override
Definition: keccak.cpp:43
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:181