Coverage Report

Created: 2025-06-22 07:35

/src/libfcrypto/libfcrypto/libfcrypto_des3_context.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * DES3 (de/en)crypt functions
3
 *
4
 * Copyright (C) 2017-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 "libfcrypto_des3_context.h"
28
#include "libfcrypto_definitions.h"
29
#include "libfcrypto_libcerror.h"
30
31
static uint8_t libfcrypto_des3_permutation_table[ 64 ] = {
32
  58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 
33
  62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 
34
  57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 
35
  61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 };
36
37
static uint8_t libfcrypto_des3_inverse_permutation_table[ 64 ] = {
38
  40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 
39
  38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 
40
  36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 
41
  34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 };
42
43
static uint8_t libfcrypto_des3_expansion_table[ 48 ] = {
44
  32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11,
45
  12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21,
46
  22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 };
47
48
static uint8_t libfcrypto_des3_post_sbox_permulation[ 32 ] = {
49
  16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 
50
  2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 };
51
52
static uint8_t libfcrypto_des3_sboxes[ 8 ][ 64 ] = {
53
  { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 
54
    0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 
55
    4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 
56
    15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 },
57
  { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 
58
    3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 
59
    0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 
60
    13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 },
61
  { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 
62
    13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 
63
    13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
64
    1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 },
65
  { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 
66
    13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 
67
    10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
68
    3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 },
69
  { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 
70
    14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 
71
    4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 
72
    11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 },
73
  { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
74
    10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
75
    9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
76
    4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 },
77
  { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
78
    13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
79
    1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
80
    6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 },
81
  { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
82
    1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
83
    7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
84
    2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };
85
86
static uint8_t libfcrypto_des3_permuted_choice_table1[ 56 ] = {
87
  57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2,
88
  59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39,
89
  31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37,
90
  29, 21, 13, 5, 28, 20, 12, 4 };
91
92
static uint8_t libfcrypto_des3_permuted_choice_table2[ 48 ] = {
93
  14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4,
94
  26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40,
95
  51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 };
96
97
static uint8_t libfcrypto_des3_iteration_shift[ 16 ] = {
98
  1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
99
100
/* Creates a DES3 context
101
 * Make sure the value context is referencing, is set to NULL
102
 * Returns 1 if successful or -1 on error
103
 */
104
int libfcrypto_des3_context_initialize(
105
     libfcrypto_des3_context_t **context,
106
     libcerror_error_t **error )
107
112
{
108
112
  libfcrypto_internal_des3_context_t *internal_context = NULL;
109
112
  static char *function                                = "libfcrypto_des3_context_initialize";
110
111
112
  if( context == NULL )
112
0
  {
113
0
    libcerror_error_set(
114
0
     error,
115
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
116
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
117
0
     "%s: invalid context.",
118
0
     function );
119
120
0
    return( -1 );
121
0
  }
122
112
  if( *context != NULL )
123
0
  {
124
0
    libcerror_error_set(
125
0
     error,
126
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
127
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
128
0
     "%s: invalid context value already set.",
129
0
     function );
130
131
0
    return( -1 );
132
0
  }
133
112
  internal_context = memory_allocate_structure(
134
112
                      libfcrypto_internal_des3_context_t );
135
136
112
  if( internal_context == NULL )
137
0
  {
138
0
    libcerror_error_set(
139
0
     error,
140
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
141
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
142
0
     "%s: unable to create context.",
143
0
     function );
144
145
0
    goto on_error;
146
0
  }
147
112
  if( memory_set(
148
112
       internal_context,
149
112
       0,
150
112
       sizeof( libfcrypto_internal_des3_context_t ) ) == NULL )
151
0
  {
152
0
    libcerror_error_set(
153
0
     error,
154
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
155
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
156
0
     "%s: unable to clear context.",
157
0
     function );
158
159
0
    goto on_error;
160
0
  }
161
112
  *context = (libfcrypto_des3_context_t *) internal_context;
162
163
112
  return( 1 );
164
165
0
on_error:
166
0
  if( internal_context != NULL )
167
0
  {
168
0
    memory_free(
169
0
     internal_context );
170
0
  }
171
0
  return( -1 );
172
112
}
173
174
/* Frees a DES3 context
175
 * Returns 1 if successful or -1 on error
176
 */
177
int libfcrypto_des3_context_free(
178
     libfcrypto_des3_context_t **context,
179
     libcerror_error_t **error )
180
112
{
181
112
  libfcrypto_internal_des3_context_t *internal_context = NULL;
182
112
  static char *function                                = "libfcrypto_des3_context_free";
183
112
  int result                                           = 1;
184
185
112
  if( context == NULL )
186
0
  {
187
0
    libcerror_error_set(
188
0
     error,
189
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
190
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
191
0
     "%s: invalid context.",
192
0
     function );
193
194
0
    return( -1 );
195
0
  }
196
112
  if( *context != NULL )
197
112
  {
198
112
    internal_context = (libfcrypto_internal_des3_context_t *) *context;
199
112
    *context         = NULL;
200
201
112
    if( memory_set(
202
112
         internal_context,
203
112
         0,
204
112
         sizeof( libfcrypto_internal_des3_context_t ) ) == NULL )
205
0
    {
206
0
      libcerror_error_set(
207
0
       error,
208
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
209
0
       LIBCERROR_MEMORY_ERROR_SET_FAILED,
210
0
       "%s: unable to clear context.",
211
0
       function );
212
213
0
      result = -1;
214
0
    }
215
112
    memory_free(
216
112
     internal_context );
217
112
  }
218
112
  return( result );
219
112
}
220
221
/* Sets the key
222
 * Returns 1 if successful or -1 on error
223
 */
224
int libfcrypto_des3_context_set_key(
225
     libfcrypto_des3_context_t *context,
226
     const uint8_t *key,
227
     size_t key_bit_size,
228
     libcerror_error_t **error )
229
112
{
230
112
  libfcrypto_internal_des3_context_t *internal_context = NULL;
231
112
  static char *function                                = "libfcrypto_des3_context_set_key";
232
112
  uint64_t value_64bit                                 = 0;
233
234
112
  if( context == NULL )
235
0
  {
236
0
    libcerror_error_set(
237
0
     error,
238
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
239
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
240
0
     "%s: invalid context.",
241
0
     function );
242
243
0
    return( -1 );
244
0
  }
245
112
  internal_context = (libfcrypto_internal_des3_context_t *) context;
246
247
112
  if( key == NULL )
248
0
  {
249
0
    libcerror_error_set(
250
0
     error,
251
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
252
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
253
0
     "%s: invalid key.",
254
0
     function );
255
256
0
    return( -1 );
257
0
  }
258
  /* 56, 112 and 168 are sizes of keys without odd-parity bits
259
   * 64, 128 and 192 are sizes of keys with odd-parity bits
260
   */
261
112
  if( ( key_bit_size != 56 )
262
112
   && ( key_bit_size != 64 )
263
112
   && ( key_bit_size != 112 )
264
112
   && ( key_bit_size != 128 )
265
112
   && ( key_bit_size != 168 )
266
112
   && ( key_bit_size != 192 ) )
267
0
  {
268
0
    libcerror_error_set(
269
0
     error,
270
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
271
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
272
0
     "%s: unsupported key bit size.",
273
0
     function );
274
275
0
    return( -1 );
276
0
  }
277
112
  byte_stream_copy_to_uint64_big_endian(
278
112
   &( key[ 0 ] ),
279
112
   value_64bit );
280
281
112
  if( ( key_bit_size == 56 )
282
112
   || ( key_bit_size == 112 )
283
112
   || ( key_bit_size == 168 ) )
284
0
  {
285
0
    value_64bit >>= 8;
286
0
  }
287
112
  internal_context->keys[ 0 ] = value_64bit;
288
289
112
  if( ( key_bit_size == 56 )
290
112
   || ( key_bit_size == 64 ) )
291
112
  {
292
112
    value_64bit = internal_context->keys[ 0 ];
293
112
  }
294
0
  else if( ( key_bit_size == 112 )
295
0
        || ( key_bit_size == 168 ) )
296
0
  {
297
0
    byte_stream_copy_to_uint64_big_endian(
298
0
     &( key[ 7 ] ),
299
0
     value_64bit );
300
301
0
    value_64bit >>= 8;
302
0
  }
303
0
  else
304
0
  {
305
0
    byte_stream_copy_to_uint64_big_endian(
306
0
     &( key[ 8 ] ),
307
0
     value_64bit );
308
0
  }
309
112
  internal_context->keys[ 1 ] = value_64bit;
310
311
112
  if( ( key_bit_size == 56 )
312
112
   || ( key_bit_size == 64 )
313
112
   || ( key_bit_size == 112 )
314
112
   || ( key_bit_size == 128 ) )
315
112
  {
316
112
    value_64bit = internal_context->keys[ 0 ];
317
112
  }
318
0
  else if( key_bit_size == 168 )
319
0
  {
320
0
    byte_stream_copy_to_uint48_big_endian(
321
0
     &( key[ 14 ] ),
322
0
     value_64bit );
323
324
0
    value_64bit <<= 8;
325
0
    value_64bit  |= key[ 20 ];
326
0
  }
327
0
  else
328
0
  {
329
0
    byte_stream_copy_to_uint64_big_endian(
330
0
     &( key[ 16 ] ),
331
0
     value_64bit );
332
0
  }
333
112
  internal_context->keys[ 2 ] = value_64bit;
334
335
112
  return( 1 );
336
112
}
337
338
/* De- or encrypts a block of data using DES3
339
 * The size must be a multitude of the DES3 block size (8 byte)
340
 * Returns 1 if successful or -1 on error
341
 */
342
int libfcrypto_internal_des3_context_crypt_block(
343
     libfcrypto_internal_des3_context_t *internal_context,
344
     uint64_t key_value,
345
     int mode,
346
     uint64_t input_value,
347
     uint64_t *output_value,
348
     libcerror_error_t **error )
349
216
{
350
216
  uint64_t sub_keys[ 16 ];
351
352
216
  static char *function                = "libfcrypto_internal_des3_context_crypt_block";
353
216
  uint64_t bit_mask                    = 0;
354
216
  uint64_t permuted_output_value       = 0;
355
216
  uint64_t value_64bit                 = 0;
356
216
  uint32_t function_result             = 0;
357
216
  uint32_t permutation_lower_32bit     = 0;
358
216
  uint32_t permutation_upper_32bit     = 0;
359
216
  uint32_t permuted_choice_lower_32bit = 0;
360
216
  uint32_t permuted_choice_upper_32bit = 0;
361
216
  uint32_t sbox_output                 = 0;
362
216
  uint32_t value_32bit                 = 0;
363
216
  uint8_t bit_shift                    = 0;
364
216
  uint8_t column_bit_mask              = 0;
365
216
  uint8_t crypt_key_index              = 0;
366
216
  uint8_t iteration_shift              = 0;
367
216
  uint8_t row_bit_mask                 = 0;
368
216
  uint8_t sbox_index                   = 0;
369
216
  uint8_t sub_key_index                = 0;
370
216
  uint8_t table_index                  = 0;
371
372
216
  if( internal_context == NULL )
373
0
  {
374
0
    libcerror_error_set(
375
0
     error,
376
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
377
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
378
0
     "%s: invalid context.",
379
0
     function );
380
381
0
    return( -1 );
382
0
  }
383
216
  if( ( mode != LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT )
384
216
   && ( mode != LIBFCRYPTO_DES3_CRYPT_MODE_DECRYPT ) )
385
0
  {
386
0
    libcerror_error_set(
387
0
     error,
388
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
389
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
390
0
     "%s: unsupported mode.",
391
0
     function );
392
393
0
    return( -1 );
394
0
  }
395
216
  if( output_value == NULL )
396
0
  {
397
0
    libcerror_error_set(
398
0
     error,
399
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
400
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
401
0
     "%s: invalid output value.",
402
0
     function );
403
404
0
    return( -1 );
405
0
  }
406
  /* Calculate the permutation
407
   */
408
216
  value_64bit = 0;
409
410
216
  for( table_index = 0;
411
14.0k
       table_index < 64;
412
13.8k
       table_index++ )
413
13.8k
  {
414
13.8k
    bit_shift = 64 - libfcrypto_des3_permutation_table[ table_index ];
415
416
13.8k
    value_64bit <<= 1;
417
13.8k
    value_64bit  |= ( input_value >> bit_shift ) & 1ULL;
418
13.8k
  }
419
216
  permutation_upper_32bit = (uint32_t) ( ( value_64bit >> 32 ) & 0xffffffffUL );
420
216
  permutation_lower_32bit = (uint32_t) ( value_64bit & 0xffffffffUL );
421
422
  /* Calculate the key schedule
423
   */
424
216
  value_64bit = 0;
425
426
216
  for( table_index = 0;
427
12.3k
       table_index < 56;
428
12.0k
       table_index++ )
429
12.0k
  {
430
12.0k
    bit_shift = 64 - libfcrypto_des3_permuted_choice_table1[ table_index ];
431
432
12.0k
    value_64bit <<= 1;
433
12.0k
    value_64bit  |= ( key_value >> bit_shift ) & 1ULL;
434
12.0k
  }
435
216
  permuted_choice_upper_32bit = (uint32_t) ( ( value_64bit >> 28 ) & 0x0fffffffUL );
436
216
  permuted_choice_lower_32bit = (uint32_t) ( value_64bit & 0x0fffffffUL );
437
438
  /* Calculate the 16 sub keys
439
   */
440
216
  for( sub_key_index = 0;
441
3.67k
       sub_key_index < 16;
442
3.45k
       sub_key_index++ )
443
3.45k
  {
444
3.45k
    iteration_shift = libfcrypto_des3_iteration_shift[ sub_key_index ];
445
446
3.45k
    permuted_choice_upper_32bit = ( ( permuted_choice_upper_32bit << 1 ) & 0x0fffffffUL ) | ( ( permuted_choice_upper_32bit >> 27 ) & 0x00000001UL );
447
3.45k
    permuted_choice_lower_32bit = ( ( permuted_choice_lower_32bit << 1 ) & 0x0fffffffUL ) | ( ( permuted_choice_lower_32bit >> 27 ) & 0x00000001UL );
448
449
3.45k
    if( iteration_shift == 2 )
450
2.59k
    {
451
2.59k
      permuted_choice_upper_32bit = ( ( permuted_choice_upper_32bit << 1 ) & 0x0fffffffUL ) | ( ( permuted_choice_upper_32bit >> 27 ) & 0x00000001UL );
452
2.59k
      permuted_choice_lower_32bit = ( ( permuted_choice_lower_32bit << 1 ) & 0x0fffffffUL ) | ( ( permuted_choice_lower_32bit >> 27 ) & 0x00000001UL );
453
2.59k
    }
454
3.45k
    value_64bit = ( (uint64_t) permuted_choice_upper_32bit << 28 ) | permuted_choice_lower_32bit;
455
        
456
3.45k
    sub_keys[ sub_key_index ] = 0;
457
        
458
3.45k
    for( table_index = 0;
459
169k
         table_index < 48;
460
165k
         table_index++ )
461
165k
    {
462
165k
      bit_shift = 56 - libfcrypto_des3_permuted_choice_table2[ table_index ];
463
464
165k
      sub_keys[ sub_key_index ] <<= 1;
465
165k
      sub_keys[ sub_key_index ]  |= ( value_64bit >> bit_shift ) & 1ULL;
466
165k
    }
467
3.45k
  }
468
216
  sbox_output = 0;
469
470
216
  for( sub_key_index = 0;
471
3.67k
       sub_key_index < 16;
472
3.45k
       sub_key_index++ )
473
3.45k
  {
474
3.45k
    if( mode == LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT )
475
2.30k
    {
476
2.30k
      crypt_key_index = sub_key_index;
477
2.30k
    }
478
1.15k
    else
479
1.15k
    {
480
1.15k
      crypt_key_index = 15 - sub_key_index;
481
1.15k
    }
482
3.45k
    value_64bit = 0;
483
        
484
3.45k
    for( table_index = 0;
485
169k
         table_index < 48;
486
165k
         table_index++ )
487
165k
    {
488
165k
      bit_shift = 32 - libfcrypto_des3_expansion_table[ table_index ];
489
490
165k
      value_64bit <<= 1;
491
165k
      value_64bit  |= ( permutation_lower_32bit >> bit_shift ) & 1ULL;
492
165k
    }
493
3.45k
    value_64bit ^= sub_keys[ crypt_key_index ];
494
495
3.45k
    for( table_index = 0;
496
31.1k
         table_index < 8;
497
27.6k
         table_index++ )
498
27.6k
    {
499
27.6k
      sbox_index = table_index * 6;
500
501
27.6k
      bit_mask  = 0x0000840000000000ULL >> sbox_index;
502
27.6k
      bit_shift = 42 - sbox_index;
503
504
27.6k
      row_bit_mask = (uint8_t) ( ( value_64bit & bit_mask ) >> bit_shift );
505
27.6k
      row_bit_mask = ( row_bit_mask >> 4 ) | ( row_bit_mask & 0x01 );
506
507
27.6k
      bit_mask  = 0x0000780000000000ULL >> sbox_index;
508
27.6k
      bit_shift = 43 - sbox_index;
509
510
27.6k
      column_bit_mask = (uint8_t) ( ( value_64bit & bit_mask ) >> bit_shift );
511
512
27.6k
      sbox_index = ( row_bit_mask << 4 ) | column_bit_mask;
513
            
514
27.6k
      sbox_output <<= 4;
515
27.6k
      sbox_output  |= libfcrypto_des3_sboxes[ table_index ][ sbox_index ] & 0x0f;
516
27.6k
    }
517
3.45k
    function_result = 0;
518
519
3.45k
    for( table_index = 0;
520
114k
         table_index < 32;
521
110k
         table_index++ )
522
110k
    {
523
110k
      bit_shift = 32 - libfcrypto_des3_post_sbox_permulation[ table_index ];
524
525
110k
      function_result <<= 1;
526
110k
      function_result  |= ( sbox_output >> bit_shift ) & 1UL;
527
110k
    }
528
3.45k
    value_32bit             = permutation_lower_32bit;
529
3.45k
    permutation_lower_32bit = permutation_upper_32bit ^ function_result;
530
3.45k
    permutation_upper_32bit = value_32bit;
531
3.45k
  }
532
216
  permuted_output_value = ( (uint64_t) permutation_lower_32bit << 32 ) | permutation_upper_32bit;
533
534
  /* Calculate the inverse permutation
535
   */
536
216
  value_64bit = 0;
537
538
216
  for( table_index = 0;
539
14.0k
       table_index < 64;
540
13.8k
       table_index++ )
541
13.8k
  {
542
13.8k
    bit_shift = 64 - libfcrypto_des3_inverse_permutation_table[ table_index ];
543
544
13.8k
    value_64bit <<= 1;
545
13.8k
    value_64bit  |= ( permuted_output_value >> bit_shift ) & 1ULL;
546
13.8k
  }
547
216
  *output_value = value_64bit;
548
549
216
  return( 1 );
550
216
}
551
552
/* De- or encrypts a block of data using DES3-CBC (Cipher Block Chaining)
553
 * The size must be a multitude of the DES3 block size (8 byte)
554
 * Returns 1 if successful or -1 on error
555
 */
556
int libfcrypto_des3_crypt_cbc(
557
     libfcrypto_des3_context_t *context,
558
     int mode,
559
     const uint8_t *initialization_vector,
560
     size_t initialization_vector_size,
561
     const uint8_t *input_data,
562
     size_t input_data_size,
563
     uint8_t *output_data,
564
     size_t output_data_size,
565
     libcerror_error_t **error )
566
56
{
567
56
  uint8_t block_data[ 8 ];
568
56
  uint8_t internal_initialization_vector[ 8 ];
569
570
56
  libfcrypto_internal_des3_context_t *internal_context = NULL;
571
56
  static char *function                                = "libfcrypto_des3_crypt_cbc";
572
56
  size_t data_offset                                   = 0;
573
56
  uint64_t value_64bit                                 = 0;
574
575
56
#if !defined( LIBFCRYPTO_UNFOLLED_LOOPS )
576
56
  uint8_t block_index                                  = 0;
577
56
#endif
578
579
56
  if( context == NULL )
580
0
  {
581
0
    libcerror_error_set(
582
0
     error,
583
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
584
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
585
0
     "%s: invalid context.",
586
0
     function );
587
588
0
    return( -1 );
589
0
  }
590
56
  internal_context = (libfcrypto_internal_des3_context_t *) context;
591
592
56
  if( initialization_vector == NULL )
593
0
  {
594
0
    libcerror_error_set(
595
0
     error,
596
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
597
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
598
0
     "%s: invalid initialization vector.",
599
0
     function );
600
601
0
    return( -1 );
602
0
  }
603
56
  if( initialization_vector_size != 8 )
604
0
  {
605
0
    libcerror_error_set(
606
0
     error,
607
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
608
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
609
0
     "%s: invalid initialization vector size value out of bounds.",
610
0
     function );
611
612
0
    return( -1 );
613
0
  }
614
56
  if( ( mode != LIBFCRYPTO_BLOWFISH_CRYPT_MODE_ENCRYPT )
615
56
   && ( mode != LIBFCRYPTO_BLOWFISH_CRYPT_MODE_DECRYPT ) )
616
0
  {
617
0
    libcerror_error_set(
618
0
     error,
619
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
620
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
621
0
     "%s: unsupported mode.",
622
0
     function );
623
624
0
    return( -1 );
625
0
  }
626
56
  if( input_data == NULL )
627
0
  {
628
0
    libcerror_error_set(
629
0
     error,
630
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
631
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
632
0
     "%s: invalid input data.",
633
0
     function );
634
635
0
    return( -1 );
636
0
  }
637
  /* Check if the input data size is a multitude of 8-byte
638
   */
639
56
  if( ( ( input_data_size & (size_t) 0x07 ) != 0 )
640
56
   || ( input_data_size < 8 )
641
56
   || ( input_data_size > (size_t) SSIZE_MAX ) )
642
3
  {
643
3
    libcerror_error_set(
644
3
     error,
645
3
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
646
3
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
647
3
     "%s: invalid input data size value out of bounds.",
648
3
     function );
649
650
3
    return( -1 );
651
3
  }
652
53
  if( output_data == NULL )
653
0
  {
654
0
    libcerror_error_set(
655
0
     error,
656
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
657
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
658
0
     "%s: invalid output data.",
659
0
     function );
660
661
0
    return( -1 );
662
0
  }
663
53
  if( ( output_data_size < input_data_size )
664
53
   || ( output_data_size > (size_t) SSIZE_MAX ) )
665
45
  {
666
45
    libcerror_error_set(
667
45
     error,
668
45
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
669
45
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
670
45
     "%s: invalid output data size value out of bounds.",
671
45
     function );
672
673
45
    return( -1 );
674
45
  }
675
8
  if( memory_copy(
676
8
       internal_initialization_vector,
677
8
       initialization_vector,
678
8
       8 ) == NULL )
679
0
  {
680
0
    libcerror_error_set(
681
0
     error,
682
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
683
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
684
0
     "%s: unable to copy initialization vector.",
685
0
     function );
686
687
0
    goto on_error;
688
0
  }
689
44
  while( data_offset < input_data_size )
690
36
  {
691
36
    if( mode == LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT )
692
36
    {
693
#if defined( LIBFCRYPTO_UNFOLLED_LOOPS )
694
      block_data[ 0 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 0 ];
695
      block_data[ 1 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 1 ];
696
      block_data[ 2 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 2 ];
697
      block_data[ 3 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 3 ];
698
      block_data[ 4 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 4 ];
699
      block_data[ 5 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 5 ];
700
      block_data[ 6 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 6 ];
701
      block_data[ 7 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 7 ];
702
#else
703
36
      for( block_index = 0;
704
324
           block_index < 8;
705
288
           block_index++ )
706
288
      {
707
288
        block_data[ block_index ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ block_index ];
708
288
      }
709
36
#endif
710
36
      data_offset -= 8;
711
712
36
      byte_stream_copy_to_uint64_big_endian(
713
36
       block_data,
714
36
       value_64bit );
715
716
36
      if( libfcrypto_internal_des3_context_crypt_block(
717
36
           internal_context,
718
36
           internal_context->keys[ 0 ],
719
36
           LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT,
720
36
           value_64bit,
721
36
           &value_64bit,
722
36
           error ) != 1 )
723
0
      {
724
0
        libcerror_error_set(
725
0
         error,
726
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
727
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
728
0
         "%s: unable to encrypt input data with first key.",
729
0
         function );
730
731
0
        goto on_error;
732
0
      }
733
36
      if( libfcrypto_internal_des3_context_crypt_block(
734
36
           internal_context,
735
36
           internal_context->keys[ 1 ],
736
36
           LIBFCRYPTO_DES3_CRYPT_MODE_DECRYPT,
737
36
           value_64bit,
738
36
           &value_64bit,
739
36
           error ) != 1 )
740
0
      {
741
0
        libcerror_error_set(
742
0
         error,
743
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
744
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
745
0
         "%s: unable to decrypt input data with second key.",
746
0
         function );
747
748
0
        goto on_error;
749
0
      }
750
36
      if( libfcrypto_internal_des3_context_crypt_block(
751
36
           internal_context,
752
36
           internal_context->keys[ 2 ],
753
36
           LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT,
754
36
           value_64bit,
755
36
           &value_64bit,
756
36
           error ) != 1 )
757
0
      {
758
0
        libcerror_error_set(
759
0
         error,
760
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
761
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
762
0
         "%s: unable to encrypt input data with third key.",
763
0
         function );
764
765
0
        goto on_error;
766
0
      }
767
36
      byte_stream_copy_from_uint64_big_endian(
768
36
       &( output_data[ data_offset ] ),
769
36
       value_64bit );
770
771
36
      if( memory_copy(
772
36
           internal_initialization_vector,
773
36
           &( output_data[ data_offset ] ),
774
36
           8 ) == NULL )
775
0
      {
776
0
        libcerror_error_set(
777
0
         error,
778
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
779
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
780
0
         "%s: unable to copy encrypted output data to initialization vector.",
781
0
         function );
782
783
0
        goto on_error;
784
0
      }
785
36
    }
786
0
    else
787
0
    {
788
0
      byte_stream_copy_to_uint64_big_endian(
789
0
       &( input_data[ data_offset ] ),
790
0
       value_64bit );
791
792
0
      if( libfcrypto_internal_des3_context_crypt_block(
793
0
           internal_context,
794
0
           internal_context->keys[ 2 ],
795
0
           LIBFCRYPTO_DES3_CRYPT_MODE_DECRYPT,
796
0
           value_64bit,
797
0
           &value_64bit,
798
0
           error ) != 1 )
799
0
      {
800
0
        libcerror_error_set(
801
0
         error,
802
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
803
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
804
0
         "%s: unable to decrypt input data with third key.",
805
0
         function );
806
807
0
        goto on_error;
808
0
      }
809
0
      if( libfcrypto_internal_des3_context_crypt_block(
810
0
           internal_context,
811
0
           internal_context->keys[ 1 ],
812
0
           LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT,
813
0
           value_64bit,
814
0
           &value_64bit,
815
0
           error ) != 1 )
816
0
      {
817
0
        libcerror_error_set(
818
0
         error,
819
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
820
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
821
0
         "%s: unable to endrypt input data with second key.",
822
0
         function );
823
824
0
        goto on_error;
825
0
      }
826
0
      if( libfcrypto_internal_des3_context_crypt_block(
827
0
           internal_context,
828
0
           internal_context->keys[ 0 ],
829
0
           LIBFCRYPTO_DES3_CRYPT_MODE_DECRYPT,
830
0
           value_64bit,
831
0
           &value_64bit,
832
0
           error ) != 1 )
833
0
      {
834
0
        libcerror_error_set(
835
0
         error,
836
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
837
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
838
0
         "%s: unable to decrypt input data with first key.",
839
0
         function );
840
841
0
        goto on_error;
842
0
      }
843
0
      byte_stream_copy_from_uint64_big_endian(
844
0
       &( output_data[ data_offset ] ),
845
0
       value_64bit );
846
847
#if defined( LIBFCRYPTO_UNFOLLED_LOOPS )
848
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 0 ];
849
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 1 ];
850
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 2 ];
851
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 3 ];
852
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 4 ];
853
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 5 ];
854
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 6 ];
855
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 7 ];
856
#else
857
0
      for( block_index = 0;
858
0
           block_index < 8;
859
0
           block_index++ )
860
0
      {
861
0
        output_data[ data_offset++ ] ^= internal_initialization_vector[ block_index ];
862
0
      }
863
0
#endif
864
0
      data_offset -= 8;
865
866
0
      if( memory_copy(
867
0
           internal_initialization_vector,
868
0
           &( input_data[ data_offset ] ),
869
0
           8 ) == NULL )
870
0
      {
871
0
        libcerror_error_set(
872
0
         error,
873
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
874
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
875
0
         "%s: unable to copy encrypted input data to initialization vector.",
876
0
         function );
877
878
0
        goto on_error;
879
0
      }
880
0
    }
881
36
    data_offset += 8;
882
36
  }
883
8
  if( memory_set(
884
8
       internal_initialization_vector,
885
8
       0,
886
8
       8 ) == NULL )
887
0
  {
888
0
    libcerror_error_set(
889
0
     error,
890
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
891
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
892
0
     "%s: unable to clear initialization vector.",
893
0
     function );
894
895
0
    goto on_error;
896
0
  }
897
8
  if( memory_set(
898
8
       block_data,
899
8
       0,
900
8
       8 ) == NULL )
901
0
  {
902
0
    libcerror_error_set(
903
0
     error,
904
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
905
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
906
0
     "%s: unable to clear block data.",
907
0
     function );
908
909
0
    goto on_error;
910
0
  }
911
8
  return( 1 );
912
913
0
on_error:
914
0
  memory_set(
915
0
   internal_initialization_vector,
916
0
   0,
917
0
   8 );
918
919
0
  memory_set(
920
0
   block_data,
921
0
   0,
922
0
   8 );
923
924
0
  return( -1 );
925
8
}
926
927
/* De- or encrypts a block of data using DES3-ECB (Electronic CodeBook)
928
 * The size must be a multitude of the DES3 block size (8 byte)
929
 * Returns 1 if successful or -1 on error
930
 */
931
int libfcrypto_des3_crypt_ecb(
932
     libfcrypto_des3_context_t *context,
933
     int mode,
934
     const uint8_t *input_data,
935
     size_t input_data_size,
936
     uint8_t *output_data,
937
     size_t output_data_size,
938
     libcerror_error_t **error )
939
56
{
940
56
  libfcrypto_internal_des3_context_t *internal_context = NULL;
941
56
  static char *function                                = "libfcrypto_des3_crypt_ecb";
942
56
  size_t data_offset                                   = 0;
943
56
  uint64_t value_64bit                                 = 0;
944
945
56
  if( context == NULL )
946
0
  {
947
0
    libcerror_error_set(
948
0
     error,
949
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
950
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
951
0
     "%s: invalid context.",
952
0
     function );
953
954
0
    return( -1 );
955
0
  }
956
56
  internal_context = (libfcrypto_internal_des3_context_t *) context;
957
958
56
  if( ( mode != LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT )
959
56
   && ( mode != LIBFCRYPTO_DES3_CRYPT_MODE_DECRYPT ) )
960
0
  {
961
0
    libcerror_error_set(
962
0
     error,
963
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
964
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
965
0
     "%s: unsupported mode.",
966
0
     function );
967
968
0
    return( -1 );
969
0
  }
970
56
  if( input_data == NULL )
971
0
  {
972
0
    libcerror_error_set(
973
0
     error,
974
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
975
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
976
0
     "%s: invalid input data.",
977
0
     function );
978
979
0
    return( -1 );
980
0
  }
981
  /* Check if the input data size is a multitude of 8-byte
982
   */
983
56
  if( ( ( input_data_size & (size_t) 0x07 ) != 0 )
984
56
   || ( input_data_size < 8 )
985
56
   || ( input_data_size > (size_t) SSIZE_MAX ) )
986
3
  {
987
3
    libcerror_error_set(
988
3
     error,
989
3
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
990
3
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
991
3
     "%s: invalid input data size value out of bounds.",
992
3
     function );
993
994
3
    return( -1 );
995
3
  }
996
53
  if( output_data == NULL )
997
0
  {
998
0
    libcerror_error_set(
999
0
     error,
1000
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1001
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1002
0
     "%s: invalid output data.",
1003
0
     function );
1004
1005
0
    return( -1 );
1006
0
  }
1007
53
  if( ( output_data_size < input_data_size )
1008
53
   || ( output_data_size > (size_t) SSIZE_MAX ) )
1009
45
  {
1010
45
    libcerror_error_set(
1011
45
     error,
1012
45
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1013
45
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1014
45
     "%s: invalid output data size value out of bounds.",
1015
45
     function );
1016
1017
45
    return( -1 );
1018
45
  }
1019
44
  while( data_offset < input_data_size )
1020
36
  {
1021
36
    byte_stream_copy_to_uint64_big_endian(
1022
36
     &( input_data[ data_offset ] ),
1023
36
     value_64bit );
1024
1025
36
    if( mode == LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT )
1026
36
    {
1027
36
      if( libfcrypto_internal_des3_context_crypt_block(
1028
36
           internal_context,
1029
36
           internal_context->keys[ 0 ],
1030
36
           LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT,
1031
36
           value_64bit,
1032
36
           &value_64bit,
1033
36
           error ) != 1 )
1034
0
      {
1035
0
        libcerror_error_set(
1036
0
         error,
1037
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1038
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1039
0
         "%s: unable to encrypt input data with first key.",
1040
0
         function );
1041
1042
0
        return( -1 );
1043
0
      }
1044
36
      if( libfcrypto_internal_des3_context_crypt_block(
1045
36
           internal_context,
1046
36
           internal_context->keys[ 1 ],
1047
36
           LIBFCRYPTO_DES3_CRYPT_MODE_DECRYPT,
1048
36
           value_64bit,
1049
36
           &value_64bit,
1050
36
           error ) != 1 )
1051
0
      {
1052
0
        libcerror_error_set(
1053
0
         error,
1054
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1055
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1056
0
         "%s: unable to decrypt input data with second key.",
1057
0
         function );
1058
1059
0
        return( -1 );
1060
0
      }
1061
36
      if( libfcrypto_internal_des3_context_crypt_block(
1062
36
           internal_context,
1063
36
           internal_context->keys[ 2 ],
1064
36
           LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT,
1065
36
           value_64bit,
1066
36
           &value_64bit,
1067
36
           error ) != 1 )
1068
0
      {
1069
0
        libcerror_error_set(
1070
0
         error,
1071
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1072
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1073
0
         "%s: unable to encrypt input data with third key.",
1074
0
         function );
1075
1076
0
        return( -1 );
1077
0
      }
1078
36
    }
1079
0
    else
1080
0
    {
1081
0
      if( libfcrypto_internal_des3_context_crypt_block(
1082
0
           internal_context,
1083
0
           internal_context->keys[ 2 ],
1084
0
           LIBFCRYPTO_DES3_CRYPT_MODE_DECRYPT,
1085
0
           value_64bit,
1086
0
           &value_64bit,
1087
0
           error ) != 1 )
1088
0
      {
1089
0
        libcerror_error_set(
1090
0
         error,
1091
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1092
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1093
0
         "%s: unable to decrypt input data with third key.",
1094
0
         function );
1095
1096
0
        return( -1 );
1097
0
      }
1098
0
      if( libfcrypto_internal_des3_context_crypt_block(
1099
0
           internal_context,
1100
0
           internal_context->keys[ 1 ],
1101
0
           LIBFCRYPTO_DES3_CRYPT_MODE_ENCRYPT,
1102
0
           value_64bit,
1103
0
           &value_64bit,
1104
0
           error ) != 1 )
1105
0
      {
1106
0
        libcerror_error_set(
1107
0
         error,
1108
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1109
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1110
0
         "%s: unable to endrypt input data with second key.",
1111
0
         function );
1112
1113
0
        return( -1 );
1114
0
      }
1115
0
      if( libfcrypto_internal_des3_context_crypt_block(
1116
0
           internal_context,
1117
0
           internal_context->keys[ 0 ],
1118
0
           LIBFCRYPTO_DES3_CRYPT_MODE_DECRYPT,
1119
0
           value_64bit,
1120
0
           &value_64bit,
1121
0
           error ) != 1 )
1122
0
      {
1123
0
        libcerror_error_set(
1124
0
         error,
1125
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1126
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1127
0
         "%s: unable to decrypt input data with first key.",
1128
0
         function );
1129
1130
0
        return( -1 );
1131
0
      }
1132
0
    }
1133
36
    byte_stream_copy_from_uint64_big_endian(
1134
36
     &( output_data[ data_offset ] ),
1135
36
     value_64bit );
1136
1137
36
    data_offset += 8;
1138
36
  }
1139
8
  return( 1 );
1140
8
}
1141
1142
/* De- or encrypts a buffer of data using DES3
1143
 * Returns 1 if successful or -1 on error
1144
 */
1145
int libfcrypto_des3_crypt(
1146
     libfcrypto_des3_context_t *context,
1147
     int mode,
1148
     const uint8_t *input_data,
1149
     size_t input_data_size,
1150
     uint8_t *output_data,
1151
     size_t output_data_size,
1152
     libcerror_error_t **error )
1153
0
{
1154
0
  static char *function = "libfcrypto_des3_crypt";
1155
1156
0
  if( libfcrypto_des3_crypt_ecb(
1157
0
       context,
1158
0
       mode,
1159
0
       input_data,
1160
0
       input_data_size,
1161
0
       output_data,
1162
0
       output_data_size,
1163
0
       error ) != 1 )
1164
0
  {
1165
0
    libcerror_error_set(
1166
0
     error,
1167
0
     LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1168
0
     LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1169
0
     "%s: unable to crypt data using DES3-ECB.",
1170
0
     function );
1171
1172
0
    return( -1 );
1173
0
  }
1174
0
  return( 1 );
1175
0
}
1176