ASCON Suite
ascon-aead-masked-common.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 
24 
25 /* Not needed if we won't be masking associated data and plaintext */
26 #if ASCON_MASKED_DATA_SHARES != 1
27 
29  (ascon_masked_state_t *state, const unsigned char *data,
30  size_t len, uint8_t first_round, ascon_masked_word_t *word,
31  uint64_t *preserve, ascon_trng_state_t *trng)
32 {
33  while (len >= 8) {
34  ascon_masked_data_load(word, data, trng);
35  ascon_masked_data_xor(&(state->M[0]), word);
36  ascon_masked_data_permute(state, first_round, preserve);
37  data += 8;
38  len -= 8;
39  }
40  if (len > 0) {
41  ascon_masked_data_load_partial(word, data, len, trng);
42  ascon_masked_data_xor(&(state->M[0]), word);
43  }
44  ascon_masked_word_pad(&(state->M[0]), len);
45  ascon_masked_data_permute(state, first_round, preserve);
46 }
47 
49  (ascon_masked_state_t *state, const unsigned char *data,
50  size_t len, uint8_t first_round, ascon_masked_word_t *word,
51  uint64_t *preserve, ascon_trng_state_t *trng)
52 {
53  while (len >= 16) {
54  ascon_masked_data_load(word, data, trng);
55  ascon_masked_data_xor(&(state->M[0]), word);
56  ascon_masked_data_load(word, data + 8, trng);
57  ascon_masked_data_xor(&(state->M[1]), word);
58  ascon_masked_data_permute(state, first_round, preserve);
59  data += 16;
60  len -= 16;
61  }
62  if (len >= 8) {
63  ascon_masked_data_load(word, data, trng);
64  ascon_masked_data_xor(&(state->M[0]), word);
65  data += 8;
66  len -= 8;
67  if (len > 0) {
68  ascon_masked_data_load_partial(word, data, len, trng);
69  ascon_masked_data_xor(&(state->M[1]), word);
70  }
71  ascon_masked_word_pad(&(state->M[1]), len);
72  } else {
73  if (len > 0) {
74  ascon_masked_data_load_partial(word, data, len, trng);
75  ascon_masked_data_xor(&(state->M[0]), word);
76  }
77  ascon_masked_word_pad(&(state->M[0]), len);
78  }
79  ascon_masked_data_permute(state, first_round, preserve);
80 }
81 
83  (ascon_masked_state_t *state, unsigned char *dest,
84  const unsigned char *src, size_t len, uint8_t first_round,
85  ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
86 {
87  while (len >= 8) {
88  ascon_masked_data_load(word, src, trng);
89  ascon_masked_data_xor(&(state->M[0]), word);
90  ascon_masked_data_store(dest, &(state->M[0]));
91  ascon_masked_data_permute(state, first_round, preserve);
92  dest += 8;
93  src += 8;
94  len -= 8;
95  }
96  if (len > 0) {
97  ascon_masked_data_load_partial(word, src, len, trng);
98  ascon_masked_data_xor(&(state->M[0]), word);
99  ascon_masked_data_store_partial(dest, len, &(state->M[0]));
100  }
101  ascon_masked_word_pad(&(state->M[0]), len);
102 }
103 
105  (ascon_masked_state_t *state, unsigned char *dest,
106  const unsigned char *src, size_t len, uint8_t first_round,
107  ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
108 {
109  while (len >= 16) {
110  ascon_masked_data_load(word, src, trng);
111  ascon_masked_data_xor(&(state->M[0]), word);
112  ascon_masked_data_load(word, src + 8, trng);
113  ascon_masked_data_xor(&(state->M[1]), word);
114  ascon_masked_data_store(dest, &(state->M[0]));
115  ascon_masked_data_store(dest + 8, &(state->M[1]));
116  ascon_masked_data_permute(state, first_round, preserve);
117  dest += 16;
118  src += 16;
119  len -= 16;
120  }
121  if (len >= 8) {
122  ascon_masked_data_load(word, src, trng);
123  ascon_masked_data_xor(&(state->M[0]), word);
124  ascon_masked_data_store(dest, &(state->M[0]));
125  dest += 8;
126  src += 8;
127  len -= 8;
128  if (len > 0) {
129  ascon_masked_data_load_partial(word, src, len, trng);
130  ascon_masked_data_xor(&(state->M[1]), word);
131  ascon_masked_data_store_partial(dest, len, &(state->M[1]));
132  }
133  ascon_masked_word_pad(&(state->M[1]), len);
134  } else {
135  if (len > 0) {
136  ascon_masked_data_load_partial(word, src, len, trng);
137  ascon_masked_data_xor(&(state->M[0]), word);
138  ascon_masked_data_store_partial(dest, len, &(state->M[0]));
139  }
140  ascon_masked_word_pad(&(state->M[0]), len);
141  }
142 }
143 
145  (ascon_masked_state_t *state, unsigned char *dest,
146  const unsigned char *src, size_t len, uint8_t first_round,
147  ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
148 {
149  while (len >= 8) {
150  ascon_masked_data_load(word, src, trng);
151  ascon_masked_data_xor(&(state->M[0]), word);
152  ascon_masked_data_store(dest, &(state->M[0]));
153  state->M[0] = *word;
154  ascon_masked_data_permute(state, first_round, preserve);
155  dest += 8;
156  src += 8;
157  len -= 8;
158  }
159  if (len > 0) {
160  ascon_masked_data_load_partial(word, src, len, trng);
161  ascon_masked_data_xor(&(state->M[0]), word);
162  ascon_masked_data_store_partial(dest, len, &(state->M[0]));
163  ascon_masked_data_replace(&(state->M[0]), word, len);
164  }
165  ascon_masked_word_pad(&(state->M[0]), len);
166 }
167 
169  (ascon_masked_state_t *state, unsigned char *dest,
170  const unsigned char *src, size_t len, uint8_t first_round,
171  ascon_masked_word_t *word, uint64_t *preserve, ascon_trng_state_t *trng)
172 {
173  while (len >= 16) {
174  ascon_masked_data_load(word, src, trng);
175  ascon_masked_data_xor(&(state->M[0]), word);
176  ascon_masked_data_store(dest, &(state->M[0]));
177  state->M[0] = *word;
178  ascon_masked_data_load(word, src + 8, trng);
179  ascon_masked_data_xor(&(state->M[1]), word);
180  ascon_masked_data_store(dest + 8, &(state->M[1]));
181  state->M[1] = *word;
182  ascon_masked_data_permute(state, first_round, preserve);
183  dest += 16;
184  src += 16;
185  len -= 16;
186  }
187  if (len >= 8) {
188  ascon_masked_data_load(word, src, trng);
189  ascon_masked_data_xor(&(state->M[0]), word);
190  ascon_masked_data_store(dest, &(state->M[0]));
191  state->M[0] = *word;
192  dest += 8;
193  src += 8;
194  len -= 8;
195  if (len > 0) {
196  ascon_masked_data_load_partial(word, src, len, trng);
197  ascon_masked_data_xor(&(state->M[1]), word);
198  ascon_masked_data_store_partial(dest, len, &(state->M[1]));
199  ascon_masked_data_replace(&(state->M[1]), word, len);
200  }
201  ascon_masked_word_pad(&(state->M[1]), len);
202  } else {
203  if (len > 0) {
204  ascon_masked_data_load_partial(word, src, len, trng);
205  ascon_masked_data_xor(&(state->M[0]), word);
206  ascon_masked_data_store_partial(dest, len, &(state->M[0]));
207  ascon_masked_data_replace(&(state->M[0]), word, len);
208  }
209  ascon_masked_word_pad(&(state->M[0]), len);
210  }
211 }
212 
213 #endif /* ASCON_MASKED_DATA_SHARES != 1 */
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.
void ascon_masked_word_pad(ascon_masked_word_t *word, unsigned offset)
Adds a padding marker to a masked word.
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.