tuple_hash_xof.c
Go to the documentation of this file.
1 /**
2  * @file tuple_hash_xof.c
3  * @brief TupleHashXOF (TupleHash with arbitrary-length output)
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
24  * @version 2.6.6
25  **/
26 
27 //Switch to the appropriate trace level
28 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
29 
30 //Dependencies
31 #include "core/crypto.h"
32 #include "xof/tuple_hash_xof.h"
33 
34 //Check crypto library configuration
35 #if (TUPLE_HASH_XOF_SUPPORT == ENABLED)
36 
37 
38 /**
39  * @brief Digest a message using TupleHashXOF
40  * @param[in] strength Number of bits of security (128 for TupleHashXOF128 and
41  * 256 for TupleHashXOF256)
42  * @param[in] key Pointer to the secret key (K)
43  * @param[in] keyLen Length of the secret key
44  * @param[in] inputFrags Tuple of input strings (X)
45  * @param[in] inputNumFrags Number of input strings in the tuple (n)
46  * @param[in] custom Customization string (S)
47  * @param[in] customLen Length of the customization string
48  * @param[out] output Pointer to the output data
49  * @param[in] outputLen Expected length of the output data (L)
50  * @return Error code
51  **/
52 
53 error_t tupleHashXofCompute(uint_t strength, const DataFrag *inputFrags,
54  uint_t inputNumFrags, const char_t *custom, size_t customLen,
55  uint8_t *output, size_t outputLen)
56 {
57  error_t error;
58  uint_t i;
59 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
60  TupleHashXofContext *context;
61 #else
62  TupleHashXofContext context[1];
63 #endif
64 
65  //Check parameters
66  if(inputFrags == NULL && inputNumFrags != 0)
68 
69  if(output == NULL && outputLen != 0)
71 
72 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
73  //Allocate a memory buffer to hold the TupleHashXOF context
74  context = cryptoAllocMem(sizeof(TupleHashXofContext));
75  //Failed to allocate memory?
76  if(context == NULL)
77  return ERROR_OUT_OF_MEMORY;
78 #endif
79 
80  //Initialize the TupleHashXOF context
81  error = tupleHashXofInit(context, strength, custom, customLen);
82 
83  //Check status code
84  if(!error)
85  {
86  //Absorb input data
87  for(i = 0; i < inputNumFrags; i++)
88  {
89  tupleHashXofAbsorb(context, inputFrags[i].buffer,
90  inputFrags[i].length);
91  }
92 
93  //Finish absorbing phase
94  tupleHashXofFinal(context);
95  //Extract data from the squeezing phase
96  tupleHashXofSqueeze(context, output, outputLen);
97  }
98 
99 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
100  //Free previously allocated memory
101  cryptoFreeMem(context);
102 #endif
103 
104  //Return status code
105  return error;
106 }
107 
108 
109 /**
110  * @brief Initialize TupleHashXOF context
111  * @param[in] context Pointer to the TupleHashXOF context to initialize
112  * @param[in] strength Number of bits of security (128 for TupleHashXOF128 and
113  * 256 for TupleHashXOF256)
114  * @param[in] custom Customization string (S)
115  * @param[in] customLen Length of the customization string
116  * @return Error code
117  **/
118 
120  const char_t *custom, size_t customLen)
121 {
122  error_t error;
123 
124  //Make sure the TupleHashXOF context is valid
125  if(context == NULL)
127 
128  //Initialize cSHAKE context
129  return cshakeInit(&context->cshakeContext, strength, "TupleHash", 9, custom,
130  customLen);
131 }
132 
133 
134 /**
135  * @brief Absorb data
136  * @param[in] context Pointer to the TupleHashXOF context
137  * @param[in] input Pointer to the input string
138  * @param[in] length Length of the string
139  **/
140 
141 void tupleHashXofAbsorb(TupleHashXofContext *context, const void *input,
142  size_t length)
143 {
144  size_t n;
145  uint8_t buffer[sizeof(size_t) + 1];
146 
147  //Absorb the string representation of the input data
148  cshakeLeftEncode(length * 8, buffer, &n);
149  cshakeAbsorb(&context->cshakeContext, buffer, n);
150  cshakeAbsorb(&context->cshakeContext, input, length);
151 }
152 
153 
154 /**
155  * @brief Finish absorbing phase
156  * @param[in] context Pointer to the TupleHashXOF context
157  **/
158 
160 {
161  uint8_t buffer[2];
162 
163  //When used as a XOF, TupleHash is computed by setting the encoded output
164  //length to 0
165  buffer[0] = 0;
166  buffer[1] = 1;
167  cshakeAbsorb(&context->cshakeContext, buffer, 2);
168 
169  //Finish absorbing phase
170  cshakeFinal(&context->cshakeContext);
171 }
172 
173 
174 /**
175  * @brief Extract data from the squeezing phase
176  * @param[in] context Pointer to the TupleHashXOF context
177  * @param[out] output Output string
178  * @param[in] length Desired output length, in bytes
179  **/
180 
181 void tupleHashXofSqueeze(TupleHashXofContext *context, uint8_t *output,
182  size_t length)
183 {
184  //Extract data from the squeezing phase
185  cshakeSqueeze(&context->cshakeContext, output, length);
186 }
187 
188 #endif
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
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
TupleHashXOF algorithm context.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t tupleHashXofInit(TupleHashXofContext *context, uint_t strength, const char_t *custom, size_t customLen)
Initialize TupleHashXOF context.
error_t
Error codes.
Definition: error.h:43
void cshakeFinal(CshakeContext *context)
Finish absorbing phase.
Definition: cshake.c:220
Data fragment descriptor.
Definition: crypto.h:1164
General definitions for cryptographic algorithms.
void tupleHashXofSqueeze(TupleHashXofContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
void tupleHashXofAbsorb(TupleHashXofContext *context, const void *input, size_t length)
Absorb data.
uint8_t length
Definition: tcp.h:375
void cshakeAbsorb(CshakeContext *context, const void *input, size_t length)
Absorb data.
Definition: cshake.c:208
CshakeContext cshakeContext
void cshakeLeftEncode(size_t value, uint8_t *buffer, size_t *length)
Encode integer as byte string.
Definition: cshake.c:262
TupleHashXOF (TupleHash with arbitrary-length output)
char char_t
Definition: compiler_port.h:55
uint8_t n
#define cryptoFreeMem(p)
Definition: crypto.h:966
error_t tupleHashXofCompute(uint_t strength, const DataFrag *inputFrags, uint_t inputNumFrags, const char_t *custom, size_t customLen, uint8_t *output, size_t outputLen)
Digest a message using TupleHashXOF.
#define cryptoAllocMem(size)
Definition: crypto.h:961
unsigned int uint_t
Definition: compiler_port.h:57
void tupleHashXofFinal(TupleHashXofContext *context)
Finish absorbing phase.
void cshakeSqueeze(CshakeContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: cshake.c:248