sha224.c
Go to the documentation of this file.
1 /**
2  * @file sha224.c
3  * @brief SHA-224 (Secure Hash Algorithm 224)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 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-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 1.9.6
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/sha224.h"
42 
43 //Check crypto library configuration
44 #if (SHA224_SUPPORT == ENABLED)
45 
46 //SHA-224 object identifier (2.16.840.1.101.3.4.2.4)
47 const uint8_t sha224Oid[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04};
48 
49 //Common interface for hash algorithms
51 {
52  "SHA-224",
53  sha224Oid,
54  sizeof(sha224Oid),
55  sizeof(Sha224Context),
59  TRUE,
64  NULL
65 };
66 
67 
68 /**
69  * @brief Digest a message using SHA-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 error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
77 {
78  //Allocate a memory buffer to hold the SHA-224 context
79  Sha224Context *context = cryptoAllocMem(sizeof(Sha224Context));
80  //Failed to allocate memory?
81  if(context == NULL)
82  return ERROR_OUT_OF_MEMORY;
83 
84  //Initialize the SHA-224 context
85  sha224Init(context);
86  //Digest the message
87  sha224Update(context, data, length);
88  //Finalize the SHA-224 message digest
89  sha224Final(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-224 message digest context
100  * @param[in] context Pointer to the SHA-224 context to initialize
101  **/
102 
103 void sha224Init(Sha224Context *context)
104 {
105  //Set initial hash value
106  context->h[0] = 0xC1059ED8;
107  context->h[1] = 0x367CD507;
108  context->h[2] = 0x3070DD17;
109  context->h[3] = 0xF70E5939;
110  context->h[4] = 0xFFC00B31;
111  context->h[5] = 0x68581511;
112  context->h[6] = 0x64F98FA7;
113  context->h[7] = 0xBEFA4FA4;
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-224 context with a portion of the message being hashed
124  * @param[in] context Pointer to the SHA-224 context
125  * @param[in] data Pointer to the buffer being hashed
126  * @param[in] length Length of the buffer
127  **/
128 
129 void sha224Update(Sha224Context *context, const void *data, size_t length)
130 {
131  //The function is defined in the exact same manner as SHA-256
132  sha256Update(context, data, length);
133 }
134 
135 
136 /**
137  * @brief Finish the SHA-224 message digest
138  * @param[in] context Pointer to the SHA-224 context
139  * @param[out] digest Calculated digest (optional parameter)
140  **/
141 
142 void sha224Final(Sha224Context *context, uint8_t *digest)
143 {
144  //The function is defined in the exact same manner as SHA-256
145  sha256Final(context, NULL);
146 
147  //Copy the resulting digest
148  if(digest != NULL)
149  cryptoMemcpy(digest, context->digest, SHA224_DIGEST_SIZE);
150 }
151 
152 #endif
uint8_t length
Definition: dtls_misc.h:149
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1094
SHA-256 algorithm context.
Definition: sha256.h:58
void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
Definition: sha256.c:201
#define TRUE
Definition: os_port.h:50
void sha224Update(Sha224Context *context, const void *data, size_t length)
Update the SHA-224 context with a portion of the message being hashed.
Definition: sha224.c:129
size_t size
Definition: sha256.h:70
#define SHA224_DIGEST_SIZE
Definition: sha224.h:41
uint32_t h[8]
Definition: sha256.h:62
void sha224Final(Sha224Context *context, uint8_t *digest)
Finish the SHA-224 message digest.
Definition: sha224.c:142
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1095
#define SHA224_MIN_PAD_SIZE
Definition: sha224.h:43
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
Definition: sha256.c:162
error_t
Error codes.
Definition: error.h:42
void sha224Init(Sha224Context *context)
Initialize SHA-224 message digest context.
Definition: sha224.c:103
General definitions for cryptographic algorithms.
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1096
error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-224.
Definition: sha224.c:76
uint8_t digest[32]
Definition: sha256.h:63
const HashAlgo sha224HashAlgo
Definition: sha224.c:50
#define cryptoMemcpy(dest, src, length)
Definition: crypto.h:642
#define cryptoFreeMem(p)
Definition: crypto.h:630
#define SHA224_BLOCK_SIZE
Definition: sha224.h:39
#define cryptoAllocMem(size)
Definition: crypto.h:625
Common interface for hash algorithms.
Definition: crypto.h:1128
uint64_t totalSize
Definition: sha256.h:71
const uint8_t sha224Oid[9]
Definition: sha224.c:47
uint8_t data[]
Definition: dtls_misc.h:176
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1093
@ NO_ERROR
Success.
Definition: error.h:44
SHA-224 (Secure Hash Algorithm 224)