/src/botan/src/lib/hash/streebog/streebog.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Streebog |
3 | | * (C) 2017 Ribose Inc. |
4 | | * (C) 2018 Jack Lloyd |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #include <botan/internal/streebog.h> |
10 | | |
11 | | #include <botan/exceptn.h> |
12 | | #include <botan/internal/bswap.h> |
13 | | #include <botan/internal/fmt.h> |
14 | | #include <botan/internal/loadstor.h> |
15 | | #include <botan/internal/stl_util.h> |
16 | | |
17 | | namespace Botan { |
18 | | |
19 | | extern const uint64_t STREEBOG_Ax[8][256]; |
20 | | extern const uint64_t STREEBOG_C[12][8]; |
21 | | |
22 | 0 | std::unique_ptr<HashFunction> Streebog::copy_state() const { |
23 | 0 | return std::make_unique<Streebog>(*this); |
24 | 0 | } |
25 | | |
26 | 258 | Streebog::Streebog(size_t output_bits) : m_output_bits(output_bits), m_count(0), m_h(8), m_S(8) { |
27 | 258 | if(output_bits != 256 && output_bits != 512) { |
28 | 0 | throw Invalid_Argument(fmt("Streebog: Invalid output length {}", output_bits)); |
29 | 0 | } |
30 | | |
31 | 258 | clear(); |
32 | 258 | } |
33 | | |
34 | 3 | std::string Streebog::name() const { |
35 | 3 | return fmt("Streebog-{}", m_output_bits); |
36 | 3 | } |
37 | | |
38 | | /* |
39 | | * Clear memory of sensitive data |
40 | | */ |
41 | 829 | void Streebog::clear() { |
42 | 829 | m_count = 0; |
43 | 829 | m_buffer.clear(); |
44 | 829 | zeroise(m_S); |
45 | | |
46 | 829 | const uint64_t fill = (m_output_bits == 512) ? 0 : 0x0101010101010101; |
47 | 829 | std::fill(m_h.begin(), m_h.end(), fill); |
48 | 829 | } |
49 | | |
50 | | /* |
51 | | * Update the hash |
52 | | */ |
53 | 21.5k | void Streebog::add_data(std::span<const uint8_t> input) { |
54 | 21.5k | BufferSlicer in(input); |
55 | | |
56 | 23.9k | while(!in.empty()) { |
57 | 2.42k | if(const auto one_block = m_buffer.handle_unaligned_data(in)) { |
58 | 506 | compress(one_block->data()); |
59 | 506 | m_count += 512; |
60 | 506 | } |
61 | | |
62 | 2.42k | if(m_buffer.in_alignment()) { |
63 | 83.0k | while(const auto aligned_block = m_buffer.next_aligned_block_to_process(in)) { |
64 | 81.9k | compress(aligned_block->data()); |
65 | 81.9k | m_count += 512; |
66 | 81.9k | } |
67 | 1.16k | } |
68 | 2.42k | } |
69 | 21.5k | } |
70 | | |
71 | | /* |
72 | | * Finalize a hash |
73 | | */ |
74 | 448 | void Streebog::final_result(std::span<uint8_t> output) { |
75 | 448 | const auto pos = m_buffer.elements_in_buffer(); |
76 | | |
77 | 448 | const uint8_t padding = 0x01; |
78 | 448 | m_buffer.append({&padding, 1}); |
79 | 448 | m_buffer.fill_up_with_zeros(); |
80 | | |
81 | 448 | compress(m_buffer.consume().data()); |
82 | 448 | m_count += pos * 8; |
83 | | |
84 | 448 | m_buffer.fill_up_with_zeros(); |
85 | 448 | store_le(m_count, m_buffer.directly_modify_first(sizeof(m_count)).data()); |
86 | 448 | compress(m_buffer.consume().data(), true); |
87 | | |
88 | 448 | compress_64(m_S.data(), true); |
89 | | // FIXME |
90 | 448 | std::memcpy(output.data(), &m_h[8 - output_length() / 8], output_length()); |
91 | 448 | clear(); |
92 | 448 | } |
93 | | |
94 | | namespace { |
95 | | |
96 | 144M | inline uint64_t force_le(uint64_t x) { |
97 | 144M | #if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN) |
98 | 144M | return x; |
99 | | #elif defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN) |
100 | | return reverse_bytes(x); |
101 | | #else |
102 | | store_le(x, reinterpret_cast<uint8_t*>(&x)); |
103 | | return x; |
104 | | #endif |
105 | 144M | } |
106 | | |
107 | 2.09M | inline void lps(uint64_t block[8]) { |
108 | 2.09M | uint8_t r[64]; |
109 | | // FIXME |
110 | 2.09M | std::memcpy(r, block, 64); |
111 | | |
112 | 18.8M | for(int i = 0; i < 8; ++i) { |
113 | 16.7M | block[i] = force_le(STREEBOG_Ax[0][r[i + 0 * 8]]) ^ force_le(STREEBOG_Ax[1][r[i + 1 * 8]]) ^ |
114 | 16.7M | force_le(STREEBOG_Ax[2][r[i + 2 * 8]]) ^ force_le(STREEBOG_Ax[3][r[i + 3 * 8]]) ^ |
115 | 16.7M | force_le(STREEBOG_Ax[4][r[i + 4 * 8]]) ^ force_le(STREEBOG_Ax[5][r[i + 5 * 8]]) ^ |
116 | 16.7M | force_le(STREEBOG_Ax[6][r[i + 6 * 8]]) ^ force_le(STREEBOG_Ax[7][r[i + 7 * 8]]); |
117 | 16.7M | } |
118 | 2.09M | } |
119 | | |
120 | | } //namespace |
121 | | |
122 | 83.3k | void Streebog::compress(const uint8_t input[], bool last_block) { |
123 | 83.3k | uint64_t M[8]; |
124 | 83.3k | std::memcpy(M, input, 64); |
125 | | |
126 | 83.3k | compress_64(M, last_block); |
127 | 83.3k | } |
128 | | |
129 | 83.7k | void Streebog::compress_64(const uint64_t M[], bool last_block) { |
130 | 83.7k | const uint64_t N = last_block ? 0 : force_le(m_count); |
131 | | |
132 | 83.7k | uint64_t hN[8]; |
133 | 83.7k | uint64_t A[8]; |
134 | | |
135 | 83.7k | copy_mem(hN, m_h.data(), 8); |
136 | 83.7k | hN[0] ^= N; |
137 | 83.7k | lps(hN); |
138 | | |
139 | 83.7k | copy_mem(A, hN, 8); |
140 | | |
141 | 753k | for(size_t i = 0; i != 8; ++i) { |
142 | 670k | hN[i] ^= M[i]; |
143 | 670k | } |
144 | | |
145 | 1.08M | for(size_t i = 0; i < 12; ++i) { |
146 | 9.04M | for(size_t j = 0; j != 8; ++j) { |
147 | 8.04M | A[j] ^= force_le(STREEBOG_C[i][j]); |
148 | 8.04M | } |
149 | 1.00M | lps(A); |
150 | | |
151 | 1.00M | lps(hN); |
152 | 9.04M | for(size_t j = 0; j != 8; ++j) { |
153 | 8.04M | hN[j] ^= A[j]; |
154 | 8.04M | } |
155 | 1.00M | } |
156 | | |
157 | 753k | for(size_t i = 0; i != 8; ++i) { |
158 | 670k | m_h[i] ^= hN[i] ^ M[i]; |
159 | 670k | } |
160 | | |
161 | 83.7k | if(!last_block) { |
162 | 82.8k | uint64_t carry = 0; |
163 | 745k | for(int i = 0; i < 8; i++) { |
164 | 662k | const uint64_t m = force_le(M[i]); |
165 | 662k | const uint64_t hi = force_le(m_S[i]); |
166 | 662k | const uint64_t t = hi + m + carry; |
167 | | |
168 | 662k | m_S[i] = force_le(t); |
169 | 662k | if(t != m) { |
170 | 658k | carry = (t < m); |
171 | 658k | } |
172 | 662k | } |
173 | 82.8k | } |
174 | 83.7k | } |
175 | | |
176 | | } // namespace Botan |