sha3_384.c
Go to the documentation of this file.
1 /**
2  * @file sha3_384.c
3  * @brief SHA3-384 hash function (SHA-3 with 384-bit output)
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-3 is a secure hash algorithm for computing a condensed representation
28  * of an electronic message. Refer to FIPS 202 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/sha3_384.h"
40 
41 //Check crypto library configuration
42 #if (SHA3_384_SUPPORT == ENABLED)
43 
44 //SHA3-384 object identifier (2.16.840.1.101.3.4.2.9)
45 const uint8_t sha3_384Oid[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09};
46 
47 //Common interface for hash algorithms
49 {
50  "SHA3-384",
52  sizeof(sha3_384Oid),
53  sizeof(Sha3_384Context),
60 };
61 
62 
63 /**
64  * @brief Digest a message using SHA3-384
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 sha3_384Compute(const void *data, size_t length, uint8_t *digest)
72 {
73  Sha3_384Context *context;
74 
75  //Allocate a memory buffer to hold the SHA3-384 context
76  context = cryptoAllocMem(sizeof(Sha3_384Context));
77  //Failed to allocate memory?
78  if(context == NULL)
79  return ERROR_OUT_OF_MEMORY;
80 
81  //Initialize the SHA3-384 context
82  sha3_384Init(context);
83  //Digest the message
84  sha3_384Update(context, data, length);
85  //Finalize the SHA3-384 message digest
86  sha3_384Final(context, digest);
87 
88  //Free previously allocated memory
89  cryptoFreeMem(context);
90  //Successful processing
91  return NO_ERROR;
92 }
93 
94 
95 /**
96  * @brief Initialize SHA3-384 message digest context
97  * @param[in] context Pointer to the SHA3-384 context to initialize
98  **/
99 
101 {
102  //The capacity of the sponge is twice the digest length
103  keccakInit(context, 2 * 384);
104 }
105 
106 
107 /**
108  * @brief Update the SHA3-384 context with a portion of the message being hashed
109  * @param[in] context Pointer to the SHA3-384 context
110  * @param[in] data Pointer to the buffer being hashed
111  * @param[in] length Length of the buffer
112  **/
113 
114 void sha3_384Update(Sha3_384Context *context, const void *data, size_t length)
115 {
116  //Absorb the input data
117  keccakAbsorb(context, data, length);
118 }
119 
120 
121 /**
122  * @brief Finish the SHA3-384 message digest
123  * @param[in] context Pointer to the SHA3-384 context
124  * @param[out] digest Calculated digest (optional parameter)
125  **/
126 
127 void sha3_384Final(Sha3_384Context *context, uint8_t *digest)
128 {
129  //Finish absorbing phase (padding byte is 0x06 for SHA-3)
130  keccakFinal(context, KECCAK_SHA3_PAD);
131  //Extract data from the squeezing phase
132  keccakSqueeze(context, digest, SHA3_384_DIGEST_SIZE);
133 }
134 
135 #endif
const uint8_t sha3_384Oid[9]
Definition: sha3_384.c:45
#define SHA3_384_DIGEST_SIZE
Definition: sha3_384.h:39
#define KECCAK_SHA3_PAD
Definition: keccak.h:91
#define cryptoFreeMem(p)
Definition: crypto.h:578
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1020
#define cryptoAllocMem(size)
Definition: crypto.h:573
General definitions for cryptographic algorithms.
#define SHA3_384_BLOCK_SIZE
Definition: sha3_384.h:37
const HashAlgo sha3_384HashAlgo
Definition: sha3_384.c:48
void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
Absorb data.
Definition: keccak.c:316
void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: keccak.c:411
Keccak context.
Definition: keccak.h:105
void sha3_384Final(Sha3_384Context *context, uint8_t *digest)
Finish the SHA3-384 message digest.
Definition: sha3_384.c:127
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1021
error_t keccakInit(KeccakContext *context, uint_t capacity)
Initialize Keccak context.
Definition: keccak.c:283
void sha3_384Init(Sha3_384Context *context)
Initialize SHA3-384 message digest context.
Definition: sha3_384.c:100
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1022
void sha3_384Update(Sha3_384Context *context, const void *data, size_t length)
Update the SHA3-384 context with a portion of the message being hashed.
Definition: sha3_384.c:114
Success.
Definition: error.h:42
error_t
Error codes.
Definition: error.h:40
uint8_t data[]
Definition: dtls_misc.h:167
SHA3-384 hash function (SHA-3 with 384-bit output)
error_t sha3_384Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA3-384.
Definition: sha3_384.c:71
Common interface for hash algorithms.
Definition: crypto.h:1054
uint8_t length
Definition: dtls_misc.h:140
void keccakFinal(KeccakContext *context, uint8_t pad)
Finish absorbing phase.
Definition: keccak.c:367
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1023