Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_hash_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_HASH_CRYPT_H
21#define LC_HASH_CRYPT_H
22
23#include "lc_aead.h"
24/*
25 * This is the hash crypt cipher operation using the Hash DRBG with SHA-512
26 * core as input.
27 */
28#include "lc_hash_drbg.h"
29#include "lc_hmac.h"
30#include "lc_memset_secure.h"
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
37/*
38 * The block size of the algorithm for generating the key stream. The min DRBG
39 * generate size is larger. This implies that there is no DRBG update operation
40 * while the key stream for one block is generated.
41 */
42#define LC_HC_KEYSTREAM_BLOCK 64
43
44struct lc_hc_cryptor {
45 struct lc_rng_ctx drbg;
46 uint8_t drbg_state[LC_DRBG_HASH_STATE_SIZE];
47 struct lc_hmac_ctx auth_ctx;
48 size_t keystream_ptr;
49 uint8_t keystream[LC_HC_KEYSTREAM_BLOCK];
50};
51
52#define LC_HC_CTX_SIZE(x) \
53 (sizeof(struct lc_aead) + sizeof(struct lc_hc_cryptor) + \
54 LC_HMAC_STATE_SIZE(x))
55
56/* Hash-based AEAD-algorithm */
57extern const struct lc_aead *lc_hash_aead;
58
59#define _LC_HC_SET_CTX(name, hashname) \
60 LC_DRBG_HASH_RNG_CTX((&name->drbg)); \
61 _LC_HMAC_SET_CTX((&name->auth_ctx), hashname, name, \
62 (sizeof(struct lc_hc_cryptor)))
63
64#define LC_HC_SET_CTX(name, hashname) \
65 LC_AEAD_CTX(name, lc_hash_aead); \
66 _LC_HC_SET_CTX(((struct lc_hc_cryptor *)name->aead_state), hashname)
68
76static inline size_t lc_hc_get_tagsize(struct lc_hc_cryptor *hc)
77{
78 struct lc_hmac_ctx *auth_ctx = &hc->auth_ctx;
79
80 return lc_hmac_macsize(auth_ctx);
81}
82
92int lc_hc_alloc(const struct lc_hash *hash, struct lc_aead_ctx **ctx);
93
101#define LC_HC_CTX_ON_STACK(name, hash) \
102 _Pragma("GCC diagnostic push") \
103 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
104 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
105 LC_ALIGNED_BUFFER(name##_ctx_buf, \
106 LC_HC_CTX_SIZE(hash), \
107 LC_HASH_COMMON_ALIGNMENT); \
108 struct lc_aead_ctx *name = (struct lc_aead_ctx *)name##_ctx_buf; \
109 LC_HC_SET_CTX(name, hash); \
110 lc_aead_zero(name); \
111 _Pragma("GCC diagnostic pop")
112
113#ifdef __cplusplus
114}
115#endif
116
117#endif /* LC_HASH_CRYPT_H */
static size_t lc_hmac_macsize(struct lc_hmac_ctx *hmac_ctx)
Return the MAC size.
Definition lc_hmac.h:185
void lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
static size_t lc_hc_get_tagsize(struct lc_hc_cryptor *hc)
Return maximum size of authentication tag.
int lc_hc_alloc(const struct lc_hash *hash, struct lc_aead_ctx **ctx)
Allocate Hash cryptor context on heap.