Botan 3.14.1
Crypto and TLS for C&
whirlpool.cpp
Go to the documentation of this file.
1/*
2* Whirlpool
3* (C) 1999-2007,2020,2026 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/whirlpool.h>
9
10#include <botan/compiler.h>
11#include <botan/internal/bit_ops.h>
12#include <botan/internal/buffer_slicer.h>
13#include <botan/internal/loadstor.h>
14#include <botan/internal/rotate.h>
15#include <botan/internal/target_info.h>
16#include <botan/internal/whirlpool_consts.h>
17#include <array>
18
19#if defined(BOTAN_HAS_CPUID)
20 #include <botan/internal/cpuid.h>
21#endif
22
23namespace Botan {
24
25namespace {
26
27#if defined(BOTAN_TARGET_ARCH_IS_ARM64) || defined(BOTAN_TARGET_ARCH_IS_X86_64) || defined(BOTAN_TARGET_ARCH_IS_PPC64)
28constexpr bool WhirlpoolUnalignedTables = true;
29#else
30constexpr bool WhirlpoolUnalignedTables = false;
31#endif
32
33// Combined S-box + MDS diffusion table
34consteval std::array<uint64_t, 256> whirlpool_T_table(const std::array<uint8_t, 256>& S) noexcept {
35 // MDS circulant matrix first row: [1, 1, 4, 1, 8, 5, 2, 9] over GF(2^8)
36 constexpr uint64_t MDS = 0x0101040108050209;
37
38 std::array<uint64_t, 256> T = {};
39 for(size_t i = 0; i != 256; ++i) {
40 T[i] = poly_mul<0x1D>(MDS, S[i]);
41 }
42 return T;
43}
44
45constexpr size_t WHIRL_U_STRIDE = 2 * sizeof(uint64_t);
46
47[[maybe_unused]] consteval std::array<uint8_t, 256 * WHIRL_U_STRIDE> whirlpool_U_table(
48 const std::array<uint64_t, 256>& T) noexcept {
49 // Duplicating each entry permits unaligned loads to perform the rotations
50 std::array<uint8_t, 256 * WHIRL_U_STRIDE> U = {};
51
52 for(size_t i = 0; i != 256; ++i) {
53 for(size_t j = 0; j != sizeof(uint64_t); ++j) {
54 U[WHIRL_U_STRIDE * i + j] = static_cast<uint8_t>(T[i] >> (8 * j));
55 U[WHIRL_U_STRIDE * i + sizeof(uint64_t) + j] = static_cast<uint8_t>(T[i] >> (8 * j));
56 }
57 }
58 return U;
59}
60
61BOTAN_FORCE_INLINE uint64_t
62whirl(uint64_t x0, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5, uint64_t x6, uint64_t x7) {
63 constexpr auto WHIRL_S = whirlpool_sbox();
64
65 if constexpr(WhirlpoolUnalignedTables) {
66 alignas(256) static constexpr auto WHIRL_U = whirlpool_U_table(whirlpool_T_table(WHIRL_S));
67
68 const uint64_t s0 = load_le<uint64_t>(WHIRL_U.data() + WHIRL_U_STRIDE * get_byte<0>(x0), 0);
69 const uint64_t s1 = load_le<uint64_t>(WHIRL_U.data() + WHIRL_U_STRIDE * get_byte<1>(x1) + 1, 0);
70 const uint64_t s2 = load_le<uint64_t>(WHIRL_U.data() + WHIRL_U_STRIDE * get_byte<2>(x2) + 2, 0);
71 const uint64_t s3 = load_le<uint64_t>(WHIRL_U.data() + WHIRL_U_STRIDE * get_byte<3>(x3) + 3, 0);
72 const uint64_t s4 = load_le<uint64_t>(WHIRL_U.data() + WHIRL_U_STRIDE * get_byte<4>(x4) + 4, 0);
73 const uint64_t s5 = load_le<uint64_t>(WHIRL_U.data() + WHIRL_U_STRIDE * get_byte<5>(x5) + 5, 0);
74 const uint64_t s6 = load_le<uint64_t>(WHIRL_U.data() + WHIRL_U_STRIDE * get_byte<6>(x6) + 6, 0);
75 const uint64_t s7 = load_le<uint64_t>(WHIRL_U.data() + WHIRL_U_STRIDE * get_byte<7>(x7) + 7, 0);
76
77 return s0 ^ s1 ^ s2 ^ s3 ^ s4 ^ s5 ^ s6 ^ s7;
78 } else {
79 alignas(256) static constexpr auto WHIRL_T = whirlpool_T_table(WHIRL_S);
80
81 const uint64_t s0 = WHIRL_T[get_byte<0>(x0)];
82 const uint64_t s1 = WHIRL_T[get_byte<1>(x1)];
83 const uint64_t s2 = WHIRL_T[get_byte<2>(x2)];
84 const uint64_t s3 = WHIRL_T[get_byte<3>(x3)];
85 const uint64_t s4 = WHIRL_T[get_byte<4>(x4)];
86 const uint64_t s5 = WHIRL_T[get_byte<5>(x5)];
87 const uint64_t s6 = WHIRL_T[get_byte<6>(x6)];
88 const uint64_t s7 = WHIRL_T[get_byte<7>(x7)];
89
90 return s0 ^ rotr<8>(s1) ^ rotr<16>(s2) ^ rotr<24>(s3) ^ rotr<32>(s4) ^ rotr<40>(s5) ^ rotr<48>(s6) ^ rotr<56>(s7);
91 }
92}
93
94} // namespace
95
96std::string Whirlpool::provider() const {
97#if defined(BOTAN_HAS_WHIRLPOOL_AVX512)
98 if(auto feat = CPUID::check(CPUID::Feature::AVX512)) {
99 return *feat;
100 }
101#endif
102
103#if defined(BOTAN_HAS_WHIRLPOOL_SIMD8X32)
104 if(auto feat = CPUID::check(CPUID::Feature::SIMD_8X32)) {
105 return *feat;
106 }
107#endif
108
109 return "base";
110}
111
112/*
113* Whirlpool Compression Function
114*/
115void Whirlpool::compress_n(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
116#if defined(BOTAN_HAS_WHIRLPOOL_AVX512)
118 return compress_n_avx512(digest, input, blocks);
119 }
120#endif
121
122#if defined(BOTAN_HAS_WHIRLPOOL_SIMD8X32)
124 return compress_n_simd8x32(digest, input, blocks);
125 }
126#endif
127
128 constexpr auto WHIRL_RC = whirlpool_rc();
129
130 BufferSlicer in(input);
131
132 for(size_t i = 0; i != blocks; ++i) {
133 const auto block = in.take(block_bytes);
134
135 uint64_t K[8];
136
137 K[0] = digest[0];
138 K[1] = digest[1];
139 K[2] = digest[2];
140 K[3] = digest[3];
141 K[4] = digest[4];
142 K[5] = digest[5];
143 K[6] = digest[6];
144 K[7] = digest[7];
145
146 uint64_t M[8];
147 load_be(M, block.data(), 8);
148
149 // First round (key masking)
150 uint64_t B0 = M[0] ^ K[0];
151 uint64_t B1 = M[1] ^ K[1];
152 uint64_t B2 = M[2] ^ K[2];
153 uint64_t B3 = M[3] ^ K[3];
154 uint64_t B4 = M[4] ^ K[4];
155 uint64_t B5 = M[5] ^ K[5];
156 uint64_t B6 = M[6] ^ K[6];
157 uint64_t B7 = M[7] ^ K[7];
158
159 for(size_t r = 0; r != 10; ++r) {
160 const uint64_t K0 = whirl(K[0], K[7], K[6], K[5], K[4], K[3], K[2], K[1]) ^ WHIRL_RC[r];
161 const uint64_t K1 = whirl(K[1], K[0], K[7], K[6], K[5], K[4], K[3], K[2]);
162 const uint64_t K2 = whirl(K[2], K[1], K[0], K[7], K[6], K[5], K[4], K[3]);
163 const uint64_t K3 = whirl(K[3], K[2], K[1], K[0], K[7], K[6], K[5], K[4]);
164 const uint64_t K4 = whirl(K[4], K[3], K[2], K[1], K[0], K[7], K[6], K[5]);
165 const uint64_t K5 = whirl(K[5], K[4], K[3], K[2], K[1], K[0], K[7], K[6]);
166 const uint64_t K6 = whirl(K[6], K[5], K[4], K[3], K[2], K[1], K[0], K[7]);
167 const uint64_t K7 = whirl(K[7], K[6], K[5], K[4], K[3], K[2], K[1], K[0]);
168
169 const uint64_t T0 = whirl(B0, B7, B6, B5, B4, B3, B2, B1) ^ K0;
170 const uint64_t T1 = whirl(B1, B0, B7, B6, B5, B4, B3, B2) ^ K1;
171 const uint64_t T2 = whirl(B2, B1, B0, B7, B6, B5, B4, B3) ^ K2;
172 const uint64_t T3 = whirl(B3, B2, B1, B0, B7, B6, B5, B4) ^ K3;
173 const uint64_t T4 = whirl(B4, B3, B2, B1, B0, B7, B6, B5) ^ K4;
174 const uint64_t T5 = whirl(B5, B4, B3, B2, B1, B0, B7, B6) ^ K5;
175 const uint64_t T6 = whirl(B6, B5, B4, B3, B2, B1, B0, B7) ^ K6;
176 const uint64_t T7 = whirl(B7, B6, B5, B4, B3, B2, B1, B0) ^ K7;
177
178 K[0] = K0;
179 K[1] = K1;
180 K[2] = K2;
181 K[3] = K3;
182 K[4] = K4;
183 K[5] = K5;
184 K[6] = K6;
185 K[7] = K7;
186
187 B0 = T0;
188 B1 = T1;
189 B2 = T2;
190 B3 = T3;
191 B4 = T4;
192 B5 = T5;
193 B6 = T6;
194 B7 = T7;
195 }
196
197 digest[0] ^= B0 ^ M[0];
198 digest[1] ^= B1 ^ M[1];
199 digest[2] ^= B2 ^ M[2];
200 digest[3] ^= B3 ^ M[3];
201 digest[4] ^= B4 ^ M[4];
202 digest[5] ^= B5 ^ M[5];
203 digest[6] ^= B6 ^ M[6];
204 digest[7] ^= B7 ^ M[7];
205 }
206}
207
209 digest.resize(8);
210 zeroise(digest);
211}
212
213std::unique_ptr<HashFunction> Whirlpool::new_object() const {
214 return std::make_unique<Whirlpool>();
215}
216
217std::unique_ptr<HashFunction> Whirlpool::copy_state() const {
218 return std::make_unique<Whirlpool>(*this);
219}
220
221void Whirlpool::add_data(std::span<const uint8_t> input) {
222 m_md.update(input);
223}
224
225void Whirlpool::final_result(std::span<uint8_t> output) {
226 m_md.final(output);
227}
228
229} // namespace Botan
std::span< const uint8_t > take(const size_t count)
static std::optional< std::string > check(CPUID::Feature feat)
Definition cpuid.h:66
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
std::string provider() const override
Definition whirlpool.cpp:96
std::unique_ptr< HashFunction > new_object() const override
static void compress_n(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static void init(digest_type &digest)
secure_vector< uint64_t > digest_type
Definition whirlpool.h:20
std::unique_ptr< HashFunction > copy_state() const override
static constexpr size_t block_bytes
Definition whirlpool.h:24
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:241
BOTAN_FORCE_INLINE constexpr T rotr(T input)
Definition rotate.h:35
constexpr T poly_mul(T x, uint8_t y)
Definition bit_ops.h:307
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:495
consteval std::array< uint64_t, 10 > whirlpool_rc() noexcept
consteval std::array< uint8_t, N > whirlpool_sbox() noexcept
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504