stm32mp2xx_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file stm32mp2xx_crypto_hash.c
3  * @brief STM32MP2 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 "stm32mp2xx.h"
36 #include "stm32mp2xx_hal.h"
37 #include "core/crypto.h"
40 #include "hash/hash_algorithms.h"
41 #include "xof/keccak.h"
42 #include "debug.h"
43 
44 //Check crypto library configuration
45 #if (STM32MP2XX_CRYPTO_HASH_SUPPORT == ENABLED)
46 
47 
48 /**
49  * @brief HASH module initialization
50  * @return Error code
51  **/
52 
54 {
55  //Enable HASH peripheral clock
56  __HAL_RCC_HASH_CLK_ENABLE();
57 
58  //Successful processing
59  return NO_ERROR;
60 }
61 
62 
63 /**
64  * @brief Update hash value
65  * @param[in] algo Hash algorithm
66  * @param[in] data Pointer to the input buffer
67  * @param[in] length Length of the input buffer
68  * @param[in,out] h Intermediate hash value
69  * @param[in] hLen Length of the intermediate hash value, in words
70  **/
71 
72 void hashProcessData(uint32_t algo, const uint8_t *data, size_t length,
73  uint32_t *h, size_t hLen)
74 {
75  uint_t i;
76  size_t blockSize;
77 
78  //Get block size
79  if(algo == HASH_CR_ALGO_SHA1 || algo == HASH_CR_ALGO_SHA224 ||
80  algo == HASH_CR_ALGO_SHA256)
81  {
82  blockSize = 64;
83  }
84  else
85  {
86  blockSize = 128;
87  }
88 
89  //Acquire exclusive access to the HASH module
91 
92  //Select the relevant hash algorithm
93  HASH->CR = HASH_CR_DATATYPE_8B | algo;
94  //Initialize the hash processor by setting the INIT bit
95  HASH->CR |= HASH_CR_INIT;
96 
97  //SHA-1, SHA-224 or SHA-256 algorithm?
98  if(blockSize == 64)
99  {
100  //Restore initial hash value
101  for(i = 0; i < hLen; i++)
102  {
103  HASH->CSR[6 + i] = h[i];
104  HASH->CSR[14 + i] = h[i];
105  }
106  }
107  else
108  {
109  //Restore initial hash value
110  for(i = 0; i < hLen; i += 2)
111  {
112  HASH->CSR[6 + i] = h[i + 1];
113  HASH->CSR[7 + i] = h[i];
114  HASH->CSR[39 + i / 2] = h[i];
115  HASH->CSR[47 + i / 2] = h[i + 1];
116  }
117  }
118 
119  //Input data are processed in a block-by-block fashion
120  while(length >= blockSize)
121  {
122  //Write the first byte of the block
123  HASH->DIN = LOAD32LE(data);
124 
125  //Wait for the BUSY bit to be cleared
126  while((HASH->SR & HASH_SR_BUSY) != 0)
127  {
128  }
129 
130  //Write the rest of the block
131  HASH->DIN = LOAD32LE(data + 4);
132  HASH->DIN = LOAD32LE(data + 8);
133  HASH->DIN = LOAD32LE(data + 12);
134  HASH->DIN = LOAD32LE(data + 16);
135  HASH->DIN = LOAD32LE(data + 20);
136  HASH->DIN = LOAD32LE(data + 24);
137  HASH->DIN = LOAD32LE(data + 28);
138  HASH->DIN = LOAD32LE(data + 32);
139  HASH->DIN = LOAD32LE(data + 36);
140  HASH->DIN = LOAD32LE(data + 40);
141  HASH->DIN = LOAD32LE(data + 44);
142  HASH->DIN = LOAD32LE(data + 48);
143  HASH->DIN = LOAD32LE(data + 52);
144  HASH->DIN = LOAD32LE(data + 56);
145  HASH->DIN = LOAD32LE(data + 60);
146 
147  //128-octet data block?
148  if(blockSize == 128)
149  {
150  HASH->DIN = LOAD32LE(data + 64);
151  HASH->DIN = LOAD32LE(data + 68);
152  HASH->DIN = LOAD32LE(data + 72);
153  HASH->DIN = LOAD32LE(data + 76);
154  HASH->DIN = LOAD32LE(data + 80);
155  HASH->DIN = LOAD32LE(data + 84);
156  HASH->DIN = LOAD32LE(data + 88);
157  HASH->DIN = LOAD32LE(data + 92);
158  HASH->DIN = LOAD32LE(data + 96);
159  HASH->DIN = LOAD32LE(data + 100);
160  HASH->DIN = LOAD32LE(data + 104);
161  HASH->DIN = LOAD32LE(data + 108);
162  HASH->DIN = LOAD32LE(data + 112);
163  HASH->DIN = LOAD32LE(data + 116);
164  HASH->DIN = LOAD32LE(data + 120);
165  HASH->DIN = LOAD32LE(data + 124);
166  }
167 
168  //Advance data pointer
169  data += blockSize;
170  length -= blockSize;
171  }
172 
173  //Partial digest computation are triggered each time the application
174  //writes the first word of the next block
175  HASH->DIN = 0;
176 
177  //Wait for the BUSY bit to be cleared
178  while((HASH->SR & HASH_SR_BUSY) != 0)
179  {
180  }
181 
182  //SHA-1, SHA-224 or SHA-256 algorithm?
183  if(blockSize == 64)
184  {
185  //Save intermediate hash value
186  for(i = 0; i < hLen; i++)
187  {
188  h[i] = HASH->CSR[6 + i];
189  }
190  }
191  else
192  {
193  //Save intermediate hash value
194  for(i = 0; i < hLen; i += 2)
195  {
196  h[i] = HASH->CSR[7 + i];
197  h[i + 1] = HASH->CSR[6 + i];
198  }
199  }
200 
201  //Release exclusive access to the HASH module
203 }
204 
205 
206 #if (SHA1_SUPPORT == ENABLED)
207 
208 /**
209  * @brief Update the SHA-1 context with a portion of the message being hashed
210  * @param[in] context Pointer to the SHA-1 context
211  * @param[in] data Pointer to the buffer being hashed
212  * @param[in] length Length of the buffer
213  **/
214 
215 void sha1Update(Sha1Context *context, const void *data, size_t length)
216 {
217  size_t n;
218 
219  //Process the incoming data
220  while(length > 0)
221  {
222  //Check whether some data is pending in the buffer
223  if(context->size == 0 && length >= 64)
224  {
225  //The length must be a multiple of 64 bytes
226  n = length - (length % 64);
227 
228  //Update hash value
230  SHA1_DIGEST_SIZE / 4);
231 
232  //Update the SHA-1 context
233  context->totalSize += n;
234  //Advance the data pointer
235  data = (uint8_t *) data + n;
236  //Remaining bytes to process
237  length -= n;
238  }
239  else
240  {
241  //The buffer can hold at most 64 bytes
242  n = MIN(length, 64 - context->size);
243 
244  //Copy the data to the buffer
245  osMemcpy(context->buffer + context->size, data, n);
246 
247  //Update the SHA-1 context
248  context->size += n;
249  context->totalSize += n;
250  //Advance the data pointer
251  data = (uint8_t *) data + n;
252  //Remaining bytes to process
253  length -= n;
254 
255  //Check whether the buffer is full
256  if(context->size == 64)
257  {
258  //Update hash value
259  hashProcessData(HASH_CR_ALGO_SHA1, context->buffer, context->size,
260  context->h, SHA1_DIGEST_SIZE / 4);
261 
262  //Empty the buffer
263  context->size = 0;
264  }
265  }
266  }
267 }
268 
269 
270 /**
271  * @brief Process message in 16-word blocks
272  * @param[in] context Pointer to the SHA-1 context
273  **/
274 
276 {
277  //Update hash value
278  hashProcessData(HASH_CR_ALGO_SHA1, context->buffer, 64, context->h,
279  SHA1_DIGEST_SIZE / 4);
280 }
281 
282 #endif
283 #if (SHA256_SUPPORT == ENABLED)
284 
285 /**
286  * @brief Update the SHA-256 context with a portion of the message being hashed
287  * @param[in] context Pointer to the SHA-256 context
288  * @param[in] data Pointer to the buffer being hashed
289  * @param[in] length Length of the buffer
290  **/
291 
292 void sha256Update(Sha256Context *context, const void *data, size_t length)
293 {
294  size_t n;
295 
296  //Process the incoming data
297  while(length > 0)
298  {
299  //Check whether some data is pending in the buffer
300  if(context->size == 0 && length >= 64)
301  {
302  //The length must be a multiple of 64 bytes
303  n = length - (length % 64);
304 
305  //Update hash value
307  SHA256_DIGEST_SIZE / 4);
308 
309  //Update the SHA-256 context
310  context->totalSize += n;
311  //Advance the data pointer
312  data = (uint8_t *) data + n;
313  //Remaining bytes to process
314  length -= n;
315  }
316  else
317  {
318  //The buffer can hold at most 64 bytes
319  n = MIN(length, 64 - context->size);
320 
321  //Copy the data to the buffer
322  osMemcpy(context->buffer + context->size, data, n);
323 
324  //Update the SHA-256 context
325  context->size += n;
326  context->totalSize += n;
327  //Advance the data pointer
328  data = (uint8_t *) data + n;
329  //Remaining bytes to process
330  length -= n;
331 
332  //Check whether the buffer is full
333  if(context->size == 64)
334  {
335  //Update hash value
336  hashProcessData(HASH_CR_ALGO_SHA256, context->buffer, context->size,
337  context->h, SHA256_DIGEST_SIZE / 4);
338 
339  //Empty the buffer
340  context->size = 0;
341  }
342  }
343  }
344 }
345 
346 
347 /**
348  * @brief Process message in 16-word blocks
349  * @param[in] context Pointer to the SHA-256 context
350  **/
351 
353 {
354  //Update hash value
355  hashProcessData(HASH_CR_ALGO_SHA256, context->buffer, 64, context->h,
356  SHA256_DIGEST_SIZE / 4);
357 }
358 
359 #endif
360 #if (SHA512_SUPPORT == ENABLED)
361 
362 /**
363  * @brief Update the SHA-512 context with a portion of the message being hashed
364  * @param[in] context Pointer to the SHA-512 context
365  * @param[in] data Pointer to the buffer being hashed
366  * @param[in] length Length of the buffer
367  **/
368 
369 void sha512Update(Sha512Context *context, const void *data, size_t length)
370 {
371  size_t n;
372 
373  //Process the incoming data
374  while(length > 0)
375  {
376  //Check whether some data is pending in the buffer
377  if(context->size == 0 && length >= 128)
378  {
379  //The length must be a multiple of 128 bytes
380  n = length - (length % 128);
381 
382  //Update hash value
383  hashProcessData(HASH_CR_ALGO_SHA512, data, n, (uint32_t *) context->h,
384  SHA512_DIGEST_SIZE / 4);
385 
386  //Update the SHA-512 context
387  context->totalSize += n;
388  //Advance the data pointer
389  data = (uint8_t *) data + n;
390  //Remaining bytes to process
391  length -= n;
392  }
393  else
394  {
395  //The buffer can hold at most 128 bytes
396  n = MIN(length, 128 - context->size);
397 
398  //Copy the data to the buffer
399  osMemcpy(context->buffer + context->size, data, n);
400 
401  //Update the SHA-512 context
402  context->size += n;
403  context->totalSize += n;
404  //Advance the data pointer
405  data = (uint8_t *) data + n;
406  //Remaining bytes to process
407  length -= n;
408 
409  //Check whether the buffer is full
410  if(context->size == 128)
411  {
412  //Update hash value
413  hashProcessData(HASH_CR_ALGO_SHA512, context->buffer, context->size,
414  (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
415 
416  //Empty the buffer
417  context->size = 0;
418  }
419  }
420  }
421 }
422 
423 
424 /**
425  * @brief Process message in 16-word blocks
426  * @param[in] context Pointer to the SHA-512 context
427  **/
428 
430 {
431  //Update hash value
433  (uint32_t *) context->h, SHA512_DIGEST_SIZE / 4);
434 }
435 
436 #endif
437 #if (KECCAK_SUPPORT == ENABLED)
438 
439 
440 /**
441  * @brief Update state array
442  * @param[in] data Pointer to the input buffer
443  * @param[in] length Length of the input buffer
444  * @param[in] blockSize Block size
445  * @param[in,out] a State array
446  **/
447 
448 void keccakProcessData(const uint8_t *data, size_t length, size_t blockSize,
449  uint32_t *a)
450 {
451  uint_t i;
452  uint32_t algo;
453 
454  //Check block size
455  if(blockSize == 72)
456  {
457  algo = HASH_CR_ALGO_SHA3_512;
458  }
459  else if(blockSize == 104)
460  {
461  algo = HASH_CR_ALGO_SHA3_384;
462  }
463  else if(blockSize == 136)
464  {
465  algo = HASH_CR_ALGO_SHA3_256;
466  }
467  else
468  {
469  algo = HASH_CR_ALGO_SHA3_224;
470  }
471 
472  //Acquire exclusive access to the HASH module
474 
475  //Select the relevant hash algorithm
476  HASH->CR = HASH_CR_DATATYPE_8B | algo;
477  //Initialize the hash processor by setting the INIT bit
478  HASH->CR |= HASH_CR_INIT;
479 
480  //Restore state array
481  for(i = 0; i < 50; i++)
482  {
483  HASH->CSR[6 + i] = HTOBE32(a[i]);
484  }
485 
486  //Valid buffer?
487  if(data != NULL)
488  {
489  //Input data are processed in a block-by-block fashion
490  while(length >= blockSize)
491  {
492  //Write the first byte of the block
493  HASH->DIN = LOAD32LE(data);
494 
495  //Wait for the BUSY bit to be cleared
496  while((HASH->SR & HASH_SR_BUSY) != 0)
497  {
498  }
499 
500  //Write the rest of the block
501  for(i = 4; i < blockSize; i += 4)
502  {
503  HASH->DIN = LOAD32LE(data + i);
504  }
505 
506  //Advance data pointer
507  data += blockSize;
508  length -= blockSize;
509  }
510  }
511  else
512  {
513  //Input data are processed in a block-by-block fashion
514  while(length >= blockSize)
515  {
516  //Write the first byte of the block
517  HASH->DIN = 0;
518 
519  //Wait for the BUSY bit to be cleared
520  while((HASH->SR & HASH_SR_BUSY) != 0)
521  {
522  }
523 
524  //Write the rest of the block
525  for(i = 4; i < blockSize; i += 4)
526  {
527  HASH->DIN = 0;
528  }
529 
530  //Decrement length
531  length -= blockSize;
532  }
533  }
534 
535  //Partial digest computation are triggered each time the application
536  //writes the first word of the next block
537  HASH->DIN = 0;
538 
539  //Wait for the BUSY bit to be cleared
540  while((HASH->SR & HASH_SR_BUSY) != 0)
541  {
542  }
543 
544  //Save state array
545  for(i = 0; i < 50; i++)
546  {
547  a[i] = HASH->CSR[6 + i];
548  a[i] = BETOH32(a[i]);
549  }
550 
551  //Release exclusive access to the HASH module
553 }
554 
555 
556 /**
557  * @brief Absorb data
558  * @param[in] context Pointer to the Keccak context
559  * @param[in] input Pointer to the buffer being hashed
560  * @param[in] length Length of the buffer
561  **/
562 
563 void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
564 {
565  size_t n;
566 
567  //Process the incoming data
568  while(length > 0)
569  {
570  //Check whether some data is pending in the buffer
571  if(context->length == 0 && length >= context->blockSize)
572  {
573  //The length must be a multiple of the block size
574  n = length - (length % context->blockSize);
575 
576  //Absorb the current block
577  keccakProcessData(input, n, context->blockSize,
578  (uint32_t *) context->a);
579 
580  //Advance the data pointer
581  input = (uint8_t *) input + n;
582  //Remaining bytes to process
583  length -= n;
584  }
585  else
586  {
587  //The buffer can hold at most one block
588  n = MIN(length, context->blockSize - context->length);
589 
590  //Copy the data to the buffer
591  osMemcpy(context->buffer + context->length, input, n);
592  //Update the length of the buffer
593  context->length += n;
594 
595  //Advance the data pointer
596  input = (uint8_t *) input + n;
597  //Remaining bytes to process
598  length -= n;
599 
600  //Check whether the buffer is full
601  if(context->length == context->blockSize)
602  {
603  //Absorb the current block
604  keccakProcessData(context->buffer, context->length,
605  context->blockSize, (uint32_t *) context->a);
606 
607  //Empty the buffer
608  context->length = 0;
609  }
610  }
611  }
612 }
613 
614 
615 /**
616  * @brief Block permutation
617  * @param[in] context Pointer to the Keccak context
618  **/
619 
621 {
622  //Perform block permutation
623  keccakProcessData(NULL, context->blockSize, context->blockSize,
624  (uint32_t *) context->a);
625 }
626 
627 #endif
628 #endif
keccak_lane_t a[5][5]
Definition: keccak.h:113
#define HASH_CR_ALGO_SHA3_224
uint8_t a
Definition: ndp.h:411
SHA-256 algorithm context.
Definition: sha256.h:62
STM32MP2 hash hardware accelerator.
uint8_t data[]
Definition: ethernet.h:222
void keccakPermutBlock(KeccakContext *context)
Block permutation.
Keccak context.
Definition: keccak.h:110
size_t size
Definition: sha256.h:73
uint32_t h[8]
Definition: sha256.h:65
#define HASH_CR_ALGO_SHA3_512
#define BETOH32(value)
Definition: cpu_endian.h:451
#define HASH_CR_ALGO_SHA1
error_t hashInit(void)
HASH module initialization.
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:74
size_t size
Definition: sha512.h:73
void sha512ProcessBlock(Sha512Context *context)
Process message in 16-word blocks.
uint8_t h
Definition: ndp.h:302
void keccakProcessData(const uint8_t *data, size_t length, size_t blockSize, uint32_t *a)
Update state array.
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define HASH_CR_ALGO_SHA256
error_t
Error codes.
Definition: error.h:43
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
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:65
General definitions for cryptographic algorithms.
uint8_t buffer[128]
Definition: sha512.h:71
#define HASH_CR_ALGO_SHA3_256
#define HASH_CR_ALGO_SHA512
STM32MP2 hardware cryptographic accelerator.
uint8_t length
Definition: tcp.h:368
uint8_t buffer[64]
Definition: sha256.h:71
#define HASH_CR_ALGO_SHA224
#define MIN(a, b)
Definition: os_port.h:63
Collection of hash algorithms.
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
void sha512Update(Sha512Context *context, const void *data, size_t length)
Update the SHA-512 context with a portion of the message being hashed.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
Keccak sponge function.
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.
OsMutex stm32mp2xxCryptoMutex
size_t length
Definition: keccak.h:122
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define HTOBE32(value)
Definition: cpu_endian.h:443
size_t size
Definition: sha1.h:73
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:71
#define HASH_CR_DATATYPE_8B
uint64_t totalSize
Definition: sha512.h:74
#define LOAD32LE(p)
Definition: cpu_endian.h:203
uint64_t totalSize
Definition: sha256.h:74
unsigned int uint_t
Definition: compiler_port.h:50
#define HASH_CR_ALGO_SHA3_384
uint8_t buffer[1]
Definition: keccak.h:119
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
#define SHA512_DIGEST_SIZE
Definition: sha512.h:45
@ NO_ERROR
Success.
Definition: error.h:44
void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
Absorb data.
Debugging facilities.