/src/libluksde/libhmac/libhmac_sha512_context.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SHA-512 functions |
3 | | * |
4 | | * Copyright (C) 2011-2024, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <common.h> |
23 | | #include <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) |
28 | | #include <openssl/sha.h> |
29 | | |
30 | | #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) |
31 | | #include <openssl/err.h> |
32 | | #include <openssl/evp.h> |
33 | | #endif |
34 | | |
35 | | #include "libhmac_byte_stream.h" |
36 | | #include "libhmac_definitions.h" |
37 | | #include "libhmac_libcerror.h" |
38 | | #include "libhmac_sha512_context.h" |
39 | | |
40 | | #if !defined( LIBHMAC_HAVE_SHA512_SUPPORT ) |
41 | | |
42 | | /* FIPS 180-2 based SHA-512 functions |
43 | | */ |
44 | | |
45 | | /* The first 64-bits of the fractional parts of the square roots of the first 8 primes [ 2, 19 ] |
46 | | */ |
47 | | uint64_t libhmac_sha512_context_prime_square_roots[ 8 ] = { |
48 | | 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, |
49 | | 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179 |
50 | | }; |
51 | | |
52 | | /* The first 64-bits of the fractional parts of the cube roots of the first 80 primes [ 2, 409 ] |
53 | | */ |
54 | | uint64_t libhmac_sha512_context_prime_cube_roots[ 80 ] = { |
55 | | 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, |
56 | | 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, |
57 | | 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, |
58 | | 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694, |
59 | | 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, |
60 | | 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, |
61 | | 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, |
62 | | 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70, |
63 | | 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, |
64 | | 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b, |
65 | | 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, |
66 | | 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, |
67 | | 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, |
68 | | 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, |
69 | | 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec, |
70 | | 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, |
71 | | 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, |
72 | | 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b, |
73 | | 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, |
74 | | 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 |
75 | | }; |
76 | | |
77 | | #define libhmac_sha512_context_transform_extend_64bit_value( values_64bit, value_64bit_index, s0, s1 ) \ |
78 | 4.21M | s0 = byte_stream_bit_rotate_right_64bit( values_64bit[ value_64bit_index - 15 ], 1 ) \ |
79 | 4.21M | ^ byte_stream_bit_rotate_right_64bit( values_64bit[ value_64bit_index - 15 ], 8 ) \ |
80 | 4.21M | ^ ( values_64bit[ value_64bit_index - 15 ] >> 7 ); \ |
81 | 4.21M | s1 = byte_stream_bit_rotate_right_64bit( values_64bit[ value_64bit_index - 2 ], 19 ) \ |
82 | 4.21M | ^ byte_stream_bit_rotate_right_64bit( values_64bit[ value_64bit_index - 2 ], 61 ) \ |
83 | 4.21M | ^ ( values_64bit[ value_64bit_index - 2 ] >> 6 ); \ |
84 | 4.21M | \ |
85 | 4.21M | values_64bit[ value_64bit_index ] = values_64bit[ value_64bit_index - 16 ] \ |
86 | 4.21M | + s0 \ |
87 | 4.21M | + values_64bit[ value_64bit_index - 7 ] \ |
88 | 4.21M | + s1 |
89 | | |
90 | | #define libhmac_sha512_context_transform_unfolded_extend_64bit_values( values_64bit, s0, s1 ) \ |
91 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 16, s0, s1 ); \ |
92 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 17, s0, s1 ); \ |
93 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 18, s0, s1 ); \ |
94 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 19, s0, s1 ); \ |
95 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 20, s0, s1 ); \ |
96 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 21, s0, s1 ); \ |
97 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 22, s0, s1 ); \ |
98 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 23, s0, s1 ); \ |
99 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 24, s0, s1 ); \ |
100 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 25, s0, s1 ); \ |
101 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 26, s0, s1 ); \ |
102 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 27, s0, s1 ); \ |
103 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 28, s0, s1 ); \ |
104 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 29, s0, s1 ); \ |
105 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 30, s0, s1 ); \ |
106 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 31, s0, s1 ); \ |
107 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 32, s0, s1 ); \ |
108 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 33, s0, s1 ); \ |
109 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 34, s0, s1 ); \ |
110 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 35, s0, s1 ); \ |
111 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 36, s0, s1 ); \ |
112 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 37, s0, s1 ); \ |
113 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 38, s0, s1 ); \ |
114 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 39, s0, s1 ); \ |
115 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 40, s0, s1 ); \ |
116 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 41, s0, s1 ); \ |
117 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 42, s0, s1 ); \ |
118 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 43, s0, s1 ); \ |
119 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 44, s0, s1 ); \ |
120 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 45, s0, s1 ); \ |
121 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 46, s0, s1 ); \ |
122 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 47, s0, s1 ); \ |
123 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 48, s0, s1 ); \ |
124 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 49, s0, s1 ); \ |
125 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 50, s0, s1 ); \ |
126 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 51, s0, s1 ); \ |
127 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 52, s0, s1 ); \ |
128 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 53, s0, s1 ); \ |
129 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 54, s0, s1 ); \ |
130 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 55, s0, s1 ); \ |
131 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 56, s0, s1 ); \ |
132 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 57, s0, s1 ); \ |
133 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 58, s0, s1 ); \ |
134 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 59, s0, s1 ); \ |
135 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 60, s0, s1 ); \ |
136 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 61, s0, s1 ); \ |
137 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 62, s0, s1 ); \ |
138 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 63, s0, s1 ); \ |
139 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 64, s0, s1 ); \ |
140 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 65, s0, s1 ); \ |
141 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 66, s0, s1 ); \ |
142 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 67, s0, s1 ); \ |
143 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 68, s0, s1 ); \ |
144 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 69, s0, s1 ); \ |
145 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 70, s0, s1 ); \ |
146 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 71, s0, s1 ); \ |
147 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 72, s0, s1 ); \ |
148 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 73, s0, s1 ); \ |
149 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 74, s0, s1 ); \ |
150 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 75, s0, s1 ); \ |
151 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 76, s0, s1 ); \ |
152 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 77, s0, s1 ); \ |
153 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 78, s0, s1 ); \ |
154 | 65.7k | libhmac_sha512_context_transform_extend_64bit_value( values_64bit, 79, s0, s1 ); |
155 | | |
156 | | #define libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, value_64bit_index, hash_values, hash_value_index0, hash_value_index1, hash_value_index2, hash_value_index3, hash_value_index4, hash_value_index5, hash_value_index6, hash_value_index7, s0, s1, t1, t2 ) \ |
157 | 5.26M | s0 = byte_stream_bit_rotate_right_64bit( hash_values[ hash_value_index0 ], 28 ) \ |
158 | 5.26M | ^ byte_stream_bit_rotate_right_64bit( hash_values[ hash_value_index0 ], 34 ) \ |
159 | 5.26M | ^ byte_stream_bit_rotate_right_64bit( hash_values[ hash_value_index0 ], 39 ); \ |
160 | 5.26M | s1 = byte_stream_bit_rotate_right_64bit( hash_values[ hash_value_index4 ], 14 ) \ |
161 | 5.26M | ^ byte_stream_bit_rotate_right_64bit( hash_values[ hash_value_index4 ], 18 ) \ |
162 | 5.26M | ^ byte_stream_bit_rotate_right_64bit( hash_values[ hash_value_index4 ], 41 ); \ |
163 | 5.26M | \ |
164 | 5.26M | t1 = hash_values[ hash_value_index7 ]; \ |
165 | 5.26M | t1 += s1; \ |
166 | 5.26M | t1 += ( hash_values[ hash_value_index4 ] & hash_values[ hash_value_index5 ] ) \ |
167 | 5.26M | ^ ( ~( hash_values[ hash_value_index4 ] ) & hash_values[ hash_value_index6 ] ); \ |
168 | 5.26M | t1 += libhmac_sha512_context_prime_cube_roots[ value_64bit_index ]; \ |
169 | 5.26M | t1 += values_64bit[ value_64bit_index ]; \ |
170 | 5.26M | t2 = s0; \ |
171 | 5.26M | t2 += ( hash_values[ hash_value_index0 ] & hash_values[ hash_value_index1 ] ) \ |
172 | 5.26M | ^ ( hash_values[ hash_value_index0 ] & hash_values[ hash_value_index2 ] ) \ |
173 | 5.26M | ^ ( hash_values[ hash_value_index1 ] & hash_values[ hash_value_index2 ] ); \ |
174 | 5.26M | \ |
175 | 5.26M | hash_values[ hash_value_index3 ] += t1; \ |
176 | 5.26M | hash_values[ hash_value_index7 ] = t1 + t2; |
177 | | |
178 | | #define libhmac_sha512_context_transform_unfolded_calculate_hash_values( values_64bit, hash_values, s0, s1, t1, t2 ) \ |
179 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 0, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
180 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 1, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
181 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 2, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
182 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 3, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
183 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 4, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
184 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 5, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
185 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 6, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
186 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 7, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
187 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 8, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
188 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 9, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
189 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 10, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
190 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 11, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
191 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 12, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
192 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 13, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
193 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 14, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
194 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 15, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
195 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 16, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
196 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 17, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
197 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 18, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
198 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 19, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
199 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 20, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
200 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 21, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
201 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 22, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
202 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 23, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
203 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 24, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
204 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 25, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
205 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 26, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
206 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 27, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
207 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 28, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
208 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 29, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
209 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 30, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
210 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 31, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
211 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 32, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
212 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 33, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
213 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 34, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
214 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 35, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
215 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 36, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
216 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 37, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
217 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 38, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
218 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 39, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
219 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 40, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
220 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 41, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
221 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 42, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
222 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 43, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
223 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 44, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
224 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 45, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
225 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 46, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
226 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 47, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
227 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 48, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
228 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 49, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
229 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 50, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
230 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 51, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
231 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 52, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
232 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 53, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
233 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 54, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
234 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 55, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
235 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 56, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
236 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 57, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
237 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 58, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
238 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 59, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
239 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 60, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
240 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 61, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
241 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 62, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
242 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 63, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
243 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 64, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
244 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 65, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
245 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 66, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
246 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 67, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
247 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 68, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
248 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 69, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
249 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 70, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
250 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 71, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ |
251 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 72, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ |
252 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 73, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ |
253 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 74, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ |
254 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 75, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ |
255 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 76, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ |
256 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 77, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ |
257 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 78, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ |
258 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_value( values_64bit, 79, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) |
259 | | |
260 | | /* Calculates the SHA-512 of 128 byte sized blocks of data in a buffer |
261 | | * Returns the number of bytes used if successful or -1 on error |
262 | | */ |
263 | | ssize_t libhmac_sha512_context_transform( |
264 | | libhmac_internal_sha512_context_t *internal_context, |
265 | | const uint8_t *buffer, |
266 | | size_t size, |
267 | | libcerror_error_t **error ) |
268 | 122 | { |
269 | 122 | uint64_t hash_values[ 8 ]; |
270 | 122 | uint64_t values_64bit[ 80 ]; |
271 | | |
272 | 122 | static char *function = "libhmac_sha512_context_transform"; |
273 | 122 | uint64_t s0 = 0; |
274 | 122 | uint64_t s1 = 0; |
275 | 122 | uint64_t t1 = 0; |
276 | 122 | uint64_t t2 = 0; |
277 | 122 | size_t buffer_offset = 0; |
278 | | |
279 | | #if !defined( LIBHMAC_UNFOLLED_LOOPS ) |
280 | | uint8_t hash_values_index = 0; |
281 | | uint8_t value_64bit_index = 0; |
282 | | #endif |
283 | | |
284 | 122 | if( internal_context == NULL ) |
285 | 0 | { |
286 | 0 | libcerror_error_set( |
287 | 0 | error, |
288 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
289 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
290 | 0 | "%s: invalid internal context.", |
291 | 0 | function ); |
292 | |
|
293 | 0 | return( -1 ); |
294 | 0 | } |
295 | 122 | if( buffer == NULL ) |
296 | 0 | { |
297 | 0 | libcerror_error_set( |
298 | 0 | error, |
299 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
300 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
301 | 0 | "%s: invalid buffer.", |
302 | 0 | function ); |
303 | |
|
304 | 0 | return( -1 ); |
305 | 0 | } |
306 | 122 | if( size > (size_t) SSIZE_MAX ) |
307 | 0 | { |
308 | 0 | libcerror_error_set( |
309 | 0 | error, |
310 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
311 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
312 | 0 | "%s: invalid size value exceeds maximum.", |
313 | 0 | function ); |
314 | |
|
315 | 0 | return( -1 ); |
316 | 0 | } |
317 | 65.9k | while( size >= LIBHMAC_SHA512_BLOCK_SIZE ) |
318 | 65.7k | { |
319 | 65.7k | if( memory_copy( |
320 | 65.7k | hash_values, |
321 | 65.7k | internal_context->hash_values, |
322 | 65.7k | sizeof( uint64_t ) * 8 ) == NULL ) |
323 | 0 | { |
324 | 0 | libcerror_error_set( |
325 | 0 | error, |
326 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
327 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
328 | 0 | "%s: unable to copy hash values.", |
329 | 0 | function ); |
330 | |
|
331 | 0 | goto on_error; |
332 | 0 | } |
333 | 65.7k | #if defined( LIBHMAC_UNFOLLED_LOOPS ) |
334 | | /* Break the block into 16 x 64-bit values |
335 | | */ |
336 | 65.7k | libhmac_byte_stream_copy_to_16x_uint64_big_endian( |
337 | 65.7k | &( buffer[ buffer_offset ] ), |
338 | 65.7k | values_64bit ); |
339 | | |
340 | 65.7k | buffer_offset += LIBHMAC_SHA512_BLOCK_SIZE; |
341 | | |
342 | | /* Extend to 80 x 64-bit values |
343 | | */ |
344 | 65.7k | libhmac_sha512_context_transform_unfolded_extend_64bit_values( |
345 | 65.7k | values_64bit, |
346 | 65.7k | s0, |
347 | 65.7k | s1 ); |
348 | | |
349 | | /* Calculate the hash values for the 64-bit values |
350 | | */ |
351 | 65.7k | libhmac_sha512_context_transform_unfolded_calculate_hash_values( |
352 | 65.7k | values_64bit, |
353 | 65.7k | hash_values, |
354 | 65.7k | s0, |
355 | 65.7k | s1, |
356 | 65.7k | t1, |
357 | 65.7k | t2 ); |
358 | | |
359 | | /* Update the hash values in the context |
360 | | */ |
361 | 65.7k | internal_context->hash_values[ 0 ] += hash_values[ 0 ]; |
362 | 65.7k | internal_context->hash_values[ 1 ] += hash_values[ 1 ]; |
363 | 65.7k | internal_context->hash_values[ 2 ] += hash_values[ 2 ]; |
364 | 65.7k | internal_context->hash_values[ 3 ] += hash_values[ 3 ]; |
365 | 65.7k | internal_context->hash_values[ 4 ] += hash_values[ 4 ]; |
366 | 65.7k | internal_context->hash_values[ 5 ] += hash_values[ 5 ]; |
367 | 65.7k | internal_context->hash_values[ 6 ] += hash_values[ 6 ]; |
368 | 65.7k | internal_context->hash_values[ 7 ] += hash_values[ 7 ]; |
369 | | |
370 | | #else |
371 | | /* Break the block into 16 x 64-bit values |
372 | | */ |
373 | | for( value_64bit_index = 0; |
374 | | value_64bit_index < 16; |
375 | | value_64bit_index++ ) |
376 | | { |
377 | | byte_stream_copy_to_uint64_big_endian( |
378 | | &( buffer[ buffer_offset ] ), |
379 | | values_64bit[ value_64bit_index ] ); |
380 | | |
381 | | buffer_offset += sizeof( uint64_t ); |
382 | | } |
383 | | /* Extend to 80 x 64-bit values |
384 | | */ |
385 | | for( value_64bit_index = 16; |
386 | | value_64bit_index < 80; |
387 | | value_64bit_index++ ) |
388 | | { |
389 | | libhmac_sha512_context_transform_extend_64bit_value( |
390 | | values_64bit, |
391 | | value_64bit_index, |
392 | | s0, |
393 | | s1 ); |
394 | | } |
395 | | /* Calculate the hash values for the 64-bit values |
396 | | */ |
397 | | for( value_64bit_index = 0; |
398 | | value_64bit_index < 80; |
399 | | value_64bit_index++ ) |
400 | | { |
401 | | s0 = byte_stream_bit_rotate_right_64bit( hash_values[ 0 ], 28 ) |
402 | | ^ byte_stream_bit_rotate_right_64bit( hash_values[ 0 ], 34 ) |
403 | | ^ byte_stream_bit_rotate_right_64bit( hash_values[ 0 ], 39 ); |
404 | | s1 = byte_stream_bit_rotate_right_64bit( hash_values[ 4 ], 14 ) |
405 | | ^ byte_stream_bit_rotate_right_64bit( hash_values[ 4 ], 18 ) |
406 | | ^ byte_stream_bit_rotate_right_64bit( hash_values[ 4 ], 41 ); |
407 | | |
408 | | t1 = hash_values[ 7 ]; |
409 | | t1 += s1; |
410 | | t1 += ( hash_values[ 4 ] & hash_values[ 5 ] ) |
411 | | ^ ( ~( hash_values[ 4 ] ) & hash_values[ 6 ] ); |
412 | | t1 += libhmac_sha512_context_prime_cube_roots[ value_64bit_index ]; |
413 | | t1 += values_64bit[ value_64bit_index ]; |
414 | | t2 = s0; |
415 | | t2 += ( hash_values[ 0 ] & hash_values[ 1 ] ) |
416 | | ^ ( hash_values[ 0 ] & hash_values[ 2 ] ) |
417 | | ^ ( hash_values[ 1 ] & hash_values[ 2 ] ); |
418 | | |
419 | | hash_values[ 7 ] = hash_values[ 6 ]; |
420 | | hash_values[ 6 ] = hash_values[ 5 ]; |
421 | | hash_values[ 5 ] = hash_values[ 4 ]; |
422 | | hash_values[ 4 ] = hash_values[ 3 ] + t1; |
423 | | hash_values[ 3 ] = hash_values[ 2 ]; |
424 | | hash_values[ 2 ] = hash_values[ 1 ]; |
425 | | hash_values[ 1 ] = hash_values[ 0 ]; |
426 | | hash_values[ 0 ] = t1 + t2; |
427 | | } |
428 | | /* Update the hash values in the context |
429 | | */ |
430 | | for( hash_values_index = 0; |
431 | | hash_values_index < 8; |
432 | | hash_values_index++ ) |
433 | | { |
434 | | internal_context->hash_values[ hash_values_index ] += hash_values[ hash_values_index ]; |
435 | | } |
436 | | #endif /* defined( LIBHMAC_UNFOLLED_LOOPS ) */ |
437 | | |
438 | 65.7k | size -= LIBHMAC_SHA512_BLOCK_SIZE; |
439 | 65.7k | } |
440 | | /* Prevent sensitive data from leaking |
441 | | */ |
442 | 122 | if( memory_set( |
443 | 122 | hash_values, |
444 | 122 | 0, |
445 | 122 | sizeof( uint64_t ) * 8 ) == NULL ) |
446 | 0 | { |
447 | 0 | libcerror_error_set( |
448 | 0 | error, |
449 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
450 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
451 | 0 | "%s: unable to clear hash values.", |
452 | 0 | function ); |
453 | |
|
454 | 0 | goto on_error; |
455 | 0 | } |
456 | 122 | if( memory_set( |
457 | 122 | values_64bit, |
458 | 122 | 0, |
459 | 122 | sizeof( uint64_t ) * 80 ) == NULL ) |
460 | 0 | { |
461 | 0 | libcerror_error_set( |
462 | 0 | error, |
463 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
464 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
465 | 0 | "%s: unable to clear 64-bit values.", |
466 | 0 | function ); |
467 | |
|
468 | 0 | goto on_error; |
469 | 0 | } |
470 | 122 | return( (ssize_t) buffer_offset ); |
471 | | |
472 | 0 | on_error: |
473 | 0 | memory_set( |
474 | 0 | values_64bit, |
475 | 0 | 0, |
476 | 0 | sizeof( uint64_t ) * 80 ); |
477 | |
|
478 | 0 | memory_set( |
479 | 0 | hash_values, |
480 | 0 | 0, |
481 | 0 | sizeof( uint64_t ) * 8 ); |
482 | |
|
483 | 0 | return( -1 ); |
484 | 122 | } |
485 | | |
486 | | #endif /* !defined( LIBHMAC_HAVE_SHA512_SUPPORT ) */ |
487 | | |
488 | | /* Creates a SHA-512 context |
489 | | * Make sure the value context is referencing, is set to NULL |
490 | | * Returns 1 if successful or -1 on error |
491 | | */ |
492 | | int libhmac_sha512_context_initialize( |
493 | | libhmac_sha512_context_t **context, |
494 | | libcerror_error_t **error ) |
495 | 61 | { |
496 | 61 | libhmac_internal_sha512_context_t *internal_context = NULL; |
497 | 61 | static char *function = "libhmac_sha512_context_initialize"; |
498 | | |
499 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA512 ) |
500 | | const EVP_MD *evp_md_type = NULL; |
501 | | #endif |
502 | | |
503 | 61 | if( context == NULL ) |
504 | 0 | { |
505 | 0 | libcerror_error_set( |
506 | 0 | error, |
507 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
508 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
509 | 0 | "%s: invalid context.", |
510 | 0 | function ); |
511 | |
|
512 | 0 | return( -1 ); |
513 | 0 | } |
514 | 61 | if( *context != NULL ) |
515 | 0 | { |
516 | 0 | libcerror_error_set( |
517 | 0 | error, |
518 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
519 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
520 | 0 | "%s: invalid context value already set.", |
521 | 0 | function ); |
522 | |
|
523 | 0 | return( -1 ); |
524 | 0 | } |
525 | 61 | internal_context = memory_allocate_structure( |
526 | 61 | libhmac_internal_sha512_context_t ); |
527 | | |
528 | 61 | if( internal_context == NULL ) |
529 | 0 | { |
530 | 0 | libcerror_error_set( |
531 | 0 | error, |
532 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
533 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
534 | 0 | "%s: unable to create context.", |
535 | 0 | function ); |
536 | |
|
537 | 0 | goto on_error; |
538 | 0 | } |
539 | 61 | if( memory_set( |
540 | 61 | internal_context, |
541 | 61 | 0, |
542 | 61 | sizeof( libhmac_internal_sha512_context_t ) ) == NULL ) |
543 | 0 | { |
544 | 0 | libcerror_error_set( |
545 | 0 | error, |
546 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
547 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
548 | 0 | "%s: unable to clear context.", |
549 | 0 | function ); |
550 | |
|
551 | 0 | memory_free( |
552 | 0 | internal_context ); |
553 | |
|
554 | 0 | return( -1 ); |
555 | 0 | } |
556 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA512_DIGEST_LENGTH ) |
557 | | if( SHA512_Init( |
558 | | &( internal_context->sha512_context ) ) != 1 ) |
559 | | { |
560 | | libcerror_error_set( |
561 | | error, |
562 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
563 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
564 | | "%s: unable to initialize context.", |
565 | | function ); |
566 | | |
567 | | goto on_error; |
568 | | } |
569 | | |
570 | | #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA512 ) |
571 | | #if defined( HAVE_EVP_MD_CTX_INIT ) |
572 | | EVP_MD_CTX_init( |
573 | | &( internal_context->internal_evp_md_context ) ); |
574 | | |
575 | | internal_context->evp_md_context = &( internal_context->internal_evp_md_context ); |
576 | | #else |
577 | | internal_context->evp_md_context = EVP_MD_CTX_new(); |
578 | | |
579 | | if( internal_context->evp_md_context == NULL ) |
580 | | { |
581 | | libcerror_error_set( |
582 | | error, |
583 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
584 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
585 | | "%s: unable to create EVP message digest context.", |
586 | | function ); |
587 | | |
588 | | goto on_error; |
589 | | } |
590 | | #endif /* defined( HAVE_EVP_MD_CTX_INIT ) */ |
591 | | |
592 | | /* TODO use EVP_MD_fetch for EVP_DigestInit_ex2 */ |
593 | | evp_md_type = EVP_sha512(); |
594 | | |
595 | | #if defined( HAVE_EVP_DIGESTINIT_EX2 ) |
596 | | if( EVP_DigestInit_ex2( |
597 | | internal_context->evp_md_context, |
598 | | evp_md_type, |
599 | | NULL ) != 1 ) |
600 | | #else |
601 | | if( EVP_DigestInit_ex( |
602 | | internal_context->evp_md_context, |
603 | | evp_md_type, |
604 | | NULL ) != 1 ) |
605 | | #endif |
606 | | { |
607 | | libcerror_error_set( |
608 | | error, |
609 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
610 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
611 | | "%s: unable to initialize context.", |
612 | | function ); |
613 | | |
614 | | #if defined( HAVE_EVP_MD_CTX_CLEANUP ) |
615 | | EVP_MD_CTX_cleanup( |
616 | | &( internal_context->internal_evp_md_context ) ); |
617 | | ERR_remove_thread_state( |
618 | | NULL ); |
619 | | #else |
620 | | EVP_MD_CTX_free( |
621 | | internal_context->evp_md_context ); |
622 | | #endif |
623 | | internal_context->evp_md_context = NULL; |
624 | | |
625 | | goto on_error; |
626 | | } |
627 | | #else |
628 | 61 | if( memory_copy( |
629 | 61 | internal_context->hash_values, |
630 | 61 | libhmac_sha512_context_prime_square_roots, |
631 | 61 | sizeof( uint64_t ) * 8 ) == NULL ) |
632 | 0 | { |
633 | 0 | libcerror_error_set( |
634 | 0 | error, |
635 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
636 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
637 | 0 | "%s: unable to copy initial hash values.", |
638 | 0 | function ); |
639 | |
|
640 | 0 | return( -1 ); |
641 | 0 | } |
642 | 61 | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA512_DIGEST_LENGTH ) */ |
643 | | |
644 | 61 | *context = (libhmac_sha512_context_t *) internal_context; |
645 | | |
646 | 61 | return( 1 ); |
647 | | |
648 | 0 | on_error: |
649 | 0 | if( internal_context != NULL ) |
650 | 0 | { |
651 | 0 | memory_free( |
652 | 0 | internal_context ); |
653 | 0 | } |
654 | 0 | return( -1 ); |
655 | 61 | } |
656 | | |
657 | | /* Frees a SHA-512 context |
658 | | * Returns 1 if successful or -1 on error |
659 | | */ |
660 | | int libhmac_sha512_context_free( |
661 | | libhmac_sha512_context_t **context, |
662 | | libcerror_error_t **error ) |
663 | 61 | { |
664 | 61 | libhmac_internal_sha512_context_t *internal_context = NULL; |
665 | 61 | static char *function = "libhmac_sha512_context_free"; |
666 | | |
667 | 61 | if( context == NULL ) |
668 | 0 | { |
669 | 0 | libcerror_error_set( |
670 | 0 | error, |
671 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
672 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
673 | 0 | "%s: invalid context.", |
674 | 0 | function ); |
675 | |
|
676 | 0 | return( -1 ); |
677 | 0 | } |
678 | 61 | if( *context != NULL ) |
679 | 61 | { |
680 | 61 | internal_context = (libhmac_internal_sha512_context_t *) *context; |
681 | 61 | *context = NULL; |
682 | | |
683 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA512_DIGEST_LENGTH ) |
684 | | /* No additional clean up necessary |
685 | | */ |
686 | | |
687 | | #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA512 ) |
688 | | #if defined( HAVE_EVP_MD_CTX_CLEANUP ) |
689 | | if( EVP_MD_CTX_cleanup( |
690 | | &( internal_context->internal_evp_md_context ) ) != 1 ) |
691 | | { |
692 | | libcerror_error_set( |
693 | | error, |
694 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
695 | | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
696 | | "%s: unable to clean up EVP message digest context.", |
697 | | function ); |
698 | | } |
699 | | /* Make sure the error state is removed otherwise OpenSSL will leak memory |
700 | | */ |
701 | | ERR_remove_thread_state( |
702 | | NULL ); |
703 | | #else |
704 | | EVP_MD_CTX_free( |
705 | | internal_context->evp_md_context ); |
706 | | |
707 | | #endif /* defined( HAVE_EVP_MD_CTX_CLEANUP ) */ |
708 | | |
709 | | internal_context->evp_md_context = NULL; |
710 | | #else |
711 | | /* No additional clean up necessary |
712 | | */ |
713 | 61 | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA512_DIGEST_LENGTH ) */ |
714 | | |
715 | 61 | memory_free( |
716 | 61 | internal_context ); |
717 | 61 | } |
718 | 61 | return( 1 ); |
719 | 61 | } |
720 | | |
721 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA512_DIGEST_LENGTH ) |
722 | | |
723 | | /* Updates the SHA-512 context using OpenSSL |
724 | | * Returns 1 if successful or -1 on error |
725 | | */ |
726 | | int libhmac_sha512_context_update( |
727 | | libhmac_sha512_context_t *context, |
728 | | const uint8_t *buffer, |
729 | | size_t size, |
730 | | libcerror_error_t **error ) |
731 | | { |
732 | | libhmac_internal_sha512_context_t *internal_context = NULL; |
733 | | static char *function = "libhmac_sha512_context_update"; |
734 | | unsigned long safe_hash_size = 0; |
735 | | |
736 | | if( context == NULL ) |
737 | | { |
738 | | libcerror_error_set( |
739 | | error, |
740 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
741 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
742 | | "%s: invalid context.", |
743 | | function ); |
744 | | |
745 | | return( -1 ); |
746 | | } |
747 | | internal_context = (libhmac_internal_sha512_context_t *) context; |
748 | | |
749 | | if( buffer == NULL ) |
750 | | { |
751 | | libcerror_error_set( |
752 | | error, |
753 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
754 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
755 | | "%s: invalid buffer.", |
756 | | function ); |
757 | | |
758 | | return( -1 ); |
759 | | } |
760 | | #if ( SIZEOF_LONG < SIZEOF_SIZE_T ) |
761 | | if( size > (size_t) ULONG_MAX ) |
762 | | #else |
763 | | if( size > (size_t) SSIZE_MAX ) |
764 | | #endif |
765 | | { |
766 | | libcerror_error_set( |
767 | | error, |
768 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
769 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
770 | | "%s: invalid size value exceeds maximum.", |
771 | | function ); |
772 | | |
773 | | return( -1 ); |
774 | | } |
775 | | if( size == 0 ) |
776 | | { |
777 | | return( 1 ); |
778 | | } |
779 | | safe_hash_size = (unsigned long) size; |
780 | | |
781 | | if( SHA512_Update( |
782 | | &( internal_context->sha512_context ), |
783 | | (const void *) buffer, |
784 | | size ) != 1 ) |
785 | | { |
786 | | libcerror_error_set( |
787 | | error, |
788 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
789 | | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
790 | | "%s: unable to update context.", |
791 | | function ); |
792 | | |
793 | | return( -1 ); |
794 | | } |
795 | | return( 1 ); |
796 | | } |
797 | | |
798 | | #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA512 ) |
799 | | |
800 | | /* Updates the SHA-512 context using OpenSSL EVP |
801 | | * Returns 1 if successful or -1 on error |
802 | | */ |
803 | | int libhmac_sha512_context_update( |
804 | | libhmac_sha512_context_t *context, |
805 | | const uint8_t *buffer, |
806 | | size_t size, |
807 | | libcerror_error_t **error ) |
808 | | { |
809 | | libhmac_internal_sha512_context_t *internal_context = NULL; |
810 | | static char *function = "libhmac_sha512_context_update"; |
811 | | |
812 | | if( context == NULL ) |
813 | | { |
814 | | libcerror_error_set( |
815 | | error, |
816 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
817 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
818 | | "%s: invalid context.", |
819 | | function ); |
820 | | |
821 | | return( -1 ); |
822 | | } |
823 | | internal_context = (libhmac_internal_sha512_context_t *) context; |
824 | | |
825 | | if( buffer == NULL ) |
826 | | { |
827 | | libcerror_error_set( |
828 | | error, |
829 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
830 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
831 | | "%s: invalid buffer.", |
832 | | function ); |
833 | | |
834 | | return( -1 ); |
835 | | } |
836 | | if( size > (size_t) SSIZE_MAX ) |
837 | | { |
838 | | libcerror_error_set( |
839 | | error, |
840 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
841 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
842 | | "%s: invalid size value exceeds maximum.", |
843 | | function ); |
844 | | |
845 | | return( -1 ); |
846 | | } |
847 | | if( size == 0 ) |
848 | | { |
849 | | return( 1 ); |
850 | | } |
851 | | if( EVP_DigestUpdate( |
852 | | internal_context->evp_md_context, |
853 | | (const void *) buffer, |
854 | | size ) != 1 ) |
855 | | { |
856 | | libcerror_error_set( |
857 | | error, |
858 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
859 | | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
860 | | "%s: unable to update context.", |
861 | | function ); |
862 | | |
863 | | return( -1 ); |
864 | | } |
865 | | return( 1 ); |
866 | | } |
867 | | |
868 | | #else |
869 | | |
870 | | /* Updates the SHA-512 context using fallback implementation |
871 | | * Returns 1 if successful or -1 on error |
872 | | */ |
873 | | int libhmac_sha512_context_update( |
874 | | libhmac_sha512_context_t *context, |
875 | | const uint8_t *buffer, |
876 | | size_t size, |
877 | | libcerror_error_t **error ) |
878 | 61 | { |
879 | 61 | libhmac_internal_sha512_context_t *internal_context = NULL; |
880 | 61 | static char *function = "libhmac_sha512_context_update"; |
881 | 61 | size_t buffer_offset = 0; |
882 | 61 | size_t remaining_block_size = 0; |
883 | 61 | ssize_t process_count = 0; |
884 | | |
885 | 61 | if( context == NULL ) |
886 | 0 | { |
887 | 0 | libcerror_error_set( |
888 | 0 | error, |
889 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
890 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
891 | 0 | "%s: invalid context.", |
892 | 0 | function ); |
893 | |
|
894 | 0 | return( -1 ); |
895 | 0 | } |
896 | 61 | internal_context = (libhmac_internal_sha512_context_t *) context; |
897 | | |
898 | 61 | if( buffer == NULL ) |
899 | 0 | { |
900 | 0 | libcerror_error_set( |
901 | 0 | error, |
902 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
903 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
904 | 0 | "%s: invalid buffer.", |
905 | 0 | function ); |
906 | |
|
907 | 0 | return( -1 ); |
908 | 0 | } |
909 | 61 | if( size > (size_t) SSIZE_MAX ) |
910 | 0 | { |
911 | 0 | libcerror_error_set( |
912 | 0 | error, |
913 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
914 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
915 | 0 | "%s: invalid size value exceeds maximum.", |
916 | 0 | function ); |
917 | |
|
918 | 0 | return( -1 ); |
919 | 0 | } |
920 | 61 | if( size == 0 ) |
921 | 0 | { |
922 | 0 | return( 1 ); |
923 | 0 | } |
924 | 61 | if( internal_context->block_offset > 0 ) |
925 | 0 | { |
926 | 0 | if( internal_context->block_offset >= LIBHMAC_SHA512_BLOCK_SIZE ) |
927 | 0 | { |
928 | 0 | libcerror_error_set( |
929 | 0 | error, |
930 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
931 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
932 | 0 | "%s: invalid context - block offset value out of bounds.", |
933 | 0 | function ); |
934 | |
|
935 | 0 | return( -1 ); |
936 | 0 | } |
937 | 0 | remaining_block_size = LIBHMAC_SHA512_BLOCK_SIZE - internal_context->block_offset; |
938 | |
|
939 | 0 | if( remaining_block_size > size ) |
940 | 0 | { |
941 | 0 | remaining_block_size = size; |
942 | 0 | } |
943 | 0 | if( memory_copy( |
944 | 0 | &( internal_context->block[ internal_context->block_offset ] ), |
945 | 0 | buffer, |
946 | 0 | remaining_block_size ) == NULL ) |
947 | 0 | { |
948 | 0 | libcerror_error_set( |
949 | 0 | error, |
950 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
951 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
952 | 0 | "%s: unable to copy data to context block.", |
953 | 0 | function ); |
954 | |
|
955 | 0 | return( -1 ); |
956 | 0 | } |
957 | 0 | internal_context->block_offset += remaining_block_size; |
958 | |
|
959 | 0 | if( internal_context->block_offset < LIBHMAC_SHA512_BLOCK_SIZE ) |
960 | 0 | { |
961 | 0 | return( 1 ); |
962 | 0 | } |
963 | 0 | buffer_offset += remaining_block_size; |
964 | 0 | size -= remaining_block_size; |
965 | |
|
966 | 0 | process_count = libhmac_sha512_context_transform( |
967 | 0 | internal_context, |
968 | 0 | internal_context->block, |
969 | 0 | LIBHMAC_SHA512_BLOCK_SIZE, |
970 | 0 | error ); |
971 | |
|
972 | 0 | if( process_count == -1 ) |
973 | 0 | { |
974 | 0 | libcerror_error_set( |
975 | 0 | error, |
976 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
977 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
978 | 0 | "%s: unable to transform context block.", |
979 | 0 | function ); |
980 | |
|
981 | 0 | return( -1 ); |
982 | 0 | } |
983 | 0 | internal_context->hash_count += process_count; |
984 | 0 | internal_context->block_offset = 0; |
985 | 0 | } |
986 | 61 | if( size > 0 ) |
987 | 61 | { |
988 | 61 | process_count = libhmac_sha512_context_transform( |
989 | 61 | internal_context, |
990 | 61 | &( buffer[ buffer_offset ] ), |
991 | 61 | size, |
992 | 61 | error ); |
993 | | |
994 | 61 | if( process_count == -1 ) |
995 | 0 | { |
996 | 0 | libcerror_error_set( |
997 | 0 | error, |
998 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
999 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
1000 | 0 | "%s: unable to transform buffer.", |
1001 | 0 | function ); |
1002 | |
|
1003 | 0 | return( -1 ); |
1004 | 0 | } |
1005 | 61 | internal_context->hash_count += process_count; |
1006 | | |
1007 | 61 | buffer_offset += process_count; |
1008 | 61 | size -= process_count; |
1009 | 61 | } |
1010 | 61 | if( size > 0 ) |
1011 | 44 | { |
1012 | 44 | if( size >= LIBHMAC_SHA512_BLOCK_SIZE ) |
1013 | 0 | { |
1014 | 0 | libcerror_error_set( |
1015 | 0 | error, |
1016 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1017 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1018 | 0 | "%s: invalid size value out of bounds.", |
1019 | 0 | function ); |
1020 | |
|
1021 | 0 | return( -1 ); |
1022 | 0 | } |
1023 | 44 | if( memory_copy( |
1024 | 44 | internal_context->block, |
1025 | 44 | &( buffer[ buffer_offset ] ), |
1026 | 44 | size ) == NULL ) |
1027 | 0 | { |
1028 | 0 | libcerror_error_set( |
1029 | 0 | error, |
1030 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1031 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1032 | 0 | "%s: unable to copy remaining data to context block.", |
1033 | 0 | function ); |
1034 | |
|
1035 | 0 | return( -1 ); |
1036 | 0 | } |
1037 | 44 | internal_context->block_offset = size; |
1038 | 44 | } |
1039 | 61 | return( 1 ); |
1040 | 61 | } |
1041 | | |
1042 | | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA512_DIGEST_LENGTH ) */ |
1043 | | |
1044 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA512_DIGEST_LENGTH ) |
1045 | | |
1046 | | /* Finalizes the SHA-512 context using OpenSSL |
1047 | | * Returns 1 if successful or -1 on error |
1048 | | */ |
1049 | | int libhmac_sha512_context_finalize( |
1050 | | libhmac_sha512_context_t *context, |
1051 | | uint8_t *hash, |
1052 | | size_t hash_size, |
1053 | | libcerror_error_t **error ) |
1054 | | { |
1055 | | libhmac_internal_sha512_context_t *internal_context = NULL; |
1056 | | static char *function = "libhmac_sha512_context_finalize"; |
1057 | | |
1058 | | if( context == NULL ) |
1059 | | { |
1060 | | libcerror_error_set( |
1061 | | error, |
1062 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1063 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1064 | | "%s: invalid context.", |
1065 | | function ); |
1066 | | |
1067 | | return( -1 ); |
1068 | | } |
1069 | | internal_context = (libhmac_internal_sha512_context_t *) context; |
1070 | | |
1071 | | if( hash == NULL ) |
1072 | | { |
1073 | | libcerror_error_set( |
1074 | | error, |
1075 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1076 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1077 | | "%s: invalid hash.", |
1078 | | function ); |
1079 | | |
1080 | | return( -1 ); |
1081 | | } |
1082 | | if( hash_size > (size_t) SSIZE_MAX ) |
1083 | | { |
1084 | | libcerror_error_set( |
1085 | | error, |
1086 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1087 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1088 | | "%s: invalid hash size value exceeds maximum.", |
1089 | | function ); |
1090 | | |
1091 | | return( -1 ); |
1092 | | } |
1093 | | if( hash_size < (size_t) LIBHMAC_SHA512_HASH_SIZE ) |
1094 | | { |
1095 | | libcerror_error_set( |
1096 | | error, |
1097 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1098 | | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1099 | | "%s: invalid hash size value too small.", |
1100 | | function ); |
1101 | | |
1102 | | return( -1 ); |
1103 | | } |
1104 | | if( SHA512_Final( |
1105 | | hash, |
1106 | | &( internal_context->sha512_context ) ) != 1 ) |
1107 | | { |
1108 | | libcerror_error_set( |
1109 | | error, |
1110 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1111 | | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
1112 | | "%s: unable to finalize context.", |
1113 | | function ); |
1114 | | |
1115 | | return( -1 ); |
1116 | | } |
1117 | | return( 1 ); |
1118 | | } |
1119 | | |
1120 | | #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA512 ) |
1121 | | |
1122 | | /* Finalizes the SHA-512 context using OpenSSL EVP |
1123 | | * Returns 1 if successful or -1 on error |
1124 | | */ |
1125 | | int libhmac_sha512_context_finalize( |
1126 | | libhmac_sha512_context_t *context, |
1127 | | uint8_t *hash, |
1128 | | size_t hash_size, |
1129 | | libcerror_error_t **error ) |
1130 | | { |
1131 | | libhmac_internal_sha512_context_t *internal_context = NULL; |
1132 | | static char *function = "libhmac_sha512_context_finalize"; |
1133 | | unsigned int safe_hash_size = 0; |
1134 | | |
1135 | | if( context == NULL ) |
1136 | | { |
1137 | | libcerror_error_set( |
1138 | | error, |
1139 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1140 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1141 | | "%s: invalid context.", |
1142 | | function ); |
1143 | | |
1144 | | return( -1 ); |
1145 | | } |
1146 | | internal_context = (libhmac_internal_sha512_context_t *) context; |
1147 | | |
1148 | | if( hash == NULL ) |
1149 | | { |
1150 | | libcerror_error_set( |
1151 | | error, |
1152 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1153 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1154 | | "%s: invalid hash.", |
1155 | | function ); |
1156 | | |
1157 | | return( -1 ); |
1158 | | } |
1159 | | if( ( hash_size < (size_t) LIBHMAC_SHA512_HASH_SIZE ) |
1160 | | || ( hash_size > (size_t) UINT_MAX ) ) |
1161 | | { |
1162 | | libcerror_error_set( |
1163 | | error, |
1164 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1165 | | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
1166 | | "%s: invalid hash size value out of bounds.", |
1167 | | function ); |
1168 | | |
1169 | | return( -1 ); |
1170 | | } |
1171 | | safe_hash_size = (unsigned int) hash_size; |
1172 | | |
1173 | | if( EVP_DigestFinal_ex( |
1174 | | internal_context->evp_md_context, |
1175 | | (unsigned char *) hash, |
1176 | | &safe_hash_size ) != 1 ) |
1177 | | { |
1178 | | libcerror_error_set( |
1179 | | error, |
1180 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1181 | | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
1182 | | "%s: unable to finalize context.", |
1183 | | function ); |
1184 | | |
1185 | | return( -1 ); |
1186 | | } |
1187 | | return( 1 ); |
1188 | | } |
1189 | | |
1190 | | #else |
1191 | | |
1192 | | /* Finalizes the SHA-512 context using fallback implementation |
1193 | | * Returns 1 if successful or -1 on error |
1194 | | */ |
1195 | | int libhmac_sha512_context_finalize( |
1196 | | libhmac_sha512_context_t *context, |
1197 | | uint8_t *hash, |
1198 | | size_t hash_size, |
1199 | | libcerror_error_t **error ) |
1200 | 61 | { |
1201 | 61 | libhmac_internal_sha512_context_t *internal_context = NULL; |
1202 | 61 | static char *function = "libhmac_sha512_context_finalize"; |
1203 | 61 | size_t block_size = 0; |
1204 | 61 | size_t number_of_blocks = 0; |
1205 | 61 | ssize_t process_count = 0; |
1206 | 61 | uint64_t bit_size = 0; |
1207 | | |
1208 | | #if !defined( LIBHMAC_UNFOLLED_LOOPS ) |
1209 | | size_t hash_index = 0; |
1210 | | int hash_values_index = 0; |
1211 | | #endif |
1212 | | |
1213 | 61 | if( context == NULL ) |
1214 | 0 | { |
1215 | 0 | libcerror_error_set( |
1216 | 0 | error, |
1217 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1218 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1219 | 0 | "%s: invalid context.", |
1220 | 0 | function ); |
1221 | |
|
1222 | 0 | return( -1 ); |
1223 | 0 | } |
1224 | 61 | internal_context = (libhmac_internal_sha512_context_t *) context; |
1225 | | |
1226 | 61 | if( hash == NULL ) |
1227 | 0 | { |
1228 | 0 | libcerror_error_set( |
1229 | 0 | error, |
1230 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1231 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1232 | 0 | "%s: invalid hash.", |
1233 | 0 | function ); |
1234 | |
|
1235 | 0 | return( -1 ); |
1236 | 0 | } |
1237 | 61 | if( hash_size < (size_t) LIBHMAC_SHA512_HASH_SIZE ) |
1238 | 0 | { |
1239 | 0 | libcerror_error_set( |
1240 | 0 | error, |
1241 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1242 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1243 | 0 | "%s: invalid hash value too small.", |
1244 | 0 | function ); |
1245 | |
|
1246 | 0 | return( -1 ); |
1247 | 0 | } |
1248 | 61 | if( hash_size > (size_t) SSIZE_MAX ) |
1249 | 0 | { |
1250 | 0 | libcerror_error_set( |
1251 | 0 | error, |
1252 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1253 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1254 | 0 | "%s: invalid hash size value exceeds maximum.", |
1255 | 0 | function ); |
1256 | |
|
1257 | 0 | return( -1 ); |
1258 | 0 | } |
1259 | 61 | if( hash_size < (size_t) LIBHMAC_SHA512_HASH_SIZE ) |
1260 | 0 | { |
1261 | 0 | libcerror_error_set( |
1262 | 0 | error, |
1263 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1264 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1265 | 0 | "%s: invalid hash size value too small.", |
1266 | 0 | function ); |
1267 | |
|
1268 | 0 | return( -1 ); |
1269 | 0 | } |
1270 | | /* Add padding with a size of 120 mod 128 |
1271 | | */ |
1272 | 61 | number_of_blocks = 1; |
1273 | | |
1274 | 61 | if( internal_context->block_offset > 119 ) |
1275 | 24 | { |
1276 | 24 | number_of_blocks += 1; |
1277 | 24 | } |
1278 | 61 | block_size = number_of_blocks * LIBHMAC_SHA512_BLOCK_SIZE; |
1279 | | |
1280 | 61 | if( memory_set( |
1281 | 61 | &( internal_context->block[ internal_context->block_offset ] ), |
1282 | 61 | 0, |
1283 | 61 | block_size - internal_context->block_offset ) == NULL ) |
1284 | 0 | { |
1285 | 0 | libcerror_error_set( |
1286 | 0 | error, |
1287 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1288 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
1289 | 0 | "%s: unable to clear context block.", |
1290 | 0 | function ); |
1291 | |
|
1292 | 0 | return( -1 ); |
1293 | 0 | } |
1294 | | /* The first byte of the padding contains 0x80 |
1295 | | */ |
1296 | 61 | internal_context->block[ internal_context->block_offset ] = 0x80; |
1297 | | |
1298 | 61 | bit_size = ( internal_context->hash_count + internal_context->block_offset ) * 8; |
1299 | | |
1300 | 61 | byte_stream_copy_from_uint64_big_endian( |
1301 | 61 | &( internal_context->block[ block_size - 8 ] ), |
1302 | 61 | bit_size ); |
1303 | | |
1304 | 61 | process_count = libhmac_sha512_context_transform( |
1305 | 61 | internal_context, |
1306 | 61 | internal_context->block, |
1307 | 61 | block_size, |
1308 | 61 | error ); |
1309 | | |
1310 | 61 | if( process_count == -1 ) |
1311 | 0 | { |
1312 | 0 | libcerror_error_set( |
1313 | 0 | error, |
1314 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1315 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
1316 | 0 | "%s: unable to transform context block.", |
1317 | 0 | function ); |
1318 | |
|
1319 | 0 | return( -1 ); |
1320 | 0 | } |
1321 | | #if !defined( LIBHMAC_UNFOLLED_LOOPS ) |
1322 | | for( hash_values_index = 0; |
1323 | | hash_values_index < 8; |
1324 | | hash_values_index++ ) |
1325 | | { |
1326 | | byte_stream_copy_from_uint64_big_endian( |
1327 | | &( hash[ hash_index ] ), |
1328 | | internal_context->hash_values[ hash_values_index ] ); |
1329 | | |
1330 | | hash_index += sizeof( uint64_t ); |
1331 | | } |
1332 | | #else |
1333 | 61 | byte_stream_copy_from_uint64_big_endian( |
1334 | 61 | &( hash[ 0 ] ), |
1335 | 61 | internal_context->hash_values[ 0 ] ); |
1336 | | |
1337 | 61 | byte_stream_copy_from_uint64_big_endian( |
1338 | 61 | &( hash[ 8 ] ), |
1339 | 61 | internal_context->hash_values[ 1 ] ); |
1340 | | |
1341 | 61 | byte_stream_copy_from_uint64_big_endian( |
1342 | 61 | &( hash[ 16 ] ), |
1343 | 61 | internal_context->hash_values[ 2 ] ); |
1344 | | |
1345 | 61 | byte_stream_copy_from_uint64_big_endian( |
1346 | 61 | &( hash[ 24 ] ), |
1347 | 61 | internal_context->hash_values[ 3 ] ); |
1348 | | |
1349 | 61 | byte_stream_copy_from_uint64_big_endian( |
1350 | 61 | &( hash[ 32 ] ), |
1351 | 61 | internal_context->hash_values[ 4 ] ); |
1352 | | |
1353 | 61 | byte_stream_copy_from_uint64_big_endian( |
1354 | 61 | &( hash[ 40 ] ), |
1355 | 61 | internal_context->hash_values[ 5 ] ); |
1356 | | |
1357 | 61 | byte_stream_copy_from_uint64_big_endian( |
1358 | 61 | &( hash[ 48 ] ), |
1359 | 61 | internal_context->hash_values[ 6 ] ); |
1360 | | |
1361 | 61 | byte_stream_copy_from_uint64_big_endian( |
1362 | 61 | &( hash[ 56 ] ), |
1363 | 61 | internal_context->hash_values[ 7 ] ); |
1364 | | |
1365 | 61 | #endif /* !defined( LIBHMAC_UNFOLLED_LOOPS ) */ |
1366 | | |
1367 | | /* Prevent sensitive data from leaking |
1368 | | */ |
1369 | 61 | if( memory_set( |
1370 | 61 | internal_context, |
1371 | 61 | 0, |
1372 | 61 | sizeof( libhmac_internal_sha512_context_t ) ) == NULL ) |
1373 | 0 | { |
1374 | 0 | libcerror_error_set( |
1375 | 0 | error, |
1376 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1377 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
1378 | 0 | "%s: unable to clear context.", |
1379 | 0 | function ); |
1380 | |
|
1381 | 0 | return( -1 ); |
1382 | 0 | } |
1383 | 61 | return( 1 ); |
1384 | 61 | } |
1385 | | |
1386 | | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA512_DIGEST_LENGTH ) */ |
1387 | | |