stm32c5xx_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file stm32c5xx_crypto_hash.c
3  * @brief STM32C5 hash hardware accelerator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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.6.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "stm32_hal.h"
36 #include "core/crypto.h"
39 #include "hash/hash_algorithms.h"
40 #include "debug.h"
41 
42 //Check crypto library configuration
43 #if (STM32C5XX_CRYPTO_HASH_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief HASH module initialization
48  * @return Error code
49  **/
50 
52 {
53  //Enable HASH peripheral clock
54  HAL_RCC_HASH_EnableClock();
55 
56  //Successful processing
57  return NO_ERROR;
58 }
59 
60 
61 /**
62  * @brief Update hash value
63  * @param[in] algo Hash algorithm
64  * @param[in] data Pointer to the input buffer
65  * @param[in] length Length of the input buffer
66  * @param[in,out] h Intermediate hash value
67  * @param[in] hLen Length of the intermediate hash value, in words
68  **/
69 
70 void hashProcessData(uint32_t algo, const uint8_t *data, size_t length,
71  uint32_t *h, size_t hLen)
72 {
73  uint_t i;
74  size_t blockSize;
75 
76  //Get block size
77  if(algo == HASH_CR_ALGO_SHA1 || algo == HASH_CR_ALGO_SHA224 ||
78  algo == HASH_CR_ALGO_SHA256)
79  {
80  blockSize = 64;
81  }
82  else
83  {
84  blockSize = 128;
85  }
86 
87  //Acquire exclusive access to the HASH module
89 
90  //Select the relevant hash algorithm
91  HASH->CR = HASH_CR_DATATYPE_8B | algo;
92  //Initialize the hash processor by setting the INIT bit
93  HASH->CR |= HASH_CR_INIT;
94 
95  //SHA-1, SHA-224 or SHA-256 algorithm?
96  if(blockSize == 64)
97  {
98  //Restore initial hash value
99  for(i = 0; i < hLen; i++)
100  {
101  HASH->CSR[6 + i] = h[i];
102  HASH->CSR[14 + i] = h[i];
103  }
104  }
105  else
106  {
107  //Restore initial hash value
108  for(i = 0; i < hLen; i += 2)
109  {
110  HASH->CSR[6 + i] = h[i + 1];
111  HASH->CSR[7 + i] = h[i];
112  HASH->CSR[39 + i / 2] = h[i];
113  HASH->CSR[47 + i / 2] = h[i + 1];
114  }
115  }
116 
117  //Input data are processed in a block-by-block fashion
118  while(length >= blockSize)
119  {
120  //Write the first byte of the block
121  HASH->DIN = __UNALIGNED_UINT32_READ(data);
122 
123  //Wait for the BUSY bit to be cleared
124  while((HASH->SR & HASH_SR_BUSY) != 0)
125  {
126  }
127 
128  //Write the rest of the block
129  HASH->DIN = __UNALIGNED_UINT32_READ(data + 4);
130  HASH->DIN = __UNALIGNED_UINT32_READ(data + 8);
131  HASH->DIN = __UNALIGNED_UINT32_READ(data + 12);
132  HASH->DIN = __UNALIGNED_UINT32_READ(data + 16);
133  HASH->DIN = __UNALIGNED_UINT32_READ(data + 20);
134  HASH->DIN = __UNALIGNED_UINT32_READ(data + 24);
135  HASH->DIN = __UNALIGNED_UINT32_READ(data + 28);
136  HASH->DIN = __UNALIGNED_UINT32_READ(data + 32);
137  HASH->DIN = __UNALIGNED_UINT32_READ(data + 36);
138  HASH->DIN = __UNALIGNED_UINT32_READ(data + 40);
139  HASH->DIN = __UNALIGNED_UINT32_READ(data + 44);
140  HASH->DIN = __UNALIGNED_UINT32_READ(data + 48);
141  HASH->DIN = __UNALIGNED_UINT32_READ(data + 52);
142  HASH->DIN = __UNALIGNED_UINT32_READ(data + 56);
143  HASH->DIN = __UNALIGNED_UINT32_READ(data + 60);
144 
145  //128-octet data block?
146  if(blockSize == 128)
147  {
148  HASH->DIN = __UNALIGNED_UINT32_READ(data + 64);
149  HASH->DIN = __UNALIGNED_UINT32_READ(data + 68);
150  HASH->DIN = __UNALIGNED_UINT32_READ(data + 72);
151  HASH->DIN = __UNALIGNED_UINT32_READ(data + 76);
152  HASH->DIN = __UNALIGNED_UINT32_READ(data + 80);
153  HASH->DIN = __UNALIGNED_UINT32_READ(data + 84);
154  HASH->DIN = __UNALIGNED_UINT32_READ(data + 88);
155  HASH->DIN = __UNALIGNED_UINT32_READ(data + 92);
156  HASH->DIN = __UNALIGNED_UINT32_READ(data + 96);
157  HASH->DIN = __UNALIGNED_UINT32_READ(data + 100);
158  HASH->DIN = __UNALIGNED_UINT32_READ(data + 104);
159  HASH->DIN = __UNALIGNED_UINT32_READ(data + 108);
160  HASH->DIN = __UNALIGNED_UINT32_READ(data + 112);
161  HASH->DIN = __UNALIGNED_UINT32_READ(data + 116);
162  HASH->DIN = __UNALIGNED_UINT32_READ(data + 120);
163  HASH->DIN = __UNALIGNED_UINT32_READ(data + 124);
164  }
165 
166  //Advance data pointer
167  data += blockSize;
168  length -= blockSize;
169  }
170 
171  //Partial digest computation are triggered each time the application
172  //writes the first word of the next block
173  HASH->DIN = 0;
174 
175  //Wait for the BUSY bit to be cleared
176  while((HASH->SR & HASH_SR_BUSY) != 0)
177  {
178  }
179 
180  //SHA-1, SHA-224 or SHA-256 algorithm?
181  if(blockSize == 64)
182  {
183  //Save intermediate hash value
184  for(i = 0; i < hLen; i++)
185  {
186  h[i] = HASH->CSR[6 + i];
187  }
188  }
189  else
190  {
191  //Save intermediate hash value
192  for(i = 0; i < hLen; i += 2)
193  {
194  h[i] = HASH->CSR[7 + i];
195  h[i + 1] = HASH->CSR[6 + i];
196  }
197  }
198 
199  //Release exclusive access to the HASH module
201 }
202 
203 
204 #if (SHA1_SUPPORT == ENABLED)
205 
206 /**
207  * @brief Update the SHA-1 context with a portion of the message being hashed
208  * @param[in] context Pointer to the SHA-1 context
209  * @param[in] data Pointer to the buffer being hashed
210  * @param[in] length Length of the buffer
211  **/
212 
213 void sha1Update(Sha1Context *context, const void *data, size_t length)
214 {
215  size_t n;
216 
217  //Process the incoming data
218  while(length > 0)
219  {
220  //Check whether some data is pending in the buffer
221  if(context->size == 0 && length >= 64)
222  {
223  //The length must be a multiple of 64 bytes
224  n = length - (length % 64);
225 
226  //Update hash value
228  SHA1_DIGEST_SIZE / 4);
229 
230  //Update the SHA-1 context
231  context->totalSize += n;
232  //Advance the data pointer
233  data = (uint8_t *) data + n;
234  //Remaining bytes to process
235  length -= n;
236  }
237  else
238  {
239  //The buffer can hold at most 64 bytes
240  n = MIN(length, 64 - context->size);
241 
242  //Copy the data to the buffer
243  osMemcpy(context->buffer + context->size, data, n);
244 
245  //Update the SHA-1 context
246  context->size += n;
247  context->totalSize += n;
248  //Advance the data pointer
249  data = (uint8_t *) data + n;
250  //Remaining bytes to process
251  length -= n;
252 
253  //Check whether the buffer is full
254  if(context->size == 64)
255  {
256  //Update hash value
257  hashProcessData(HASH_CR_ALGO_SHA1, context->buffer, context->size,
258  context->h, SHA1_DIGEST_SIZE / 4);
259 
260  //Empty the buffer
261  context->size = 0;
262  }
263  }
264  }
265 }
266 
267 
268 /**
269  * @brief Process message in 16-word blocks
270  * @param[in] context Pointer to the SHA-1 context
271  **/
272 
274 {
275  //Update hash value
276  hashProcessData(HASH_CR_ALGO_SHA1, context->buffer, 64, context->h,
277  SHA1_DIGEST_SIZE / 4);
278 }
279 
280 #endif
281 #if (SHA256_SUPPORT == ENABLED)
282 
283 /**
284  * @brief Update the SHA-256 context with a portion of the message being hashed
285  * @param[in] context Pointer to the SHA-256 context
286  * @param[in] data Pointer to the buffer being hashed
287  * @param[in] length Length of the buffer
288  **/
289 
290 void sha256Update(Sha256Context *context, const void *data, size_t length)
291 {
292  size_t n;
293 
294  //Process the incoming data
295  while(length > 0)
296  {
297  //Check whether some data is pending in the buffer
298  if(context->size == 0 && length >= 64)
299  {
300  //The length must be a multiple of 64 bytes
301  n = length - (length % 64);
302 
303  //Update hash value
305  SHA256_DIGEST_SIZE / 4);
306 
307  //Update the SHA-256 context
308  context->totalSize += n;
309  //Advance the data pointer
310  data = (uint8_t *) data + n;
311  //Remaining bytes to process
312  length -= n;
313  }
314  else
315  {
316  //The buffer can hold at most 64 bytes
317  n = MIN(length, 64 - context->size);
318 
319  //Copy the data to the buffer
320  osMemcpy(context->buffer + context->size, data, n);
321 
322  //Update the SHA-256 context
323  context->size += n;
324  context->totalSize += n;
325  //Advance the data pointer
326  data = (uint8_t *) data + n;
327  //Remaining bytes to process
328  length -= n;
329 
330  //Check whether the buffer is full
331  if(context->size == 64)
332  {
333  //Update hash value
334  hashProcessData(HASH_CR_ALGO_SHA256, context->buffer, context->size,
335  context->h, SHA256_DIGEST_SIZE / 4);
336 
337  //Empty the buffer
338  context->size = 0;
339  }
340  }
341  }
342 }
343 
344 
345 /**
346  * @brief Process message in 16-word blocks
347  * @param[in] context Pointer to the SHA-256 context
348  **/
349 
351 {
352  //Update hash value
353  hashProcessData(HASH_CR_ALGO_SHA256, context->buffer, 64, context->h,
354  SHA256_DIGEST_SIZE / 4);
355 }
356 
357 #endif
358 #if (SHA512_SUPPORT == ENABLED) && defined(HASH_CR_ALGO_SHA512)
359 
360 /**
361  * @brief Update the SHA-512 context with a portion of the message being hashed
362  * @param[in] context Pointer to the SHA-512 context
363  * @param[in] data Pointer to the buffer being hashed
364  * @param[in] length Length of the buffer
365  **/
366 
367 void sha512Update(Sha512Context *context, const void *data, size_t length)
368 {
369  size_t n;
370 
371  //Process the incoming data
372  while(length > 0)
373  {
374  //Check whether some data is pending in the buffer
375  if(context->size == 0 && length >= 128)
376  {
377  //The length must be a multiple of 128 bytes
378  n = length - (length % 128);
379 
380  //Update hash value
381  hashProcessData(HASH_CR_ALGO_SHA512, data, n, (uint32_t *) context->h,
382  SHA512_DIGEST_SIZE / 4);
383 
384  //Update the SHA-512 context
385  context->totalSize += n;
386  //Advance the data pointer
387  data = (uint8_t *) data + n;
388  //Remaining bytes to process
389  length -= n;
390  }
391  else
392  {
393  //The buffer can hold at most 128 bytes
394  n = MIN(length, 128 - context->size);
395 
396  //Copy the data to the buffer
397  osMemcpy(context->buffer + context->size, data, n);
398 
399  //Update the SHA-512 context
400  context->size += n;
401  context->totalSize += n;
402  //Advance the data pointer
403  data = (uint8_t *) data + n;
404  //Remaining bytes to process
405  length -= n;
406 
407  //Check whether the buffer is full
408  if(context->size == 128)
409  {
410  //Update hash value
411  hashProcessData(HASH_CR_ALGO_SHA512, context->buffer, context->size,
412  (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
413 
414  //Empty the buffer
415  context->size = 0;
416  }
417  }
418  }
419 }
420 
421 
422 /**
423  * @brief Process message in 16-word blocks
424  * @param[in] context Pointer to the SHA-512 context
425  **/
426 
427 void sha512ProcessBlock(Sha512Context *context)
428 {
429  //Update hash value
431  (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
432 }
433 
434 #endif
435 #endif
void sha512ProcessBlock(Sha512Context *context)
Process message in 16-word blocks.
#define HASH_CR_ALGO_SHA1
STM32C5 hardware cryptographic accelerator.
#define HASH_CR_ALGO_SHA224
#define HASH_CR_ALGO_SHA256
SHA-256 algorithm context.
Definition: sha256.h:62
uint8_t data[]
Definition: ethernet.h:224
size_t size
Definition: sha256.h:69
uint32_t h[8]
Definition: sha256.h:63
void hashProcessData(uint32_t algo, const uint8_t *data, size_t length, uint32_t *h, size_t hLen)
Update hash value.
uint64_t totalSize
Definition: sha1.h:70
size_t size
Definition: sha512.h:69
#define HASH_CR_DATATYPE_8B
uint8_t h
Definition: ndp.h:302
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
SHA-512 algorithm context.
Definition: sha512.h:62
uint32_t h[5]
Definition: sha1.h:63
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
General definitions for cryptographic algorithms.
uint8_t buffer[128]
Definition: sha512.h:67
error_t hashInit(void)
HASH module initialization.
#define HASH_CR_ALGO_SHA512
uint8_t length
Definition: tcp.h:375
uint8_t buffer[64]
Definition: sha256.h:67
#define MIN(a, b)
Definition: os_port.h:63
Collection of hash algorithms.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
uint64_t h[8]
Definition: sha512.h:63
uint8_t n
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
size_t size
Definition: sha1.h:69
void sha256ProcessBlock(Sha256Context *context)
Process message in 16-word blocks.
SHA-1 algorithm context.
Definition: sha1.h:62
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
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
OsMutex stm32c5xxCryptoMutex
#define SHA512_DIGEST_SIZE
Definition: sha512.h:45
STM32C5 hash hardware accelerator.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void sha512Update(Sha512Context *context, const void *data, size_t length)
Update the SHA-512 context with a portion of the message being hashed.