m460_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file m460_crypto_hash.c
3  * @brief M460 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 "m460.h"
36 #include "core/crypto.h"
39 #include "hash/hash_algorithms.h"
40 #include "xof/keccak.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (M460_CRYPTO_HASH_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Update hash value
49  * @param[in] opmode Hash algorithm
50  * @param[in] data Pointer to the input buffer
51  * @param[in] length Length of the input buffer
52  * @param[in,out] h Intermediate hash value
53  * @param[in] hLen Length of the intermediate hash value, in words
54  **/
55 
56 void hashProcessData(uint32_t opmode, const uint8_t *data,
57  size_t length, uint32_t *h, size_t hLen)
58 {
59  uint_t i;
60 
61  //Acquire exclusive access to the CRYPTO module
63 
64  //Reset CRYPTO controller
65  SYS->IPRST0 |= SYS_IPRST0_CRPTRST_Msk;
66  SYS->IPRST0 &= ~SYS_IPRST0_CRPTRST_Msk;
67 
68  //Select the relevant hash algorithm
69  CRPT->HMAC_CTL = CRPT_HMAC_CTL_INSWAP_Msk | CRPT_HMAC_CTL_OUTSWAP_Msk |
70  CRPT_HMAC_CTL_DMACSCAD_Msk | opmode;
71 
72  //SHA-1, SHA-224 or SHA-256 algorithm?
73  if(opmode == CRPT_HMAC_CTL_OPMODE_SHA1 || opmode == CRPT_HMAC_CTL_OPMODE_SHA224 ||
75  {
76  //Restore initial hash value
77  for(i = 0; i < hLen; i++)
78  {
79  CRPT->HMAC_FDBCK[2 * i] = h[i];
80  }
81  }
82  else
83  {
84  //Restore initial hash value
85  for(i = 0; i < hLen; i++)
86  {
87  CRPT->HMAC_FDBCK[i] = h[i];
88  }
89  }
90 
91  //Start SHA engine
92  CRPT->HMAC_CTL |= CRPT_HMAC_CTL_START_Msk;
93 
94  //Process input data
95  for(i = 0; i < length; i += 4)
96  {
97  //Wait for the DATINREQ bit to be set
98  while((CRPT->HMAC_STS & CRPT_HMAC_STS_DATINREQ_Msk) == 0)
99  {
100  }
101 
102  //Write one word of data
103  CRPT->HMAC_DATIN = __UNALIGNED_UINT32_READ(data + i);
104  }
105 
106  //Wait for the processing to complete
107  while((CRPT->HMAC_STS & CRPT_HMAC_STS_DATINREQ_Msk) == 0)
108  {
109  }
110 
111  //SHA-1, SHA-224 or SHA-256 algorithm?
112  if(opmode == CRPT_HMAC_CTL_OPMODE_SHA1 || opmode == CRPT_HMAC_CTL_OPMODE_SHA224 ||
113  opmode == CRPT_HMAC_CTL_OPMODE_SHA256)
114  {
115  //Save intermediate hash value
116  for(i = 0; i < hLen; i++)
117  {
118  h[i] = CRPT->HMAC_FDBCK[2 * i];
119  }
120  }
121  else
122  {
123  //Save intermediate hash value
124  for(i = 0; i < hLen; i++)
125  {
126  h[i] = CRPT->HMAC_FDBCK[i];
127  }
128  }
129 
130  //Stop SHA engine
131  CRPT->HMAC_CTL |= CRPT_HMAC_CTL_STOP_Msk;
132 
133  //Release exclusive access to the CRYPTO module
135 }
136 
137 
138 #if (SHA1_SUPPORT == ENABLED)
139 
140 /**
141  * @brief Update the SHA-1 context with a portion of the message being hashed
142  * @param[in] context Pointer to the SHA-1 context
143  * @param[in] data Pointer to the buffer being hashed
144  * @param[in] length Length of the buffer
145  **/
146 
147 void sha1Update(Sha1Context *context, const void *data, size_t length)
148 {
149  size_t n;
150 
151  //Process the incoming data
152  while(length > 0)
153  {
154  //Check whether some data is pending in the buffer
155  if(context->size == 0 && length >= 64)
156  {
157  //The length must be a multiple of 64 bytes
158  n = length - (length % 64);
159 
160  //Update hash value
162  SHA1_DIGEST_SIZE / 4);
163 
164  //Update the SHA-1 context
165  context->totalSize += n;
166  //Advance the data pointer
167  data = (uint8_t *) data + n;
168  //Remaining bytes to process
169  length -= n;
170  }
171  else
172  {
173  //The buffer can hold at most 64 bytes
174  n = MIN(length, 64 - context->size);
175 
176  //Copy the data to the buffer
177  osMemcpy(context->buffer + context->size, data, n);
178 
179  //Update the SHA-1 context
180  context->size += n;
181  context->totalSize += n;
182  //Advance the data pointer
183  data = (uint8_t *) data + n;
184  //Remaining bytes to process
185  length -= n;
186 
187  //Check whether the buffer is full
188  if(context->size == 64)
189  {
190  //Update hash value
192  context->size, context->h, SHA1_DIGEST_SIZE / 4);
193 
194  //Empty the buffer
195  context->size = 0;
196  }
197  }
198  }
199 }
200 
201 
202 /**
203  * @brief Process message in 16-word blocks
204  * @param[in] context Pointer to the SHA-1 context
205  **/
206 
208 {
209  //Update hash value
211  context->h, SHA1_DIGEST_SIZE / 4);
212 }
213 
214 #endif
215 #if (SHA256_SUPPORT == ENABLED)
216 
217 /**
218  * @brief Update the SHA-256 context with a portion of the message being hashed
219  * @param[in] context Pointer to the SHA-256 context
220  * @param[in] data Pointer to the buffer being hashed
221  * @param[in] length Length of the buffer
222  **/
223 
224 void sha256Update(Sha256Context *context, const void *data, size_t length)
225 {
226  size_t n;
227 
228  //Process the incoming data
229  while(length > 0)
230  {
231  //Check whether some data is pending in the buffer
232  if(context->size == 0 && length >= 64)
233  {
234  //The length must be a multiple of 64 bytes
235  n = length - (length % 64);
236 
237  //Update hash value
239  SHA256_DIGEST_SIZE / 4);
240 
241  //Update the SHA-256 context
242  context->totalSize += n;
243  //Advance the data pointer
244  data = (uint8_t *) data + n;
245  //Remaining bytes to process
246  length -= n;
247  }
248  else
249  {
250  //The buffer can hold at most 64 bytes
251  n = MIN(length, 64 - context->size);
252 
253  //Copy the data to the buffer
254  osMemcpy(context->buffer + context->size, data, n);
255 
256  //Update the SHA-256 context
257  context->size += n;
258  context->totalSize += n;
259  //Advance the data pointer
260  data = (uint8_t *) data + n;
261  //Remaining bytes to process
262  length -= n;
263 
264  //Check whether the buffer is full
265  if(context->size == 64)
266  {
267  //Update hash value
269  context->size, context->h, SHA256_DIGEST_SIZE / 4);
270 
271  //Empty the buffer
272  context->size = 0;
273  }
274  }
275  }
276 }
277 
278 
279 /**
280  * @brief Process message in 16-word blocks
281  * @param[in] context Pointer to the SHA-256 context
282  **/
283 
285 {
286  //Update hash value
288  context->h, SHA256_DIGEST_SIZE / 4);
289 }
290 
291 #endif
292 #if (SHA512_SUPPORT == ENABLED)
293 
294 /**
295  * @brief Update the SHA-512 context with a portion of the message being hashed
296  * @param[in] context Pointer to the SHA-512 context
297  * @param[in] data Pointer to the buffer being hashed
298  * @param[in] length Length of the buffer
299  **/
300 
301 void sha512Update(Sha512Context *context, const void *data, size_t length)
302 {
303  size_t n;
304 
305  //Process the incoming data
306  while(length > 0)
307  {
308  //Check whether some data is pending in the buffer
309  if(context->size == 0 && length >= 128)
310  {
311  //The length must be a multiple of 128 bytes
312  n = length - (length % 128);
313 
314  //Update hash value
316  (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
317 
318  //Update the SHA-512 context
319  context->totalSize += n;
320  //Advance the data pointer
321  data = (uint8_t *) data + n;
322  //Remaining bytes to process
323  length -= n;
324  }
325  else
326  {
327  //The buffer can hold at most 128 bytes
328  n = MIN(length, 128 - context->size);
329 
330  //Copy the data to the buffer
331  osMemcpy(context->buffer + context->size, data, n);
332 
333  //Update the SHA-512 context
334  context->size += n;
335  context->totalSize += n;
336  //Advance the data pointer
337  data = (uint8_t *) data + n;
338  //Remaining bytes to process
339  length -= n;
340 
341  //Check whether the buffer is full
342  if(context->size == 128)
343  {
344  //Update hash value
346  context->size, (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
347 
348  //Empty the buffer
349  context->size = 0;
350  }
351  }
352  }
353 }
354 
355 
356 /**
357  * @brief Process message in 16-word blocks
358  * @param[in] context Pointer to the SHA-512 context
359  **/
360 
362 {
363  //Update hash value
365  (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
366 }
367 
368 #endif
369 #if (KECCAK_SUPPORT == ENABLED)
370 
371 
372 /**
373  * @brief Update state array
374  * @param[in] data Pointer to the input buffer
375  * @param[in] length Length of the input buffer
376  * @param[in] blockSize Block size
377  * @param[in,out] a State array
378  **/
379 
380 void keccakProcessData(const uint8_t *data, size_t length, size_t blockSize,
381  uint32_t *a)
382 {
383  uint_t i;
384  uint32_t opmode;
385 
386  //Check block size
387  if(blockSize == 72)
388  {
390  }
391  else if(blockSize == 104)
392  {
394  }
395  else if(blockSize == 136)
396  {
398  }
399  else
400  {
402  }
403 
404  //Acquire exclusive access to the CRYPTO module
406 
407  //Reset CRYPTO controller
408  SYS->IPRST0 |= SYS_IPRST0_CRPTRST_Msk;
409  SYS->IPRST0 &= ~SYS_IPRST0_CRPTRST_Msk;
410 
411  //Select the relevant hash algorithm
412  CRPT->HMAC_CTL = CRPT_HMAC_CTL_INSWAP_Msk | CRPT_HMAC_CTL_OUTSWAP_Msk |
413  CRPT_HMAC_CTL_SHA3EN_Msk | CRPT_HMAC_CTL_DMACSCAD_Msk | opmode;
414 
415  //Restore state array
416  for(i = 0; i < 16; i++)
417  {
418  CRPT->HMAC_FDBCK[i] = reverseInt32(a[49 - i]);
419  }
420 
421  for(i = 0; i < 34; i++)
422  {
423  CRPT->HMAC_FDBCK[54 + i] = reverseInt32(a[33 - i]);
424  }
425 
426  //Start SHA engine
427  CRPT->HMAC_CTL |= CRPT_HMAC_CTL_START_Msk;
428 
429  //Valid buffer?
430  if(data != NULL)
431  {
432  //Process input data
433  for(i = 0; i < length; i += 4)
434  {
435  //Wait for the DATINREQ bit to be set
436  while((CRPT->HMAC_STS & CRPT_HMAC_STS_DATINREQ_Msk) == 0)
437  {
438  }
439 
440  //Write one word of data
441  CRPT->HMAC_DATIN = __UNALIGNED_UINT32_READ(data + i);
442  }
443  }
444  else
445  {
446  //Process input data
447  for(i = 0; i < length; i += 4)
448  {
449  //Wait for the DATINREQ bit to be set
450  while((CRPT->HMAC_STS & CRPT_HMAC_STS_DATINREQ_Msk) == 0)
451  {
452  }
453 
454  //Write one word of data
455  CRPT->HMAC_DATIN = 0;
456  }
457  }
458 
459  //Wait for the processing to complete
460  while((CRPT->HMAC_STS & CRPT_HMAC_STS_DATINREQ_Msk) == 0)
461  {
462  }
463 
464  //Save state array
465  for(i = 0; i < 34; i++)
466  {
467  a[i] = reverseInt32(CRPT->HMAC_FDBCK[87 - i]);
468  }
469 
470  for(i = 0; i < 16; i++)
471  {
472  a[34 + i] = reverseInt32(CRPT->HMAC_FDBCK[15 - i]);
473  }
474 
475  //Stop SHA engine
476  CRPT->HMAC_CTL |= CRPT_HMAC_CTL_STOP_Msk;
477 
478  //Release exclusive access to the CRYPTO module
480 }
481 
482 
483 /**
484  * @brief Absorb data
485  * @param[in] context Pointer to the Keccak context
486  * @param[in] input Pointer to the buffer being hashed
487  * @param[in] length Length of the buffer
488  **/
489 
490 void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
491 {
492  size_t n;
493 
494  //Process the incoming data
495  while(length > 0)
496  {
497  //Check whether some data is pending in the buffer
498  if(context->length == 0 && length >= context->blockSize)
499  {
500  //The length must be a multiple of the block size
501  n = length - (length % context->blockSize);
502 
503  //Absorb the current block
504  keccakProcessData(input, n, context->blockSize,
505  (uint32_t *) context->a);
506 
507  //Advance the data pointer
508  input = (uint8_t *) input + n;
509  //Remaining bytes to process
510  length -= n;
511  }
512  else
513  {
514  //The buffer can hold at most one block
515  n = MIN(length, context->blockSize - context->length);
516 
517  //Copy the data to the buffer
518  osMemcpy(context->buffer + context->length, input, n);
519  //Update the length of the buffer
520  context->length += n;
521 
522  //Advance the data pointer
523  input = (uint8_t *) input + n;
524  //Remaining bytes to process
525  length -= n;
526 
527  //Check whether the buffer is full
528  if(context->length == context->blockSize)
529  {
530  //Absorb the current block
531  keccakProcessData(context->buffer, context->length,
532  context->blockSize, (uint32_t *) context->a);
533 
534  //Empty the buffer
535  context->length = 0;
536  }
537  }
538  }
539 }
540 
541 
542 /**
543  * @brief Block permutation
544  * @param[in] context Pointer to the Keccak context
545  **/
546 
548 {
549  //Perform block permutation
550  keccakProcessData(NULL, context->blockSize, context->blockSize,
551  (uint32_t *) context->a);
552 }
553 
554 #endif
555 #endif
void hashProcessData(uint32_t opmode, const uint8_t *data, size_t length, uint32_t *h, size_t hLen)
Update hash value.
keccak_lane_t a[5][5]
Definition: keccak.h:113
uint8_t a
Definition: ndp.h:411
SHA-256 algorithm context.
Definition: sha256.h:62
#define CRPT_HMAC_CTL_OPMODE_SHA256
void sha256ProcessBlock(Sha256Context *context)
Process message in 16-word blocks.
uint8_t data[]
Definition: ethernet.h:222
Keccak context.
Definition: keccak.h:110
size_t size
Definition: sha256.h:73
uint32_t h[8]
Definition: sha256.h:65
#define CRPT_HMAC_CTL_OPMODE_SHA3_224
void sha512ProcessBlock(Sha512Context *context)
Process message in 16-word blocks.
uint64_t totalSize
Definition: sha1.h:74
void keccakPermutBlock(KeccakContext *context)
Block permutation.
size_t size
Definition: sha512.h:73
#define CRPT_HMAC_CTL_OPMODE_SHA512
#define CRPT_HMAC_CTL_OPMODE_SHA1
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
uint8_t h
Definition: ndp.h:302
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
M460 hash hardware accelerator.
void keccakProcessData(const uint8_t *data, size_t length, size_t blockSize, uint32_t *a)
Update state array.
SHA-512 algorithm context.
Definition: sha512.h:62
uint32_t h[5]
Definition: sha1.h:65
General definitions for cryptographic algorithms.
uint8_t buffer[128]
Definition: sha512.h:71
#define CRPT_HMAC_CTL_OPMODE_SHA3_256
uint8_t length
Definition: tcp.h:368
uint8_t buffer[64]
Definition: sha256.h:71
#define MIN(a, b)
Definition: os_port.h:63
Collection of hash algorithms.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
Keccak sponge function.
void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
Absorb data.
uint64_t h[8]
Definition: sha512.h:65
uint8_t n
uint_t blockSize
Definition: keccak.h:121
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
#define CRPT_HMAC_CTL_OPMODE_SHA224
size_t length
Definition: keccak.h:122
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
M460 hardware cryptographic accelerator.
size_t size
Definition: sha1.h:73
SHA-1 algorithm context.
Definition: sha1.h:62
uint32_t reverseInt32(uint32_t value)
Reverse bit order in a 32-bit word.
Definition: cpu_endian.c:123
#define CRPT_HMAC_CTL_OPMODE_SHA3_512
#define CRPT_HMAC_CTL_OPMODE_SHA3_384
uint8_t buffer[64]
Definition: sha1.h:71
uint64_t totalSize
Definition: sha512.h:74
uint64_t totalSize
Definition: sha256.h:74
unsigned int uint_t
Definition: compiler_port.h:50
uint8_t buffer[1]
Definition: keccak.h:119
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
OsMutex m460CryptoMutex
Definition: m460_crypto.c:42
#define SHA512_DIGEST_SIZE
Definition: sha512.h:45
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.
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.