ASCON Suite
ascon-masked-word.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_MASKED_WORD_H
24 #define ASCON_MASKED_WORD_H
25 
26 #include <ascon/masking.h>
27 #include "ascon-masked-backend.h"
28 #include "random/ascon-trng.h"
29 #include "core/ascon-util.h"
30 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
66 typedef union
67 {
70 
72  uint32_t W[ASCON_MASKED_MAX_SHARES * 2];
73 
75  uint8_t B[ASCON_MASKED_MAX_SHARES * 8];
76 
78 
79 #if !defined(ASCON_MASKED_WORD_BACKEND_DIRECT_XOR)
80 
88 #define ascon_mask64_rotate_share1_0(x) (rightRotate11_64((x)))
89 
97 #define ascon_mask64_rotate_share2_0(x) (rightRotate22_64((x)))
98 
106 #define ascon_mask64_rotate_share2_1(x) (rightRotate11_64((x)))
107 
115 #define ascon_mask64_rotate_share3_0(x) (rightRotate33_64((x)))
116 
124 #define ascon_mask64_rotate_share3_1(x) (rightRotate22_64((x)))
125 
133 #define ascon_mask64_rotate_share3_2(x) (rightRotate11_64((x)))
134 
142 #define ascon_mask64_unrotate_share1_0(x) (rightRotate53_64((x)))
143 
151 #define ascon_mask64_unrotate_share2_0(x) (rightRotate42_64((x)))
152 
160 #define ascon_mask64_unrotate_share2_1(x) (rightRotate53_64((x)))
161 
169 #define ascon_mask64_unrotate_share3_0(x) (rightRotate31_64((x)))
170 
178 #define ascon_mask64_unrotate_share3_1(x) (rightRotate42_64((x)))
179 
187 #define ascon_mask64_unrotate_share3_2(x) (rightRotate53_64((x)))
188 
196 #define ascon_mask32_rotate_share1_0(x) (rightRotate5((x)))
197 
205 #define ascon_mask32_rotate_share2_0(x) (rightRotate10((x)))
206 
214 #define ascon_mask32_rotate_share2_1(x) (rightRotate5((x)))
215 
223 #define ascon_mask32_rotate_share3_0(x) (rightRotate15((x)))
224 
232 #define ascon_mask32_rotate_share3_1(x) (rightRotate10((x)))
233 
241 #define ascon_mask32_rotate_share3_2(x) (rightRotate5((x)))
242 
250 #define ascon_mask32_unrotate_share1_0(x) (rightRotate27((x)))
251 
259 #define ascon_mask32_unrotate_share2_0(x) (rightRotate22((x)))
260 
268 #define ascon_mask32_unrotate_share2_1(x) (rightRotate27((x)))
269 
277 #define ascon_mask32_unrotate_share3_0(x) (rightRotate17((x)))
278 
286 #define ascon_mask32_unrotate_share3_1(x) (rightRotate22((x)))
287 
295 #define ascon_mask32_unrotate_share3_2(x) (rightRotate27((x)))
296 
297 #else /* ASCON_MASKED_WORD_BACKEND_DIRECT_XOR */
298 
299 /* Direct XOR masking method does not rotate the shares */
300 #define ascon_mask64_rotate_share1_0(x) (x)
301 #define ascon_mask64_rotate_share2_0(x) (x)
302 #define ascon_mask64_rotate_share2_1(x) (x)
303 #define ascon_mask64_rotate_share3_0(x) (x)
304 #define ascon_mask64_rotate_share3_1(x) (x)
305 #define ascon_mask64_rotate_share3_2(x) (x)
306 #define ascon_mask64_unrotate_share1_0(x) (x)
307 #define ascon_mask64_unrotate_share2_0(x) (x)
308 #define ascon_mask64_unrotate_share2_1(x) (x)
309 #define ascon_mask64_unrotate_share3_0(x) (x)
310 #define ascon_mask64_unrotate_share3_1(x) (x)
311 #define ascon_mask64_unrotate_share3_2(x) (x)
312 #define ascon_mask32_rotate_share1_0(x) (x)
313 #define ascon_mask32_rotate_share2_0(x) (x)
314 #define ascon_mask32_rotate_share2_1(x) (x)
315 #define ascon_mask32_rotate_share3_0(x) (x)
316 #define ascon_mask32_rotate_share3_1(x) (x)
317 #define ascon_mask32_rotate_share3_2(x) (x)
318 #define ascon_mask32_unrotate_share1_0(x) (x)
319 #define ascon_mask32_unrotate_share2_0(x) (x)
320 #define ascon_mask32_unrotate_share2_1(x) (x)
321 #define ascon_mask32_unrotate_share3_0(x) (x)
322 #define ascon_mask32_unrotate_share3_1(x) (x)
323 #define ascon_mask32_unrotate_share3_2(x) (x)
324 
325 #endif /* ASCON_MASKED_WORD_BACKEND_DIRECT_XOR */
326 
335 
347  (ascon_masked_word_t *word, const uint8_t *data,
348  ascon_trng_state_t *trng);
349 
360  (ascon_masked_word_t *word, const uint8_t *data, unsigned size,
361  ascon_trng_state_t *trng);
362 
376  (ascon_masked_word_t *word, const uint8_t *data1,
377  const uint8_t *data2, ascon_trng_state_t *trng);
378 
388  (uint8_t *data, const ascon_masked_word_t *word);
389 
401  (uint8_t *data, unsigned size, const ascon_masked_word_t *word);
402 
414  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
415  ascon_trng_state_t *trng);
416 
424  (ascon_masked_word_t *dest, const ascon_masked_word_t *src);
425 
434  (ascon_masked_word_t *dest, const ascon_masked_word_t *src, unsigned size);
435 
444  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
445  ascon_trng_state_t *trng);
446 
455  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
456  ascon_trng_state_t *trng);
457 
466 
478  (ascon_masked_word_t *word, const uint8_t *data,
479  ascon_trng_state_t *trng);
480 
491  (ascon_masked_word_t *word, const uint8_t *data, unsigned size,
492  ascon_trng_state_t *trng);
493 
507  (ascon_masked_word_t *word, const uint8_t *data1,
508  const uint8_t *data2, ascon_trng_state_t *trng);
509 
519  (uint8_t *data, const ascon_masked_word_t *word);
520 
532  (uint8_t *data, unsigned size, const ascon_masked_word_t *word);
533 
545  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
546  ascon_trng_state_t *trng);
547 
555  (ascon_masked_word_t *dest, const ascon_masked_word_t *src);
556 
565  (ascon_masked_word_t *dest, const ascon_masked_word_t *src, unsigned size);
566 
575  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
576  ascon_trng_state_t *trng);
577 
586  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
587  ascon_trng_state_t *trng);
588 
597 
609  (ascon_masked_word_t *word, const uint8_t *data,
610  ascon_trng_state_t *trng);
611 
624  (ascon_masked_word_t *word, const uint8_t *data, unsigned size,
625  ascon_trng_state_t *trng);
626 
640  (ascon_masked_word_t *word, const uint8_t *data1,
641  const uint8_t *data2, ascon_trng_state_t *trng);
642 
652  (uint8_t *data, const ascon_masked_word_t *word);
653 
665  (uint8_t *data, unsigned size, const ascon_masked_word_t *word);
666 
678  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
679  ascon_trng_state_t *trng);
680 
688  (ascon_masked_word_t *dest, const ascon_masked_word_t *src);
689 
698  (ascon_masked_word_t *dest, const ascon_masked_word_t *src, unsigned size);
699 
708  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
709  ascon_trng_state_t *trng);
710 
719  (ascon_masked_word_t *dest, const ascon_masked_word_t *src,
720  ascon_trng_state_t *trng);
721 
728 void ascon_masked_word_pad(ascon_masked_word_t *word, unsigned offset);
729 
736 
737 #ifdef __cplusplus
738 }
739 #endif
740 
741 #endif
#define ASCON_MASKED_MAX_SHARES
Maximum number of shares to use in the library.
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.
Access to the system's random number source.
Definitions to support masked ASCON ciphers.
unsigned char data[8]
[snippet_key]
Definition: snippets.c:14
State of the random number source.
Definition: ascon-trng.h:64
Masked 64-bit word with up to ASCON_MASKED_MAX_SHARES shares.