xref: /freebsd/crypto/libecc/src/hash/bash384.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
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_BASH384
13 
14 #include <libecc/hash/bash384.h>
15 
16 /* Init hash function. Returns 0 on success, -1 on error. */
bash384_init(bash384_context * ctx)17 int bash384_init(bash384_context *ctx)
18 {
19 	int ret;
20 
21 	ret = _bash_init(ctx, BASH384_DIGEST_SIZE); EG(ret, err);
22 
23 	/* Tell that we are initialized */
24 	ctx->magic = BASH384_HASH_MAGIC;
25 
26 err:
27 	return ret;
28 }
29 
30 /* Update hash function. Returns 0 on success, -1 on error. */
bash384_update(bash384_context * ctx,const u8 * input,u32 ilen)31 int bash384_update(bash384_context *ctx, const u8 *input, u32 ilen)
32 {
33 	int ret;
34 
35 	BASH384_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. */
bash384_final(bash384_context * ctx,u8 output[BASH384_DIGEST_SIZE])44 int bash384_final(bash384_context *ctx, u8 output[BASH384_DIGEST_SIZE])
45 {
46 	int ret;
47 
48 	BASH384_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  */
bash384_scattered(const u8 ** inputs,const u32 * ilens,u8 output[BASH384_DIGEST_SIZE])66 int bash384_scattered(const u8 **inputs, const u32 *ilens,
67 			u8 output[BASH384_DIGEST_SIZE])
68 {
69 	bash384_context ctx;
70 	int ret, pos = 0;
71 
72 	MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
73 
74 	ret = bash384_init(&ctx); EG(ret, err);
75 
76 	while (inputs[pos] != NULL) {
77 		const u8 *buf = inputs[pos];
78 		u32 buflen = ilens[pos];
79 
80 		ret = bash384_update(&ctx, buf, buflen); EG(ret, err);
81 
82 		pos += 1;
83 	}
84 
85 	ret = bash384_final(&ctx, output);
86 
87 err:
88 	return ret;
89 }
90 
91 /*
92  * Single call version performing init/update/final on given input.
93  * Returns 0 on success, -1 on error.
94  */
bash384(const u8 * input,u32 ilen,u8 output[BASH384_DIGEST_SIZE])95 int bash384(const u8 *input, u32 ilen, u8 output[BASH384_DIGEST_SIZE])
96 {
97 	bash384_context ctx;
98 	int ret;
99 
100 	ret = bash384_init(&ctx); EG(ret, err);
101 	ret = bash384_update(&ctx, input, ilen); EG(ret, err);
102 	ret = bash384_final(&ctx, output);
103 
104 err:
105 	return ret;
106 }
107 
108 #else /* WITH_HASH_BASH384 */
109 
110 /*
111  * Dummy definition to avoid the empty translation unit ISO C warning
112  */
113 typedef int dummy;
114 #endif /* WITH_HASH_BASH384 */
115