36 #if defined(ISAP_ALG_NAME)
38 #if !defined(ISAP_KEY_SIZE)
39 #define ISAP_KEY_SIZE 16
40 #define ISAP_NONCE_SIZE 16
41 #define ISAP_TAG_SIZE 16
44 #define ISAP_CONCAT_INNER(name,suffix) name##suffix
45 #define ISAP_CONCAT(name,suffix) ISAP_CONCAT_INNER(name,suffix)
48 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_A)
49 [
sizeof(ISAP_STATE) - ISAP_NONCE_SIZE] = {
50 0x01, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
51 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
55 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA)
56 [
sizeof(ISAP_STATE) - ISAP_KEY_SIZE] = {
57 0x02, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
58 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
62 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_KE)
63 [
sizeof(ISAP_STATE) - ISAP_KEY_SIZE] = {
64 0x03, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
65 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
79 static void ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
80 (ISAP_STATE *state,
const unsigned char *k,
const unsigned char *iv,
81 const unsigned char *data,
unsigned data_len)
83 #if defined(ISAP_PERMUTE_SLICED)
84 unsigned bit, num_bits;
87 memcpy(state->B, k, ISAP_KEY_SIZE);
88 memcpy(state->B + ISAP_KEY_SIZE, iv,
sizeof(state->B) - ISAP_KEY_SIZE);
90 ISAP_PERMUTE_SLICED(state, ISAP_sK);
93 num_bits = data_len * 8 - 1;
94 for (bit = 0; bit < num_bits; ++bit) {
96 (((uint32_t)(data[bit / 8])) << (24 + bit % 8)) & 0x80000000U;
97 ISAP_PERMUTE_SLICED(state, ISAP_sB);
100 (((uint32_t)(data[bit / 8])) << (24 + bit % 8)) & 0x80000000U;
101 ISAP_PERMUTE_SLICED(state, ISAP_sK);
103 unsigned bit, num_bits;
106 memcpy(state->B, k, ISAP_KEY_SIZE);
107 memcpy(state->B + ISAP_KEY_SIZE, iv,
sizeof(state->B) - ISAP_KEY_SIZE);
108 ISAP_PERMUTE(state, ISAP_sK);
111 num_bits = data_len * 8 - 1;
112 for (bit = 0; bit < num_bits; ++bit) {
113 state->B[0] ^= (data[bit / 8] << (bit % 8)) & 0x80;
114 ISAP_PERMUTE(state, ISAP_sB);
116 state->B[0] ^= (data[bit / 8] << (bit % 8)) & 0x80;
117 ISAP_PERMUTE(state, ISAP_sK);
131 static void ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)
132 (ISAP_STATE *state,
const unsigned char *k,
const unsigned char *npub,
133 unsigned char *c,
const unsigned char *m,
size_t mlen)
135 #if defined(ISAP_PERMUTE_SLICED)
136 unsigned char block[ISAP_RATE];
139 ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
140 (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KE), npub, ISAP_NONCE_SIZE);
145 while (mlen >= ISAP_RATE) {
146 ISAP_PERMUTE_SLICED(state, ISAP_sE);
148 lw_xor_block_2_src(c, block, m, ISAP_RATE);
154 ISAP_PERMUTE_SLICED(state, ISAP_sE);
156 lw_xor_block_2_src(c, block, m, (
unsigned)mlen);
160 ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
161 (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KE), npub, ISAP_NONCE_SIZE);
162 memcpy(state->B +
sizeof(ISAP_STATE) - ISAP_NONCE_SIZE,
163 npub, ISAP_NONCE_SIZE);
166 while (mlen >= ISAP_RATE) {
167 ISAP_PERMUTE(state, ISAP_sE);
168 lw_xor_block_2_src(c, state->B, m, ISAP_RATE);
174 ISAP_PERMUTE(state, ISAP_sE);
175 lw_xor_block_2_src(c, state->B, m, (
unsigned)mlen);
191 static void ISAP_CONCAT(ISAP_ALG_NAME,_mac)
192 (ISAP_STATE *state,
const unsigned char *k,
const unsigned char *npub,
193 const unsigned char *ad,
size_t adlen,
194 const unsigned char *c,
size_t clen,
197 #if defined(ISAP_PERMUTE_SLICED)
198 unsigned char preserve[
sizeof(ISAP_STATE) - ISAP_TAG_SIZE];
199 unsigned char padded[ISAP_RATE];
203 memcpy(state->B, npub, ISAP_NONCE_SIZE);
204 memcpy(state->B + ISAP_NONCE_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_IV_A),
205 sizeof(state->B) - ISAP_NONCE_SIZE);
207 ISAP_PERMUTE_SLICED(state, ISAP_sH);
208 while (adlen >= ISAP_RATE) {
210 ISAP_PERMUTE_SLICED(state, ISAP_sH);
214 temp = (unsigned)adlen;
215 memcpy(padded, ad, temp);
217 memset(padded + temp + 1, 0,
sizeof(padded) - (temp + 1));
219 ISAP_PERMUTE_SLICED(state, ISAP_sH);
223 while (clen >= ISAP_RATE) {
225 ISAP_PERMUTE_SLICED(state, ISAP_sH);
229 temp = (unsigned)clen;
230 memcpy(padded, c, temp);
232 memset(padded + temp + 1, 0,
sizeof(padded) - (temp + 1));
234 ISAP_PERMUTE_SLICED(state, ISAP_sH);
238 memcpy(tag, state->B, ISAP_TAG_SIZE);
239 memcpy(preserve, state->B + ISAP_TAG_SIZE,
sizeof(preserve));
240 ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
241 (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA), tag, ISAP_TAG_SIZE);
243 memcpy(state->B + ISAP_TAG_SIZE, preserve,
sizeof(preserve));
245 ISAP_PERMUTE_SLICED(state, ISAP_sH);
249 unsigned char preserve[
sizeof(ISAP_STATE) - ISAP_TAG_SIZE];
253 memcpy(state->B, npub, ISAP_NONCE_SIZE);
254 memcpy(state->B + ISAP_NONCE_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_IV_A),
255 sizeof(state->B) - ISAP_NONCE_SIZE);
256 ISAP_PERMUTE(state, ISAP_sH);
257 while (adlen >= ISAP_RATE) {
258 lw_xor_block(state->B, ad, ISAP_RATE);
259 ISAP_PERMUTE(state, ISAP_sH);
263 temp = (unsigned)adlen;
264 lw_xor_block(state->B, ad, temp);
265 state->B[temp] ^= 0x80;
266 ISAP_PERMUTE(state, ISAP_sH);
267 state->B[
sizeof(state->B) - 1] ^= 0x01;
270 while (clen >= ISAP_RATE) {
271 lw_xor_block(state->B, c, ISAP_RATE);
272 ISAP_PERMUTE(state, ISAP_sH);
276 temp = (unsigned)clen;
277 lw_xor_block(state->B, c, temp);
278 state->B[temp] ^= 0x80;
279 ISAP_PERMUTE(state, ISAP_sH);
282 memcpy(tag, state->B, ISAP_TAG_SIZE);
283 memcpy(preserve, state->B + ISAP_TAG_SIZE,
sizeof(preserve));
284 ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
285 (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA), tag, ISAP_TAG_SIZE);
286 memcpy(state->B + ISAP_TAG_SIZE, preserve,
sizeof(preserve));
287 ISAP_PERMUTE(state, ISAP_sH);
288 memcpy(tag, state->B, ISAP_TAG_SIZE);
292 int ISAP_CONCAT(ISAP_ALG_NAME,_aead_encrypt)
293 (
unsigned char *c,
size_t *clen,
294 const unsigned char *m,
size_t mlen,
295 const unsigned char *ad,
size_t adlen,
296 const unsigned char *npub,
297 const unsigned char *k)
302 *clen = mlen + ISAP_TAG_SIZE;
305 ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)(&state, k, npub, c, m, mlen);
308 ISAP_CONCAT(ISAP_ALG_NAME,_mac)
309 (&state, k, npub, ad, adlen, c, mlen, c + mlen);
313 int ISAP_CONCAT(ISAP_ALG_NAME,_aead_decrypt)
314 (
unsigned char *m,
size_t *mlen,
315 const unsigned char *c,
size_t clen,
316 const unsigned char *ad,
size_t adlen,
317 const unsigned char *npub,
318 const unsigned char *k)
321 unsigned char tag[ISAP_TAG_SIZE];
324 if (clen < ISAP_TAG_SIZE)
326 *mlen = clen - ISAP_TAG_SIZE;
329 ISAP_CONCAT(ISAP_ALG_NAME,_mac)(&state, k, npub, ad, adlen, c, *mlen, tag);
332 ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)(&state, k, npub, m, c, *mlen);
335 return aead_check_tag(m, *mlen, tag, c + *mlen, ISAP_TAG_SIZE);
350 #undef ISAP_PERMUTE_SLICED
351 #undef ISAP_CONCAT_INNER
#define ascon_squeeze_sliced(state, data, offset)
Squeezes data from the ASCON state in sliced form.
Definition: internal-ascon.h:232
#define ascon_absorb_sliced(state, data, offset)
Absorbs data into the ASCON state in sliced form.
Definition: internal-ascon.h:175
void ascon_to_sliced(ascon_state_t *state)
Converts an ASCON state from byte form into sliced form.
Definition: internal-ascon.c:96
#define ascon_set_sliced(state, data, offset)
Sets data into the ASCON state in sliced form.
Definition: internal-ascon.h:156
void ascon_from_sliced(ascon_state_t *state)
Converts an ASCON state from sliced form into byte form.
Definition: internal-ascon.c:110