25 #if defined(ASCON_MASKED_WORD_BACKEND_C32)
33 #define ascon_bit_permute_step(_y, mask, shift) \
36 uint32_t t = ((y >> (shift)) ^ y) & (mask); \
37 (_y) = (y ^ t) ^ (t << (shift)); \
48 #define ascon_separate(x) \
50 ascon_bit_permute_step((x), 0x22222222, 1); \
51 ascon_bit_permute_step((x), 0x0c0c0c0c, 2); \
52 ascon_bit_permute_step((x), 0x00f000f0, 4); \
53 ascon_bit_permute_step((x), 0x0000ff00, 8); \
55 #define ascon_combine(x) \
57 ascon_bit_permute_step((x), 0x0000aaaa, 15); \
58 ascon_bit_permute_step((x), 0x0000cccc, 14); \
59 ascon_bit_permute_step((x), 0x0000f0f0, 12); \
60 ascon_bit_permute_step((x), 0x0000ff00, 8); \
70 word->
W[0] = random1a;
71 word->
W[1] = random1b;
74 #if ASCON_MASKED_MAX_SHARES >= 3
78 #if ASCON_MASKED_MAX_SHARES >= 4
92 ascon_separate(random1a);
93 ascon_separate(random1b);
96 word->
W[0] = (high << 16) | (low & 0x0000FFFFU);
97 word->
W[1] = (high & 0xFFFF0000U) | (low >> 16);
98 high = (random1a << 16) | (random1b & 0x0000FFFFU);
99 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
102 #if ASCON_MASKED_MAX_SHARES >= 3
106 #if ASCON_MASKED_MAX_SHARES >= 4
117 uint32_t random1a, random1b;
121 uint64_t masked = random;
141 high = (uint32_t)(masked >> 32);
142 low = (uint32_t)masked;
143 random1a = (uint32_t)(random >> 32);
144 random1b = (uint32_t)random;
145 ascon_separate(random1a);
146 ascon_separate(random1b);
147 ascon_separate(high);
149 word->
W[0] = (high << 16) | (low & 0x0000FFFFU);
150 word->
W[1] = (high & 0xFFFF0000U) | (low >> 16);
151 high = (random1a << 16) | (random1b & 0x0000FFFFU);
152 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
155 #if ASCON_MASKED_MAX_SHARES >= 3
159 #if ASCON_MASKED_MAX_SHARES >= 4
173 ascon_separate(random1a);
174 ascon_separate(random1b);
175 ascon_separate(high);
177 word->
W[0] = (high << 16) | (low & 0x0000FFFFU);
178 word->
W[1] = (high & 0xFFFF0000U) | (low >> 16);
179 high = (random1a << 16) | (random1b & 0x0000FFFFU);
180 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
183 #if ASCON_MASKED_MAX_SHARES >= 3
187 #if ASCON_MASKED_MAX_SHARES >= 4
196 uint32_t high1 = (word->
W[0] >> 16) | (word->
W[1] & 0xFFFF0000U);
197 uint32_t low1 = (word->
W[0] & 0x0000FFFFU) | (word->
W[1] << 16);
200 uint32_t high3 = (high2 >> 16) | (low2 & 0xFFFF0000U);
201 uint32_t low3 = (high2 & 0x0000FFFFU) | (low2 << 16);
202 ascon_combine(high1);
204 ascon_combine(high3);
213 uint64_t masked1, masked2;
214 uint32_t high1, low1;
215 uint32_t high2, low2;
216 uint32_t high3, low3;
219 high1 = (word->
W[0] >> 16) | (word->
W[1] & 0xFFFF0000U);
220 low1 = (word->
W[0] & 0x0000FFFFU) | (word->
W[1] << 16);
223 high3 = (high2 >> 16) | (low2 & 0xFFFF0000U);
224 low3 = (high2 & 0x0000FFFFU) | (low2 << 16);
225 ascon_combine(high1);
227 ascon_combine(high3);
231 masked1 = (((uint64_t)high1) << 32) | low1;
232 masked2 = (((uint64_t)high3) << 32) | low3;
250 data[0] = (uint8_t)(masked1 ^ masked2);
260 dest->
W[0] = src->
W[0] ^ random1a;
261 dest->
W[1] = src->
W[1] ^ random1b;
269 dest->
S[0] ^= src->
S[0];
270 dest->
S[1] ^= src->
S[1];
276 uint32_t mask1 = (~((uint32_t)0)) >> (size * 4U);
277 uint32_t mask2 = ~mask1;
278 dest->
W[0] = (dest->
W[0] & mask1) | (src->
W[0] & mask2);
279 dest->
W[1] = (dest->
W[1] & mask1) | (src->
W[1] & mask2);
282 dest->
W[2] = (dest->
W[2] & mask1) | (src->
W[2] & mask2);
283 dest->
W[3] = (dest->
W[3] & mask1) | (src->
W[3] & mask2);
286 #if ASCON_MASKED_MAX_SHARES >= 3
294 dest->
W[0] = random1a ^ src->
W[0];
295 dest->
W[1] = random1b ^ src->
W[1];
302 #if ASCON_MASKED_MAX_SHARES >= 4
310 #if ASCON_MASKED_MAX_SHARES >= 4
318 dest->
W[0] = random1a ^ src->
W[0] ^
320 dest->
W[1] = random1b ^ src->
W[1] ^
334 #if ASCON_MASKED_MAX_SHARES >= 3
343 word->
W[0] = random1a ^ random2a;
344 word->
W[1] = random1b ^ random2b;
349 #if ASCON_MASKED_MAX_SHARES >= 4
365 ascon_separate(random1a);
366 ascon_separate(random1b);
367 ascon_separate(high);
369 word->
W[0] = ((high << 16) | (low & 0x0000FFFFU)) ^
371 word->
W[1] = ((high & 0xFFFF0000U) | (low >> 16)) ^
373 high = (random1a << 16) | (random1b & 0x0000FFFFU);
374 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
377 #if ASCON_MASKED_MAX_SHARES >= 4
388 uint32_t random1a, random1b;
389 uint32_t random2a, random2b;
393 uint64_t masked = random;
415 high = (uint32_t)(masked >> 32);
416 low = (uint32_t)masked;
417 random1a = (uint32_t)(random >> 32);
418 random1b = (uint32_t)random;
419 ascon_separate(random1a);
420 ascon_separate(random1b);
421 ascon_separate(high);
423 word->
W[0] = ((high << 16) | (low & 0x0000FFFFU)) ^ random2a;
424 word->
W[1] = ((high & 0xFFFF0000U) | (low >> 16)) ^ random2b;
425 high = (random1a << 16) | (random1b & 0x0000FFFFU);
426 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
431 #if ASCON_MASKED_MAX_SHARES >= 4
447 ascon_separate(random1a);
448 ascon_separate(random1b);
449 ascon_separate(high);
451 word->
W[0] = ((high << 16) | (low & 0x0000FFFFU)) ^
453 word->
W[1] = ((high & 0xFFFF0000U) | (low >> 16)) ^
455 high = (random1a << 16) | (random1b & 0x0000FFFFU);
456 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
459 #if ASCON_MASKED_MAX_SHARES >= 4
468 uint32_t high1 = (word->
W[0] >> 16) | (word->
W[1] & 0xFFFF0000U);
469 uint32_t low1 = (word->
W[0] & 0x0000FFFFU) | (word->
W[1] << 16);
474 uint32_t high3 = (high2 >> 16) | (low2 & 0xFFFF0000U);
475 uint32_t low3 = (high2 & 0x0000FFFFU) | (low2 << 16);
476 ascon_combine(high1);
478 ascon_combine(high3);
487 uint64_t masked1, masked2;
488 uint32_t high1, low1;
489 uint32_t high2, low2;
490 uint32_t high3, low3;
493 high1 = (word->
W[0] >> 16) | (word->
W[1] & 0xFFFF0000U);
494 low1 = (word->
W[0] & 0x0000FFFFU) | (word->
W[1] << 16);
499 high3 = (high2 >> 16) | (low2 & 0xFFFF0000U);
500 low3 = (high2 & 0x0000FFFFU) | (low2 << 16);
501 ascon_combine(high1);
503 ascon_combine(high3);
507 masked1 = (((uint64_t)high1) << 32) | low1;
508 masked2 = (((uint64_t)high3) << 32) | low3;
526 data[0] = (uint8_t)(masked1 ^ masked2);
538 dest->
W[0] = src->
W[0] ^ random1a ^ random2a;
539 dest->
W[1] = src->
W[1] ^ random1b ^ random2b;
549 dest->
S[0] ^= src->
S[0];
550 dest->
S[1] ^= src->
S[1];
551 dest->
S[2] ^= src->
S[2];
557 uint32_t mask1 = (~((uint32_t)0)) >> (size * 4U);
558 uint32_t mask2 = ~mask1;
559 dest->
W[0] = (dest->
W[0] & mask1) | (src->
W[0] & mask2);
560 dest->
W[1] = (dest->
W[1] & mask1) | (src->
W[1] & mask2);
563 dest->
W[2] = (dest->
W[2] & mask1) | (src->
W[2] & mask2);
564 dest->
W[3] = (dest->
W[3] & mask1) | (src->
W[3] & mask2);
567 dest->
W[4] = (dest->
W[4] & mask1) | (src->
W[4] & mask2);
568 dest->
W[5] = (dest->
W[5] & mask1) | (src->
W[5] & mask2);
579 dest->
W[0] = random1a ^ random2a ^ src->
W[0];
580 dest->
W[1] = random1b ^ random2b ^ src->
W[1];
585 #if ASCON_MASKED_MAX_SHARES >= 4
591 #if ASCON_MASKED_MAX_SHARES >= 4
601 dest->
W[0] = random1a ^ random2a ^ src->
W[0] ^
603 dest->
W[1] = random1b ^ random2b ^ src->
W[1] ^
617 #if ASCON_MASKED_MAX_SHARES >= 4
628 word->
W[0] = random1a ^ random2a ^ random3a;
629 word->
W[1] = random1b ^ random2b ^ random3b;
650 ascon_separate(random1a);
651 ascon_separate(random1b);
652 ascon_separate(high);
654 word->
W[0] = ((high << 16) | (low & 0x0000FFFFU)) ^
657 word->
W[1] = ((high & 0xFFFF0000U) | (low >> 16)) ^
660 high = (random1a << 16) | (random1b & 0x0000FFFFU);
661 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
671 uint32_t random1a, random1b;
672 uint32_t random2a, random2b;
673 uint32_t random3a, random3b;
677 uint64_t masked = random;
701 high = (uint32_t)(masked >> 32);
702 low = (uint32_t)masked;
703 random1a = (uint32_t)(random >> 32);
704 random1b = (uint32_t)random;
705 ascon_separate(random1a);
706 ascon_separate(random1b);
707 ascon_separate(high);
709 word->
W[0] = ((high << 16) | (low & 0x0000FFFFU)) ^ random2a ^ random3a;
710 word->
W[1] = ((high & 0xFFFF0000U) | (low >> 16)) ^ random2b ^ random3b;
711 high = (random1a << 16) | (random1b & 0x0000FFFFU);
712 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
733 ascon_separate(random1a);
734 ascon_separate(random1b);
735 ascon_separate(high);
737 word->
W[0] = ((high << 16) | (low & 0x0000FFFFU)) ^
740 word->
W[1] = ((high & 0xFFFF0000U) | (low >> 16)) ^
743 high = (random1a << 16) | (random1b & 0x0000FFFFU);
744 low = (random1a & 0xFFFF0000U) | (random1b >> 16);
752 uint32_t high1 = (word->
W[0] >> 16) | (word->
W[1] & 0xFFFF0000U);
753 uint32_t low1 = (word->
W[0] & 0x0000FFFFU) | (word->
W[1] << 16);
760 uint32_t high3 = (high2 >> 16) | (low2 & 0xFFFF0000U);
761 uint32_t low3 = (high2 & 0x0000FFFFU) | (low2 << 16);
762 ascon_combine(high1);
764 ascon_combine(high3);
773 uint64_t masked1, masked2;
774 uint32_t high1, low1;
775 uint32_t high2, low2;
776 uint32_t high3, low3;
779 high1 = (word->
W[0] >> 16) | (word->
W[1] & 0xFFFF0000U);
780 low1 = (word->
W[0] & 0x0000FFFFU) | (word->
W[1] << 16);
787 high3 = (high2 >> 16) | (low2 & 0xFFFF0000U);
788 low3 = (high2 & 0x0000FFFFU) | (low2 << 16);
789 ascon_combine(high1);
791 ascon_combine(high3);
795 masked1 = (((uint64_t)high1) << 32) | low1;
796 masked2 = (((uint64_t)high3) << 32) | low3;
814 data[0] = (uint8_t)(masked1 ^ masked2);
828 dest->
W[0] = src->
W[0] ^ random1a ^ random2a ^ random3a;
829 dest->
W[1] = src->
W[1] ^ random1b ^ random2b ^ random3b;
841 dest->
S[0] ^= src->
S[0];
842 dest->
S[1] ^= src->
S[1];
843 dest->
S[2] ^= src->
S[2];
844 dest->
S[3] ^= src->
S[3];
850 uint32_t mask1 = (~((uint32_t)0)) >> (size * 4U);
851 uint32_t mask2 = ~mask1;
852 dest->
W[0] = (dest->
W[0] & mask1) | (src->
W[0] & mask2);
853 dest->
W[1] = (dest->
W[1] & mask1) | (src->
W[1] & mask2);
856 dest->
W[2] = (dest->
W[2] & mask1) | (src->
W[2] & mask2);
857 dest->
W[3] = (dest->
W[3] & mask1) | (src->
W[3] & mask2);
860 dest->
W[4] = (dest->
W[4] & mask1) | (src->
W[4] & mask2);
861 dest->
W[5] = (dest->
W[5] & mask1) | (src->
W[5] & mask2);
864 dest->
W[6] = (dest->
W[6] & mask1) | (src->
W[6] & mask2);
865 dest->
W[7] = (dest->
W[7] & mask1) | (src->
W[7] & mask2);
878 dest->
W[0] = random1a ^ random2a ^ random3a ^ src->
W[0];
879 dest->
W[1] = random1b ^ random2b ^ random3b ^ src->
W[1];
898 dest->
W[0] = random1a ^ random2a ^ random3a ^ src->
W[0];
899 dest->
W[1] = random1b ^ random2b ^ random3b ^ src->
W[1];
912 word->
W[1] ^= (((uint32_t)0x80000000U) >> (offset * 4U));
void ascon_masked_word_x4_load_32(ascon_masked_word_t *word, const uint8_t *data1, const uint8_t *data2, ascon_trng_state_t *trng)
Loads two 32-bit big endian values from buffers, masks them, and writes the result to a x4 masked wor...
void ascon_masked_word_pad(ascon_masked_word_t *word, unsigned offset)
Adds a padding marker to a masked word.
void ascon_masked_word_x3_replace(ascon_masked_word_t *dest, const ascon_masked_word_t *src, unsigned size)
Replace part of a destination x3 masked word with part of a source.
void ascon_masked_word_x2_store_partial(uint8_t *data, unsigned size, const ascon_masked_word_t *word)
Unmasks and stores the contents of a x2 masked word structure to a partial buffer.
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_store_partial(uint8_t *data, unsigned size, const ascon_masked_word_t *word)
Unmasks and stores the contents of a x3 masked word structure to a partial buffer.
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_x2_zero(ascon_masked_word_t *word, ascon_trng_state_t *trng)
Sets a x2 masked word to zero.
void ascon_masked_word_x4_load_partial(ascon_masked_word_t *word, const uint8_t *data, unsigned size, ascon_trng_state_t *trng)
Loads a 8-bit to 56-bit big endian value from buffer, masks it, and writes it to a x4 masked word str...
void ascon_masked_word_separator(ascon_masked_word_t *word)
Adds a separator marker to a masked word.
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_load_32(ascon_masked_word_t *word, const uint8_t *data1, const uint8_t *data2, ascon_trng_state_t *trng)
Loads two 32-bit big endian values from buffers, masks them, and writes the result to a x3 masked wor...
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_x4_replace(ascon_masked_word_t *dest, const ascon_masked_word_t *src, unsigned size)
Replace part of a destination x4 masked word with part of a source.
void ascon_masked_word_x4_store_partial(uint8_t *data, unsigned size, const ascon_masked_word_t *word)
Unmasks and stores the contents of a x4 masked word structure to a partial buffer.
void ascon_masked_word_x2_load_partial(ascon_masked_word_t *word, const uint8_t *data, unsigned size, ascon_trng_state_t *trng)
Loads a 8-bit to 56-bit big endian value from buffer, masks it, and writes it to a x2 masked word str...
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_x2_load_32(ascon_masked_word_t *word, const uint8_t *data1, const uint8_t *data2, ascon_trng_state_t *trng)
Loads two 32-bit big endian values from buffers, masks them, and writes the result to a x2 masked wor...
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_xor(ascon_masked_word_t *dest, const ascon_masked_word_t *src)
XOR's a source x4 masked word against a destination x4 masked word.
void ascon_masked_word_x2_replace(ascon_masked_word_t *dest, const ascon_masked_word_t *src, unsigned size)
Replace part of a destination x2 masked word with part of a source.
void ascon_masked_word_x3_load_partial(ascon_masked_word_t *word, const uint8_t *data, unsigned size, ascon_trng_state_t *trng)
Loads a 8-bit to 56-bit big endian value from buffer, masks it, and writes it to a x3 masked word str...
void ascon_masked_word_x3_xor(ascon_masked_word_t *dest, const ascon_masked_word_t *src)
XOR's a source x3 masked word against a destination x3 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_zero(ascon_masked_word_t *word, ascon_trng_state_t *trng)
Sets a x4 masked word to zero.
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_zero(ascon_masked_word_t *word, ascon_trng_state_t *trng)
Sets a x3 masked word to zero.
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_xor(ascon_masked_word_t *dest, const ascon_masked_word_t *src)
XOR's a source x2 masked word against a destination x2 masked word.
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.
Utility functions for operating on masked words.
#define ascon_mask32_unrotate_share3_1(x)
Unrotates 32-bit masked share 3 with respect to share 1.
#define ascon_mask32_unrotate_share2_1(x)
Unrotates 32-bit masked share 2 with respect to share 1.
#define ascon_mask32_unrotate_share2_0(x)
Unrotates 32-bit masked share 2 with respect to share 0.
#define ascon_mask32_rotate_share3_0(x)
Rotates 32-bit masked share 3 with respect to share 0.
#define ascon_mask32_rotate_share2_0(x)
Rotates 32-bit masked share 2 with respect to share 0.
#define ascon_mask32_unrotate_share1_0(x)
Unrotates 32-bit masked share 1 with respect to share 0.
#define ascon_mask32_rotate_share1_0(x)
Rotates 32-bit masked share 1 with respect to share 0.
#define ascon_mask32_unrotate_share3_0(x)
Unrotates 32-bit masked share 3 with respect to share 0.
uint32_t ascon_trng_generate_32(ascon_trng_state_t *state)
Generates a 32-bit random value for masking operations.
uint64_t ascon_trng_generate_64(ascon_trng_state_t *state)
Generates a 64-bit random value for masking operations.
#define be_load_word16(ptr)
#define leftRotate16_64(a)
#define be_store_word16(ptr, x)
#define rightRotate8_64(a)
#define be_load_word32(ptr)
#define rightRotate16_64(a)
#define leftRotate32_64(a)
#define rightRotate32_64(a)
#define leftRotate8_64(a)
#define be_store_word32(ptr, x)
unsigned char data[8]
[snippet_key]
State of the random number source.
Masked 64-bit word with up to ASCON_MASKED_MAX_SHARES shares.
uint32_t W[ASCON_MASKED_MAX_SHARES *2]
uint64_t S[ASCON_MASKED_MAX_SHARES]