Botan 3.14.1
Crypto and TLS for C&
whirlpool_consts.h
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_WHIRLPOOL_CONSTS_H_
8#define BOTAN_WHIRLPOOL_CONSTS_H_
9
10#include <botan/types.h>
11#include <botan/internal/loadstor.h>
12#include <array>
13
14namespace Botan {
15
16// Derive the 256-byte S-box from the Whirlpool E and R mini-boxes
17template <size_t N = 256>
18consteval std::array<uint8_t, N> whirlpool_sbox() noexcept {
19 static_assert(N == 256 || N == 80);
20
21 constexpr uint8_t Ebox[16] = {1, 11, 9, 12, 13, 6, 15, 3, 14, 8, 7, 4, 10, 2, 5, 0};
22 constexpr uint8_t Rbox[16] = {7, 12, 11, 13, 14, 4, 9, 15, 6, 3, 8, 10, 2, 5, 1, 0};
23
24 // Derive the inverse of the E table
25 uint8_t Eibox[16] = {};
26 for(size_t i = 0; i != 16; ++i) {
27 Eibox[Ebox[i]] = static_cast<uint8_t>(i);
28 }
29
30 std::array<uint8_t, N> S = {};
31 for(size_t i = 0; i != N; ++i) {
32 const uint8_t L = Ebox[i >> 4];
33 const uint8_t R = Eibox[i & 0x0F];
34 const uint8_t T = Rbox[L ^ R];
35 S[i] = static_cast<uint8_t>((Ebox[L ^ T] << 4) | Eibox[R ^ T]);
36 }
37 return S;
38}
39
40// Round constants are from the first 64 elements of the sbox
41template <bool LittleEndian = false>
42consteval std::array<uint64_t, 10> whirlpool_rc() noexcept {
43 constexpr auto S80 = whirlpool_sbox<80>();
44 std::array<uint64_t, 10> RC = {};
45 for(size_t r = 0; r != 10; ++r) {
46 if(LittleEndian) {
47 RC[r] = load_le<uint64_t>(S80.data(), r);
48 } else {
49 RC[r] = load_be<uint64_t>(S80.data(), r);
50 }
51 }
52 return RC;
53}
54
55} // namespace Botan
56
57#endif
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