23 #ifndef LW_INTERNAL_ASCON_H
24 #define LW_INTERNAL_ASCON_H
26 #include "internal-util.h"
43 #if !defined(__AVR__) && !defined(LW_UTIL_CPU_IS_64BIT)
44 #define ASCON_SLICED 1
46 #define ASCON_SLICED 0
52 #define ASCON128_IV 0x80400c0600000000ULL
57 #define ASCON128a_IV 0x80800c0800000000ULL
62 #define ASCON80PQ_IV 0xa0400c06U
116 #define ascon_bit_permute_step(_y, mask, shift) \
119 uint32_t t = ((y >> (shift)) ^ y) & (mask); \
120 (_y) = (y ^ t) ^ (t << (shift)); \
131 #define ascon_separate(x) \
133 ascon_bit_permute_step((x), 0x22222222, 1); \
134 ascon_bit_permute_step((x), 0x0c0c0c0c, 2); \
135 ascon_bit_permute_step((x), 0x00f000f0, 4); \
136 ascon_bit_permute_step((x), 0x0000ff00, 8); \
138 #define ascon_combine(x) \
140 ascon_bit_permute_step((x), 0x0000aaaa, 15); \
141 ascon_bit_permute_step((x), 0x0000cccc, 14); \
142 ascon_bit_permute_step((x), 0x0000f0f0, 12); \
143 ascon_bit_permute_step((x), 0x0000ff00, 8); \
156 #define ascon_set_sliced(state, data, offset) \
158 ascon_state_t *s = (state); \
159 uint32_t high = be_load_word32((data)); \
160 uint32_t low = be_load_word32((data) + 4); \
161 ascon_separate(high); \
162 ascon_separate(low); \
163 s->W[(offset) * 2] = (high << 16) | (low & 0x0000FFFFU); \
164 s->W[(offset) * 2 + 1] = (high & 0xFFFF0000U) | (low >> 16); \
175 #define ascon_absorb_sliced(state, data, offset) \
177 ascon_state_t *s = (state); \
178 uint32_t high = be_load_word32((data)); \
179 uint32_t low = be_load_word32((data) + 4); \
180 ascon_separate(high); \
181 ascon_separate(low); \
182 s->W[(offset) * 2] ^= (high << 16) | (low & 0x0000FFFFU); \
183 s->W[(offset) * 2 + 1] ^= (high & 0xFFFF0000U) | (low >> 16); \
196 #define ascon_absorb32_low_sliced(state, data, offset) \
198 ascon_state_t *s = (state); \
199 uint32_t low = be_load_word32((data)); \
200 ascon_separate(low); \
201 s->W[(offset) * 2] ^= (low & 0x0000FFFFU); \
202 s->W[(offset) * 2 + 1] ^= (low >> 16); \
215 #define ascon_absorb32_high_sliced(state, data, offset) \
217 ascon_state_t *s = (state); \
218 uint32_t high = be_load_word32((data)); \
219 ascon_separate(high); \
220 s->W[(offset) * 2] ^= (high << 16); \
221 s->W[(offset) * 2 + 1] ^= (high & 0xFFFF0000U); \
232 #define ascon_squeeze_sliced(state, data, offset) \
234 ascon_state_t *s = (state); \
235 uint32_t high, low; \
236 high = (s->W[(offset) * 2] >> 16) | \
237 (s->W[(offset) * 2 + 1] & 0xFFFF0000U); \
238 low = (s->W[(offset) * 2] & 0x0000FFFFU) | \
239 (s->W[(offset) * 2 + 1] << 16); \
240 ascon_combine(high); \
241 ascon_combine(low); \
242 be_store_word32((data), high); \
243 be_store_word32((data) + 4, low); \
255 #define ascon_encrypt_sliced(state, c, m, offset) \
257 ascon_state_t *s = (state); \
258 uint32_t high = be_load_word32((m)); \
259 uint32_t low = be_load_word32((m) + 4); \
260 ascon_separate(high); \
261 ascon_separate(low); \
262 s->W[(offset) * 2] ^= (high << 16) | (low & 0x0000FFFFU); \
263 s->W[(offset) * 2 + 1] ^= (high & 0xFFFF0000U) | (low >> 16); \
264 high = (s->W[(offset) * 2] >> 16) | \
265 (s->W[(offset) * 2 + 1] & 0xFFFF0000U); \
266 low = (s->W[(offset) * 2] & 0x0000FFFFU) | \
267 (s->W[(offset) * 2 + 1] << 16); \
268 ascon_combine(high); \
269 ascon_combine(low); \
270 be_store_word32((c), high); \
271 be_store_word32((c) + 4, low); \
283 #define ascon_decrypt_sliced(state, m, c, offset) \
285 ascon_state_t *s = (state); \
286 uint32_t high, low, high2, low2; \
287 high = be_load_word32((c)); \
288 low = be_load_word32((c) + 4); \
289 ascon_separate(high); \
290 ascon_separate(low); \
291 high2 = high ^ ((s->W[(offset) * 2] >> 16) | \
292 (s->W[(offset) * 2 + 1] & 0xFFFF0000U)); \
293 low2 = low ^ ((s->W[(offset) * 2] & 0x0000FFFFU) | \
294 (s->W[(offset) * 2 + 1] << 16)); \
295 s->W[(offset) * 2] = (high << 16) | (low & 0x0000FFFFU); \
296 s->W[(offset) * 2 + 1] = (high & 0xFFFF0000U) | (low >> 16); \
297 ascon_combine(high2); \
298 ascon_combine(low2); \
299 be_store_word32((m), high2); \
300 be_store_word32((m) + 4, low2); \
311 #define ascon_separator() (state.W[8] ^= 0x01)
313 #define ascon_separator() (state.B[39] ^= 0x01)
void ascon_permute_sliced(ascon_state_t *state, uint8_t first_round)
Permutes the ASCON state in sliced form.
Definition: internal-ascon.c:124
Structure of the internal state of the ASCON permutation.
Definition: internal-ascon.h:67
void ascon_permute(ascon_state_t *state, uint8_t first_round)
Permutes the ASCON state.
Definition: internal-ascon.c:39
void ascon_to_sliced(ascon_state_t *state)
Converts an ASCON state from byte form into sliced form.
Definition: internal-ascon.c:96
void ascon_from_sliced(ascon_state_t *state)
Converts an ASCON state from sliced form into byte form.
Definition: internal-ascon.c:110