37 #if defined(ISAP_ALG_NAME)
39 #if !defined(ISAP_KEY_SIZE)
40 #define ISAP_KEY_SIZE 16
41 #define ISAP_NONCE_SIZE 16
42 #define ISAP_TAG_SIZE 16
45 #define ISAP_CONCAT_INNER(name,suffix) name##suffix
46 #define ISAP_CONCAT(name,suffix) ISAP_CONCAT_INNER(name,suffix)
49 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_A)
50 [
sizeof(ISAP_STATE) - ISAP_NONCE_SIZE] = {
51 0x01, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
52 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
56 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_KA)
57 [
sizeof(ISAP_STATE) - ISAP_KEY_SIZE] = {
58 0x02, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
59 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
63 static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_KE)
64 [
sizeof(ISAP_STATE) - ISAP_KEY_SIZE] = {
65 0x03, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
66 ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
79 static void ISAP_CONCAT(ISAP_ALG_NAME,_pk_rekey)
80 (ISAP_STATE *state,
const unsigned char *pk,
81 const unsigned char *data,
unsigned data_len)
83 #if defined(ISAP_PERMUTE_SLICED)
84 unsigned bit, num_bits;
87 memcpy(state->B, pk,
sizeof(state->B));
90 num_bits = data_len * 8 - 1;
91 for (bit = 0; bit < num_bits; ++bit) {
93 (((uint32_t)(data[bit / 8])) << (24 + bit % 8)) & 0x80000000U;
94 ISAP_PERMUTE_SLICED(state, ISAP_sB);
97 (((uint32_t)(data[bit / 8])) << (24 + bit % 8)) & 0x80000000U;
98 ISAP_PERMUTE_SLICED(state, ISAP_sK);
100 unsigned bit, num_bits;
103 memcpy(state->B, pk,
sizeof(state->B));
106 num_bits = data_len * 8 - 1;
107 for (bit = 0; bit < num_bits; ++bit) {
108 state->B[0] ^= (data[bit / 8] << (bit % 8)) & 0x80;
109 ISAP_PERMUTE(state, ISAP_sB);
111 state->B[0] ^= (data[bit / 8] << (bit % 8)) & 0x80;
112 ISAP_PERMUTE(state, ISAP_sK);
126 static void ISAP_CONCAT(ISAP_ALG_NAME,_pk_encrypt)
127 (ISAP_STATE *state,
const ISAP_KEY_STATE *pk,
const unsigned char *npub,
128 unsigned char *c,
const unsigned char *m,
size_t mlen)
130 #if defined(ISAP_PERMUTE_SLICED)
131 unsigned char block[ISAP_RATE];
134 ISAP_CONCAT(ISAP_ALG_NAME,_pk_rekey)(state, pk->ke, npub, ISAP_NONCE_SIZE);
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,_pk_rekey)(state, pk->ke, npub, ISAP_NONCE_SIZE);
155 memcpy(state->B +
sizeof(ISAP_STATE) - ISAP_NONCE_SIZE,
156 npub, ISAP_NONCE_SIZE);
159 while (mlen >= ISAP_RATE) {
160 ISAP_PERMUTE(state, ISAP_sE);
161 lw_xor_block_2_src(c, state->B, m, ISAP_RATE);
167 ISAP_PERMUTE(state, ISAP_sE);
168 lw_xor_block_2_src(c, state->B, m, (
unsigned)mlen);
184 static void ISAP_CONCAT(ISAP_ALG_NAME,_pk_mac)
185 (ISAP_STATE *state,
const ISAP_KEY_STATE *pk,
186 const unsigned char *npub,
187 const unsigned char *ad,
size_t adlen,
188 const unsigned char *c,
size_t clen,
191 #if defined(ISAP_PERMUTE_SLICED)
192 unsigned char preserve[
sizeof(ISAP_STATE) - ISAP_TAG_SIZE];
193 unsigned char padded[ISAP_RATE];
197 memcpy(state->B, npub, ISAP_NONCE_SIZE);
198 memcpy(state->B + ISAP_NONCE_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_A),
199 sizeof(state->B) - ISAP_NONCE_SIZE);
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);
232 memcpy(tag, state->B, ISAP_TAG_SIZE);
233 memcpy(preserve, state->B + ISAP_TAG_SIZE,
sizeof(preserve));
234 ISAP_CONCAT(ISAP_ALG_NAME,_pk_rekey)(state, pk->ka, tag, ISAP_TAG_SIZE);
236 memcpy(state->B + ISAP_TAG_SIZE, preserve,
sizeof(preserve));
238 ISAP_PERMUTE_SLICED(state, ISAP_sH);
242 unsigned char preserve[
sizeof(ISAP_STATE) - ISAP_TAG_SIZE];
246 memcpy(state->B, npub, ISAP_NONCE_SIZE);
247 memcpy(state->B + ISAP_NONCE_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_A),
248 sizeof(state->B) - ISAP_NONCE_SIZE);
249 ISAP_PERMUTE(state, ISAP_sH);
250 while (adlen >= ISAP_RATE) {
251 lw_xor_block(state->B, ad, ISAP_RATE);
252 ISAP_PERMUTE(state, ISAP_sH);
256 temp = (unsigned)adlen;
257 lw_xor_block(state->B, ad, temp);
258 state->B[temp] ^= 0x80;
259 ISAP_PERMUTE(state, ISAP_sH);
260 state->B[
sizeof(state->B) - 1] ^= 0x01;
263 while (clen >= ISAP_RATE) {
264 lw_xor_block(state->B, c, ISAP_RATE);
265 ISAP_PERMUTE(state, ISAP_sH);
269 temp = (unsigned)clen;
270 lw_xor_block(state->B, c, temp);
271 state->B[temp] ^= 0x80;
272 ISAP_PERMUTE(state, ISAP_sH);
275 memcpy(tag, state->B, ISAP_TAG_SIZE);
276 memcpy(preserve, state->B + ISAP_TAG_SIZE,
sizeof(preserve));
277 ISAP_CONCAT(ISAP_ALG_NAME,_pk_rekey)(state, pk->ka, tag, ISAP_TAG_SIZE);
278 memcpy(state->B + ISAP_TAG_SIZE, preserve,
sizeof(preserve));
279 ISAP_PERMUTE(state, ISAP_sH);
280 memcpy(tag, state->B, ISAP_TAG_SIZE);
293 int ISAP_CONCAT(ISAP_ALG_NAME,_aead_pk_init)
294 (ISAP_KEY_STATE *pk,
const unsigned char *k)
296 #if defined(ISAP_PERMUTE_SLICED)
300 memcpy(state.B, k, ISAP_KEY_SIZE);
301 memcpy(state.B + ISAP_KEY_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_KE),
302 sizeof(state.B) - ISAP_KEY_SIZE);
304 ISAP_PERMUTE_SLICED(&state, ISAP_sK);
305 memcpy(pk->ke, state.B,
sizeof(state.B));
308 memcpy(state.B, k, ISAP_KEY_SIZE);
309 memcpy(state.B + ISAP_KEY_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_KA),
310 sizeof(state.B) - ISAP_KEY_SIZE);
312 ISAP_PERMUTE_SLICED(&state, ISAP_sK);
313 memcpy(pk->ka, state.B,
sizeof(state.B));
320 memcpy(state.B, k, ISAP_KEY_SIZE);
321 memcpy(state.B + ISAP_KEY_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_KE),
322 sizeof(state.B) - ISAP_KEY_SIZE);
323 ISAP_PERMUTE(&state, ISAP_sK);
324 memcpy(pk->ke, state.B,
sizeof(state.B));
327 memcpy(state.B, k, ISAP_KEY_SIZE);
328 memcpy(state.B + ISAP_KEY_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_pk_IV_KA),
329 sizeof(state.B) - ISAP_KEY_SIZE);
330 ISAP_PERMUTE(&state, ISAP_sK);
331 memcpy(pk->ka, state.B,
sizeof(state.B));
337 int ISAP_CONCAT(ISAP_ALG_NAME,_aead_pk_encrypt)
338 (
unsigned char *c,
size_t *clen,
339 const unsigned char *m,
size_t mlen,
340 const unsigned char *ad,
size_t adlen,
341 const unsigned char *npub,
342 const ISAP_KEY_STATE *pk)
347 *clen = mlen + ISAP_TAG_SIZE;
350 ISAP_CONCAT(ISAP_ALG_NAME,_pk_encrypt)(&state, pk, npub, c, m, mlen);
353 ISAP_CONCAT(ISAP_ALG_NAME,_pk_mac)
354 (&state, pk, npub, ad, adlen, c, mlen, c + mlen);
358 int ISAP_CONCAT(ISAP_ALG_NAME,_aead_pk_decrypt)
359 (
unsigned char *m,
size_t *mlen,
360 const unsigned char *c,
size_t clen,
361 const unsigned char *ad,
size_t adlen,
362 const unsigned char *npub,
363 const ISAP_KEY_STATE *pk)
366 unsigned char tag[ISAP_TAG_SIZE];
369 if (clen < ISAP_TAG_SIZE)
371 *mlen = clen - ISAP_TAG_SIZE;
374 ISAP_CONCAT(ISAP_ALG_NAME,_pk_mac)
375 (&state, pk, npub, ad, adlen, c, *mlen, tag);
378 ISAP_CONCAT(ISAP_ALG_NAME,_pk_encrypt)(&state, pk, npub, m, c, *mlen);
381 return aead_check_tag(m, *mlen, tag, c + *mlen, ISAP_TAG_SIZE);
389 #undef ISAP_KEY_STATE
397 #undef ISAP_PERMUTE_SLICED
398 #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