ASCON Suite
ascon-aead-masked-common.h
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 #ifndef ASCON_AEAD_MASKED_COMMON_H
24 #define ASCON_AEAD_MASKED_COMMON_H
25 
26 /* Common utilities for supporting the implementation of masked AEAD modes */
27 
28 #include <ascon/aead-masked.h>
31 #include "aead/ascon-aead-common.h"
32 #include "core/ascon-util.h"
33 #include <string.h>
34 
47  (ascon_masked_state_t *state, const unsigned char *data,
48  size_t len, uint8_t first_round, ascon_masked_word_t *word,
49  uint64_t *preserve, ascon_trng_state_t *trng);
50 
63  (ascon_masked_state_t *state, const unsigned char *data,
64  size_t len, uint8_t first_round, ascon_masked_word_t *word,
65  uint64_t *preserve, ascon_trng_state_t *trng);
66 
80  (ascon_masked_state_t *state, unsigned char *dest,
81  const unsigned char *src, size_t len, uint8_t first_round,
82  ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng);
83 
97  (ascon_masked_state_t *state, unsigned char *dest,
98  const unsigned char *src, size_t len, uint8_t first_round,
99  ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng);
100 
114  (ascon_masked_state_t *state, unsigned char *dest,
115  const unsigned char *src, size_t len, uint8_t first_round,
116  ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng);
117 
131  (ascon_masked_state_t *state, unsigned char *dest,
132  const unsigned char *src, size_t len, uint8_t first_round,
133  ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng);
134 
137 #if ASCON_MASKED_KEY_SHARES == 2
138 
139 #define ascon_masked_key_load(word, data, trng) \
140  ascon_masked_word_x2_load((word), (data), (trng))
141 #define ascon_masked_key_store(data, word) \
142  ascon_masked_word_x2_store((data), (word))
143 #define ascon_masked_key_xor(dest, src) \
144  ascon_masked_word_x2_xor((dest), (const ascon_masked_word_t *)(src))
145 #define ascon_masked_key_permute(state, first_round, preserve) \
146  ascon_x2_permute((state), (first_round), (preserve))
147 #define ascon_masked_key_randomize(state, trng) \
148  ascon_x2_randomize((state), (trng))
149 #define ascon_copy_key_to_x1(state_x1, state) \
150  ascon_x2_copy_to_x1((state_x1), (state))
151 #define ascon_copy_key_to_x2(state, trng) do { ; } while (0)
152 #define ascon_copy_key_from_x1(state, state_x1, trng) \
153  ascon_x2_copy_from_x1((state), (state_x1), (trng))
154 #define ascon_copy_key_from_x2(state, trng) \
155  ascon_x2_randomize((state), (trng))
156 
157 #elif ASCON_MASKED_KEY_SHARES == 3
158 
159 #define ascon_masked_key_load(word, data, trng) \
160  ascon_masked_word_x3_load((word), (data), (trng))
161 #define ascon_masked_key_store(data, word) \
162  ascon_masked_word_x3_store((data), (word))
163 #define ascon_masked_key_xor(dest, src) \
164  ascon_masked_word_x3_xor((dest), (const ascon_masked_word_t *)(src))
165 #define ascon_masked_key_permute(state, first_round, preserve) \
166  ascon_x3_permute((state), (first_round), (preserve))
167 #define ascon_masked_key_randomize(state, trng) \
168  ascon_x3_randomize((state), (trng))
169 #define ascon_copy_key_to_x1(state_x1, state) \
170  ascon_x3_copy_to_x1((state_x1), (state))
171 #define ascon_copy_key_to_x2(state, trng) \
172  ascon_x2_copy_from_x3((state), (state), (trng))
173 #define ascon_copy_key_to_x3(state, trng) do { ; } while (0)
174 #define ascon_copy_key_from_x1(state, state_x1, trng) \
175  ascon_x3_copy_from_x1((state), (state_x1), (trng))
176 #define ascon_copy_key_from_x2(state, trng) \
177  ascon_x3_copy_from_x2((state), (state), (trng))
178 #define ascon_copy_key_from_x3(state, trng) \
179  ascon_x3_randomize((state), (trng))
180 
181 #else /* ASCON_MASKED_KEY_SHARES == 4 */
182 
183 #define ascon_masked_key_load(word, data, trng) \
184  ascon_masked_word_x4_load((word), (data), (trng))
185 #define ascon_masked_key_store(data, word) \
186  ascon_masked_word_x4_store((data), (word))
187 #define ascon_masked_key_xor(dest, src) \
188  ascon_masked_word_x4_xor((dest), (const ascon_masked_word_t *)(src))
189 #define ascon_masked_key_permute(state, first_round, preserve) \
190  ascon_x4_permute((state), (first_round), (preserve))
191 #define ascon_masked_key_randomize(state, trng) \
192  ascon_x4_randomize((state), (trng))
193 #define ascon_copy_key_to_x1(state_x1, state) \
194  ascon_x4_copy_to_x1((state_x1), (state))
195 #define ascon_copy_key_to_x2(state, trng) \
196  ascon_x2_copy_from_x4((state), (state), (trng))
197 #define ascon_copy_key_to_x3(state, trng) \
198  ascon_x3_copy_from_x4((state), (state), (trng))
199 #define ascon_copy_key_to_x4(state, trng) do { ; } while (0)
200 #define ascon_copy_key_from_x1(state, state_x1, trng) \
201  ascon_x4_copy_from_x1((state), (state_x1), (trng))
202 #define ascon_copy_key_from_x2(state, trng) \
203  ascon_x4_copy_from_x2((state), (state), (trng))
204 #define ascon_copy_key_from_x3(state, trng) \
205  ascon_x4_copy_from_x3((state), (state), (trng))
206 #define ascon_copy_key_from_x4(state, trng) \
207  ascon_x4_randomize((state), (trng))
208 
209 #endif /* ASCON_MASKED_KEY_SHARES == 4 */
210 
211 #if ASCON_MASKED_DATA_SHARES == 1
212 
213 /* Masking is not needed with 1 data share */
214 
215 #elif ASCON_MASKED_DATA_SHARES == 2
216 
217 #define ascon_masked_data_load(word, data, trng) \
218  ascon_masked_word_x2_load((word), (data), (trng))
219 #define ascon_masked_data_load_partial(word, data, len, trng) \
220  ascon_masked_word_x2_load_partial((word), (data), len, (trng))
221 #define ascon_masked_data_store(data, word) \
222  ascon_masked_word_x2_store((data), (word))
223 #define ascon_masked_data_store_partial(data, len, word) \
224  ascon_masked_word_x2_store_partial((data), (len), (word))
225 #define ascon_masked_data_xor(dest, src) \
226  ascon_masked_word_x2_xor((dest), (src))
227 #define ascon_masked_data_permute(state, first_round, preserve) \
228  ascon_x2_permute((state), (first_round), (preserve))
229 #define ascon_masked_data_replace(dest, src, size) \
230  ascon_masked_word_x2_replace((dest), (src), (size))
231 
232 #elif ASCON_MASKED_DATA_SHARES == 3
233 
234 #define ascon_masked_data_load(word, data, trng) \
235  ascon_masked_word_x3_load((word), (data), (trng))
236 #define ascon_masked_data_load_partial(word, data, len, trng) \
237  ascon_masked_word_x3_load_partial((word), (data), len, (trng))
238 #define ascon_masked_data_store(data, word) \
239  ascon_masked_word_x3_store((data), (word))
240 #define ascon_masked_data_store_partial(data, len, word) \
241  ascon_masked_word_x3_store_partial((data), (len), (word))
242 #define ascon_masked_data_xor(dest, src) \
243  ascon_masked_word_x3_xor((dest), (src))
244 #define ascon_masked_data_permute(state, first_round, preserve) \
245  ascon_x3_permute((state), (first_round), (preserve))
246 #define ascon_masked_data_replace(dest, src, size) \
247  ascon_masked_word_x3_replace((dest), (src), (size))
248 
249 #else /* ASCON_MASKED_DATA_SHARES == 4 */
250 
251 #define ascon_masked_data_load(word, data, trng) \
252  ascon_masked_word_x4_load((word), (data), (trng))
253 #define ascon_masked_data_load_partial(word, data, len, trng) \
254  ascon_masked_word_x4_load_partial((word), (data), len, (trng))
255 #define ascon_masked_data_store(data, word) \
256  ascon_masked_word_x4_store((data), (word))
257 #define ascon_masked_data_store_partial(data, len, word) \
258  ascon_masked_word_x4_store_partial((data), (len), (word))
259 #define ascon_masked_data_xor(dest, src) \
260  ascon_masked_word_x4_xor((dest), (src))
261 #define ascon_masked_data_permute(state, first_round, preserve) \
262  ascon_x4_permute((state), (first_round), (preserve))
263 #define ascon_masked_data_replace(dest, src, size) \
264  ascon_masked_word_x4_replace((dest), (src), (size))
265 
266 #endif /* ASCON_MASKED_DATA_SHARES == 4 */
267 
270 #endif
Masked ASCON-128 encryption algorithm and related family members.
void ascon_masked_aead_decrypt_16(ascon_masked_state_t *state, unsigned char *dest, const unsigned char *src, size_t len, uint8_t first_round, ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
Decrypts a block of data with an ASCON state and a 16-byte rate.
void ascon_masked_aead_absorb_8(ascon_masked_state_t *state, const unsigned char *data, size_t len, uint8_t first_round, ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
Absorbs data into a masked ASCON state with an 8-byte rate.
void ascon_masked_aead_encrypt_16(ascon_masked_state_t *state, unsigned char *dest, const unsigned char *src, size_t len, uint8_t first_round, ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
Encrypts a block of data with a masked ASCON state and a 16-byte rate.
void ascon_masked_aead_absorb_16(ascon_masked_state_t *state, const unsigned char *data, size_t len, uint8_t first_round, ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
Absorbs data into a masked ASCON state with a 16-byte rate.
void ascon_masked_aead_decrypt_8(ascon_masked_state_t *state, unsigned char *dest, const unsigned char *src, size_t len, uint8_t first_round, ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
Decrypts a block of data with a masked ASCON state and an 8-byte rate.
void ascon_masked_aead_encrypt_8(ascon_masked_state_t *state, unsigned char *dest, const unsigned char *src, size_t len, uint8_t first_round, ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
Encrypts a block of data with a masked ASCON state and an 8-byte rate.
Configures the number of shares to use for masked AEAD modes.
Utility functions for operating on masked ASCON states with between 2 and 4 shares.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
unsigned char data[8]
[snippet_key]
Definition: snippets.c:14
State of the ASCON permutation which has been masked with up to 4 shares.
State of the random number source.
Definition: ascon-trng.h:64
Masked 64-bit word with up to ASCON_MASKED_MAX_SHARES shares.