lpc54xxx_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file lpc54xxx_crypto_hash.c
3  * @brief LPC54000 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.0
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_sha.h"
37 #include "core/crypto.h"
40 #include "hash/hash_algorithms.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (LPC54XXX_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  sha_ctx_t context;
60 
61  //Size of the digest value
63 
64  //Acquire exclusive access to the SHA module
66 
67  //Initialize hash computation
68  status = SHA_Init(SHA0, &context, kSHA_Sha1);
69 
70  //Check status code
71  if(status == kStatus_Success)
72  {
73  //Digest the message
74  SHA_Update(SHA0, &context, data, length, true);
75  }
76 
77  //Check status code
78  if(status == kStatus_Success)
79  {
80  //Get the resulting digest value
81  SHA_Finish(SHA0, &context, digest, &n);
82  }
83 
84  //Release exclusive access to the SHA 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  sha_ctx_t context;
107 
108  //Size of the digest value
110 
111  //Acquire exclusive access to the SHA module
113 
114  //Initialize hash computation
115  status = SHA_Init(SHA0, &context, kSHA_Sha256);
116 
117  //Check status code
118  if(status == kStatus_Success)
119  {
120  //Digest the message
121  SHA_Update(SHA0, &context, data, length, true);
122  }
123 
124  //Check status code
125  if(status == kStatus_Success)
126  {
127  //Get the resulting digest value
128  SHA_Finish(SHA0, &context, digest, &n);
129  }
130 
131  //Release exclusive access to the SHA module
133 
134  //Return status code
135  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
136 }
137 
138 #endif
139 #endif
General definitions for cryptographic algorithms.
Debugging facilities.
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
uint8_t data[]
Definition: ethernet.h:222
Collection of hash algorithms.
OsMutex lpc54xxxCryptoMutex
LPC54000 hardware cryptographic accelerator.
error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-1.
LPC54000 hash hardware accelerator.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
uint8_t length
Definition: tcp.h:368