lpc55s6x_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file lpc55s6x_crypto_hash.c
3  * @brief LPC55S6x 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 "fsl_device_registers.h"
36 #include "fsl_hashcrypt.h"
37 #include "core/crypto.h"
40 #include "hash/hash_algorithms.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (LPC55S6X_CRYPTO_HASH_SUPPORT == ENABLED)
45 #if (SHA1_SUPPORT == ENABLED)
46 
47 /**
48  * @brief Digest a message using SHA-1
49  * @param[in] data Pointer to the message being hashed
50  * @param[in] length Length of the message
51  * @param[out] digest Pointer to the calculated digest
52  * @return Error code
53  **/
54 
55 error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
56 {
57  size_t n;
58  status_t status;
59  hashcrypt_hash_ctx_t context;
60 
61  //Size of the digest value
63 
64  //Acquire exclusive access to the HASHCRYPT module
66 
67  //Initialize hash computation
68  status = HASHCRYPT_SHA_Init(HASHCRYPT, &context, kHASHCRYPT_Sha1);
69 
70  //Check status code
71  if(status == kStatus_Success)
72  {
73  //Digest the message
74  HASHCRYPT_SHA_Update(HASHCRYPT, &context, data, length);
75  }
76 
77  //Check status code
78  if(status == kStatus_Success)
79  {
80  //Get the resulting digest value
81  HASHCRYPT_SHA_Finish(HASHCRYPT, &context, digest, &n);
82  }
83 
84  //Release exclusive access to the HASHCRYPT module
86 
87  //Return status code
88  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
89 }
90 
91 #endif
92 #if (SHA256_SUPPORT == ENABLED)
93 
94 /**
95  * @brief Digest a message using SHA-256
96  * @param[in] data Pointer to the message being hashed
97  * @param[in] length Length of the message
98  * @param[out] digest Pointer to the calculated digest
99  * @return Error code
100  **/
101 
102 error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
103 {
104  size_t n;
105  status_t status;
106  hashcrypt_hash_ctx_t context;
107 
108  //Size of the digest value
110 
111  //Acquire exclusive access to the HASHCRYPT module
113 
114  //Initialize hash computation
115  status = HASHCRYPT_SHA_Init(HASHCRYPT, &context, kHASHCRYPT_Sha256);
116 
117  //Check status code
118  if(status == kStatus_Success)
119  {
120  //Digest the message
121  HASHCRYPT_SHA_Update(HASHCRYPT, &context, data, length);
122  }
123 
124  //Check status code
125  if(status == kStatus_Success)
126  {
127  //Get the resulting digest value
128  HASHCRYPT_SHA_Finish(HASHCRYPT, &context, digest, &n);
129  }
130 
131  //Release exclusive access to the HASHCRYPT module
133 
134  //Return status code
135  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
136 }
137 
138 #endif
139 #endif
error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
uint8_t data[]
Definition: ethernet.h:222
LPC55S6x hardware cryptographic accelerator.
error_t
Error codes.
Definition: error.h:43
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:368
Collection of hash algorithms.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
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.
OsMutex lpc55s6xCryptoMutex
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
LPC55S6x hash hardware accelerator.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-1.