/src/botan/src/lib/hash/shake/shake.cpp
Line | Count | Source (jump to first uncovered line) |
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/internal/shake.h> |
9 | | #include <botan/internal/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 | 0 | { |
17 | 0 | if(output_bits % 8 != 0) |
18 | 0 | throw Invalid_Argument("SHAKE_128: Invalid output length " + |
19 | 0 | std::to_string(output_bits)); |
20 | 0 | } |
21 | | |
22 | | std::string SHAKE_128::name() const |
23 | 0 | { |
24 | 0 | return "SHAKE-128(" + std::to_string(m_output_bits) + ")"; |
25 | 0 | } |
26 | | |
27 | | HashFunction* SHAKE_128::clone() const |
28 | 0 | { |
29 | 0 | return new SHAKE_128(m_output_bits); |
30 | 0 | } |
31 | | |
32 | | std::unique_ptr<HashFunction> SHAKE_128::copy_state() const |
33 | 0 | { |
34 | 0 | return std::unique_ptr<HashFunction>(new SHAKE_128(*this)); |
35 | 0 | } |
36 | | |
37 | | void SHAKE_128::clear() |
38 | 0 | { |
39 | 0 | zeroise(m_S); |
40 | 0 | m_S_pos = 0; |
41 | 0 | } |
42 | | |
43 | | void SHAKE_128::add_data(const uint8_t input[], size_t length) |
44 | 0 | { |
45 | 0 | m_S_pos = SHA_3::absorb(SHAKE_128_BITRATE, m_S, m_S_pos, input, length); |
46 | 0 | } |
47 | | |
48 | | void SHAKE_128::final_result(uint8_t output[]) |
49 | 0 | { |
50 | 0 | SHA_3::finish(SHAKE_128_BITRATE, m_S, m_S_pos, 0x1F, 0x80); |
51 | 0 | SHA_3::expand(SHAKE_128_BITRATE, m_S, output, output_length()); |
52 | 0 | clear(); |
53 | 0 | } |
54 | | |
55 | | SHAKE_256::SHAKE_256(size_t output_bits) : |
56 | | m_output_bits(output_bits), m_S(25), m_S_pos(0) |
57 | 0 | { |
58 | 0 | if(output_bits % 8 != 0) |
59 | 0 | throw Invalid_Argument("SHAKE_256: Invalid output length " + |
60 | 0 | std::to_string(output_bits)); |
61 | 0 | } |
62 | | |
63 | | std::string SHAKE_256::name() const |
64 | 0 | { |
65 | 0 | return "SHAKE-256(" + std::to_string(m_output_bits) + ")"; |
66 | 0 | } |
67 | | |
68 | | HashFunction* SHAKE_256::clone() const |
69 | 0 | { |
70 | 0 | return new SHAKE_256(m_output_bits); |
71 | 0 | } |
72 | | |
73 | | std::unique_ptr<HashFunction> SHAKE_256::copy_state() const |
74 | 0 | { |
75 | 0 | return std::unique_ptr<HashFunction>(new SHAKE_256(*this)); |
76 | 0 | } |
77 | | |
78 | | void SHAKE_256::clear() |
79 | 0 | { |
80 | 0 | zeroise(m_S); |
81 | 0 | m_S_pos = 0; |
82 | 0 | } |
83 | | |
84 | | void SHAKE_256::add_data(const uint8_t input[], size_t length) |
85 | 0 | { |
86 | 0 | m_S_pos = SHA_3::absorb(SHAKE_256_BITRATE, m_S, m_S_pos, input, length); |
87 | 0 | } |
88 | | |
89 | | void SHAKE_256::final_result(uint8_t output[]) |
90 | 0 | { |
91 | 0 | SHA_3::finish(SHAKE_256_BITRATE, m_S, m_S_pos, 0x1F, 0x80); |
92 | 0 | SHA_3::expand(SHAKE_256_BITRATE, m_S, output, output_length()); |
93 | |
|
94 | 0 | clear(); |
95 | 0 | } |
96 | | |
97 | | } |