sm3.c
Go to the documentation of this file.
1 /**
2  * @file sm3.c
3  * @brief SM3 hash function
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "hash/sm3.h"
37 
38 //Check crypto library configuration
39 #if (SM3_SUPPORT == ENABLED)
40 
41 //Macro to access the workspace as a circular buffer
42 #define W(n) w[(n) & 0x0F]
43 
44 //SM3 auxiliary functions
45 #define FF1(x, y, z) ((x) ^ (y) ^ (z))
46 #define FF2(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
47 #define GG1(x, y, z) ((x) ^ (y) ^ (z))
48 #define GG2(x, y, z) (((x) & (y)) | ((~x) & (z)))
49 #define P0(x) ((x) ^ ROL32(x, 9) ^ ROL32(x, 17))
50 #define P1(x) ((x) ^ ROL32(x, 15) ^ ROL32(x, 23))
51 
52 //Constants T_j
53 #define TJ1 0x79CC4519
54 #define TJ2 0x7A879D8A
55 
56 //SM3 padding
57 static const uint8_t padding[64] =
58 {
59  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
63 };
64 
65 //SM3 object identifier (1.0.10118.3.0.65)
66 const uint8_t SM3_OID[6] = {0x28, 0xCF, 0x06, 0x03, 0x00, 0x41};
67 
68 //Common interface for hash algorithms
70 {
71  "SM3",
72  SM3_OID,
73  sizeof(SM3_OID),
74  sizeof(Sm3Context),
78  TRUE,
84 };
85 
86 
87 /**
88  * @brief Digest a message using SM3
89  * @param[in] data Pointer to the message being hashed
90  * @param[in] length Length of the message
91  * @param[out] digest Pointer to the calculated digest
92  * @return Error code
93  **/
94 
95 error_t sm3Compute(const void *data, size_t length, uint8_t *digest)
96 {
97 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
98  Sm3Context *context;
99 #else
100  Sm3Context context[1];
101 #endif
102 
103  //Check parameters
104  if(data == NULL && length != 0)
106 
107  if(digest == NULL)
109 
110 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
111  //Allocate a memory buffer to hold the SM3 context
112  context = cryptoAllocMem(sizeof(Sm3Context));
113  //Failed to allocate memory?
114  if(context == NULL)
115  return ERROR_OUT_OF_MEMORY;
116 #endif
117 
118  //Initialize the SM3 context
119  sm3Init(context);
120  //Digest the message
121  sm3Update(context, data, length);
122  //Finalize the SM3 message digest
123  sm3Final(context, digest);
124 
125 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
126  //Free previously allocated memory
127  cryptoFreeMem(context);
128 #endif
129 
130  //Successful processing
131  return NO_ERROR;
132 }
133 
134 
135 /**
136  * @brief Initialize SM3 message digest context
137  * @param[in] context Pointer to the SM3 context to initialize
138  **/
139 
140 void sm3Init(Sm3Context *context)
141 {
142  //Set initial hash value
143  context->h[0] = 0x7380166F;
144  context->h[1] = 0x4914B2B9;
145  context->h[2] = 0x172442D7;
146  context->h[3] = 0xDA8A0600;
147  context->h[4] = 0xA96F30BC;
148  context->h[5] = 0x163138AA;
149  context->h[6] = 0xE38DEE4D;
150  context->h[7] = 0xB0FB0E4E;
151 
152  //Number of bytes in the buffer
153  context->size = 0;
154  //Total length of the message
155  context->totalSize = 0;
156 }
157 
158 
159 /**
160  * @brief Update the SM3 context with a portion of the message being hashed
161  * @param[in] context Pointer to the SM3 context
162  * @param[in] data Pointer to the buffer being hashed
163  * @param[in] length Length of the buffer
164  **/
165 
166 void sm3Update(Sm3Context *context, const void *data, size_t length)
167 {
168  size_t n;
169 
170  //Process the incoming data
171  while(length > 0)
172  {
173  //The buffer can hold at most 64 bytes
174  n = MIN(length, 64 - context->size);
175 
176  //Copy the data to the buffer
177  osMemcpy(context->buffer + context->size, data, n);
178 
179  //Update the SM3 context
180  context->size += n;
181  context->totalSize += n;
182  //Advance the data pointer
183  data = (uint8_t *) data + n;
184  //Remaining bytes to process
185  length -= n;
186 
187  //Process message in 16-word blocks
188  if(context->size == 64)
189  {
190  //Transform the 16-word block
191  sm3ProcessBlock(context);
192  //Empty the buffer
193  context->size = 0;
194  }
195  }
196 }
197 
198 
199 /**
200  * @brief Finish the SM3 message digest
201  * @param[in] context Pointer to the SM3 context
202  * @param[out] digest Calculated digest (optional parameter)
203  **/
204 
205 void sm3Final(Sm3Context *context, uint8_t *digest)
206 {
207  uint_t i;
208  size_t paddingSize;
209  uint64_t totalSize;
210 
211  //Length of the original message (before padding)
212  totalSize = context->totalSize * 8;
213 
214  //Pad the message so that its length is congruent to 56 modulo 64
215  if(context->size < 56)
216  {
217  paddingSize = 56 - context->size;
218  }
219  else
220  {
221  paddingSize = 64 + 56 - context->size;
222  }
223 
224  //Append padding
225  sm3Update(context, padding, paddingSize);
226 
227  //Append the length of the original message
228  context->w[14] = htobe32((uint32_t) (totalSize >> 32));
229  context->w[15] = htobe32((uint32_t) totalSize);
230 
231  //Calculate the message digest
232  sm3ProcessBlock(context);
233 
234  //Convert from host byte order to big-endian byte order
235  for(i = 0; i < 8; i++)
236  {
237  context->h[i] = htobe32(context->h[i]);
238  }
239 
240  //Copy the resulting digest
241  if(digest != NULL)
242  {
243  osMemcpy(digest, context->digest, SM3_DIGEST_SIZE);
244  }
245 }
246 
247 
248 /**
249  * @brief Finish the SM3 message digest (no padding added)
250  * @param[in] context Pointer to the SM3 context
251  * @param[out] digest Calculated digest
252  **/
253 
254 void sm3FinalRaw(Sm3Context *context, uint8_t *digest)
255 {
256  uint_t i;
257 
258  //Convert from host byte order to big-endian byte order
259  for(i = 0; i < 8; i++)
260  {
261  context->h[i] = htobe32(context->h[i]);
262  }
263 
264  //Copy the resulting digest
265  osMemcpy(digest, context->digest, SM3_DIGEST_SIZE);
266 
267  //Convert from big-endian byte order to host byte order
268  for(i = 0; i < 8; i++)
269  {
270  context->h[i] = betoh32(context->h[i]);
271  }
272 }
273 
274 
275 /**
276  * @brief Process message in 16-word blocks
277  * @param[in] context Pointer to the SM3 context
278  **/
279 
281 {
282  uint_t i;
283  uint32_t ss1;
284  uint32_t ss2;
285  uint32_t tt1;
286  uint32_t tt2;
287  uint32_t temp;
288 
289  //Initialize the 8 working registers
290  uint32_t a = context->h[0];
291  uint32_t b = context->h[1];
292  uint32_t c = context->h[2];
293  uint32_t d = context->h[3];
294  uint32_t e = context->h[4];
295  uint32_t f = context->h[5];
296  uint32_t g = context->h[6];
297  uint32_t h = context->h[7];
298 
299  //Process message in 16-word blocks
300  uint32_t *w = context->w;
301 
302  //Convert from big-endian byte order to host byte order
303  for(i = 0; i < 16; i++)
304  {
305  w[i] = betoh32(w[i]);
306  }
307 
308  //SM3 compression function
309  for(i = 0; i < 64; i++)
310  {
311  //Message expansion
312  if(i >= 12)
313  {
314  temp = W(i + 4) ^ W(i + 11) ^ ROL32(W(i + 1), 15);
315  W(i + 4) = P1(temp) ^ ROL32(W(i + 7), 7) ^ W(i + 14);
316  }
317 
318  //Calculate TT1 and TT2
319  if(i < 16)
320  {
321  temp = ROL32(a, 12) + e + ROL32(TJ1, i);
322  ss1 = ROL32(temp, 7);
323  ss2 = ss1 ^ ROL32(a, 12);
324  tt1 = FF1(a, b, c) + d + ss2 + (W(i) ^ W(i + 4));
325  tt2 = GG1(e, f, g) + h + ss1 + W(i);
326  }
327  else
328  {
329  temp = ROL32(a, 12) + e + ROL32(TJ2, i % 32);
330  ss1 = ROL32(temp, 7);
331  ss2 = ss1 ^ ROL32(a, 12);
332  tt1 = FF2(a, b, c) + d + ss2 + (W(i) ^ W(i + 4));
333  tt2 = GG2(e, f, g) + h + ss1 + W(i);
334  }
335 
336  //Update working registers
337  d = c;
338  c = ROL32(b, 9);
339  b = a;
340  a = tt1;
341  h = g;
342  g = ROL32(f, 19);
343  f = e;
344  e = P0(tt2);
345  }
346 
347  //Update the hash value
348  context->h[0] ^= a;
349  context->h[1] ^= b;
350  context->h[2] ^= c;
351  context->h[3] ^= d;
352  context->h[4] ^= e;
353  context->h[5] ^= f;
354  context->h[6] ^= g;
355  context->h[7] ^= h;
356 }
357 
358 #endif
unsigned int uint_t
Definition: compiler_port.h:50
#define betoh32(value)
Definition: cpu_endian.h:454
#define htobe32(value)
Definition: cpu_endian.h:446
General definitions for cryptographic algorithms.
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:956
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:965
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:963
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:961
#define cryptoAllocMem(size)
Definition: crypto.h:765
#define cryptoFreeMem(p)
Definition: crypto.h:770
#define ROL32(a, n)
Definition: crypto.h:776
void(* HashAlgoInit)(void *context)
Definition: crypto.h:959
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t data[]
Definition: ethernet.h:222
uint8_t b
Definition: nbns_common.h:104
uint8_t h
Definition: ndp.h:302
uint8_t c
Definition: ndp.h:514
uint8_t a
Definition: ndp.h:411
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
#define TRUE
Definition: os_port.h:50
void sm3Update(Sm3Context *context, const void *data, size_t length)
Update the SM3 context with a portion of the message being hashed.
Definition: sm3.c:166
#define P0(x)
Definition: sm3.c:49
void sm3Init(Sm3Context *context)
Initialize SM3 message digest context.
Definition: sm3.c:140
void sm3ProcessBlock(Sm3Context *context)
Process message in 16-word blocks.
Definition: sm3.c:280
#define FF1(x, y, z)
Definition: sm3.c:45
#define GG1(x, y, z)
Definition: sm3.c:47
const uint8_t SM3_OID[6]
Definition: sm3.c:66
#define FF2(x, y, z)
Definition: sm3.c:46
error_t sm3Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SM3.
Definition: sm3.c:95
const HashAlgo sm3HashAlgo
Definition: sm3.c:69
#define W(n)
Definition: sm3.c:42
void sm3Final(Sm3Context *context, uint8_t *digest)
Finish the SM3 message digest.
Definition: sm3.c:205
#define GG2(x, y, z)
Definition: sm3.c:48
#define TJ2
Definition: sm3.c:54
#define TJ1
Definition: sm3.c:53
void sm3FinalRaw(Sm3Context *context, uint8_t *digest)
Finish the SM3 message digest (no padding added)
Definition: sm3.c:254
#define P1(x)
Definition: sm3.c:50
SM3 hash function.
#define SM3_BLOCK_SIZE
Definition: sm3.h:43
#define SM3_MIN_PAD_SIZE
Definition: sm3.h:47
#define SM3_DIGEST_SIZE
Definition: sm3.h:45
Common interface for hash algorithms.
Definition: crypto.h:1014
SM3 algorithm context.
Definition: sm3.h:62
uint8_t digest[32]
Definition: sm3.h:66
uint64_t totalSize
Definition: sm3.h:74
size_t size
Definition: sm3.h:73
uint8_t buffer[64]
Definition: sm3.h:71
uint32_t h[8]
Definition: sm3.h:65
uint32_t w[16]
Definition: sm3.h:70
uint8_t length
Definition: tcp.h:368