36 #if defined(ISAP_ALG_NAME)
38 #define ISAP_CONCAT_INNER(name,suffix) name##suffix
39 #define ISAP_CONCAT(name,suffix) ISAP_CONCAT_INNER(name,suffix)
42 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_A)
45 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
49 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA)
52 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
56 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_KE)
59 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
73 static void ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
74 (ISAP_STATE *state,
const unsigned char *k,
const unsigned char *iv,
75 const unsigned char *data,
unsigned data_len)
77 #if defined(ISAP_PERMUTE_SLICED)
78 unsigned bit, num_bits;
84 ISAP_PERMUTE_SLICED(state, ISAP_sK);
87 num_bits = data_len * 8 - 1;
88 for (bit = 0; bit < num_bits; ++bit) {
90 (((uint32_t)(data[bit / 8])) << (24 + bit % 8)) & 0x80000000U;
91 ISAP_PERMUTE_SLICED(state, ISAP_sB);
94 (((uint32_t)(data[bit / 8])) << (24 + bit % 8)) & 0x80000000U;
95 ISAP_PERMUTE_SLICED(state, ISAP_sK);
97 unsigned bit, num_bits;
102 ISAP_PERMUTE(state, ISAP_sK);
105 num_bits = data_len * 8 - 1;
106 for (bit = 0; bit < num_bits; ++bit) {
107 state->B[0] ^= (data[bit / 8] << (bit % 8)) & 0x80;
108 ISAP_PERMUTE(state, ISAP_sB);
110 state->B[0] ^= (data[bit / 8] << (bit % 8)) & 0x80;
111 ISAP_PERMUTE(state, ISAP_sK);
125 static void ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)
126 (ISAP_STATE *state,
const unsigned char *k,
const unsigned char *npub,
127 unsigned char *c,
const unsigned char *m,
unsigned long long mlen)
129 #if defined(ISAP_PERMUTE_SLICED)
130 unsigned char block[ISAP_RATE];
133 ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
139 while (mlen >= ISAP_RATE) {
140 ISAP_PERMUTE_SLICED(state, ISAP_sE);
142 lw_xor_block_2_src(c, block, m, ISAP_RATE);
148 ISAP_PERMUTE_SLICED(state, ISAP_sE);
150 lw_xor_block_2_src(c, block, m, (
unsigned)mlen);
154 ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
160 while (mlen >= ISAP_RATE) {
161 ISAP_PERMUTE(state, ISAP_sE);
162 lw_xor_block_2_src(c, state->B, m, ISAP_RATE);
168 ISAP_PERMUTE(state, ISAP_sE);
169 lw_xor_block_2_src(c, state->B, m, (
unsigned)mlen);
185 static void ISAP_CONCAT(ISAP_ALG_NAME,_mac)
186 (ISAP_STATE *state,
const unsigned char *k,
const unsigned char *npub,
187 const unsigned char *ad,
unsigned long long adlen,
188 const unsigned char *c,
unsigned long long clen,
191 #if defined(ISAP_PERMUTE_SLICED)
193 unsigned char padded[ISAP_RATE];
201 ISAP_PERMUTE_SLICED(state, ISAP_sH);
202 while (adlen >= ISAP_RATE) {
204 ISAP_PERMUTE_SLICED(state, ISAP_sH);
208 temp = (unsigned)adlen;
209 memcpy(padded, ad, temp);
211 memset(padded + temp + 1, 0,
sizeof(padded) - (temp + 1));
213 ISAP_PERMUTE_SLICED(state, ISAP_sH);
217 while (clen >= ISAP_RATE) {
219 ISAP_PERMUTE_SLICED(state, ISAP_sH);
223 temp = (unsigned)clen;
224 memcpy(padded, c, temp);
226 memset(padded + temp + 1, 0,
sizeof(padded) - (temp + 1));
228 ISAP_PERMUTE_SLICED(state, ISAP_sH);
233 memcpy(preserve, state->B +
ISAP_TAG_SIZE,
sizeof(preserve));
234 ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
235 (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA), tag,
ISAP_TAG_SIZE);
237 memcpy(state->B +
ISAP_TAG_SIZE, preserve,
sizeof(preserve));
239 ISAP_PERMUTE_SLICED(state, ISAP_sH);
250 ISAP_PERMUTE(state, ISAP_sH);
251 while (adlen >= ISAP_RATE) {
252 lw_xor_block(state->B, ad, ISAP_RATE);
253 ISAP_PERMUTE(state, ISAP_sH);
257 temp = (unsigned)adlen;
258 lw_xor_block(state->B, ad, temp);
259 state->B[temp] ^= 0x80;
260 ISAP_PERMUTE(state, ISAP_sH);
261 state->B[
sizeof(state->B) - 1] ^= 0x01;
264 while (clen >= ISAP_RATE) {
265 lw_xor_block(state->B, c, ISAP_RATE);
266 ISAP_PERMUTE(state, ISAP_sH);
270 temp = (unsigned)clen;
271 lw_xor_block(state->B, c, temp);
272 state->B[temp] ^= 0x80;
273 ISAP_PERMUTE(state, ISAP_sH);
277 memcpy(preserve, state->B +
ISAP_TAG_SIZE,
sizeof(preserve));
278 ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
279 (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA), tag,
ISAP_TAG_SIZE);
280 memcpy(state->B +
ISAP_TAG_SIZE, preserve,
sizeof(preserve));
281 ISAP_PERMUTE(state, ISAP_sH);
286 int ISAP_CONCAT(ISAP_ALG_NAME,_aead_encrypt)
287 (
unsigned char *c,
unsigned long long *clen,
288 const unsigned char *m,
unsigned long long mlen,
289 const unsigned char *ad,
unsigned long long adlen,
290 const unsigned char *nsec,
291 const unsigned char *npub,
292 const unsigned char *k)
301 ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)(&state, k, npub, c, m, mlen);
304 ISAP_CONCAT(ISAP_ALG_NAME,_mac)
305 (&state, k, npub, ad, adlen, c, mlen, c + mlen);
309 int ISAP_CONCAT(ISAP_ALG_NAME,_aead_decrypt)
310 (
unsigned char *m,
unsigned long long *mlen,
312 const unsigned char *c,
unsigned long long clen,
313 const unsigned char *ad,
unsigned long long adlen,
314 const unsigned char *npub,
315 const unsigned char *k)
327 ISAP_CONCAT(ISAP_ALG_NAME,_mac)(&state, k, npub, ad, adlen, c, *mlen, tag);
330 ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)(&state, k, npub, m, c, *mlen);
348 #undef ISAP_PERMUTE_SLICED
349 #undef ISAP_CONCAT_INNER
#define ascon_squeeze_sliced(state, data, offset)
Squeezes data from the ASCON state in sliced form.
Definition: internal-ascon.h:217
#define ascon_absorb_sliced(state, data, offset)
Absorbs data into the ASCON state in sliced form.
Definition: internal-ascon.h:160
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:141
int aead_check_tag(unsigned char *plaintext, unsigned long long plaintext_len, const unsigned char *tag1, const unsigned char *tag2, unsigned tag_len)
Check an authentication tag in constant time.
Definition: aead-common.c:26
#define ISAP_NONCE_SIZE
Size of the nonce for all ISAP family members.
Definition: isap.h:68
#define ISAP_TAG_SIZE
Size of the authentication tag for all ISAP family members.
Definition: isap.h:63
void ascon_from_sliced(ascon_state_t *state)
Converts an ASCON state from sliced form into byte form.
Definition: internal-ascon.c:110
#define ISAP_KEY_SIZE
Size of the key for all ISAP family members.
Definition: isap.h:58