27 static uint8_t
const ASCON128_IV[8] =
28 {0x80, 0x40, 0x0c, 0x06, 0x00, 0x00, 0x00, 0x00};
32 static void ascon128_masked_aead_init
38 uint64_t *preserve,
const unsigned char *npub,
42 #if ASCON_MASKED_KEY_SHARES == 2
44 #elif ASCON_MASKED_KEY_SHARES == 3
55 ascon_masked_key_randomize(
state, trng);
56 ascon_masked_key_load(word, ASCON128_IV, trng);
57 ascon_masked_key_xor(&(
state->M[0]), word);
58 ascon_masked_key_xor(&(
state->M[1]), &(k->
k[0]));
59 ascon_masked_key_xor(&(
state->M[2]), &(k->
k[1]));
60 ascon_masked_key_load(word, npub, trng);
61 ascon_masked_key_xor(&(
state->M[3]), word);
62 ascon_masked_key_load(word, npub + 8, trng);
63 ascon_masked_key_xor(&(
state->M[4]), word);
64 ascon_masked_key_permute(
state, 0, preserve);
65 ascon_masked_key_xor(&(
state->M[3]), &(k->
k[0]));
66 ascon_masked_key_xor(&(
state->M[4]), &(k->
k[1]));
69 #if ASCON_MASKED_DATA_SHARES == 1
70 ascon_copy_key_to_x1(state_x1,
state);
71 #elif ASCON_MASKED_DATA_SHARES == 2
72 ascon_copy_key_to_x2(
state, trng);
73 #elif ASCON_MASKED_DATA_SHARES == 3
74 ascon_copy_key_to_x3(
state, trng);
76 ascon_copy_key_to_x4(
state, trng);
81 static void ascon128_masked_aead_finalize
90 #if ASCON_MASKED_KEY_SHARES == 2
92 #elif ASCON_MASKED_KEY_SHARES == 3
102 #if ASCON_MASKED_DATA_SHARES == 1
103 ascon_copy_key_from_x1(
state, state_x1, trng);
104 #elif ASCON_MASKED_DATA_SHARES == 2
105 ascon_copy_key_from_x2(
state, trng);
106 #elif ASCON_MASKED_DATA_SHARES == 3
107 ascon_copy_key_from_x3(
state, trng);
109 ascon_copy_key_from_x4(
state, trng);
113 ascon_masked_key_xor(&(
state->M[1]), &(k->
k[0]));
114 ascon_masked_key_xor(&(
state->M[2]), &(k->
k[1]));
115 ascon_masked_key_permute(
state, 0, preserve);
116 ascon_masked_key_xor(&(
state->M[3]), &(k->
k[0]));
117 ascon_masked_key_xor(&(
state->M[4]), &(k->
k[1]));
118 ascon_masked_key_store(tag, &(
state->M[3]));
119 ascon_masked_key_store(tag + 8, &(
state->M[4]));
123 (
unsigned char *c,
size_t *clen,
124 const unsigned char *m,
size_t mlen,
125 const unsigned char *ad,
size_t adlen,
126 const unsigned char *npub,
130 #if ASCON_MASKED_DATA_SHARES == 1
132 unsigned char partial;
144 #if ASCON_MASKED_DATA_SHARES == 1
146 ascon128_masked_aead_init
147 (&
state, &state_x1, &trng, &word, preserve, npub, k);
161 ascon128_masked_aead_finalize
162 (&
state, &state_x1, &trng, preserve, k, c + mlen);
165 ascon128_masked_aead_init(&
state, &trng, &word, preserve, npub, k);
170 (&
state, ad, adlen, 6, &word, preserve, &trng);
178 (&
state, c, m, mlen, 6, &word, preserve, &trng);
181 ascon128_masked_aead_finalize(&
state, &trng, preserve, k, c + mlen);
185 #if ASCON_MASKED_DATA_SHARES == 1
195 (
unsigned char *m,
size_t *mlen,
196 const unsigned char *c,
size_t clen,
197 const unsigned char *ad,
size_t adlen,
198 const unsigned char *npub,
202 #if ASCON_MASKED_DATA_SHARES == 1
204 unsigned char partial;
220 #if ASCON_MASKED_DATA_SHARES == 1
222 ascon128_masked_aead_init
223 (&
state, &state_x1, &trng, &word, preserve, npub, k);
237 ascon128_masked_aead_finalize(&
state, &state_x1, &trng, preserve, k, tag);
240 ascon128_masked_aead_init(&
state, &trng, &word, preserve, npub, k);
245 (&
state, ad, adlen, 6, &word, preserve, &trng);
253 (&
state, m, c, *mlen, 6, &word, preserve, &trng);
256 ascon128_masked_aead_finalize(&
state, &trng, preserve, k, tag);
263 #if ASCON_MASKED_DATA_SHARES == 1
#define ASCON128_TAG_SIZE
Size of the authentication tag for ASCON-128 and ASCON-128a.
unsigned char ascon_aead_encrypt_8(ascon_state_t *state, unsigned char *dest, const unsigned char *src, size_t len, uint8_t first_round, unsigned char partial)
Encrypts a block of data with an ASCON state and an 8-byte rate.
int ascon_aead_check_tag(unsigned char *plaintext, size_t plaintext_len, const unsigned char *tag1, const unsigned char *tag2, size_t size)
Check an authentication tag in constant time.
unsigned char ascon_aead_decrypt_8(ascon_state_t *state, unsigned char *dest, const unsigned char *src, size_t len, uint8_t first_round, unsigned char partial)
Decrypts a block of data with an ASCON state and an 8-byte rate.
void ascon_aead_absorb_8(ascon_state_t *state, const unsigned char *data, size_t len, uint8_t first_round, int last_permute)
Absorbs data into an ASCON state with an 8-byte rate.
int ascon128_masked_aead_decrypt(unsigned char *m, size_t *mlen, const unsigned char *c, size_t clen, const unsigned char *ad, size_t adlen, const unsigned char *npub, const ascon_masked_key_128_t *k)
Decrypts and authenticates a packet with masked ASCON-128.
void ascon128_masked_aead_encrypt(unsigned char *c, size_t *clen, const unsigned char *m, size_t mlen, const unsigned char *ad, size_t adlen, const unsigned char *npub, const ascon_masked_key_128_t *k)
Encrypts and authenticates a packet with masked ASCON-128.
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_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.
#define ASCON_MASKED_DATA_SHARES
Number of shares to use for plaintext data and associated data, between 1 and ASCON_MASKED_KEY_SHARES...
#define ASCON_MASKED_KEY_SHARES
Number of shares to use for key material, between 2 and 4 with the default being 4.
void ascon_masked_state_free(ascon_masked_state_t *state)
Frees an ASCON-x2 permutation state and attempts to destroy any sensitive material.
void ascon_masked_state_init(ascon_masked_state_t *state)
Initializes the words of a masked ASCON permutation state.
void ascon_masked_word_separator(ascon_masked_word_t *word)
Adds a separator marker to a masked word.
uint64_t ascon_trng_generate_64(ascon_trng_state_t *state)
Generates a 64-bit random value for masking operations.
int ascon_trng_init(ascon_trng_state_t *state)
Initializes the random number source for generating a sequence of masking material at high speed.
void ascon_trng_free(ascon_trng_state_t *state)
Frees the random number source and destroys any sensitive material.
#define ascon_pad(state, offset)
#define ascon_separator(state)
void ascon_free(ascon_state_t *state)
Frees an ASCON permutation state and attempts to destroy any sensitive material.
ascon_state_t state
[snippet_key]
128-bit key that has been masked to hide its value when the code is operating on it.
ascon_masked_key_word_t k[2]
State of the ASCON permutation which has been masked with up to 4 shares.
State of the random number source.
Masked 64-bit word with up to ASCON_MASKED_MAX_SHARES shares.
Structure of the internal state of the ASCON permutation.
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.