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