ascon_xof128.c
Go to the documentation of this file.
1 /**
2  * @file ascon_xof128.c
3  * @brief Ascon-XOF128 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-XOF128 is an eXtendable Output Functions (XOF), where the output size
30  * of the hash of the message can be selected by the user, and the supported
31  * security strength is 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_xof128.h"
43 
44 //Check crypto library configuration
45 #if (ASCON_XOF128_SUPPORT == ENABLED)
46 
47 //Ascon-XOF128 object identifier (0.0)
48 const uint8_t ASCON_XOF128_OID[1] = {0x00};
49 
50 //Common interface for XOF algorithms
52 {
53  "Ascon-XOF128",
55  sizeof(ASCON_XOF128_OID),
56  sizeof(AsconXof128Context),
58  (XofAlgoComputeEx) NULL,
60  (XofAlgoInitEx) NULL,
64 };
65 
66 
67 /**
68  * @brief Digest a message using Ascon-XOF128
69  * @param[in] input Pointer to the input data
70  * @param[in] inputLen Length of the input data
71  * @param[out] output Pointer to the output data
72  * @param[in] outputLen Expected length of the output data
73  * @return Error code
74  **/
75 
76 error_t asconXof128Compute(const void *input, size_t inputLen, uint8_t *output,
77  size_t outputLen)
78 {
79 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
80  AsconXof128Context *context;
81 #else
82  AsconXof128Context context[1];
83 #endif
84 
85  //Check parameters
86  if(input == NULL && inputLen != 0)
88 
89  if(output == NULL && outputLen != 0)
91 
92 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
93  //Allocate a memory buffer to hold the Ascon-XOF128 context
94  context = cryptoAllocMem(sizeof(AsconXof128Context));
95  //Failed to allocate memory?
96  if(context == NULL)
97  return ERROR_OUT_OF_MEMORY;
98 #endif
99 
100  //Initialize the Ascon-XOF128 context
101  asconXof128Init(context);
102  //Absorb input data
103  asconXof128Absorb(context, input, inputLen);
104  //Finish absorbing phase
105  asconXof128Final(context);
106  //Extract data from the squeezing phase
107  asconXof128Squeeze(context, output, outputLen);
108 
109 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
110  //Free previously allocated memory
111  cryptoFreeMem(context);
112 #endif
113 
114  //Successful operation
115  return NO_ERROR;
116 }
117 
118 
119 /**
120  * @brief Initialize Ascon-XOF128 context
121  * @param[in] context Pointer to the Ascon-XOF128 context to initialize
122  * @return Error code
123  **/
124 
126 {
127  //Make sure the Ascon-XOF128 context is valid
128  if(context == NULL)
130 
131  //The 320-bit internal state of Ascon-XOF128 is initialized with the
132  //concatenation of the 64-bit IV and 256 zeroes
133  context->state.x[0] = 0x00CC0003;
134  context->state.x[1] = 0x00000800;
135  context->state.x[2] = 0;
136  context->state.x[3] = 0;
137  context->state.x[4] = 0;
138  context->state.x[5] = 0;
139  context->state.x[6] = 0;
140  context->state.x[7] = 0;
141  context->state.x[8] = 0;
142  context->state.x[9] = 0;
143 
144  //Apply Ascon-p[12] permutation
145  asconP(&context->state, 12);
146 
147  //Number of bytes in the buffer
148  context->length = 0;
149 
150  //Successful operation
151  return NO_ERROR;
152 }
153 
154 
155 /**
156  * @brief Absorb data
157  * @param[in] context Pointer to the Ascon-XOF128 context
158  * @param[in] input Pointer to the buffer being hashed
159  * @param[in] length Length of the buffer
160  **/
161 
162 void asconXof128Absorb(AsconXof128Context *context, const void *input,
163  size_t length)
164 {
165  size_t n;
166 
167  //Process the incoming data
168  while(length > 0)
169  {
170  //The buffer can hold at most 8 bytes
171  n = MIN(length, 8 - context->length);
172 
173  //Copy the data to the buffer
174  osMemcpy(context->buffer + context->length, input, n);
175  //Adjust the length of the buffer
176  context->length += n;
177 
178  //Advance the data pointer
179  input = (uint8_t *) input + n;
180  //Remaining bytes to process
181  length -= n;
182 
183  //The message is partitioned into 64-bit blocks
184  if(context->length == 8)
185  {
186  //Each message block Mi is XORed with the state
187  context->state.x[0] ^= LOAD32LE(context->buffer);
188  context->state.x[1] ^= LOAD32LE(context->buffer + 4);
189 
190  //For all message blocks except the final block Mn,the XOR operation
191  //is immediately followed by applying Ascon-p[12] to the state
192  asconP(&context->state, 12);
193 
194  //The input buffer is empty
195  context->length = 0;
196  }
197  }
198 }
199 
200 
201 /**
202  * @brief Finish absorbing phase
203  * @param[in] context Pointer to the Ascon-XOF128 context
204  **/
205 
207 {
208  size_t i;
209 
210  //Get the length of the partial block Mn~
211  i = context->length;
212 
213  //Appends a one followed by one or more zeroes to data
214  context->buffer[i++] = 0x01;
215 
216  //Partial block Mn~ is padded to a full block Mn
217  while(i < 8)
218  {
219  context->buffer[i++] = 0;
220  }
221 
222  //The final block Mn is XORed with the state
223  context->state.x[0] ^= LOAD32LE(context->buffer);
224  context->state.x[1] ^= LOAD32LE(context->buffer + 4);
225 
226  //The squeezing phase begins with an application of Ascon-p[12] to the state
227  asconP(&context->state, 12);
228 
229  //The value of S[0:63] is then taken as hash block H0
230  STORE32LE(context->state.x[0], context->buffer);
231  STORE32LE(context->state.x[1], context->buffer + 4);
232 
233  //Number of bytes available in the output buffer
234  context->length = 8;
235 }
236 
237 
238 /**
239  * @brief Extract data from the squeezing phase
240  * @param[in] context Pointer to the Ascon-XOF128 context
241  * @param[out] output Output string
242  * @param[in] length Desired output length, in bytes
243  **/
244 
245 void asconXof128Squeeze(AsconXof128Context *context, uint8_t *output,
246  size_t length)
247 {
248  size_t n;
249 
250  //An arbitrary number of output bits can be squeezed out of the state
251  while(length > 0)
252  {
253  //Check whether more data is required
254  if(context->length == 0)
255  {
256  //Apply Ascon-p[12] permutation
257  asconP(&context->state, 12);
258 
259  //The value of S[0:63] is then taken as hash block H0
260  STORE32LE(context->state.x[0], context->buffer);
261  STORE32LE(context->state.x[1], context->buffer + 4);
262 
263  //Number of bytes available in the output buffer
264  context->length = 8;
265  }
266 
267  //Compute the number of bytes to process at a time
268  n = MIN(length, context->length);
269 
270  //Copy the output string
271  if(output != NULL)
272  {
273  osMemcpy(output, context->buffer + 8 - context->length, n);
274  }
275 
276  //Number of bytes available in the output buffer
277  context->length -= n;
278 
279  //Advance the data pointer
280  output = (uint8_t *) output + n;
281  //Number of bytes that remains to be written
282  length -= n;
283  }
284 }
285 
286 #endif
const XofAlgo asconXof128XofAlgo
Definition: ascon_xof128.c:51
Ascon-XOF128 algorithm context.
Definition: ascon_xof128.h:52
void asconXof128Absorb(AsconXof128Context *context, const void *input, size_t length)
Absorb data.
Definition: ascon_xof128.c:162
void(* XofAlgoAbsorb)(void *context, const void *input, size_t length)
Definition: crypto.h:1191
error_t(* XofAlgoInit)(void *context)
Definition: crypto.h:1186
const uint8_t ASCON_XOF128_OID[1]
Definition: ascon_xof128.c:48
#define STORE32LE(a, p)
Definition: cpu_endian.h:279
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
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
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:375
void asconXof128Squeeze(AsconXof128Context *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: ascon_xof128.c:245
#define MIN(a, b)
Definition: os_port.h:63
error_t(* XofAlgoCompute)(const void *input, size_t inputLen, uint8_t *output, size_t outputLen)
Definition: crypto.h:1180
uint8_t buffer[8]
Definition: ascon_xof128.h:54
Common interface for XOF algorithms.
Definition: crypto.h:1265
uint8_t n
void asconXof128Final(AsconXof128Context *context)
Finish absorbing phase.
Definition: ascon_xof128.c:206
AsconState state
Definition: ascon_xof128.h:53
#define cryptoFreeMem(p)
Definition: crypto.h:966
error_t asconXof128Init(AsconXof128Context *context)
Initialize Ascon-XOF128 context.
Definition: ascon_xof128.c:125
#define cryptoAllocMem(size)
Definition: crypto.h:961
Ascon-XOF128 extendable-output function.
void(* XofAlgoSqueeze)(void *context, uint8_t *output, size_t length)
Definition: crypto.h:1193
#define LOAD32LE(p)
Definition: cpu_endian.h:203
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
error_t asconXof128Compute(const void *input, size_t inputLen, uint8_t *output, size_t outputLen)
Digest a message using Ascon-XOF128.
Definition: ascon_xof128.c:76