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-2020 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 1.9.8
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 sha384Oid[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02};
48 
49 //Common interface for hash algorithms
51 {
52  "SHA-384",
53  sha384Oid,
54  sizeof(sha384Oid),
55  sizeof(Sha384Context),
59  TRUE,
65 };
66 
67 
68 /**
69  * @brief Digest a message using SHA-384
70  * @param[in] data Pointer to the message being hashed
71  * @param[in] length Length of the message
72  * @param[out] digest Pointer to the calculated digest
73  * @return Error code
74  **/
75 
76 error_t sha384Compute(const void *data, size_t length, uint8_t *digest)
77 {
78  //Allocate a memory buffer to hold the SHA-384 context
79  Sha384Context *context = cryptoAllocMem(sizeof(Sha384Context));
80  //Failed to allocate memory?
81  if(context == NULL)
82  return ERROR_OUT_OF_MEMORY;
83 
84  //Initialize the SHA-384 context
85  sha384Init(context);
86  //Digest the message
87  sha384Update(context, data, length);
88  //Finalize the SHA-384 message digest
89  sha384Final(context, digest);
90 
91  //Free previously allocated memory
92  cryptoFreeMem(context);
93  //Successful processing
94  return NO_ERROR;
95 }
96 
97 
98 /**
99  * @brief Initialize SHA-384 message digest context
100  * @param[in] context Pointer to the SHA-384 context to initialize
101  **/
102 
103 void sha384Init(Sha384Context *context)
104 {
105  //Set initial hash value
106  context->h[0] = 0xCBBB9D5DC1059ED8;
107  context->h[1] = 0x629A292A367CD507;
108  context->h[2] = 0x9159015A3070DD17;
109  context->h[3] = 0x152FECD8F70E5939;
110  context->h[4] = 0x67332667FFC00B31;
111  context->h[5] = 0x8EB44A8768581511;
112  context->h[6] = 0xDB0C2E0D64F98FA7;
113  context->h[7] = 0x47B5481DBEFA4FA4;
114 
115  //Number of bytes in the buffer
116  context->size = 0;
117  //Total length of the message
118  context->totalSize = 0;
119 }
120 
121 
122 /**
123  * @brief Update the SHA-384 context with a portion of the message being hashed
124  * @param[in] context Pointer to the SHA-384 context
125  * @param[in] data Pointer to the buffer being hashed
126  * @param[in] length Length of the buffer
127  **/
128 
129 void sha384Update(Sha384Context *context, const void *data, size_t length)
130 {
131  //The function is defined in the exact same manner as SHA-512
132  sha512Update(context, data, length);
133 }
134 
135 
136 /**
137  * @brief Finish the SHA-384 message digest
138  * @param[in] context Pointer to the SHA-384 context
139  * @param[out] digest Calculated digest (optional parameter)
140  **/
141 
142 void sha384Final(Sha384Context *context, uint8_t *digest)
143 {
144  //The function is defined in the exact same manner as SHA-512
145  sha512Final(context, NULL);
146 
147  //Copy the resulting digest
148  if(digest != NULL)
149  osMemcpy(digest, context->digest, SHA384_DIGEST_SIZE);
150 }
151 
152 
153 /**
154  * @brief Finish the SHA-384 message digest (no padding is added)
155  * @param[in] context Pointer to the SHA-384 context
156  * @param[out] digest Calculated digest
157  **/
158 
159 void sha384FinalRaw(Sha384Context *context, uint8_t *digest)
160 {
161  uint_t i;
162 
163  //Convert from host byte order to big-endian byte order
164  for(i = 0; i < 8; i++)
165  {
166  context->h[i] = htobe64(context->h[i]);
167  }
168 
169  //Copy the resulting digest
170  osMemcpy(digest, context->digest, SHA384_DIGEST_SIZE);
171 
172  //Convert from big-endian byte order to host byte order
173  for(i = 0; i < 8; i++)
174  {
175  context->h[i] = betoh64(context->h[i]);
176  }
177 }
178 
179 #endif
uint8_t length
Definition: coap_common.h:190
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1028
uint8_t data[]
Definition: ethernet.h:209
void sha512Final(Sha512Context *context, uint8_t *digest)
Finish the SHA-512 message digest.
Definition: sha512.c:218
#define TRUE
Definition: os_port.h:50
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:129
#define SHA384_MIN_PAD_SIZE
Definition: sha384.h:43
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1029
size_t size
Definition: sha512.h:70
const HashAlgo sha384HashAlgo
Definition: sha384.c:50
#define osMemcpy(dest, src, length)
Definition: os_port.h:134
error_t
Error codes.
Definition: error.h:42
error_t sha384Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-384.
Definition: sha384.c:76
SHA-512 algorithm context.
Definition: sha512.h:58
General definitions for cryptographic algorithms.
#define htobe64(value)
Definition: cpu_endian.h:447
#define SHA384_BLOCK_SIZE
Definition: sha384.h:39
SHA-384 (Secure Hash Algorithm 384)
#define SHA384_DIGEST_SIZE
Definition: sha384.h:41
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1030
uint64_t h[8]
Definition: sha512.h:62
#define betoh64(value)
Definition: cpu_endian.h:455
void sha384Init(Sha384Context *context)
Initialize SHA-384 message digest context.
Definition: sha384.c:103
#define cryptoFreeMem(p)
Definition: crypto.h:630
uint8_t digest[64]
Definition: sha512.h:63
#define cryptoAllocMem(size)
Definition: crypto.h:625
void sha384FinalRaw(Sha384Context *context, uint8_t *digest)
Finish the SHA-384 message digest (no padding is added)
Definition: sha384.c:159
Common interface for hash algorithms.
Definition: crypto.h:1062
uint64_t totalSize
Definition: sha512.h:71
unsigned int uint_t
Definition: compiler_port.h:45
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1027
void sha384Final(Sha384Context *context, uint8_t *digest)
Finish the SHA-384 message digest.
Definition: sha384.c:142
const uint8_t sha384Oid[9]
Definition: sha384.c:47
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:1031
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.
Definition: sha512.c:179