libxcks  0.1.0.1
keccak.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_KECCAK_HPP_6A4377C8_9736_4336_BCAB_C8D49C398CA7
25 #define INC_KECCAK_HPP_6A4377C8_9736_4336_BCAB_C8D49C398CA7
26 
27 //---------------------------------------------------------------------------
28 #include "checksumex.hpp"
29 //---------------------------------------------------------------------------
30 
31 
32 namespace libxcks
33 {
44 {
45  protected:
47  static constexpr unsigned SHA3_KECCAK_SPONGE_WORDS = (1600 / 8) / sizeof(uint64_t);
48 
49  uint64_t saved;
51  unsigned byteIndex;
52  unsigned wordIndex;
53  const unsigned capacityWords;
54  const bool useSHA3Hash;
55 
56  public:
60  AbstractKeccakImpl() = delete;
61 
70  AbstractKeccakImpl(const unsigned size, const bool isSHA3Hash = true);
71 
75  void reset() override final;
76 
83  void update(const uint8_t* buf, size_t len) override final;
84 
85  protected:
90  void finish();
91 
92  private:
96  void transform();
97 
105  static inline constexpr uint64_t rol(uint64_t x, int n)
106  {
107  return ((x << n) | (x >> (64 - n)));
108  }
109 };
110 //---------------------------------------------------------------------------
111 
112 
134 class SHA3_224 final : public AbstractKeccakImpl
135 {
136  public:
141 
150  uint8_t* getValue(uint8_t* buffer) const override;
151 
159  size_t getSize() const override final { return 28; }
160 
166  std::string getName() const override final
167  {
168  return getHashName();
169  }
170 
176  ArrayString getAltNames() const override final
177  {
178  return getAlternativeNames();
179  }
180 
190  ChecksumAlgoId getID() const override final
191  {
192  return getIdentifier();
193  }
194 
200  static std::string getHashName()
201  {
202  return "SHA3-224";
203  }
204 
211 
221  static constexpr ChecksumAlgoId getIdentifier()
222  {
223  return ChecksumAlgoId::SHA3_224;
224  }
225 
235  {
236  return new SHA3_224();
237  }
238 };
239 //---------------------------------------------------------------------------
240 
241 
263 class SHA3_256 final : public AbstractKeccakImpl
264 {
265  public:
270 
279  uint8_t* getValue(uint8_t* buffer) const override;
280 
288  size_t getSize() const override final { return 32; }
289 
295  std::string getName() const override final
296  {
297  return getHashName();
298  }
299 
305  ArrayString getAltNames() const override final
306  {
307  return getAlternativeNames();
308  }
309 
319  ChecksumAlgoId getID() const override final
320  {
321  return getIdentifier();
322  }
323 
329  static std::string getHashName()
330  {
331  return "SHA3-256";
332  }
333 
340 
350  static constexpr ChecksumAlgoId getIdentifier()
351  {
352  return ChecksumAlgoId::SHA3_256;
353  }
354 
364  {
365  return new SHA3_256();
366  }
367 };
368 //---------------------------------------------------------------------------
369 
370 
392 class SHA3_384 final : public AbstractKeccakImpl
393 {
394  public:
399 
408  uint8_t* getValue(uint8_t* buffer) const override final;
409 
417  size_t getSize() const override final { return 48; }
418 
424  std::string getName() const override final
425  {
426  return getHashName();
427  }
428 
434  ArrayString getAltNames() const override final
435  {
436  return getAlternativeNames();
437  }
438 
448  ChecksumAlgoId getID() const override final
449  {
450  return getIdentifier();
451  }
452 
458  static std::string getHashName()
459  {
460  return "SHA3-384";
461  }
462 
469 
479  static constexpr ChecksumAlgoId getIdentifier()
480  {
481  return ChecksumAlgoId::SHA3_384;
482  }
483 
493  {
494  return new SHA3_384();
495  }
496 };
497 //---------------------------------------------------------------------------
498 
499 
521 class SHA3_512 final : public AbstractKeccakImpl
522 {
523  public:
528 
537  uint8_t* getValue(uint8_t* buffer) const override final;
538 
546  size_t getSize() const override final { return 64; }
547 
553  std::string getName() const override final
554  {
555  return getHashName();
556  }
557 
563  ArrayString getAltNames() const override final
564  {
565  return getAlternativeNames();
566  }
567 
577  ChecksumAlgoId getID() const override final
578  {
579  return getIdentifier();
580  }
581 
587  static std::string getHashName()
588  {
589  return "SHA3-512";
590  }
591 
598 
608  static constexpr ChecksumAlgoId getIdentifier()
609  {
610  return ChecksumAlgoId::SHA3_512;
611  }
612 
622  {
623  return new SHA3_512();
624  }
625 };
626 //---------------------------------------------------------------------------
627 
628 
629 #if 0
651 class Keccak224 final : public AbstractKeccakImpl
652 {
653  public:
657  Keccak224() : AbstractKeccakImpl(Keccak224::getSize(), false) {}
658 
667  uint8_t* getValue(uint8_t* buffer) const override final;
668 
676  size_t getSize() const override final { return 28; }
677 
683  std::string getName() const override final
684  {
685  return getHashName();
686  }
687 
693  ArrayString getAltNames() const override final
694  {
695  return getAlternativeNames();
696  }
697 
707  ChecksumAlgoId getID() const override final
708  {
709  return getIdentifier();
710  }
711 
717  static std::string getHashName()
718  {
719  return "Keccak224";
720  }
721 
727  static ArrayString getAlternativeNames();
728 
738  static constexpr ChecksumAlgoId getIdentifier()
739  {
740  return ChecksumAlgoId::Keccak224;
741  }
742 
751  static Checksum* getNewInstance()
752  {
753  return new Keccak224();
754  }
755 };
756 //---------------------------------------------------------------------------
757 
758 
780 class Keccak256 final : public AbstractKeccakImpl
781 {
782  public:
786  Keccak256() : AbstractKeccakImpl(Keccak256::getSize(), false) {}
787 
796  uint8_t* getValue(uint8_t* buffer) const override;
797 
805  size_t getSize() const override final { return 32; }
806 
812  std::string getName() const override final
813  {
814  return getHashName();
815  }
816 
822  ArrayString getAltNames() const override final
823  {
824  return getAlternativeNames();
825  }
826 
836  ChecksumAlgoId getID() const override final
837  {
838  return getIdentifier();
839  }
840 
846  static std::string getHashName()
847  {
848  return "Keccak256";
849  }
850 
856  static ArrayString getAlternativeNames();
857 
867  static constexpr ChecksumAlgoId getIdentifier()
868  {
869  return ChecksumAlgoId::Keccak256;
870  }
871 
880  static Checksum* getNewInstance()
881  {
882  return new Keccak256();
883  }
884 };
885 //---------------------------------------------------------------------------
886 
887 
909 class Keccak384 final : public AbstractKeccakImpl
910 {
911  public:
915  Keccak384() : AbstractKeccakImpl(Keccak384::getSize(), false) {}
916 
925  uint8_t* getValue(uint8_t* buffer) const override;
926 
934  size_t getSize() const override final { return 48; }
935 
941  std::string getName() const override final
942  {
943  return getHashName();
944  }
945 
951  ArrayString getAltNames() const override final
952  {
953  return getAlternativeNames();
954  }
955 
965  ChecksumAlgoId getID() const override final
966  {
967  return getIdentifier();
968  }
969 
975  static std::string getHashName()
976  {
977  return "Keccak384";
978  }
979 
985  static ArrayString getAlternativeNames();
986 
996  static constexpr ChecksumAlgoId getIdentifier()
997  {
998  return ChecksumAlgoId::Keccak384;
999  }
1000 
1009  static Checksum* getNewInstance()
1010  {
1011  return new Keccak384();
1012  }
1013 };
1014 //---------------------------------------------------------------------------
1015 
1016 
1038 class Keccak512 final : public AbstractKeccakImpl
1039 {
1040  public:
1044  Keccak512() : AbstractKeccakImpl(Keccak512::getSize(), false) {}
1045 
1054  uint8_t* getValue(uint8_t* buffer) const override final;
1055 
1063  size_t getSize() const override final { return 64; }
1064 
1070  std::string getName() const override final
1071  {
1072  return getHashName();
1073  }
1074 
1080  ArrayString getAltNames() const override final
1081  {
1082  return getAlternativeNames();
1083  }
1084 
1094  ChecksumAlgoId getID() const override final
1095  {
1096  return getIdentifier();
1097  }
1098 
1104  static std::string getHashName()
1105  {
1106  return "Keccak512";
1107  }
1108 
1114  static ArrayString getAlternativeNames();
1115 
1125  static constexpr ChecksumAlgoId getIdentifier()
1126  {
1127  return ChecksumAlgoId::Keccak512;
1128  }
1129 
1138  static Checksum* getNewInstance()
1139  {
1140  return new Keccak512();
1141  }
1142 };
1143 //---------------------------------------------------------------------------
1144 #endif
1145 } // namespace libxcks
1146 //---------------------------------------------------------------------------
1147 
1148 #endif // INC_KECCAK_HPP_6A4377C8_9736_4336_BCAB_C8D49C398CA7
Add some utilities to Checksum class.
Computes the Keccak hash from a byte stream.
Definition: keccak.hpp:44
void update(const uint8_t *buf, size_t len) override final
Updates the Keccak hash with specified array of bytes.
Definition: keccak.cpp:69
const bool useSHA3Hash
true if result is SHA3 hash, false if "original" Keccak hash.
Definition: keccak.hpp:54
unsigned byteIndex
0..7–the next byte after the set one (starts from 0; 0–none are buffered).
Definition: keccak.hpp:51
AbstractKeccakImpl()=delete
Disable default constructor.
unsigned wordIndex
0..24–the next word to integrate input (starts from 0).
Definition: keccak.hpp:52
void reset() override final
Resets the Keccak hash to initial value.
Definition: keccak.cpp:56
uint64_t state[SHA3_KECCAK_SPONGE_WORDS]
Keccak's state in 'words'.
Definition: keccak.hpp:50
void finish()
Process the remaining bytes in the internal buffer and the usual prolog according to the standard.
Definition: keccak.cpp:220
static constexpr unsigned SHA3_KECCAK_SPONGE_WORDS
'Words' here refers to uint64_t
Definition: keccak.hpp:47
uint64_t saved
The portion of the input message that we didn't consume yet.
Definition: keccak.hpp:49
const unsigned capacityWords
The double size of the hash output in words (e.g. 16 for Keccak 512).
Definition: keccak.hpp:53
Computes a hash from a byte stream.
Definition: checksumex.hpp:72
Computes a checksum from a byte stream.
Definition: checksum.hpp:54
Computes the SHA3-224 hash from a byte stream.
Definition: keccak.hpp:135
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: keccak.hpp:221
ChecksumAlgoId getID() const override final
Returns an unique identifier for the checksum or the hash algorithm.
Definition: keccak.hpp:190
SHA3_224()
Default constructor.
Definition: keccak.hpp:140
size_t getSize() const override final
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: keccak.hpp:159
uint8_t * getValue(uint8_t *buffer) const override
Returns the SHA3-224 hash value in the first 28 bytes of the given address.
Definition: keccak.cpp:260
static ArrayString getAlternativeNames()
Returns the alternative names of the SHA3-224 hash algorithm.
Definition: keccak.cpp:275
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: keccak.hpp:200
ArrayString getAltNames() const override final
Returns the alternative names of the SHA3-224 hash algorithm.
Definition: keccak.hpp:176
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: keccak.hpp:234
std::string getName() const override final
Returns the name of the checksum or the hash algorithm.
Definition: keccak.hpp:166
Computes the SHA3-256 hash from a byte stream.
Definition: keccak.hpp:264
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: keccak.hpp:329
static ArrayString getAlternativeNames()
Returns the alternative names of the SHA3-256 hash algorithm.
Definition: keccak.cpp:305
SHA3_256()
Default constructor.
Definition: keccak.hpp:269
std::string getName() const override final
Returns the name of the checksum or the hash algorithm.
Definition: keccak.hpp:295
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: keccak.hpp:363
ChecksumAlgoId getID() const override final
Returns an unique identifier for the checksum or the hash algorithm.
Definition: keccak.hpp:319
size_t getSize() const override final
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: keccak.hpp:288
ArrayString getAltNames() const override final
Returns the alternative names of the SHA3-256 hash algorithm.
Definition: keccak.hpp:305
uint8_t * getValue(uint8_t *buffer) const override
Returns the SHA3-256 hash value in the first 32 bytes of the given address.
Definition: keccak.cpp:290
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: keccak.hpp:350
Computes the SHA3-384 hash from a byte stream.
Definition: keccak.hpp:393
ChecksumAlgoId getID() const override final
Returns an unique identifier for the checksum or the hash algorithm.
Definition: keccak.hpp:448
ArrayString getAltNames() const override final
Returns the alternative names of the SHA3-384 hash algorithm.
Definition: keccak.hpp:434
size_t getSize() const override final
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: keccak.hpp:417
SHA3_384()
Default constructor.
Definition: keccak.hpp:398
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: keccak.hpp:492
uint8_t * getValue(uint8_t *buffer) const override final
Returns the SHA3-384 hash value in the first 48 bytes of the given address.
Definition: keccak.cpp:320
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: keccak.hpp:479
static ArrayString getAlternativeNames()
Returns the alternative names of the SHA3-384 hash algorithm.
Definition: keccak.cpp:335
std::string getName() const override final
Returns the name of the checksum or the hash algorithm.
Definition: keccak.hpp:424
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: keccak.hpp:458
Computes the SHA3-512 hash from a byte stream.
Definition: keccak.hpp:522
std::string getName() const override final
Returns the name of the checksum or the hash algorithm.
Definition: keccak.hpp:553
ChecksumAlgoId getID() const override final
Returns an unique identifier for the checksum or the hash algorithm.
Definition: keccak.hpp:577
ArrayString getAltNames() const override final
Returns the alternative names of the SHA3-512 hash algorithm.
Definition: keccak.hpp:563
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: keccak.hpp:621
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: keccak.hpp:587
static ArrayString getAlternativeNames()
Returns the alternative names of the SHA3-512 hash algorithm.
Definition: keccak.cpp:365
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: keccak.hpp:608
uint8_t * getValue(uint8_t *buffer) const override final
Returns the SHA3-512 hash value in the first 64 bytes of the given address.
Definition: keccak.cpp:350
SHA3_512()
Default constructor.
Definition: keccak.hpp:527
size_t getSize() const override final
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: keccak.hpp:546
ChecksumAlgoId
Ids of algorithms of checksums.
Definition: types.hpp:65
std::vector< std::string > ArrayString
Array of strings.
Definition: types.hpp:44