Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/libecc/src/hash/bash256.c
34889 views
1
/*
2
* Copyright (C) 2022 - This file is part of libecc project
3
*
4
* Authors:
5
* Ryad BENADJILA <[email protected]>
6
* Arnaud EBALARD <[email protected]>
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_BASH256
13
14
#include <libecc/hash/bash256.h>
15
16
/* Init hash function. Returns 0 on success, -1 on error. */
17
int bash256_init(bash256_context *ctx)
18
{
19
int ret;
20
21
ret = _bash_init(ctx, BASH256_DIGEST_SIZE); EG(ret, err);
22
23
/* Tell that we are initialized */
24
ctx->magic = BASH256_HASH_MAGIC;
25
26
err:
27
return ret;
28
}
29
30
/* Update hash function. Returns 0 on success, -1 on error. */
31
int bash256_update(bash256_context *ctx, const u8 *input, u32 ilen)
32
{
33
int ret;
34
35
BASH256_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. */
44
int bash256_final(bash256_context *ctx, u8 output[BASH256_DIGEST_SIZE])
45
{
46
int ret;
47
48
BASH256_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
*/
66
int bash256_scattered(const u8 **inputs, const u32 *ilens,
67
u8 output[BASH256_DIGEST_SIZE])
68
{
69
bash256_context ctx;
70
int ret, pos = 0;
71
72
MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
73
74
ret = bash256_init(&ctx); EG(ret, err);
75
76
while (inputs[pos] != NULL) {
77
ret = bash256_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
78
pos += 1;
79
}
80
81
ret = bash256_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
*/
91
int bash256(const u8 *input, u32 ilen, u8 output[BASH256_DIGEST_SIZE])
92
{
93
bash256_context ctx;
94
int ret;
95
96
ret = bash256_init(&ctx); EG(ret, err);
97
ret = bash256_update(&ctx, input, ilen); EG(ret, err);
98
ret = bash256_final(&ctx, output); EG(ret, err);
99
100
err:
101
return ret;
102
}
103
104
#else /* WITH_HASH_BASH256 */
105
106
/*
107
* Dummy definition to avoid the empty translation unit ISO C warning
108
*/
109
typedef int dummy;
110
#endif /* WITH_HASH_BASH256 */
111
112