ASCON Suite
ascon-hasha.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/hash.h>
24 #include "core/ascon-util-snp.h"
25 #include <string.h>
26 
27 void ascon_hasha(unsigned char *out, const unsigned char *in, size_t inlen)
28 {
31  ascon_xofa_absorb(&(state.xof), in, inlen);
33  ascon_xofa_free(&(state.xof));
34 }
35 
37 {
38  /* IV for ASCON-HASHA after processing it with the permutation */
39 #if defined(ASCON_BACKEND_SLICED64)
40  static uint64_t const iv[5] = {
41  0x01470194fc6528a6ULL, 0x738ec38ac0adffa7ULL,
42  0x2ec8e3296c76384cULL, 0xd6f6a54d7f52377dULL,
43  0xa13c42a223be8d87ULL
44  };
45  memcpy(state->xof.state.S, iv, sizeof(iv));
46 #elif defined(ASCON_BACKEND_SLICED32)
47  static uint32_t const iv[10] = {
48  0x1b16eb02, 0x0108e46d, 0xd29083f3, 0x5b9b8efd,
49  0x2891ae4a, 0x7ad66562, 0xee3bfc7f, 0x9dc27156,
50  0x16801633, 0xc61d5fa9
51  };
52  memcpy(state->xof.state.W, iv, sizeof(iv));
53 #else
54  static uint8_t const iv[40] = {
55  0x01, 0x47, 0x01, 0x94, 0xfc, 0x65, 0x28, 0xa6,
56  0x73, 0x8e, 0xc3, 0x8a, 0xc0, 0xad, 0xff, 0xa7,
57  0x2e, 0xc8, 0xe3, 0x29, 0x6c, 0x76, 0x38, 0x4c,
58  0xd6, 0xf6, 0xa5, 0x4d, 0x7f, 0x52, 0x37, 0x7d,
59  0xa1, 0x3c, 0x42, 0xa2, 0x23, 0xbe, 0x8d, 0x87
60  };
61 #if defined(ASCON_BACKEND_DIRECT_XOR)
62  memcpy(state->xof.state.B, iv, sizeof(iv));
63 #else
64  ascon_init(&(state->xof.state));
65  ascon_overwrite_bytes(&(state->xof.state), iv, sizeof(iv));
66  ascon_release(&(state->xof.state));
67 #endif
68 #endif
69  state->xof.count = 0;
70  state->xof.mode = 0;
71 }
72 
74 {
75 #if defined(ASCON_BACKEND_SLICED64) || defined(ASCON_BACKEND_SLICED32) || \
76  defined(ASCON_BACKEND_DIRECT_XOR)
78 #else
81 #endif
82 }
83 
85 {
86  ascon_xofa_free(&(state->xof));
87 }
88 
90  (ascon_hasha_state_t *state, const unsigned char *in, size_t inlen)
91 {
92  ascon_xofa_absorb(&(state->xof), in, inlen);
93 }
94 
96 {
98 }
99 
101 {
102  ascon_xofa_copy(&(dest->xof), &(src->xof));
103 }
void ascon_hasha_finalize(ascon_hasha_state_t *state, unsigned char *out)
Returns the final hash value from an ASCON-HASHA hashing operation.
Definition: ascon-hasha.c:95
void ascon_hasha_reinit(ascon_hasha_state_t *state)
Re-initializes the state for an ASCON-HASHA hashing operation.
Definition: ascon-hasha.c:73
void ascon_hasha(unsigned char *out, const unsigned char *in, size_t inlen)
Hashes a block of input data with ASCON-HASHA.
Definition: ascon-hasha.c:27
void ascon_hasha_init(ascon_hasha_state_t *state)
Initializes the state for an ASCON-HASHA hashing operation.
Definition: ascon-hasha.c:36
void ascon_hasha_free(ascon_hasha_state_t *state)
Frees the ASCON-HASHA state and destroys any sensitive material.
Definition: ascon-hasha.c:84
void ascon_hasha_update(ascon_hasha_state_t *state, const unsigned char *in, size_t inlen)
Updates an ASCON-HASHA state with more input data.
Definition: ascon-hasha.c:90
void ascon_hasha_copy(ascon_hasha_state_t *dest, const ascon_hasha_state_t *src)
Clones a copy of an ASCON-HASHA state.
Definition: ascon-hasha.c:100
ASCON-HASH and ASCON-HASHA hash algorithms.
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-HASHA incremental mode.
Definition: hash.h:52
ascon_xofa_state_t xof
Definition: hash.h:53
uint32_t W[10]
Definition: permutation.h:65
uint64_t S[5]
Definition: permutation.h:64
uint8_t B[40]
Definition: permutation.h:66
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
#define ASCON_HASH_SIZE
Size of the hash output for ASCON-HASH and the default hash output size for ASCON-XOF.
Definition: xof.h:43
void ascon_xofa_copy(ascon_xofa_state_t *dest, const ascon_xofa_state_t *src)
Clones a copy of an ASCON-XOFA state.
Definition: ascon-xofa.c:343
void ascon_xofa_free(ascon_xofa_state_t *state)
Frees the ASCON-XOFA state and destroys any sensitive material.
Definition: ascon-xofa.c:216