Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfvde/libcaes/libcaes_context.c
Line
Count
Source
1
/*
2
 * AES de/encryption context 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
#if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_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
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
36
#include <openssl/core_names.h>
37
#endif
38
39
#include "libcaes_context.h"
40
#include "libcaes_definitions.h"
41
#include "libcaes_libcerror.h"
42
#include "libcaes_types.h"
43
44
#if !defined( LIBCAES_HAVE_AES_SUPPORT )
45
46
/* FIPS-197 compliant AES encryption functions
47
 *
48
 * The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
49
 *
50
 * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
51
 * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
52
 */
53
54
#define libcaes_calculate_forward_substitution_value( value, index0, index1, index2, index3 ) \
55
1.17M
  value   = libcaes_forward_substitution_box[ index3 ]; \
56
1.17M
  value <<= 8; \
57
1.17M
  value  |= libcaes_forward_substitution_box[ index2 ]; \
58
1.17M
  value <<= 8; \
59
1.17M
  value  |= libcaes_forward_substitution_box[ index1 ]; \
60
1.17M
  value <<= 8; \
61
1.17M
  value  |= libcaes_forward_substitution_box[ index0 ];
62
63
#define libcaes_calculate_forward_table_value( value, index0, index1, index2, index3 ) \
64
10.9M
  value  = libcaes_forward_table0[ index0 ]; \
65
10.9M
  value ^= libcaes_forward_table1[ index1 ]; \
66
10.9M
  value ^= libcaes_forward_table2[ index2 ]; \
67
10.9M
  value ^= libcaes_forward_table3[ index3 ];
68
69
#define libcaes_calculate_reverse_substitution_value( value, index0, index1, index2, index3 ) \
70
52.0M
  value   = libcaes_reverse_substitution_box[ index3 ]; \
71
52.0M
  value <<= 8; \
72
52.0M
  value  |= libcaes_reverse_substitution_box[ index2 ]; \
73
52.0M
  value <<= 8; \
74
52.0M
  value  |= libcaes_reverse_substitution_box[ index1 ]; \
75
52.0M
  value <<= 8; \
76
52.0M
  value  |= libcaes_reverse_substitution_box[ index0 ];
77
78
#define libcaes_calculate_reverse_table_value( value, index0, index1, index2, index3 ) \
79
471M
  value  = libcaes_reverse_table0[ index0 ]; \
80
471M
  value ^= libcaes_reverse_table1[ index1 ]; \
81
471M
  value ^= libcaes_reverse_table2[ index2 ]; \
82
471M
  value ^= libcaes_reverse_table3[ index3 ];
83
84
#define libcaes_calculate_forward_substitution_round( round_keys, values_32bit, cipher_values_32bit, substitution_value ) \
85
294k
  libcaes_calculate_forward_substitution_value( \
86
294k
   substitution_value, \
87
294k
   ( cipher_values_32bit[ 0 ] & 0xff ), \
88
294k
   ( ( cipher_values_32bit[ 1 ] >> 8 ) & 0xff ), \
89
294k
   ( ( cipher_values_32bit[ 2 ] >> 16 ) & 0xff ), \
90
294k
   ( ( cipher_values_32bit[ 3 ] >> 24 ) & 0xff ) ); \
91
294k
\
92
294k
  values_32bit[ 0 ] = round_keys[ 0 ] ^ substitution_value; \
93
294k
\
94
294k
  libcaes_calculate_forward_substitution_value( \
95
294k
   substitution_value, \
96
294k
   ( cipher_values_32bit[ 1 ] & 0xff ), \
97
294k
   ( ( cipher_values_32bit[ 2 ] >> 8 ) & 0xff ), \
98
294k
   ( ( cipher_values_32bit[ 3 ] >> 16 ) & 0xff ), \
99
294k
   ( ( cipher_values_32bit[ 0 ] >> 24 ) & 0xff ) ); \
100
294k
\
101
294k
  values_32bit[ 1 ] = round_keys[ 1 ] ^ substitution_value; \
102
294k
\
103
294k
  libcaes_calculate_forward_substitution_value( \
104
294k
   substitution_value, \
105
294k
   ( cipher_values_32bit[ 2 ] & 0xff ), \
106
294k
   ( ( cipher_values_32bit[ 3 ] >> 8 ) & 0xff ), \
107
294k
   ( ( cipher_values_32bit[ 0 ] >> 16 ) & 0xff ), \
108
294k
   ( ( cipher_values_32bit[ 1 ] >> 24 ) & 0xff ) ); \
109
294k
\
110
294k
  values_32bit[ 2 ] = round_keys[ 2 ] ^ substitution_value; \
111
294k
\
112
294k
  libcaes_calculate_forward_substitution_value( \
113
294k
   substitution_value, \
114
294k
   ( cipher_values_32bit[ 3 ] & 0xff ), \
115
294k
   ( ( cipher_values_32bit[ 0 ] >> 8 ) & 0xff ), \
116
294k
   ( ( cipher_values_32bit[ 1 ] >> 16 ) & 0xff ), \
117
294k
   ( ( cipher_values_32bit[ 2 ] >> 24 ) & 0xff ) ); \
118
294k
\
119
294k
  values_32bit[ 3 ] = round_keys[ 3 ] ^ substitution_value;
120
121
#define libcaes_calculate_forward_table_round( round_keys, values_32bit, cipher_values_32bit, table_value ) \
122
2.73M
  libcaes_calculate_forward_table_value( \
123
2.73M
   table_value, \
124
2.73M
   ( cipher_values_32bit[ 0 ] & 0xff ), \
125
2.73M
   ( ( cipher_values_32bit[ 1 ] >> 8 ) & 0xff ), \
126
2.73M
   ( ( cipher_values_32bit[ 2 ] >> 16 ) & 0xff ), \
127
2.73M
   ( ( cipher_values_32bit[ 3 ] >> 24 ) & 0xff ) ); \
128
2.73M
\
129
2.73M
  values_32bit[ 0 ] = round_keys[ 0 ] ^ table_value; \
130
2.73M
\
131
2.73M
  libcaes_calculate_forward_table_value( \
132
2.73M
   table_value, \
133
2.73M
   ( cipher_values_32bit[ 1 ] & 0xff ), \
134
2.73M
   ( ( cipher_values_32bit[ 2 ] >> 8 ) & 0xff ), \
135
2.73M
   ( ( cipher_values_32bit[ 3 ] >> 16 ) & 0xff ), \
136
2.73M
   ( ( cipher_values_32bit[ 0 ] >> 24 ) & 0xff ) ); \
137
2.73M
\
138
2.73M
  values_32bit[ 1 ] = round_keys[ 1 ] ^ table_value; \
139
2.73M
\
140
2.73M
  libcaes_calculate_forward_table_value( \
141
2.73M
   table_value, \
142
2.73M
   ( cipher_values_32bit[ 2 ] & 0xff ), \
143
2.73M
   ( ( cipher_values_32bit[ 3 ] >> 8 ) & 0xff ), \
144
2.73M
   ( ( cipher_values_32bit[ 0 ] >> 16 ) & 0xff ), \
145
2.73M
   ( ( cipher_values_32bit[ 1 ] >> 24 ) & 0xff ) ); \
146
2.73M
\
147
2.73M
  values_32bit[ 2 ] = round_keys[ 2 ] ^ table_value; \
148
2.73M
\
149
2.73M
  libcaes_calculate_forward_table_value( \
150
2.73M
   table_value, \
151
2.73M
   ( cipher_values_32bit[ 3 ] & 0xff ), \
152
2.73M
   ( ( cipher_values_32bit[ 0 ] >> 8 ) & 0xff ), \
153
2.73M
   ( ( cipher_values_32bit[ 1 ] >> 16 ) & 0xff ), \
154
2.73M
   ( ( cipher_values_32bit[ 2 ] >> 24 ) & 0xff ) ); \
155
2.73M
\
156
2.73M
  values_32bit[ 3 ] = round_keys[ 3 ] ^ table_value;
157
158
#define libcaes_calculate_reverse_substitution_round( round_keys, values_32bit, cipher_values_32bit, substitution_value ) \
159
13.0M
  libcaes_calculate_reverse_substitution_value( \
160
13.0M
   substitution_value, \
161
13.0M
   ( cipher_values_32bit[ 0 ] & 0xff ), \
162
13.0M
   ( ( cipher_values_32bit[ 3 ] >> 8 ) & 0xff ), \
163
13.0M
   ( ( cipher_values_32bit[ 2 ] >> 16 ) & 0xff ), \
164
13.0M
   ( ( cipher_values_32bit[ 1 ] >> 24 ) & 0xff ) ); \
165
13.0M
\
166
13.0M
  values_32bit[ 0 ] = round_keys[ 0 ] ^ substitution_value; \
167
13.0M
\
168
13.0M
  libcaes_calculate_reverse_substitution_value( \
169
13.0M
   substitution_value, \
170
13.0M
   ( cipher_values_32bit[ 1 ] & 0xff ), \
171
13.0M
   ( ( cipher_values_32bit[ 0 ] >> 8 ) & 0xff ), \
172
13.0M
   ( ( cipher_values_32bit[ 3 ] >> 16 ) & 0xff ), \
173
13.0M
   ( ( cipher_values_32bit[ 2 ] >> 24 ) & 0xff ) ); \
174
13.0M
\
175
13.0M
  values_32bit[ 1 ] = round_keys[ 1 ] ^ substitution_value; \
176
13.0M
\
177
13.0M
  libcaes_calculate_reverse_substitution_value( \
178
13.0M
   substitution_value, \
179
13.0M
   ( cipher_values_32bit[ 2 ] & 0xff ), \
180
13.0M
   ( ( cipher_values_32bit[ 1 ] >> 8 ) & 0xff ), \
181
13.0M
   ( ( cipher_values_32bit[ 0 ] >> 16 ) & 0xff ), \
182
13.0M
   ( ( cipher_values_32bit[ 3 ] >> 24 ) & 0xff ) ); \
183
13.0M
\
184
13.0M
  values_32bit[ 2 ] = round_keys[ 2 ] ^ substitution_value; \
185
13.0M
\
186
13.0M
  libcaes_calculate_reverse_substitution_value( \
187
13.0M
   substitution_value, \
188
13.0M
   ( cipher_values_32bit[ 3 ] & 0xff ), \
189
13.0M
   ( ( cipher_values_32bit[ 2 ] >> 8 ) & 0xff ), \
190
13.0M
   ( ( cipher_values_32bit[ 1 ] >> 16 ) & 0xff ), \
191
13.0M
   ( ( cipher_values_32bit[ 0 ] >> 24 ) & 0xff ) ); \
192
13.0M
\
193
13.0M
  values_32bit[ 3 ] = round_keys[ 3 ] ^ substitution_value;
194
195
#define libcaes_calculate_reverse_table_round( round_keys, values_32bit, cipher_values_32bit, table_value ) \
196
117M
  libcaes_calculate_reverse_table_value( \
197
117M
   table_value, \
198
117M
   ( cipher_values_32bit[ 0 ] & 0xff ), \
199
117M
   ( ( cipher_values_32bit[ 3 ] >> 8 ) & 0xff ), \
200
117M
   ( ( cipher_values_32bit[ 2 ] >> 16 ) & 0xff ), \
201
117M
   ( ( cipher_values_32bit[ 1 ] >> 24 ) & 0xff ) ); \
202
117M
\
203
117M
  values_32bit[ 0 ] = round_keys[ 0 ] ^ table_value; \
204
117M
\
205
117M
  libcaes_calculate_reverse_table_value( \
206
117M
   table_value, \
207
117M
   ( cipher_values_32bit[ 1 ] & 0xff ), \
208
117M
   ( ( cipher_values_32bit[ 0 ] >> 8 ) & 0xff ), \
209
117M
   ( ( cipher_values_32bit[ 3 ] >> 16 ) & 0xff ), \
210
117M
   ( ( cipher_values_32bit[ 2 ] >> 24 ) & 0xff ) ); \
211
117M
\
212
117M
  values_32bit[ 1 ] = round_keys[ 1 ] ^ table_value; \
213
117M
\
214
117M
  libcaes_calculate_reverse_table_value( \
215
117M
   table_value, \
216
117M
   ( cipher_values_32bit[ 2 ] & 0xff ), \
217
117M
   ( ( cipher_values_32bit[ 1 ] >> 8 ) & 0xff ), \
218
117M
   ( ( cipher_values_32bit[ 0 ] >> 16 ) & 0xff ), \
219
117M
   ( ( cipher_values_32bit[ 3 ] >> 24 ) & 0xff ) ); \
220
117M
\
221
117M
  values_32bit[ 2 ] = round_keys[ 2 ] ^ table_value; \
222
117M
\
223
117M
  libcaes_calculate_reverse_table_value( \
224
117M
   table_value, \
225
117M
   ( cipher_values_32bit[ 3 ] & 0xff ), \
226
117M
   ( ( cipher_values_32bit[ 2 ] >> 8 ) & 0xff ), \
227
117M
   ( ( cipher_values_32bit[ 1 ] >> 16 ) & 0xff ), \
228
117M
   ( ( cipher_values_32bit[ 0 ] >> 24 ) & 0xff ) ); \
229
117M
\
230
117M
  values_32bit[ 3 ] = round_keys[ 3 ] ^ table_value;
231
232
/* Forward S-box & tables
233
 */
234
static uint8_t libcaes_forward_substitution_box[ 256 ];
235
236
static uint32_t libcaes_forward_table0[ 256 ];
237
static uint32_t libcaes_forward_table1[ 256 ];
238
static uint32_t libcaes_forward_table2[ 256 ];
239
static uint32_t libcaes_forward_table3[ 256 ];
240
241
/* Reverse S-box & tables
242
 */
243
static uint8_t libcaes_reverse_substitution_box[ 256 ];
244
245
static uint32_t libcaes_reverse_table0[ 256 ];
246
static uint32_t libcaes_reverse_table1[ 256 ];
247
static uint32_t libcaes_reverse_table2[ 256 ];
248
static uint32_t libcaes_reverse_table3[ 256 ];
249
250
/* Round constants
251
 */
252
static uint32_t libcaes_round_constants[ 10 ];
253
254
static int libcaes_tables_initialized = 0;
255
256
#endif /* !defined( LIBCAES_HAVE_AES_SUPPORT ) */
257
258
/* Creates a context
259
 * Make sure the value context is referencing, is set to NULL
260
 * Returns 1 if successful or -1 on error
261
 */
262
int libcaes_context_initialize(
263
     libcaes_context_t **context,
264
     libcerror_error_t **error )
265
13.1k
{
266
13.1k
  libcaes_internal_context_t *internal_context = NULL;
267
13.1k
  static char *function                        = "libcaes_context_initialize";
268
269
#if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && ( defined( HAVE_EVP_CRYPTO_AES_CBC ) || defined( HAVE_EVP_CRYPTO_AES_ECB ) )
270
  char error_string[ 256 ];
271
272
  unsigned long error_code                     = 0;
273
#endif
274
275
13.1k
  if( context == NULL )
276
0
  {
277
0
    libcerror_error_set(
278
0
     error,
279
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
280
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
281
0
     "%s: invalid context.",
282
0
     function );
283
284
0
    return( -1 );
285
0
  }
286
13.1k
  if( *context != NULL )
287
0
  {
288
0
    libcerror_error_set(
289
0
     error,
290
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
291
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
292
0
     "%s: invalid context value already set.",
293
0
     function );
294
295
0
    return( -1 );
296
0
  }
297
13.1k
  internal_context = memory_allocate_structure(
298
13.1k
                      libcaes_internal_context_t );
299
300
13.1k
  if( internal_context == NULL )
301
0
  {
302
0
    libcerror_error_set(
303
0
     error,
304
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
305
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
306
0
     "%s: unable to create context.",
307
0
     function );
308
309
0
    goto on_error;
310
0
  }
311
13.1k
  if( memory_set(
312
13.1k
       internal_context,
313
13.1k
       0,
314
13.1k
       sizeof( libcaes_internal_context_t ) ) == NULL )
315
0
  {
316
0
    libcerror_error_set(
317
0
     error,
318
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
319
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
320
0
     "%s: unable to clear context.",
321
0
     function );
322
323
0
    goto on_error;
324
0
  }
325
#if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && ( defined( HAVE_AES_CBC_ENCRYPT ) || defined( HAVE_AES_ECB_ENCRYPT ) )
326
  /* No additional initialization necessary */
327
328
#elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && ( defined( HAVE_EVP_CRYPTO_AES_CBC ) || defined( HAVE_EVP_CRYPTO_AES_ECB ) )
329
#if defined( HAVE_EVP_CIPHER_CTX_INIT )
330
  EVP_CIPHER_CTX_init(
331
   &( internal_context->internal_evp_cipher_context ) );
332
333
  internal_context->evp_cipher_context = &( internal_context->internal_evp_cipher_context );
334
#else
335
  internal_context->evp_cipher_context = EVP_CIPHER_CTX_new();
336
337
  if( internal_context->evp_cipher_context == NULL )
338
  {
339
    error_code = ERR_get_error();
340
341
    ERR_error_string_n(
342
     error_code,
343
     error_string,
344
     256 );
345
346
    libcerror_error_set(
347
     error,
348
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
349
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
350
     "%s: unable to create EVP cipher context with error: %s.",
351
     function,
352
     error_string );
353
354
    goto on_error;
355
  }
356
#endif /* defined( HAVE_EVP_CIPHER_CTX_INIT ) */
357
#else
358
13.1k
  if( libcaes_tables_initialized == 0 )
359
8
  {
360
8
    if( libcaes_initialize_tables(
361
8
         error ) != 1 )
362
0
    {
363
0
      libcerror_error_set(
364
0
       error,
365
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
366
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
367
0
       "%s: unable to initialize tables.",
368
0
       function );
369
370
0
      goto on_error;
371
0
    }
372
8
    libcaes_tables_initialized = 1;
373
8
  }
374
13.1k
#endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && ( defined( HAVE_AES_CBC_ENCRYPT ) || defined( HAVE_AES_ECB_ENCRYPT ) ) */
375
376
13.1k
  *context = (libcaes_context_t *) internal_context;
377
378
13.1k
  return( 1 );
379
380
0
on_error:
381
0
  if( internal_context != NULL )
382
0
  {
383
0
    memory_free(
384
0
     internal_context );
385
0
  }
386
0
  return( -1 );
387
13.1k
}
388
389
/* Frees a context
390
 * Returns 1 if successful or -1 on error
391
 */
392
int libcaes_context_free(
393
     libcaes_context_t **context,
394
     libcerror_error_t **error )
395
13.4k
{
396
13.4k
  libcaes_internal_context_t *internal_context = NULL;
397
13.4k
  static char *function                        = "libcaes_context_free";
398
13.4k
  int result                                   = 1;
399
400
#if defined( HAVE_LIBCRYPTO ) && defined( HAVE_EVP_CIPHER_CTX_CLEANUP )
401
  char error_string[ 256 ];
402
403
  unsigned long error_code                     = 0;
404
#endif
405
406
13.4k
  if( context == NULL )
407
0
  {
408
0
    libcerror_error_set(
409
0
     error,
410
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
411
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
412
0
     "%s: invalid context.",
413
0
     function );
414
415
0
    return( -1 );
416
0
  }
417
13.4k
  if( *context != NULL )
418
13.4k
  {
419
13.4k
    internal_context = (libcaes_internal_context_t *) *context;
420
13.4k
    *context         = NULL;
421
422
#if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && ( defined( HAVE_AES_CBC_ENCRYPT ) || defined( HAVE_AES_ECB_ENCRYPT ) )
423
    /* No additional clean up necessary */
424
425
#elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && ( defined( HAVE_EVP_CRYPTO_AES_CBC ) || defined( HAVE_EVP_CRYPTO_AES_ECB ) )
426
#if defined( HAVE_EVP_CIPHER_CTX_CLEANUP )
427
    if( EVP_CIPHER_CTX_cleanup(
428
         &( internal_context->internal_evp_cipher_context ) ) != 1 )
429
    {
430
      error_code = ERR_get_error();
431
432
      ERR_error_string_n(
433
       error_code,
434
       error_string,
435
       256 );
436
437
      libcerror_error_set(
438
       error,
439
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
440
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
441
       "%s: unable to clean up EVP cipher context with error: %s.",
442
       function,
443
       error_string );
444
445
      result = -1;
446
    }
447
    /* Make sure the error state is removed otherwise OpenSSL will leak memory
448
     */
449
    ERR_clear_error();
450
#else
451
    EVP_CIPHER_CTX_free(
452
     internal_context->evp_cipher_context );
453
454
#endif /* defined( HAVE_EVP_CIPHER_CTX_CLEANUP ) */
455
456
    internal_context->evp_cipher_context = NULL;
457
#else
458
    /* No additional clean up necessary */
459
460
13.4k
#endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && ( defined( HAVE_AES_CBC_ENCRYPT ) || defined( HAVE_AES_ECB_ENCRYPT ) ) */
461
462
13.4k
    memory_free(
463
13.4k
     internal_context );
464
13.4k
  }
465
13.4k
  return( result );
466
13.4k
}
467
468
/* Sets the key
469
 * Returns 1 if successful or -1 on error
470
 */
471
int libcaes_context_set_key(
472
     libcaes_context_t *context,
473
     int mode,
474
     const uint8_t *key,
475
     size_t key_bit_size,
476
     libcerror_error_t **error )
477
9.29k
{
478
9.29k
  libcaes_internal_context_t *internal_context = NULL;
479
9.29k
  static char *function                        = "libcaes_context_set_key";
480
481
9.29k
  if( context == NULL )
482
0
  {
483
0
    libcerror_error_set(
484
0
     error,
485
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
486
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
487
0
     "%s: invalid context.",
488
0
     function );
489
490
0
    return( -1 );
491
0
  }
492
9.29k
  internal_context = (libcaes_internal_context_t *) context;
493
494
9.29k
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
495
5.40k
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
496
0
  {
497
0
    libcerror_error_set(
498
0
     error,
499
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
500
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
501
0
     "%s: unsupported mode.",
502
0
     function );
503
504
0
    return( -1 );
505
0
  }
506
9.29k
  if( ( key_bit_size != 128 )
507
1.94k
   && ( key_bit_size != 192 )
508
1.94k
   && ( key_bit_size != 256 ) )
509
1
  {
510
1
    libcerror_error_set(
511
1
     error,
512
1
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
513
1
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
514
1
     "%s: unsupported key bit size.",
515
1
     function );
516
517
1
    return( -1 );
518
1
  }
519
#if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && ( defined( HAVE_AES_CBC_ENCRYPT ) || defined( HAVE_AES_ECB_ENCRYPT ) )
520
  if( mode == LIBCAES_CRYPT_MODE_ENCRYPT )
521
  {
522
    if( AES_set_encrypt_key(
523
         (unsigned char *) key,
524
         (int) key_bit_size,
525
         &( internal_context->key ) ) != 0 )
526
    {
527
      libcerror_error_set(
528
       error,
529
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
530
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
531
       "%s: unable to set encryption key.",
532
       function );
533
534
      return( -1 );
535
    }
536
  }
537
  else
538
  {
539
    if( AES_set_decrypt_key(
540
         (unsigned char *) key,
541
         (int) key_bit_size,
542
         &( internal_context->key ) ) != 0 )
543
    {
544
      libcerror_error_set(
545
       error,
546
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
547
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
548
       "%s: unable to set decryption key.",
549
       function );
550
551
      return( -1 );
552
    }
553
  }
554
555
#elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && ( defined( HAVE_EVP_CRYPTO_AES_CBC ) || defined( HAVE_EVP_CRYPTO_AES_ECB ) )
556
  if( key == NULL )
557
  {
558
    libcerror_error_set(
559
     error,
560
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
561
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
562
     "%s: invalid key.",
563
     function );
564
565
    return( -1 );
566
  }
567
  if( memory_copy(
568
       internal_context->key,
569
       key,
570
       key_bit_size / 8 ) == NULL )
571
  {
572
    libcerror_error_set(
573
     error,
574
     LIBCERROR_ERROR_DOMAIN_MEMORY,
575
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
576
     "%s: unable to copy key.",
577
     function );
578
579
    return( -1 );
580
  }
581
  internal_context->key_bit_size = key_bit_size;
582
583
#else
584
9.29k
  if( mode == LIBCAES_CRYPT_MODE_ENCRYPT )
585
5.40k
  {
586
5.40k
    if( libcaes_internal_context_set_encryption_key(
587
5.40k
         internal_context,
588
5.40k
         key,
589
5.40k
         key_bit_size,
590
5.40k
         error ) != 1 )
591
0
    {
592
0
      libcerror_error_set(
593
0
       error,
594
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
595
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
596
0
       "%s: unable to set encryption key.",
597
0
       function );
598
599
0
      return( -1 );
600
0
    }
601
5.40k
  }
602
3.88k
  else
603
3.88k
  {
604
3.88k
    if( libcaes_internal_context_set_decryption_key(
605
3.88k
         internal_context,
606
3.88k
         key,
607
3.88k
         key_bit_size,
608
3.88k
         error ) != 1 )
609
0
    {
610
0
      libcerror_error_set(
611
0
       error,
612
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
613
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
614
0
       "%s: unable to set decryption key.",
615
0
       function );
616
617
0
      return( -1 );
618
0
    }
619
3.88k
  }
620
9.29k
#endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && ( defined( HAVE_AES_CBC_ENCRYPT ) || defined( HAVE_AES_ECB_ENCRYPT ) ) */
621
622
9.29k
  return( 1 );
623
9.29k
}
624
625
#if !defined( LIBCAES_HAVE_AES_SUPPORT )
626
627
/* Initializes the AES encryption and decryption tables
628
 * Returns 1 if successful or -1 on error
629
 */
630
int libcaes_initialize_tables(
631
     libcerror_error_t **error )
632
12
{
633
12
  uint8_t logs_table[ 256 ];
634
12
  uint8_t powers_table[ 256 ];
635
636
12
  static char *function        = "libcaes_initialize_tables";
637
12
  uint16_t byte_index          = 0;
638
12
  uint16_t table_index         = 0;
639
12
  uint8_t byte_value           = 0;
640
12
  uint8_t log_value            = 0;
641
12
  uint8_t round_constant_index = 0;
642
12
  uint8_t substitution_value   = 0;
643
644
12
  if( memory_set(
645
12
       logs_table,
646
12
       0,
647
12
       sizeof( uint8_t ) * 256 ) == NULL )
648
0
  {
649
0
    libcerror_error_set(
650
0
     error,
651
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
652
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
653
0
     "%s: unable to clear logs table.",
654
0
     function );
655
656
0
    return( -1 );
657
0
  }
658
  /* Fill the powers and logs tables over GF( 2^8 )
659
   */
660
12
  byte_value = 1;
661
662
12
  for( byte_index = 0;
663
3.08k
       byte_index < 256;
664
3.07k
       byte_index++ )
665
3.07k
  {
666
3.07k
    powers_table[ byte_index ] = byte_value;
667
3.07k
    logs_table[ byte_value ]   = (uint8_t) byte_index;
668
669
3.07k
    if( ( byte_value & 0x80 ) == 0 )
670
1.53k
    {
671
1.53k
      byte_value ^= ( byte_value << 1 ) & 0xff;
672
1.53k
    }
673
1.53k
    else
674
1.53k
    {
675
1.53k
      byte_value ^= ( ( byte_value << 1 ) & 0xff ) ^ 0x1b;
676
1.53k
    }
677
3.07k
  }
678
  /* Fill the round constants
679
   */
680
12
  byte_value = 1;
681
682
12
  for( round_constant_index = 0;
683
132
       round_constant_index < 10;
684
120
       round_constant_index++ )
685
120
  {
686
120
    libcaes_round_constants[ round_constant_index ] = (uint32_t) byte_value;
687
688
120
    if( ( byte_value & 0x80 ) == 0 )
689
108
    {
690
108
      byte_value = ( byte_value << 1 ) & 0xff;
691
108
    }
692
12
    else
693
12
    {
694
12
      byte_value = ( ( byte_value << 1 ) & 0xff ) ^ 0x1b;
695
12
    }
696
120
  }
697
698
  /* Fill the forward and reverse S-boxes
699
   */
700
12
  libcaes_forward_substitution_box[ 0x00 ] = 0x63;
701
12
  libcaes_reverse_substitution_box[ 0x63 ] = 0x00;
702
703
12
  for( byte_index = 1;
704
3.07k
       byte_index < 256;
705
3.06k
       byte_index++ )
706
3.06k
  {
707
3.06k
    table_index = 255 - logs_table[ byte_index ];
708
3.06k
    byte_value  = powers_table[ table_index ];
709
710
3.06k
    substitution_value = ( ( byte_value << 1 ) & 0xff )
711
3.06k
                       | ( byte_value >> 7 );
712
713
3.06k
    byte_value ^= substitution_value;
714
715
3.06k
    substitution_value = ( ( substitution_value << 1 ) & 0xff )
716
3.06k
                       | ( substitution_value >> 7 );
717
718
3.06k
    byte_value ^= substitution_value;
719
720
3.06k
    substitution_value = ( ( substitution_value << 1 ) & 0xff )
721
3.06k
                       | ( substitution_value >> 7 );
722
723
3.06k
    byte_value ^= substitution_value;
724
725
3.06k
    substitution_value = ( ( substitution_value << 1 ) & 0xff )
726
3.06k
                       | ( substitution_value >> 7 );
727
728
3.06k
    substitution_value ^= byte_value ^ 0x63;
729
730
3.06k
    libcaes_forward_substitution_box[ byte_index ]         = substitution_value;
731
3.06k
    libcaes_reverse_substitution_box[ substitution_value ] = (uint8_t) byte_index;
732
3.06k
  }
733
  /* Fill the forward and reverse tables
734
   */
735
12
  for( byte_index = 0;
736
3.08k
       byte_index < 256;
737
3.07k
       byte_index++ )
738
3.07k
  {
739
3.07k
    byte_value = libcaes_forward_substitution_box[ byte_index ];
740
741
3.07k
    if( ( byte_value & 0x80 ) == 0 )
742
1.53k
    {
743
1.53k
      substitution_value = ( byte_value << 1 ) & 0xff;
744
1.53k
    }
745
1.53k
    else
746
1.53k
    {
747
1.53k
      substitution_value = ( ( byte_value << 1 ) & 0xff ) ^ 0x1b;
748
1.53k
    }
749
3.07k
    libcaes_forward_table0[ byte_index ]   = byte_value ^ substitution_value;
750
3.07k
    libcaes_forward_table0[ byte_index ] <<= 8;
751
3.07k
    libcaes_forward_table0[ byte_index ]  |= byte_value;
752
3.07k
    libcaes_forward_table0[ byte_index ] <<= 8;
753
3.07k
    libcaes_forward_table0[ byte_index ]  |= byte_value;
754
3.07k
    libcaes_forward_table0[ byte_index ] <<= 8;
755
3.07k
    libcaes_forward_table0[ byte_index ]  |= substitution_value;
756
757
3.07k
    libcaes_forward_table1[ byte_index ] = byte_stream_bit_rotate_left(
758
3.07k
                                            libcaes_forward_table0[ byte_index ],
759
3.07k
                                            8 );
760
761
3.07k
    libcaes_forward_table2[ byte_index ] = byte_stream_bit_rotate_left(
762
3.07k
                                            libcaes_forward_table1[ byte_index ],
763
3.07k
                                            8 );
764
765
3.07k
    libcaes_forward_table3[ byte_index ] = byte_stream_bit_rotate_left(
766
3.07k
                                            libcaes_forward_table2[ byte_index ],
767
3.07k
                                            8 );
768
769
3.07k
    substitution_value = libcaes_reverse_substitution_box[ byte_index ];
770
771
3.07k
    libcaes_reverse_table0[ byte_index ] = 0;
772
773
3.07k
    if( substitution_value != 0 )
774
3.06k
    {
775
3.06k
      log_value = logs_table[ substitution_value ];
776
777
3.06k
      table_index = (logs_table[ 0x0b ] + log_value) % 255;
778
779
3.06k
      libcaes_reverse_table0[ byte_index ] ^= powers_table[ table_index ];
780
3.06k
      libcaes_reverse_table0[ byte_index ] <<= 8;
781
782
3.06k
      table_index = ( logs_table[ 0x0d ] + log_value ) % 255;
783
784
3.06k
      libcaes_reverse_table0[ byte_index ] ^= powers_table[ table_index ];
785
3.06k
      libcaes_reverse_table0[ byte_index ] <<= 8;
786
787
3.06k
      table_index = ( logs_table[ 0x09 ] + log_value ) % 255;
788
789
3.06k
      libcaes_reverse_table0[ byte_index ] ^= powers_table[ table_index ];
790
3.06k
      libcaes_reverse_table0[ byte_index ] <<= 8;
791
792
3.06k
      table_index = ( logs_table[ 0x0e ] + log_value ) % 255;
793
794
3.06k
      libcaes_reverse_table0[ byte_index ] ^= powers_table[ table_index ];
795
3.06k
    }
796
3.07k
    libcaes_reverse_table1[ byte_index ] = byte_stream_bit_rotate_left(
797
3.07k
                                            libcaes_reverse_table0[ byte_index ],
798
3.07k
                                            8 );
799
800
3.07k
    libcaes_reverse_table2[ byte_index ] = byte_stream_bit_rotate_left(
801
3.07k
                                            libcaes_reverse_table1[ byte_index ],
802
3.07k
                                            8 );
803
804
3.07k
    libcaes_reverse_table3[ byte_index ] = byte_stream_bit_rotate_left(
805
3.07k
                                            libcaes_reverse_table2[ byte_index ],
806
3.07k
                                            8 );
807
3.07k
  }
808
12
  return( 1 );
809
12
}
810
811
/* Sets the AES decryption key
812
 * Returns 1 if successful or -1 on error
813
 */
814
int libcaes_internal_context_set_decryption_key(
815
     libcaes_internal_context_t *internal_context,
816
     const uint8_t *key,
817
     size_t key_bit_size,
818
     libcerror_error_t **error )
819
3.88k
{
820
3.88k
  libcaes_internal_context_t *encryption_context = NULL;
821
3.88k
  static char *function                          = "libcaes_internal_context_set_decryption_key";
822
3.88k
  uint32_t *encryption_round_keys                = NULL;
823
3.88k
  uint32_t *round_keys                           = NULL;
824
3.88k
  size_t round_keys_byte_offset                  = 0;
825
3.88k
  uint8_t byte_value0                            = 0;
826
3.88k
  uint8_t byte_value1                            = 0;
827
3.88k
  uint8_t byte_value2                            = 0;
828
3.88k
  uint8_t byte_value3                            = 0;
829
3.88k
  int round_key_iterator                         = 0;
830
831
3.88k
  if( internal_context == NULL )
832
0
  {
833
0
    libcerror_error_set(
834
0
     error,
835
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
836
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
837
0
     "%s: invalid context.",
838
0
     function );
839
840
0
    return( -1 );
841
0
  }
842
3.88k
  if( key == NULL )
843
0
  {
844
0
    libcerror_error_set(
845
0
     error,
846
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
847
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
848
0
     "%s: invalid key.",
849
0
     function );
850
851
0
    return( -1 );
852
0
  }
853
3.88k
  if( ( key_bit_size != 128 )
854
234
   && ( key_bit_size != 192 )
855
234
   && ( key_bit_size != 256 ) )
856
0
  {
857
0
    libcerror_error_set(
858
0
     error,
859
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
860
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
861
0
     "%s: unsupported key bit size.",
862
0
     function );
863
864
0
    return( -1 );
865
0
  }
866
3.88k
  if( key_bit_size == 128 )
867
3.65k
  {
868
3.65k
    internal_context->number_of_round_keys = 10;
869
3.65k
  }
870
234
  else if( key_bit_size == 192 )
871
0
  {
872
0
    internal_context->number_of_round_keys = 12;
873
0
  }
874
234
  else if( key_bit_size == 256 )
875
234
  {
876
234
    internal_context->number_of_round_keys = 14;
877
234
  }
878
  /* Align the buffer to next 16-byte blocks
879
   */
880
3.88k
  internal_context->round_keys = (uint32_t *) ( 16 + ( (intptr_t) internal_context->round_keys_data & ~( 15 ) ) );
881
882
3.88k
  round_keys = internal_context->round_keys;
883
884
3.88k
  if( libcaes_context_initialize(
885
3.88k
       (libcaes_context_t **) &encryption_context,
886
3.88k
       error ) != 1 )
887
0
  {
888
0
    libcerror_error_set(
889
0
     error,
890
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
891
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
892
0
     "%s: unable to create encryption context.",
893
0
     function );
894
895
0
    goto on_error;
896
0
  }
897
3.88k
  if( libcaes_internal_context_set_encryption_key(
898
3.88k
       encryption_context,
899
3.88k
       key,
900
3.88k
       key_bit_size,
901
3.88k
       error ) != 1 )
902
0
  {
903
0
    libcerror_error_set(
904
0
     error,
905
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
906
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
907
0
     "%s: unable to set encryption key.",
908
0
     function );
909
910
0
    goto on_error;
911
0
  }
912
  /* Point to the end of the round keys
913
   */
914
3.88k
  round_keys_byte_offset = encryption_context->number_of_round_keys * sizeof( uint32_t );
915
916
3.88k
  encryption_round_keys = &( encryption_context->round_keys[ round_keys_byte_offset ] );
917
918
3.88k
  round_keys[ 0 ] = encryption_round_keys[ 0 ];
919
3.88k
  round_keys[ 1 ] = encryption_round_keys[ 1 ];
920
3.88k
  round_keys[ 2 ] = encryption_round_keys[ 2 ];
921
3.88k
  round_keys[ 3 ] = encryption_round_keys[ 3 ];
922
923
3.88k
  round_keys            += 4;
924
3.88k
  encryption_round_keys -= 4;
925
926
3.88k
  for( round_key_iterator = internal_context->number_of_round_keys;
927
39.8k
       round_key_iterator > 1;
928
35.9k
       round_key_iterator-- )
929
35.9k
  {
930
35.9k
    byte_value0 = ( encryption_round_keys[ 0 ] & 0xff );
931
35.9k
    byte_value1 = ( ( encryption_round_keys[ 0 ] >> 8 ) & 0xff );
932
35.9k
    byte_value2 = ( ( encryption_round_keys[ 0 ] >> 16 ) & 0xff );
933
35.9k
    byte_value3 = ( ( encryption_round_keys[ 0 ] >> 24 ) & 0xff );
934
935
35.9k
    byte_value0 = libcaes_forward_substitution_box[ byte_value0 ];
936
35.9k
    byte_value1 = libcaes_forward_substitution_box[ byte_value1 ];
937
35.9k
    byte_value2 = libcaes_forward_substitution_box[ byte_value2 ];
938
35.9k
    byte_value3 = libcaes_forward_substitution_box[ byte_value3 ];
939
940
35.9k
    round_keys[ 0 ] = libcaes_reverse_table0[ byte_value0 ]
941
35.9k
              ^ libcaes_reverse_table1[ byte_value1 ]
942
35.9k
              ^ libcaes_reverse_table2[ byte_value2 ]
943
35.9k
              ^ libcaes_reverse_table3[ byte_value3 ];
944
945
35.9k
    byte_value0 = ( encryption_round_keys[ 1 ] & 0xff );
946
35.9k
    byte_value1 = ( ( encryption_round_keys[ 1 ] >> 8 ) & 0xff );
947
35.9k
    byte_value2 = ( ( encryption_round_keys[ 1 ] >> 16 ) & 0xff );
948
35.9k
    byte_value3 = ( ( encryption_round_keys[ 1 ] >> 24 ) & 0xff );
949
950
35.9k
    byte_value0 = libcaes_forward_substitution_box[ byte_value0 ];
951
35.9k
    byte_value1 = libcaes_forward_substitution_box[ byte_value1 ];
952
35.9k
    byte_value2 = libcaes_forward_substitution_box[ byte_value2 ];
953
35.9k
    byte_value3 = libcaes_forward_substitution_box[ byte_value3 ];
954
955
35.9k
    round_keys[ 1 ] = libcaes_reverse_table0[ byte_value0 ]
956
35.9k
              ^ libcaes_reverse_table1[ byte_value1 ]
957
35.9k
              ^ libcaes_reverse_table2[ byte_value2 ]
958
35.9k
              ^ libcaes_reverse_table3[ byte_value3 ];
959
960
35.9k
    byte_value0 = ( encryption_round_keys[ 2 ] & 0xff );
961
35.9k
    byte_value1 = ( ( encryption_round_keys[ 2 ] >> 8 ) & 0xff );
962
35.9k
    byte_value2 = ( ( encryption_round_keys[ 2 ] >> 16 ) & 0xff );
963
35.9k
    byte_value3 = ( ( encryption_round_keys[ 2 ] >> 24 ) & 0xff );
964
965
35.9k
    byte_value0 = libcaes_forward_substitution_box[ byte_value0 ];
966
35.9k
    byte_value1 = libcaes_forward_substitution_box[ byte_value1 ];
967
35.9k
    byte_value2 = libcaes_forward_substitution_box[ byte_value2 ];
968
35.9k
    byte_value3 = libcaes_forward_substitution_box[ byte_value3 ];
969
970
35.9k
    round_keys[ 2 ] = libcaes_reverse_table0[ byte_value0 ]
971
35.9k
              ^ libcaes_reverse_table1[ byte_value1 ]
972
35.9k
              ^ libcaes_reverse_table2[ byte_value2 ]
973
35.9k
              ^ libcaes_reverse_table3[ byte_value3 ];
974
975
35.9k
    byte_value0 = ( encryption_round_keys[ 3 ] & 0xff );
976
35.9k
    byte_value1 = ( ( encryption_round_keys[ 3 ] >> 8 ) & 0xff );
977
35.9k
    byte_value2 = ( ( encryption_round_keys[ 3 ] >> 16 ) & 0xff );
978
35.9k
    byte_value3 = ( ( encryption_round_keys[ 3 ] >> 24 ) & 0xff );
979
980
35.9k
    byte_value0 = libcaes_forward_substitution_box[ byte_value0 ];
981
35.9k
    byte_value1 = libcaes_forward_substitution_box[ byte_value1 ];
982
35.9k
    byte_value2 = libcaes_forward_substitution_box[ byte_value2 ];
983
35.9k
    byte_value3 = libcaes_forward_substitution_box[ byte_value3 ];
984
985
35.9k
    round_keys[ 3 ] = libcaes_reverse_table0[ byte_value0 ]
986
35.9k
              ^ libcaes_reverse_table1[ byte_value1 ]
987
35.9k
              ^ libcaes_reverse_table2[ byte_value2 ]
988
35.9k
              ^ libcaes_reverse_table3[ byte_value3 ];
989
990
35.9k
    round_keys            += 4;
991
35.9k
    encryption_round_keys -= 4;
992
35.9k
  }
993
3.88k
  round_keys[ 0 ] = encryption_round_keys[ 0 ];
994
3.88k
  round_keys[ 1 ] = encryption_round_keys[ 1 ];
995
3.88k
  round_keys[ 2 ] = encryption_round_keys[ 2 ];
996
3.88k
  round_keys[ 3 ] = encryption_round_keys[ 3 ];
997
998
3.88k
  if( libcaes_context_free(
999
3.88k
       (libcaes_context_t **) &encryption_context,
1000
3.88k
       error ) != 1 )
1001
0
  {
1002
0
    libcerror_error_set(
1003
0
     error,
1004
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1005
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1006
0
     "%s: unable to free encryption context.",
1007
0
     function );
1008
1009
0
    goto on_error;
1010
0
  }
1011
3.88k
  return( 1 );
1012
1013
0
on_error:
1014
0
  if( encryption_context != NULL )
1015
0
  {
1016
0
    libcaes_context_free(
1017
0
     (libcaes_context_t **) &encryption_context,
1018
0
     NULL );
1019
0
  }
1020
0
  return( -1 );
1021
3.88k
}
1022
1023
/* Sets the AES encryption key
1024
 * Returns 1 if successful or -1 on error
1025
 */
1026
int libcaes_internal_context_set_encryption_key(
1027
     libcaes_internal_context_t *internal_context,
1028
     const uint8_t *key,
1029
     size_t key_bit_size,
1030
     libcerror_error_t **error )
1031
9.60k
{
1032
9.60k
  static char *function    = "libcaes_internal_context_set_encryption_key";
1033
9.60k
  uint32_t *round_keys     = NULL;
1034
9.60k
  size_t key_index         = 0;
1035
9.60k
  int round_constant_index = 0;
1036
1037
9.60k
  if( internal_context == NULL )
1038
0
  {
1039
0
    libcerror_error_set(
1040
0
     error,
1041
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1042
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1043
0
     "%s: invalid context.",
1044
0
     function );
1045
1046
0
    return( -1 );
1047
0
  }
1048
9.60k
  if( key == NULL )
1049
0
  {
1050
0
    libcerror_error_set(
1051
0
     error,
1052
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1053
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1054
0
     "%s: invalid key.",
1055
0
     function );
1056
1057
0
    return( -1 );
1058
0
  }
1059
9.60k
  if( ( key_bit_size != 128 )
1060
1.94k
   && ( key_bit_size != 192 )
1061
1.94k
   && ( key_bit_size != 256 ) )
1062
0
  {
1063
0
    libcerror_error_set(
1064
0
     error,
1065
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1066
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1067
0
     "%s: unsupported key bit size.",
1068
0
     function );
1069
1070
0
    return( -1 );
1071
0
  }
1072
  /* Align the buffer to next 16-byte blocks
1073
   */
1074
9.60k
  internal_context->round_keys = (uint32_t *) ( 16 + ( (intptr_t) internal_context->round_keys_data & ~( 15 ) ) );
1075
1076
9.60k
  round_keys = internal_context->round_keys;
1077
1078
9.60k
  for( key_index = 0;
1079
55.8k
       key_index < key_bit_size / 8;
1080
46.1k
       key_index += 4 )
1081
46.1k
  {
1082
46.1k
    byte_stream_copy_to_uint32_little_endian(
1083
46.1k
           &( key[ key_index ] ),
1084
46.1k
           round_keys[ round_constant_index ] );
1085
1086
46.1k
    round_constant_index++;
1087
46.1k
  }
1088
9.60k
  if( key_bit_size == 128 )
1089
7.66k
  {
1090
7.66k
    internal_context->number_of_round_keys = 10;
1091
1092
7.66k
    for( round_constant_index = 0;
1093
84.2k
         round_constant_index < 10;
1094
76.6k
         round_constant_index++ )
1095
76.6k
    {
1096
76.6k
      round_keys[ 4 ]  = libcaes_round_constants[ round_constant_index ];
1097
76.6k
      round_keys[ 4 ] ^= round_keys[ 0 ];
1098
76.6k
      round_keys[ 4 ] ^= (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 3 ] >> 8 ) & 0xff ];
1099
76.6k
      round_keys[ 4 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 3 ] >> 16 ) & 0xff ] ) << 8;
1100
76.6k
      round_keys[ 4 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 3 ] >> 24 ) & 0xff ] ) << 16;
1101
76.6k
      round_keys[ 4 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 3 ] ) & 0xff ] ) << 24;
1102
1103
76.6k
      round_keys[ 5 ] = round_keys[ 1 ] ^ round_keys[ 4 ];
1104
76.6k
      round_keys[ 6 ] = round_keys[ 2 ] ^ round_keys[ 5 ];
1105
76.6k
      round_keys[ 7 ] = round_keys[ 3 ] ^ round_keys[ 6 ];
1106
1107
76.6k
      round_keys += 4;
1108
76.6k
    }
1109
7.66k
  }
1110
1.94k
  else if( key_bit_size == 192 )
1111
0
  {
1112
0
    internal_context->number_of_round_keys = 12;
1113
1114
0
    for( round_constant_index = 0;
1115
0
         round_constant_index < 8;
1116
0
         round_constant_index++ )
1117
0
    {
1118
0
      round_keys[ 6 ]  = libcaes_round_constants[ round_constant_index ];
1119
0
      round_keys[ 6 ] ^= round_keys[ 0 ];
1120
0
      round_keys[ 6 ] ^= (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 5 ] >> 8 ) & 0xff ];
1121
0
      round_keys[ 6 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 5 ] >> 16 ) & 0xff ] ) << 8;
1122
0
      round_keys[ 6 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 5 ] >> 24 ) & 0xff ] ) << 16;
1123
0
      round_keys[ 6 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 5 ] ) & 0xff ] ) << 24;
1124
1125
0
      round_keys[ 7 ]  = round_keys[ 1 ] ^ round_keys[ 6 ];
1126
0
      round_keys[ 8 ]  = round_keys[ 2 ] ^ round_keys[ 7 ];
1127
0
      round_keys[ 9 ]  = round_keys[ 3 ] ^ round_keys[ 8 ];
1128
0
      round_keys[ 10 ] = round_keys[ 4 ] ^ round_keys[ 9 ];
1129
0
      round_keys[ 11 ] = round_keys[ 5 ] ^ round_keys[ 10 ];
1130
1131
0
      round_keys += 6;
1132
0
    }
1133
0
  }
1134
1.94k
  else if( key_bit_size == 256 )
1135
1.94k
  {
1136
1.94k
    internal_context->number_of_round_keys = 14;
1137
1138
1.94k
    for( round_constant_index = 0;
1139
15.5k
         round_constant_index < 7;
1140
13.6k
         round_constant_index++ )
1141
13.6k
    {
1142
13.6k
      round_keys[ 8 ]  = libcaes_round_constants[ round_constant_index ];
1143
13.6k
      round_keys[ 8 ] ^= round_keys[ 0 ];
1144
13.6k
      round_keys[ 8 ] ^= (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 7 ] >> 8 ) & 0xff ];
1145
13.6k
      round_keys[ 8 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 7 ] >> 16 ) & 0xff ] ) << 8;
1146
13.6k
      round_keys[ 8 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 7 ] >> 24 ) & 0xff ] ) << 16;
1147
13.6k
      round_keys[ 8 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 7 ] ) & 0xff ] ) << 24;
1148
1149
13.6k
      round_keys[ 9 ]  = round_keys[ 1 ] ^ round_keys[ 8 ];
1150
13.6k
      round_keys[ 10 ] = round_keys[ 2 ] ^ round_keys[ 9 ];
1151
13.6k
      round_keys[ 11 ] = round_keys[ 3 ] ^ round_keys[ 10 ];
1152
1153
13.6k
      round_keys[ 12 ]  = round_keys[ 4 ];
1154
13.6k
      round_keys[ 12 ] ^= (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 11 ] ) & 0xff ];
1155
13.6k
      round_keys[ 12 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 11 ] >> 8 ) & 0xff ] ) << 8;
1156
13.6k
      round_keys[ 12 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 11 ] >> 16 ) & 0xff ] ) << 16;
1157
13.6k
      round_keys[ 12 ] ^= ( (uint32_t) libcaes_forward_substitution_box[ ( round_keys[ 11 ] >> 24 ) & 0xff ] ) << 24;
1158
1159
13.6k
      round_keys[ 13 ] = round_keys[ 5 ] ^ round_keys[ 12 ];
1160
13.6k
      round_keys[ 14 ] = round_keys[ 6 ] ^ round_keys[ 13 ];
1161
13.6k
      round_keys[ 15 ] = round_keys[ 7 ] ^ round_keys[ 14 ];
1162
1163
13.6k
      round_keys += 8;
1164
13.6k
    }
1165
1.94k
  }
1166
9.60k
  return( 1 );
1167
9.60k
}
1168
1169
#endif /* !defined( LIBCAES_HAVE_AES_SUPPORT ) */
1170
1171
#if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && defined( HAVE_AES_CBC_ENCRYPT )
1172
1173
/* De- or encrypts a block of data using AES-CBC (Cipher Block Chaining) using OpenSSL
1174
 * The size must be a multitude of the AES block size (16 byte)
1175
 * Returns 1 if successful or -1 on error
1176
 */
1177
int libcaes_crypt_cbc(
1178
     libcaes_context_t *context,
1179
     int mode,
1180
     const uint8_t *initialization_vector,
1181
     size_t initialization_vector_size,
1182
     const uint8_t *input_data,
1183
     size_t input_data_size,
1184
     uint8_t *output_data,
1185
     size_t output_data_size,
1186
     libcerror_error_t **error )
1187
{
1188
  uint8_t safe_initialization_vector[ 16 ];
1189
1190
  libcaes_internal_context_t *internal_context = NULL;
1191
  static char *function                        = "libcaes_crypt_cbc";
1192
  int safe_mode                                = 0;
1193
1194
  if( context == NULL )
1195
  {
1196
    libcerror_error_set(
1197
     error,
1198
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1199
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1200
     "%s: invalid context.",
1201
     function );
1202
1203
    return( -1 );
1204
  }
1205
  internal_context = (libcaes_internal_context_t *) context;
1206
1207
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
1208
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
1209
  {
1210
    libcerror_error_set(
1211
     error,
1212
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1213
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1214
     "%s: unsupported mode.",
1215
     function );
1216
1217
    return( -1 );
1218
  }
1219
  if( initialization_vector == NULL )
1220
  {
1221
    libcerror_error_set(
1222
     error,
1223
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1224
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1225
     "%s: invalid initialization vector.",
1226
     function );
1227
1228
    return( -1 );
1229
  }
1230
  if( initialization_vector_size != 16 )
1231
  {
1232
    libcerror_error_set(
1233
     error,
1234
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1235
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1236
     "%s: invalid initialization vector size value out of bounds.",
1237
     function );
1238
1239
    return( -1 );
1240
  }
1241
  if( input_data == NULL )
1242
  {
1243
    libcerror_error_set(
1244
     error,
1245
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1246
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1247
     "%s: invalid input data.",
1248
     function );
1249
1250
    return( -1 );
1251
  }
1252
  /* Check if the input data size is a multitude of 16-byte
1253
   */
1254
  if( ( ( input_data_size & (size_t) 0x0f ) != 0 )
1255
   || ( input_data_size > (size_t) SSIZE_MAX ) )
1256
  {
1257
    libcerror_error_set(
1258
     error,
1259
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1260
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1261
     "%s: invalid input data size value out of bounds.",
1262
     function );
1263
1264
    return( -1 );
1265
  }
1266
  if( output_data == NULL )
1267
  {
1268
    libcerror_error_set(
1269
     error,
1270
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1271
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1272
     "%s: invalid output data.",
1273
     function );
1274
1275
    return( -1 );
1276
  }
1277
  if( ( output_data_size < input_data_size )
1278
   || ( output_data_size > (size_t) SSIZE_MAX ) )
1279
  {
1280
    libcerror_error_set(
1281
     error,
1282
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1283
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1284
     "%s: invalid output data size value out of bounds.",
1285
     function );
1286
1287
    return( -1 );
1288
  }
1289
  if( mode == LIBCAES_CRYPT_MODE_ENCRYPT )
1290
  {
1291
    safe_mode = AES_ENCRYPT;
1292
  }
1293
  else
1294
  {
1295
    safe_mode = AES_DECRYPT;
1296
  }
1297
  /* AES_cbc_encrypt overwrites the data in the initialization vector
1298
   */
1299
  if( memory_copy(
1300
       safe_initialization_vector,
1301
       initialization_vector,
1302
       16 ) == NULL )
1303
  {
1304
    libcerror_error_set(
1305
     error,
1306
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1307
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1308
     "%s: unable to copy initialization vector.",
1309
     function );
1310
1311
    return( -1 );
1312
  }
1313
  AES_cbc_encrypt(
1314
   (unsigned char *) input_data,
1315
   (unsigned char *) output_data,
1316
   input_data_size,
1317
   &( internal_context->key ),
1318
   (unsigned char *) safe_initialization_vector,
1319
   safe_mode );
1320
1321
  if( memory_set(
1322
       safe_initialization_vector,
1323
       0,
1324
       16 ) == NULL )
1325
  {
1326
    libcerror_error_set(
1327
     error,
1328
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1329
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
1330
     "%s: unable to clear initialization vector.",
1331
     function );
1332
1333
    return( -1 );
1334
  }
1335
  return( 1 );
1336
}
1337
1338
#elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_CBC )
1339
1340
/* De- or encrypts a block of data using AES-CBC (Cipher Block Chaining) using OpenSSL EVP
1341
 * The size must be a multitude of the AES block size (16 byte)
1342
 * Returns 1 if successful or -1 on error
1343
 */
1344
int libcaes_crypt_cbc(
1345
     libcaes_context_t *context,
1346
     int mode,
1347
     const uint8_t *initialization_vector,
1348
     size_t initialization_vector_size,
1349
     const uint8_t *input_data,
1350
     size_t input_data_size,
1351
     uint8_t *output_data,
1352
     size_t output_data_size,
1353
     libcerror_error_t **error )
1354
{
1355
  char error_string[ 256 ];
1356
1357
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
1358
  OSSL_PARAM parameters[2];
1359
1360
  EVP_CIPHER *cipher                           = NULL;
1361
  const char *cipher_string                    = NULL;
1362
  unsigned int padding                         = 0;
1363
#else
1364
  const EVP_CIPHER *cipher                     = NULL;
1365
#endif
1366
1367
  libcaes_internal_context_t *internal_context = NULL;
1368
  static char *function                        = "libcaes_crypt_cbc";
1369
  unsigned long error_code                     = 0;
1370
  int safe_output_data_size                    = 0;
1371
  int total_data_size                          = 0;
1372
1373
  if( context == NULL )
1374
  {
1375
    libcerror_error_set(
1376
     error,
1377
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1378
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1379
     "%s: invalid context.",
1380
     function );
1381
1382
    return( -1 );
1383
  }
1384
  internal_context = (libcaes_internal_context_t *) context;
1385
1386
  if( ( internal_context->key_bit_size != 128 )
1387
   && ( internal_context->key_bit_size != 192 )
1388
   && ( internal_context->key_bit_size != 256 ) )
1389
  {
1390
    libcerror_error_set(
1391
     error,
1392
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1393
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1394
     "%s: invalid context - unsupported key bit size.",
1395
     function );
1396
1397
    return( -1 );
1398
  }
1399
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
1400
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
1401
  {
1402
    libcerror_error_set(
1403
     error,
1404
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1405
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1406
     "%s: unsupported mode.",
1407
     function );
1408
1409
    return( -1 );
1410
  }
1411
  if( initialization_vector == NULL )
1412
  {
1413
    libcerror_error_set(
1414
     error,
1415
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1416
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1417
     "%s: invalid initialization vector.",
1418
     function );
1419
1420
    return( -1 );
1421
  }
1422
  if( initialization_vector_size != 16 )
1423
  {
1424
    libcerror_error_set(
1425
     error,
1426
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1427
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1428
     "%s: invalid initialization vector size value out of bounds.",
1429
     function );
1430
1431
    return( -1 );
1432
  }
1433
  if( input_data == NULL )
1434
  {
1435
    libcerror_error_set(
1436
     error,
1437
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1438
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1439
     "%s: invalid input data.",
1440
     function );
1441
1442
    return( -1 );
1443
  }
1444
  /* Check if the input data size is a multitude of 16-byte
1445
   */
1446
  if( ( ( input_data_size & (size_t) 0x0f ) != 0 )
1447
   || ( input_data_size > (size_t) INT_MAX ) )
1448
  {
1449
    libcerror_error_set(
1450
     error,
1451
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1452
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1453
     "%s: invalid input data size value out of bounds.",
1454
     function );
1455
1456
    return( -1 );
1457
  }
1458
  if( output_data == NULL )
1459
  {
1460
    libcerror_error_set(
1461
     error,
1462
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1463
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1464
     "%s: invalid output data.",
1465
     function );
1466
1467
    return( -1 );
1468
  }
1469
  if( ( output_data_size < input_data_size )
1470
   || ( output_data_size > (size_t) INT_MAX ) )
1471
  {
1472
    libcerror_error_set(
1473
     error,
1474
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1475
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1476
     "%s: invalid output data size value out of bounds.",
1477
     function );
1478
1479
    return( -1 );
1480
  }
1481
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
1482
  if( EVP_CIPHER_CTX_reset(
1483
       internal_context->evp_cipher_context ) != 1 )
1484
  {
1485
    error_code = ERR_get_error();
1486
1487
    ERR_error_string_n(
1488
     error_code,
1489
     error_string,
1490
     256 );
1491
1492
    libcerror_error_set(
1493
     error,
1494
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1495
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1496
     "%s: unable to reset cipher context with error: %s.",
1497
     function,
1498
     error_string );
1499
1500
    goto on_error;
1501
  }
1502
  if( internal_context->key_bit_size == 128 )
1503
  {
1504
    cipher_string = "AES-128-CBC";
1505
  }
1506
  else if( internal_context->key_bit_size == 192 )
1507
  {
1508
    cipher_string = "AES-192-CBC";
1509
  }
1510
  else if( internal_context->key_bit_size == 256 )
1511
  {
1512
    cipher_string = "AES-256-CBC";
1513
  }
1514
  cipher = EVP_CIPHER_fetch(
1515
            NULL,
1516
            cipher_string,
1517
            NULL );
1518
1519
  if( cipher == NULL )
1520
  {
1521
    libcerror_error_set(
1522
     error,
1523
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1524
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1525
     "%s: missing cipher.",
1526
     function );
1527
1528
    goto on_error;
1529
  }
1530
  parameters[0] = OSSL_PARAM_construct_uint(
1531
                   OSSL_CIPHER_PARAM_PADDING,
1532
                   &padding );
1533
1534
  parameters[1] = OSSL_PARAM_construct_end();
1535
1536
  if( EVP_CipherInit_ex2(
1537
       internal_context->evp_cipher_context,
1538
       cipher,
1539
       (unsigned char *) internal_context->key,
1540
       (unsigned char *) initialization_vector,
1541
       mode,
1542
       parameters ) != 1 )
1543
  {
1544
    error_code = ERR_get_error();
1545
1546
    ERR_error_string_n(
1547
     error_code,
1548
     error_string,
1549
     256 );
1550
1551
    libcerror_error_set(
1552
     error,
1553
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1554
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1555
     "%s: unable to initialize cipher with error: %s.",
1556
     function,
1557
     error_string );
1558
1559
    goto on_error;
1560
  }
1561
#else
1562
  if( internal_context->key_bit_size == 128 )
1563
  {
1564
    cipher = EVP_aes_128_cbc();
1565
  }
1566
  else if( internal_context->key_bit_size == 192 )
1567
  {
1568
    cipher = EVP_aes_192_cbc();
1569
  }
1570
  else if( internal_context->key_bit_size == 256 )
1571
  {
1572
    cipher = EVP_aes_256_cbc();
1573
  }
1574
  if( EVP_CipherInit_ex(
1575
       internal_context->evp_cipher_context,
1576
       cipher,
1577
       NULL,
1578
       (unsigned char *) internal_context->key,
1579
       (unsigned char *) initialization_vector,
1580
       mode ) != 1 )
1581
  {
1582
    error_code = ERR_get_error();
1583
1584
    ERR_error_string_n(
1585
     error_code,
1586
     error_string,
1587
     256 );
1588
1589
    libcerror_error_set(
1590
     error,
1591
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1592
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1593
     "%s: unable to initialize cipher with error: %s.",
1594
     function,
1595
     error_string );
1596
1597
    goto on_error;
1598
  }
1599
  if( EVP_CIPHER_CTX_set_padding(
1600
       internal_context->evp_cipher_context,
1601
       0 ) != 1 )
1602
  {
1603
    error_code = ERR_get_error();
1604
1605
    ERR_error_string_n(
1606
     error_code,
1607
     error_string,
1608
     256 );
1609
1610
    libcerror_error_set(
1611
     error,
1612
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1613
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1614
     "%s: unable to set padding in context with error: %s.",
1615
     function,
1616
     error_string );
1617
1618
    goto on_error;
1619
  }
1620
#endif /* defined( HAVE_EVP_CIPHERINIT_EX2 ) */
1621
1622
  if( EVP_CipherUpdate(
1623
       internal_context->evp_cipher_context,
1624
       (unsigned char *) output_data,
1625
       &safe_output_data_size,
1626
       (unsigned char *) input_data,
1627
       input_data_size ) != 1 )
1628
  {
1629
    error_code = ERR_get_error();
1630
1631
    ERR_error_string_n(
1632
     error_code,
1633
     error_string,
1634
     256 );
1635
1636
    libcerror_error_set(
1637
     error,
1638
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1639
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1640
     "%s: unable to update cipher with error: %s.",
1641
     function,
1642
     error_string );
1643
1644
    goto on_error;
1645
  }
1646
  /* Get any remaining data, i.e. the final block, and write it
1647
   * to the output data
1648
   */
1649
  total_data_size = 0;
1650
1651
  if( EVP_CipherFinal_ex(
1652
       internal_context->evp_cipher_context,
1653
       (unsigned char *) &( output_data[ (size_t) safe_output_data_size ] ),
1654
       &total_data_size ) != 1 )
1655
  {
1656
    error_code = ERR_get_error();
1657
1658
    ERR_error_string_n(
1659
     error_code,
1660
     error_string,
1661
     256 );
1662
1663
    libcerror_error_set(
1664
     error,
1665
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1666
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1667
     "%s: unable to finalize cipher with error: %s.",
1668
     function,
1669
     error_string );
1670
1671
    goto on_error;
1672
  }
1673
  safe_output_data_size += total_data_size;
1674
1675
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
1676
  EVP_CIPHER_free(
1677
   cipher );
1678
#endif
1679
  return( 1 );
1680
1681
on_error:
1682
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
1683
  if( cipher != NULL )
1684
  {
1685
    EVP_CIPHER_free(
1686
     cipher );
1687
  }
1688
#endif
1689
  return( -1 );
1690
}
1691
1692
#else
1693
1694
/* De- or encrypts a block of data using AES-CBC (Cipher Block Chaining) using fallback implementation
1695
 * The size must be a multitude of the AES block size (16 byte)
1696
 * Returns 1 if successful or -1 on error
1697
 */
1698
int libcaes_crypt_cbc(
1699
     libcaes_context_t *context,
1700
     int mode,
1701
     const uint8_t *initialization_vector,
1702
     size_t initialization_vector_size,
1703
     const uint8_t *input_data,
1704
     size_t input_data_size,
1705
     uint8_t *output_data,
1706
     size_t output_data_size,
1707
     libcerror_error_t **error )
1708
14.9k
{
1709
14.9k
  uint8_t internal_initialization_vector[ 16 ];
1710
1711
14.9k
  static char *function = "libcaes_crypt_cbc";
1712
14.9k
  size_t data_offset    = 0;
1713
1714
#if !defined( LIBCAES_UNFOLLED_LOOPS )
1715
  uint8_t block_index   = 0;
1716
#endif
1717
1718
14.9k
  if( context == NULL )
1719
0
  {
1720
0
    libcerror_error_set(
1721
0
     error,
1722
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1723
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1724
0
     "%s: invalid context.",
1725
0
     function );
1726
1727
0
    return( -1 );
1728
0
  }
1729
14.9k
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
1730
50
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
1731
0
  {
1732
0
    libcerror_error_set(
1733
0
     error,
1734
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1735
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1736
0
     "%s: unsupported mode.",
1737
0
     function );
1738
1739
0
    return( -1 );
1740
0
  }
1741
14.9k
  if( initialization_vector == NULL )
1742
0
  {
1743
0
    libcerror_error_set(
1744
0
     error,
1745
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1746
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1747
0
     "%s: invalid initialization vector.",
1748
0
     function );
1749
1750
0
    return( -1 );
1751
0
  }
1752
14.9k
  if( initialization_vector_size != 16 )
1753
0
  {
1754
0
    libcerror_error_set(
1755
0
     error,
1756
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1757
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1758
0
     "%s: invalid initialization vector size value out of bounds.",
1759
0
     function );
1760
1761
0
    return( -1 );
1762
0
  }
1763
14.9k
  if( input_data == NULL )
1764
0
  {
1765
0
    libcerror_error_set(
1766
0
     error,
1767
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1768
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1769
0
     "%s: invalid input data.",
1770
0
     function );
1771
1772
0
    return( -1 );
1773
0
  }
1774
  /* Check if the input data size is a multitude of 16-byte
1775
   */
1776
14.9k
  if( ( ( input_data_size & (size_t) 0x0f ) != 0 )
1777
14.9k
   || ( input_data_size < 16 )
1778
14.9k
   || ( input_data_size > (size_t) SSIZE_MAX ) )
1779
7
  {
1780
7
    libcerror_error_set(
1781
7
     error,
1782
7
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1783
7
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1784
7
     "%s: invalid input data size value out of bounds.",
1785
7
     function );
1786
1787
7
    return( -1 );
1788
7
  }
1789
14.9k
  if( output_data == NULL )
1790
0
  {
1791
0
    libcerror_error_set(
1792
0
     error,
1793
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1794
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1795
0
     "%s: invalid output data.",
1796
0
     function );
1797
1798
0
    return( -1 );
1799
0
  }
1800
14.9k
  if( ( output_data_size < input_data_size )
1801
14.8k
   || ( output_data_size > (size_t) SSIZE_MAX ) )
1802
41
  {
1803
41
    libcerror_error_set(
1804
41
     error,
1805
41
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1806
41
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1807
41
     "%s: invalid output data size value of bounds.",
1808
41
     function );
1809
1810
41
    return( -1 );
1811
41
  }
1812
14.8k
  if( memory_copy(
1813
14.8k
       internal_initialization_vector,
1814
14.8k
       initialization_vector,
1815
14.8k
       16 ) == NULL )
1816
0
  {
1817
0
    libcerror_error_set(
1818
0
     error,
1819
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1820
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1821
0
     "%s: unable to copy initialization vector.",
1822
0
     function );
1823
1824
0
    goto on_error;
1825
0
  }
1826
14.8k
  if( ( mode == LIBCAES_CRYPT_MODE_ENCRYPT )
1827
4
   && ( output_data != input_data ) )
1828
4
  {
1829
4
    if( memory_copy(
1830
4
         output_data,
1831
4
         input_data,
1832
4
         input_data_size ) == NULL )
1833
0
    {
1834
0
      libcerror_error_set(
1835
0
       error,
1836
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1837
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1838
0
       "%s: unable to copy input data to output data.",
1839
0
       function );
1840
1841
0
      goto on_error;
1842
0
    }
1843
4
  }
1844
462k
  while( data_offset <= ( input_data_size - 16 ) )
1845
447k
  {
1846
447k
    if( mode == LIBCAES_CRYPT_MODE_ENCRYPT )
1847
10
    {
1848
10
#if defined( LIBCAES_UNFOLLED_LOOPS )
1849
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 0 ];
1850
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 1 ];
1851
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 2 ];
1852
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 3 ];
1853
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 4 ];
1854
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 5 ];
1855
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 6 ];
1856
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 7 ];
1857
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 8 ];
1858
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 9 ];
1859
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 10 ];
1860
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 11 ];
1861
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 12 ];
1862
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 13 ];
1863
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 14 ];
1864
10
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 15 ];
1865
#else
1866
      for( block_index = 0;
1867
           block_index < 16;
1868
           block_index++ )
1869
      {
1870
        output_data[ data_offset++ ] ^= internal_initialization_vector[ block_index ];
1871
      }
1872
#endif
1873
10
      data_offset -= 16;
1874
1875
10
      if( libcaes_crypt_ecb(
1876
10
           context,
1877
10
           LIBCAES_CRYPT_MODE_ENCRYPT,
1878
10
           &( output_data[ data_offset ] ),
1879
10
           16,
1880
10
           &( output_data[ data_offset ] ),
1881
10
           16,
1882
10
           error ) != 1 )
1883
0
      {
1884
0
        libcerror_error_set(
1885
0
         error,
1886
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1887
0
         LIBCERROR_ENCRYPTION_ERROR_ENCRYPT_FAILED,
1888
0
         "%s: unable to encrypt output data.",
1889
0
         function );
1890
1891
0
        goto on_error;
1892
0
      }
1893
10
      if( memory_copy(
1894
10
           internal_initialization_vector,
1895
10
           &( output_data[ data_offset ] ),
1896
10
           16 ) == NULL )
1897
0
      {
1898
0
        libcerror_error_set(
1899
0
         error,
1900
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
1901
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1902
0
         "%s: unable to copy enrypted output data to initialization vector.",
1903
0
         function );
1904
1905
0
        goto on_error;
1906
0
      }
1907
10
    }
1908
447k
    else
1909
447k
    {
1910
447k
      if( libcaes_crypt_ecb(
1911
447k
           context,
1912
447k
           LIBCAES_CRYPT_MODE_DECRYPT,
1913
447k
           &( input_data[ data_offset ] ),
1914
447k
           16,
1915
447k
           &( output_data[ data_offset ] ),
1916
447k
           16,
1917
447k
           error ) != 1 )
1918
0
      {
1919
0
        libcerror_error_set(
1920
0
         error,
1921
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1922
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1923
0
         "%s: unable to decrypt output data.",
1924
0
         function );
1925
1926
0
        goto on_error;
1927
0
      }
1928
447k
#if defined( LIBCAES_UNFOLLED_LOOPS )
1929
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 0 ];
1930
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 1 ];
1931
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 2 ];
1932
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 3 ];
1933
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 4 ];
1934
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 5 ];
1935
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 6 ];
1936
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 7 ];
1937
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 8 ];
1938
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 9 ];
1939
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 10 ];
1940
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 11 ];
1941
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 12 ];
1942
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 13 ];
1943
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 14 ];
1944
447k
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 15 ];
1945
#else
1946
      for( block_index = 0;
1947
           block_index < 16;
1948
           block_index++ )
1949
      {
1950
        output_data[ data_offset++ ] ^= internal_initialization_vector[ block_index ];
1951
      }
1952
#endif
1953
447k
      data_offset -= 16;
1954
1955
447k
      if( memory_copy(
1956
447k
           internal_initialization_vector,
1957
447k
           &( input_data[ data_offset ] ),
1958
447k
           16 ) == NULL )
1959
0
      {
1960
0
        libcerror_error_set(
1961
0
         error,
1962
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
1963
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1964
0
         "%s: unable to copy enrypted input data to initialization vector.",
1965
0
         function );
1966
1967
0
        goto on_error;
1968
0
      }
1969
447k
    }
1970
447k
    data_offset += 16;
1971
447k
    }
1972
14.8k
  if( memory_set(
1973
14.8k
       internal_initialization_vector,
1974
14.8k
       0,
1975
14.8k
       16 ) == NULL )
1976
0
  {
1977
0
    libcerror_error_set(
1978
0
     error,
1979
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1980
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
1981
0
     "%s: unable to clear initialization vector.",
1982
0
     function );
1983
1984
0
    goto on_error;
1985
0
  }
1986
14.8k
  return( 1 );
1987
1988
0
on_error:
1989
0
  memory_set(
1990
0
   internal_initialization_vector,
1991
0
   0,
1992
0
   16 );
1993
1994
0
  return( -1 );
1995
14.8k
}
1996
1997
#endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && defined( HAVE_AES_CBC_ENCRYPT ) */
1998
1999
/* De- or encrypts a block of data using AES-CCM (Counter with CBC-MAC)
2000
 * Note that the key must be set in encryption mode (LIBCAES_CRYPT_MODE_ENCRYPT) for both de- and encryption.
2001
 *
2002
 * WARNING: this function does not implement RFC 3610 compliant AES-CCM
2003
 * This function is deprecated use libcaes_crypt_ccm_ctr_only instead.
2004
 *
2005
 * Returns 1 if successful or -1 on error
2006
 */
2007
int libcaes_crypt_ccm(
2008
     libcaes_context_t *context,
2009
     int mode,
2010
     const uint8_t *nonce,
2011
     size_t nonce_size,
2012
     const uint8_t *input_data,
2013
     size_t input_data_size,
2014
     uint8_t *output_data,
2015
     size_t output_data_size,
2016
     libcerror_error_t **error )
2017
72
{
2018
72
  uint8_t block_data[ 16 ];
2019
72
  uint8_t internal_initialization_vector[ 16 ];
2020
2021
72
  static char *function      = "libcaes_crypt_ccm";
2022
72
  size_t data_offset         = 0;
2023
72
  size_t remaining_data_size = 0;
2024
72
  uint8_t block_index        = 0;
2025
2026
72
  if( context == NULL )
2027
0
  {
2028
0
    libcerror_error_set(
2029
0
     error,
2030
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2031
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2032
0
     "%s: invalid context.",
2033
0
     function );
2034
2035
0
    return( -1 );
2036
0
  }
2037
72
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
2038
72
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
2039
0
  {
2040
0
    libcerror_error_set(
2041
0
     error,
2042
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2043
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2044
0
     "%s: unsupported mode.",
2045
0
     function );
2046
2047
0
    return( -1 );
2048
0
  }
2049
72
  if( nonce == NULL )
2050
0
  {
2051
0
    libcerror_error_set(
2052
0
     error,
2053
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2054
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2055
0
     "%s: invalid nonce.",
2056
0
     function );
2057
2058
0
    return( -1 );
2059
0
  }
2060
72
  if( nonce_size >= (size_t) 15 )
2061
0
  {
2062
0
    libcerror_error_set(
2063
0
     error,
2064
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2065
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2066
0
     "%s: invalid nonce size value out of bounds.",
2067
0
     function );
2068
2069
0
    return( -1 );
2070
0
  }
2071
72
  if( input_data == NULL )
2072
0
  {
2073
0
    libcerror_error_set(
2074
0
     error,
2075
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2076
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2077
0
     "%s: invalid input data.",
2078
0
     function );
2079
2080
0
    return( -1 );
2081
0
  }
2082
72
  if( ( input_data_size < 16 )
2083
65
   || ( input_data_size > (size_t) SSIZE_MAX ) )
2084
7
  {
2085
7
    libcerror_error_set(
2086
7
     error,
2087
7
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2088
7
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2089
7
     "%s: invalid input data size value out of bounds.",
2090
7
     function );
2091
2092
7
    return( -1 );
2093
7
  }
2094
65
  if( output_data == NULL )
2095
0
  {
2096
0
    libcerror_error_set(
2097
0
     error,
2098
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2099
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2100
0
     "%s: invalid output data.",
2101
0
     function );
2102
2103
0
    return( -1 );
2104
0
  }
2105
65
  if( ( output_data_size < input_data_size )
2106
18
   || ( output_data_size > (size_t) SSIZE_MAX ) )
2107
47
  {
2108
47
    libcerror_error_set(
2109
47
     error,
2110
47
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2111
47
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2112
47
     "%s: invalid output data size value out of bounds.",
2113
47
     function );
2114
2115
47
    return( -1 );
2116
47
  }
2117
  /* The IV consists of:
2118
   * 1 byte size value formatted as: 15 - nonce size - 1
2119
   * a maximum of 14 bytes containing nonce bytes
2120
   * 1 byte counter
2121
   */
2122
18
  if( memory_set(
2123
18
       internal_initialization_vector,
2124
18
       0,
2125
18
       16 ) == NULL )
2126
0
  {
2127
0
    libcerror_error_set(
2128
0
     error,
2129
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2130
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2131
0
     "%s: unable to clear initialization vector.",
2132
0
     function );
2133
2134
0
    goto on_error;
2135
0
  }
2136
18
  if( memory_copy(
2137
18
       &( internal_initialization_vector[ 1 ] ),
2138
18
       nonce,
2139
18
       nonce_size ) == NULL )
2140
0
  {
2141
0
    libcerror_error_set(
2142
0
     error,
2143
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2144
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2145
0
     "%s: unable to copy nonce to initialization vector.",
2146
0
     function );
2147
2148
0
    goto on_error;
2149
0
  }
2150
18
  internal_initialization_vector[ 0 ] = 15 - (uint8_t) nonce_size - 1;
2151
2152
18
  if( memory_copy(
2153
18
       output_data,
2154
18
       input_data,
2155
18
       input_data_size ) == NULL )
2156
0
  {
2157
0
    libcerror_error_set(
2158
0
     error,
2159
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2160
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2161
0
     "%s: unable to copy input data to output data.",
2162
0
     function );
2163
2164
0
    goto on_error;
2165
0
  }
2166
52
  while( data_offset <= ( input_data_size - 16 ) )
2167
34
  {
2168
34
    if( libcaes_crypt_ecb(
2169
34
         context,
2170
34
         LIBCAES_CRYPT_MODE_ENCRYPT,
2171
34
         internal_initialization_vector,
2172
34
         16,
2173
34
         block_data,
2174
34
         16,
2175
34
         error ) != 1 )
2176
0
    {
2177
0
      libcerror_error_set(
2178
0
       error,
2179
0
       LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2180
0
       LIBCERROR_ENCRYPTION_ERROR_ENCRYPT_FAILED,
2181
0
       "%s: unable to encrypt initialization vector.",
2182
0
       function );
2183
2184
0
      goto on_error;
2185
0
    }
2186
34
#if defined( LIBCAES_UNFOLLED_LOOPS )
2187
34
    output_data[ data_offset++ ] ^= block_data[ 0 ];
2188
34
    output_data[ data_offset++ ] ^= block_data[ 1 ];
2189
34
    output_data[ data_offset++ ] ^= block_data[ 2 ];
2190
34
    output_data[ data_offset++ ] ^= block_data[ 3 ];
2191
34
    output_data[ data_offset++ ] ^= block_data[ 4 ];
2192
34
    output_data[ data_offset++ ] ^= block_data[ 5 ];
2193
34
    output_data[ data_offset++ ] ^= block_data[ 6 ];
2194
34
    output_data[ data_offset++ ] ^= block_data[ 7 ];
2195
34
    output_data[ data_offset++ ] ^= block_data[ 8 ];
2196
34
    output_data[ data_offset++ ] ^= block_data[ 9 ];
2197
34
    output_data[ data_offset++ ] ^= block_data[ 10 ];
2198
34
    output_data[ data_offset++ ] ^= block_data[ 11 ];
2199
34
    output_data[ data_offset++ ] ^= block_data[ 12 ];
2200
34
    output_data[ data_offset++ ] ^= block_data[ 13 ];
2201
34
    output_data[ data_offset++ ] ^= block_data[ 14 ];
2202
34
    output_data[ data_offset++ ] ^= block_data[ 15 ];
2203
#else
2204
    for( block_index = 0;
2205
         block_index < 16;
2206
         block_index++ )
2207
    {
2208
      output_data[ data_offset++ ] ^= block_data[ block_index ];
2209
    }
2210
#endif
2211
34
    internal_initialization_vector[ 15 ] += 1;
2212
34
  }
2213
18
  if( data_offset < input_data_size )
2214
14
  {
2215
14
    remaining_data_size = input_data_size - data_offset;
2216
2217
14
    if( libcaes_crypt_ecb(
2218
14
         context,
2219
14
         LIBCAES_CRYPT_MODE_ENCRYPT,
2220
14
         internal_initialization_vector,
2221
14
         16,
2222
14
         block_data,
2223
14
         16,
2224
14
         error ) != 1 )
2225
0
    {
2226
0
      libcerror_error_set(
2227
0
       error,
2228
0
       LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2229
0
       LIBCERROR_ENCRYPTION_ERROR_ENCRYPT_FAILED,
2230
0
       "%s: unable to encrypt initialization vector.",
2231
0
       function );
2232
2233
0
      goto on_error;
2234
0
    }
2235
14
    for( block_index = 0;
2236
114
         block_index < (uint8_t) remaining_data_size;
2237
100
         block_index++ )
2238
100
    {
2239
100
      output_data[ data_offset++ ] ^= block_data[ block_index ];
2240
100
    }
2241
14
  }
2242
18
  if( memory_set(
2243
18
       block_data,
2244
18
       0,
2245
18
       16 ) == NULL )
2246
0
  {
2247
0
    libcerror_error_set(
2248
0
     error,
2249
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2250
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2251
0
     "%s: unable to clear block data.",
2252
0
     function );
2253
2254
0
    goto on_error;
2255
0
  }
2256
18
  if( memory_set(
2257
18
       internal_initialization_vector,
2258
18
       0,
2259
18
       16 ) == NULL )
2260
0
  {
2261
0
    libcerror_error_set(
2262
0
     error,
2263
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2264
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2265
0
     "%s: unable to clear initialization vector.",
2266
0
     function );
2267
2268
0
    goto on_error;
2269
0
  }
2270
18
  return( 1 );
2271
2272
0
on_error:
2273
0
  memory_set(
2274
0
   block_data,
2275
0
   0,
2276
0
   16 );
2277
2278
0
  memory_set(
2279
0
   internal_initialization_vector,
2280
0
   0,
2281
0
   16 );
2282
2283
0
  return( -1 );
2284
18
}
2285
2286
/* De- or encrypts a block of data using only the Counter (CTR) mode of AES-CCM (Counter with CBC-MAC)
2287
 * Note that the key must be set in encryption mode (LIBCAES_CRYPT_MODE_ENCRYPT) for both de- and encryption.
2288
 * Returns 1 if successful or -1 on error
2289
 */
2290
int libcaes_crypt_ccm_ctr_only(
2291
     libcaes_context_t *context,
2292
     int mode,
2293
     const uint8_t *nonce,
2294
     size_t nonce_size,
2295
     const uint8_t *input_data,
2296
     size_t input_data_size,
2297
     uint8_t *output_data,
2298
     size_t output_data_size,
2299
     libcerror_error_t **error )
2300
1.34k
{
2301
1.34k
  uint8_t block_data[ 16 ];
2302
1.34k
  uint8_t internal_initialization_vector[ 16 ];
2303
2304
1.34k
  static char *function      = "libcaes_crypt_ccm_ctr_only";
2305
1.34k
  size_t data_offset         = 0;
2306
1.34k
  size_t remaining_data_size = 0;
2307
1.34k
  uint8_t block_index        = 0;
2308
1.34k
  uint8_t l_prime            = 0;
2309
2310
1.34k
  if( context == NULL )
2311
0
  {
2312
0
    libcerror_error_set(
2313
0
     error,
2314
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2315
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2316
0
     "%s: invalid context.",
2317
0
     function );
2318
2319
0
    return( -1 );
2320
0
  }
2321
1.34k
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
2322
0
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
2323
0
  {
2324
0
    libcerror_error_set(
2325
0
     error,
2326
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2327
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2328
0
     "%s: unsupported mode.",
2329
0
     function );
2330
2331
0
    return( -1 );
2332
0
  }
2333
1.34k
  if( nonce == NULL )
2334
0
  {
2335
0
    libcerror_error_set(
2336
0
     error,
2337
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2338
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2339
0
     "%s: invalid nonce.",
2340
0
     function );
2341
2342
0
    return( -1 );
2343
0
  }
2344
1.34k
  if( nonce_size >= (size_t) 15 )
2345
0
  {
2346
0
    libcerror_error_set(
2347
0
     error,
2348
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2349
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2350
0
     "%s: invalid nonce size value out of bounds.",
2351
0
     function );
2352
2353
0
    return( -1 );
2354
0
  }
2355
1.34k
  if( input_data == NULL )
2356
0
  {
2357
0
    libcerror_error_set(
2358
0
     error,
2359
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2360
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2361
0
     "%s: invalid input data.",
2362
0
     function );
2363
2364
0
    return( -1 );
2365
0
  }
2366
1.34k
  if( ( input_data_size < 16 )
2367
1.33k
   || ( input_data_size > (size_t) SSIZE_MAX ) )
2368
4
  {
2369
4
    libcerror_error_set(
2370
4
     error,
2371
4
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2372
4
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2373
4
     "%s: invalid input data size value out of bounds.",
2374
4
     function );
2375
2376
4
    return( -1 );
2377
4
  }
2378
1.33k
  if( output_data == NULL )
2379
0
  {
2380
0
    libcerror_error_set(
2381
0
     error,
2382
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2383
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2384
0
     "%s: invalid output data.",
2385
0
     function );
2386
2387
0
    return( -1 );
2388
0
  }
2389
1.33k
  if( ( output_data_size < input_data_size )
2390
1.33k
   || ( output_data_size > (size_t) SSIZE_MAX ) )
2391
0
  {
2392
0
    libcerror_error_set(
2393
0
     error,
2394
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2395
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2396
0
     "%s: invalid output data size value out of bounds.",
2397
0
     function );
2398
2399
0
    return( -1 );
2400
0
  }
2401
1.33k
  l_prime = 15 - (uint8_t) nonce_size - 1;
2402
2403
  /* The IV consists of:
2404
   * 1 byte size value formatted as: 15 - nonce size - 1
2405
   * a maximum of 14 bytes containing nonce bytes
2406
   * 1 byte counter
2407
   */
2408
1.33k
  if( memory_set(
2409
1.33k
       internal_initialization_vector,
2410
1.33k
       0,
2411
1.33k
       16 ) == NULL )
2412
0
  {
2413
0
    libcerror_error_set(
2414
0
     error,
2415
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2416
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2417
0
     "%s: unable to clear initialization vector.",
2418
0
     function );
2419
2420
0
    goto on_error;
2421
0
  }
2422
1.33k
  internal_initialization_vector[ 0 ] = l_prime;
2423
2424
1.33k
  if( memory_copy(
2425
1.33k
       &( internal_initialization_vector[ 1 ] ),
2426
1.33k
       nonce,
2427
1.33k
       nonce_size ) == NULL )
2428
0
  {
2429
0
    libcerror_error_set(
2430
0
     error,
2431
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2432
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2433
0
     "%s: unable to copy nonce to initialization vector.",
2434
0
     function );
2435
2436
0
    goto on_error;
2437
0
  }
2438
1.33k
  if( memory_copy(
2439
1.33k
       output_data,
2440
1.33k
       input_data,
2441
1.33k
       input_data_size ) == NULL )
2442
0
  {
2443
0
    libcerror_error_set(
2444
0
     error,
2445
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2446
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2447
0
     "%s: unable to copy input data to output data.",
2448
0
     function );
2449
2450
0
    goto on_error;
2451
0
  }
2452
13.7k
  while( data_offset <= ( input_data_size - 16 ) )
2453
12.3k
  {
2454
12.3k
    internal_initialization_vector[ 15 ] += 1;
2455
2456
12.3k
    if( libcaes_crypt_ecb(
2457
12.3k
         context,
2458
12.3k
         LIBCAES_CRYPT_MODE_ENCRYPT,
2459
12.3k
         internal_initialization_vector,
2460
12.3k
         16,
2461
12.3k
         block_data,
2462
12.3k
         16,
2463
12.3k
         error ) != 1 )
2464
0
    {
2465
0
      libcerror_error_set(
2466
0
       error,
2467
0
       LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2468
0
       LIBCERROR_ENCRYPTION_ERROR_ENCRYPT_FAILED,
2469
0
       "%s: unable to encrypt initialization vector.",
2470
0
       function );
2471
2472
0
      goto on_error;
2473
0
    }
2474
12.3k
#if defined( LIBCAES_UNFOLLED_LOOPS )
2475
12.3k
    output_data[ data_offset++ ] ^= block_data[ 0 ];
2476
12.3k
    output_data[ data_offset++ ] ^= block_data[ 1 ];
2477
12.3k
    output_data[ data_offset++ ] ^= block_data[ 2 ];
2478
12.3k
    output_data[ data_offset++ ] ^= block_data[ 3 ];
2479
12.3k
    output_data[ data_offset++ ] ^= block_data[ 4 ];
2480
12.3k
    output_data[ data_offset++ ] ^= block_data[ 5 ];
2481
12.3k
    output_data[ data_offset++ ] ^= block_data[ 6 ];
2482
12.3k
    output_data[ data_offset++ ] ^= block_data[ 7 ];
2483
12.3k
    output_data[ data_offset++ ] ^= block_data[ 8 ];
2484
12.3k
    output_data[ data_offset++ ] ^= block_data[ 9 ];
2485
12.3k
    output_data[ data_offset++ ] ^= block_data[ 10 ];
2486
12.3k
    output_data[ data_offset++ ] ^= block_data[ 11 ];
2487
12.3k
    output_data[ data_offset++ ] ^= block_data[ 12 ];
2488
12.3k
    output_data[ data_offset++ ] ^= block_data[ 13 ];
2489
12.3k
    output_data[ data_offset++ ] ^= block_data[ 14 ];
2490
12.3k
    output_data[ data_offset++ ] ^= block_data[ 15 ];
2491
#else
2492
    for( block_index = 0;
2493
         block_index < 16;
2494
         block_index++ )
2495
    {
2496
      output_data[ data_offset++ ] ^= block_data[ block_index ];
2497
    }
2498
#endif
2499
12.3k
  }
2500
1.33k
  if( data_offset < input_data_size )
2501
1.15k
  {
2502
1.15k
    remaining_data_size = input_data_size - data_offset;
2503
2504
1.15k
    internal_initialization_vector[ 15 ] += 1;
2505
2506
1.15k
    if( libcaes_crypt_ecb(
2507
1.15k
         context,
2508
1.15k
         LIBCAES_CRYPT_MODE_ENCRYPT,
2509
1.15k
         internal_initialization_vector,
2510
1.15k
         16,
2511
1.15k
         block_data,
2512
1.15k
         16,
2513
1.15k
         error ) != 1 )
2514
0
    {
2515
0
      libcerror_error_set(
2516
0
       error,
2517
0
       LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2518
0
       LIBCERROR_ENCRYPTION_ERROR_ENCRYPT_FAILED,
2519
0
       "%s: unable to encrypt initialization vector.",
2520
0
       function );
2521
2522
0
      goto on_error;
2523
0
    }
2524
1.15k
    for( block_index = 0;
2525
8.78k
         block_index < (uint8_t) remaining_data_size;
2526
7.62k
         block_index++ )
2527
7.62k
    {
2528
7.62k
      output_data[ data_offset++ ] ^= block_data[ block_index ];
2529
7.62k
    }
2530
1.15k
  }
2531
1.33k
  if( memory_set(
2532
1.33k
       block_data,
2533
1.33k
       0,
2534
1.33k
       16 ) == NULL )
2535
0
  {
2536
0
    libcerror_error_set(
2537
0
     error,
2538
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2539
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2540
0
     "%s: unable to clear block data.",
2541
0
     function );
2542
2543
0
    goto on_error;
2544
0
  }
2545
1.33k
  if( memory_set(
2546
1.33k
       internal_initialization_vector,
2547
1.33k
       0,
2548
1.33k
       16 ) == NULL )
2549
0
  {
2550
0
    libcerror_error_set(
2551
0
     error,
2552
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2553
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2554
0
     "%s: unable to clear initialization vector.",
2555
0
     function );
2556
2557
0
    goto on_error;
2558
0
  }
2559
1.33k
  return( 1 );
2560
2561
0
on_error:
2562
0
  memory_set(
2563
0
   block_data,
2564
0
   0,
2565
0
   16 );
2566
2567
0
  memory_set(
2568
0
   internal_initialization_vector,
2569
0
   0,
2570
0
   16 );
2571
2572
0
  return( -1 );
2573
1.33k
}
2574
2575
/* De- or encrypts a block of data using AES-CFB (Cipher Feedback Mode)
2576
 * Note that the key must be set with mode LIBCAES_CRYPT_MODE_ENCRYPT
2577
 * Returns 1 if successful or -1 on error
2578
 */
2579
int libcaes_crypt_cfb(
2580
     libcaes_context_t *context,
2581
     int mode,
2582
     const uint8_t *initialization_vector,
2583
     size_t initialization_vector_size,
2584
     const uint8_t *input_data,
2585
     size_t input_data_size,
2586
     uint8_t *output_data,
2587
     size_t output_data_size,
2588
     libcerror_error_t **error )
2589
0
{
2590
0
  uint8_t internal_initialization_vector[ 16 ];
2591
2592
0
  static char *function              = "libcaes_crypt_cfb";
2593
0
  size_t data_offset                 = 0;
2594
0
  size_t initialization_vector_index = 0;
2595
0
  uint8_t byte_value                 = 0;
2596
2597
0
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
2598
0
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
2599
0
  {
2600
0
    libcerror_error_set(
2601
0
     error,
2602
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2603
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2604
0
     "%s: unsupported mode.",
2605
0
     function );
2606
2607
0
    return( -1 );
2608
0
  }
2609
0
  if( initialization_vector == NULL )
2610
0
  {
2611
0
    libcerror_error_set(
2612
0
     error,
2613
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2614
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2615
0
     "%s: invalid initialization vector.",
2616
0
     function );
2617
2618
0
    return( -1 );
2619
0
  }
2620
0
  if( initialization_vector_size != 16 )
2621
0
  {
2622
0
    libcerror_error_set(
2623
0
     error,
2624
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2625
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2626
0
     "%s: invalid initialization vector size value out of bounds.",
2627
0
     function );
2628
2629
0
    return( -1 );
2630
0
  }
2631
0
  if( input_data == NULL )
2632
0
  {
2633
0
    libcerror_error_set(
2634
0
     error,
2635
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2636
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2637
0
     "%s: invalid input data.",
2638
0
     function );
2639
2640
0
    return( -1 );
2641
0
  }
2642
  /* Check if the input data size is a multitude of 16-byte
2643
   */
2644
0
  if( ( ( input_data_size & (size_t) 0x0f ) != 0 )
2645
0
   || ( input_data_size < 16 )
2646
0
   || ( input_data_size > (size_t) SSIZE_MAX ) )
2647
0
  {
2648
0
    libcerror_error_set(
2649
0
     error,
2650
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2651
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2652
0
     "%s: invalid input data size value out of bounds.",
2653
0
     function );
2654
2655
0
    return( -1 );
2656
0
  }
2657
0
  if( output_data == NULL )
2658
0
  {
2659
0
    libcerror_error_set(
2660
0
     error,
2661
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2662
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2663
0
     "%s: invalid output data.",
2664
0
     function );
2665
2666
0
    return( -1 );
2667
0
  }
2668
0
  if( ( output_data_size < input_data_size )
2669
0
   || ( output_data_size > (size_t) SSIZE_MAX ) )
2670
0
  {
2671
0
    libcerror_error_set(
2672
0
     error,
2673
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2674
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2675
0
     "%s: invalid output data size value out of bounds.",
2676
0
     function );
2677
2678
0
    return( -1 );
2679
0
  }
2680
0
  if( memory_copy(
2681
0
       internal_initialization_vector,
2682
0
       initialization_vector,
2683
0
       16 ) == NULL )
2684
0
  {
2685
0
    libcerror_error_set(
2686
0
     error,
2687
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2688
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2689
0
     "%s: unable to copy initialization vector.",
2690
0
     function );
2691
2692
0
    goto on_error;
2693
0
  }
2694
0
  initialization_vector_index = 16;
2695
2696
0
  for( data_offset = 0;
2697
0
       data_offset < input_data_size;
2698
0
       data_offset++ )
2699
0
  {
2700
0
    if( initialization_vector_index > 15 )
2701
0
    {
2702
0
      if( libcaes_crypt_ecb(
2703
0
           context,
2704
0
           LIBCAES_CRYPT_MODE_ENCRYPT,
2705
0
           internal_initialization_vector,
2706
0
           initialization_vector_size,
2707
0
           internal_initialization_vector,
2708
0
           initialization_vector_size,
2709
0
           error ) != 1 )
2710
0
      {
2711
0
        libcerror_error_set(
2712
0
         error,
2713
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2714
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
2715
0
         "%s: unable to de/encrypt initialization vector.",
2716
0
         function );
2717
2718
0
        goto on_error;
2719
0
      }
2720
0
      initialization_vector_index = 0;
2721
0
    }
2722
0
    output_data[ data_offset ] = input_data[ data_offset ] ^ internal_initialization_vector[ initialization_vector_index ];
2723
2724
0
    if( mode == LIBCAES_CRYPT_MODE_ENCRYPT )
2725
0
    {
2726
0
      byte_value = output_data[ data_offset ];
2727
0
    }
2728
0
    else
2729
0
    {
2730
0
      byte_value = input_data[ data_offset ];
2731
0
    }
2732
0
    internal_initialization_vector[ initialization_vector_index++ ] = byte_value;
2733
0
  }
2734
0
  if( memory_set(
2735
0
       internal_initialization_vector,
2736
0
       0,
2737
0
       16 ) == NULL )
2738
0
  {
2739
0
    libcerror_error_set(
2740
0
     error,
2741
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2742
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2743
0
     "%s: unable to clear initialization vector.",
2744
0
     function );
2745
2746
0
    goto on_error;
2747
0
  }
2748
0
  return( 1 );
2749
2750
0
on_error:
2751
0
  memory_set(
2752
0
   internal_initialization_vector,
2753
0
   0,
2754
0
   16 );
2755
2756
0
  return( -1 );
2757
0
}
2758
2759
#if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && defined( HAVE_AES_ECB_ENCRYPT )
2760
2761
/* De- or encrypts a block of data using AES-ECB (Electronic CodeBook) using OpenSSL
2762
 * The size must be a multitude of the AES block size (16 byte)
2763
 * Returns 1 if successful or -1 on error
2764
 */
2765
int libcaes_crypt_ecb(
2766
     libcaes_context_t *context,
2767
     int mode,
2768
     const uint8_t *input_data,
2769
     size_t input_data_size,
2770
     uint8_t *output_data,
2771
     size_t output_data_size,
2772
     libcerror_error_t **error )
2773
{
2774
  libcaes_internal_context_t *internal_context = NULL;
2775
  static char *function                        = "libcaes_crypt_ecb";
2776
  int result                                   = 1;
2777
  int safe_mode                                = 0;
2778
2779
  if( context == NULL )
2780
  {
2781
    libcerror_error_set(
2782
     error,
2783
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2784
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2785
     "%s: invalid context.",
2786
     function );
2787
2788
    return( -1 );
2789
  }
2790
  internal_context = (libcaes_internal_context_t *) context;
2791
2792
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
2793
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
2794
  {
2795
    libcerror_error_set(
2796
     error,
2797
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2798
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2799
     "%s: unsupported mode.",
2800
     function );
2801
2802
    return( -1 );
2803
  }
2804
  if( input_data == NULL )
2805
  {
2806
    libcerror_error_set(
2807
     error,
2808
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2809
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2810
     "%s: invalid input data.",
2811
     function );
2812
2813
    return( -1 );
2814
  }
2815
  if( ( input_data_size < 16 )
2816
   || ( input_data_size > (size_t) SSIZE_MAX ) )
2817
  {
2818
    libcerror_error_set(
2819
     error,
2820
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2821
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2822
     "%s: invalid input data size value out of bounds.",
2823
     function );
2824
2825
    return( -1 );
2826
  }
2827
  if( output_data == NULL )
2828
  {
2829
    libcerror_error_set(
2830
     error,
2831
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2832
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2833
     "%s: invalid output data.",
2834
     function );
2835
2836
    return( -1 );
2837
  }
2838
  if( ( output_data_size < input_data_size )
2839
   || ( output_data_size > (size_t) SSIZE_MAX ) )
2840
  {
2841
    libcerror_error_set(
2842
     error,
2843
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2844
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2845
     "%s: invalid output data size value out of bounds.",
2846
     function );
2847
2848
    return( -1 );
2849
  }
2850
  if( mode == LIBCAES_CRYPT_MODE_ENCRYPT )
2851
  {
2852
    safe_mode = AES_ENCRYPT;
2853
  }
2854
  else
2855
  {
2856
    safe_mode = AES_DECRYPT;
2857
  }
2858
  AES_ecb_encrypt(
2859
   (unsigned char *) input_data,
2860
   (unsigned char *) output_data,
2861
   &( internal_context->key ),
2862
   safe_mode );
2863
2864
  return( result );
2865
}
2866
2867
#elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_ECB )
2868
2869
/* De- or encrypts a block of data using AES-ECB (Electronic CodeBook) using OpenSSL EVP
2870
 * The size must be a multitude of the AES block size (16 byte)
2871
 * Returns 1 if successful or -1 on error
2872
 */
2873
int libcaes_crypt_ecb(
2874
     libcaes_context_t *context,
2875
     int mode,
2876
     const uint8_t *input_data,
2877
     size_t input_data_size,
2878
     uint8_t *output_data,
2879
     size_t output_data_size,
2880
     libcerror_error_t **error )
2881
{
2882
  char error_string[ 256 ];
2883
2884
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
2885
  OSSL_PARAM parameters[2];
2886
2887
  EVP_CIPHER *cipher                           = NULL;
2888
  const char *cipher_string                    = NULL;
2889
  unsigned int padding                         = 0;
2890
#else
2891
  const EVP_CIPHER *cipher                     = NULL;
2892
#endif
2893
2894
  libcaes_internal_context_t *internal_context = NULL;
2895
  static char *function                        = "libcaes_crypt_ecb";
2896
  unsigned long error_code                     = 0;
2897
  int safe_output_data_size                    = 0;
2898
  int total_data_size                          = 0;
2899
2900
  if( context == NULL )
2901
  {
2902
    libcerror_error_set(
2903
     error,
2904
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2905
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2906
     "%s: invalid context.",
2907
     function );
2908
2909
    return( -1 );
2910
  }
2911
  internal_context = (libcaes_internal_context_t *) context;
2912
2913
  if( ( internal_context->key_bit_size != 128 )
2914
   && ( internal_context->key_bit_size != 192 )
2915
   && ( internal_context->key_bit_size != 256 ) )
2916
  {
2917
    libcerror_error_set(
2918
     error,
2919
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2920
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2921
     "%s: invalid context - unsupported key bit size.",
2922
     function );
2923
2924
    return( -1 );
2925
  }
2926
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
2927
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
2928
  {
2929
    libcerror_error_set(
2930
     error,
2931
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2932
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2933
     "%s: unsupported mode.",
2934
     function );
2935
2936
    return( -1 );
2937
  }
2938
  if( input_data == NULL )
2939
  {
2940
    libcerror_error_set(
2941
     error,
2942
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2943
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2944
     "%s: invalid input data.",
2945
     function );
2946
2947
    return( -1 );
2948
  }
2949
  if( ( input_data_size < 16 )
2950
   || ( input_data_size > (size_t) INT_MAX ) )
2951
  {
2952
    libcerror_error_set(
2953
     error,
2954
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2955
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2956
     "%s: invalid input data size value out of bounds.",
2957
     function );
2958
2959
    return( -1 );
2960
  }
2961
  if( output_data == NULL )
2962
  {
2963
    libcerror_error_set(
2964
     error,
2965
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2966
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2967
     "%s: invalid output data.",
2968
     function );
2969
2970
    return( -1 );
2971
  }
2972
  if( ( output_data_size < input_data_size )
2973
   || ( output_data_size > (size_t) INT_MAX ) )
2974
  {
2975
    libcerror_error_set(
2976
     error,
2977
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2978
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2979
     "%s: invalid output data size value out of bounds.",
2980
     function );
2981
2982
    return( -1 );
2983
  }
2984
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
2985
  if( EVP_CIPHER_CTX_reset(
2986
       internal_context->evp_cipher_context ) != 1 )
2987
  {
2988
    error_code = ERR_get_error();
2989
2990
    ERR_error_string_n(
2991
     error_code,
2992
     error_string,
2993
     256 );
2994
2995
    libcerror_error_set(
2996
     error,
2997
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2998
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2999
     "%s: unable to reset cipher context with error: %s.",
3000
     function,
3001
     error_string );
3002
3003
    goto on_error;
3004
  }
3005
  if( internal_context->key_bit_size == 128 )
3006
  {
3007
    cipher_string = "AES-128-ECB";
3008
  }
3009
  else if( internal_context->key_bit_size == 192 )
3010
  {
3011
    cipher_string = "AES-192-ECB";
3012
  }
3013
  else if( internal_context->key_bit_size == 256 )
3014
  {
3015
    cipher_string = "AES-256-ECB";
3016
  }
3017
  cipher = EVP_CIPHER_fetch(
3018
            NULL,
3019
            cipher_string,
3020
            NULL );
3021
3022
  if( cipher == NULL )
3023
  {
3024
    libcerror_error_set(
3025
     error,
3026
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3027
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3028
     "%s: missing cipher.",
3029
     function );
3030
3031
    goto on_error;
3032
  }
3033
  parameters[0] = OSSL_PARAM_construct_uint(
3034
                   OSSL_CIPHER_PARAM_PADDING,
3035
                   &padding );
3036
3037
  parameters[1] = OSSL_PARAM_construct_end();
3038
3039
  if( EVP_CipherInit_ex2(
3040
       internal_context->evp_cipher_context,
3041
       cipher,
3042
       (unsigned char *) internal_context->key,
3043
       NULL,
3044
       mode,
3045
       parameters ) != 1 )
3046
  {
3047
    error_code = ERR_get_error();
3048
3049
    ERR_error_string_n(
3050
     error_code,
3051
     error_string,
3052
     256 );
3053
3054
    libcerror_error_set(
3055
     error,
3056
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3057
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
3058
     "%s: unable to initialize cipher with error: %s.",
3059
     function,
3060
     error_string );
3061
3062
    goto on_error;
3063
  }
3064
#else
3065
  if( internal_context->key_bit_size == 128 )
3066
  {
3067
    cipher = EVP_aes_128_ecb();
3068
  }
3069
  else if( internal_context->key_bit_size == 192 )
3070
  {
3071
    cipher = EVP_aes_192_ecb();
3072
  }
3073
  else if( internal_context->key_bit_size == 256 )
3074
  {
3075
    cipher = EVP_aes_256_ecb();
3076
  }
3077
  if( EVP_CipherInit_ex(
3078
       internal_context->evp_cipher_context,
3079
       cipher,
3080
       NULL,
3081
       (unsigned char *) internal_context->key,
3082
       NULL,
3083
       mode ) != 1 )
3084
  {
3085
    error_code = ERR_get_error();
3086
3087
    ERR_error_string_n(
3088
     error_code,
3089
     error_string,
3090
     256 );
3091
3092
    libcerror_error_set(
3093
     error,
3094
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3095
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
3096
     "%s: unable to initialize cipher with error: %s.",
3097
     function,
3098
     error_string );
3099
3100
    goto on_error;
3101
  }
3102
  if( EVP_CIPHER_CTX_set_padding(
3103
       internal_context->evp_cipher_context,
3104
       0 ) != 1 )
3105
  {
3106
    error_code = ERR_get_error();
3107
3108
    ERR_error_string_n(
3109
     error_code,
3110
     error_string,
3111
     256 );
3112
3113
    libcerror_error_set(
3114
     error,
3115
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3116
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3117
     "%s: unable to set padding in context with error: %s.",
3118
     function,
3119
     error_string );
3120
3121
    goto on_error;
3122
  }
3123
#endif /* defined( HAVE_EVP_CIPHERINIT_EX2 ) */
3124
3125
  if( EVP_CipherUpdate(
3126
       internal_context->evp_cipher_context,
3127
       (unsigned char *) output_data,
3128
       &safe_output_data_size,
3129
       (unsigned char *) input_data,
3130
       16 ) != 1 )
3131
  {
3132
    error_code = ERR_get_error();
3133
3134
    ERR_error_string_n(
3135
     error_code,
3136
     error_string,
3137
     256 );
3138
3139
    libcerror_error_set(
3140
     error,
3141
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3142
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3143
     "%s: unable to update cipher with error: %s.",
3144
     function,
3145
     error_string );
3146
3147
    goto on_error;
3148
  }
3149
  /* Get any remaining data, i.e. the final block, and write it
3150
   * to the output data
3151
   */
3152
  total_data_size = 0;
3153
3154
  if( EVP_CipherFinal_ex(
3155
       internal_context->evp_cipher_context,
3156
       (unsigned char *) &( output_data[ (size_t) safe_output_data_size ] ),
3157
       &total_data_size ) != 1 )
3158
  {
3159
    error_code = ERR_get_error();
3160
3161
    ERR_error_string_n(
3162
     error_code,
3163
     error_string,
3164
     256 );
3165
3166
    libcerror_error_set(
3167
     error,
3168
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3169
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
3170
     "%s: unable to finalize cipher with error: %s.",
3171
     function,
3172
     error_string );
3173
3174
    goto on_error;
3175
  }
3176
  safe_output_data_size += total_data_size;
3177
3178
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
3179
  EVP_CIPHER_free(
3180
   cipher );
3181
#endif
3182
  return( 1 );
3183
3184
on_error:
3185
#if defined( HAVE_EVP_CIPHERINIT_EX2 )
3186
  if( cipher != NULL )
3187
  {
3188
    EVP_CIPHER_free(
3189
     cipher );
3190
  }
3191
#endif
3192
  return( -1 );
3193
}
3194
3195
#else
3196
3197
/* De- or encrypts a block of data using AES-ECB (Electronic CodeBook) using fallback implementation
3198
 * The size must be a multitude of the AES block size (16 byte)
3199
 * Returns 1 if successful or -1 on error
3200
 */
3201
int libcaes_crypt_ecb(
3202
     libcaes_context_t *context,
3203
     int mode,
3204
     const uint8_t *input_data,
3205
     size_t input_data_size,
3206
     uint8_t *output_data,
3207
     size_t output_data_size,
3208
     libcerror_error_t **error )
3209
13.3M
{
3210
13.3M
  uint32_t cipher_values_32bit[ 4 ];
3211
13.3M
  uint32_t values_32bit[ 4 ];
3212
3213
13.3M
  libcaes_internal_context_t *internal_context = NULL;
3214
13.3M
  uint32_t *round_keys                         = NULL;
3215
13.3M
  static char *function                        = "libcaes_crypt_ecb";
3216
13.3M
  size_t data_offset                           = 0;
3217
13.3M
  uint32_t substitution_value                  = 0;
3218
13.3M
  uint32_t table_value                         = 0;
3219
13.3M
  int result                                   = 1;
3220
13.3M
  int round_key_iterator                       = 0;
3221
3222
13.3M
  if( context == NULL )
3223
0
  {
3224
0
    libcerror_error_set(
3225
0
     error,
3226
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3227
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3228
0
     "%s: invalid context.",
3229
0
     function );
3230
3231
0
    return( -1 );
3232
0
  }
3233
13.3M
  internal_context = (libcaes_internal_context_t *) context;
3234
3235
13.3M
  if( internal_context->round_keys == NULL )
3236
0
  {
3237
0
    libcerror_error_set(
3238
0
     error,
3239
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3240
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3241
0
     "%s: invalid context - missing round keys.",
3242
0
     function );
3243
3244
0
    return( -1 );
3245
0
  }
3246
13.3M
  if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT )
3247
294k
   && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) )
3248
0
  {
3249
0
    libcerror_error_set(
3250
0
     error,
3251
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3252
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3253
0
     "%s: unsupported mode.",
3254
0
     function );
3255
3256
0
    return( -1 );
3257
0
  }
3258
13.3M
  if( input_data == NULL )
3259
0
  {
3260
0
    libcerror_error_set(
3261
0
     error,
3262
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3263
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3264
0
     "%s: invalid input data.",
3265
0
     function );
3266
3267
0
    return( -1 );
3268
0
  }
3269
  /* Check if the input data size is a multitude of 16-byte
3270
   */
3271
13.3M
  if( ( ( input_data_size & (size_t) 0x0f ) != 0 )
3272
13.3M
   || ( input_data_size < 16 )
3273
13.3M
   || ( input_data_size > (size_t) SSIZE_MAX ) )
3274
5
  {
3275
5
    libcerror_error_set(
3276
5
     error,
3277
5
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3278
5
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
3279
5
     "%s: invalid input data size value out of bounds.",
3280
5
     function );
3281
3282
5
    return( -1 );
3283
5
  }
3284
13.3M
  if( output_data == NULL )
3285
0
  {
3286
0
    libcerror_error_set(
3287
0
     error,
3288
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3289
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3290
0
     "%s: invalid output data.",
3291
0
     function );
3292
3293
0
    return( -1 );
3294
0
  }
3295
13.3M
  if( ( output_data_size < input_data_size )
3296
13.3M
   || ( output_data_size > (size_t) SSIZE_MAX ) )
3297
45
  {
3298
45
    libcerror_error_set(
3299
45
     error,
3300
45
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3301
45
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
3302
45
     "%s: invalid output data size value out of bounds.",
3303
45
     function );
3304
3305
45
    return( -1 );
3306
45
  }
3307
26.6M
  while( data_offset < input_data_size )
3308
13.3M
  {
3309
13.3M
    byte_stream_copy_to_uint32_little_endian(
3310
13.3M
     &( input_data[ data_offset ] ),
3311
13.3M
     values_32bit[ 0 ] );
3312
3313
13.3M
    byte_stream_copy_to_uint32_little_endian(
3314
13.3M
     &( input_data[ data_offset + 4 ] ),
3315
13.3M
     values_32bit[ 1 ] );
3316
3317
13.3M
    byte_stream_copy_to_uint32_little_endian(
3318
13.3M
     &( input_data[ data_offset + 8 ] ),
3319
13.3M
     values_32bit[ 2 ] );
3320
3321
13.3M
    byte_stream_copy_to_uint32_little_endian(
3322
13.3M
     &( input_data[ data_offset + 12 ] ),
3323
13.3M
     values_32bit[ 3 ] );
3324
3325
13.3M
    round_keys = internal_context->round_keys;
3326
3327
13.3M
    values_32bit[ 0 ] ^= round_keys[ 0 ];
3328
13.3M
    values_32bit[ 1 ] ^= round_keys[ 1 ];
3329
13.3M
    values_32bit[ 2 ] ^= round_keys[ 2 ];
3330
13.3M
    values_32bit[ 3 ] ^= round_keys[ 3 ];
3331
3332
13.3M
    round_keys += 4;
3333
3334
13.3M
    if( mode == LIBCAES_CRYPT_MODE_ENCRYPT )
3335
294k
    {
3336
294k
      for( round_key_iterator = ( internal_context->number_of_round_keys / 2 );
3337
1.51M
           round_key_iterator > 1;
3338
1.21M
           round_key_iterator-- )
3339
1.21M
      {
3340
1.21M
        libcaes_calculate_forward_table_round(
3341
1.21M
         round_keys,
3342
1.21M
         cipher_values_32bit,
3343
1.21M
         values_32bit,
3344
1.21M
         table_value );
3345
3346
1.21M
        round_keys += 4;
3347
3348
1.21M
        libcaes_calculate_forward_table_round(
3349
1.21M
         round_keys,
3350
1.21M
         values_32bit,
3351
1.21M
         cipher_values_32bit,
3352
1.21M
         table_value );
3353
3354
1.21M
        round_keys += 4;
3355
1.21M
      }
3356
294k
      libcaes_calculate_forward_table_round(
3357
294k
       round_keys,
3358
294k
       cipher_values_32bit,
3359
294k
       values_32bit,
3360
294k
       table_value );
3361
3362
294k
      round_keys += 4;
3363
3364
294k
      libcaes_calculate_forward_substitution_round(
3365
294k
       round_keys,
3366
294k
       values_32bit,
3367
294k
       cipher_values_32bit,
3368
294k
       substitution_value );
3369
294k
    }
3370
13.0M
    else
3371
13.0M
    {
3372
13.0M
      for( round_key_iterator = ( internal_context->number_of_round_keys / 2 );
3373
65.3M
           round_key_iterator > 1;
3374
52.3M
           round_key_iterator-- )
3375
52.3M
      {
3376
52.3M
        libcaes_calculate_reverse_table_round(
3377
52.3M
         round_keys,
3378
52.3M
         cipher_values_32bit,
3379
52.3M
         values_32bit,
3380
52.3M
         table_value );
3381
3382
52.3M
        round_keys += 4;
3383
3384
52.3M
        libcaes_calculate_reverse_table_round(
3385
52.3M
         round_keys,
3386
52.3M
         values_32bit,
3387
52.3M
         cipher_values_32bit,
3388
52.3M
         table_value );
3389
3390
52.3M
        round_keys += 4;
3391
52.3M
      }
3392
13.0M
      libcaes_calculate_reverse_table_round(
3393
13.0M
       round_keys,
3394
13.0M
       cipher_values_32bit,
3395
13.0M
       values_32bit,
3396
13.0M
       table_value );
3397
3398
13.0M
      round_keys += 4;
3399
3400
13.0M
      libcaes_calculate_reverse_substitution_round(
3401
13.0M
       round_keys,
3402
13.0M
       values_32bit,
3403
13.0M
       cipher_values_32bit,
3404
13.0M
       table_value );
3405
13.0M
    }
3406
13.3M
    byte_stream_copy_from_uint32_little_endian(
3407
13.3M
     &( output_data[ data_offset ] ),
3408
13.3M
     values_32bit[ 0 ] );
3409
3410
13.3M
    byte_stream_copy_from_uint32_little_endian(
3411
13.3M
     &( output_data[ data_offset + 4 ] ),
3412
13.3M
     values_32bit[ 1 ] );
3413
3414
13.3M
    byte_stream_copy_from_uint32_little_endian(
3415
13.3M
     &( output_data[ data_offset + 8 ] ),
3416
13.3M
     values_32bit[ 2 ] );
3417
3418
13.3M
    byte_stream_copy_from_uint32_little_endian(
3419
13.3M
     &( output_data[ data_offset + 12 ] ),
3420
13.3M
     values_32bit[ 3 ] );
3421
3422
13.3M
    if( memory_set(
3423
13.3M
         values_32bit,
3424
13.3M
         0,
3425
13.3M
         sizeof( uint32_t ) * 4 ) == NULL )
3426
0
    {
3427
0
      libcerror_error_set(
3428
0
       error,
3429
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
3430
0
       LIBCERROR_MEMORY_ERROR_SET_FAILED,
3431
0
       "%s: unable to clear values 32-bit.",
3432
0
       function );
3433
3434
0
      result = -1;
3435
0
    }
3436
13.3M
    if( memory_set(
3437
13.3M
         cipher_values_32bit,
3438
13.3M
         0,
3439
13.3M
         sizeof( uint32_t ) * 4 ) == NULL )
3440
0
    {
3441
0
      libcerror_error_set(
3442
0
       error,
3443
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
3444
0
       LIBCERROR_MEMORY_ERROR_SET_FAILED,
3445
0
       "%s: unable to clear cipher values 32-bit.",
3446
0
       function );
3447
3448
0
      result = -1;
3449
0
    }
3450
13.3M
    data_offset += 16;
3451
13.3M
  }
3452
13.3M
  return( result );
3453
13.3M
}
3454
3455
#endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_AES_H ) && defined( HAVE_AES_ECB_ENCRYPT ) */
3456