ASCON Suite
ascon-hash.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_hash(unsigned char *out, const unsigned char *in, size_t inlen)
28 {
31  ascon_xof_absorb(&(state.xof), in, inlen);
33  ascon_xof_free(&(state.xof));
34 }
35 
37 {
38  /* IV for ASCON-HASH after processing it with the permutation */
39 #if defined(ASCON_BACKEND_SLICED64)
40  static uint64_t const iv[5] = {
41  0xee9398aadb67f03dULL, 0x8bb21831c60f1002ULL,
42  0xb48a92db98d5da62ULL, 0x43189921b8f8e3e8ULL,
43  0x348fa5c9d525e140ULL
44  };
45  memcpy(state->xof.state.S, iv, sizeof(iv));
46 #elif defined(ASCON_BACKEND_SLICED32)
47  static uint32_t const iv[10] = {
48  0xa540dbc7, 0xf9afb5c6, 0x1445a340, 0xbd249301,
49  0x604d4fc8, 0xcb9ba8b5, 0x94514c98, 0x12a4eede,
50  0x6339f398, 0x4bca84c0
51  };
52  memcpy(state->xof.state.W, iv, sizeof(iv));
53 #else
54  static uint8_t const iv[40] = {
55  0xee, 0x93, 0x98, 0xaa, 0xdb, 0x67, 0xf0, 0x3d,
56  0x8b, 0xb2, 0x18, 0x31, 0xc6, 0x0f, 0x10, 0x02,
57  0xb4, 0x8a, 0x92, 0xdb, 0x98, 0xd5, 0xda, 0x62,
58  0x43, 0x18, 0x99, 0x21, 0xb8, 0xf8, 0xe3, 0xe8,
59  0x34, 0x8f, 0xa5, 0xc9, 0xd5, 0x25, 0xe1, 0x40
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_xof_free(&(state->xof));
87 }
88 
90  (ascon_hash_state_t *state, const unsigned char *in, size_t inlen)
91 {
92  ascon_xof_absorb(&(state->xof), in, inlen);
93 }
94 
95 void ascon_hash_finalize(ascon_hash_state_t *state, unsigned char *out)
96 {
98 }
99 
101 {
102  ascon_xof_copy(&(dest->xof), &(src->xof));
103 }
void ascon_hash_init(ascon_hash_state_t *state)
Initializes the state for an ASCON-HASH hashing operation.
Definition: ascon-hash.c:36
void ascon_hash_update(ascon_hash_state_t *state, const unsigned char *in, size_t inlen)
Updates an ASCON-HASH state with more input data.
Definition: ascon-hash.c:90
void ascon_hash_finalize(ascon_hash_state_t *state, unsigned char *out)
Returns the final hash value from an ASCON-HASH hashing operation.
Definition: ascon-hash.c:95
void ascon_hash(unsigned char *out, const unsigned char *in, size_t inlen)
Hashes a block of input data with ASCON-HASH.
Definition: ascon-hash.c:27
void ascon_hash_free(ascon_hash_state_t *state)
Frees the ASCON-HASH state and destroys any sensitive material.
Definition: ascon-hash.c:84
void ascon_hash_copy(ascon_hash_state_t *dest, const ascon_hash_state_t *src)
Clones a copy of an ASCON-HASH state.
Definition: ascon-hash.c:100
void ascon_hash_reinit(ascon_hash_state_t *state)
Re-initializes the state for an ASCON-HASH hashing operation.
Definition: ascon-hash.c:73
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-HASH incremental mode.
Definition: hash.h:43
ascon_xof_state_t xof
Definition: hash.h:44
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_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
#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_xof_copy(ascon_xof_state_t *dest, const ascon_xof_state_t *src)
Clones a copy of an ASCON-XOF state.
Definition: ascon-xof.c:344
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