Botan  2.7.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  SHA_3::finish(m_bitrate, m_S, m_S_pos, 0x01, 0x80);
57 
58  /*
59  * We never have to run the permutation again because we only support
60  * limited output lengths
61  */
62  copy_out_vec_le(output, m_output_bits/8, m_S);
63 
64  clear();
65  }
66 
67 }
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
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
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
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:183
void copy_out_vec_le(uint8_t out[], size_t out_bytes, const std::vector< T, Alloc > &in)
Definition: loadstor.h:690