Leancrypto 1.6.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_ascon_aead.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 - 2025, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20#ifndef LC_ASCON_AEAD_H
21#define LC_ASCON_AEAD_H
22
23#include "lc_aead.h"
24#include "lc_hash.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
31#define LC_ASCON_MAX_KEYSIZE 64
32
33struct lc_ascon_cryptor {
34 uint8_t key[LC_ASCON_MAX_KEYSIZE];
35 uint8_t keylen;
36 uint8_t rate_offset;
37 uint8_t statesize;
38 uint8_t roundb;
39 uint8_t taglen;
40 const struct lc_hash *hash;
41 uint64_t *state;
42};
43
44#define LC_ASCON_ALIGNMENT LC_XOR_ALIGNMENT(LC_HASH_COMMON_ALIGNMENT)
45
46#define _LC_ASCON_SET_CTX(name, hashname, ctx, offset) \
47 name->state = LC_ALIGN_HASH_MASK(((uint8_t *)(ctx)) + (offset)); \
48 name->hash = hashname
49
50#define LC_ASCON_SET_CTX(name, hashname, ascon_impl) \
51 LC_AEAD_HASH_ALIGN_CTX(name, ascon_impl); \
52 _LC_ASCON_SET_CTX(((struct lc_ascon_cryptor *)name->aead_state), \
53 hashname, \
54 ((struct lc_ascon_cryptor *)name->aead_state), \
55 (sizeof(struct lc_ascon_cryptor)))
56
57static inline int lc_ascon_load_key(struct lc_ascon_cryptor *ascon,
58 const uint8_t *key, size_t keylen)
59{
60 if (ascon && keylen < LC_ASCON_MAX_KEYSIZE) {
61 memcpy(ascon->key, key, keylen);
62 ascon->keylen = (uint8_t)keylen;
63 return 0;
64 }
65 return -EINVAL;
66}
67
68/*
69 * This function adds the padding byte with which the AAD as well as the
70 * plaintext is appended with.
71 */
72static inline void lc_ascon_add_padbyte(struct lc_ascon_cryptor *ascon,
73 size_t offset)
74{
75 const struct lc_hash *hash = ascon->hash;
76 /* Rationale for pad byte: see ascon_squeeze_common */
77 static const uint8_t pad_data = 0x01;
78
79 /*
80 * The data was exactly a multiple of the rate -> permute before adding
81 * the padding byte.
82 */
83 if (offset == hash->sponge_rate)
84 offset = 0;
85
86 lc_sponge_add_bytes(hash, ascon->state, &pad_data, offset, 1);
87}
89
90#ifdef __cplusplus
91}
92#endif
93
94#endif /* LC_ASCON_AEAD_H */
int lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
int lc_sponge_add_bytes(const struct lc_hash *hash, void *state, const uint8_t *data, size_t offset, size_t length)
Function to add (in GF(2), using bitwise exclusive-or) data given as bytes into the sponge state.