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_BASH224
13*f0865ec9SKyle Evans
14*f0865ec9SKyle Evans #include <libecc/hash/bash224.h>
15*f0865ec9SKyle Evans
16*f0865ec9SKyle Evans /* Init hash function. Returns 0 on success, -1 on error. */
bash224_init(bash224_context * ctx)17*f0865ec9SKyle Evans int bash224_init(bash224_context *ctx)
18*f0865ec9SKyle Evans {
19*f0865ec9SKyle Evans int ret;
20*f0865ec9SKyle Evans
21*f0865ec9SKyle Evans ret = _bash_init(ctx, BASH224_DIGEST_SIZE); EG(ret, err);
22*f0865ec9SKyle Evans
23*f0865ec9SKyle Evans /* Tell that we are initialized */
24*f0865ec9SKyle Evans ctx->magic = BASH224_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. */
bash224_update(bash224_context * ctx,const u8 * input,u32 ilen)31*f0865ec9SKyle Evans int bash224_update(bash224_context *ctx, const u8 *input, u32 ilen)
32*f0865ec9SKyle Evans {
33*f0865ec9SKyle Evans int ret;
34*f0865ec9SKyle Evans
35*f0865ec9SKyle Evans BASH224_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. */
bash224_final(bash224_context * ctx,u8 output[BASH224_DIGEST_SIZE])44*f0865ec9SKyle Evans int bash224_final(bash224_context *ctx, u8 output[BASH224_DIGEST_SIZE])
45*f0865ec9SKyle Evans {
46*f0865ec9SKyle Evans int ret;
47*f0865ec9SKyle Evans
48*f0865ec9SKyle Evans BASH224_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 */
bash224_scattered(const u8 ** inputs,const u32 * ilens,u8 output[BASH224_DIGEST_SIZE])66*f0865ec9SKyle Evans int bash224_scattered(const u8 **inputs, const u32 *ilens,
67*f0865ec9SKyle Evans u8 output[BASH224_DIGEST_SIZE])
68*f0865ec9SKyle Evans {
69*f0865ec9SKyle Evans bash224_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 = bash224_init(&ctx); EG(ret, err);
75*f0865ec9SKyle Evans
76*f0865ec9SKyle Evans while (inputs[pos] != NULL) {
77*f0865ec9SKyle Evans ret = bash224_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
78*f0865ec9SKyle Evans pos += 1;
79*f0865ec9SKyle Evans }
80*f0865ec9SKyle Evans
81*f0865ec9SKyle Evans ret = bash224_final(&ctx, output);
82*f0865ec9SKyle Evans
83*f0865ec9SKyle Evans err:
84*f0865ec9SKyle Evans return ret;
85*f0865ec9SKyle Evans }
86*f0865ec9SKyle Evans
87*f0865ec9SKyle Evans /*
88*f0865ec9SKyle Evans * Single call version performing init/update/final on given input.
89*f0865ec9SKyle Evans * Returns 0 on success, -1 on error.
90*f0865ec9SKyle Evans */
bash224(const u8 * input,u32 ilen,u8 output[BASH224_DIGEST_SIZE])91*f0865ec9SKyle Evans int bash224(const u8 *input, u32 ilen, u8 output[BASH224_DIGEST_SIZE])
92*f0865ec9SKyle Evans {
93*f0865ec9SKyle Evans bash224_context ctx;
94*f0865ec9SKyle Evans int ret;
95*f0865ec9SKyle Evans
96*f0865ec9SKyle Evans ret = bash224_init(&ctx); EG(ret, err);
97*f0865ec9SKyle Evans ret = bash224_update(&ctx, input, ilen); EG(ret, err);
98*f0865ec9SKyle Evans ret = bash224_final(&ctx, output);
99*f0865ec9SKyle Evans
100*f0865ec9SKyle Evans err:
101*f0865ec9SKyle Evans return ret;
102*f0865ec9SKyle Evans }
103*f0865ec9SKyle Evans
104*f0865ec9SKyle Evans #else /* WITH_HASH_BASH224 */
105*f0865ec9SKyle Evans
106*f0865ec9SKyle Evans /*
107*f0865ec9SKyle Evans * Dummy definition to avoid the empty translation unit ISO C warning
108*f0865ec9SKyle Evans */
109*f0865ec9SKyle Evans typedef int dummy;
110*f0865ec9SKyle Evans #endif /* WITH_HASH_BASH224 */
111