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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @section Description
26  *
27  * SHA-512/224 is a secure hash algorithm for computing a condensed representation
28  * of an electronic message. Refer to FIPS 180-4 for more details
29  *
30  * @author Oryx Embedded SARL (www.oryx-embedded.com)
31  * @version 1.9.0
32  **/
33 
34 //Switch to the appropriate trace level
35 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
36 
37 //Dependencies
38 #include "core/crypto.h"
39 #include "hash/sha512_224.h"
40 
41 //Check crypto library configuration
42 #if (SHA512_224_SUPPORT == ENABLED)
43 
44 //SHA-512/224 object identifier (2.16.840.1.101.3.4.2.5)
45 const uint8_t sha512_224Oid[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05};
46 
47 //Common interface for hash algorithms
49 {
50  "SHA-512/224",
52  sizeof(sha512_224Oid),
53  sizeof(Sha512_224Context),
60 };
61 
62 
63 /**
64  * @brief Digest a message using SHA-512/224
65  * @param[in] data Pointer to the message being hashed
66  * @param[in] length Length of the message
67  * @param[out] digest Pointer to the calculated digest
68  * @return Error code
69  **/
70 
71 error_t sha512_224Compute(const void *data, size_t length, uint8_t *digest)
72 {
73  //Allocate a memory buffer to hold the SHA-512/224 context
75  //Failed to allocate memory?
76  if(context == NULL)
77  return ERROR_OUT_OF_MEMORY;
78 
79  //Initialize the SHA-512/224 context
80  sha512_224Init(context);
81  //Digest the message
82  sha512_224Update(context, data, length);
83  //Finalize the SHA-512/224 message digest
84  sha512_224Final(context, digest);
85 
86  //Free previously allocated memory
87  cryptoFreeMem(context);
88  //Successful processing
89  return NO_ERROR;
90 }
91 
92 
93 /**
94  * @brief Initialize SHA-512/224 message digest context
95  * @param[in] context Pointer to the SHA-512/224 context to initialize
96  **/
97 
99 {
100  //Set initial hash value
101  context->h[0] = 0x8C3D37C819544DA2;
102  context->h[1] = 0x73E1996689DCD4D6;
103  context->h[2] = 0x1DFAB7AE32FF9C82;
104  context->h[3] = 0x679DD514582F9FCF;
105  context->h[4] = 0x0F6D2B697BD44DA8;
106  context->h[5] = 0x77E36F7304C48942;
107  context->h[6] = 0x3F9D85A86A1D36C8;
108  context->h[7] = 0x1112E6AD91D692A1;
109 
110  //Number of bytes in the buffer
111  context->size = 0;
112  //Total length of the message
113  context->totalSize = 0;
114 }
115 
116 
117 /**
118  * @brief Update the SHA-512/224 context with a portion of the message being hashed
119  * @param[in] context Pointer to the SHA-512/224 context
120  * @param[in] data Pointer to the buffer being hashed
121  * @param[in] length Length of the buffer
122  **/
123 
124 void sha512_224Update(Sha512_224Context *context, const void *data, size_t length)
125 {
126  //The function is defined in the exact same manner as SHA-512
127  sha512Update(context, data, length);
128 }
129 
130 
131 /**
132  * @brief Finish the SHA-512/224 message digest
133  * @param[in] context Pointer to the SHA-512/224 context
134  * @param[out] digest Calculated digest (optional parameter)
135  **/
136 
137 void sha512_224Final(Sha512_224Context *context, uint8_t *digest)
138 {
139  //The function is defined in the exact same manner as SHA-512
140  sha512Final(context, NULL);
141 
142  //Copy the resulting digest
143  if(digest != NULL)
144  cryptoMemcpy(digest, context->digest, SHA512_224_DIGEST_SIZE);
145 }
146 
147 #endif
void sha512_224Init(Sha512_224Context *context)
Initialize SHA-512/224 message digest context.
Definition: sha512_224.c:98
#define cryptoMemcpy(dest, src, length)
Definition: crypto.h:590
#define cryptoFreeMem(p)
Definition: crypto.h:578
SHA-512 algorithm context.
Definition: sha512.h:54
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1020
#define SHA512_224_DIGEST_SIZE
Definition: sha512_224.h:39
#define cryptoAllocMem(size)
Definition: crypto.h:573
#define SHA512_224_BLOCK_SIZE
Definition: sha512_224.h:37
General definitions for cryptographic algorithms.
void sha512Final(Sha512Context *context, uint8_t *digest)
Finish the SHA-512 message digest.
Definition: sha512.c:213
size_t size
Definition: sha512.h:66
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1021
uint64_t h[8]
Definition: sha512.h:58
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:174
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1022
Success.
Definition: error.h:42
error_t
Error codes.
Definition: error.h:40
SHA-512/224 (Secure Hash Algorithm)
error_t sha512_224Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-512/224.
Definition: sha512_224.c:71
uint8_t data[]
Definition: dtls_misc.h:167
const HashAlgo sha512_224HashAlgo
Definition: sha512_224.c:48
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:124
void sha512_224Final(Sha512_224Context *context, uint8_t *digest)
Finish the SHA-512/224 message digest.
Definition: sha512_224.c:137
uint8_t digest[64]
Definition: sha512.h:59
Common interface for hash algorithms.
Definition: crypto.h:1054
uint8_t length
Definition: dtls_misc.h:140
uint64_t totalSize
Definition: sha512.h:67
const uint8_t sha512_224Oid[9]
Definition: sha512_224.c:45
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1023