/src/libluksde/libluksde/libluksde_sector_data.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Sector data functions |
3 | | * |
4 | | * Copyright (C) 2013-2023, 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 "libluksde_definitions.h" |
28 | | #include "libluksde_encryption_context.h" |
29 | | #include "libluksde_libbfio.h" |
30 | | #include "libluksde_libcerror.h" |
31 | | #include "libluksde_libcnotify.h" |
32 | | #include "libluksde_sector_data.h" |
33 | | |
34 | | /* Creates sector data |
35 | | * Make sure the value sector_data is referencing, is set to NULL |
36 | | * Returns 1 if successful or -1 on error |
37 | | */ |
38 | | int libluksde_sector_data_initialize( |
39 | | libluksde_sector_data_t **sector_data, |
40 | | size_t data_size, |
41 | | libcerror_error_t **error ) |
42 | 0 | { |
43 | 0 | static char *function = "libluksde_sector_data_initialize"; |
44 | |
|
45 | 0 | if( sector_data == NULL ) |
46 | 0 | { |
47 | 0 | libcerror_error_set( |
48 | 0 | error, |
49 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
50 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
51 | 0 | "%s: invalid sector data.", |
52 | 0 | function ); |
53 | |
|
54 | 0 | return( -1 ); |
55 | 0 | } |
56 | 0 | if( *sector_data != NULL ) |
57 | 0 | { |
58 | 0 | libcerror_error_set( |
59 | 0 | error, |
60 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
61 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
62 | 0 | "%s: invalid sector data value already set.", |
63 | 0 | function ); |
64 | |
|
65 | 0 | return( -1 ); |
66 | 0 | } |
67 | 0 | if( ( data_size == 0 ) |
68 | 0 | || ( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
73 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
74 | 0 | "%s: invalid data size value exceeds maximum.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | return( -1 ); |
78 | 0 | } |
79 | 0 | *sector_data = memory_allocate_structure( |
80 | 0 | libluksde_sector_data_t ); |
81 | |
|
82 | 0 | if( *sector_data == NULL ) |
83 | 0 | { |
84 | 0 | libcerror_error_set( |
85 | 0 | error, |
86 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
87 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
88 | 0 | "%s: unable to create sector data.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 0 | if( memory_set( |
94 | 0 | *sector_data, |
95 | 0 | 0, |
96 | 0 | sizeof( libluksde_sector_data_t ) ) == NULL ) |
97 | 0 | { |
98 | 0 | libcerror_error_set( |
99 | 0 | error, |
100 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
101 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
102 | 0 | "%s: unable to clear sector data.", |
103 | 0 | function ); |
104 | |
|
105 | 0 | memory_free( |
106 | 0 | *sector_data ); |
107 | |
|
108 | 0 | *sector_data = NULL; |
109 | |
|
110 | 0 | return( -1 ); |
111 | 0 | } |
112 | 0 | ( *sector_data )->data = (uint8_t *) memory_allocate( |
113 | 0 | sizeof( uint8_t ) * data_size ); |
114 | |
|
115 | 0 | if( ( *sector_data )->data == NULL ) |
116 | 0 | { |
117 | 0 | libcerror_error_set( |
118 | 0 | error, |
119 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
120 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
121 | 0 | "%s: unable to create data.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | goto on_error; |
125 | 0 | } |
126 | 0 | ( *sector_data )->data_size = data_size; |
127 | |
|
128 | 0 | return( 1 ); |
129 | | |
130 | 0 | on_error: |
131 | 0 | if( *sector_data != NULL ) |
132 | 0 | { |
133 | 0 | memory_free( |
134 | 0 | *sector_data ); |
135 | |
|
136 | 0 | *sector_data = NULL; |
137 | 0 | } |
138 | 0 | return( -1 ); |
139 | 0 | } |
140 | | |
141 | | /* Frees sector data |
142 | | * Returns 1 if successful or -1 on error |
143 | | */ |
144 | | int libluksde_sector_data_free( |
145 | | libluksde_sector_data_t **sector_data, |
146 | | libcerror_error_t **error ) |
147 | 0 | { |
148 | 0 | static char *function = "libluksde_sector_data_free"; |
149 | 0 | int result = 1; |
150 | |
|
151 | 0 | if( sector_data == NULL ) |
152 | 0 | { |
153 | 0 | libcerror_error_set( |
154 | 0 | error, |
155 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
156 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
157 | 0 | "%s: invalid sector data.", |
158 | 0 | function ); |
159 | |
|
160 | 0 | return( -1 ); |
161 | 0 | } |
162 | 0 | if( *sector_data != NULL ) |
163 | 0 | { |
164 | 0 | if( memory_set( |
165 | 0 | ( *sector_data )->data, |
166 | 0 | 0, |
167 | 0 | ( *sector_data )->data_size ) == NULL ) |
168 | 0 | { |
169 | 0 | libcerror_error_set( |
170 | 0 | error, |
171 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
172 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
173 | 0 | "%s: unable to clear data.", |
174 | 0 | function ); |
175 | |
|
176 | 0 | result = -1; |
177 | 0 | } |
178 | 0 | memory_free( |
179 | 0 | ( *sector_data )->data ); |
180 | |
|
181 | 0 | memory_free( |
182 | 0 | *sector_data ); |
183 | |
|
184 | 0 | *sector_data = NULL; |
185 | 0 | } |
186 | 0 | return( result ); |
187 | 0 | } |
188 | | |
189 | | /* Reads sector data |
190 | | * Returns 1 if successful or -1 on error |
191 | | */ |
192 | | int libluksde_sector_data_read_file_io_handle( |
193 | | libluksde_sector_data_t *sector_data, |
194 | | libbfio_handle_t *file_io_handle, |
195 | | off64_t file_offset, |
196 | | libluksde_encryption_context_t *encryption_context, |
197 | | uint64_t sector_number, |
198 | | libcerror_error_t **error ) |
199 | 0 | { |
200 | 0 | uint8_t *encrypted_data = NULL; |
201 | 0 | static char *function = "libluksde_sector_data_read_file_io_handle"; |
202 | 0 | ssize_t read_count = 0; |
203 | |
|
204 | 0 | if( sector_data == NULL ) |
205 | 0 | { |
206 | 0 | libcerror_error_set( |
207 | 0 | error, |
208 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
209 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
210 | 0 | "%s: invalid sector data.", |
211 | 0 | function ); |
212 | |
|
213 | 0 | return( -1 ); |
214 | 0 | } |
215 | 0 | if( sector_data->data == NULL ) |
216 | 0 | { |
217 | 0 | libcerror_error_set( |
218 | 0 | error, |
219 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
220 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
221 | 0 | "%s: invalid sector data - missing data.", |
222 | 0 | function ); |
223 | |
|
224 | 0 | return( -1 ); |
225 | 0 | } |
226 | 0 | encrypted_data = (uint8_t *) memory_allocate( |
227 | 0 | sizeof( uint8_t ) * sector_data->data_size ); |
228 | |
|
229 | 0 | if( encrypted_data == NULL ) |
230 | 0 | { |
231 | 0 | libcerror_error_set( |
232 | 0 | error, |
233 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
234 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
235 | 0 | "%s: unable to create encrypted data.", |
236 | 0 | function ); |
237 | |
|
238 | 0 | goto on_error; |
239 | 0 | } |
240 | | #if defined( HAVE_DEBUG_OUTPUT ) |
241 | | if( libcnotify_verbose != 0 ) |
242 | | { |
243 | | libcnotify_printf( |
244 | | "%s: reading sector: %" PRIu64 " data at offset: %" PRIi64 " (0x%08" PRIx64 ")\n", |
245 | | function, |
246 | | sector_number, |
247 | | file_offset, |
248 | | file_offset ); |
249 | | } |
250 | | #endif |
251 | 0 | read_count = libbfio_handle_read_buffer_at_offset( |
252 | 0 | file_io_handle, |
253 | 0 | encrypted_data, |
254 | 0 | sector_data->data_size, |
255 | 0 | file_offset, |
256 | 0 | error ); |
257 | |
|
258 | 0 | if( read_count != (ssize_t) sector_data->data_size ) |
259 | 0 | { |
260 | 0 | libcerror_error_set( |
261 | 0 | error, |
262 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
263 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
264 | 0 | "%s: unable to read sector: %" PRIu64 " data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
265 | 0 | function, |
266 | 0 | sector_number, |
267 | 0 | file_offset, |
268 | 0 | file_offset ); |
269 | |
|
270 | 0 | goto on_error; |
271 | 0 | } |
272 | | #if defined( HAVE_DEBUG_OUTPUT ) |
273 | | if( libcnotify_verbose != 0 ) |
274 | | { |
275 | | libcnotify_printf( |
276 | | "%s: encrypted sector: %" PRIu64 " data:\n", |
277 | | function, |
278 | | sector_number ); |
279 | | libcnotify_print_data( |
280 | | encrypted_data, |
281 | | sector_data->data_size, |
282 | | 0 ); |
283 | | } |
284 | | #endif |
285 | 0 | if( libluksde_encryption_context_crypt( |
286 | 0 | encryption_context, |
287 | 0 | LIBLUKSDE_ENCRYPTION_CRYPT_MODE_DECRYPT, |
288 | 0 | encrypted_data, |
289 | 0 | sector_data->data_size, |
290 | 0 | sector_data->data, |
291 | 0 | sector_data->data_size, |
292 | 0 | sector_number, |
293 | 0 | error ) != 1 ) |
294 | 0 | { |
295 | 0 | libcerror_error_set( |
296 | 0 | error, |
297 | 0 | LIBCERROR_ERROR_DOMAIN_ENCRYPTION, |
298 | 0 | LIBCERROR_ENCRYPTION_ERROR_GENERIC, |
299 | 0 | "%s: unable to decrypt sector: %" PRIu64 " data.", |
300 | 0 | function, |
301 | 0 | sector_number ); |
302 | |
|
303 | 0 | goto on_error; |
304 | 0 | } |
305 | | #if defined( HAVE_DEBUG_OUTPUT ) |
306 | | if( libcnotify_verbose != 0 ) |
307 | | { |
308 | | libcnotify_printf( |
309 | | "%s: sector: %" PRIu64 " data:\n", |
310 | | function, |
311 | | sector_number ); |
312 | | libcnotify_print_data( |
313 | | sector_data->data, |
314 | | sector_data->data_size, |
315 | | 0 ); |
316 | | } |
317 | | #endif |
318 | 0 | memory_free( |
319 | 0 | encrypted_data ); |
320 | |
|
321 | 0 | return( 1 ); |
322 | | |
323 | 0 | on_error: |
324 | 0 | if( encrypted_data != NULL ) |
325 | 0 | { |
326 | 0 | memory_free( |
327 | 0 | encrypted_data ); |
328 | 0 | } |
329 | 0 | return( -1 ); |
330 | 0 | } |
331 | | |