ASCON Suite
ascon-kmac.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_kmac_init_precomputed(ascon_xof_state_t *state)
35 {
36 #if defined(ASCON_BACKEND_SLICED64)
37  static uint64_t const iv[5] = {
38  0x7a09132495dfa176ULL, 0x1b19e04f31cc4caeULL,
39  0x64ba72afaa61d2b1ULL, 0xd2964e09a5169084ULL,
40  0x05bc6c865abe514bULL
41  };
42  memcpy(state->state.S, iv, sizeof(iv));
43 #elif defined(ASCON_BACKEND_SLICED32)
44  static uint32_t const iv[10] = {
45  0xc1527f1e, 0x72148bc5, 0x558b5aa2, 0x32c34a2f,
46  0xa4c309c5, 0x4f5ff49c, 0xc6a13642, 0x9932c188,
47  0x36a2c6d9, 0x0e693f03
48  };
49  memcpy(state->state.W, iv, sizeof(iv));
50 #else
51  static uint8_t const iv[40] = {
52  0x7a, 0x09, 0x13, 0x24, 0x95, 0xdf, 0xa1, 0x76,
53  0x1b, 0x19, 0xe0, 0x4f, 0x31, 0xcc, 0x4c, 0xae,
54  0x64, 0xba, 0x72, 0xaf, 0xaa, 0x61, 0xd2, 0xb1,
55  0xd2, 0x96, 0x4e, 0x09, 0xa5, 0x16, 0x90, 0x84,
56  0x05, 0xbc, 0x6c, 0x86, 0x5a, 0xbe, 0x51, 0x4b
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_kmac_init(&state, key, keylen, custom, customlen, outlen);
78  ascon_xof_absorb(&(state.xof), in, inlen);
79  ascon_xof_squeeze(&(state.xof), out, outlen);
81 }
82 
84  (ascon_kmac_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_KMAC_SIZE) {
88  ascon_kmac_init_precomputed(&(state->xof));
89  ascon_xof_absorb_custom(&(state->xof), custom, customlen);
90  } else {
91  ascon_xof_init_custom(&(state->xof), "KMAC", custom, customlen, outlen);
92  }
93  ascon_xof_absorb(&(state->xof), key, keylen);
94 }
95 
97  (ascon_kmac_state_t *state, const unsigned char *key, size_t keylen,
98  const unsigned char *custom, size_t customlen, size_t outlen)
99 {
101  ascon_kmac_init(state, key, keylen, custom, customlen, outlen);
102 }
103 
105 {
106  if (state)
107  ascon_xof_free(&(state->xof));
108 }
109 
111  (ascon_kmac_state_t *state, const unsigned char *in, size_t inlen)
112 {
113  ascon_xof_absorb(&(state->xof), in, inlen);
114 }
115 
117  (ascon_kmac_state_t *state, unsigned char *out, size_t outlen)
118 {
119  ascon_xof_squeeze(&(state->xof), out, outlen);
120 }
void ascon_kmac_reinit(ascon_kmac_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-XOF.
Definition: ascon-kmac.c:97
void ascon_kmac_absorb(ascon_kmac_state_t *state, const unsigned char *in, size_t inlen)
Absorbs more input data into an incremental ASCON-KMAC state.
Definition: ascon-kmac.c:111
void ascon_kmac_free(ascon_kmac_state_t *state)
Frees the ASCON-KMAC state and destroys any sensitive material.
Definition: ascon-kmac.c:104
void ascon_kmac_squeeze(ascon_kmac_state_t *state, unsigned char *out, size_t outlen)
Squeezes output data from an incremental ASCON-KMAC state.
Definition: ascon-kmac.c:117
void ascon_kmac_init(ascon_kmac_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-XOF.
Definition: ascon-kmac.c:84
void ascon_kmac(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-XOF.
Definition: ascon-kmac.c:71
void ascon_xof_absorb_custom(ascon_xof_state_t *state, const unsigned char *custom, size_t customlen)
Absorbs a customization string into an ASCON-XOF state.
Definition: ascon-xof.c:132
Keyed Message Authentication Code (KMAC) based on ASCON-XOF.
#define ASCON_KMAC_SIZE
Default size of the output for ASCON-KMAC.
Definition: kmac.h:53
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-KMAC incremental mode.
Definition: kmac.h:64
State information for ASCON-XOF incremental mode.
Definition: xof.h:61
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_xof_free(ascon_xof_state_t *state)
Frees the ASCON-XOF state and destroys any sensitive material.
Definition: ascon-xof.c:218
void ascon_xof_absorb(ascon_xof_state_t *state, const unsigned char *in, size_t inlen)
Absorbs more input data into an ASCON-XOF state.
Definition: ascon-xof.c:229
void ascon_xof_init_custom(ascon_xof_state_t *state, const char *function_name, const unsigned char *custom, size_t customlen, size_t outlen)
Initializes the state for an incremental ASCON-XOF operation, with a named function,...
Definition: ascon-xof.c:146
void ascon_xof_squeeze(ascon_xof_state_t *state, unsigned char *out, size_t outlen)
Squeezes output data from an ASCON-XOF state.
Definition: ascon-xof.c:279