sha384.c
Go to the documentation of this file.
1 /**
2  * @file sha384.c
3  * @brief SHA-384 (Secure Hash Algorithm 384)
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  * @section Description
28  *
29  * SHA-384 is a secure hash algorithm for computing a condensed representation
30  * of an electronic message. Refer to FIPS 180-4 for more details
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 2.4.4
34  **/
35 
36 //Switch to the appropriate trace level
37 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
38 
39 //Dependencies
40 #include "core/crypto.h"
41 #include "hash/sha384.h"
42 
43 //Check crypto library configuration
44 #if (SHA384_SUPPORT == ENABLED)
45 
46 //SHA-384 object identifier (2.16.840.1.101.3.4.2.2)
47 const uint8_t SHA384_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02};
48 
49 //Common interface for hash algorithms
51 {
52  "SHA-384",
53  SHA384_OID,
54  sizeof(SHA384_OID),
55  sizeof(Sha384Context),
59  TRUE,
64 #if ((defined(MIMXRT1160_CRYPTO_HASH_SUPPORT) && MIMXRT1160_CRYPTO_HASH_SUPPORT == ENABLED) || \
65  (defined(MIMXRT1170_CRYPTO_HASH_SUPPORT) && MIMXRT1170_CRYPTO_HASH_SUPPORT == ENABLED))
66  NULL,
67 #else
69 #endif
70 };
71 
72 
73 /**
74  * @brief Digest a message using SHA-384
75  * @param[in] data Pointer to the message being hashed
76  * @param[in] length Length of the message
77  * @param[out] digest Pointer to the calculated digest
78  * @return Error code
79  **/
80 
81 __weak_func error_t sha384Compute(const void *data, size_t length, uint8_t *digest)
82 {
83 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
84  Sha384Context *context;
85 #else
86  Sha384Context context[1];
87 #endif
88 
89  //Check parameters
90  if(data == NULL && length != 0)
92 
93  if(digest == NULL)
95 
96 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
97  //Allocate a memory buffer to hold the SHA-384 context
98  context = cryptoAllocMem(sizeof(Sha384Context));
99  //Failed to allocate memory?
100  if(context == NULL)
101  return ERROR_OUT_OF_MEMORY;
102 #endif
103 
104  //Initialize the SHA-384 context
105  sha384Init(context);
106  //Digest the message
107  sha384Update(context, data, length);
108  //Finalize the SHA-384 message digest
109  sha384Final(context, digest);
110 
111 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
112  //Free previously allocated memory
113  cryptoFreeMem(context);
114 #endif
115 
116  //Successful processing
117  return NO_ERROR;
118 }
119 
120 
121 /**
122  * @brief Initialize SHA-384 message digest context
123  * @param[in] context Pointer to the SHA-384 context to initialize
124  **/
125 
126 __weak_func void sha384Init(Sha384Context *context)
127 {
128  //Set initial hash value
129  context->h[0] = 0xCBBB9D5DC1059ED8;
130  context->h[1] = 0x629A292A367CD507;
131  context->h[2] = 0x9159015A3070DD17;
132  context->h[3] = 0x152FECD8F70E5939;
133  context->h[4] = 0x67332667FFC00B31;
134  context->h[5] = 0x8EB44A8768581511;
135  context->h[6] = 0xDB0C2E0D64F98FA7;
136  context->h[7] = 0x47B5481DBEFA4FA4;
137 
138  //Number of bytes in the buffer
139  context->size = 0;
140  //Total length of the message
141  context->totalSize = 0;
142 }
143 
144 
145 /**
146  * @brief Update the SHA-384 context with a portion of the message being hashed
147  * @param[in] context Pointer to the SHA-384 context
148  * @param[in] data Pointer to the buffer being hashed
149  * @param[in] length Length of the buffer
150  **/
151 
152 __weak_func void sha384Update(Sha384Context *context, const void *data, size_t length)
153 {
154  //The function is defined in the exact same manner as SHA-512
155  sha512Update(context, data, length);
156 }
157 
158 
159 /**
160  * @brief Finish the SHA-384 message digest
161  * @param[in] context Pointer to the SHA-384 context
162  * @param[out] digest Calculated digest (optional parameter)
163  **/
164 
165 __weak_func void sha384Final(Sha384Context *context, uint8_t *digest)
166 {
167  //The function is defined in the exact same manner as SHA-512
168  sha512Final(context, NULL);
169 
170  //Copy the resulting digest
171  if(digest != NULL)
172  {
173  osMemcpy(digest, context->digest, SHA384_DIGEST_SIZE);
174  }
175 }
176 
177 
178 /**
179  * @brief Finish the SHA-384 message digest (no padding added)
180  * @param[in] context Pointer to the SHA-384 context
181  * @param[out] digest Calculated digest
182  **/
183 
184 __weak_func void sha384FinalRaw(Sha384Context *context, uint8_t *digest)
185 {
186  uint_t i;
187 
188  //Convert from host byte order to big-endian byte order
189  for(i = 0; i < 8; i++)
190  {
191  context->h[i] = htobe64(context->h[i]);
192  }
193 
194  //Copy the resulting digest
195  osMemcpy(digest, context->digest, SHA384_DIGEST_SIZE);
196 
197  //Convert from big-endian byte order to host byte order
198  for(i = 0; i < 8; i++)
199  {
200  context->h[i] = betoh64(context->h[i]);
201  }
202 }
203 
204 #endif
__weak_func void sha384FinalRaw(Sha384Context *context, uint8_t *digest)
Finish the SHA-384 message digest (no padding added)
Definition: sha384.c:184
void(* HashAlgoInit)(void *context)
Definition: crypto.h:991
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define SHA384_MIN_PAD_SIZE
Definition: sha384.h:43
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:993
__weak_func void sha384Init(Sha384Context *context)
Initialize SHA-384 message digest context.
Definition: sha384.c:126
size_t size
Definition: sha512.h:73
const HashAlgo sha384HashAlgo
Definition: sha384.c:50
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
error_t
Error codes.
Definition: error.h:43
SHA-512 algorithm context.
Definition: sha512.h:62
General definitions for cryptographic algorithms.
#define htobe64(value)
Definition: cpu_endian.h:447
#define SHA384_BLOCK_SIZE
Definition: sha384.h:39
uint8_t length
Definition: tcp.h:368
SHA-384 (Secure Hash Algorithm 384)
#define SHA384_DIGEST_SIZE
Definition: sha384.h:41
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:995
uint64_t h[8]
Definition: sha512.h:65
#define betoh64(value)
Definition: cpu_endian.h:455
void sha512Final(Sha512Context *context, uint8_t *digest)
Finish the SHA-512 message digest.
#define cryptoFreeMem(p)
Definition: crypto.h:791
uint8_t digest[64]
Definition: sha512.h:66
__weak_func void sha384Final(Sha384Context *context, uint8_t *digest)
Finish the SHA-384 message digest.
Definition: sha384.c:165
__weak_func void sha384Update(Sha384Context *context, const void *data, size_t length)
Update the SHA-384 context with a portion of the message being hashed.
Definition: sha384.c:152
#define cryptoAllocMem(size)
Definition: crypto.h:786
Common interface for hash algorithms.
Definition: crypto.h:1046
uint64_t totalSize
Definition: sha512.h:74
__weak_func error_t sha384Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-384.
Definition: sha384.c:81
unsigned int uint_t
Definition: compiler_port.h:50
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:988
const uint8_t SHA384_OID[9]
Definition: sha384.c:47
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:997
@ 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.