ASCON Suite
ascon-aead-inc-80pq.c
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 #include "aead/ascon-aead-common.h"
24 #include "core/ascon-util-snp.h"
25 #include <string.h>
26 
27 /* Initialization vector for ASCON-80pq */
28 static uint8_t const ASCON80PQ_IV[4] = {0xa0, 0x40, 0x0c, 0x06};
29 
31  (ascon80pq_state_t *state, const unsigned char *ad, size_t adlen,
32  const unsigned char *npub, const unsigned char *k)
33 {
34  /* Initialize the ASCON state */
35  memcpy(state->key, k, ASCON80PQ_KEY_SIZE);
36  ascon_init(&(state->state));
37  ascon_overwrite_bytes(&(state->state), ASCON80PQ_IV, 0, 4);
39  ascon_overwrite_bytes(&(state->state), npub, 24, ASCON80PQ_NONCE_SIZE);
40  ascon_permute(&(state->state), 0);
41  ascon_absorb_partial(&(state->state), state->key, 20, ASCON80PQ_KEY_SIZE);
42 
43  /* Absorb the associated data into the state */
44  if (adlen > 0)
45  ascon_aead_absorb_8(&(state->state), ad, adlen, 6, 1);
46 
47  /* Separator between the associated data and the payload */
48  ascon_separator(&(state->state));
49 
50  /* Prepare for encryption or decryption */
51  ascon_release(&(state->state));
52  state->posn = 0;
53 }
54 
56 {
57  if (state) {
58  ascon_acquire(&(state->state));
59  ascon_free(&(state->state));
61  }
62 }
63 
65  (ascon80pq_state_t *state, const unsigned char *in,
66  unsigned char *out, size_t len)
67 {
68  ascon_acquire(&(state->state));
70  (&(state->state), out, in, len, 6, state->posn);
71  ascon_release(&(state->state));
72 }
73 
75  (ascon80pq_state_t *state, unsigned char *tag)
76 {
77  /* Pad the final plaintext block */
78  ascon_acquire(&(state->state));
79  ascon_pad(&(state->state), state->posn);
80 
81  /* Finalize and compute the authentication tag */
83  ascon_permute(&(state->state), 0);
84  ascon_absorb_16(&(state->state), state->key + 4, 24);
85  ascon_squeeze_16(&(state->state), tag, 24);
86 
87  /* Clean up */
88  ascon_free(&(state->state));
90 }
91 
93  (ascon80pq_state_t *state, const unsigned char *in,
94  unsigned char *out, size_t len)
95 {
96  ascon_acquire(&(state->state));
98  (&(state->state), out, in, len, 6, state->posn);
99  ascon_release(&(state->state));
100 }
101 
103  (ascon80pq_state_t *state, const unsigned char *tag)
104 {
105  unsigned char tag2[ASCON80PQ_TAG_SIZE];
106  int result;
107 
108  /* Pad the final ciphertext block */
109  ascon_acquire(&(state->state));
110  ascon_pad(&(state->state), state->posn);
111 
112  /* Finalize and check the authentication tag */
113  ascon_absorb_partial(&(state->state), state->key, 8, ASCON80PQ_KEY_SIZE);
114  ascon_permute(&(state->state), 0);
115  ascon_absorb_16(&(state->state), state->key + 4, 24);
116  ascon_squeeze_16(&(state->state), tag2, 24);
117  result = ascon_aead_check_tag(0, 0, tag2, tag, ASCON80PQ_TAG_SIZE);
118 
119  /* Clean up */
120  ascon_clean(tag2, sizeof(tag2));
121  ascon_free(&(state->state));
123  return result;
124 }
#define ASCON80PQ_NONCE_SIZE
Size of the nonce for ASCON-80pq.
Definition: aead.h:75
#define ASCON80PQ_TAG_SIZE
Size of the authentication tag for ASCON-80pq.
Definition: aead.h:80
#define ASCON80PQ_KEY_SIZE
Size of the key for ASCON-80pq.
Definition: aead.h:70
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.
void ascon80pq_aead_start(ascon80pq_state_t *state, const unsigned char *ad, size_t adlen, const unsigned char *npub, const unsigned char *k)
Starts encrypting or decrypting a packet with ASCON-80pq in incremental mode.
void ascon80pq_aead_encrypt_block(ascon80pq_state_t *state, const unsigned char *in, unsigned char *out, size_t len)
Encrypts a block of data with ASCON-80pq in incremental mode.
void ascon80pq_aead_abort(ascon80pq_state_t *state)
Aborts use of ASCON-80pq in incremental mode.
void ascon80pq_aead_decrypt_block(ascon80pq_state_t *state, const unsigned char *in, unsigned char *out, size_t len)
Decrypts a block of data with ASCON-80pq in incremental mode.
void ascon80pq_aead_encrypt_finalize(ascon80pq_state_t *state, unsigned char *tag)
Finalizes an incremental ASCON-80pq encryption operation and generates the authentication tag.
int ascon80pq_aead_decrypt_finalize(ascon80pq_state_t *state, const unsigned char *tag)
Finalizes an incremental ASCON-80pq decryption operation and checks the authentication tag.
#define ascon_pad(state, offset)
#define ascon_absorb_16(state, data, offset)
#define ascon_absorb_partial(state, data, offset, count)
#define ascon_squeeze_16(state, data, offset)
#define ascon_separator(state)
void ascon_free(ascon_state_t *state)
Frees an ASCON permutation state and attempts to destroy any sensitive material.
void ascon_release(ascon_state_t *state)
Temporarily releases access to any shared hardware resources that a permutation state was using.
void ascon_overwrite_bytes(ascon_state_t *state, const uint8_t *data, unsigned offset, unsigned size)
Overwrites existing bytes in the ASCON state.
void ascon_permute(ascon_state_t *state, uint8_t first_round)
Permutes the ASCON state with a specified number of rounds.
Definition: ascon-c32.c:36
void ascon_acquire(ascon_state_t *state)
Re-acquires access to any shared hardware resources that a permutation state was using.
void ascon_init(ascon_state_t *state)
Initializes the words of the ASCON permutation state to zero.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
State information for the incremental version of ASCON-80pq.
Definition: aead.h:299
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
Definition: ascon-clean.c:38