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