sha3_224.c
Go to the documentation of this file.
1 /**
2  * @file sha3_224.c
3  * @brief SHA3-224 hash function (SHA-3 with 224-bit output)
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-3 is a secure hash algorithm for computing a condensed representation
30  * of an electronic message. Refer to FIPS 202 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/sha3_224.h"
42 
43 //Check crypto library configuration
44 #if (SHA3_224_SUPPORT == ENABLED)
45 
46 //SHA3-224 object identifier (2.16.840.1.101.3.4.2.7)
47 const uint8_t sha3_224Oid[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x07};
48 
49 //Common interface for hash algorithms
51 {
52  "SHA3-224",
54  sizeof(sha3_224Oid),
55  sizeof(Sha3_224Context),
59  FALSE,
64  NULL
65 };
66 
67 
68 /**
69  * @brief Digest a message using SHA3-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 sha3_224Compute(const void *data, size_t length, uint8_t *digest)
77 {
78  Sha3_224Context *context;
79 
80  //Allocate a memory buffer to hold the SHA3-224 context
81  context = cryptoAllocMem(sizeof(Sha3_224Context));
82  //Failed to allocate memory?
83  if(context == NULL)
84  return ERROR_OUT_OF_MEMORY;
85 
86  //Initialize the SHA3-224 context
87  sha3_224Init(context);
88  //Digest the message
89  sha3_224Update(context, data, length);
90  //Finalize the SHA3-224 message digest
91  sha3_224Final(context, digest);
92 
93  //Free previously allocated memory
94  cryptoFreeMem(context);
95  //Successful processing
96  return NO_ERROR;
97 }
98 
99 
100 /**
101  * @brief Initialize SHA3-224 message digest context
102  * @param[in] context Pointer to the SHA3-224 context to initialize
103  **/
104 
106 {
107  //The capacity of the sponge is twice the digest length
108  keccakInit(context, 2 * 224);
109 }
110 
111 
112 /**
113  * @brief Update the SHA3-224 context with a portion of the message being hashed
114  * @param[in] context Pointer to the SHA3-224 context
115  * @param[in] data Pointer to the buffer being hashed
116  * @param[in] length Length of the buffer
117  **/
118 
119 void sha3_224Update(Sha3_224Context *context, const void *data, size_t length)
120 {
121  //Absorb the input data
122  keccakAbsorb(context, data, length);
123 }
124 
125 
126 /**
127  * @brief Finish the SHA3-224 message digest
128  * @param[in] context Pointer to the SHA3-224 context
129  * @param[out] digest Calculated digest (optional parameter)
130  **/
131 
132 void sha3_224Final(Sha3_224Context *context, uint8_t *digest)
133 {
134  //Finish absorbing phase (padding byte is 0x06 for SHA-3)
135  keccakFinal(context, KECCAK_SHA3_PAD);
136  //Extract data from the squeezing phase
137  keccakSqueeze(context, digest, SHA3_224_DIGEST_SIZE);
138 }
139 
140 #endif
uint8_t length
Definition: coap_common.h:190
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1028
uint8_t data[]
Definition: ethernet.h:209
SHA3-224 hash function (SHA-3 with 224-bit output)
Keccak context.
Definition: keccak.h:107
const uint8_t sha3_224Oid[9]
Definition: sha3_224.c:47
void keccakFinal(KeccakContext *context, uint8_t pad)
Finish absorbing phase.
Definition: keccak.c:369
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1029
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
#define SHA3_224_DIGEST_SIZE
Definition: sha3_224.h:41
General definitions for cryptographic algorithms.
void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
Absorb data.
Definition: keccak.c:318
#define KECCAK_SHA3_PAD
Definition: keccak.h:93
error_t sha3_224Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA3-224.
Definition: sha3_224.c:76
#define SHA3_224_MIN_PAD_SIZE
Definition: sha3_224.h:43
void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: keccak.c:413
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1030
void sha3_224Update(Sha3_224Context *context, const void *data, size_t length)
Update the SHA3-224 context with a portion of the message being hashed.
Definition: sha3_224.c:119
#define cryptoFreeMem(p)
Definition: crypto.h:630
void sha3_224Final(Sha3_224Context *context, uint8_t *digest)
Finish the SHA3-224 message digest.
Definition: sha3_224.c:132
#define cryptoAllocMem(size)
Definition: crypto.h:625
error_t keccakInit(KeccakContext *context, uint_t capacity)
Initialize Keccak context.
Definition: keccak.c:285
Common interface for hash algorithms.
Definition: crypto.h:1062
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1027
void sha3_224Init(Sha3_224Context *context)
Initialize SHA3-224 message digest context.
Definition: sha3_224.c:105
const HashAlgo sha3_224HashAlgo
Definition: sha3_224.c:50
Success.
Definition: error.h:44
#define SHA3_224_BLOCK_SIZE
Definition: sha3_224.h:39