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_256
18
19 #include <libecc/hash/sha512-256.h>
20
21 /* Init hash function. Returns 0 on success, -1 on error. */
sha512_256_init(sha512_256_context * ctx)22 int sha512_256_init(sha512_256_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)(0x22312194FC2BF72C);
30 ctx->sha512_state[1] = (u64)(0x9F555FA3C84C64C2);
31 ctx->sha512_state[2] = (u64)(0x2393B86B6F53B151);
32 ctx->sha512_state[3] = (u64)(0x963877195940EABD);
33 ctx->sha512_state[4] = (u64)(0x96283EE2A88EFFE3);
34 ctx->sha512_state[5] = (u64)(0xBE5E1E2553863992);
35 ctx->sha512_state[6] = (u64)(0x2B0199FC2C85B8AA);
36 ctx->sha512_state[7] = (u64)(0x0EB72DDC81C52CA2);
37
38 /* Tell that we are initialized */
39 ctx->magic = SHA512_256_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_256_update(sha512_256_context * ctx,const u8 * input,u32 ilen)47 int sha512_256_update(sha512_256_context *ctx, const u8 *input, u32 ilen)
48 {
49 int ret;
50
51 SHA512_256_HASH_CHECK_INITIALIZED(ctx, ret, err);
52
53 ret = sha512_core_update(ctx, input, ilen);
54
55 err:
56 return ret;
57 }
58
59 /*
60 * Finalize hash function. Returns 0 on success, -1 on error.
61 */
sha512_256_final(sha512_256_context * ctx,u8 output[SHA512_256_DIGEST_SIZE])62 int sha512_256_final(sha512_256_context *ctx, u8 output[SHA512_256_DIGEST_SIZE])
63 {
64 int ret;
65
66 SHA512_256_HASH_CHECK_INITIALIZED(ctx, ret, err);
67 ret = sha512_core_final(ctx, output, SHA512_256_DIGEST_SIZE); EG(ret, err);
68
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_256_scattered(const u8 ** inputs,const u32 * ilens,u8 output[SHA512_256_DIGEST_SIZE])82 int sha512_256_scattered(const u8 **inputs, const u32 *ilens,
83 u8 output[SHA512_256_DIGEST_SIZE])
84 {
85 sha512_256_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_256_init(&ctx); EG(ret, err);
92
93 while (inputs[pos] != NULL) {
94 ret = sha512_256_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
95 pos += 1;
96 }
97
98 ret = sha512_256_final(&ctx, output);
99
100 err:
101 return ret;
102 }
103
104 /* init/update/finalize on a single buffer 'input' of length 'ilen'. */
sha512_256(const u8 * input,u32 ilen,u8 output[SHA512_256_DIGEST_SIZE])105 int sha512_256(const u8 *input, u32 ilen, u8 output[SHA512_256_DIGEST_SIZE])
106 {
107 sha512_256_context ctx;
108 int ret;
109
110 ret = sha512_256_init(&ctx); EG(ret, err);
111 ret = sha512_256_update(&ctx, input, ilen); EG(ret, err);
112 ret = sha512_256_final(&ctx, output);
113
114 err:
115 return ret;
116 }
117
118 #else /* WITH_HASH_SHA512_256 */
119
120 /*
121 * Dummy definition to avoid the empty translation unit ISO C warning
122 */
123 typedef int dummy;
124 #endif /* WITH_HASH_SHA512_256 */
125