Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libbde/libbde/libbde_eow_block_map.c
Line
Count
Source
1
/*
2
 * Encrypt-on-Write (EOW) block map functions
3
 *
4
 * Copyright (C) 2011-2026, 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_eow_block_map.h"
29
#include "libbde_libbfio.h"
30
#include "libbde_libcerror.h"
31
#include "libbde_libcnotify.h"
32
33
#include "bde_eow.h"
34
35
/* Creates an Encrypt-on-Write (EOW) block map
36
 * Make sure the value eow_block_map is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libbde_eow_block_map_initialize(
40
     libbde_eow_block_map_t **eow_block_map,
41
     libcerror_error_t **error )
42
1.24k
{
43
1.24k
  static char *function = "libbde_eow_block_map_initialize";
44
45
1.24k
  if( eow_block_map == 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 Encrypt-on-Write (EOW) block map.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
1.24k
  if( *eow_block_map != 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 Encrypt-on-Write (EOW) block map value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
1.24k
  *eow_block_map = memory_allocate_structure(
68
1.24k
                    libbde_eow_block_map_t );
69
70
1.24k
  if( *eow_block_map == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
75
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76
0
     "%s: unable to create Encrypt-on-Write (EOW) block map.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
1.24k
  if( memory_set(
82
1.24k
       *eow_block_map,
83
1.24k
       0,
84
1.24k
       sizeof( libbde_eow_block_map_t ) ) == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
89
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
90
0
     "%s: unable to clear Encrypt-on-Write (EOW) block map.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
1.24k
  return( 1 );
96
97
0
on_error:
98
0
  if( *eow_block_map != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *eow_block_map );
102
103
0
    *eow_block_map = NULL;
104
0
  }
105
0
  return( -1 );
106
1.24k
}
107
108
/* Frees an Encrypt-on-Write (EOW) block map
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libbde_eow_block_map_free(
112
     libbde_eow_block_map_t **eow_block_map,
113
     libcerror_error_t **error )
114
1.24k
{
115
1.24k
  static char *function = "libbde_eow_block_map_free";
116
117
1.24k
  if( eow_block_map == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid Encrypt-on-Write (EOW) block map.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
1.24k
  if( *eow_block_map != NULL )
129
1.24k
  {
130
1.24k
    memory_free(
131
1.24k
     *eow_block_map );
132
133
1.24k
    *eow_block_map = NULL;
134
1.24k
  }
135
1.24k
  return( 1 );
136
1.24k
}
137
138
/* Reads an Encrypt-on-Write (EOW) block map
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libbde_eow_block_map_read_data(
142
     libbde_eow_block_map_t *eow_block_map,
143
     const uint8_t *data,
144
     size_t data_size,
145
     libcerror_error_t **error )
146
1.02k
{
147
1.02k
  static char *function = "libbde_eow_block_map_read_data";
148
1.02k
  uint16_t header_size  = 0;
149
150
#if defined( HAVE_DEBUG_OUTPUT )
151
  uint32_t value_32bit  = 0;
152
#endif
153
154
1.02k
  if( eow_block_map == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid Encrypt-on-Write (EOW) block map.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
1.02k
  if( data == NULL )
166
0
  {
167
0
    libcerror_error_set(
168
0
     error,
169
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
170
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
171
0
     "%s: invalid data.",
172
0
     function );
173
174
0
    return( -1 );
175
0
  }
176
1.02k
  if( ( data_size < sizeof( bde_eow_block_map_t ) )
177
1.02k
   || ( data_size > (size_t) SSIZE_MAX ) )
178
0
  {
179
0
    libcerror_error_set(
180
0
     error,
181
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
182
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
183
0
     "%s: invalid data size value out of bounds.",
184
0
     function );
185
186
0
    return( -1 );
187
0
  }
188
#if defined( HAVE_DEBUG_OUTPUT )
189
  if( libcnotify_verbose != 0 )
190
  {
191
    libcnotify_printf(
192
     "%s: Encrypt-on-Write (EOW) block map:\n",
193
     function );
194
    libcnotify_print_data(
195
     data,
196
     sizeof( bde_eow_block_map_t ),
197
     1 );
198
  }
199
#endif
200
1.02k
  if( memory_compare(
201
1.02k
       data,
202
1.02k
       "FVE-EOWBM\x00",
203
1.02k
       10 ) != 0 )
204
55
  {
205
55
    libcerror_error_set(
206
55
     error,
207
55
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
208
55
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
209
55
     "%s: invalid signature.",
210
55
     function );
211
212
55
    return( -1 );
213
55
  }
214
965
  byte_stream_copy_to_uint16_little_endian(
215
965
   ( (bde_eow_block_map_t *) data )->header_size,
216
965
   header_size );
217
218
965
  byte_stream_copy_to_uint32_little_endian(
219
965
   ( (bde_eow_block_map_t *) data )->block_map_size,
220
965
   eow_block_map->block_map_size );
221
222
965
  byte_stream_copy_to_uint64_little_endian(
223
965
   ( (bde_eow_block_map_t *) data )->volume_region_offset,
224
965
   eow_block_map->volume_region_offset );
225
226
965
  byte_stream_copy_to_uint64_little_endian(
227
965
   ( (bde_eow_block_map_t *) data )->volume_region_size,
228
965
   eow_block_map->volume_region_size );
229
230
965
  byte_stream_copy_to_uint64_little_endian(
231
965
   ( (bde_eow_block_map_t *) data )->relocation_log_area_offset,
232
965
   eow_block_map->relocation_log_area_offset );
233
234
965
  byte_stream_copy_to_uint32_little_endian(
235
965
   ( (bde_eow_block_map_t *) data )->first_block_record_offset,
236
965
   eow_block_map->first_block_record_offset );
237
238
965
  byte_stream_copy_to_uint32_little_endian(
239
965
   ( (bde_eow_block_map_t *) data )->second_block_record_offset,
240
965
   eow_block_map->second_block_record_offset );
241
242
965
  byte_stream_copy_to_uint32_little_endian(
243
965
   ( (bde_eow_block_map_t *) data )->block_record_size,
244
965
   eow_block_map->block_record_size );
245
246
#if defined( HAVE_DEBUG_OUTPUT )
247
  if( libcnotify_verbose != 0 )
248
  {
249
    libcnotify_printf(
250
     "%s: signature\t\t\t\t: %c%c%c%c%c%c%c%c%c\\x%02" PRIx8 "\n",
251
     function,
252
     data[ 0 ],
253
     data[ 1 ],
254
     data[ 2 ],
255
     data[ 3 ],
256
     data[ 4 ],
257
     data[ 5 ],
258
     data[ 6 ],
259
     data[ 7 ],
260
     data[ 8 ],
261
     data[ 9 ] );
262
263
    libcnotify_printf(
264
     "%s: header size\t\t\t\t: %" PRIu16 "\n",
265
     function,
266
     header_size );
267
268
    libcnotify_printf(
269
     "%s: block map size\t\t\t\t: %" PRIu32 "\n",
270
     function,
271
     eow_block_map->block_map_size );
272
273
    byte_stream_copy_to_uint32_little_endian(
274
     ( (bde_eow_block_map_t *) data )->block_map_index,
275
     value_32bit );
276
    libcnotify_printf(
277
     "%s: block map index\t\t\t\t: %" PRIu32 "\n",
278
     function,
279
     value_32bit );
280
281
    libcnotify_printf(
282
     "%s: volume region offset\t\t\t: 0x%08" PRIx64 "\n",
283
     function,
284
     eow_block_map->volume_region_offset );
285
286
    libcnotify_printf(
287
     "%s: volume region size\t\t\t: %" PRIu64 "\n",
288
     function,
289
     eow_block_map->volume_region_size );
290
291
    libcnotify_printf(
292
     "%s: relocation log area offset\t\t: 0x%08" PRIx64 "\n",
293
     function,
294
     eow_block_map->relocation_log_area_offset );
295
296
    libcnotify_printf(
297
     "%s: first block record offset\t\t: 0x%08" PRIx32 "\n",
298
     function,
299
     eow_block_map->first_block_record_offset );
300
301
    libcnotify_printf(
302
     "%s: second block record offset\t\t: 0x%08" PRIx32 "\n",
303
     function,
304
     eow_block_map->second_block_record_offset );
305
306
    libcnotify_printf(
307
     "%s: block record size\t\t\t: %" PRIu32 "\n",
308
     function,
309
     eow_block_map->block_record_size );
310
311
    byte_stream_copy_to_uint32_little_endian(
312
     ( (bde_eow_block_map_t *) data )->checksum,
313
     value_32bit );
314
    libcnotify_printf(
315
     "%s: checksum\t\t\t\t: 0x%08" PRIx32 "\n",
316
     function,
317
     value_32bit );
318
319
    libcnotify_printf(
320
     "\n" );
321
  }
322
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
323
324
965
  if( header_size != 60 )
325
4
  {
326
4
    libcerror_error_set(
327
4
     error,
328
4
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
329
4
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
330
4
     "%s: unsupported header size.",
331
4
     function );
332
333
4
    return( -1 );
334
4
  }
335
961
  return( 1 );
336
965
}
337
338
/* Reads a Encrypt-on-Write (EOW) block map
339
 * Returns 1 if successful or -1 on error
340
 */
341
int libbde_eow_block_map_read_file_io_handle(
342
     libbde_eow_block_map_t *eow_block_map,
343
     libbfio_handle_t *file_io_handle,
344
     off64_t file_offset,
345
     uint32_t block_map_size,
346
     libcerror_error_t **error )
347
1.24k
{
348
1.24k
  uint8_t *eow_block_map_data = NULL;
349
1.24k
  static char *function       = "libbde_eow_block_map_read_file_io_handle";
350
1.24k
  ssize_t read_count          = 0;
351
352
1.24k
  if( eow_block_map == NULL )
353
0
  {
354
0
    libcerror_error_set(
355
0
     error,
356
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
357
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
358
0
     "%s: invalid Encrypt-on-Write (EOW) block map.",
359
0
     function );
360
361
0
    return( -1 );
362
0
  }
363
1.24k
  if( ( block_map_size < sizeof( bde_eow_block_map_t ) )
364
1.23k
   || ( block_map_size > (uint32_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
365
40
  {
366
40
    libcerror_error_set(
367
40
     error,
368
40
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
369
40
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
370
40
     "%s: invalid block map size value out of bounds.",
371
40
     function );
372
373
40
    return( -1 );
374
40
  }
375
1.20k
  eow_block_map_data = (uint8_t *) memory_allocate(
376
1.20k
                                    sizeof( uint8_t ) * (size_t) block_map_size );
377
378
1.20k
  if( eow_block_map_data == NULL )
379
0
  {
380
0
    libcerror_error_set(
381
0
     error,
382
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
383
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
384
0
     "%s: unable to create block map data.",
385
0
     function );
386
387
0
    goto on_error;
388
0
  }
389
1.20k
  read_count = libbfio_handle_read_buffer_at_offset(
390
1.20k
                file_io_handle,
391
1.20k
                eow_block_map_data,
392
1.20k
                (size_t) block_map_size,
393
1.20k
                file_offset,
394
1.20k
                error );
395
396
1.20k
  if( read_count != (ssize_t) block_map_size )
397
184
  {
398
184
    libcerror_error_set(
399
184
     error,
400
184
     LIBCERROR_ERROR_DOMAIN_IO,
401
184
     LIBCERROR_IO_ERROR_READ_FAILED,
402
184
     "%s: unable to read Encrypt-on-Write (EOW) block map data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
403
184
     function,
404
184
     file_offset,
405
184
     file_offset );
406
407
184
    goto on_error;
408
184
  }
409
1.02k
  if( libbde_eow_block_map_read_data(
410
1.02k
       eow_block_map,
411
1.02k
       eow_block_map_data,
412
1.02k
       sizeof( bde_eow_block_map_t ),
413
1.02k
       error ) != 1 )
414
59
  {
415
59
    libcerror_error_set(
416
59
     error,
417
59
     LIBCERROR_ERROR_DOMAIN_IO,
418
59
     LIBCERROR_IO_ERROR_READ_FAILED,
419
59
     "%s: unable to read Encrypt-on-Write (EOW) block map.",
420
59
     function );
421
422
59
    goto on_error;
423
59
  }
424
961
  memory_free(
425
961
   eow_block_map_data );
426
427
961
  return( 1 );
428
429
243
on_error:
430
243
  if( eow_block_map_data != NULL )
431
243
  {
432
243
    memory_free(
433
243
     eow_block_map_data );
434
243
  }
435
243
  return( -1 );
436
1.02k
}
437