tuple_hash.c
Go to the documentation of this file.
1 /**
2  * @file tuple_hash.c
3  * @brief TupleHash 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  * @section Description
28  *
29  * TupleHash is a SHA-3-derived hash function with variable-length output that
30  * is designed to simply hash a tuple of input strings
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 2.6.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/tuple_hash.h"
42 
43 //Check crypto library configuration
44 #if (TUPLE_HASH_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Digest a message using TupleHash
49  * @param[in] strength Number of bits of security (128 for TupleHash128 and
50  * 256 for TupleHash256)
51  * @param[in] dataFrags Tuple of input strings (X)
52  * @param[in] dataNumFrags Number of input strings in the tuple (n)
53  * @param[in] custom Customization string (S)
54  * @param[in] customLen Length of the customization string
55  * @param[out] digest Calculated digest
56  * @param[in] digestLen Expected length of the digest (L)
57  * @return Error code
58  **/
59 
60 error_t tupleHashCompute(uint_t strength, const DataFrag *dataFrags,
61  uint_t dataNumFrags, const char_t *custom, size_t customLen,
62  uint8_t *digest, size_t digestLen)
63 {
64  error_t error;
65  uint_t i;
66 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
67  TupleHashContext *context;
68 #else
69  TupleHashContext context[1];
70 #endif
71 
72  //Check parameters
73  if(dataFrags == NULL && dataNumFrags != 0)
75 
76  if(digest == NULL && digestLen != 0)
78 
79 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
80  //Allocate a memory buffer to hold the TupleHash context
81  context = cryptoAllocMem(sizeof(TupleHashContext));
82  //Failed to allocate memory?
83  if(context == NULL)
84  return ERROR_OUT_OF_MEMORY;
85 #endif
86 
87  //Initialize the TupleHash context
88  error = tupleHashInit(context, strength, custom, customLen);
89 
90  //Check status code
91  if(!error)
92  {
93  //Digest the message
94  for(i = 0; i < dataNumFrags; i++)
95  {
96  tupleHashUpdate(context, dataFrags[i].buffer, dataFrags[i].length);
97  }
98 
99  //Finalize the TupleHash computation
100  tupleHashFinal(context, digest, digestLen);
101  }
102 
103 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
104  //Free previously allocated memory
105  cryptoFreeMem(context);
106 #endif
107 
108  //Return status code
109  return error;
110 }
111 
112 
113 /**
114  * @brief Initialize TupleHash message digest context
115  * @param[in] context Pointer to the TupleHash context to initialize
116  * @param[in] strength Number of bits of security (128 for TupleHash128 and
117  * 256 for TupleHash256)
118  * @param[in] custom Customization string (S)
119  * @param[in] customLen Length of the customization string
120  * @return Error code
121  **/
122 
124  const char_t *custom, size_t customLen)
125 {
126  //Make sure the TupleHash context is valid
127  if(context == NULL)
129 
130  //Initialize cSHAKE context
131  return cshakeInit(&context->cshakeContext, strength, "TupleHash", 9, custom,
132  customLen);
133 }
134 
135 
136 /**
137  * @brief Update the TupleHash context with a portion of the message being hashed
138  * @param[in] context Pointer to the TupleHash context
139  * @param[in] data Pointer to the input string
140  * @param[in] length Length of the string
141  **/
142 
143 void tupleHashUpdate(TupleHashContext *context, const void *data, size_t length)
144 {
145  size_t n;
146  uint8_t buffer[sizeof(size_t) + 1];
147 
148  //Absorb the string representation of the input data
149  cshakeLeftEncode(length * 8, buffer, &n);
150  cshakeAbsorb(&context->cshakeContext, buffer, n);
151  cshakeAbsorb(&context->cshakeContext, data, length);
152 }
153 
154 
155 /**
156  * @brief Finish the TupleHash message digest
157  * @param[in] context Pointer to the TupleHash context
158  * @param[out] digest Calculated digest
159  * @param[in] length Expected length of the digest (L)
160  **/
161 
162 void tupleHashFinal(TupleHashContext *context, uint8_t *digest, size_t length)
163 {
164  size_t n;
165  uint8_t buffer[sizeof(size_t) + 1];
166 
167  //Absorb the string representation of L
168  cshakeRightEncode(length * 8, buffer, &n);
169  cshakeAbsorb(&context->cshakeContext, buffer, n);
170 
171  //Finish absorbing phase
172  cshakeFinal(&context->cshakeContext);
173  //Extract data from the squeezing phase
174  cshakeSqueeze(&context->cshakeContext, digest, length);
175 }
176 
177 #endif
TupleHash hash function.
void tupleHashFinal(TupleHashContext *context, uint8_t *digest, size_t length)
Finish the TupleHash message digest.
Definition: tuple_hash.c:162
error_t cshakeInit(CshakeContext *context, uint_t strength, const char_t *name, size_t nameLen, const char_t *custom, size_t customLen)
Initialize cSHAKE context.
Definition: cshake.c:124
CshakeContext cshakeContext
Definition: tuple_hash.h:55
uint8_t data[]
Definition: ethernet.h:224
void tupleHashUpdate(TupleHashContext *context, const void *data, size_t length)
Update the TupleHash context with a portion of the message being hashed.
Definition: tuple_hash.c:143
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
error_t tupleHashCompute(uint_t strength, const DataFrag *dataFrags, uint_t dataNumFrags, const char_t *custom, size_t customLen, uint8_t *digest, size_t digestLen)
Digest a message using TupleHash.
Definition: tuple_hash.c:60
error_t tupleHashInit(TupleHashContext *context, uint_t strength, const char_t *custom, size_t customLen)
Initialize TupleHash message digest context.
Definition: tuple_hash.c:123
void cshakeFinal(CshakeContext *context)
Finish absorbing phase.
Definition: cshake.c:220
Data fragment descriptor.
Definition: crypto.h:1164
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:375
void cshakeAbsorb(CshakeContext *context, const void *input, size_t length)
Absorb data.
Definition: cshake.c:208
void cshakeLeftEncode(size_t value, uint8_t *buffer, size_t *length)
Encode integer as byte string.
Definition: cshake.c:262
char char_t
Definition: compiler_port.h:55
uint8_t n
TupleHash algorithm context.
Definition: tuple_hash.h:54
#define cryptoFreeMem(p)
Definition: crypto.h:966
#define cryptoAllocMem(size)
Definition: crypto.h:961
void cshakeRightEncode(size_t value, uint8_t *buffer, size_t *length)
Encode integer as byte string.
Definition: cshake.c:298
unsigned int uint_t
Definition: compiler_port.h:57
void cshakeSqueeze(CshakeContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: cshake.c:248