1 /*
2 * Copyright (C) 2022 - This file is part of libecc project
3 *
4 * Authors:
5 * Ryad BENADJILA <ryadbenadjila@gmail.com>
6 * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7 *
8 * This software is licensed under a dual BSD and GPL v2 license.
9 * See LICENSE file at the root folder of the project.
10 */
11 #include <libecc/lib_ecc_config.h>
12 #ifdef WITH_HASH_BASH224
13
14 #include <libecc/hash/bash224.h>
15
16 /* Init hash function. Returns 0 on success, -1 on error. */
bash224_init(bash224_context * ctx)17 int bash224_init(bash224_context *ctx)
18 {
19 int ret;
20
21 ret = _bash_init(ctx, BASH224_DIGEST_SIZE); EG(ret, err);
22
23 /* Tell that we are initialized */
24 ctx->magic = BASH224_HASH_MAGIC;
25
26 err:
27 return ret;
28 }
29
30 /* Update hash function. Returns 0 on success, -1 on error. */
bash224_update(bash224_context * ctx,const u8 * input,u32 ilen)31 int bash224_update(bash224_context *ctx, const u8 *input, u32 ilen)
32 {
33 int ret;
34
35 BASH224_HASH_CHECK_INITIALIZED(ctx, ret, err);
36
37 ret = _bash_update((bash_context *)ctx, input, ilen);
38
39 err:
40 return ret;
41 }
42
43 /* Finalize hash function. Returns 0 on success, -1 on error. */
bash224_final(bash224_context * ctx,u8 output[BASH224_DIGEST_SIZE])44 int bash224_final(bash224_context *ctx, u8 output[BASH224_DIGEST_SIZE])
45 {
46 int ret;
47
48 BASH224_HASH_CHECK_INITIALIZED(ctx, ret, err);
49
50 ret = _bash_finalize((bash_context *)ctx, output); EG(ret, err);
51
52 /* Tell that we are uninitialized */
53 ctx->magic = WORD(0);
54 ret = 0;
55
56 err:
57 return ret;
58 }
59
60 /*
61 * Scattered version performing init/update/finalize on a vector of buffers
62 * 'inputs' with the length of each buffer passed via 'ilens'. The function
63 * loops on pointers in 'inputs' until it finds a NULL pointer. The function
64 * returns 0 on success, -1 on error.
65 */
bash224_scattered(const u8 ** inputs,const u32 * ilens,u8 output[BASH224_DIGEST_SIZE])66 int bash224_scattered(const u8 **inputs, const u32 *ilens,
67 u8 output[BASH224_DIGEST_SIZE])
68 {
69 bash224_context ctx;
70 int ret, pos = 0;
71
72 MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
73
74 ret = bash224_init(&ctx); EG(ret, err);
75
76 while (inputs[pos] != NULL) {
77 ret = bash224_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
78 pos += 1;
79 }
80
81 ret = bash224_final(&ctx, output);
82
83 err:
84 return ret;
85 }
86
87 /*
88 * Single call version performing init/update/final on given input.
89 * Returns 0 on success, -1 on error.
90 */
bash224(const u8 * input,u32 ilen,u8 output[BASH224_DIGEST_SIZE])91 int bash224(const u8 *input, u32 ilen, u8 output[BASH224_DIGEST_SIZE])
92 {
93 bash224_context ctx;
94 int ret;
95
96 ret = bash224_init(&ctx); EG(ret, err);
97 ret = bash224_update(&ctx, input, ilen); EG(ret, err);
98 ret = bash224_final(&ctx, output);
99
100 err:
101 return ret;
102 }
103
104 #else /* WITH_HASH_BASH224 */
105
106 /*
107 * Dummy definition to avoid the empty translation unit ISO C warning
108 */
109 typedef int dummy;
110 #endif /* WITH_HASH_BASH224 */
111