mimxrt1180_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file mimxrt1180_crypto_hash.c
3  * @brief i.MX RT1180 hash hardware accelerator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "fsl_device_registers.h"
36 #include "ele_crypto.h"
37 #include "core/crypto.h"
40 #include "hash/hash_algorithms.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (MIMXRT1180_CRYPTO_HASH_SUPPORT == ENABLED)
45 
46 //ELE hash context
47 SDK_ALIGN(static ele_hash_ctx_t eleHashContext, 8);
48 //ELE digest output
49 SDK_ALIGN(static uint8_t eleDigestOut[64], 8);
50 
51 
52 #if (SHA224_SUPPORT == ENABLED)
53 
54 /**
55  * @brief Digest a message using SHA-224
56  * @param[in] data Pointer to the message being hashed
57  * @param[in] length Length of the message
58  * @param[out] digest Pointer to the calculated digest
59  * @return Error code
60  **/
61 
62 error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
63 {
64  uint32_t n;
65  status_t status;
66 
67  //Acquire exclusive access to the ELE module
69 
70  //Digest the message
71  status = ELE_Hash(MU_APPS_S3MUA, data, length, eleDigestOut,
72  SHA224_DIGEST_SIZE, &n, kELE_Sha224);
73 
74  //Release exclusive access to the ELE module
76 
77  //Copy the resulting digest
78  osMemcpy(digest, eleDigestOut, SHA224_DIGEST_SIZE);
79 
80  //Return status code
81  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
82 }
83 
84 
85 /**
86  * @brief Initialize SHA-224 message digest context
87  * @param[in] context Pointer to the SHA-224 context to initialize
88  **/
89 
90 void sha224Init(Sha224Context *context)
91 {
92  //Acquire exclusive access to the ELE module
94 
95  //Initialize hash computation
96  ELE_Hash_Init(MU_APPS_S3MUA, &eleHashContext, kELE_Sha224);
97  //Save hash context
98  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
99 
100  //Release exclusive access to the ELE module
102 }
103 
104 
105 /**
106  * @brief Update the SHA-224 context with a portion of the message being hashed
107  * @param[in] context Pointer to the SHA-224 context
108  * @param[in] data Pointer to the buffer being hashed
109  * @param[in] length Length of the buffer
110  **/
111 
112 void sha224Update(Sha224Context *context, const void *data, size_t length)
113 {
114  //Acquire exclusive access to the ELE module
116 
117  //Restore hash context
118  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
119  //Digest the message
120  ELE_Hash_Update(MU_APPS_S3MUA, &eleHashContext, kELE_Sha224, data, length);
121  //Save hash context
122  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
123 
124  //Release exclusive access to the ELE module
126 }
127 
128 
129 /**
130  * @brief Finish the SHA-224 message digest
131  * @param[in] context Pointer to the SHA-224 context
132  * @param[out] digest Calculated digest
133  **/
134 
135 void sha224Final(Sha224Context *context, uint8_t *digest)
136 {
137  uint32_t n;
138 
139  //Acquire exclusive access to the ELE module
141 
142  //Restore hash context
143  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
144 
145  //Finalize hash computation
146  ELE_Hash_Finish(MU_APPS_S3MUA, &eleHashContext, kELE_Sha224,
147  eleDigestOut, SHA224_DIGEST_SIZE, &n, NULL, 0);
148 
149  //Release exclusive access to the ELE module
151 
152  //Copy the resulting digest
153  osMemcpy(digest, eleDigestOut, SHA224_DIGEST_SIZE);
154 }
155 
156 #endif
157 #if (SHA256_SUPPORT == ENABLED)
158 
159 /**
160  * @brief Digest a message using SHA-256
161  * @param[in] data Pointer to the message being hashed
162  * @param[in] length Length of the message
163  * @param[out] digest Pointer to the calculated digest
164  * @return Error code
165  **/
166 
167 error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
168 {
169  uint32_t n;
170  status_t status;
171 
172  //Acquire exclusive access to the ELE module
174 
175  //Digest the message
176  status = ELE_Hash(MU_APPS_S3MUA, data, length, eleDigestOut,
177  SHA256_DIGEST_SIZE, &n, kELE_Sha256);
178 
179  //Release exclusive access to the ELE module
181 
182  //Copy the resulting digest
183  osMemcpy(digest, eleDigestOut, SHA256_DIGEST_SIZE);
184 
185  //Return status code
186  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
187 }
188 
189 
190 /**
191  * @brief Initialize SHA-256 message digest context
192  * @param[in] context Pointer to the SHA-256 context to initialize
193  **/
194 
195 void sha256Init(Sha256Context *context)
196 {
197  //Acquire exclusive access to the ELE module
199 
200  //Initialize hash computation
201  ELE_Hash_Init(MU_APPS_S3MUA, &eleHashContext, kELE_Sha256);
202  //Save hash context
203  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
204 
205  //Release exclusive access to the ELE module
207 }
208 
209 
210 /**
211  * @brief Update the SHA-256 context with a portion of the message being hashed
212  * @param[in] context Pointer to the SHA-256 context
213  * @param[in] data Pointer to the buffer being hashed
214  * @param[in] length Length of the buffer
215  **/
216 
217 void sha256Update(Sha256Context *context, const void *data, size_t length)
218 {
219  //Acquire exclusive access to the ELE module
221 
222  //Restore hash context
223  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
224  //Digest the message
225  ELE_Hash_Update(MU_APPS_S3MUA, &eleHashContext, kELE_Sha256, data, length);
226  //Save hash context
227  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
228 
229  //Release exclusive access to the ELE module
231 }
232 
233 
234 /**
235  * @brief Finish the SHA-256 message digest
236  * @param[in] context Pointer to the SHA-256 context
237  * @param[out] digest Calculated digest
238  **/
239 
240 void sha256Final(Sha256Context *context, uint8_t *digest)
241 {
242  uint32_t n;
243 
244  //Acquire exclusive access to the ELE module
246 
247  //Restore hash context
248  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
249 
250  //Finalize hash computation
251  ELE_Hash_Finish(MU_APPS_S3MUA, &eleHashContext, kELE_Sha256,
252  eleDigestOut, SHA256_DIGEST_SIZE, &n, NULL, 0);
253 
254  //Release exclusive access to the ELE module
256 
257  //Copy the resulting digest
258  osMemcpy(digest, eleDigestOut, SHA256_DIGEST_SIZE);
259 }
260 
261 #endif
262 #if (SHA384_SUPPORT == ENABLED)
263 
264 /**
265  * @brief Digest a message using SHA-384
266  * @param[in] data Pointer to the message being hashed
267  * @param[in] length Length of the message
268  * @param[out] digest Pointer to the calculated digest
269  * @return Error code
270  **/
271 
272 error_t sha384Compute(const void *data, size_t length, uint8_t *digest)
273 {
274  uint32_t n;
275  status_t status;
276 
277  //Acquire exclusive access to the ELE module
279 
280  //Digest the message
281  status = ELE_Hash(MU_APPS_S3MUA, data, length, eleDigestOut,
282  SHA384_DIGEST_SIZE, &n, kELE_Sha384);
283 
284  //Release exclusive access to the ELE module
286 
287  //Copy the resulting digest
288  osMemcpy(digest, eleDigestOut, SHA384_DIGEST_SIZE);
289 
290  //Return status code
291  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
292 }
293 
294 
295 /**
296  * @brief Initialize SHA-384 message digest context
297  * @param[in] context Pointer to the SHA-384 context to initialize
298  **/
299 
300 void sha384Init(Sha384Context *context)
301 {
302  //Acquire exclusive access to the ELE module
304 
305  //Initialize hash computation
306  ELE_Hash_Init(MU_APPS_S3MUA, &eleHashContext, kELE_Sha384);
307  //Save hash context
308  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
309 
310  //Release exclusive access to the ELE module
312 }
313 
314 
315 /**
316  * @brief Update the SHA-384 context with a portion of the message being hashed
317  * @param[in] context Pointer to the SHA-384 context
318  * @param[in] data Pointer to the buffer being hashed
319  * @param[in] length Length of the buffer
320  **/
321 
322 void sha384Update(Sha384Context *context, const void *data, size_t length)
323 {
324  //Acquire exclusive access to the ELE module
326 
327  //Restore hash context
328  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
329  //Digest the message
330  ELE_Hash_Update(MU_APPS_S3MUA, &eleHashContext, kELE_Sha384, data, length);
331  //Save hash context
332  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
333 
334  //Release exclusive access to the ELE module
336 }
337 
338 
339 /**
340  * @brief Finish the SHA-384 message digest
341  * @param[in] context Pointer to the SHA-384 context
342  * @param[out] digest Calculated digest
343  **/
344 
345 void sha384Final(Sha384Context *context, uint8_t *digest)
346 {
347  uint32_t n;
348 
349  //Acquire exclusive access to the ELE module
351 
352  //Restore hash context
353  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
354 
355  //Finalize hash computation
356  ELE_Hash_Finish(MU_APPS_S3MUA, &eleHashContext, kELE_Sha384,
357  eleDigestOut, SHA384_DIGEST_SIZE, &n, NULL, 0);
358 
359  //Release exclusive access to the ELE module
361 
362  //Copy the resulting digest
363  osMemcpy(digest, eleDigestOut, SHA384_DIGEST_SIZE);
364 }
365 
366 #endif
367 #if (SHA512_SUPPORT == ENABLED)
368 
369 /**
370  * @brief Digest a message using SHA-512
371  * @param[in] data Pointer to the message being hashed
372  * @param[in] length Length of the message
373  * @param[out] digest Pointer to the calculated digest
374  * @return Error code
375  **/
376 
377 error_t sha512Compute(const void *data, size_t length, uint8_t *digest)
378 {
379  uint32_t n;
380  status_t status;
381 
382  //Acquire exclusive access to the ELE module
384 
385  //Digest the message
386  status = ELE_Hash(MU_APPS_S3MUA, data, length, eleDigestOut,
387  SHA512_DIGEST_SIZE, &n, kELE_Sha512);
388 
389  //Release exclusive access to the ELE module
391 
392  //Copy the resulting digest
393  osMemcpy(digest, eleDigestOut, SHA512_DIGEST_SIZE);
394 
395  //Return status code
396  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
397 }
398 
399 
400 /**
401  * @brief Initialize SHA-512 message digest context
402  * @param[in] context Pointer to the SHA-512 context to initialize
403  **/
404 
405 void sha512Init(Sha512Context *context)
406 {
407  //Acquire exclusive access to the ELE module
409 
410  //Initialize hash computation
411  ELE_Hash_Init(MU_APPS_S3MUA, &eleHashContext, kELE_Sha512);
412  //Save hash context
413  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
414 
415  //Release exclusive access to the ELE module
417 }
418 
419 
420 /**
421  * @brief Update the SHA-512 context with a portion of the message being hashed
422  * @param[in] context Pointer to the SHA-512 context
423  * @param[in] data Pointer to the buffer being hashed
424  * @param[in] length Length of the buffer
425  **/
426 
427 void sha512Update(Sha512Context *context, const void *data, size_t length)
428 {
429  //Acquire exclusive access to the ELE module
431 
432  //Restore hash context
433  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
434  //Digest the message
435  ELE_Hash_Update(MU_APPS_S3MUA, &eleHashContext, kELE_Sha512, data, length);
436  //Save hash context
437  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
438 
439  //Release exclusive access to the ELE module
441 }
442 
443 
444 /**
445  * @brief Finish the SHA-512 message digest
446  * @param[in] context Pointer to the SHA-512 context
447  * @param[out] digest Calculated digest
448  **/
449 
450 void sha512Final(Sha512Context *context, uint8_t *digest)
451 {
452  uint32_t n;
453 
454  //Acquire exclusive access to the ELE module
456 
457  //Restore hash context
458  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
459 
460  //Finalize hash computation
461  ELE_Hash_Finish(MU_APPS_S3MUA, &eleHashContext, kELE_Sha512,
462  eleDigestOut, SHA512_DIGEST_SIZE, &n, NULL, 0);
463 
464  //Release exclusive access to the ELE module
466 
467  //Copy the resulting digest
468  osMemcpy(digest, eleDigestOut, SHA512_DIGEST_SIZE);
469 }
470 
471 #endif
472 #if (SM3_SUPPORT == ENABLED)
473 
474 /**
475  * @brief Digest a message using SM3
476  * @param[in] data Pointer to the message being hashed
477  * @param[in] length Length of the message
478  * @param[out] digest Pointer to the calculated digest
479  * @return Error code
480  **/
481 
482 error_t sm3Compute(const void *data, size_t length, uint8_t *digest)
483 {
484  uint32_t n;
485  status_t status;
486 
487  //Acquire exclusive access to the ELE module
489 
490  //Digest the message
491  status = ELE_Hash(MU_APPS_S3MUA, data, length, eleDigestOut,
492  SM3_DIGEST_SIZE, &n, kELE_SM3256);
493 
494  //Release exclusive access to the ELE module
496 
497  //Copy the resulting digest
498  osMemcpy(digest, eleDigestOut, SM3_DIGEST_SIZE);
499 
500  //Return status code
501  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
502 }
503 
504 
505 /**
506  * @brief Initialize SM3 message digest context
507  * @param[in] context Pointer to the SM3 context to initialize
508  **/
509 
510 void sm3Init(Sm3Context *context)
511 {
512  //Acquire exclusive access to the ELE module
514 
515  //Initialize hash computation
516  ELE_Hash_Init(MU_APPS_S3MUA, &eleHashContext, kELE_SM3256);
517  //Save hash context
518  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
519 
520  //Release exclusive access to the ELE module
522 }
523 
524 
525 /**
526  * @brief Update the SM3 context with a portion of the message being hashed
527  * @param[in] context Pointer to the SM3 context
528  * @param[in] data Pointer to the buffer being hashed
529  * @param[in] length Length of the buffer
530  **/
531 
532 void sm3Update(Sm3Context *context, const void *data, size_t length)
533 {
534  //Acquire exclusive access to the ELE module
536 
537  //Restore hash context
538  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
539  //Digest the message
540  ELE_Hash_Update(MU_APPS_S3MUA, &eleHashContext, kELE_SM3256, data, length);
541  //Save hash context
542  osMemcpy(&context->eleContext, &eleHashContext, sizeof(ele_hash_ctx_t));
543 
544  //Release exclusive access to the ELE module
546 }
547 
548 
549 /**
550  * @brief Finish the SM3 message digest
551  * @param[in] context Pointer to the SM3 context
552  * @param[out] digest Calculated digest
553  **/
554 
555 void sm3Final(Sm3Context *context, uint8_t *digest)
556 {
557  uint32_t n;
558 
559  //Acquire exclusive access to the ELE module
561 
562  //Restore hash context
563  osMemcpy(&eleHashContext, &context->eleContext, sizeof(ele_hash_ctx_t));
564 
565  //Finalize hash computation
566  ELE_Hash_Finish(MU_APPS_S3MUA, &eleHashContext, kELE_SM3256,
567  eleDigestOut, SM3_DIGEST_SIZE, &n, NULL, 0);
568 
569  //Release exclusive access to the ELE module
571 
572  //Copy the resulting digest
573  osMemcpy(digest, eleDigestOut, SM3_DIGEST_SIZE);
574 }
575 
576 #endif
577 #endif
void sha512Init(Sha512Context *context)
Initialize SHA-512 message digest context.
error_t sha512Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-512.
void sha256Init(Sha256Context *context)
Initialize SHA-256 message digest context.
SHA-256 algorithm context.
Definition: sha256.h:62
void sm3Init(Sm3Context *context)
Initialize SM3 message digest context.
uint8_t data[]
Definition: ethernet.h:224
void sha224Init(Sha224Context *context)
Initialize SHA-224 message digest context.
i.MX RT1180 hash hardware accelerator
#define SHA224_DIGEST_SIZE
Definition: sha224.h:41
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
SM3 algorithm context.
Definition: sm3.h:62
void sha384Init(Sha384Context *context)
Initialize SHA-384 message digest context.
void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
void sha384Update(Sha384Context *context, const void *data, size_t length)
Update the SHA-384 context with a portion of the message being hashed.
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
SHA-512 algorithm context.
Definition: sha512.h:62
General definitions for cryptographic algorithms.
void sha224Final(Sha224Context *context, uint8_t *digest)
Finish the SHA-224 message digest.
OsMutex mimxrt1180CryptoMutex
uint8_t length
Definition: tcp.h:375
error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-224.
#define SHA384_DIGEST_SIZE
Definition: sha384.h:41
Collection of hash algorithms.
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.
i.MX RT1180 hardware cryptographic accelerator (ELE)
void sha384Final(Sha384Context *context, uint8_t *digest)
Finish the SHA-384 message digest.
void sha512Final(Sha512Context *context, uint8_t *digest)
Finish the SHA-512 message digest.
SDK_ALIGN(static ele_hash_ctx_t eleHashContext, 8)
error_t sha384Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-384.
error_t sm3Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SM3.
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
void sha224Update(Sha224Context *context, const void *data, size_t length)
Update the SHA-224 context with a portion of the message being hashed.
error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
#define SHA512_DIGEST_SIZE
Definition: sha512.h:45
void sm3Update(Sm3Context *context, const void *data, size_t length)
Update the SM3 context with a portion of the message being hashed.
@ NO_ERROR
Success.
Definition: error.h:44
void sha512Update(Sha512Context *context, const void *data, size_t length)
Update the SHA-512 context with a portion of the message being hashed.
Debugging facilities.
void sm3Final(Sm3Context *context, uint8_t *digest)
Finish the SM3 message digest.
#define SM3_DIGEST_SIZE
Definition: sm3.h:45