shake256_512.c
Go to the documentation of this file.
1 /**
2  * @file shake256_512.c
3  * @brief SHAKE256/512 hash function
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.6
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "hash/shake256_512.h"
37 
38 //Check crypto library configuration
39 #if (SHAKE256_512_SUPPORT == ENABLED)
40 
41 //Common interface for hash algorithms
43 {
44  "SHAKE256/512",
46  sizeof(SHAKE256_OID),
47  sizeof(Shake256_512Context),
51  TRUE,
56  NULL
57 };
58 
59 
60 /**
61  * @brief Digest a message using SHAKE256/512
62  * @param[in] data Pointer to the message being hashed
63  * @param[in] length Length of the message
64  * @param[out] digest Pointer to the calculated digest
65  * @return Error code
66  **/
67 
68 __weak_func error_t shake256_512Compute(const void *data, size_t length, uint8_t *digest)
69 {
70 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
71  Shake256_512Context *context;
72 #else
73  Shake256_512Context context[1];
74 #endif
75 
76  //Check parameters
77  if(data == NULL && length != 0)
79 
80  if(digest == NULL)
82 
83 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
84  //Allocate a memory buffer to hold the SHAKE256/512 context
85  context = cryptoAllocMem(sizeof(Shake256_512Context));
86  //Failed to allocate memory?
87  if(context == NULL)
88  return ERROR_OUT_OF_MEMORY;
89 #endif
90 
91  //Initialize the SHAKE256/512 context
92  shake256_512Init(context);
93  //Digest the message
94  shake256_512Update(context, data, length);
95  //Finalize the SHAKE256/512 message digest
96  shake256_512Final(context, digest);
97 
98 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
99  //Free previously allocated memory
100  cryptoFreeMem(context);
101 #endif
102 
103  //Successful processing
104  return NO_ERROR;
105 }
106 
107 
108 /**
109  * @brief Initialize SHAKE256/512 message digest context
110  * @param[in] context Pointer to the SHAKE256/512 context to initialize
111  **/
112 
113 __weak_func void shake256_512Init(Shake256_512Context *context)
114 {
115  //Initialize the SHAKE256 context
116  shakeInit(context, 256);
117 }
118 
119 
120 /**
121  * @brief Update the SHAKE256/512 context with a portion of the message being hashed
122  * @param[in] context Pointer to the SHAKE256/512 context
123  * @param[in] data Pointer to the buffer being hashed
124  * @param[in] length Length of the buffer
125  **/
126 
127 __weak_func void shake256_512Update(Shake256_512Context *context, const void *data, size_t length)
128 {
129  //Absorb the input data
130  shakeAbsorb(context, data, length);
131 }
132 
133 
134 /**
135  * @brief Finish the SHAKE256/512 message digest
136  * @param[in] context Pointer to the SHAKE256/512 context
137  * @param[out] digest Calculated digest
138  **/
139 
140 __weak_func void shake256_512Final(Shake256_512Context *context, uint8_t *digest)
141 {
142  //Finish absorbing phase
143  shakeFinal(context);
144  //Extract data from the squeezing phase
145  shakeSqueeze(context, digest, SHAKE256_512_DIGEST_SIZE);
146 }
147 
148 #endif
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1174
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define SHAKE256_512_BLOCK_SIZE
Definition: shake256_512.h:39
#define SHAKE256_512_DIGEST_SIZE
Definition: shake256_512.h:41
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1175
__weak_func void shake256_512Final(Shake256_512Context *context, uint8_t *digest)
Finish the SHAKE256/512 message digest.
Definition: shake256_512.c:140
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define SHAKE256_512_MIN_PAD_SIZE
Definition: shake256_512.h:43
error_t
Error codes.
Definition: error.h:43
error_t shakeInit(ShakeContext *context, uint_t strength)
Initialize SHAKE context.
Definition: shake.c:187
const uint8_t SHAKE256_OID[9]
Definition: shake.c:50
__weak_func void shake256_512Init(Shake256_512Context *context)
Initialize SHAKE256/512 message digest context.
Definition: shake256_512.c:113
void shakeAbsorb(ShakeContext *context, const void *input, size_t length)
Absorb data.
Definition: shake.c:241
General definitions for cryptographic algorithms.
void shakeSqueeze(ShakeContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: shake.c:267
__weak_func error_t shake256_512Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHAKE256/512.
Definition: shake256_512.c:68
SHAKE algorithm context.
Definition: shake.h:53
uint8_t length
Definition: tcp.h:375
const HashAlgo shake256_512HashAlgo
Definition: shake256_512.c:42
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1176
#define cryptoFreeMem(p)
Definition: crypto.h:966
#define cryptoAllocMem(size)
Definition: crypto.h:961
void shakeFinal(ShakeContext *context)
Finish absorbing phase.
Definition: shake.c:253
Common interface for hash algorithms.
Definition: crypto.h:1243
SHAKE256/512 hash function.
__weak_func void shake256_512Update(Shake256_512Context *context, const void *data, size_t length)
Update the SHAKE256/512 context with a portion of the message being hashed.
Definition: shake256_512.c:127
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1171
@ NO_ERROR
Success.
Definition: error.h:44