Crypto++  8.6
Free C++ class library of cryptographic schemes
sm3.cpp
1 // sm3.cpp - written and placed in the public domain by Jeffrey Walton and Han Lulu
2 // Based on the specification provided by Sean Shen and Xiaodong Lee.
3 // Based on code by Krzysztof Kwiatkowski and Jack Lloyd.
4 // Also see https://tools.ietf.org/html/draft-shen-sm3-hash.
5 //
6 // We understand future ARMv8 enhancements are supposed
7 // to include SM3 and SM4 related instructions so the function
8 // is stubbed for an eventual SM3_HashMultipleBlocks_ARMV8.
9 
10 #include "pch.h"
11 #include "config.h"
12 
13 #include "sm3.h"
14 #include "misc.h"
15 #include "cpu.h"
16 
17 ANONYMOUS_NAMESPACE_BEGIN
18 
19 using CryptoPP::byte;
20 using CryptoPP::word32;
22 
23 using CryptoPP::SM3;
24 using CryptoPP::GetBlock;
26 
27 inline word32 P0(word32 X)
28 {
29  return X ^ rotlConstant<9>(X) ^ rotlConstant<17>(X);
30 }
31 
32 inline word32 P1(word32 X)
33 {
34  return X ^ rotlConstant<15>(X) ^ rotlConstant<23>(X);
35 }
36 
37 inline word32 EE(word32 W0, word32 W7, word32 W13, word32 W3, word32 W10)
38 {
39  return P1(W0 ^ W7 ^ rotlConstant<15>(W13)) ^ rotlConstant<7>(W3) ^ W10;
40 }
41 
42 inline word32 FF(word32 X, word32 Y, word32 Z)
43 {
44  return (X & Y) | ((X | Y) & Z);
45 }
46 
47 inline word32 GG(word32 X, word32 Y, word32 Z)
48 {
49  return ((Z ^ (X & (Y ^ Z))));
50 }
51 
52 inline void R1(word32 A, word32& B, word32 C, word32& D, word32 E, word32& F,
53  word32 G, word32& H, word32 TJ, word32 Wi, word32 Wj)
54 {
55  const word32 A12 = rotlConstant<12>(A);
56  const word32 TT0 = rotlConstant<7>(A12 + E + TJ);
57  const word32 TT1 = (A ^ B ^ C) + D + (TT0 ^ A12) + Wj;
58  const word32 TT2 = (E ^ F ^ G) + H + TT0 + Wi;
59 
60  B = rotlConstant<9>(B); D = TT1;
61  F = rotlConstant<19>(F); H = P0(TT2);
62 }
63 
64 inline void R2(word32 A, word32& B, word32 C, word32& D, word32 E, word32& F,
65  word32 G, word32& H, word32 TJ, word32 Wi, word32 Wj)
66 {
67  const word32 A12 = rotlConstant<12>(A);
68  const word32 TT0 = rotlConstant<7>(A12 + E + TJ);
69  const word32 TT1 = FF(A, B, C) + D + (TT0 ^ A12) + Wj;
70  const word32 TT2 = GG(E, F, G) + H + TT0 + Wi;
71 
72  B = rotlConstant<9>(B); D = TT1;
73  F = rotlConstant<19>(F); H = P0(TT2);
74 }
75 
76 // Krzysztof Kwiatkowski did a very nice job with this function.
77 size_t SM3_HashMultipleBlocks_CXX(word32 *state, const word32 *data, size_t length)
78 {
79  CRYPTOPP_ASSERT(data);
80 
81  word32 A = state[0], B = state[1], C = state[2], D = state[3];
82  word32 E = state[4], F = state[5], G = state[6], H = state[7];
83 
84  while (length >= SM3::BLOCKSIZE)
85  {
86  // Reverse bytes on LittleEndian; align pointer on BigEndian
87  typedef GetBlock<word32, BigEndian, false> InBlock;
88  InBlock iblk(data);
89 
90  word32 W00, W01, W02, W03, W04, W05, W06, W07, W08, W09, W10, W11, W12, W13, W14, W15;
91  iblk(W00)(W01)(W02)(W03)(W04)(W05)(W06)(W07)(W08)(W09)(W10)(W11)(W12)(W13)(W14)(W15);
92 
93  R1(A, B, C, D, E, F, G, H, 0x79CC4519, W00, W00 ^ W04);
94  W00 = EE(W00, W07, W13, W03, W10);
95  R1(D, A, B, C, H, E, F, G, 0xF3988A32, W01, W01 ^ W05);
96  W01 = EE(W01, W08, W14, W04, W11);
97  R1(C, D, A, B, G, H, E, F, 0xE7311465, W02, W02 ^ W06);
98  W02 = EE(W02, W09, W15, W05, W12);
99  R1(B, C, D, A, F, G, H, E, 0xCE6228CB, W03, W03 ^ W07);
100  W03 = EE(W03, W10, W00, W06, W13);
101  R1(A, B, C, D, E, F, G, H, 0x9CC45197, W04, W04 ^ W08);
102  W04 = EE(W04, W11, W01, W07, W14);
103  R1(D, A, B, C, H, E, F, G, 0x3988A32F, W05, W05 ^ W09);
104  W05 = EE(W05, W12, W02, W08, W15);
105  R1(C, D, A, B, G, H, E, F, 0x7311465E, W06, W06 ^ W10);
106  W06 = EE(W06, W13, W03, W09, W00);
107  R1(B, C, D, A, F, G, H, E, 0xE6228CBC, W07, W07 ^ W11);
108  W07 = EE(W07, W14, W04, W10, W01);
109  R1(A, B, C, D, E, F, G, H, 0xCC451979, W08, W08 ^ W12);
110  W08 = EE(W08, W15, W05, W11, W02);
111  R1(D, A, B, C, H, E, F, G, 0x988A32F3, W09, W09 ^ W13);
112  W09 = EE(W09, W00, W06, W12, W03);
113  R1(C, D, A, B, G, H, E, F, 0x311465E7, W10, W10 ^ W14);
114  W10 = EE(W10, W01, W07, W13, W04);
115  R1(B, C, D, A, F, G, H, E, 0x6228CBCE, W11, W11 ^ W15);
116  W11 = EE(W11, W02, W08, W14, W05);
117  R1(A, B, C, D, E, F, G, H, 0xC451979C, W12, W12 ^ W00);
118  W12 = EE(W12, W03, W09, W15, W06);
119  R1(D, A, B, C, H, E, F, G, 0x88A32F39, W13, W13 ^ W01);
120  W13 = EE(W13, W04, W10, W00, W07);
121  R1(C, D, A, B, G, H, E, F, 0x11465E73, W14, W14 ^ W02);
122  W14 = EE(W14, W05, W11, W01, W08);
123  R1(B, C, D, A, F, G, H, E, 0x228CBCE6, W15, W15 ^ W03);
124  W15 = EE(W15, W06, W12, W02, W09);
125  R2(A, B, C, D, E, F, G, H, 0x9D8A7A87, W00, W00 ^ W04);
126  W00 = EE(W00, W07, W13, W03, W10);
127  R2(D, A, B, C, H, E, F, G, 0x3B14F50F, W01, W01 ^ W05);
128  W01 = EE(W01, W08, W14, W04, W11);
129  R2(C, D, A, B, G, H, E, F, 0x7629EA1E, W02, W02 ^ W06);
130  W02 = EE(W02, W09, W15, W05, W12);
131  R2(B, C, D, A, F, G, H, E, 0xEC53D43C, W03, W03 ^ W07);
132  W03 = EE(W03, W10, W00, W06, W13);
133  R2(A, B, C, D, E, F, G, H, 0xD8A7A879, W04, W04 ^ W08);
134  W04 = EE(W04, W11, W01, W07, W14);
135  R2(D, A, B, C, H, E, F, G, 0xB14F50F3, W05, W05 ^ W09);
136  W05 = EE(W05, W12, W02, W08, W15);
137  R2(C, D, A, B, G, H, E, F, 0x629EA1E7, W06, W06 ^ W10);
138  W06 = EE(W06, W13, W03, W09, W00);
139  R2(B, C, D, A, F, G, H, E, 0xC53D43CE, W07, W07 ^ W11);
140  W07 = EE(W07, W14, W04, W10, W01);
141  R2(A, B, C, D, E, F, G, H, 0x8A7A879D, W08, W08 ^ W12);
142  W08 = EE(W08, W15, W05, W11, W02);
143  R2(D, A, B, C, H, E, F, G, 0x14F50F3B, W09, W09 ^ W13);
144  W09 = EE(W09, W00, W06, W12, W03);
145  R2(C, D, A, B, G, H, E, F, 0x29EA1E76, W10, W10 ^ W14);
146  W10 = EE(W10, W01, W07, W13, W04);
147  R2(B, C, D, A, F, G, H, E, 0x53D43CEC, W11, W11 ^ W15);
148  W11 = EE(W11, W02, W08, W14, W05);
149  R2(A, B, C, D, E, F, G, H, 0xA7A879D8, W12, W12 ^ W00);
150  W12 = EE(W12, W03, W09, W15, W06);
151  R2(D, A, B, C, H, E, F, G, 0x4F50F3B1, W13, W13 ^ W01);
152  W13 = EE(W13, W04, W10, W00, W07);
153  R2(C, D, A, B, G, H, E, F, 0x9EA1E762, W14, W14 ^ W02);
154  W14 = EE(W14, W05, W11, W01, W08);
155  R2(B, C, D, A, F, G, H, E, 0x3D43CEC5, W15, W15 ^ W03);
156  W15 = EE(W15, W06, W12, W02, W09);
157  R2(A, B, C, D, E, F, G, H, 0x7A879D8A, W00, W00 ^ W04);
158  W00 = EE(W00, W07, W13, W03, W10);
159  R2(D, A, B, C, H, E, F, G, 0xF50F3B14, W01, W01 ^ W05);
160  W01 = EE(W01, W08, W14, W04, W11);
161  R2(C, D, A, B, G, H, E, F, 0xEA1E7629, W02, W02 ^ W06);
162  W02 = EE(W02, W09, W15, W05, W12);
163  R2(B, C, D, A, F, G, H, E, 0xD43CEC53, W03, W03 ^ W07);
164  W03 = EE(W03, W10, W00, W06, W13);
165  R2(A, B, C, D, E, F, G, H, 0xA879D8A7, W04, W04 ^ W08);
166  W04 = EE(W04, W11, W01, W07, W14);
167  R2(D, A, B, C, H, E, F, G, 0x50F3B14F, W05, W05 ^ W09);
168  W05 = EE(W05, W12, W02, W08, W15);
169  R2(C, D, A, B, G, H, E, F, 0xA1E7629E, W06, W06 ^ W10);
170  W06 = EE(W06, W13, W03, W09, W00);
171  R2(B, C, D, A, F, G, H, E, 0x43CEC53D, W07, W07 ^ W11);
172  W07 = EE(W07, W14, W04, W10, W01);
173  R2(A, B, C, D, E, F, G, H, 0x879D8A7A, W08, W08 ^ W12);
174  W08 = EE(W08, W15, W05, W11, W02);
175  R2(D, A, B, C, H, E, F, G, 0x0F3B14F5, W09, W09 ^ W13);
176  W09 = EE(W09, W00, W06, W12, W03);
177  R2(C, D, A, B, G, H, E, F, 0x1E7629EA, W10, W10 ^ W14);
178  W10 = EE(W10, W01, W07, W13, W04);
179  R2(B, C, D, A, F, G, H, E, 0x3CEC53D4, W11, W11 ^ W15);
180  W11 = EE(W11, W02, W08, W14, W05);
181  R2(A, B, C, D, E, F, G, H, 0x79D8A7A8, W12, W12 ^ W00);
182  W12 = EE(W12, W03, W09, W15, W06);
183  R2(D, A, B, C, H, E, F, G, 0xF3B14F50, W13, W13 ^ W01);
184  W13 = EE(W13, W04, W10, W00, W07);
185  R2(C, D, A, B, G, H, E, F, 0xE7629EA1, W14, W14 ^ W02);
186  W14 = EE(W14, W05, W11, W01, W08);
187  R2(B, C, D, A, F, G, H, E, 0xCEC53D43, W15, W15 ^ W03);
188  W15 = EE(W15, W06, W12, W02, W09);
189  R2(A, B, C, D, E, F, G, H, 0x9D8A7A87, W00, W00 ^ W04);
190  W00 = EE(W00, W07, W13, W03, W10);
191  R2(D, A, B, C, H, E, F, G, 0x3B14F50F, W01, W01 ^ W05);
192  W01 = EE(W01, W08, W14, W04, W11);
193  R2(C, D, A, B, G, H, E, F, 0x7629EA1E, W02, W02 ^ W06);
194  W02 = EE(W02, W09, W15, W05, W12);
195  R2(B, C, D, A, F, G, H, E, 0xEC53D43C, W03, W03 ^ W07);
196  W03 = EE(W03, W10, W00, W06, W13);
197  R2(A, B, C, D, E, F, G, H, 0xD8A7A879, W04, W04 ^ W08);
198  R2(D, A, B, C, H, E, F, G, 0xB14F50F3, W05, W05 ^ W09);
199  R2(C, D, A, B, G, H, E, F, 0x629EA1E7, W06, W06 ^ W10);
200  R2(B, C, D, A, F, G, H, E, 0xC53D43CE, W07, W07 ^ W11);
201  R2(A, B, C, D, E, F, G, H, 0x8A7A879D, W08, W08 ^ W12);
202  R2(D, A, B, C, H, E, F, G, 0x14F50F3B, W09, W09 ^ W13);
203  R2(C, D, A, B, G, H, E, F, 0x29EA1E76, W10, W10 ^ W14);
204  R2(B, C, D, A, F, G, H, E, 0x53D43CEC, W11, W11 ^ W15);
205  R2(A, B, C, D, E, F, G, H, 0xA7A879D8, W12, W12 ^ W00);
206  R2(D, A, B, C, H, E, F, G, 0x4F50F3B1, W13, W13 ^ W01);
207  R2(C, D, A, B, G, H, E, F, 0x9EA1E762, W14, W14 ^ W02);
208  R2(B, C, D, A, F, G, H, E, 0x3D43CEC5, W15, W15 ^ W03);
209 
210  A = (state[0] ^= A);
211  B = (state[1] ^= B);
212  C = (state[2] ^= C);
213  D = (state[3] ^= D);
214  E = (state[4] ^= E);
215  F = (state[5] ^= F);
216  G = (state[6] ^= G);
217  H = (state[7] ^= H);
218 
219  data += SM3::BLOCKSIZE/sizeof(word32);
220  length -= SM3::BLOCKSIZE;
221  }
222 
223  return length;
224 }
225 
226 ANONYMOUS_NAMESPACE_END
227 
228 NAMESPACE_BEGIN(CryptoPP)
229 
230 void SM3::InitState(HashWordType *state)
231 {
232  const word32 s[] = {
233  0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600,
234  0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e
235  };
236 
237  std::memcpy(state, s, sizeof(s));
238 }
239 
240 void SM3::Transform(word32 *state, const word32 *data)
241 {
242  CRYPTOPP_ASSERT(state);
243  CRYPTOPP_ASSERT(data);
244 
245  SM3_HashMultipleBlocks_CXX(state, data, SM3::BLOCKSIZE);
246 }
247 
248 size_t SM3::HashMultipleBlocks(const HashWordType *input, size_t length)
249 {
250  const size_t res = length & (SM3::BLOCKSIZE - 1);
251  SM3_HashMultipleBlocks_CXX(m_state, input, length-res);
252  return res;
253 }
254 
255 NAMESPACE_END
byte
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:56
SM3
SM3 hash function.
Definition: sm3.h:27
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68
rotlConstant
T rotlConstant(T x)
Performs a left rotate.
Definition: misc.h:1547
pch.h
Precompiled header file.
word32
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:62
GetBlock
Access a block of memory.
Definition: misc.h:2528
misc.h
Utility functions for the Crypto++ library.
BigEndian
EnumToType< ByteOrder, BIG_ENDIAN_ORDER > BigEndian
Provides a constant for BigEndian.
Definition: cryptlib.h:152
sm3.h
Classes for the SM3 hash function.
cpu.h
Functions for CPU features and intrinsics.
SM3::Transform
static void Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
Definition: sm3.cpp:240
CryptoPP
Crypto++ library namespace.
config.h
Library configuration file.