ASCON Suite
ascon-util-snp.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_UTIL_SNP_H
24 #define ASCON_UTIL_SNP_H
25 
26 /*
27  * Utilities to help with absorbing and squeezing data without the
28  * function call overhead of the SnP API in <ascon/permutation.h>.
29  *
30  * These utilties are highly tied to the specific backend that is
31  * selected so they are not suitable for the public-facing API.
32  */
33 
34 #include <ascon/permutation.h>
35 #include "ascon-select-backend.h"
36 #include "ascon-util.h"
37 #include <string.h>
38 
39 #if defined(ASCON_BACKEND_SLICED32)
40 
41 #include "ascon-sliced32.h"
42 
43 #define ascon_separator(state) ((state)->W[8] ^= 0x01)
44 #define ascon_pad(state, offset) \
45  ((state)->W[((offset) / 8) * 2 + 1] ^= \
46  (0x80000000U >> (((offset) & 7) * 4)))
47 
48 #define ascon_absorb_8(state, data, offset) \
49  ascon_absorb_sliced((state), (data), (offset) / 8)
50 #define ascon_absorb_16(state, data, offset) \
51  do { \
52  ascon_absorb_sliced((state), (data), (offset) / 8); \
53  ascon_absorb_sliced((state), (data) + 8, (offset) / 8 + 1); \
54  } while (0)
55 #define ascon_absorb_partial(state, data, offset, count) \
56  ascon_add_bytes((state), (data), (offset), (count))
57 
58 #define ascon_squeeze_8(state, data, offset) \
59  ascon_squeeze_sliced((state), (data), (offset) / 8)
60 #define ascon_squeeze_16(state, data, offset) \
61  do { \
62  ascon_squeeze_sliced((state), (data), (offset) / 8); \
63  ascon_squeeze_sliced((state), (data) + 8, (offset) / 8 + 1); \
64  } while (0)
65 #define ascon_squeeze_partial(state, data, offset, count) \
66  ascon_extract_bytes((state), (data), (offset), (count))
67 
68 #define ascon_encrypt_8(state, dest, src, offset) \
69  ascon_encrypt_sliced((state), (dest), (src), (offset) / 8)
70 #define ascon_encrypt_16(state, dest, src, offset) \
71  do { \
72  ascon_encrypt_sliced((state), (dest), (src), (offset) / 8); \
73  ascon_encrypt_sliced((state), (dest) + 8, (src) + 8, (offset) / 8 + 1); \
74  } while (0)
75 #define ascon_encrypt_partial(state, dest, src, offset, count) \
76  do { \
77  ascon_add_bytes((state), (src), (offset), (count)); \
78  ascon_extract_bytes((state), (dest), (offset), (count)); \
79  } while (0)
80 
81 #define ascon_decrypt_8(state, dest, src, offset) \
82  ascon_decrypt_sliced((state), (dest), (src), (offset) / 8)
83 #define ascon_decrypt_16(state, dest, src, offset) \
84  do { \
85  ascon_decrypt_sliced((state), (dest), (src), (offset) / 8); \
86  ascon_decrypt_sliced((state), (dest) + 8, (src) + 8, (offset) / 8 + 1); \
87  } while (0)
88 #define ascon_decrypt_partial(state, dest, src, offset, count) \
89  ascon_extract_and_overwrite_bytes((state), (src), (dest), (offset), (count))
90 
91 #elif defined(ASCON_BACKEND_SLICED64)
92 
93 #define ascon_separator(state) ((state)->S[4] ^= 0x01)
94 #define ascon_pad(state, offset) \
95  ((state)->S[(offset) / 8] ^= \
96  (0x8000000000000000ULL >> (((offset) & 7) * 8)))
97 
98 #define ascon_absorb_8(state, data, offset) \
99  ((state)->S[(offset) / 8] ^= be_load_word64((data)))
100 #define ascon_absorb_16(state, data, offset) \
101  do { \
102  ((state)->S[(offset) / 8] ^= be_load_word64((data))); \
103  ((state)->S[(offset) / 8 + 1] ^= be_load_word64((data) + 8)); \
104  } while (0)
105 #define ascon_absorb_partial(state, data, offset, count) \
106  ascon_add_bytes((state), (data), (offset), (count))
107 
108 #define ascon_squeeze_8(state, data, offset) \
109  be_store_word64((data), (state)->S[(offset) / 8])
110 #define ascon_squeeze_16(state, data, offset) \
111  do { \
112  be_store_word64((data), (state)->S[(offset) / 8]); \
113  be_store_word64((data) + 8, (state)->S[(offset) / 8 + 1]); \
114  } while (0)
115 #define ascon_squeeze_partial(state, data, offset, count) \
116  ascon_extract_bytes((state), (data), (offset), (count))
117 
118 #define ascon_encrypt_8(state, dest, src, offset) \
119  do { \
120  (state)->S[(offset) / 8] ^= be_load_word64((src)); \
121  be_store_word64((dest), (state)->S[(offset) / 8]); \
122  } while (0)
123 #define ascon_encrypt_16(state, dest, src, offset) \
124  do { \
125  (state)->S[(offset) / 8] ^= be_load_word64((src)); \
126  (state)->S[(offset) / 8 + 1] ^= be_load_word64((src) + 8); \
127  be_store_word64((dest), (state)->S[(offset) / 8]); \
128  be_store_word64((dest) + 8, (state)->S[(offset) / 8 + 1]); \
129  } while (0)
130 #define ascon_encrypt_partial(state, dest, src, offset, count) \
131  do { \
132  ascon_add_bytes((state), (src), (offset), (count)); \
133  ascon_extract_bytes((state), (dest), (offset), (count)); \
134  } while (0)
135 
136 #define ascon_decrypt_8(state, dest, src, offset) \
137  do { \
138  uint64_t word = be_load_word64((src)); \
139  be_store_word64((dest), word ^ (state)->S[(offset) / 8]); \
140  (state)->S[(offset) / 8] = word; \
141  } while (0)
142 #define ascon_decrypt_16(state, dest, src, offset) \
143  do { \
144  uint64_t word = be_load_word64((src)); \
145  be_store_word64((dest), word ^ (state)->S[(offset) / 8]); \
146  (state)->S[(offset) / 8] = word; \
147  word = be_load_word64((src) + 8); \
148  be_store_word64((dest) + 8, word ^ (state)->S[(offset) / 8 + 1]); \
149  (state)->S[(offset) / 8 + 1] = word; \
150  } while (0)
151 #define ascon_decrypt_partial(state, dest, src, offset, count) \
152  ascon_extract_and_overwrite_bytes((state), (src), (dest), (offset), (count))
153 
154 #elif defined(ASCON_BACKEND_DIRECT_XOR) && !defined(ASCON_FORCE_GENERIC)
155 
156 #define ascon_separator(state) ((state)->B[39] ^= 0x01)
157 #define ascon_pad(state, offset) ((state)->B[(offset)] ^= 0x80)
158 
159 #define ascon_absorb_8(state, data, offset) \
160  lw_xor_block((state)->B + (offset), (data), 8)
161 #define ascon_absorb_16(state, data, offset) \
162  lw_xor_block((state)->B + (offset), (data), 16)
163 #define ascon_absorb_partial(state, data, offset, count) \
164  lw_xor_block((state)->B + (offset), (data), (count))
165 
166 #define ascon_squeeze_8(state, data, offset) \
167  memcpy((data), (state)->B + (offset), 8)
168 #define ascon_squeeze_16(state, data, offset) \
169  memcpy((data), (state)->B + (offset), 16)
170 #define ascon_squeeze_partial(state, data, offset, count) \
171  memcpy((data), (state)->B + (offset), (count))
172 
173 #define ascon_encrypt_8(state, dest, src, offset) \
174  lw_xor_block_2_dest((dest), (state)->B + (offset), (src), 8)
175 #define ascon_encrypt_16(state, dest, src, offset) \
176  lw_xor_block_2_dest((dest), (state)->B + (offset), (src), 16)
177 #define ascon_encrypt_partial(state, dest, src, offset, count) \
178  lw_xor_block_2_dest((dest), (state)->B + (offset), (src), (count))
179 
180 #define ascon_decrypt_8(state, dest, src, offset) \
181  lw_xor_block_swap((dest), (state)->B + (offset), (src), 8)
182 #define ascon_decrypt_16(state, dest, src, offset) \
183  lw_xor_block_swap((dest), (state)->B + (offset), (src), 16)
184 #define ascon_decrypt_partial(state, dest, src, offset, count) \
185  lw_xor_block_swap((dest), (state)->B + (offset), (src), (count))
186 
187 #else /* ASCON_BACKEND_GENERIC */
188 
189 #define ascon_separator(state) \
190  do { \
191  uint8_t sep = 0x01; \
192  ascon_add_bytes((state), &sep, 39, 1); \
193  } while (0)
194 #define ascon_pad(state, offset) \
195  do { \
196  uint8_t padding = 0x80; \
197  ascon_add_bytes((state), &padding, (offset), 1); \
198  } while (0)
199 
200 #define ascon_absorb_8(state, data, offset) \
201  ascon_add_bytes((state), (data), (offset), 8)
202 #define ascon_absorb_16(state, data, offset) \
203  ascon_add_bytes((state), (data), (offset), 16)
204 #define ascon_absorb_partial(state, data, offset, count) \
205  ascon_add_bytes((state), (data), (offset), (count))
206 
207 #define ascon_squeeze_8(state, data, offset) \
208  ascon_extract_bytes((state), (data), (offset), 8)
209 #define ascon_squeeze_16(state, data, offset) \
210  ascon_extract_bytes((state), (data), (offset), 16)
211 #define ascon_squeeze_partial(state, data, offset, count) \
212  ascon_extract_bytes((state), (data), (offset), (count))
213 
214 #define ascon_encrypt_8(state, dest, src, offset) \
215  do { \
216  ascon_add_bytes((state), (src), (offset), 8); \
217  ascon_extract_bytes((state), (dest), (offset), 8); \
218  } while (0)
219 #define ascon_encrypt_16(state, dest, src, offset) \
220  do { \
221  ascon_add_bytes((state), (src), (offset), 16); \
222  ascon_extract_bytes((state), (dest), (offset), 16); \
223  } while (0)
224 #define ascon_encrypt_partial(state, dest, src, offset, count) \
225  do { \
226  ascon_add_bytes((state), (src), (offset), (count)); \
227  ascon_extract_bytes((state), (dest), (offset), (count)); \
228  } while (0)
229 
230 #define ascon_decrypt_8(state, dest, src, offset) \
231  ascon_extract_and_overwrite_bytes((state), (src), (dest), (offset), 8)
232 #define ascon_decrypt_16(state, dest, src, offset) \
233  ascon_extract_and_overwrite_bytes((state), (src), (dest), (offset), 16)
234 #define ascon_decrypt_partial(state, dest, src, offset, count) \
235  ascon_extract_and_overwrite_bytes((state), (src), (dest), (offset), (count))
236 
237 #endif /* ASCON_BACKEND_GENERIC */
238 
239 #if defined(ASCON_BACKEND_INIT)
247 #else
248 #define ascon_backend_init(state) do { ; } while (0)
249 #endif
250 
251 #if defined(ASCON_BACKEND_FREE)
259 #else
260 #define ascon_backend_free(state) do { ; } while (0)
261 #endif
262 
263 #endif
#define ascon_backend_free(state)
#define ascon_backend_init(state)
Direct access to the ASCON permutation primitive.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
Structure of the internal state of the ASCON permutation.
Definition: permutation.h:63