/src/libbde/libhmac/libhmac_sha256_context.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * SHA-256 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_sha256_context.h" | 
| 39 |  |  | 
| 40 |  | #if !defined( LIBHMAC_HAVE_SHA256_SUPPORT ) | 
| 41 |  |  | 
| 42 |  | /* FIPS 180-2 based SHA-256 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_sha256_context_prime_square_roots[ 8 ] = { | 
| 48 |  |   0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL, | 
| 49 |  |   0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL | 
| 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_sha256_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_sha256_context_transform_extend_32bit_value( values_32bit, value_32bit_index, s0, s1 ) \ | 
| 74 | 5.51M |   s0 = byte_stream_bit_rotate_right_32bit( values_32bit[ value_32bit_index - 15 ], 7 ) \ | 
| 75 | 5.51M |      ^ byte_stream_bit_rotate_right_32bit( values_32bit[ value_32bit_index - 15 ], 18 ) \ | 
| 76 | 5.51M |      ^ ( values_32bit[ value_32bit_index - 15 ] >> 3 ); \ | 
| 77 | 5.51M |   s1 = byte_stream_bit_rotate_right_32bit( values_32bit[ value_32bit_index - 2 ], 17 ) \ | 
| 78 | 5.51M |      ^ byte_stream_bit_rotate_right_32bit( values_32bit[ value_32bit_index - 2 ], 19 ) \ | 
| 79 | 5.51M |      ^ ( values_32bit[ value_32bit_index - 2 ] >> 10 ); \ | 
| 80 | 5.51M | \ | 
| 81 | 5.51M |   values_32bit[ value_32bit_index ] = values_32bit[ value_32bit_index - 16 ] \ | 
| 82 | 5.51M |                                     + s0 \ | 
| 83 | 5.51M |                                     + values_32bit[ value_32bit_index - 7 ] \ | 
| 84 | 5.51M |                                     + s1 | 
| 85 |  |  | 
| 86 |  | #define libhmac_sha256_context_transform_unfolded_extend_32bit_values( values_32bit, s0, s1 ) \ | 
| 87 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 16, s0, s1 ); \ | 
| 88 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 17, s0, s1 ); \ | 
| 89 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 18, s0, s1 ); \ | 
| 90 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 19, s0, s1 ); \ | 
| 91 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 20, s0, s1 ); \ | 
| 92 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 21, s0, s1 ); \ | 
| 93 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 22, s0, s1 ); \ | 
| 94 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 23, s0, s1 ); \ | 
| 95 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 24, s0, s1 ); \ | 
| 96 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 25, s0, s1 ); \ | 
| 97 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 26, s0, s1 ); \ | 
| 98 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 27, s0, s1 ); \ | 
| 99 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 28, s0, s1 ); \ | 
| 100 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 29, s0, s1 ); \ | 
| 101 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 30, s0, s1 ); \ | 
| 102 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 31, s0, s1 ); \ | 
| 103 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 32, s0, s1 ); \ | 
| 104 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 33, s0, s1 ); \ | 
| 105 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 34, s0, s1 ); \ | 
| 106 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 35, s0, s1 ); \ | 
| 107 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 36, s0, s1 ); \ | 
| 108 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 37, s0, s1 ); \ | 
| 109 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 38, s0, s1 ); \ | 
| 110 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 39, s0, s1 ); \ | 
| 111 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 40, s0, s1 ); \ | 
| 112 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 41, s0, s1 ); \ | 
| 113 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 42, s0, s1 ); \ | 
| 114 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 43, s0, s1 ); \ | 
| 115 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 44, s0, s1 ); \ | 
| 116 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 45, s0, s1 ); \ | 
| 117 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 46, s0, s1 ); \ | 
| 118 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 47, s0, s1 ); \ | 
| 119 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 48, s0, s1 ); \ | 
| 120 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 49, s0, s1 ); \ | 
| 121 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 50, s0, s1 ); \ | 
| 122 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 51, s0, s1 ); \ | 
| 123 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 52, s0, s1 ); \ | 
| 124 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 53, s0, s1 ); \ | 
| 125 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 54, s0, s1 ); \ | 
| 126 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 55, s0, s1 ); \ | 
| 127 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 56, s0, s1 ); \ | 
| 128 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 57, s0, s1 ); \ | 
| 129 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 58, s0, s1 ); \ | 
| 130 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 59, s0, s1 ); \ | 
| 131 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 60, s0, s1 ); \ | 
| 132 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 61, s0, s1 ); \ | 
| 133 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 62, s0, s1 ); \ | 
| 134 | 114k |   libhmac_sha256_context_transform_extend_32bit_value( values_32bit, 63, s0, s1 ); | 
| 135 |  |  | 
| 136 |  | #define libhmac_sha256_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 | 7.35M |   s0 = byte_stream_bit_rotate_right_32bit( hash_values[ hash_value_index0 ], 2 ) \ | 
| 138 | 7.35M |      ^ byte_stream_bit_rotate_right_32bit( hash_values[ hash_value_index0 ], 13 ) \ | 
| 139 | 7.35M |      ^ byte_stream_bit_rotate_right_32bit( hash_values[ hash_value_index0 ], 22 ); \ | 
| 140 | 7.35M |   s1 = byte_stream_bit_rotate_right_32bit( hash_values[ hash_value_index4 ], 6 ) \ | 
| 141 | 7.35M |      ^ byte_stream_bit_rotate_right_32bit( hash_values[ hash_value_index4 ], 11 ) \ | 
| 142 | 7.35M |      ^ byte_stream_bit_rotate_right_32bit( hash_values[ hash_value_index4 ], 25 ); \ | 
| 143 | 7.35M | \ | 
| 144 | 7.35M |   t1  = hash_values[ hash_value_index7 ]; \ | 
| 145 | 7.35M |   t1 += s1; \ | 
| 146 | 7.35M |   t1 += ( hash_values[ hash_value_index4 ] & hash_values[ hash_value_index5 ] ) \ | 
| 147 | 7.35M |       ^ ( ~( hash_values[ hash_value_index4 ] ) & hash_values[ hash_value_index6 ] ); \ | 
| 148 | 7.35M |   t1 += libhmac_sha256_context_prime_cube_roots[ value_32bit_index ]; \ | 
| 149 | 7.35M |   t1 += values_32bit[ value_32bit_index ]; \ | 
| 150 | 7.35M |   t2  = s0; \ | 
| 151 | 7.35M |   t2 += ( hash_values[ hash_value_index0 ] & hash_values[ hash_value_index1 ] ) \ | 
| 152 | 7.35M |       ^ ( hash_values[ hash_value_index0 ] & hash_values[ hash_value_index2 ] ) \ | 
| 153 | 7.35M |       ^ ( hash_values[ hash_value_index1 ] & hash_values[ hash_value_index2 ] ); \ | 
| 154 | 7.35M | \ | 
| 155 | 7.35M |   hash_values[ hash_value_index3 ] += t1; \ | 
| 156 | 7.35M |   hash_values[ hash_value_index7 ]  = t1 + t2; | 
| 157 |  |  | 
| 158 |  | #define libhmac_sha256_context_transform_unfolded_calculate_hash_values( values_32bit, hash_values, s0, s1, t1, t2 ) \ | 
| 159 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 0, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ | 
| 160 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 1, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ | 
| 161 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 2, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ | 
| 162 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 3, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ | 
| 163 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 4, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ | 
| 164 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 5, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ | 
| 165 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 6, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ | 
| 166 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 7, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ | 
| 167 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 8, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ | 
| 168 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 9, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ | 
| 169 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 10, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ | 
| 170 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 11, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ | 
| 171 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 12, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ | 
| 172 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 13, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ | 
| 173 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 14, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ | 
| 174 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 15, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ | 
| 175 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 16, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ | 
| 176 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 17, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ | 
| 177 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 18, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ | 
| 178 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 19, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ | 
| 179 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 20, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ | 
| 180 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 21, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ | 
| 181 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 22, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ | 
| 182 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 23, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ | 
| 183 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 24, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ | 
| 184 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 25, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ | 
| 185 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 26, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ | 
| 186 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 27, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ | 
| 187 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 28, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ | 
| 188 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 29, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ | 
| 189 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 30, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ | 
| 190 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 31, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ | 
| 191 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 32, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ | 
| 192 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 33, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ | 
| 193 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 34, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ | 
| 194 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 35, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ | 
| 195 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 36, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ | 
| 196 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 37, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ | 
| 197 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 38, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ | 
| 198 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 39, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ | 
| 199 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 40, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ | 
| 200 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 41, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ | 
| 201 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 42, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ | 
| 202 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 43, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ | 
| 203 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 44, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ | 
| 204 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 45, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ | 
| 205 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 46, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ | 
| 206 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 47, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ | 
| 207 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 48, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ | 
| 208 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 49, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ | 
| 209 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 50, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ | 
| 210 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 51, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ | 
| 211 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 52, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ | 
| 212 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 53, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ | 
| 213 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 54, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ | 
| 214 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 55, hash_values, 1, 2, 3, 4, 5, 6, 7, 0, s0, s1, t1, t2 ) \ | 
| 215 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 56, hash_values, 0, 1, 2, 3, 4, 5, 6, 7, s0, s1, t1, t2 ) \ | 
| 216 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 57, hash_values, 7, 0, 1, 2, 3, 4, 5, 6, s0, s1, t1, t2 ) \ | 
| 217 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 58, hash_values, 6, 7, 0, 1, 2, 3, 4, 5, s0, s1, t1, t2 ) \ | 
| 218 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 59, hash_values, 5, 6, 7, 0, 1, 2, 3, 4, s0, s1, t1, t2 ) \ | 
| 219 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 60, hash_values, 4, 5, 6, 7, 0, 1, 2, 3, s0, s1, t1, t2 ) \ | 
| 220 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 61, hash_values, 3, 4, 5, 6, 7, 0, 1, 2, s0, s1, t1, t2 ) \ | 
| 221 | 114k |   libhmac_sha256_context_transform_unfolded_calculate_hash_value( values_32bit, 62, hash_values, 2, 3, 4, 5, 6, 7, 0, 1, s0, s1, t1, t2 ) \ | 
| 222 | 114k |   libhmac_sha256_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-256 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_sha256_context_transform( | 
| 228 |  |          libhmac_internal_sha256_context_t *internal_context, | 
| 229 |  |          const uint8_t *buffer, | 
| 230 |  |          size_t size, | 
| 231 |  |          libcerror_error_t **error ) | 
| 232 | 116 | { | 
| 233 | 116 |   uint32_t hash_values[ 8 ]; | 
| 234 | 116 |   uint32_t values_32bit[ 64 ]; | 
| 235 |  |  | 
| 236 | 116 |   static char *function     = "libhmac_sha256_context_transform"; | 
| 237 | 116 |   size_t buffer_offset      = 0; | 
| 238 | 116 |   uint32_t s0               = 0; | 
| 239 | 116 |   uint32_t s1               = 0; | 
| 240 | 116 |   uint32_t t1               = 0; | 
| 241 | 116 |   uint32_t t2               = 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 | 116 |   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 | 116 |   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 | 116 |   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 | 115k |   while( size >= LIBHMAC_SHA256_BLOCK_SIZE ) | 
| 282 | 114k |   { | 
| 283 | 114k |     if( memory_copy( | 
| 284 | 114k |          hash_values, | 
| 285 | 114k |          internal_context->hash_values, | 
| 286 | 114k |          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 | 114k | #if defined( LIBHMAC_UNFOLLED_LOOPS ) | 
| 298 |  |     /* Break the block into 16 x 32-bit values | 
| 299 |  |      */ | 
| 300 | 114k |     libhmac_byte_stream_copy_to_16x_uint32_big_endian( | 
| 301 | 114k |      &( buffer[ buffer_offset ] ), | 
| 302 | 114k |      values_32bit ); | 
| 303 |  |  | 
| 304 | 114k |     buffer_offset += LIBHMAC_SHA256_BLOCK_SIZE; | 
| 305 |  |  | 
| 306 |  |     /* Extend to 64 x 32-bit values | 
| 307 |  |      */ | 
| 308 | 114k |     libhmac_sha256_context_transform_unfolded_extend_32bit_values( | 
| 309 | 114k |      values_32bit, | 
| 310 | 114k |      s0, | 
| 311 | 114k |      s1 ); | 
| 312 |  |  | 
| 313 |  |     /* Calculate the hash values for the 32-bit values | 
| 314 |  |      */ | 
| 315 | 114k |     libhmac_sha256_context_transform_unfolded_calculate_hash_values( | 
| 316 | 114k |      values_32bit, | 
| 317 | 114k |      hash_values, | 
| 318 | 114k |      s0, | 
| 319 | 114k |      s1, | 
| 320 | 114k |      t1, | 
| 321 | 114k |      t2 ); | 
| 322 |  |  | 
| 323 |  |     /* Update the hash values in the context | 
| 324 |  |      */ | 
| 325 | 114k |     internal_context->hash_values[ 0 ] += hash_values[ 0 ]; | 
| 326 | 114k |     internal_context->hash_values[ 1 ] += hash_values[ 1 ]; | 
| 327 | 114k |     internal_context->hash_values[ 2 ] += hash_values[ 2 ]; | 
| 328 | 114k |     internal_context->hash_values[ 3 ] += hash_values[ 3 ]; | 
| 329 | 114k |     internal_context->hash_values[ 4 ] += hash_values[ 4 ]; | 
| 330 | 114k |     internal_context->hash_values[ 5 ] += hash_values[ 5 ]; | 
| 331 | 114k |     internal_context->hash_values[ 6 ] += hash_values[ 6 ]; | 
| 332 | 114k |     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_sha256_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_sha256_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 | 114k |     size -= LIBHMAC_SHA256_BLOCK_SIZE; | 
| 403 | 114k |   } | 
| 404 |  |   /* Prevent sensitive data from leaking | 
| 405 |  |    */ | 
| 406 | 116 |   if( memory_set( | 
| 407 | 116 |        hash_values, | 
| 408 | 116 |        0, | 
| 409 | 116 |        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 | 116 |   if( memory_set( | 
| 421 | 116 |        values_32bit, | 
| 422 | 116 |        0, | 
| 423 | 116 |        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 | 116 |   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 | 116 | } | 
| 449 |  |  | 
| 450 |  | #endif /* !defined( LIBHMAC_HAVE_SHA256_SUPPORT ) */ | 
| 451 |  |  | 
| 452 |  | /* Creates a SHA-256 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_sha256_context_initialize( | 
| 457 |  |      libhmac_sha256_context_t **context, | 
| 458 |  |      libcerror_error_t **error ) | 
| 459 | 58 | { | 
| 460 | 58 |   libhmac_internal_sha256_context_t *internal_context = NULL; | 
| 461 | 58 |   static char *function                               = "libhmac_sha256_context_initialize"; | 
| 462 |  |  | 
| 463 |  | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA256 ) | 
| 464 |  |   const EVP_MD *evp_md_type                           = NULL; | 
| 465 |  | #endif | 
| 466 |  |  | 
| 467 | 58 |   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 | 58 |   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 | 58 |   internal_context = memory_allocate_structure( | 
| 490 | 58 |                       libhmac_internal_sha256_context_t ); | 
| 491 |  |  | 
| 492 | 58 |   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 | 58 |   if( memory_set( | 
| 504 | 58 |        internal_context, | 
| 505 | 58 |        0, | 
| 506 | 58 |        sizeof( libhmac_internal_sha256_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( SHA256_DIGEST_LENGTH ) | 
| 521 |  |   if( SHA256_Init( | 
| 522 |  |        &( internal_context->sha256_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_SHA256 ) | 
| 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_sha256(); | 
| 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 | 58 |   if( memory_copy( | 
| 593 | 58 |        internal_context->hash_values, | 
| 594 | 58 |        libhmac_sha256_context_prime_square_roots, | 
| 595 | 58 |        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 | 58 | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA256_DIGEST_LENGTH ) */ | 
| 607 |  |  | 
| 608 | 58 |   *context = (libhmac_sha256_context_t *) internal_context; | 
| 609 |  |  | 
| 610 | 58 |   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 | 58 | } | 
| 620 |  |  | 
| 621 |  | /* Frees a SHA-256 context | 
| 622 |  |  * Returns 1 if successful or -1 on error | 
| 623 |  |  */ | 
| 624 |  | int libhmac_sha256_context_free( | 
| 625 |  |      libhmac_sha256_context_t **context, | 
| 626 |  |      libcerror_error_t **error ) | 
| 627 | 58 | { | 
| 628 | 58 |   libhmac_internal_sha256_context_t *internal_context = NULL; | 
| 629 | 58 |   static char *function                               = "libhmac_sha256_context_free"; | 
| 630 |  |  | 
| 631 | 58 |   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 | 58 |   if( *context != NULL ) | 
| 643 | 58 |   { | 
| 644 | 58 |     internal_context = (libhmac_internal_sha256_context_t *) *context; | 
| 645 | 58 |     *context         = NULL; | 
| 646 |  |  | 
| 647 |  | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA256_DIGEST_LENGTH ) | 
| 648 |  |     /* No additional clean up necessary | 
| 649 |  |      */ | 
| 650 |  |  | 
| 651 |  | #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA256 ) | 
| 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 | 58 | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA256_DIGEST_LENGTH ) */ | 
| 678 |  |  | 
| 679 | 58 |     memory_free( | 
| 680 | 58 |      internal_context ); | 
| 681 | 58 |   } | 
| 682 | 58 |   return( 1 ); | 
| 683 | 58 | } | 
| 684 |  |  | 
| 685 |  | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA256_DIGEST_LENGTH ) | 
| 686 |  |  | 
| 687 |  | /* Updates the SHA-256 context using OpenSSL | 
| 688 |  |  * Returns 1 if successful or -1 on error | 
| 689 |  |  */ | 
| 690 |  | int libhmac_sha256_context_update( | 
| 691 |  |      libhmac_sha256_context_t *context, | 
| 692 |  |      const uint8_t *buffer, | 
| 693 |  |      size_t size, | 
| 694 |  |      libcerror_error_t **error ) | 
| 695 |  | { | 
| 696 |  |   libhmac_internal_sha256_context_t *internal_context = NULL; | 
| 697 |  |   static char *function                               = "libhmac_sha256_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_sha256_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->sha256_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_SHA256 ) | 
| 763 |  |  | 
| 764 |  | /* Updates the SHA-256 context using OpenSSL EVP | 
| 765 |  |  * Returns 1 if successful or -1 on error | 
| 766 |  |  */ | 
| 767 |  | int libhmac_sha256_context_update( | 
| 768 |  |      libhmac_sha256_context_t *context, | 
| 769 |  |      const uint8_t *buffer, | 
| 770 |  |      size_t size, | 
| 771 |  |      libcerror_error_t **error ) | 
| 772 |  | { | 
| 773 |  |   libhmac_internal_sha256_context_t *internal_context = NULL; | 
| 774 |  |   static char *function                               = "libhmac_sha256_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_sha256_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-256 context using fallback implementation | 
| 835 |  |  * Returns 1 if successful or -1 on error | 
| 836 |  |  */ | 
| 837 |  | int libhmac_sha256_context_update( | 
| 838 |  |      libhmac_sha256_context_t *context, | 
| 839 |  |      const uint8_t *buffer, | 
| 840 |  |      size_t size, | 
| 841 |  |      libcerror_error_t **error ) | 
| 842 | 58 | { | 
| 843 | 58 |   libhmac_internal_sha256_context_t *internal_context = NULL; | 
| 844 | 58 |   static char *function                               = "libhmac_sha256_context_update"; | 
| 845 | 58 |   size_t buffer_offset                                = 0; | 
| 846 | 58 |   size_t remaining_block_size                         = 0; | 
| 847 | 58 |   ssize_t process_count                               = 0; | 
| 848 |  |  | 
| 849 | 58 |   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 | 58 |   internal_context = (libhmac_internal_sha256_context_t *) context; | 
| 861 |  |  | 
| 862 | 58 |   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 | 58 |   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 | 58 |   if( size == 0 ) | 
| 885 | 0 |   { | 
| 886 | 0 |     return( 1 ); | 
| 887 | 0 |   } | 
| 888 | 58 |   if( internal_context->block_offset > 0 ) | 
| 889 | 0 |   { | 
| 890 | 0 |     if( internal_context->block_offset >= LIBHMAC_SHA256_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_SHA256_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_SHA256_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_sha256_context_transform( | 
| 931 | 0 |                      internal_context, | 
| 932 | 0 |                      internal_context->block, | 
| 933 | 0 |                      LIBHMAC_SHA256_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 | 58 |   if( size > 0 ) | 
| 951 | 58 |   { | 
| 952 | 58 |     process_count = libhmac_sha256_context_transform( | 
| 953 | 58 |                      internal_context, | 
| 954 | 58 |                      &( buffer[ buffer_offset ] ), | 
| 955 | 58 |                      size, | 
| 956 | 58 |                      error ); | 
| 957 |  |  | 
| 958 | 58 |     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 | 58 |     internal_context->hash_count += process_count; | 
| 970 |  |  | 
| 971 | 58 |     buffer_offset += process_count; | 
| 972 | 58 |     size          -= process_count; | 
| 973 | 58 |   } | 
| 974 | 58 |   if( size > 0 ) | 
| 975 | 41 |   { | 
| 976 | 41 |     if( size >= LIBHMAC_SHA256_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 | 58 |   return( 1 ); | 
| 1004 | 58 | } | 
| 1005 |  |  | 
| 1006 |  | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA256_DIGEST_LENGTH ) */ | 
| 1007 |  |  | 
| 1008 |  | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA256_DIGEST_LENGTH ) | 
| 1009 |  |  | 
| 1010 |  | /* Finalizes the SHA-256 context using OpenSSL | 
| 1011 |  |  * Returns 1 if successful or -1 on error | 
| 1012 |  |  */ | 
| 1013 |  | int libhmac_sha256_context_finalize( | 
| 1014 |  |      libhmac_sha256_context_t *context, | 
| 1015 |  |      uint8_t *hash, | 
| 1016 |  |      size_t hash_size, | 
| 1017 |  |      libcerror_error_t **error ) | 
| 1018 |  | { | 
| 1019 |  |   libhmac_internal_sha256_context_t *internal_context = NULL; | 
| 1020 |  |   static char *function                               = "libhmac_sha256_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_sha256_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_SHA256_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->sha256_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_SHA256 ) | 
| 1085 |  |  | 
| 1086 |  | /* Finalizes the SHA-256 context using OpenSSL EVP | 
| 1087 |  |  * Returns 1 if successful or -1 on error | 
| 1088 |  |  */ | 
| 1089 |  | int libhmac_sha256_context_finalize( | 
| 1090 |  |      libhmac_sha256_context_t *context, | 
| 1091 |  |      uint8_t *hash, | 
| 1092 |  |      size_t hash_size, | 
| 1093 |  |      libcerror_error_t **error ) | 
| 1094 |  | { | 
| 1095 |  |   libhmac_internal_sha256_context_t *internal_context = NULL; | 
| 1096 |  |   static char *function                               = "libhmac_sha256_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_sha256_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_SHA256_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-256 context using fallback implementation | 
| 1157 |  |  * Returns 1 if successful or -1 on error | 
| 1158 |  |  */ | 
| 1159 |  | int libhmac_sha256_context_finalize( | 
| 1160 |  |      libhmac_sha256_context_t *context, | 
| 1161 |  |      uint8_t *hash, | 
| 1162 |  |      size_t hash_size, | 
| 1163 |  |      libcerror_error_t **error ) | 
| 1164 | 58 | { | 
| 1165 | 58 |   libhmac_internal_sha256_context_t *internal_context = NULL; | 
| 1166 | 58 |   static char *function                               = "libhmac_sha256_context_finalize"; | 
| 1167 | 58 |   size_t block_size                                   = 0; | 
| 1168 | 58 |   size_t number_of_blocks                             = 0; | 
| 1169 | 58 |   ssize_t process_count                               = 0; | 
| 1170 | 58 |   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 | 58 |   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 | 58 |   internal_context = (libhmac_internal_sha256_context_t *) context; | 
| 1189 |  |  | 
| 1190 | 58 |   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 | 58 |   if( hash_size < (size_t) LIBHMAC_SHA256_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 | 58 |   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 | 58 |   if( hash_size < (size_t) LIBHMAC_SHA256_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 | 58 |   number_of_blocks = 1; | 
| 1237 |  |  | 
| 1238 | 58 |   if( internal_context->block_offset > 55 ) | 
| 1239 | 25 |   { | 
| 1240 | 25 |     number_of_blocks += 1; | 
| 1241 | 25 |   } | 
| 1242 | 58 |   block_size = number_of_blocks * LIBHMAC_SHA256_BLOCK_SIZE; | 
| 1243 |  |  | 
| 1244 | 58 |   if( memory_set( | 
| 1245 | 58 |        &( internal_context->block[ internal_context->block_offset ] ), | 
| 1246 | 58 |        0, | 
| 1247 | 58 |        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 | 58 |   internal_context->block[ internal_context->block_offset ] = 0x80; | 
| 1261 |  |  | 
| 1262 | 58 |   bit_size = ( internal_context->hash_count + internal_context->block_offset ) * 8; | 
| 1263 |  |  | 
| 1264 | 58 |   byte_stream_copy_from_uint64_big_endian( | 
| 1265 | 58 |    &( internal_context->block[ block_size - 8 ] ), | 
| 1266 | 58 |    bit_size ); | 
| 1267 |  |  | 
| 1268 | 58 |   process_count = libhmac_sha256_context_transform( | 
| 1269 | 58 |                    internal_context, | 
| 1270 | 58 |                    internal_context->block, | 
| 1271 | 58 |                    block_size, | 
| 1272 | 58 |                    error ); | 
| 1273 |  |  | 
| 1274 | 58 |   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 |  | #if !defined( LIBHMAC_UNFOLLED_LOOPS ) | 
| 1286 |  |   for( hash_values_index = 0; | 
| 1287 |  |        hash_values_index < 8; | 
| 1288 |  |        hash_values_index++ ) | 
| 1289 |  |   { | 
| 1290 |  |     byte_stream_copy_from_uint32_big_endian( | 
| 1291 |  |      &( hash[ hash_index ] ), | 
| 1292 |  |      internal_context->hash_values[ hash_values_index ] ); | 
| 1293 |  |  | 
| 1294 |  |     hash_index += sizeof( uint32_t ); | 
| 1295 |  |   } | 
| 1296 |  | #else | 
| 1297 | 58 |   byte_stream_copy_from_uint32_big_endian( | 
| 1298 | 58 |    &( hash[ 0 ] ), | 
| 1299 | 58 |    internal_context->hash_values[ 0 ] ); | 
| 1300 |  |  | 
| 1301 | 58 |   byte_stream_copy_from_uint32_big_endian( | 
| 1302 | 58 |    &( hash[ 4 ] ), | 
| 1303 | 58 |    internal_context->hash_values[ 1 ] ); | 
| 1304 |  |  | 
| 1305 | 58 |   byte_stream_copy_from_uint32_big_endian( | 
| 1306 | 58 |    &( hash[ 8 ] ), | 
| 1307 | 58 |    internal_context->hash_values[ 2 ] ); | 
| 1308 |  |  | 
| 1309 | 58 |   byte_stream_copy_from_uint32_big_endian( | 
| 1310 | 58 |    &( hash[ 12 ] ), | 
| 1311 | 58 |    internal_context->hash_values[ 3 ] ); | 
| 1312 |  |  | 
| 1313 | 58 |   byte_stream_copy_from_uint32_big_endian( | 
| 1314 | 58 |    &( hash[ 16 ] ), | 
| 1315 | 58 |    internal_context->hash_values[ 4 ] ); | 
| 1316 |  |  | 
| 1317 | 58 |   byte_stream_copy_from_uint32_big_endian( | 
| 1318 | 58 |    &( hash[ 20 ] ), | 
| 1319 | 58 |    internal_context->hash_values[ 5 ] ); | 
| 1320 |  |  | 
| 1321 | 58 |   byte_stream_copy_from_uint32_big_endian( | 
| 1322 | 58 |    &( hash[ 24 ] ), | 
| 1323 | 58 |    internal_context->hash_values[ 6 ] ); | 
| 1324 |  |  | 
| 1325 | 58 |   byte_stream_copy_from_uint32_big_endian( | 
| 1326 | 58 |    &( hash[ 28 ] ), | 
| 1327 | 58 |    internal_context->hash_values[ 7 ] ); | 
| 1328 |  |  | 
| 1329 | 58 | #endif /* !defined( LIBHMAC_UNFOLLED_LOOPS ) */ | 
| 1330 |  |  | 
| 1331 |  |   /* Prevent sensitive data from leaking | 
| 1332 |  |    */ | 
| 1333 | 58 |   if( memory_set( | 
| 1334 | 58 |        internal_context, | 
| 1335 | 58 |        0, | 
| 1336 | 58 |        sizeof( libhmac_internal_sha256_context_t ) ) == NULL ) | 
| 1337 | 0 |   { | 
| 1338 | 0 |     libcerror_error_set( | 
| 1339 | 0 |      error, | 
| 1340 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 1341 | 0 |      LIBCERROR_MEMORY_ERROR_SET_FAILED, | 
| 1342 | 0 |      "%s: unable to clear context.", | 
| 1343 | 0 |      function ); | 
| 1344 |  | 
 | 
| 1345 | 0 |     return( -1 ); | 
| 1346 | 0 |   } | 
| 1347 | 58 |   return( 1 ); | 
| 1348 | 58 | } | 
| 1349 |  |  | 
| 1350 |  | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA256_DIGEST_LENGTH ) */ | 
| 1351 |  |  |