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