xref: /freebsd/crypto/libecc/src/hash/sha512-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_SHA512_224
18 
19 #include <libecc/hash/sha512-224.h>
20 
21 /* Init hash function. Returns 0 on success, -1 on error. */
sha512_224_init(sha512_224_context * ctx)22 int sha512_224_init(sha512_224_context *ctx)
23 {
24 	int ret;
25 
26 	MUST_HAVE((ctx != NULL), ret, err);
27 
28 	ctx->sha512_total[0] = ctx->sha512_total[1] = 0;
29 	ctx->sha512_state[0] = (u64)(0x8C3D37C819544DA2);
30 	ctx->sha512_state[1] = (u64)(0x73E1996689DCD4D6);
31 	ctx->sha512_state[2] = (u64)(0x1DFAB7AE32FF9C82);
32 	ctx->sha512_state[3] = (u64)(0x679DD514582F9FCF);
33 	ctx->sha512_state[4] = (u64)(0x0F6D2B697BD44DA8);
34 	ctx->sha512_state[5] = (u64)(0x77E36F7304C48942);
35 	ctx->sha512_state[6] = (u64)(0x3F9D85A86A1D36C8);
36 	ctx->sha512_state[7] = (u64)(0x1112E6AD91D692A1);
37 
38 	/* Tell that we are initialized */
39 	ctx->magic = SHA512_224_HASH_MAGIC;
40 	ret = 0;
41 
42 err:
43 	return ret;
44 }
45 
46 /* Update hash function. Returns 0 on success, -1 on error. */
sha512_224_update(sha512_224_context * ctx,const u8 * input,u32 ilen)47 int sha512_224_update(sha512_224_context *ctx, const u8 *input, u32 ilen)
48 {
49 	int ret;
50 
51 	SHA512_224_HASH_CHECK_INITIALIZED(ctx, ret, err);
52 
53 	ret = sha512_core_update(ctx, input, ilen);
54 
55 err:
56 	return ret;
57 }
58 
59 /* Finalize hash function. Returns 0 on success, -1 on error. */
sha512_224_final(sha512_224_context * ctx,u8 output[SHA512_224_DIGEST_SIZE])60 int sha512_224_final(sha512_224_context *ctx, u8 output[SHA512_224_DIGEST_SIZE])
61 {
62 	int ret;
63 
64 	SHA512_224_HASH_CHECK_INITIALIZED(ctx, ret, err);
65 
66 	ret = sha512_core_final(ctx, output, SHA512_224_DIGEST_SIZE); EG(ret, err);
67 
68 	/* Tell that we are uninitialized */
69 	ctx->magic = WORD(0);
70 	ret = 0;
71 
72 err:
73 	return ret;
74 }
75 
76 /*
77  * Scattered version performing init/update/finalize on a vector of buffers
78  * 'inputs' with the length of each buffer passed via 'ilens'. The function
79  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
80  * returns 0 on success, -1 on error.
81  */
sha512_224_scattered(const u8 ** inputs,const u32 * ilens,u8 output[SHA512_224_DIGEST_SIZE])82 int sha512_224_scattered(const u8 **inputs, const u32 *ilens,
83 		      u8 output[SHA512_224_DIGEST_SIZE])
84 {
85 	sha512_224_context ctx;
86 	int pos = 0;
87 	int ret;
88 
89 	MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
90 
91 	ret = sha512_224_init(&ctx); EG(ret, err);
92 
93 	while (inputs[pos] != NULL) {
94 		ret = sha512_224_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
95 		pos += 1;
96 	}
97 
98 	ret = sha512_224_final(&ctx, output);
99 
100 err:
101 	return ret;
102 }
103 
104 /* init/update/finalize on a single buffer 'input' of length 'ilen'. */
sha512_224(const u8 * input,u32 ilen,u8 output[SHA512_224_DIGEST_SIZE])105 int sha512_224(const u8 *input, u32 ilen, u8 output[SHA512_224_DIGEST_SIZE])
106 {
107 	sha512_224_context ctx;
108 	int ret;
109 
110 	ret = sha512_224_init(&ctx); EG(ret, err);
111 	ret = sha512_224_update(&ctx, input, ilen); EG(ret, err);
112 	ret = sha512_224_final(&ctx, output);
113 
114 err:
115 	return ret;
116 }
117 
118 #else /* WITH_HASH_SHA512_224 */
119 
120 /*
121  * Dummy definition to avoid the empty translation unit ISO C warning
122  */
123 typedef int dummy;
124 #endif /* WITH_HASH_SHA512_224 */
125