m2354_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file m2354_crypto_hash.c
3  * @brief M2354 hash hardware accelerator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "m2354.h"
36 #include "core/crypto.h"
39 #include "hash/hash_algorithms.h"
40 #include "debug.h"
41 
42 //Check crypto library configuration
43 #if (M2354_CRYPTO_HASH_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Update hash value
48  * @param[in] opmode Hash algorithm
49  * @param[in] data Pointer to the input buffer
50  * @param[in] length Length of the input buffer
51  * @param[in,out] h Intermediate hash value
52  * @param[in] hLen Length of the intermediate hash value, in words
53  **/
54 
55 void hashProcessData(uint32_t opmode, const uint8_t *data,
56  size_t length, uint32_t *h, size_t hLen)
57 {
58  uint_t i;
59 
60  //Acquire exclusive access to the CRYPTO module
62 
63  //Reset CRYPTO controller
64  SYS->IPRST0 |= SYS_IPRST0_CRPTRST_Msk;
65  SYS->IPRST0 &= ~SYS_IPRST0_CRPTRST_Msk;
66 
67  //Select the relevant hash algorithm
68  CRPT->HMAC_CTL = CRPT_HMAC_CTL_INSWAP_Msk | CRPT_HMAC_CTL_OUTSWAP_Msk |
69  CRPT_HMAC_CTL_DMACSCAD_Msk | opmode;
70 
71  //SHA-1, SHA-224, SHA-256 or SM3 algorithm?
72  if(opmode == CRPT_HMAC_CTL_OPMODE_SHA1 || opmode == CRPT_HMAC_CTL_OPMODE_SHA224 ||
73  opmode == CRPT_HMAC_CTL_OPMODE_SHA256 || opmode == CRPT_HMAC_CTL_SM3EN_Msk)
74  {
75  //Restore initial hash value
76  for(i = 0; i < hLen; i++)
77  {
78  CRPT->HMAC_FDBCK[2 * i] = h[i];
79  }
80  }
81  else
82  {
83  //Restore initial hash value
84  for(i = 0; i < hLen; i++)
85  {
86  CRPT->HMAC_FDBCK[i] = h[i];
87  }
88  }
89 
90  //Start SHA engine
91  CRPT->HMAC_CTL |= CRPT_HMAC_CTL_START_Msk;
92 
93  //Process input data
94  for(i = 0; i < length; i += 4)
95  {
96  //Wait for the DATINREQ bit to be set
97  while((CRPT->HMAC_STS & CRPT_HMAC_STS_DATINREQ_Msk) == 0)
98  {
99  }
100 
101  //Write one word of data
102  CRPT->HMAC_DATIN = __UNALIGNED_UINT32_READ(data + i);
103  }
104 
105  //Wait for the processing to complete
106  while((CRPT->HMAC_STS & CRPT_HMAC_STS_DATINREQ_Msk) == 0)
107  {
108  }
109 
110  //SHA-1, SHA-224, SHA-256 or SM3 algorithm?
111  if(opmode == CRPT_HMAC_CTL_OPMODE_SHA1 || opmode == CRPT_HMAC_CTL_OPMODE_SHA224 ||
112  opmode == CRPT_HMAC_CTL_OPMODE_SHA256 || opmode == CRPT_HMAC_CTL_SM3EN_Msk)
113  {
114  //Save intermediate hash value
115  for(i = 0; i < hLen; i++)
116  {
117  h[i] = CRPT->HMAC_FDBCK[2 * i];
118  }
119  }
120  else
121  {
122  //Save intermediate hash value
123  for(i = 0; i < hLen; i++)
124  {
125  h[i] = CRPT->HMAC_FDBCK[i];
126  }
127  }
128 
129  //Stop SHA engine
130  CRPT->HMAC_CTL |= CRPT_HMAC_CTL_STOP_Msk;
131 
132  //Release exclusive access to the CRYPTO module
134 }
135 
136 
137 #if (SHA1_SUPPORT == ENABLED)
138 
139 /**
140  * @brief Update the SHA-1 context with a portion of the message being hashed
141  * @param[in] context Pointer to the SHA-1 context
142  * @param[in] data Pointer to the buffer being hashed
143  * @param[in] length Length of the buffer
144  **/
145 
146 void sha1Update(Sha1Context *context, const void *data, size_t length)
147 {
148  size_t n;
149 
150  //Process the incoming data
151  while(length > 0)
152  {
153  //Check whether some data is pending in the buffer
154  if(context->size == 0 && length >= 64)
155  {
156  //The length must be a multiple of 64 bytes
157  n = length - (length % 64);
158 
159  //Update hash value
161  SHA1_DIGEST_SIZE / 4);
162 
163  //Update the SHA-1 context
164  context->totalSize += n;
165  //Advance the data pointer
166  data = (uint8_t *) data + n;
167  //Remaining bytes to process
168  length -= n;
169  }
170  else
171  {
172  //The buffer can hold at most 64 bytes
173  n = MIN(length, 64 - context->size);
174 
175  //Copy the data to the buffer
176  osMemcpy(context->buffer + context->size, data, n);
177 
178  //Update the SHA-1 context
179  context->size += n;
180  context->totalSize += n;
181  //Advance the data pointer
182  data = (uint8_t *) data + n;
183  //Remaining bytes to process
184  length -= n;
185 
186  //Check whether the buffer is full
187  if(context->size == 64)
188  {
189  //Update hash value
191  context->size, context->h, SHA1_DIGEST_SIZE / 4);
192 
193  //Empty the buffer
194  context->size = 0;
195  }
196  }
197  }
198 }
199 
200 
201 /**
202  * @brief Process message in 16-word blocks
203  * @param[in] context Pointer to the SHA-1 context
204  **/
205 
207 {
208  //Update hash value
210  context->h, SHA1_DIGEST_SIZE / 4);
211 }
212 
213 #endif
214 #if (SHA256_SUPPORT == ENABLED)
215 
216 /**
217  * @brief Update the SHA-256 context with a portion of the message being hashed
218  * @param[in] context Pointer to the SHA-256 context
219  * @param[in] data Pointer to the buffer being hashed
220  * @param[in] length Length of the buffer
221  **/
222 
223 void sha256Update(Sha256Context *context, const void *data, size_t length)
224 {
225  size_t n;
226 
227  //Process the incoming data
228  while(length > 0)
229  {
230  //Check whether some data is pending in the buffer
231  if(context->size == 0 && length >= 64)
232  {
233  //The length must be a multiple of 64 bytes
234  n = length - (length % 64);
235 
236  //Update hash value
238  SHA256_DIGEST_SIZE / 4);
239 
240  //Update the SHA-256 context
241  context->totalSize += n;
242  //Advance the data pointer
243  data = (uint8_t *) data + n;
244  //Remaining bytes to process
245  length -= n;
246  }
247  else
248  {
249  //The buffer can hold at most 64 bytes
250  n = MIN(length, 64 - context->size);
251 
252  //Copy the data to the buffer
253  osMemcpy(context->buffer + context->size, data, n);
254 
255  //Update the SHA-256 context
256  context->size += n;
257  context->totalSize += n;
258  //Advance the data pointer
259  data = (uint8_t *) data + n;
260  //Remaining bytes to process
261  length -= n;
262 
263  //Check whether the buffer is full
264  if(context->size == 64)
265  {
266  //Update hash value
268  context->size, context->h, SHA256_DIGEST_SIZE / 4);
269 
270  //Empty the buffer
271  context->size = 0;
272  }
273  }
274  }
275 }
276 
277 
278 /**
279  * @brief Process message in 16-word blocks
280  * @param[in] context Pointer to the SHA-256 context
281  **/
282 
284 {
285  //Update hash value
287  context->h, SHA256_DIGEST_SIZE / 4);
288 }
289 
290 #endif
291 #if (SHA512_SUPPORT == ENABLED)
292 
293 /**
294  * @brief Update the SHA-512 context with a portion of the message being hashed
295  * @param[in] context Pointer to the SHA-512 context
296  * @param[in] data Pointer to the buffer being hashed
297  * @param[in] length Length of the buffer
298  **/
299 
300 void sha512Update(Sha512Context *context, const void *data, size_t length)
301 {
302  size_t n;
303 
304  //Process the incoming data
305  while(length > 0)
306  {
307  //Check whether some data is pending in the buffer
308  if(context->size == 0 && length >= 128)
309  {
310  //The length must be a multiple of 128 bytes
311  n = length - (length % 128);
312 
313  //Update hash value
315  (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
316 
317  //Update the SHA-512 context
318  context->totalSize += n;
319  //Advance the data pointer
320  data = (uint8_t *) data + n;
321  //Remaining bytes to process
322  length -= n;
323  }
324  else
325  {
326  //The buffer can hold at most 128 bytes
327  n = MIN(length, 128 - context->size);
328 
329  //Copy the data to the buffer
330  osMemcpy(context->buffer + context->size, data, n);
331 
332  //Update the SHA-512 context
333  context->size += n;
334  context->totalSize += n;
335  //Advance the data pointer
336  data = (uint8_t *) data + n;
337  //Remaining bytes to process
338  length -= n;
339 
340  //Check whether the buffer is full
341  if(context->size == 128)
342  {
343  //Update hash value
345  context->size, (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
346 
347  //Empty the buffer
348  context->size = 0;
349  }
350  }
351  }
352 }
353 
354 
355 /**
356  * @brief Process message in 16-word blocks
357  * @param[in] context Pointer to the SHA-512 context
358  **/
359 
361 {
362  //Update hash value
364  (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
365 }
366 
367 #endif
368 #if (SM3_SUPPORT == ENABLED)
369 
370 /**
371  * @brief Update the SM3 context with a portion of the message being hashed
372  * @param[in] context Pointer to the SM3 context
373  * @param[in] data Pointer to the buffer being hashed
374  * @param[in] length Length of the buffer
375  **/
376 
377 void sm3Update(Sm3Context *context, const void *data, size_t length)
378 {
379  size_t n;
380 
381  //Process the incoming data
382  while(length > 0)
383  {
384  //Check whether some data is pending in the buffer
385  if(context->size == 0 && length >= 64)
386  {
387  //The length must be a multiple of 64 bytes
388  n = length - (length % 64);
389 
390  //Update hash value
391  hashProcessData(CRPT_HMAC_CTL_SM3EN_Msk, data, n, context->h,
392  SM3_DIGEST_SIZE / 4);
393 
394  //Update the SM3 context
395  context->totalSize += n;
396  //Advance the data pointer
397  data = (uint8_t *) data + n;
398  //Remaining bytes to process
399  length -= n;
400  }
401  else
402  {
403  //The buffer can hold at most 64 bytes
404  n = MIN(length, 64 - context->size);
405 
406  //Copy the data to the buffer
407  osMemcpy(context->buffer + context->size, data, n);
408 
409  //Update the SM3 context
410  context->size += n;
411  context->totalSize += n;
412  //Advance the data pointer
413  data = (uint8_t *) data + n;
414  //Remaining bytes to process
415  length -= n;
416 
417  //Check whether the buffer is full
418  if(context->size == 64)
419  {
420  //Update hash value
421  hashProcessData(CRPT_HMAC_CTL_SM3EN_Msk, context->buffer,
422  context->size, context->h, SM3_DIGEST_SIZE / 4);
423 
424  //Empty the buffer
425  context->size = 0;
426  }
427  }
428  }
429 }
430 
431 
432 /**
433  * @brief Process message in 16-word blocks
434  * @param[in] context Pointer to the SM3 context
435  **/
436 
438 {
439  //Update hash value
440  hashProcessData(CRPT_HMAC_CTL_SM3EN_Msk, context->buffer, 64,
441  context->h, SM3_DIGEST_SIZE / 4);
442 }
443 
444 #endif
445 #endif
void sha512ProcessBlock(Sha512Context *context)
Process message in 16-word blocks.
M2354 hash hardware accelerator.
SHA-256 algorithm context.
Definition: sha256.h:62
#define CRPT_HMAC_CTL_OPMODE_SHA256
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
uint8_t data[]
Definition: ethernet.h:222
size_t size
Definition: sha256.h:69
SM3 algorithm context.
Definition: sm3.h:62
uint32_t h[8]
Definition: sha256.h:63
void hashProcessData(uint32_t opmode, const uint8_t *data, size_t length, uint32_t *h, size_t hLen)
Update hash value.
uint8_t buffer[64]
Definition: sm3.h:67
void sm3Update(Sm3Context *context, const void *data, size_t length)
Update the SM3 context with a portion of the message being hashed.
void sha256ProcessBlock(Sha256Context *context)
Process message in 16-word blocks.
uint64_t totalSize
Definition: sm3.h:70
uint64_t totalSize
Definition: sha1.h:70
size_t size
Definition: sha512.h:69
uint8_t h
Definition: ndp.h:302
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
SHA-512 algorithm context.
Definition: sha512.h:62
uint32_t h[5]
Definition: sha1.h:63
uint32_t h[8]
Definition: sm3.h:63
General definitions for cryptographic algorithms.
uint8_t buffer[128]
Definition: sha512.h:67
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
uint8_t length
Definition: tcp.h:375
uint8_t buffer[64]
Definition: sha256.h:67
void sm3ProcessBlock(Sm3Context *context)
Process message in 16-word blocks.
#define MIN(a, b)
Definition: os_port.h:63
Collection of hash algorithms.
#define CRPT_HMAC_CTL_OPMODE_SHA512
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
void sha512Update(Sha512Context *context, const void *data, size_t length)
Update the SHA-512 context with a portion of the message being hashed.
uint64_t h[8]
Definition: sha512.h:63
uint8_t n
#define CRPT_HMAC_CTL_OPMODE_SHA1
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
OsMutex m2354CryptoMutex
Definition: m2354_crypto.c:42
size_t size
Definition: sha1.h:69
#define CRPT_HMAC_CTL_OPMODE_SHA224
SHA-1 algorithm context.
Definition: sha1.h:62
M2354 hardware cryptographic accelerator.
uint8_t buffer[64]
Definition: sha1.h:67
uint64_t totalSize
Definition: sha512.h:70
uint64_t totalSize
Definition: sha256.h:70
unsigned int uint_t
Definition: compiler_port.h:57
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
size_t size
Definition: sm3.h:69
#define SHA512_DIGEST_SIZE
Definition: sha512.h:45
Debugging facilities.
#define SM3_DIGEST_SIZE
Definition: sm3.h:45