Botan 3.11.0
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/internal/bit_ops.h>
11#include <botan/internal/buffer_slicer.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/rotate.h>
14#include <array>
15
16namespace Botan {
17
18namespace {
19
20// Derive the 256-byte S-box from the Whirlpool E and R mini-boxes
21consteval std::array<uint8_t, 256> whirlpool_sbox() noexcept {
22 constexpr uint8_t Ebox[16] = {1, 11, 9, 12, 13, 6, 15, 3, 14, 8, 7, 4, 10, 2, 5, 0};
23 constexpr uint8_t Rbox[16] = {7, 12, 11, 13, 14, 4, 9, 15, 6, 3, 8, 10, 2, 5, 1, 0};
24
25 // Derive the inverse of the E table
26 uint8_t Eibox[16] = {};
27 for(size_t i = 0; i != 16; ++i) {
28 Eibox[Ebox[i]] = static_cast<uint8_t>(i);
29 }
30
31 std::array<uint8_t, 256> S = {};
32 for(size_t i = 0; i != 256; ++i) {
33 const uint8_t L = Ebox[i >> 4];
34 const uint8_t R = Eibox[i & 0x0F];
35 const uint8_t T = Rbox[L ^ R];
36 S[i] = static_cast<uint8_t>((Ebox[L ^ T] << 4) | Eibox[R ^ T]);
37 }
38 return S;
39}
40
41// Combined S-box + MDS diffusion table
42consteval std::array<uint64_t, 256> whirlpool_T_table(const std::array<uint8_t, 256>& S) noexcept {
43 // MDS circulant matrix first row: [1, 1, 4, 1, 8, 5, 2, 9] over GF(2^8)
44 constexpr uint64_t MDS = 0x0101040108050209;
45
46 std::array<uint64_t, 256> T = {};
47 for(size_t i = 0; i != 256; ++i) {
48 T[i] = poly_mul<0x1D>(MDS, S[i]);
49 }
50 return T;
51}
52
53// Round constants are from the first 64 elements of the sbox
54consteval std::array<uint64_t, 10> whirlpool_rc(const std::array<uint8_t, 256>& S) noexcept {
55 std::array<uint64_t, 10> RC = {};
56 for(size_t r = 0; r != 10; ++r) {
57 RC[r] = load_be<uint64_t>(S.data(), r);
58 }
59 return RC;
60}
61
62constexpr auto WHIRL_S = whirlpool_sbox();
63alignas(256) constexpr auto WHIRL_T = whirlpool_T_table(WHIRL_S);
64constexpr auto WHIRL_RC = whirlpool_rc(WHIRL_S);
65
66uint64_t whirl(uint64_t x0, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5, uint64_t x6, uint64_t x7) {
67 const uint64_t s0 = WHIRL_T[get_byte<0>(x0)];
68 const uint64_t s1 = WHIRL_T[get_byte<1>(x1)];
69 const uint64_t s2 = WHIRL_T[get_byte<2>(x2)];
70 const uint64_t s3 = WHIRL_T[get_byte<3>(x3)];
71 const uint64_t s4 = WHIRL_T[get_byte<4>(x4)];
72 const uint64_t s5 = WHIRL_T[get_byte<5>(x5)];
73 const uint64_t s6 = WHIRL_T[get_byte<6>(x6)];
74 const uint64_t s7 = WHIRL_T[get_byte<7>(x7)];
75
76 return s0 ^ rotr<8>(s1) ^ rotr<16>(s2) ^ rotr<24>(s3) ^ rotr<32>(s4) ^ rotr<40>(s5) ^ rotr<48>(s6) ^ rotr<56>(s7);
77}
78
79} // namespace
80
81/*
82* Whirlpool Compression Function
83*/
84void Whirlpool::compress_n(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
85 BufferSlicer in(input);
86
87 for(size_t i = 0; i != blocks; ++i) {
88 const auto block = in.take(block_bytes);
89
90 uint64_t K[11 * 8] = {0};
91
92 K[0] = digest[0];
93 K[1] = digest[1];
94 K[2] = digest[2];
95 K[3] = digest[3];
96 K[4] = digest[4];
97 K[5] = digest[5];
98 K[6] = digest[6];
99 K[7] = digest[7];
100
101 // Whirlpool key schedule:
102 for(size_t r = 1; r != 11; ++r) {
103 const uint64_t PK0 = K[8 * (r - 1) + 0];
104 const uint64_t PK1 = K[8 * (r - 1) + 1];
105 const uint64_t PK2 = K[8 * (r - 1) + 2];
106 const uint64_t PK3 = K[8 * (r - 1) + 3];
107 const uint64_t PK4 = K[8 * (r - 1) + 4];
108 const uint64_t PK5 = K[8 * (r - 1) + 5];
109 const uint64_t PK6 = K[8 * (r - 1) + 6];
110 const uint64_t PK7 = K[8 * (r - 1) + 7];
111
112 K[8 * r + 0] = whirl(PK0, PK7, PK6, PK5, PK4, PK3, PK2, PK1) ^ WHIRL_RC[r - 1];
113 K[8 * r + 1] = whirl(PK1, PK0, PK7, PK6, PK5, PK4, PK3, PK2);
114 K[8 * r + 2] = whirl(PK2, PK1, PK0, PK7, PK6, PK5, PK4, PK3);
115 K[8 * r + 3] = whirl(PK3, PK2, PK1, PK0, PK7, PK6, PK5, PK4);
116 K[8 * r + 4] = whirl(PK4, PK3, PK2, PK1, PK0, PK7, PK6, PK5);
117 K[8 * r + 5] = whirl(PK5, PK4, PK3, PK2, PK1, PK0, PK7, PK6);
118 K[8 * r + 6] = whirl(PK6, PK5, PK4, PK3, PK2, PK1, PK0, PK7);
119 K[8 * r + 7] = whirl(PK7, PK6, PK5, PK4, PK3, PK2, PK1, PK0);
120 }
121
122 uint64_t M[8] = {0};
123 load_be(M, block.data(), 8);
124
125 // First round (key masking)
126 uint64_t B0 = M[0] ^ K[0];
127 uint64_t B1 = M[1] ^ K[1];
128 uint64_t B2 = M[2] ^ K[2];
129 uint64_t B3 = M[3] ^ K[3];
130 uint64_t B4 = M[4] ^ K[4];
131 uint64_t B5 = M[5] ^ K[5];
132 uint64_t B6 = M[6] ^ K[6];
133 uint64_t B7 = M[7] ^ K[7];
134
135 for(size_t r = 1; r != 11; ++r) {
136 const uint64_t T0 = whirl(B0, B7, B6, B5, B4, B3, B2, B1) ^ K[8 * r + 0];
137 const uint64_t T1 = whirl(B1, B0, B7, B6, B5, B4, B3, B2) ^ K[8 * r + 1];
138 const uint64_t T2 = whirl(B2, B1, B0, B7, B6, B5, B4, B3) ^ K[8 * r + 2];
139 const uint64_t T3 = whirl(B3, B2, B1, B0, B7, B6, B5, B4) ^ K[8 * r + 3];
140 const uint64_t T4 = whirl(B4, B3, B2, B1, B0, B7, B6, B5) ^ K[8 * r + 4];
141 const uint64_t T5 = whirl(B5, B4, B3, B2, B1, B0, B7, B6) ^ K[8 * r + 5];
142 const uint64_t T6 = whirl(B6, B5, B4, B3, B2, B1, B0, B7) ^ K[8 * r + 6];
143 const uint64_t T7 = whirl(B7, B6, B5, B4, B3, B2, B1, B0) ^ K[8 * r + 7];
144
145 B0 = T0;
146 B1 = T1;
147 B2 = T2;
148 B3 = T3;
149 B4 = T4;
150 B5 = T5;
151 B6 = T6;
152 B7 = T7;
153 }
154
155 digest[0] ^= B0 ^ M[0];
156 digest[1] ^= B1 ^ M[1];
157 digest[2] ^= B2 ^ M[2];
158 digest[3] ^= B3 ^ M[3];
159 digest[4] ^= B4 ^ M[4];
160 digest[5] ^= B5 ^ M[5];
161 digest[6] ^= B6 ^ M[6];
162 digest[7] ^= B7 ^ M[7];
163 }
164}
165
167 digest.resize(8);
168 zeroise(digest);
169}
170
171std::unique_ptr<HashFunction> Whirlpool::new_object() const {
172 return std::make_unique<Whirlpool>();
173}
174
175std::unique_ptr<HashFunction> Whirlpool::copy_state() const {
176 return std::make_unique<Whirlpool>(*this);
177}
178
179void Whirlpool::add_data(std::span<const uint8_t> input) {
180 m_md.update(input);
181}
182
183void Whirlpool::final_result(std::span<uint8_t> output) {
184 m_md.final(output);
185}
186
187} // namespace Botan
std::span< const uint8_t > take(const size_t count)
std::unique_ptr< HashFunction > new_object() const override
static void compress_n(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition whirlpool.cpp:84
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
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:124
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:306
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504