Leancrypto
0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
aead
api
lc_kmac_crypt.h
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2022 - 2024, 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_KMAC_CRYPT_H
21
#define LC_KMAC_CRYPT_H
22
23
#include "ext_headers.h"
24
#include "
lc_aead.h
"
25
#include "
lc_memory_support.h
"
26
27
/*
28
* This is the KMAC crypt cipher operation using the KMAC output as keystream
29
*/
30
#include "
lc_kmac.h
"
31
#include "
lc_memset_secure.h
"
32
33
#ifdef __cplusplus
34
extern
"C"
{
35
#endif
36
38
struct
lc_kc_cryptor {
39
struct
lc_kmac_ctx kmac;
40
struct
lc_kmac_ctx auth_ctx;
41
size_t
keystream_ptr;
42
uint8_t *keystream;
43
};
44
45
/*
46
* The block size of the algorithm for generating the key stream. It must be
47
* a multiple of the cSHAKE block size.
48
*/
49
#define LC_KC_KEYSTREAM_BLOCK LC_SHA3_256_SIZE_BLOCK
50
51
#define LC_KMAC_CRYPT_ALIGNMENT LC_XOR_ALIGNMENT(LC_HASH_COMMON_ALIGNMENT)
52
53
#define LC_ALIGN_KMAC_CRYPT_MASK(p) \
54
LC_ALIGN_PTR_8(p, LC_ALIGNMENT_MASK(LC_KMAC_CRYPT_ALIGNMENT))
55
56
/*
57
* One block LC_KMAC_CRYPT_ALIGNMENT is required to ensure the
58
* ->keystream pointer is aligned
59
*/
60
#define LC_KC_STATE_SIZE(x) \
61
(LC_KMAC_STATE_SIZE(x) + LC_KMAC_STATE_SIZE_REINIT(x) + \
62
LC_KC_KEYSTREAM_BLOCK + LC_KMAC_CRYPT_ALIGNMENT)
63
#define LC_KC_CTX_SIZE(x) \
64
(sizeof(struct lc_aead) + sizeof(struct lc_kc_cryptor) + \
65
LC_KC_STATE_SIZE(x))
66
67
/* KMAC-based AEAD-algorithm */
68
extern
const
struct
lc_aead *lc_kmac_aead;
69
70
/* Ensure that ->keystream is aligned to XOR alignment requirement */
71
#define _LC_KC_SET_CTX(name, hashname) \
72
_LC_KMAC_SET_CTX((&name->kmac), hashname, name, \
73
(sizeof(struct lc_kc_cryptor))); \
74
_LC_KMAC_SET_CTX_REINIT((&name->auth_ctx), hashname, name, \
75
(sizeof(struct lc_kc_cryptor) + \
76
LC_KMAC_STATE_SIZE(hashname))); \
77
name->keystream = LC_ALIGN_KMAC_CRYPT_MASK( \
78
(uint8_t *)((uint8_t *)name + \
79
(sizeof(struct lc_kc_cryptor) + \
80
LC_KMAC_STATE_SIZE(hashname) + \
81
LC_KMAC_STATE_SIZE_REINIT(hashname))))
82
83
#define LC_KC_SET_CTX(name, hashname) \
84
LC_AEAD_CTX(name, lc_kmac_aead); \
85
_LC_KC_SET_CTX(((struct lc_kc_cryptor *)name->aead_state), hashname)
87
99
int
lc_kc_alloc
(
const
struct
lc_hash
*hash,
struct
lc_aead_ctx **ctx);
100
110
#define LC_KC_CTX_ON_STACK(name, hash) \
111
_Pragma("GCC diagnostic push") \
112
_Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
113
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
114
LC_ALIGNED_BUFFER(name##_ctx_buf, \
115
LC_KC_CTX_SIZE(hash), \
116
LC_KMAC_CRYPT_ALIGNMENT); \
117
struct lc_aead_ctx *name = (struct lc_aead_ctx *)name##_ctx_buf; \
118
LC_KC_SET_CTX(name, hash); \
119
_Pragma("GCC diagnostic pop")
120
/* invocation of lc_kc_zero(name); not needed */
121
122
#ifdef __cplusplus
123
}
124
#endif
125
126
#endif
/* LC_KMAC_CRYPT_H */
lc_hash
void lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
lc_aead.h
lc_kmac.h
lc_kc_alloc
int lc_kc_alloc(const struct lc_hash *hash, struct lc_aead_ctx **ctx)
Allocate KMAC cryptor context on heap.
lc_memory_support.h
lc_memset_secure.h
Generated by
1.11.0