1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans * Copyright (C) 2022 - 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_BASH384
13*f0865ec9SKyle Evans
14*f0865ec9SKyle Evans #include <libecc/hash/bash384.h>
15*f0865ec9SKyle Evans
16*f0865ec9SKyle Evans /* Init hash function. Returns 0 on success, -1 on error. */
bash384_init(bash384_context * ctx)17*f0865ec9SKyle Evans int bash384_init(bash384_context *ctx)
18*f0865ec9SKyle Evans {
19*f0865ec9SKyle Evans int ret;
20*f0865ec9SKyle Evans
21*f0865ec9SKyle Evans ret = _bash_init(ctx, BASH384_DIGEST_SIZE); EG(ret, err);
22*f0865ec9SKyle Evans
23*f0865ec9SKyle Evans /* Tell that we are initialized */
24*f0865ec9SKyle Evans ctx->magic = BASH384_HASH_MAGIC;
25*f0865ec9SKyle Evans
26*f0865ec9SKyle Evans err:
27*f0865ec9SKyle Evans return ret;
28*f0865ec9SKyle Evans }
29*f0865ec9SKyle Evans
30*f0865ec9SKyle Evans /* Update hash function. Returns 0 on success, -1 on error. */
bash384_update(bash384_context * ctx,const u8 * input,u32 ilen)31*f0865ec9SKyle Evans int bash384_update(bash384_context *ctx, const u8 *input, u32 ilen)
32*f0865ec9SKyle Evans {
33*f0865ec9SKyle Evans int ret;
34*f0865ec9SKyle Evans
35*f0865ec9SKyle Evans BASH384_HASH_CHECK_INITIALIZED(ctx, ret, err);
36*f0865ec9SKyle Evans
37*f0865ec9SKyle Evans ret = _bash_update((bash_context *)ctx, input, ilen);
38*f0865ec9SKyle Evans
39*f0865ec9SKyle Evans err:
40*f0865ec9SKyle Evans return ret;
41*f0865ec9SKyle Evans }
42*f0865ec9SKyle Evans
43*f0865ec9SKyle Evans /* Finalize hash function. Returns 0 on success, -1 on error. */
bash384_final(bash384_context * ctx,u8 output[BASH384_DIGEST_SIZE])44*f0865ec9SKyle Evans int bash384_final(bash384_context *ctx, u8 output[BASH384_DIGEST_SIZE])
45*f0865ec9SKyle Evans {
46*f0865ec9SKyle Evans int ret;
47*f0865ec9SKyle Evans
48*f0865ec9SKyle Evans BASH384_HASH_CHECK_INITIALIZED(ctx, ret, err);
49*f0865ec9SKyle Evans
50*f0865ec9SKyle Evans ret = _bash_finalize((bash_context *)ctx, output); EG(ret, err);
51*f0865ec9SKyle Evans
52*f0865ec9SKyle Evans /* Tell that we are uninitialized */
53*f0865ec9SKyle Evans ctx->magic = WORD(0);
54*f0865ec9SKyle Evans ret = 0;
55*f0865ec9SKyle Evans
56*f0865ec9SKyle Evans err:
57*f0865ec9SKyle Evans return ret;
58*f0865ec9SKyle Evans }
59*f0865ec9SKyle Evans
60*f0865ec9SKyle Evans /*
61*f0865ec9SKyle Evans * Scattered version performing init/update/finalize on a vector of buffers
62*f0865ec9SKyle Evans * 'inputs' with the length of each buffer passed via 'ilens'. The function
63*f0865ec9SKyle Evans * loops on pointers in 'inputs' until it finds a NULL pointer. The function
64*f0865ec9SKyle Evans * returns 0 on success, -1 on error.
65*f0865ec9SKyle Evans */
bash384_scattered(const u8 ** inputs,const u32 * ilens,u8 output[BASH384_DIGEST_SIZE])66*f0865ec9SKyle Evans int bash384_scattered(const u8 **inputs, const u32 *ilens,
67*f0865ec9SKyle Evans u8 output[BASH384_DIGEST_SIZE])
68*f0865ec9SKyle Evans {
69*f0865ec9SKyle Evans bash384_context ctx;
70*f0865ec9SKyle Evans int ret, pos = 0;
71*f0865ec9SKyle Evans
72*f0865ec9SKyle Evans MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
73*f0865ec9SKyle Evans
74*f0865ec9SKyle Evans ret = bash384_init(&ctx); EG(ret, err);
75*f0865ec9SKyle Evans
76*f0865ec9SKyle Evans while (inputs[pos] != NULL) {
77*f0865ec9SKyle Evans const u8 *buf = inputs[pos];
78*f0865ec9SKyle Evans u32 buflen = ilens[pos];
79*f0865ec9SKyle Evans
80*f0865ec9SKyle Evans ret = bash384_update(&ctx, buf, buflen); EG(ret, err);
81*f0865ec9SKyle Evans
82*f0865ec9SKyle Evans pos += 1;
83*f0865ec9SKyle Evans }
84*f0865ec9SKyle Evans
85*f0865ec9SKyle Evans ret = bash384_final(&ctx, output);
86*f0865ec9SKyle Evans
87*f0865ec9SKyle Evans err:
88*f0865ec9SKyle Evans return ret;
89*f0865ec9SKyle Evans }
90*f0865ec9SKyle Evans
91*f0865ec9SKyle Evans /*
92*f0865ec9SKyle Evans * Single call version performing init/update/final on given input.
93*f0865ec9SKyle Evans * Returns 0 on success, -1 on error.
94*f0865ec9SKyle Evans */
bash384(const u8 * input,u32 ilen,u8 output[BASH384_DIGEST_SIZE])95*f0865ec9SKyle Evans int bash384(const u8 *input, u32 ilen, u8 output[BASH384_DIGEST_SIZE])
96*f0865ec9SKyle Evans {
97*f0865ec9SKyle Evans bash384_context ctx;
98*f0865ec9SKyle Evans int ret;
99*f0865ec9SKyle Evans
100*f0865ec9SKyle Evans ret = bash384_init(&ctx); EG(ret, err);
101*f0865ec9SKyle Evans ret = bash384_update(&ctx, input, ilen); EG(ret, err);
102*f0865ec9SKyle Evans ret = bash384_final(&ctx, output);
103*f0865ec9SKyle Evans
104*f0865ec9SKyle Evans err:
105*f0865ec9SKyle Evans return ret;
106*f0865ec9SKyle Evans }
107*f0865ec9SKyle Evans
108*f0865ec9SKyle Evans #else /* WITH_HASH_BASH384 */
109*f0865ec9SKyle Evans
110*f0865ec9SKyle Evans /*
111*f0865ec9SKyle Evans * Dummy definition to avoid the empty translation unit ISO C warning
112*f0865ec9SKyle Evans */
113*f0865ec9SKyle Evans typedef int dummy;
114*f0865ec9SKyle Evans #endif /* WITH_HASH_BASH384 */
115