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