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