Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_aead.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_AEAD_H
21#define LC_AEAD_H
22
23#include "lc_memory_support.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
30struct lc_aead {
31 int (*setkey)(void *state, const uint8_t *key, const size_t keylen,
32 const uint8_t *iv, size_t ivlen);
33 void (*encrypt)(void *state, const uint8_t *plaintext,
34 uint8_t *ciphertext, size_t datalen, const uint8_t *aad,
35 size_t aadlen, uint8_t *tag, size_t taglen);
36 void (*enc_init)(void *state, const uint8_t *aad, size_t aadlen);
37 void (*enc_update)(void *state, const uint8_t *plaintext,
38 uint8_t *ciphertext, size_t datalen);
39 void (*enc_final)(void *state, uint8_t *tag, size_t taglen);
40 int (*decrypt)(void *state, const uint8_t *ciphertext,
41 uint8_t *plaintext, size_t datalen, const uint8_t *aad,
42 size_t aadlen, const uint8_t *tag, size_t taglen);
43 void (*dec_init)(void *state, const uint8_t *aad, size_t aadlen);
44 void (*dec_update)(void *state, const uint8_t *ciphertext,
45 uint8_t *plaintext, size_t datalen);
46 int (*dec_final)(void *state, const uint8_t *tag, size_t taglen);
47 void (*zero)(void *state);
48};
49
50struct lc_aead_ctx {
51 const struct lc_aead *aead;
52 void *aead_state;
53};
54
55#define LC_AEAD_CTX(name, cb) \
56 name->aead = cb; \
57 name->aead_state = (uint8_t *)(name) + sizeof(struct lc_aead_ctx)
58
59#define LC_AEAD_HASH_ALIGN_CTX(name, cb) \
60 name->aead = cb; \
61 name->aead_state = LC_ALIGN_HASH_MASK((uint8_t *)(name) + \
62 sizeof(struct lc_aead_ctx))
64
86static inline void lc_aead_zero(struct lc_aead_ctx *ctx)
87{
88 const struct lc_aead *aead;
89 void *aead_state;
90
91 if (!ctx)
92 return;
93
94 aead = ctx->aead;
95 aead_state = ctx->aead_state;
96
97 if (!aead || !aead_state)
98 return;
99
100 aead->zero(aead_state);
101}
102
109static inline void lc_aead_zero_free(struct lc_aead_ctx *ctx)
110{
111 if (!ctx)
112 return;
113
114 lc_aead_zero(ctx);
115 lc_free(ctx);
116}
117
133static inline int lc_aead_setkey(struct lc_aead_ctx *ctx, const uint8_t *key,
134 const size_t keylen, const uint8_t *iv,
135 size_t ivlen)
136{
137 const struct lc_aead *aead;
138 void *aead_state;
139
140 if (!ctx)
141 return -EINVAL;
142
143 aead = ctx->aead;
144 aead_state = ctx->aead_state;
145
146 if (!aead || !aead_state)
147 return -EINVAL;
148
149 return aead->setkey(aead_state, key, keylen, iv, ivlen);
150}
151
173static inline int lc_aead_encrypt(struct lc_aead_ctx *ctx,
174 const uint8_t *plaintext, uint8_t *ciphertext,
175 size_t datalen, const uint8_t *aad,
176 size_t aadlen, uint8_t *tag, size_t taglen)
177{
178 const struct lc_aead *aead;
179 void *aead_state;
180
181 if (!ctx)
182 return -EINVAL;
183
184 aead = ctx->aead;
185 aead_state = ctx->aead_state;
186
187 if (!aead || !aead_state || !aead->encrypt)
188 return -EOPNOTSUPP;
189
190 aead->encrypt(aead_state, plaintext, ciphertext, datalen, aad, aadlen,
191 tag, taglen);
192
193 return 0;
194}
195
210static inline int lc_aead_enc_init(struct lc_aead_ctx *ctx, const uint8_t *aad,
211 size_t aadlen)
212{
213 const struct lc_aead *aead;
214 void *aead_state;
215
216 if (!ctx)
217 return -EINVAL;
218
219 aead = ctx->aead;
220 aead_state = ctx->aead_state;
221
222 if (!aead || !aead_state || !aead->enc_init)
223 return -EOPNOTSUPP;
224
225 aead->enc_init(aead_state, aad, aadlen);
226
227 return 0;
228}
229
246static inline int lc_aead_enc_update(struct lc_aead_ctx *ctx,
247 const uint8_t *plaintext,
248 uint8_t *ciphertext, size_t datalen)
249{
250 const struct lc_aead *aead;
251 void *aead_state;
252
253 if (!ctx)
254 return -EINVAL;
255
256 aead = ctx->aead;
257 aead_state = ctx->aead_state;
258
259 if (!aead || !aead_state || !aead->enc_update)
260 return -EOPNOTSUPP;
261
262 aead->enc_update(aead_state, plaintext, ciphertext, datalen);
263
264 return 0;
265}
266
280static inline int lc_aead_enc_final(struct lc_aead_ctx *ctx, uint8_t *tag,
281 size_t taglen)
282{
283 const struct lc_aead *aead;
284 void *aead_state;
285
286 if (!ctx)
287 return -EINVAL;
288
289 aead = ctx->aead;
290 aead_state = ctx->aead_state;
291
292 if (!aead || !aead_state || !aead->enc_final)
293 return -EOPNOTSUPP;
294
295 aead->enc_final(aead_state, tag, taglen);
296
297 return 0;
298}
299
321static inline int lc_aead_decrypt(struct lc_aead_ctx *ctx,
322 const uint8_t *ciphertext, uint8_t *plaintext,
323 size_t datalen, const uint8_t *aad,
324 size_t aadlen, const uint8_t *tag,
325 size_t taglen)
326{
327 const struct lc_aead *aead;
328 void *aead_state;
329
330 if (!ctx)
331 return -EINVAL;
332
333 aead = ctx->aead;
334 aead_state = ctx->aead_state;
335
336 if (!aead || !aead_state || !aead->decrypt)
337 return -EOPNOTSUPP;
338
339 return aead->decrypt(aead_state, ciphertext, plaintext, datalen, aad,
340 aadlen, tag, taglen);
341}
342
357static inline int lc_aead_dec_init(struct lc_aead_ctx *ctx, const uint8_t *aad,
358 size_t aadlen)
359{
360 const struct lc_aead *aead;
361 void *aead_state;
362
363 if (!ctx)
364 return -EINVAL;
365
366 aead = ctx->aead;
367 aead_state = ctx->aead_state;
368
369 if (!aead || !aead_state || !aead->dec_init)
370 return -EOPNOTSUPP;
371
372 aead->dec_init(aead_state, aad, aadlen);
373
374 return 0;
375}
376
393static inline int lc_aead_dec_update(struct lc_aead_ctx *ctx,
394 const uint8_t *ciphertext,
395 uint8_t *plaintext, size_t datalen)
396{
397 const struct lc_aead *aead;
398 void *aead_state;
399
400 if (!ctx)
401 return -EINVAL;
402
403 aead = ctx->aead;
404 aead_state = ctx->aead_state;
405
406 if (!aead || !aead_state || !aead->dec_update)
407 return -EOPNOTSUPP;
408
409 aead->dec_update(aead_state, ciphertext, plaintext, datalen);
410
411 return 0;
412}
413
426static inline int lc_aead_dec_final(struct lc_aead_ctx *ctx, const uint8_t *tag,
427 size_t taglen)
428{
429 const struct lc_aead *aead;
430 void *aead_state;
431
432 if (!ctx)
433 return -EINVAL;
434
435 aead = ctx->aead;
436 aead_state = ctx->aead_state;
437
438 if (!aead || !aead_state || !aead->dec_final)
439 return -EOPNOTSUPP;
440
441 return aead->dec_final(aead_state, tag, taglen);
442}
443
444#ifdef __cplusplus
445}
446#endif
447
448#endif /* LC_AEAD_H */
static int lc_aead_setkey(struct lc_aead_ctx *ctx, const uint8_t *key, const size_t keylen, const uint8_t *iv, size_t ivlen)
Set the key for the AEAD encyption or decryption operation.
Definition lc_aead.h:133
static int lc_aead_dec_update(struct lc_aead_ctx *ctx, const uint8_t *ciphertext, uint8_t *plaintext, size_t datalen)
AEAD-decrypt data - send partial data.
Definition lc_aead.h:393
static int lc_aead_dec_final(struct lc_aead_ctx *ctx, const uint8_t *tag, size_t taglen)
AEAD-decrypt data - Perform authentication.
Definition lc_aead.h:426
static int lc_aead_enc_init(struct lc_aead_ctx *ctx, const uint8_t *aad, size_t aadlen)
Initialize AEAD encryption.
Definition lc_aead.h:210
static int lc_aead_encrypt(struct lc_aead_ctx *ctx, const uint8_t *plaintext, uint8_t *ciphertext, size_t datalen, const uint8_t *aad, size_t aadlen, uint8_t *tag, size_t taglen)
AEAD-encrypt data in one call.
Definition lc_aead.h:173
static int lc_aead_enc_update(struct lc_aead_ctx *ctx, const uint8_t *plaintext, uint8_t *ciphertext, size_t datalen)
AEAD-encrypt data - send partial data.
Definition lc_aead.h:246
static int lc_aead_enc_final(struct lc_aead_ctx *ctx, uint8_t *tag, size_t taglen)
Complete AEAD encryption - Obtain the authentication tag from the encryption operation.
Definition lc_aead.h:280
static void lc_aead_zero_free(struct lc_aead_ctx *ctx)
Zeroize and free AEAD context.
Definition lc_aead.h:109
static void lc_aead_zero(struct lc_aead_ctx *ctx)
Zeroize AEAD context.
Definition lc_aead.h:86
static int lc_aead_decrypt(struct lc_aead_ctx *ctx, const uint8_t *ciphertext, uint8_t *plaintext, size_t datalen, const uint8_t *aad, size_t aadlen, const uint8_t *tag, size_t taglen)
AEAD-decrypt data in one call.
Definition lc_aead.h:321
static int lc_aead_dec_init(struct lc_aead_ctx *ctx, const uint8_t *aad, size_t aadlen)
Initialize AEAD decryption.
Definition lc_aead.h:357
void lc_free(void *ptr)
free the memory allocated with lc_alloc_aligned