sha512_224.c
Go to the documentation of this file.
1 /**
2  * @file sha512_224.c
3  * @brief SHA-512/224 (Secure Hash Algorithm)
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-512/224 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/sha512_224.h"
42 
43 //Check crypto library configuration
44 #if (SHA512_224_SUPPORT == ENABLED)
45 
46 //SHA-512/224 object identifier (2.16.840.1.101.3.4.2.5)
47 const uint8_t SHA512_224_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05};
48 
49 //Common interface for hash algorithms
51 {
52  "SHA-512/224",
54  sizeof(SHA512_224_OID),
55  sizeof(Sha512_224Context),
59  TRUE,
64  NULL
65 };
66 
67 
68 /**
69  * @brief Digest a message using SHA-512/224
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 __weak_func error_t sha512_224Compute(const void *data, size_t length, uint8_t *digest)
77 {
78 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
79  Sha512_224Context *context;
80 #else
81  Sha512_224Context context[1];
82 #endif
83 
84  //Check parameters
85  if(data == NULL && length != 0)
87 
88  if(digest == NULL)
90 
91 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
92  //Allocate a memory buffer to hold the SHA-512/224 context
93  context = cryptoAllocMem(sizeof(Sha512_224Context));
94  //Failed to allocate memory?
95  if(context == NULL)
96  return ERROR_OUT_OF_MEMORY;
97 #endif
98 
99  //Initialize the SHA-512/224 context
100  sha512_224Init(context);
101  //Digest the message
102  sha512_224Update(context, data, length);
103  //Finalize the SHA-512/224 message digest
104  sha512_224Final(context, digest);
105 
106 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
107  //Free previously allocated memory
108  cryptoFreeMem(context);
109 #endif
110 
111  //Successful processing
112  return NO_ERROR;
113 }
114 
115 
116 /**
117  * @brief Initialize SHA-512/224 message digest context
118  * @param[in] context Pointer to the SHA-512/224 context to initialize
119  **/
120 
121 __weak_func void sha512_224Init(Sha512_224Context *context)
122 {
123  //Set initial hash value
124  context->h[0] = 0x8C3D37C819544DA2;
125  context->h[1] = 0x73E1996689DCD4D6;
126  context->h[2] = 0x1DFAB7AE32FF9C82;
127  context->h[3] = 0x679DD514582F9FCF;
128  context->h[4] = 0x0F6D2B697BD44DA8;
129  context->h[5] = 0x77E36F7304C48942;
130  context->h[6] = 0x3F9D85A86A1D36C8;
131  context->h[7] = 0x1112E6AD91D692A1;
132 
133  //Number of bytes in the buffer
134  context->size = 0;
135  //Total length of the message
136  context->totalSize = 0;
137 }
138 
139 
140 /**
141  * @brief Update the SHA-512/224 context with a portion of the message being hashed
142  * @param[in] context Pointer to the SHA-512/224 context
143  * @param[in] data Pointer to the buffer being hashed
144  * @param[in] length Length of the buffer
145  **/
146 
147 __weak_func void sha512_224Update(Sha512_224Context *context, const void *data, size_t length)
148 {
149  //The function is defined in the exact same manner as SHA-512
150  sha512Update(context, data, length);
151 }
152 
153 
154 /**
155  * @brief Finish the SHA-512/224 message digest
156  * @param[in] context Pointer to the SHA-512/224 context
157  * @param[out] digest Calculated digest (optional parameter)
158  **/
159 
160 __weak_func void sha512_224Final(Sha512_224Context *context, uint8_t *digest)
161 {
162  //The function is defined in the exact same manner as SHA-512
163  sha512Final(context, NULL);
164 
165  //Copy the resulting digest
166  if(digest != NULL)
167  {
168  osMemcpy(digest, context->digest, SHA512_224_DIGEST_SIZE);
169  }
170 }
171 
172 #endif
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 SHA512_224_DIGEST_SIZE
Definition: sha512_224.h:41
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:993
const HashAlgo sha512_224HashAlgo
Definition: sha512_224.c:50
size_t size
Definition: sha512.h:73
__weak_func void sha512_224Update(Sha512_224Context *context, const void *data, size_t length)
Update the SHA-512/224 context with a portion of the message being hashed.
Definition: sha512_224.c:147
__weak_func void sha512_224Final(Sha512_224Context *context, uint8_t *digest)
Finish the SHA-512/224 message digest.
Definition: sha512_224.c:160
@ 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.
__weak_func error_t sha512_224Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-512/224.
Definition: sha512_224.c:76
uint8_t length
Definition: tcp.h:368
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:995
SHA-512/224 (Secure Hash Algorithm)
const uint8_t SHA512_224_OID[9]
Definition: sha512_224.c:47
uint64_t h[8]
Definition: sha512.h:65
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 sha512_224Init(Sha512_224Context *context)
Initialize SHA-512/224 message digest context.
Definition: sha512_224.c:121
#define cryptoAllocMem(size)
Definition: crypto.h:786
#define SHA512_224_MIN_PAD_SIZE
Definition: sha512_224.h:43
Common interface for hash algorithms.
Definition: crypto.h:1046
uint64_t totalSize
Definition: sha512.h:74
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:988
@ NO_ERROR
Success.
Definition: error.h:44
#define SHA512_224_BLOCK_SIZE
Definition: sha512_224.h:39
void sha512Update(Sha512Context *context, const void *data, size_t length)
Update the SHA-512 context with a portion of the message being hashed.