gd32w5xx_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file gd32w5xx_crypto_hash.c
3  * @brief GD32W5 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 "gd32w51x.h"
36 #include "core/crypto.h"
39 #include "hash/hash_algorithms.h"
40 #include "debug.h"
41 
42 //Check crypto library configuration
43 #if (GD32W5XX_CRYPTO_HASH_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief HAU module initialization
48  * @return Error code
49  **/
50 
52 {
53  //Enable HAU peripheral clock
54  rcu_periph_clock_enable(RCU_HAU);
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 hauProcessData(uint32_t algo, const uint8_t *data, size_t length,
71  uint32_t *h, size_t hLen)
72 {
73  uint_t i;
74 
75  //Acquire exclusive access to the HAU module
77 
78  //Select the relevant hash algorithm
79  HAU_CTL = HAU_SWAPPING_8BIT | algo;
80  //Start digest calculation
81  HAU_CTL |= HAU_CTL_START;
82 
83  //Restore initial hash value
84  for(i = 0; i < hLen; i++)
85  {
86  HAU_CTXS(6 + i) = h[i];
87  HAU_CTXS(14 + i) = h[i];
88  }
89 
90  //Input data are processed in a block-by-block fashion
91  while(length >= 64)
92  {
93  //Write the first byte of the block
94  HAU_DI = __UNALIGNED_UINT32_READ(data);
95 
96  //Wait for the BUSY bit to be cleared
97  while((HAU_STAT & HAU_STAT_BUSY) != 0)
98  {
99  }
100 
101  //Write the rest of the block
102  HAU_DI = __UNALIGNED_UINT32_READ(data + 4);
103  HAU_DI = __UNALIGNED_UINT32_READ(data + 8);
104  HAU_DI = __UNALIGNED_UINT32_READ(data + 12);
105  HAU_DI = __UNALIGNED_UINT32_READ(data + 16);
106  HAU_DI = __UNALIGNED_UINT32_READ(data + 20);
107  HAU_DI = __UNALIGNED_UINT32_READ(data + 24);
108  HAU_DI = __UNALIGNED_UINT32_READ(data + 28);
109  HAU_DI = __UNALIGNED_UINT32_READ(data + 32);
110  HAU_DI = __UNALIGNED_UINT32_READ(data + 36);
111  HAU_DI = __UNALIGNED_UINT32_READ(data + 40);
112  HAU_DI = __UNALIGNED_UINT32_READ(data + 44);
113  HAU_DI = __UNALIGNED_UINT32_READ(data + 48);
114  HAU_DI = __UNALIGNED_UINT32_READ(data + 52);
115  HAU_DI = __UNALIGNED_UINT32_READ(data + 56);
116  HAU_DI = __UNALIGNED_UINT32_READ(data + 60);
117 
118  //Advance data pointer
119  data += 64;
120  length -= 64;
121  }
122 
123  //Partial digest computation are triggered each time the application
124  //writes the first word of the next block
125  HAU_DI = 0;
126 
127  //Wait for the BUSY bit to be cleared
128  while((HAU_STAT & HAU_STAT_BUSY) != 0)
129  {
130  }
131 
132  //Save intermediate hash value
133  for(i = 0; i < hLen; i++)
134  {
135  h[i] = HAU_CTXS(14 + i);
136  }
137 
138  //Release exclusive access to the HAU module
140 }
141 
142 
143 #if (MD5_SUPPORT == ENABLED)
144 
145 /**
146  * @brief Update the MD5 context with a portion of the message being hashed
147  * @param[in] context Pointer to the MD5 context
148  * @param[in] data Pointer to the buffer being hashed
149  * @param[in] length Length of the buffer
150  **/
151 
152 void md5Update(Md5Context *context, const void *data, size_t length)
153 {
154  size_t n;
155 
156  //Process the incoming data
157  while(length > 0)
158  {
159  //Check whether some data is pending in the buffer
160  if(context->size == 0 && length >= 64)
161  {
162  //The length must be a multiple of 64 bytes
163  n = length - (length % 64);
164 
165  //Update hash value
166  hauProcessData(HAU_ALGO_MD5, data, n, context->h,
167  MD5_DIGEST_SIZE / 4);
168 
169  //Update the MD5 context
170  context->totalSize += n;
171  //Advance the data pointer
172  data = (uint8_t *) data + n;
173  //Remaining bytes to process
174  length -= n;
175  }
176  else
177  {
178  //The buffer can hold at most 64 bytes
179  n = MIN(length, 64 - context->size);
180 
181  //Copy the data to the buffer
182  osMemcpy(context->buffer + context->size, data, n);
183 
184  //Update the MD5 context
185  context->size += n;
186  context->totalSize += n;
187  //Advance the data pointer
188  data = (uint8_t *) data + n;
189  //Remaining bytes to process
190  length -= n;
191 
192  //Check whether the buffer is full
193  if(context->size == 64)
194  {
195  //Update hash value
196  hauProcessData(HAU_ALGO_MD5, context->buffer, context->size,
197  context->h, MD5_DIGEST_SIZE / 4);
198 
199  //Empty the buffer
200  context->size = 0;
201  }
202  }
203  }
204 }
205 
206 
207 /**
208  * @brief Process message in 16-word blocks
209  * @param[in] context Pointer to the MD5 context
210  **/
211 
213 {
214  //Update hash value
215  hauProcessData(HAU_ALGO_MD5, context->buffer, 64, context->h,
216  MD5_DIGEST_SIZE / 4);
217 }
218 
219 #endif
220 #if (SHA1_SUPPORT == ENABLED)
221 
222 /**
223  * @brief Update the SHA-1 context with a portion of the message being hashed
224  * @param[in] context Pointer to the SHA-1 context
225  * @param[in] data Pointer to the buffer being hashed
226  * @param[in] length Length of the buffer
227  **/
228 
229 void sha1Update(Sha1Context *context, const void *data, size_t length)
230 {
231  size_t n;
232 
233  //Process the incoming data
234  while(length > 0)
235  {
236  //Check whether some data is pending in the buffer
237  if(context->size == 0 && length >= 64)
238  {
239  //The length must be a multiple of 64 bytes
240  n = length - (length % 64);
241 
242  //Update hash value
243  hauProcessData(HAU_ALGO_SHA1, data, n, context->h,
244  SHA1_DIGEST_SIZE / 4);
245 
246  //Update the SHA-1 context
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  else
254  {
255  //The buffer can hold at most 64 bytes
256  n = MIN(length, 64 - context->size);
257 
258  //Copy the data to the buffer
259  osMemcpy(context->buffer + context->size, data, n);
260 
261  //Update the SHA-1 context
262  context->size += n;
263  context->totalSize += n;
264  //Advance the data pointer
265  data = (uint8_t *) data + n;
266  //Remaining bytes to process
267  length -= n;
268 
269  //Check whether the buffer is full
270  if(context->size == 64)
271  {
272  //Update hash value
273  hauProcessData(HAU_ALGO_SHA1, context->buffer, context->size,
274  context->h, SHA1_DIGEST_SIZE / 4);
275 
276  //Empty the buffer
277  context->size = 0;
278  }
279  }
280  }
281 }
282 
283 
284 /**
285  * @brief Process message in 16-word blocks
286  * @param[in] context Pointer to the SHA-1 context
287  **/
288 
290 {
291  //Update hash value
292  hauProcessData(HAU_ALGO_SHA1, context->buffer, 64, context->h,
293  SHA1_DIGEST_SIZE / 4);
294 }
295 
296 #endif
297 #if (SHA256_SUPPORT == ENABLED)
298 
299 /**
300  * @brief Update the SHA-256 context with a portion of the message being hashed
301  * @param[in] context Pointer to the SHA-256 context
302  * @param[in] data Pointer to the buffer being hashed
303  * @param[in] length Length of the buffer
304  **/
305 
306 void sha256Update(Sha256Context *context, const void *data, size_t length)
307 {
308  size_t n;
309 
310  //Process the incoming data
311  while(length > 0)
312  {
313  //Check whether some data is pending in the buffer
314  if(context->size == 0 && length >= 64)
315  {
316  //The length must be a multiple of 64 bytes
317  n = length - (length % 64);
318 
319  //Update hash value
320  hauProcessData(HAU_ALGO_SHA256, data, n, context->h,
321  SHA256_DIGEST_SIZE / 4);
322 
323  //Update the SHA-256 context
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  else
331  {
332  //The buffer can hold at most 64 bytes
333  n = MIN(length, 64 - context->size);
334 
335  //Copy the data to the buffer
336  osMemcpy(context->buffer + context->size, data, n);
337 
338  //Update the SHA-256 context
339  context->size += n;
340  context->totalSize += n;
341  //Advance the data pointer
342  data = (uint8_t *) data + n;
343  //Remaining bytes to process
344  length -= n;
345 
346  //Check whether the buffer is full
347  if(context->size == 64)
348  {
349  //Update hash value
350  hauProcessData(HAU_ALGO_SHA256, context->buffer, context->size,
351  context->h, SHA256_DIGEST_SIZE / 4);
352 
353  //Empty the buffer
354  context->size = 0;
355  }
356  }
357  }
358 }
359 
360 
361 /**
362  * @brief Process message in 16-word blocks
363  * @param[in] context Pointer to the SHA-256 context
364  **/
365 
367 {
368  //Update hash value
369  hauProcessData(HAU_ALGO_SHA256, context->buffer, 64, context->h,
370  SHA256_DIGEST_SIZE / 4);
371 }
372 
373 #endif
374 #endif
SHA-256 algorithm context.
Definition: sha256.h:62
void md5ProcessBlock(Md5Context *context)
Process message in 16-word blocks.
uint8_t data[]
Definition: ethernet.h:222
size_t size
Definition: sha256.h:73
uint32_t h[8]
Definition: sha256.h:65
uint64_t totalSize
Definition: sha1.h:74
uint8_t buffer[64]
Definition: md5.h:71
uint8_t h
Definition: ndp.h:302
OsMutex gd32w5xxCryptoMutex
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
error_t
Error codes.
Definition: error.h:43
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 MD5_DIGEST_SIZE
Definition: md5.h:45
Collection of hash algorithms.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
void md5Update(Md5Context *context, const void *data, size_t length)
Update the MD5 context with a portion of the message being hashed.
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
GD32W5 hardware cryptographic accelerator.
size_t size
Definition: md5.h:73
SHA-1 algorithm context.
Definition: sha1.h:62
GD32W5 hash hardware accelerator.
uint8_t buffer[64]
Definition: sha1.h:71
void sha256ProcessBlock(Sha256Context *context)
Process message in 16-word blocks.
uint64_t totalSize
Definition: sha256.h:74
void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
void hauProcessData(uint32_t algo, const uint8_t *data, size_t length, uint32_t *h, size_t hLen)
Update hash value.
unsigned int uint_t
Definition: compiler_port.h:50
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
error_t hauInit(void)
HAU module initialization.
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
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
Debugging facilities.