1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans * Copyright (C) 2021 - This file is part of libecc project
3*f0865ec9SKyle Evans *
4*f0865ec9SKyle Evans * Authors:
5*f0865ec9SKyle Evans * Ryad BENADJILA <ryadbenadjila@gmail.com>
6*f0865ec9SKyle Evans * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7*f0865ec9SKyle Evans *
8*f0865ec9SKyle Evans * This software is licensed under a dual BSD and GPL v2 license.
9*f0865ec9SKyle Evans * See LICENSE file at the root folder of the project.
10*f0865ec9SKyle Evans */
11*f0865ec9SKyle Evans #include <libecc/lib_ecc_config.h>
12*f0865ec9SKyle Evans #ifdef WITH_HASH_SHAKE256
13*f0865ec9SKyle Evans
14*f0865ec9SKyle Evans #include <libecc/hash/shake256.h>
15*f0865ec9SKyle Evans
shake256_init(shake256_context * ctx)16*f0865ec9SKyle Evans int shake256_init(shake256_context *ctx)
17*f0865ec9SKyle Evans {
18*f0865ec9SKyle Evans int ret;
19*f0865ec9SKyle Evans
20*f0865ec9SKyle Evans ret = _shake_init(ctx, SHAKE256_DIGEST_SIZE, SHAKE256_BLOCK_SIZE); EG(ret, err);
21*f0865ec9SKyle Evans
22*f0865ec9SKyle Evans /* Tell that we are initialized */
23*f0865ec9SKyle Evans ctx->magic = SHAKE256_HASH_MAGIC;
24*f0865ec9SKyle Evans
25*f0865ec9SKyle Evans err:
26*f0865ec9SKyle Evans return ret;
27*f0865ec9SKyle Evans }
28*f0865ec9SKyle Evans
shake256_update(shake256_context * ctx,const u8 * input,u32 ilen)29*f0865ec9SKyle Evans int shake256_update(shake256_context *ctx, const u8 *input, u32 ilen)
30*f0865ec9SKyle Evans {
31*f0865ec9SKyle Evans int ret;
32*f0865ec9SKyle Evans
33*f0865ec9SKyle Evans SHAKE256_HASH_CHECK_INITIALIZED(ctx, ret, err);
34*f0865ec9SKyle Evans
35*f0865ec9SKyle Evans ret = _shake_update((shake_context *)ctx, input, ilen);
36*f0865ec9SKyle Evans
37*f0865ec9SKyle Evans err:
38*f0865ec9SKyle Evans return ret;
39*f0865ec9SKyle Evans }
40*f0865ec9SKyle Evans
shake256_final(shake256_context * ctx,u8 output[SHAKE256_DIGEST_SIZE])41*f0865ec9SKyle Evans int shake256_final(shake256_context *ctx, u8 output[SHAKE256_DIGEST_SIZE])
42*f0865ec9SKyle Evans {
43*f0865ec9SKyle Evans int ret;
44*f0865ec9SKyle Evans
45*f0865ec9SKyle Evans SHAKE256_HASH_CHECK_INITIALIZED(ctx, ret, err);
46*f0865ec9SKyle Evans
47*f0865ec9SKyle Evans ret = _shake_finalize((shake_context *)ctx, output);
48*f0865ec9SKyle Evans
49*f0865ec9SKyle Evans /* Tell that we are uninitialized */
50*f0865ec9SKyle Evans ctx->magic = WORD(0);
51*f0865ec9SKyle Evans
52*f0865ec9SKyle Evans err:
53*f0865ec9SKyle Evans return ret;
54*f0865ec9SKyle Evans }
55*f0865ec9SKyle Evans
shake256_scattered(const u8 ** inputs,const u32 * ilens,u8 output[SHAKE256_DIGEST_SIZE])56*f0865ec9SKyle Evans int shake256_scattered(const u8 **inputs, const u32 *ilens,
57*f0865ec9SKyle Evans u8 output[SHAKE256_DIGEST_SIZE])
58*f0865ec9SKyle Evans {
59*f0865ec9SKyle Evans shake256_context ctx;
60*f0865ec9SKyle Evans int pos = 0, ret;
61*f0865ec9SKyle Evans
62*f0865ec9SKyle Evans MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
63*f0865ec9SKyle Evans
64*f0865ec9SKyle Evans ret = shake256_init(&ctx); EG(ret, err);
65*f0865ec9SKyle Evans
66*f0865ec9SKyle Evans while (inputs[pos] != NULL) {
67*f0865ec9SKyle Evans ret = shake256_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
68*f0865ec9SKyle Evans pos += 1;
69*f0865ec9SKyle Evans }
70*f0865ec9SKyle Evans
71*f0865ec9SKyle Evans ret = shake256_final(&ctx, output);
72*f0865ec9SKyle Evans
73*f0865ec9SKyle Evans err:
74*f0865ec9SKyle Evans return ret;
75*f0865ec9SKyle Evans }
76*f0865ec9SKyle Evans
shake256(const u8 * input,u32 ilen,u8 output[SHAKE256_DIGEST_SIZE])77*f0865ec9SKyle Evans int shake256(const u8 *input, u32 ilen, u8 output[SHAKE256_DIGEST_SIZE])
78*f0865ec9SKyle Evans {
79*f0865ec9SKyle Evans int ret;
80*f0865ec9SKyle Evans shake256_context ctx;
81*f0865ec9SKyle Evans
82*f0865ec9SKyle Evans ret = shake256_init(&ctx); EG(ret, err);
83*f0865ec9SKyle Evans ret = shake256_update(&ctx, input, ilen); EG(ret, err);
84*f0865ec9SKyle Evans ret = shake256_final(&ctx, output);
85*f0865ec9SKyle Evans
86*f0865ec9SKyle Evans err:
87*f0865ec9SKyle Evans return ret;
88*f0865ec9SKyle Evans }
89*f0865ec9SKyle Evans
90*f0865ec9SKyle Evans #else /* WITH_HASH_SHAKE256 */
91*f0865ec9SKyle Evans
92*f0865ec9SKyle Evans /*
93*f0865ec9SKyle Evans * Dummy definition to avoid the empty translation unit ISO C warning
94*f0865ec9SKyle Evans */
95*f0865ec9SKyle Evans typedef int dummy;
96*f0865ec9SKyle Evans #endif /* WITH_HASH_SHAKE256 */
97