ASCON Suite
ascon-kmaca.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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/kmac.h>
24 #include <ascon/utility.h>
26 #include "core/ascon-util-snp.h"
27 #include <string.h>
28 
34 static void ascon_kmaca_init_precomputed(ascon_xofa_state_t *state)
35 {
36 #if defined(ASCON_BACKEND_SLICED64)
37  static uint64_t const iv[5] = {
38  0x47d45e034222e472ULL, 0xed0da2bb5580c30aULL,
39  0xedceed89ce04c765ULL, 0xffe052a5533eaa30ULL,
40  0xc8be4956f967f91aULL
41  };
42  memcpy(state->state.S, iv, sizeof(iv));
43 #elif defined(ASCON_BACKEND_SLICED32)
44  static uint32_t const iv[10] = {
45  0xbee180ac, 0x183115c5, 0xb305f090, 0xe2df0893,
46  0xbab1a2bb, 0xebeab094, 0xf8c3d604, 0xfc1c17f4,
47  0x869edbd4, 0xaf21e5e3
48  };
49  memcpy(state->state.W, iv, sizeof(iv));
50 #else
51  static uint8_t const iv[40] = {
52  0x47, 0xd4, 0x5e, 0x03, 0x42, 0x22, 0xe4, 0x72,
53  0xed, 0x0d, 0xa2, 0xbb, 0x55, 0x80, 0xc3, 0x0a,
54  0xed, 0xce, 0xed, 0x89, 0xce, 0x04, 0xc7, 0x65,
55  0xff, 0xe0, 0x52, 0xa5, 0x53, 0x3e, 0xaa, 0x30,
56  0xc8, 0xbe, 0x49, 0x56, 0xf9, 0x67, 0xf9, 0x1a
57  };
58 #if defined(ASCON_BACKEND_DIRECT_XOR)
59  memcpy(state->state.B, iv, sizeof(iv));
60 #else
61  ascon_init(&(state->state));
62  ascon_overwrite_bytes(&(state->state), iv, sizeof(iv));
63  ascon_release(&(state->state));
64 #endif
65 #endif
66  state->count = 0;
67  state->mode = 0;
68 }
69 
71  (const unsigned char *key, size_t keylen,
72  const unsigned char *in, size_t inlen,
73  const unsigned char *custom, size_t customlen,
74  unsigned char *out, size_t outlen)
75 {
77  ascon_kmaca_init(&state, key, keylen, custom, customlen, outlen);
78  ascon_xofa_absorb(&(state.xof), in, inlen);
79  ascon_xofa_squeeze(&(state.xof), out, outlen);
81 }
82 
84  (ascon_kmaca_state_t *state, const unsigned char *key, size_t keylen,
85  const unsigned char *custom, size_t customlen, size_t outlen)
86 {
87  if (outlen == ASCON_KMACA_SIZE) {
88  ascon_kmaca_init_precomputed(&(state->xof));
89  ascon_xofa_absorb_custom(&(state->xof), custom, customlen);
90  } else {
91  ascon_xofa_init_custom(&(state->xof), "KMAC", custom, customlen, outlen);
92  }
93  ascon_xofa_absorb(&(state->xof), key, keylen);
94 }
95 
97  (ascon_kmaca_state_t *state, const unsigned char *key, size_t keylen,
98  const unsigned char *custom, size_t customlen, size_t outlen)
99 {
101  ascon_kmaca_init(state, key, keylen, custom, customlen, outlen);
102 }
103 
105 {
106  if (state)
107  ascon_xofa_free(&(state->xof));
108 }
109 
111  (ascon_kmaca_state_t *state, const unsigned char *in, size_t inlen)
112 {
113  ascon_xofa_absorb(&(state->xof), in, inlen);
114 }
115 
117  (ascon_kmaca_state_t *state, unsigned char *out, size_t outlen)
118 {
119  ascon_xofa_squeeze(&(state->xof), out, outlen);
120 }
void ascon_kmaca_absorb(ascon_kmaca_state_t *state, const unsigned char *in, size_t inlen)
Absorbs more input data into an incremental ASCON-KMACA state.
Definition: ascon-kmaca.c:111
void ascon_kmaca_init(ascon_kmaca_state_t *state, const unsigned char *key, size_t keylen, const unsigned char *custom, size_t customlen, size_t outlen)
Initializes an incremental KMAC state using ASCON-XOFA.
Definition: ascon-kmaca.c:84
void ascon_kmaca(const unsigned char *key, size_t keylen, const unsigned char *in, size_t inlen, const unsigned char *custom, size_t customlen, unsigned char *out, size_t outlen)
Computes a KMAC value using ASCON-XOFA.
Definition: ascon-kmaca.c:71
void ascon_kmaca_free(ascon_kmaca_state_t *state)
Frees the ASCON-KMACA state and destroys any sensitive material.
Definition: ascon-kmaca.c:104
void ascon_kmaca_squeeze(ascon_kmaca_state_t *state, unsigned char *out, size_t outlen)
Squeezes output data from an incremental ASCON-KMACA state.
Definition: ascon-kmaca.c:117
void ascon_kmaca_reinit(ascon_kmaca_state_t *state, const unsigned char *key, size_t keylen, const unsigned char *custom, size_t customlen, size_t outlen)
Re-initializes an incremental KMAC state using ASCON-XOFA.
Definition: ascon-kmaca.c:97
void ascon_xofa_absorb_custom(ascon_xofa_state_t *state, const unsigned char *custom, size_t customlen)
Absorbs a customization string into an ASCON-XOFA state.
Definition: ascon-xofa.c:130
Keyed Message Authentication Code (KMAC) based on ASCON-XOF.
#define ASCON_KMACA_SIZE
Default size of the output for ASCON-KMACA.
Definition: kmac.h:58
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_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 ASCON-KMACA incremental mode.
Definition: kmac.h:73
State information for ASCON-XOFA incremental mode.
Definition: xof.h:72
uint32_t W[10]
Definition: permutation.h:65
uint64_t S[5]
Definition: permutation.h:64
uint8_t B[40]
Definition: permutation.h:66
System utilities of use to applications that use ASCON.
void ascon_xofa_squeeze(ascon_xofa_state_t *state, unsigned char *out, size_t outlen)
Squeezes output data from an ASCON-XOFA state.
Definition: ascon-xofa.c:277
void ascon_xofa_absorb(ascon_xofa_state_t *state, const unsigned char *in, size_t inlen)
Absorbs more input data into an ASCON-XOFA state.
Definition: ascon-xofa.c:227
void ascon_xofa_init_custom(ascon_xofa_state_t *state, const char *function_name, const unsigned char *custom, size_t customlen, size_t outlen)
Initializes the state for an incremental ASCON-XOFA operation, with a named function,...
Definition: ascon-xofa.c:144
void ascon_xofa_free(ascon_xofa_state_t *state)
Frees the ASCON-XOFA state and destroys any sensitive material.
Definition: ascon-xofa.c:216