ASCON Suite
ascon-masked-state.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "ascon-masked-state.h"
24 #include "ascon-masked-backend.h"
25 #include "core/ascon-util-snp.h"
26 #include <ascon/utility.h>
27 #include <string.h>
28 
30 {
31  memset(state, 0, sizeof(ascon_masked_state_t));
32 }
33 
35 {
36  if (state)
38 }
39 
41 {
42  int index;
43  for (index = 0; index < 5; ++index) {
45  (&(state->M[index]), &(state->M[index]), trng);
46  }
47 }
48 
50  (ascon_masked_state_t *dest, const ascon_state_t *src,
51  ascon_trng_state_t *trng)
52 {
53 #if defined(ASCON_BACKEND_DIRECT_XOR)
54  int index;
55  for (index = 0; index < 5; ++index)
56  ascon_masked_word_x2_load(&(dest->M[index]), src->B + index * 8, trng);
57 #else
58  int index;
59  unsigned char word[8];
60  for (index = 0; index < 5; ++index) {
61  ascon_squeeze_8(src, word, index * 8);
62  ascon_masked_word_x2_load(&(dest->M[index]), word, trng);
63  }
64  ascon_clean(word, sizeof(word));
65 #endif
66 }
67 
69 {
70  int index;
71  ascon_init(dest);
72 #if defined(ASCON_BACKEND_DIRECT_XOR)
73  for (index = 0; index < 5; ++index)
74  ascon_masked_word_x2_store(dest->B + index * 8, &(src->M[index]));
75 #else
76  unsigned char word[8];
77  for (index = 0; index < 5; ++index) {
78  ascon_masked_word_x2_store(word, &(src->M[index]));
79  ascon_overwrite_bytes(dest, word, index * 8, 8);
80  }
81  ascon_clean(word, sizeof(word));
82 #endif
83 }
84 
86  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
87  ascon_trng_state_t *trng)
88 {
89  int index;
90  for (index = 0; index < 5; ++index) {
92  (&(dest->M[index]), &(src->M[index]), trng);
93  }
94 }
95 
96 #if ASCON_MASKED_MAX_SHARES >= 3
97 
99  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
100  ascon_trng_state_t *trng)
101 {
102  int index;
103  for (index = 0; index < 5; ++index)
104  ascon_masked_word_x2_from_x3(&(dest->M[index]), &(src->M[index]), trng);
105 }
106 
107 #endif /* ASCON_MASKED_MAX_SHARES >= 3 */
108 
109 #if ASCON_MASKED_MAX_SHARES >= 4
110 
112  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
113  ascon_trng_state_t *trng)
114 {
115  int index;
116  for (index = 0; index < 5; ++index)
117  ascon_masked_word_x2_from_x4(&(dest->M[index]), &(src->M[index]), trng);
118 }
119 
120 #endif /* ASCON_MASKED_MAX_SHARES >= 4 */
121 
122 #if ASCON_MASKED_MAX_SHARES >= 3
123 
125 {
126  int index;
127  for (index = 0; index < 5; ++index) {
129  (&(state->M[index]), &(state->M[index]), trng);
130  }
131 }
132 
134  (ascon_masked_state_t *dest, const ascon_state_t *src,
135  ascon_trng_state_t *trng)
136 {
137 #if defined(ASCON_BACKEND_DIRECT_XOR)
138  int index;
139  for (index = 0; index < 5; ++index)
140  ascon_masked_word_x3_load(&(dest->M[index]), src->B + index * 8, trng);
141 #else
142  int index;
143  unsigned char word[8];
144  for (index = 0; index < 5; ++index) {
145  ascon_squeeze_8(src, word, index * 8);
146  ascon_masked_word_x3_load(&(dest->M[index]), word, trng);
147  }
148  ascon_clean(word, sizeof(word));
149 #endif
150 }
151 
153 {
154  int index;
155  ascon_init(dest);
156 #if defined(ASCON_BACKEND_DIRECT_XOR)
157  for (index = 0; index < 5; ++index)
158  ascon_masked_word_x3_store(dest->B + index * 8, &(src->M[index]));
159 #else
160  unsigned char word[8];
161  for (index = 0; index < 5; ++index) {
162  ascon_masked_word_x3_store(word, &(src->M[index]));
163  ascon_overwrite_bytes(dest, word, index * 8, 8);
164  }
165  ascon_clean(word, sizeof(word));
166 #endif
167 }
168 
170  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
171  ascon_trng_state_t *trng)
172 {
173  int index;
174  for (index = 0; index < 5; ++index)
175  ascon_masked_word_x3_from_x2(&(dest->M[index]), &(src->M[index]), trng);
176 }
177 
179  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
180  ascon_trng_state_t *trng)
181 {
182  int index;
183  for (index = 0; index < 5; ++index) {
185  (&(dest->M[index]), &(src->M[index]), trng);
186  }
187 }
188 
189 #if ASCON_MASKED_MAX_SHARES >= 4
190 
192  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
193  ascon_trng_state_t *trng)
194 {
195  int index;
196  for (index = 0; index < 5; ++index)
197  ascon_masked_word_x3_from_x4(&(dest->M[index]), &(src->M[index]), trng);
198 }
199 
200 #endif /* ASCON_MASKED_MAX_SHARES >= 4 */
201 
202 #endif /* ASCON_MASKED_MAX_SHARES >= 3 */
203 
204 #if ASCON_MASKED_MAX_SHARES >= 4
205 
207 {
208  int index;
209  for (index = 0; index < 5; ++index) {
211  (&(state->M[index]), &(state->M[index]), trng);
212  }
213 }
214 
216  (ascon_masked_state_t *dest, const ascon_state_t *src,
217  ascon_trng_state_t *trng)
218 {
219 #if defined(ASCON_BACKEND_DIRECT_XOR)
220  int index;
221  for (index = 0; index < 5; ++index)
222  ascon_masked_word_x4_load(&(dest->M[index]), src->B + index * 8, trng);
223 #else
224  int index;
225  unsigned char word[8];
226  for (index = 0; index < 5; ++index) {
227  ascon_squeeze_8(src, word, index * 8);
228  ascon_masked_word_x4_load(&(dest->M[index]), word, trng);
229  }
230  ascon_clean(word, sizeof(word));
231 #endif
232 }
233 
235 {
236  int index;
237  ascon_init(dest);
238 #if defined(ASCON_BACKEND_DIRECT_XOR)
239  for (index = 0; index < 5; ++index)
240  ascon_masked_word_x4_store(dest->B + index * 8, &(src->M[index]));
241 #else
242  unsigned char word[8];
243  for (index = 0; index < 5; ++index) {
244  ascon_masked_word_x4_store(word, &(src->M[index]));
245  ascon_overwrite_bytes(dest, word, index * 8, 8);
246  }
247  ascon_clean(word, sizeof(word));
248 #endif
249 }
250 
252  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
253  ascon_trng_state_t *trng)
254 {
255  int index;
256  for (index = 0; index < 5; ++index)
257  ascon_masked_word_x4_from_x2(&(dest->M[index]), &(src->M[index]), trng);
258 }
259 
261  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
262  ascon_trng_state_t *trng)
263 {
264  int index;
265  for (index = 0; index < 5; ++index)
266  ascon_masked_word_x4_from_x3(&(dest->M[index]), &(src->M[index]), trng);
267 }
268 
270  (ascon_masked_state_t *dest, const ascon_masked_state_t *src,
271  ascon_trng_state_t *trng)
272 {
273  int index;
274  for (index = 0; index < 5; ++index) {
276  (&(dest->M[index]), &(src->M[index]), trng);
277  }
278 }
279 
280 #endif /* ASCON_MASKED_MAX_SHARES >= 4 */
void ascon_x4_copy_to_x1(ascon_state_t *dest, const ascon_masked_state_t *src)
Copies the entire ASCON-x4 permutation state to a regular ASCON-x1 permutation state.
void ascon_x2_copy_from_x3(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x3 permutation state to an ASCON-x2 permutation state.
void ascon_x2_copy_from_x2(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x2 permutation state from a source to a destination.
void ascon_x4_randomize(ascon_masked_state_t *state, ascon_trng_state_t *trng)
Randomizes an ASCON-x4 permutation state.
void ascon_x2_copy_from_x4(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x4 permutation state to an ASCON-x2 permutation state.
void ascon_x4_copy_from_x4(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x4 permutation state from a source to a destination.
void ascon_x3_copy_to_x1(ascon_state_t *dest, const ascon_masked_state_t *src)
Copies the entire ASCON-x3 permutation state to a regular ASCON-x1 permutation state.
void ascon_x3_copy_from_x3(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x3 permutation state from a source to a destination.
void ascon_x4_copy_from_x3(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x4 permutation state from an ASCON-x3 permutation state.
void ascon_x4_copy_from_x2(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x4 permutation state from an ASCON-x2 permutation state.
void ascon_x3_copy_from_x2(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x3 permutation state from an ASCON-x2 permutation state.
void ascon_x3_copy_from_x1(ascon_masked_state_t *dest, const ascon_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x3 permutation state from a regular ASCON-x1 permutation state.
void ascon_masked_state_free(ascon_masked_state_t *state)
Frees an ASCON-x2 permutation state and attempts to destroy any sensitive material.
void ascon_masked_state_init(ascon_masked_state_t *state)
Initializes the words of a masked ASCON permutation state.
void ascon_x2_randomize(ascon_masked_state_t *state, ascon_trng_state_t *trng)
Randomizes an ASCON-x2 permutation state.
void ascon_x3_randomize(ascon_masked_state_t *state, ascon_trng_state_t *trng)
Randomizes an ASCON-x3 permutation state.
void ascon_x3_copy_from_x4(ascon_masked_state_t *dest, const ascon_masked_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x3 permutation state from an ASCON-x4 permutation state.
void ascon_x4_copy_from_x1(ascon_masked_state_t *dest, const ascon_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x4 permutation state from a regular ASCON-x1 permutation state.
void ascon_x2_copy_to_x1(ascon_state_t *dest, const ascon_masked_state_t *src)
Copies the entire ASCON-x2 permutation state to a regular ASCON-x1 permutation state.
void ascon_x2_copy_from_x1(ascon_masked_state_t *dest, const ascon_state_t *src, ascon_trng_state_t *trng)
Copies the entire ASCON-x2 permutation state from a regular ASCON-x1 permutation state.
Utility functions for operating on masked ASCON states with between 2 and 4 shares.
void ascon_masked_word_x2_load(ascon_masked_word_t *word, const uint8_t *data, ascon_trng_state_t *trng)
Loads a 64-bit big endian value from buffer, masks it, and writes it to a x2 masked word structure.
void ascon_masked_word_x2_store(uint8_t *data, const ascon_masked_word_t *word)
Unmasks and stores the contents of a x2 masked word structure.
void ascon_masked_word_x4_from_x2(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Converts a x2 masked word into a x4 masked word.
void ascon_masked_word_x3_from_x2(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Converts a x2 masked word into a x3 masked word.
void ascon_masked_word_x2_from_x4(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Converts a x4 masked word into a x2 masked word.
void ascon_masked_word_x4_load(ascon_masked_word_t *word, const uint8_t *data, ascon_trng_state_t *trng)
Loads a 64-bit big endian value from buffer, masks it, and writes it to a x4 masked word structure.
void ascon_masked_word_x3_load(ascon_masked_word_t *word, const uint8_t *data, ascon_trng_state_t *trng)
Loads a 64-bit big endian value from buffer, masks it, and writes it to a x3 masked word structure.
void ascon_masked_word_x2_from_x3(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Converts a x3 masked word into a x2 masked word.
void ascon_masked_word_x3_from_x4(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Converts a x4 masked word into a x3 masked word.
void ascon_masked_word_x3_randomize(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Randomizes a x3 masked word by incorporating fresh randomness.
void ascon_masked_word_x4_from_x3(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Converts a x3 masked word into a x4 masked word.
void ascon_masked_word_x4_randomize(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Randomizes a x4 masked word by incorporating fresh randomness.
void ascon_masked_word_x4_store(uint8_t *data, const ascon_masked_word_t *word)
Unmasks and stores the contents of a x4 masked word structure.
void ascon_masked_word_x3_store(uint8_t *data, const ascon_masked_word_t *word)
Unmasks and stores the contents of a x3 masked word structure.
void ascon_masked_word_x2_randomize(ascon_masked_word_t *dest, const ascon_masked_word_t *src, ascon_trng_state_t *trng)
Randomizes a x2 masked word by incorporating fresh randomness.
#define ascon_squeeze_8(state, data, offset)
void ascon_overwrite_bytes(ascon_state_t *state, const uint8_t *data, unsigned offset, unsigned size)
Overwrites existing bytes in the ASCON state.
void ascon_init(ascon_state_t *state)
Initializes the words of the ASCON permutation state to zero.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
State of the ASCON permutation which has been masked with up to 4 shares.
ascon_masked_word_t M[5]
State of the random number source.
Definition: ascon-trng.h:64
Structure of the internal state of the ASCON permutation.
Definition: permutation.h:63
uint8_t B[40]
Definition: permutation.h:66
System utilities of use to applications that use ASCON.
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
Definition: ascon-clean.c:38