libxcks  0.1.0.1
sm3.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_SM3_HPP_8F10C8B6_226C_4673_BDD5_B1BA3F7C180C
25 #define INC_SM3_HPP_8F10C8B6_226C_4673_BDD5_B1BA3F7C180C
26 
27 //---------------------------------------------------------------------------
28 #include "checksumex.hpp"
29 //---------------------------------------------------------------------------
30 
31 
32 namespace libxcks
33 {
56 class SM3 final : public ChecksumEx
57 {
59  private:
60  static constexpr size_t SM3_DIGEST_SIZE = 32;
61  static constexpr size_t SM3_BLOCK_SIZE = 64;
62 
63  protected:
64  // State of computation between the single steps.
65  uint32_t digest[SM3_DIGEST_SIZE / sizeof(uint32_t)];
66  uint32_t workspace[SM3_BLOCK_SIZE / sizeof(uint32_t)];
67  uint32_t workspace_used;
68  uint32_t processed_high;
69  uint32_t processed_low;
70 
71  private:
72  static constexpr uint32_t SM3_PAD_SIZE = 56;
73  static constexpr uint32_t T0 = 0x79cc4519u;
74  static constexpr uint32_t T1 = 0x7a879d8au;
75 
76  static inline uint32_t FF0(const uint32_t x, const uint32_t y, const uint32_t z) { return (( x ) ^ ( y ) ^ ( z )); }
77  static inline uint32_t FF1(const uint32_t x, const uint32_t y, const uint32_t z) { return ((( x ) & ( y )) | (( x ) & ( z )) | (( y ) & ( z ))); }
78  static inline uint32_t GG0(const uint32_t x, const uint32_t y, const uint32_t z) { return (( x ) ^ ( y ) ^ ( z )); }
79  static inline uint32_t GG1(const uint32_t x, const uint32_t y, const uint32_t z) { return ((( x ) & ( y )) | (( ~x ) & ( z ))); }
80 
81  static inline uint32_t rotleft(const uint32_t x, const uint32_t y)
82  {
83  return (x << y) | (x >> (32 - (y & 0x3F)));
84  }
85 
86  static inline uint32_t p0(const uint32_t x)
87  {
88  return x ^ rotleft(x, 9) ^ rotleft(x, 17);
89  }
90 
91  static inline uint32_t p1(const uint32_t x)
92  {
93  return (x ^ rotleft(x, 15) ^ rotleft(x, 23));
94  }
95 
96  static inline uint32_t min(const uint32_t x, const uint32_t y)
97  {
98  return (x > y) ? y : x;
99  }
100 
101  static inline uint32_t swap(const uint32_t x)
102  {
103  return ((x & 0xff000000) >> 24) | ((x & 0x00ff0000) >> 8) | ((x & 0x0000ff00) << 8) | (x << 24);
104  }
105 
106  static inline void addwc(uint32_t* hi, uint32_t* lo, const uint32_t val)
107  {
108  *lo += val;
109  *hi += *lo < val;
110  }
111 
112  static void transformBlock(uint32_t* digest, uint8_t* input);
113  void finalize(uint8_t* output);
114 
115  protected:
117 
118  public:
122  SM3();
123 
127  void reset() override;
128 
137  uint8_t* getValue(uint8_t* buffer) const override;
138 
146  size_t getSize() const override { return SM3_DIGEST_SIZE; }
147 
154  void update(const uint8_t* buf, size_t len) override;
155 
156  public:
162  std::string getName() const override
163  {
164  return getHashName();
165  }
166 
176  ChecksumAlgoId getID() const override
177  {
178  return getIdentifier();
179  }
180 
186  static std::string getHashName()
187  {
188  return "SM3";
189  }
190 
200  static constexpr ChecksumAlgoId getIdentifier()
201  {
202  return ChecksumAlgoId::SM3;
203  }
204 
214  {
215  return new SM3();
216  }
217 };
218 //---------------------------------------------------------------------------
219 } // namespace libxcks
220 //---------------------------------------------------------------------------
221 
222 #endif // INC_SM3_HPP_8F10C8B6_226C_4673_BDD5_B1BA3F7C180C
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 SM3 hash from a byte stream.
Definition: sm3.hpp:57
void reset() override
Resets the SM3 hash to initial state of computation.
size_t getSize() const override
Returns the minimal size to allocate in memory to store the hash with the getValue(buffer) method.
Definition: sm3.hpp:146
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the hash algorithm.
Definition: sm3.hpp:200
std::string getName() const override
Returns the name of the checksum or the hash algorithm.
Definition: sm3.hpp:162
static std::string getHashName()
Returns the name of the hash algorithm.
Definition: sm3.hpp:186
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: sm3.hpp:213
void update(const uint8_t *buf, size_t len) override
Updates the SM3 hash with specified array of bytes.
SM3()
Default constructor.
ChecksumAlgoId getID() const override
Returns an unique identifier for the checksum or the hash algorithm.
Definition: sm3.hpp:176
uint8_t * getValue(uint8_t *buffer) const override
Returns the SM3 hash value in the first 32 bytes of the given address.
ChecksumAlgoId
Ids of algorithms of checksums.
Definition: types.hpp:65