1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans * Copyright (C) 2017 - 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 * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8*f0865ec9SKyle Evans *
9*f0865ec9SKyle Evans * Contributors:
10*f0865ec9SKyle Evans * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11*f0865ec9SKyle Evans * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12*f0865ec9SKyle Evans *
13*f0865ec9SKyle Evans * This software is licensed under a dual BSD and GPL v2 license.
14*f0865ec9SKyle Evans * See LICENSE file at the root folder of the project.
15*f0865ec9SKyle Evans */
16*f0865ec9SKyle Evans #include <libecc/lib_ecc_config.h>
17*f0865ec9SKyle Evans #ifdef WITH_HASH_SHA3_512
18*f0865ec9SKyle Evans
19*f0865ec9SKyle Evans #include <libecc/hash/sha3-512.h>
20*f0865ec9SKyle Evans
21*f0865ec9SKyle Evans /* Init hash function. Returns 0 on success, -1 on error. */
sha3_512_init(sha3_512_context * ctx)22*f0865ec9SKyle Evans int sha3_512_init(sha3_512_context *ctx)
23*f0865ec9SKyle Evans {
24*f0865ec9SKyle Evans int ret;
25*f0865ec9SKyle Evans
26*f0865ec9SKyle Evans ret = _sha3_init(ctx, SHA3_512_DIGEST_SIZE); EG(ret, err);
27*f0865ec9SKyle Evans
28*f0865ec9SKyle Evans /* Tell that we are initialized */
29*f0865ec9SKyle Evans ctx->magic = SHA3_512_HASH_MAGIC;
30*f0865ec9SKyle Evans
31*f0865ec9SKyle Evans err:
32*f0865ec9SKyle Evans return ret;
33*f0865ec9SKyle Evans }
34*f0865ec9SKyle Evans
35*f0865ec9SKyle Evans /* Update hash function. Returns 0 on success, -1 on error. */
sha3_512_update(sha3_512_context * ctx,const u8 * input,u32 ilen)36*f0865ec9SKyle Evans int sha3_512_update(sha3_512_context *ctx, const u8 *input, u32 ilen)
37*f0865ec9SKyle Evans {
38*f0865ec9SKyle Evans int ret;
39*f0865ec9SKyle Evans
40*f0865ec9SKyle Evans SHA3_512_HASH_CHECK_INITIALIZED(ctx, ret, err);
41*f0865ec9SKyle Evans
42*f0865ec9SKyle Evans ret = _sha3_update((sha3_context *)ctx, input, ilen);
43*f0865ec9SKyle Evans
44*f0865ec9SKyle Evans err:
45*f0865ec9SKyle Evans return ret;
46*f0865ec9SKyle Evans }
47*f0865ec9SKyle Evans
48*f0865ec9SKyle Evans /* Finalize hash function. Returns 0 on success, -1 on error. */
sha3_512_final(sha3_512_context * ctx,u8 output[SHA3_512_DIGEST_SIZE])49*f0865ec9SKyle Evans int sha3_512_final(sha3_512_context *ctx, u8 output[SHA3_512_DIGEST_SIZE])
50*f0865ec9SKyle Evans {
51*f0865ec9SKyle Evans int ret;
52*f0865ec9SKyle Evans
53*f0865ec9SKyle Evans SHA3_512_HASH_CHECK_INITIALIZED(ctx, ret, err);
54*f0865ec9SKyle Evans
55*f0865ec9SKyle Evans ret = _sha3_finalize((sha3_context *)ctx, output); EG(ret, err);
56*f0865ec9SKyle Evans
57*f0865ec9SKyle Evans /* Tell that we are uninitialized */
58*f0865ec9SKyle Evans ctx->magic = WORD(0);
59*f0865ec9SKyle Evans ret = 0;
60*f0865ec9SKyle Evans
61*f0865ec9SKyle Evans err:
62*f0865ec9SKyle Evans return ret;
63*f0865ec9SKyle Evans }
64*f0865ec9SKyle Evans
65*f0865ec9SKyle Evans /*
66*f0865ec9SKyle Evans * Scattered version performing init/update/finalize on a vector of buffers
67*f0865ec9SKyle Evans * 'inputs' with the length of each buffer passed via 'ilens'. The function
68*f0865ec9SKyle Evans * loops on pointers in 'inputs' until it finds a NULL pointer. The function
69*f0865ec9SKyle Evans * returns 0 on success, -1 on error.
70*f0865ec9SKyle Evans */
sha3_512_scattered(const u8 ** inputs,const u32 * ilens,u8 output[SHA3_512_DIGEST_SIZE])71*f0865ec9SKyle Evans int sha3_512_scattered(const u8 **inputs, const u32 *ilens,
72*f0865ec9SKyle Evans u8 output[SHA3_512_DIGEST_SIZE])
73*f0865ec9SKyle Evans {
74*f0865ec9SKyle Evans sha3_512_context ctx;
75*f0865ec9SKyle Evans int ret, pos = 0;
76*f0865ec9SKyle Evans
77*f0865ec9SKyle Evans MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
78*f0865ec9SKyle Evans
79*f0865ec9SKyle Evans ret = sha3_512_init(&ctx); EG(ret, err);
80*f0865ec9SKyle Evans
81*f0865ec9SKyle Evans while (inputs[pos] != NULL) {
82*f0865ec9SKyle Evans ret = sha3_512_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
83*f0865ec9SKyle Evans pos += 1;
84*f0865ec9SKyle Evans }
85*f0865ec9SKyle Evans
86*f0865ec9SKyle Evans ret = sha3_512_final(&ctx, output);
87*f0865ec9SKyle Evans
88*f0865ec9SKyle Evans err:
89*f0865ec9SKyle Evans return ret;
90*f0865ec9SKyle Evans }
91*f0865ec9SKyle Evans
92*f0865ec9SKyle Evans /*
93*f0865ec9SKyle Evans * Single call version performing init/update/final on given input.
94*f0865ec9SKyle Evans * Returns 0 on success, -1 on error.
95*f0865ec9SKyle Evans */
sha3_512(const u8 * input,u32 ilen,u8 output[SHA3_512_DIGEST_SIZE])96*f0865ec9SKyle Evans int sha3_512(const u8 *input, u32 ilen, u8 output[SHA3_512_DIGEST_SIZE])
97*f0865ec9SKyle Evans {
98*f0865ec9SKyle Evans sha3_512_context ctx;
99*f0865ec9SKyle Evans int ret;
100*f0865ec9SKyle Evans
101*f0865ec9SKyle Evans ret = sha3_512_init(&ctx); EG(ret, err);
102*f0865ec9SKyle Evans ret = sha3_512_update(&ctx, input, ilen); EG(ret, err);
103*f0865ec9SKyle Evans ret = sha3_512_final(&ctx, output);
104*f0865ec9SKyle Evans
105*f0865ec9SKyle Evans err:
106*f0865ec9SKyle Evans return ret;
107*f0865ec9SKyle Evans }
108*f0865ec9SKyle Evans
109*f0865ec9SKyle Evans #else /* WITH_HASH_SHA3_512 */
110*f0865ec9SKyle Evans
111*f0865ec9SKyle Evans /*
112*f0865ec9SKyle Evans * Dummy definition to avoid the empty translation unit ISO C warning
113*f0865ec9SKyle Evans */
114*f0865ec9SKyle Evans typedef int dummy;
115*f0865ec9SKyle Evans #endif /* WITH_HASH_SHA3_512 */
116