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