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