Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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/parsing.h>
11 #include <botan/exceptn.h>
12
13 namespace Botan {
14
15 Keccak_1600::Keccak_1600(size_t output_bits) :
16 m_output_bits(output_bits),
17 m_bitrate(1600 - 2*output_bits),
18 m_S(25),
19 m_S_pos(0)
20 {
21 // We only support the parameters for the SHA-3 proposal
22
23 if(output_bits != 224 && output_bits != 256 &&
24 output_bits != 384 && output_bits != 512)
25 throw Invalid_Argument("Keccak_1600: Invalid output length " +
26 std::to_string(output_bits));
27 }
28
29 std::string Keccak_1600::name() const
30 {
31 return "Keccak-1600(" + std::to_string(m_output_bits) + ")";
32 }
33
34 HashFunction* Keccak_1600::clone() const
35 {
36 return new Keccak_1600(m_output_bits);
37 }
38
39 void Keccak_1600::clear()
40 {
41 zeroise(m_S);
42 m_S_pos = 0;
43 }
44
45 void Keccak_1600::add_data(const uint8_t input[], size_t length)
46 {
47 m_S_pos = SHA_3::absorb(m_bitrate, m_S, m_S_pos, input, length);
48 }
49
50 void Keccak_1600::final_result(uint8_t output[])
51 {
52 std::vector<uint8_t> padding(m_bitrate / 8 - m_S_pos);
53
54 padding[0] = 0x01;
55 padding[padding.size()-1] |= 0x80;
56
57 add_data(padding.data(), padding.size());
58
59 /*
60 * We never have to run the permutation again because we only support
61 * limited output lengths
62 */
63 for(size_t i = 0; i != m_output_bits/8; ++i)
64 output[i] = get_byte(7 - (i % 8), m_S[i/8]);
65
66 clear();
67 }
68
69 }
Botan::Invalid_Argument
Definition: exceptn.h:34
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
Botan::SHA_3::absorb
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:129
Botan::Keccak_1600::clone
HashFunction * clone() const override
Definition: keccak.cpp:34
Botan::Keccak_1600::name
std::string name() const override
Definition: keccak.cpp:29
Botan
Definition: alg_id.cpp:13
Botan::HashFunction
Definition: hash.h:19
Botan::get_byte
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:47
Botan::Keccak_1600::Keccak_1600
Keccak_1600(size_t output_bits=512)
Definition: keccak.cpp:15
Botan::Keccak_1600::clear
void clear() override
Definition: keccak.cpp:39
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:211