/src/libbde/libbde/libbde_stretch_key.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Stretch Key metadata entry 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 | | #include "libbde_debug.h" |
28 | | #include "libbde_definitions.h" |
29 | | #include "libbde_libcerror.h" |
30 | | #include "libbde_libcnotify.h" |
31 | | #include "libbde_metadata_entry.h" |
32 | | #include "libbde_stretch_key.h" |
33 | | |
34 | | #include "bde_metadata.h" |
35 | | |
36 | | /* Creates a stretch key |
37 | | * Make sure the value stretch key is referencing, is set to NULL |
38 | | * Returns 1 if successful or -1 on error |
39 | | */ |
40 | | int libbde_stretch_key_initialize( |
41 | | libbde_stretch_key_t **stretch_key, |
42 | | libcerror_error_t **error ) |
43 | 984 | { |
44 | 984 | static char *function = "libbde_stretch_key_initialize"; |
45 | | |
46 | 984 | if( stretch_key == NULL ) |
47 | 0 | { |
48 | 0 | libcerror_error_set( |
49 | 0 | error, |
50 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
51 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
52 | 0 | "%s: invalid stretch key.", |
53 | 0 | function ); |
54 | |
|
55 | 0 | return( -1 ); |
56 | 0 | } |
57 | 984 | if( *stretch_key != NULL ) |
58 | 0 | { |
59 | 0 | libcerror_error_set( |
60 | 0 | error, |
61 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
62 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
63 | 0 | "%s: invalid stretch key value already set.", |
64 | 0 | function ); |
65 | |
|
66 | 0 | return( -1 ); |
67 | 0 | } |
68 | 984 | *stretch_key = memory_allocate_structure( |
69 | 984 | libbde_stretch_key_t ); |
70 | | |
71 | 984 | if( *stretch_key == NULL ) |
72 | 0 | { |
73 | 0 | libcerror_error_set( |
74 | 0 | error, |
75 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
76 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
77 | 0 | "%s: unable to create stretch key.", |
78 | 0 | function ); |
79 | |
|
80 | 0 | goto on_error; |
81 | 0 | } |
82 | 984 | if( memory_set( |
83 | 984 | *stretch_key, |
84 | 984 | 0, |
85 | 984 | sizeof( libbde_stretch_key_t ) ) == NULL ) |
86 | 0 | { |
87 | 0 | libcerror_error_set( |
88 | 0 | error, |
89 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
90 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
91 | 0 | "%s: unable to clear stretch key.", |
92 | 0 | function ); |
93 | |
|
94 | 0 | goto on_error; |
95 | 0 | } |
96 | 984 | return( 1 ); |
97 | | |
98 | 0 | on_error: |
99 | 0 | if( *stretch_key != NULL ) |
100 | 0 | { |
101 | 0 | memory_free( |
102 | 0 | *stretch_key ); |
103 | |
|
104 | 0 | *stretch_key = NULL; |
105 | 0 | } |
106 | 0 | return( -1 ); |
107 | 984 | } |
108 | | |
109 | | /* Frees a stretch key |
110 | | * Returns 1 if successful or -1 on error |
111 | | */ |
112 | | int libbde_stretch_key_free( |
113 | | libbde_stretch_key_t **stretch_key, |
114 | | libcerror_error_t **error ) |
115 | 984 | { |
116 | 984 | static char *function = "libbde_stretch_key_free"; |
117 | | |
118 | 984 | if( stretch_key == NULL ) |
119 | 0 | { |
120 | 0 | libcerror_error_set( |
121 | 0 | error, |
122 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
123 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
124 | 0 | "%s: invalid stretch key.", |
125 | 0 | function ); |
126 | |
|
127 | 0 | return( -1 ); |
128 | 0 | } |
129 | 984 | if( *stretch_key != NULL ) |
130 | 984 | { |
131 | 984 | if( ( *stretch_key )->data != NULL ) |
132 | 981 | { |
133 | 981 | memory_free( |
134 | 981 | ( *stretch_key )->data ); |
135 | 981 | } |
136 | 984 | memory_free( |
137 | 984 | *stretch_key ); |
138 | | |
139 | 984 | *stretch_key = NULL; |
140 | 984 | } |
141 | 984 | return( 1 ); |
142 | 984 | } |
143 | | |
144 | | /* Reads a stretch key from the metadata entry |
145 | | * Returns 1 if successful or -1 on error |
146 | | */ |
147 | | int libbde_stretch_key_read( |
148 | | libbde_stretch_key_t *stretch_key, |
149 | | libbde_metadata_entry_t *metadata_entry, |
150 | | libcerror_error_t **error ) |
151 | 984 | { |
152 | 984 | uint8_t *value_data = NULL; |
153 | 984 | static char *function = "libbde_stretch_key_read"; |
154 | 984 | size_t value_data_size = 0; |
155 | | |
156 | 984 | if( stretch_key == NULL ) |
157 | 0 | { |
158 | 0 | libcerror_error_set( |
159 | 0 | error, |
160 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
161 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
162 | 0 | "%s: invalid stretch key.", |
163 | 0 | function ); |
164 | |
|
165 | 0 | return( -1 ); |
166 | 0 | } |
167 | 984 | if( metadata_entry == NULL ) |
168 | 0 | { |
169 | 0 | libcerror_error_set( |
170 | 0 | error, |
171 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
172 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
173 | 0 | "%s: invalid metadata entry.", |
174 | 0 | function ); |
175 | |
|
176 | 0 | return( -1 ); |
177 | 0 | } |
178 | 984 | if( metadata_entry->value_data == NULL ) |
179 | 0 | { |
180 | 0 | libcerror_error_set( |
181 | 0 | error, |
182 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
183 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
184 | 0 | "%s: invalid metadata entry - missing value data.", |
185 | 0 | function ); |
186 | |
|
187 | 0 | return( -1 ); |
188 | 0 | } |
189 | 984 | if( metadata_entry->value_type != LIBBDE_VALUE_TYPE_STRETCH_KEY ) |
190 | 0 | { |
191 | 0 | libcerror_error_set( |
192 | 0 | error, |
193 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
194 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
195 | 0 | "%s: invalid metadata entry - unsupported value type: 0x%04" PRIx16 ".", |
196 | 0 | function, |
197 | 0 | metadata_entry->value_type ); |
198 | |
|
199 | 0 | return( -1 ); |
200 | 0 | } |
201 | 984 | value_data = metadata_entry->value_data; |
202 | 984 | value_data_size = metadata_entry->value_data_size; |
203 | | |
204 | 984 | if( ( value_data_size < sizeof( bde_metadata_entry_stretch_key_header_t ) ) |
205 | 984 | || ( value_data_size > MEMORY_MAXIMUM_ALLOCATION_SIZE ) ) |
206 | 3 | { |
207 | 3 | libcerror_error_set( |
208 | 3 | error, |
209 | 3 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
210 | 3 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
211 | 3 | "%s: value data size value out of bounds.", |
212 | 3 | function ); |
213 | | |
214 | 3 | return( -1 ); |
215 | 3 | } |
216 | 981 | byte_stream_copy_to_uint32_little_endian( |
217 | 981 | ( (bde_metadata_entry_stretch_key_header_t *) value_data )->encryption_method, |
218 | 981 | stretch_key->encryption_method ); |
219 | | |
220 | 981 | if( memory_copy( |
221 | 981 | stretch_key->salt, |
222 | 981 | ( (bde_metadata_entry_stretch_key_header_t *) value_data )->salt, |
223 | 981 | 16 ) == NULL ) |
224 | 0 | { |
225 | 0 | libcerror_error_set( |
226 | 0 | error, |
227 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
228 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
229 | 0 | "%s: unable to copy salt to stretch key.", |
230 | 0 | function ); |
231 | |
|
232 | 0 | goto on_error; |
233 | 0 | } |
234 | | #if defined( HAVE_DEBUG_OUTPUT ) |
235 | | if( libcnotify_verbose != 0 ) |
236 | | { |
237 | | libcnotify_printf( |
238 | | "%s: encryption method\t\t\t\t: 0x%08" PRIx32 " (%s)\n", |
239 | | function, |
240 | | stretch_key->encryption_method, |
241 | | libbde_debug_print_encryption_method( |
242 | | stretch_key->encryption_method ) ); |
243 | | |
244 | | libcnotify_printf( |
245 | | "%s: salt:\n", |
246 | | function ); |
247 | | libcnotify_print_data( |
248 | | ( (bde_metadata_entry_stretch_key_header_t *) value_data )->salt, |
249 | | 16, |
250 | | 0 ); |
251 | | } |
252 | | #endif |
253 | 981 | value_data += sizeof( bde_metadata_entry_stretch_key_header_t ); |
254 | 981 | value_data_size -= sizeof( bde_metadata_entry_stretch_key_header_t ); |
255 | | |
256 | | #if defined( HAVE_DEBUG_OUTPUT ) |
257 | | if( libcnotify_verbose != 0 ) |
258 | | { |
259 | | libcnotify_printf( |
260 | | "%s: encrypted data:\n", |
261 | | function ); |
262 | | libcnotify_print_data( |
263 | | value_data, |
264 | | value_data_size, |
265 | | 0 ); |
266 | | } |
267 | | #endif |
268 | 981 | stretch_key->data = (uint8_t *) memory_allocate( |
269 | 981 | sizeof( uint8_t ) * value_data_size ); |
270 | | |
271 | 981 | if( stretch_key->data == NULL ) |
272 | 0 | { |
273 | 0 | libcerror_error_set( |
274 | 0 | error, |
275 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
276 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
277 | 0 | "%s: unable to create data.", |
278 | 0 | function ); |
279 | |
|
280 | 0 | goto on_error; |
281 | 0 | } |
282 | 981 | if( memory_copy( |
283 | 981 | stretch_key->data, |
284 | 981 | value_data, |
285 | 981 | value_data_size ) == NULL ) |
286 | 0 | { |
287 | 0 | libcerror_error_set( |
288 | 0 | error, |
289 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
290 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
291 | 0 | "%s: unable to copy data to stretch key.", |
292 | 0 | function ); |
293 | |
|
294 | 0 | goto on_error; |
295 | 0 | } |
296 | 981 | stretch_key->data_size = value_data_size; |
297 | | |
298 | 981 | return( 1 ); |
299 | | |
300 | 0 | on_error: |
301 | 0 | if( stretch_key->data != NULL ) |
302 | 0 | { |
303 | 0 | memory_free( |
304 | 0 | stretch_key->data ); |
305 | |
|
306 | 0 | stretch_key->data = NULL; |
307 | 0 | } |
308 | 0 | return( -1 ); |
309 | 981 | } |
310 | | |