ASCON Suite
ascon-siv-128a.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 <ascon/siv.h>
24 #include "aead/ascon-aead-common.h"
25 #include "core/ascon-util-snp.h"
26 #include <string.h>
27 
31 static uint8_t const ASCON128a_IV1[8] =
32  {0x81, 0x80, 0x0c, 0x08, 0x00, 0x00, 0x00, 0x00};
33 
37 static uint8_t const ASCON128a_IV2[8] =
38  {0x82, 0x80, 0x0c, 0x08, 0x00, 0x00, 0x00, 0x00};
39 
48 static void ascon128a_siv_init
49  (ascon_state_t *state, const unsigned char *npub,
50  const unsigned char *k, const uint8_t iv[8])
51 {
53  ascon_overwrite_bytes(state, iv, 0, 8);
56  ascon_permute(state, 0);
57  ascon_absorb_16(state, k, 24);
58 }
59 
72 static void ascon_siv_encrypt_16
73  (ascon_state_t *state, unsigned char *dest,
74  const unsigned char *src, size_t len, uint8_t first_round)
75 {
76  unsigned char block[16];
77  while (len >= 16) {
78  ascon_permute(state, first_round);
79  ascon_squeeze_16(state, block, 0);
80  lw_xor_block_2_src(dest, block, src, 16);
81  dest += 16;
82  src += 16;
83  len -= 16;
84  }
85  if (len > 0) {
86  ascon_permute(state, first_round);
87  ascon_squeeze_16(state, block, 0);
88  lw_xor_block_2_src(dest, block, src, len);
89  }
90 }
91 
93  (unsigned char *c, size_t *clen,
94  const unsigned char *m, size_t mlen,
95  const unsigned char *ad, size_t adlen,
96  const unsigned char *npub,
97  const unsigned char *k)
98 {
100 
101  /* Set the length of the returned ciphertext */
102  *clen = mlen + ASCON128_TAG_SIZE;
103 
104  /* Initialize the ASCON state for the authentication phase */
105  ascon128a_siv_init(&state, npub, k, ASCON128a_IV1);
106 
107  /* Absorb the associated data into the state */
108  if (adlen > 0)
109  ascon_aead_absorb_16(&state, ad, adlen, 4, 1);
110 
111  /* Separator between the associated data and the payload */
113 
114  /* Absorb the plaintext data into the state */
115  ascon_aead_absorb_16(&state, m, mlen, 4, 0);
116 
117  /* Compute the authentication tag */
118  ascon_absorb_16(&state, k, 16);
119  ascon_permute(&state, 0);
120  ascon_absorb_16(&state, k, 24);
121  ascon_squeeze_16(&state, c + mlen, 24);
122  ascon_free(&state);
123 
124  /* Re-initalize the ASCON state for the encryption phase */
125  ascon128a_siv_init(&state, c + mlen, k, ASCON128a_IV2);
126 
127  /* Encrypt the plaintext to create the ciphertext */
128  ascon_siv_encrypt_16(&state, c, m, mlen, 4);
129  ascon_free(&state);
130 }
131 
133  (unsigned char *m, size_t *mlen,
134  const unsigned char *c, size_t clen,
135  const unsigned char *ad, size_t adlen,
136  const unsigned char *npub,
137  const unsigned char *k)
138 {
140  unsigned char tag[ASCON128_TAG_SIZE];
141  int result;
142 
143  /* Set the length of the returned plaintext */
144  if (clen < ASCON128_TAG_SIZE)
145  return -1;
146  clen -= ASCON128_TAG_SIZE;
147  *mlen = clen;
148 
149  /* Initalize the ASCON state for the encryption phase */
150  ascon128a_siv_init(&state, c + clen, k, ASCON128a_IV2);
151 
152  /* Decrypt the ciphertext to create the plaintext */
153  ascon_siv_encrypt_16(&state, m, c, clen, 4);
154  ascon_free(&state);
155 
156  /* Re-initialize the ASCON state for the authentication phase */
157  ascon128a_siv_init(&state, npub, k, ASCON128a_IV1);
158 
159  /* Absorb the associated data into the state */
160  if (adlen > 0)
161  ascon_aead_absorb_16(&state, ad, adlen, 4, 1);
162 
163  /* Separator between the associated data and the payload */
165 
166  /* Absorb the plaintext data into the state */
167  ascon_aead_absorb_16(&state, m, clen, 4, 0);
168 
169  /* Compute and check authentication tag */
170  ascon_absorb_16(&state, k, 16);
171  ascon_permute(&state, 0);
172  ascon_absorb_16(&state, k, 24);
173  ascon_squeeze_16(&state, tag, 24);
174  result = ascon_aead_check_tag(m, clen, tag, c + clen, ASCON128_TAG_SIZE);
175  ascon_clean(tag, sizeof(tag));
176  ascon_free(&state);
177  return result;
178 }
#define ASCON128_TAG_SIZE
Size of the authentication tag for ASCON-128 and ASCON-128a.
Definition: aead.h:65
#define ASCON128_NONCE_SIZE
Size of the nonce for ASCON-128 and ASCON-128a.
Definition: aead.h:60
#define ASCON128_KEY_SIZE
Size of the key for ASCON-128 and ASCON-128a.
Definition: aead.h:55
void ascon_aead_absorb_16(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 a 16-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.
int ascon128a_siv_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 unsigned char *k)
Decrypts and authenticates a packet with ASCON-128a-SIV.
void ascon128a_siv_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 unsigned char *k)
Encrypts and authenticates a packet with ASCON-128a-SIV.
#define ascon_absorb_16(state, data, offset)
#define ascon_squeeze_16(state, data, offset)
#define ascon_separator(state)
#define lw_xor_block_2_src(dest, src1, src2, len)
Definition: ascon-util.h:195
void ascon_free(ascon_state_t *state)
Frees an ASCON permutation state and attempts to destroy any sensitive material.
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_init(ascon_state_t *state)
Initializes the words of the ASCON permutation state to zero.
SIV encryption primitives built around the ASCON permutation.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
Structure of the internal state of the ASCON permutation.
Definition: permutation.h:63
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
Definition: ascon-clean.c:38