ascon_cxof128.c
Go to the documentation of this file.
1 /**
2  * @file ascon_cxof128.c
3  * @brief Ascon-CXOF128 customizable extendable-output 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  * Ascon-CXOF128 is a customized eXtendable Output Functions (XOF) that allows
30  * users to specify a customization string and choose the output size of the
31  * message hash. It supports a security strength of up to 128 bits
32  *
33  * @author Oryx Embedded SARL (www.oryx-embedded.com)
34  * @version 2.6.6
35  **/
36 
37 //Switch to the appropriate trace level
38 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
39 
40 //Dependencies
41 #include "core/crypto.h"
42 #include "lwc/ascon_cxof128.h"
43 
44 //Check crypto library configuration
45 #if (ASCON_CXOF128_SUPPORT == ENABLED)
46 
47 //Ascon-CXOF128 object identifier (0.0)
48 const uint8_t ASCON_CXOF128_OID[1] = {0x00};
49 
50 //Common interface for XOF algorithms
52 {
53  "Ascon-CXOF128",
55  sizeof(ASCON_CXOF128_OID),
56  sizeof(AsconCxof128Context),
57  (XofAlgoCompute) NULL,
59  (XofAlgoInit) NULL,
64 };
65 
66 
67 /**
68  * @brief Digest a message using Ascon-CXOF128
69  * @param[in] input Pointer to the input data
70  * @param[in] inputLen Length of the input data
71  * @param[in] custom Customization string (Z)
72  * @param[in] customLen Length of the customization string
73  * @param[out] output Pointer to the output data
74  * @param[in] outputLen Expected length of the output data
75  * @return Error code
76  **/
77 
78 error_t asconCxof128Compute(const void *input, size_t inputLen,
79  const char_t *custom, size_t customLen, uint8_t *output, size_t outputLen)
80 {
81  error_t error;
82 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
83  AsconCxof128Context *context;
84 #else
85  AsconCxof128Context context[1];
86 #endif
87 
88  //Check parameters
89  if(input == NULL && inputLen != 0)
91 
92  if(output == NULL && outputLen != 0)
94 
95 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
96  //Allocate a memory buffer to hold the Ascon-CXOF128 context
97  context = cryptoAllocMem(sizeof(AsconCxof128Context));
98  //Failed to allocate memory?
99  if(context == NULL)
100  return ERROR_OUT_OF_MEMORY;
101 #endif
102 
103  //Initialize the Ascon-CXOF128 context
104  error = asconCxof128Init(context, custom, customLen);
105 
106  //Check status code
107  if(!error)
108  {
109  //Absorb input data
110  asconCxof128Absorb(context, input, inputLen);
111  //Finish absorbing phase
112  asconCxof128Final(context);
113  //Extract data from the squeezing phase
114  asconCxof128Squeeze(context, output, outputLen);
115  }
116 
117 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
118  //Free previously allocated memory
119  cryptoFreeMem(context);
120 #endif
121 
122  //Return status code
123  return error;
124 }
125 
126 
127 /**
128  * @brief Initialize Ascon-CXOF128 context
129  * @param[in] context Pointer to the Ascon-CXOF128 context to initialize
130  * @param[in] custom Customization string (Z)
131  * @param[in] customLen Length of the customization string, in bytes
132  * @return Error code
133  **/
134 
136  size_t customLen)
137 {
138  //Make sure the Ascon-CXOF128 context is valid
139  if(context == NULL)
141 
142  //The length of the customization string shall be at most 2048 bits
143  if(customLen > 256)
144  return ERROR_INVALID_LENGTH;
145 
146  //For domain separation, Ascon-CXOF128 uses a different IV than Ascon-XOF128
147  context->state.x[0] = 0x00CC0004;
148  context->state.x[1] = 0x00000800;
149  context->state.x[2] = 0;
150  context->state.x[3] = 0;
151  context->state.x[4] = 0;
152  context->state.x[5] = 0;
153  context->state.x[6] = 0;
154  context->state.x[7] = 0;
155  context->state.x[8] = 0;
156  context->state.x[9] = 0;
157 
158  //Apply Ascon-p[12] permutation
159  asconP(&context->state, 12);
160 
161  //Z0 is a 64-bit integer that represents the bit-length of the customization
162  //string
163  STORE64LE(customLen * 8, context->buffer);
164 
165  //Update the state with Z0
166  context->state.x[0] ^= LOAD32LE(context->buffer);
167  context->state.x[1] ^= LOAD32LE(context->buffer + 4);
168 
169  //Apply Ascon-p[12] permutation
170  asconP(&context->state, 12);
171 
172  //The customization string Z is parsed into blocks
173  while(customLen >= 8)
174  {
175  //Update the state with Zi
176  context->state.x[0] ^= LOAD32LE(custom);
177  context->state.x[1] ^= LOAD32LE(custom + 4);
178 
179  //Apply Ascon-p[12] permutation
180  asconP(&context->state, 12);
181 
182  //Next block
183  custom += 8;
184  customLen -= 8;
185  }
186 
187  //Partial block Zm~ is padded to a full block Zm
188  osMemset(context->buffer, 0, 8);
189  osMemcpy(context->buffer, custom, customLen);
190  context->buffer[customLen] = 0x01;
191 
192  //Update the state with Zm
193  context->state.x[0] ^= LOAD32LE(context->buffer);
194  context->state.x[1] ^= LOAD32LE(context->buffer + 4);
195 
196  //Apply Ascon-p[12] permutation
197  asconP(&context->state, 12);
198 
199  //Number of bytes in the buffer
200  context->length = 0;
201 
202  //Successful initialization
203  return NO_ERROR;
204 }
205 
206 
207 /**
208  * @brief Absorb data
209  * @param[in] context Pointer to the Ascon-CXOF128 context
210  * @param[in] input Pointer to the buffer being hashed
211  * @param[in] length Length of the buffer
212  **/
213 
214 void asconCxof128Absorb(AsconCxof128Context *context, const void *input,
215  size_t length)
216 {
217  size_t n;
218 
219  //Process the incoming data
220  while(length > 0)
221  {
222  //The buffer can hold at most 8 bytes
223  n = MIN(length, 8 - context->length);
224 
225  //Copy the data to the buffer
226  osMemcpy(context->buffer + context->length, input, n);
227  //Adjust the length of the buffer
228  context->length += n;
229 
230  //Advance the data pointer
231  input = (uint8_t *) input + n;
232  //Remaining bytes to process
233  length -= n;
234 
235  //The message is partitioned into 64-bit blocks
236  if(context->length == 8)
237  {
238  //Each message block Mi is XORed with the state
239  context->state.x[0] ^= LOAD32LE(context->buffer);
240  context->state.x[1] ^= LOAD32LE(context->buffer + 4);
241 
242  //For all message blocks except the final block Mn,the XOR operation
243  //is immediately followed by applying Ascon-p[12] to the state
244  asconP(&context->state, 12);
245 
246  //The input buffer is empty
247  context->length = 0;
248  }
249  }
250 }
251 
252 
253 /**
254  * @brief Finish absorbing phase
255  * @param[in] context Pointer to the Ascon-CXOF128 context
256  **/
257 
259 {
260  size_t i;
261 
262  //Get the length of the partial block Mn~
263  i = context->length;
264 
265  //Appends a one followed by one or more zeroes to data
266  context->buffer[i++] = 0x01;
267 
268  //Partial block Mn~ is padded to a full block Mn
269  while(i < 8)
270  {
271  context->buffer[i++] = 0;
272  }
273 
274  //The final block Mn is XORed with the state
275  context->state.x[0] ^= LOAD32LE(context->buffer);
276  context->state.x[1] ^= LOAD32LE(context->buffer + 4);
277 
278  //The squeezing phase begins with an application of Ascon-p[12] to the state
279  asconP(&context->state, 12);
280 
281  //The value of S[0:63] is then taken as hash block H0
282  STORE32LE(context->state.x[0], context->buffer);
283  STORE32LE(context->state.x[1], context->buffer + 4);
284 
285  //Number of bytes available in the output buffer
286  context->length = 8;
287 }
288 
289 
290 /**
291  * @brief Extract data from the squeezing phase
292  * @param[in] context Pointer to the Ascon-CXOF128 context
293  * @param[out] output Output string
294  * @param[in] length Desired output length, in bytes
295  **/
296 
297 void asconCxof128Squeeze(AsconCxof128Context *context, uint8_t *output,
298  size_t length)
299 {
300  size_t n;
301 
302  //An arbitrary number of output bits can be squeezed out of the state
303  while(length > 0)
304  {
305  //Check whether more data is required
306  if(context->length == 0)
307  {
308  //Apply Ascon-p[12] permutation
309  asconP(&context->state, 12);
310 
311  //The value of S[0:63] is then taken as hash block H0
312  STORE32LE(context->state.x[0], context->buffer);
313  STORE32LE(context->state.x[1], context->buffer + 4);
314 
315  //Number of bytes available in the output buffer
316  context->length = 8;
317  }
318 
319  //Compute the number of bytes to process at a time
320  n = MIN(length, context->length);
321 
322  //Copy the output string
323  if(output != NULL)
324  {
325  osMemcpy(output, context->buffer + 8 - context->length, n);
326  }
327 
328  //Number of bytes available in the output buffer
329  context->length -= n;
330 
331  //Advance the data pointer
332  output = (uint8_t *) output + n;
333  //Number of bytes that remains to be written
334  length -= n;
335  }
336 }
337 
338 #endif
void(* XofAlgoAbsorb)(void *context, const void *input, size_t length)
Definition: crypto.h:1191
error_t(* XofAlgoInit)(void *context)
Definition: crypto.h:1186
#define STORE32LE(a, p)
Definition: cpu_endian.h:279
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
Ascon-CXOF128 customizable extendable-output function.
error_t(* XofAlgoInitEx)(void *context, const char_t *custom, size_t customLen)
Definition: crypto.h:1188
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
error_t
Error codes.
Definition: error.h:43
uint32_t x[10]
Definition: ascon.h:49
error_t asconCxof128Init(AsconCxof128Context *context, const char_t *custom, size_t customLen)
Initialize Ascon-CXOF128 context.
Ascon-CXOF128 algorithm context.
Definition: ascon_cxof128.h:52
@ ERROR_INVALID_LENGTH
Definition: error.h:111
General definitions for cryptographic algorithms.
void asconCxof128Final(AsconCxof128Context *context)
Finish absorbing phase.
uint8_t length
Definition: tcp.h:375
#define MIN(a, b)
Definition: os_port.h:63
const uint8_t ASCON_CXOF128_OID[1]
Definition: ascon_cxof128.c:48
error_t(* XofAlgoCompute)(const void *input, size_t inputLen, uint8_t *output, size_t outputLen)
Definition: crypto.h:1180
Common interface for XOF algorithms.
Definition: crypto.h:1265
char char_t
Definition: compiler_port.h:55
uint8_t n
#define STORE64LE(a, p)
Definition: cpu_endian.h:311
error_t asconCxof128Compute(const void *input, size_t inputLen, const char_t *custom, size_t customLen, uint8_t *output, size_t outputLen)
Digest a message using Ascon-CXOF128.
Definition: ascon_cxof128.c:78
#define cryptoFreeMem(p)
Definition: crypto.h:966
#define cryptoAllocMem(size)
Definition: crypto.h:961
const XofAlgo asconCxof128XofAlgo
Definition: ascon_cxof128.c:51
void asconCxof128Squeeze(AsconCxof128Context *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
void asconCxof128Absorb(AsconCxof128Context *context, const void *input, size_t length)
Absorb data.
void(* XofAlgoSqueeze)(void *context, uint8_t *output, size_t length)
Definition: crypto.h:1193
#define LOAD32LE(p)
Definition: cpu_endian.h:203
#define osMemset(p, value, length)
Definition: os_port.h:141
error_t(* XofAlgoComputeEx)(const void *input, size_t inputLen, const char_t *custom, size_t customLen, uint8_t *output, size_t outputLen)
Definition: crypto.h:1183
@ NO_ERROR
Success.
Definition: error.h:44
void asconP(AsconState *s, uint_t nr)
Ascon-p[rnd] permutation.
Definition: ascon.c:63
void(* XofAlgoFinal)(void *context)
Definition: crypto.h:1192