libxcks  0.1.0.1
whirlpool.hpp
Go to the documentation of this file.
1 /*
2  * libxcks
3  * Copyright (C) 2022 Julien Couot
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 #ifndef INC_WHIRLPOOL_HPP_6E1F0061_4A5D_41A1_A6BE_82379C71A72D
25 #define INC_WHIRLPOOL_HPP_6E1F0061_4A5D_41A1_A6BE_82379C71A72D
26 
27 //---------------------------------------------------------------------------
28 #include "checksumex.hpp"
29 //---------------------------------------------------------------------------
30 
31 
32 namespace libxcks
33 {
57 class Whirlpool final : public ChecksumEx
58 {
59  protected:
60  #define WHIRLPOOL_DIGESTBYTES 64
61  #define WHIRLPOOL_DIGESTBITS (8*WHIRLPOOL_DIGESTBYTES)
62  #define WHIRLPOOL_DIGESTQUADWORDS (WHIRLPOOL_DIGESTBYTES/8)
63 
64  #define WHIRLPOOL_WBLOCKBYTES 64
65  #define WHIRLPOOL_WBLOCKBITS (8*WHIRLPOOL_WBLOCKBYTES)
66 
67  #define WHIRLPOOL_LENGTHBYTES 32
68  #define WHIRLPOOL_LENGTHBITS (8*WHIRLPOOL_LENGTHBYTES)
69 
72  int bufferBits;
73  int bufferPos;
75 
76 
77  static const uint64_t C0[];
78  static const uint64_t C1[];
79  static const uint64_t C2[];
80  static const uint64_t C3[];
81  static const uint64_t C4[];
82  static const uint64_t C5[];
83  static const uint64_t C6[];
84  static const uint64_t C7[];
85 
87  static const unsigned int R;
88  static const uint64_t rc[];
89 
90  public:
94  Whirlpool();
95 
99  void reset() override;
100 
111  uint8_t* getValue(uint8_t* buffer) const override;
112 
120  size_t getSize() const override { return WHIRLPOOL_DIGESTBYTES; }
121 
128  void update(const uint8_t* buf, size_t len) override;
129 
130  protected:
137  void finish(uint8_t* const result);
138 
142  void process_buffer();
143 
144  public:
150  std::string getName() const override
151  {
152  return getHashName();
153  }
154 
164  ChecksumAlgoId getID() const override
165  {
166  return getIdentifier();
167  }
168 
174  static std::string getHashName()
175  {
176  return "Whirlpool";
177  }
178 
188  static constexpr ChecksumAlgoId getIdentifier()
189  {
190  return ChecksumAlgoId::Whirlpool;
191  }
192 
202  {
203  return new Whirlpool();
204  }
205 };
206 //---------------------------------------------------------------------------
207 } // namespace libxcks
208 //---------------------------------------------------------------------------
209 
210 #endif // INC_WHIRLPOOL_HPP_6E1F0061_4A5D_41A1_A6BE_82379C71A72D
Add some utilities to Checksum class.
Computes a hash from a byte stream.
Definition: checksumex.hpp:72
Computes a checksum from a byte stream.
Definition: checksum.hpp:54
Computes the Whirlpool hash from a byte stream.
Definition: whirlpool.hpp:58
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: whirlpool.hpp:188
static const unsigned int R
The number of rounds of the internal dedicated block cipher.
Definition: whirlpool.hpp:87
int bufferBits
Current number of bits on the buffer.
Definition: whirlpool.hpp:72
uint8_t bitLength[WHIRLPOOL_LENGTHBYTES]
Global number of hashed bits (256-bit counter).
Definition: whirlpool.hpp:70
size_t getSize() const override
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: whirlpool.hpp:120
void update(const uint8_t *buf, size_t len) override
Updates the Whirlpool hash with specified array of bytes.
Definition: whirlpool.cpp:687
static const uint64_t rc[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:88
static const uint64_t C0[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:77
static const uint64_t C5[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:82
Whirlpool()
Default constructor.
Definition: whirlpool.cpp:600
uint8_t buffer[WHIRLPOOL_WBLOCKBYTES]
Buffer of data to hash.
Definition: whirlpool.hpp:71
uint8_t * getValue(uint8_t *buffer) const override
Returns the Whirlpool hash value in the first 64 bytes of the given address.
Definition: whirlpool.cpp:1020
void process_buffer()
The core Whirlpool transform.
Definition: whirlpool.cpp:796
static const uint64_t C4[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:81
static const uint64_t C6[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:83
std::string getName() const override
Returns the name of the checksum or the hash algorithm.
Definition: whirlpool.hpp:150
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: whirlpool.hpp:174
void reset() override
Resets the Whirlpool hash to initial state of computation.
Definition: whirlpool.cpp:610
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: whirlpool.hpp:201
uint64_t hash[WHIRLPOOL_DIGESTQUADWORDS]
The hashing state.
Definition: whirlpool.hpp:74
static const uint64_t C3[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:80
static const uint64_t C7[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:84
void finish(uint8_t *const result)
Process the remaining bytes in the internal buffer and the usual prolog according to the standard.
Definition: whirlpool.cpp:628
static const uint64_t C1[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:78
static const uint64_t C2[]
Table used to compute the Whirlpool hash.
Definition: whirlpool.hpp:79
ChecksumAlgoId getID() const override
Returns an unique identifier for the checksum or the hash algorithm.
Definition: whirlpool.hpp:164
int bufferPos
Current (possibly incomplete) byte slot on the buffer.
Definition: whirlpool.hpp:73
ChecksumAlgoId
Ids of algorithms of checksums.
Definition: types.hpp:65
#define WHIRLPOOL_DIGESTBYTES
Size of the message digest in bytes.
Definition: whirlpool.hpp:60
#define WHIRLPOOL_DIGESTQUADWORDS
Size of the message digest in quad words.
Definition: whirlpool.hpp:62
#define WHIRLPOOL_LENGTHBYTES
Number of hashed bytes.
Definition: whirlpool.hpp:67
#define WHIRLPOOL_WBLOCKBYTES
Size of the buffer of data for hashing in bytes.
Definition: whirlpool.hpp:64