xref: /freebsd/crypto/libecc/src/hash/sha3-224.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1 /*
2  *  Copyright (C) 2017 - 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  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8  *
9  *  Contributors:
10  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12  *
13  *  This software is licensed under a dual BSD and GPL v2 license.
14  *  See LICENSE file at the root folder of the project.
15  */
16 #include <libecc/lib_ecc_config.h>
17 #ifdef WITH_HASH_SHA3_224
18 
19 #include <libecc/hash/sha3-224.h>
20 
21 /* Init hash function. Returns 0 on success, -1 on error. */
sha3_224_init(sha3_224_context * ctx)22 int sha3_224_init(sha3_224_context *ctx)
23 {
24 	int ret;
25 
26 	ret = _sha3_init(ctx, SHA3_224_DIGEST_SIZE); EG(ret, err);
27 
28 	/* Tell that we are initialized */
29 	ctx->magic = SHA3_224_HASH_MAGIC;
30 
31 err:
32 	return ret;
33 }
34 
35 /* Update hash function. Returns 0 on success, -1 on error. */
sha3_224_update(sha3_224_context * ctx,const u8 * input,u32 ilen)36 int sha3_224_update(sha3_224_context *ctx, const u8 *input, u32 ilen)
37 {
38 	int ret;
39 
40 	SHA3_224_HASH_CHECK_INITIALIZED(ctx, ret, err);
41 
42 	ret = _sha3_update((sha3_context *)ctx, input, ilen);
43 
44 err:
45 	return ret;
46 }
47 
48 /* Finalize hash function. Returns 0 on success, -1 on error. */
sha3_224_final(sha3_224_context * ctx,u8 output[SHA3_224_DIGEST_SIZE])49 int sha3_224_final(sha3_224_context *ctx, u8 output[SHA3_224_DIGEST_SIZE])
50 {
51 	int ret;
52 
53 	SHA3_224_HASH_CHECK_INITIALIZED(ctx, ret, err);
54 
55 	ret = _sha3_finalize((sha3_context *)ctx, output); EG(ret, err);
56 
57 	/* Tell that we are uninitialized */
58 	ctx->magic = WORD(0);
59 	ret = 0;
60 
61 err:
62 	return ret;
63 }
64 
65 /*
66  * Scattered version performing init/update/finalize on a vector of buffers
67  * 'inputs' with the length of each buffer passed via 'ilens'. The function
68  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
69  * returns 0 on success, -1 on error.
70  */
sha3_224_scattered(const u8 ** inputs,const u32 * ilens,u8 output[SHA3_224_DIGEST_SIZE])71 int sha3_224_scattered(const u8 **inputs, const u32 *ilens,
72 			u8 output[SHA3_224_DIGEST_SIZE])
73 {
74 	sha3_224_context ctx;
75 	int ret, pos = 0;
76 
77 	MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
78 
79 	ret = sha3_224_init(&ctx); EG(ret, err);
80 
81 	while (inputs[pos] != NULL) {
82 		ret = sha3_224_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
83 		pos += 1;
84 	}
85 
86 	ret = sha3_224_final(&ctx, output);
87 
88 err:
89 	return ret;
90 }
91 
92 /*
93  * Single call version performing init/update/final on given input.
94  * Returns 0 on success, -1 on error.
95  */
sha3_224(const u8 * input,u32 ilen,u8 output[SHA3_224_DIGEST_SIZE])96 int sha3_224(const u8 *input, u32 ilen, u8 output[SHA3_224_DIGEST_SIZE])
97 {
98 	sha3_224_context ctx;
99 	int ret;
100 
101 	ret = sha3_224_init(&ctx); EG(ret, err);
102 	ret = sha3_224_update(&ctx, input, ilen); EG(ret, err);
103 	ret = sha3_224_final(&ctx, output);
104 
105 err:
106 	return ret;
107 }
108 
109 #else /* WITH_HASH_SHA3_224 */
110 
111 /*
112  * Dummy definition to avoid the empty translation unit ISO C warning
113  */
114 typedef int dummy;
115 #endif /* WITH_HASH_SHA3_224 */
116