/src/libbde/libcaes/libcaes_tweaked_context.c
Line | Count | Source |
1 | | /* |
2 | | * AES encryption 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_EVP_H ) |
28 | | #include <openssl/err.h> |
29 | | #include <openssl/evp.h> |
30 | | #endif |
31 | | |
32 | | #if defined( HAVE_EVP_CIPHERINIT_EX2 ) |
33 | | #include <openssl/core_names.h> |
34 | | #endif |
35 | | |
36 | | #include "libcaes_context.h" |
37 | | #include "libcaes_definitions.h" |
38 | | #include "libcaes_libcerror.h" |
39 | | #include "libcaes_tweaked_context.h" |
40 | | #include "libcaes_types.h" |
41 | | |
42 | | /* Creates a tweaked context |
43 | | * Make sure the value context is referencing, is set to NULL |
44 | | * Returns 1 if successful or -1 on error |
45 | | */ |
46 | | int libcaes_tweaked_context_initialize( |
47 | | libcaes_tweaked_context_t **tweaked_context, |
48 | | libcerror_error_t **error ) |
49 | 2.81k | { |
50 | 2.81k | libcaes_internal_tweaked_context_t *internal_tweaked_context = NULL; |
51 | 2.81k | static char *function = "libcaes_tweaked_context_initialize"; |
52 | | |
53 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) |
54 | | char error_string[ 256 ]; |
55 | | |
56 | | unsigned long error_code = 0; |
57 | | #endif |
58 | | |
59 | 2.81k | if( tweaked_context == NULL ) |
60 | 0 | { |
61 | 0 | libcerror_error_set( |
62 | 0 | error, |
63 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
64 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
65 | 0 | "%s: invalid tweaked context.", |
66 | 0 | function ); |
67 | |
|
68 | 0 | return( -1 ); |
69 | 0 | } |
70 | 2.81k | if( *tweaked_context != NULL ) |
71 | 0 | { |
72 | 0 | libcerror_error_set( |
73 | 0 | error, |
74 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
75 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
76 | 0 | "%s: invalid tweaked context value already set.", |
77 | 0 | function ); |
78 | |
|
79 | 0 | return( -1 ); |
80 | 0 | } |
81 | 2.81k | internal_tweaked_context = memory_allocate_structure( |
82 | 2.81k | libcaes_internal_tweaked_context_t ); |
83 | | |
84 | 2.81k | if( internal_tweaked_context == NULL ) |
85 | 0 | { |
86 | 0 | libcerror_error_set( |
87 | 0 | error, |
88 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
89 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
90 | 0 | "%s: unable to create tweaked context.", |
91 | 0 | function ); |
92 | |
|
93 | 0 | goto on_error; |
94 | 0 | } |
95 | 2.81k | if( memory_set( |
96 | 2.81k | internal_tweaked_context, |
97 | 2.81k | 0, |
98 | 2.81k | sizeof( libcaes_internal_tweaked_context_t ) ) == NULL ) |
99 | 0 | { |
100 | 0 | libcerror_error_set( |
101 | 0 | error, |
102 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
103 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
104 | 0 | "%s: unable to clear tweaked context.", |
105 | 0 | function ); |
106 | |
|
107 | 0 | memory_free( |
108 | 0 | internal_tweaked_context ); |
109 | |
|
110 | 0 | return( -1 ); |
111 | 0 | } |
112 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) |
113 | | #if defined( HAVE_EVP_CIPHER_CTX_INIT ) |
114 | | EVP_CIPHER_CTX_init( |
115 | | &( internal_tweaked_context->internal_evp_cipher_context ) ); |
116 | | |
117 | | internal_tweaked_context->evp_cipher_context = &( internal_tweaked_context->internal_evp_cipher_context ); |
118 | | #else |
119 | | internal_tweaked_context->evp_cipher_context = EVP_CIPHER_CTX_new(); |
120 | | |
121 | | if( internal_tweaked_context->evp_cipher_context == NULL ) |
122 | | { |
123 | | error_code = ERR_get_error(); |
124 | | |
125 | | ERR_error_string_n( |
126 | | error_code, |
127 | | error_string, |
128 | | 256 ); |
129 | | |
130 | | libcerror_error_set( |
131 | | error, |
132 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
133 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
134 | | "%s: unable to create EVP cipher context with error: %s.", |
135 | | function, |
136 | | error_string ); |
137 | | |
138 | | goto on_error; |
139 | | } |
140 | | #endif /* defined( HAVE_EVP_CIPHER_CTX_INIT ) */ |
141 | | #else |
142 | 2.81k | if( libcaes_context_initialize( |
143 | 2.81k | &( internal_tweaked_context->main_context ), |
144 | 2.81k | error ) != 1) |
145 | 0 | { |
146 | 0 | libcerror_error_set( |
147 | 0 | error, |
148 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
149 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
150 | 0 | "%s: unable to initialize main context.", |
151 | 0 | function ); |
152 | |
|
153 | 0 | goto on_error; |
154 | 0 | } |
155 | 2.81k | if( libcaes_context_initialize( |
156 | 2.81k | &( internal_tweaked_context->tweak_context ), |
157 | 2.81k | error ) != 1) |
158 | 0 | { |
159 | 0 | libcerror_error_set( |
160 | 0 | error, |
161 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
162 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
163 | 0 | "%s: unable to initialize tweak context.", |
164 | 0 | function ); |
165 | |
|
166 | 0 | goto on_error; |
167 | 0 | } |
168 | 2.81k | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) */ |
169 | | |
170 | 2.81k | *tweaked_context = (libcaes_tweaked_context_t *) internal_tweaked_context; |
171 | | |
172 | 2.81k | return( 1 ); |
173 | | |
174 | 0 | on_error: |
175 | 0 | if( internal_tweaked_context != NULL ) |
176 | 0 | { |
177 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) |
178 | | /* No additional clean up necessary */ |
179 | | |
180 | | #else |
181 | 0 | if( internal_tweaked_context->main_context != NULL ) |
182 | 0 | { |
183 | 0 | libcaes_context_free( |
184 | 0 | &( internal_tweaked_context->main_context ), |
185 | 0 | NULL ); |
186 | 0 | } |
187 | 0 | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) */ |
188 | |
|
189 | 0 | memory_free( |
190 | 0 | internal_tweaked_context ); |
191 | 0 | } |
192 | 0 | return( -1 ); |
193 | 2.81k | } |
194 | | |
195 | | /* Frees a tweaked context |
196 | | * Returns 1 if successful or -1 on error |
197 | | */ |
198 | | int libcaes_tweaked_context_free( |
199 | | libcaes_tweaked_context_t **tweaked_context, |
200 | | libcerror_error_t **error ) |
201 | 2.81k | { |
202 | 2.81k | libcaes_internal_tweaked_context_t *internal_tweaked_context = NULL; |
203 | 2.81k | static char *function = "libcaes_tweaked_context_free"; |
204 | 2.81k | int result = 1; |
205 | | |
206 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_EVP_CIPHER_CTX_CLEANUP ) |
207 | | char error_string[ 256 ]; |
208 | | |
209 | | unsigned long error_code = 0; |
210 | | #endif |
211 | | |
212 | 2.81k | if( tweaked_context == NULL ) |
213 | 0 | { |
214 | 0 | libcerror_error_set( |
215 | 0 | error, |
216 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
217 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
218 | 0 | "%s: invalid tweaked context.", |
219 | 0 | function ); |
220 | |
|
221 | 0 | return( -1 ); |
222 | 0 | } |
223 | 2.81k | if( *tweaked_context != NULL ) |
224 | 2.81k | { |
225 | 2.81k | internal_tweaked_context = (libcaes_internal_tweaked_context_t *) *tweaked_context; |
226 | 2.81k | *tweaked_context = NULL; |
227 | | |
228 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) |
229 | | #if defined( HAVE_EVP_CIPHER_CTX_CLEANUP ) |
230 | | if( EVP_CIPHER_CTX_cleanup( |
231 | | &( internal_tweaked_context->internal_evp_cipher_context ) ) != 1 ) |
232 | | { |
233 | | error_code = ERR_get_error(); |
234 | | |
235 | | ERR_error_string_n( |
236 | | error_code, |
237 | | error_string, |
238 | | 256 ); |
239 | | |
240 | | libcerror_error_set( |
241 | | error, |
242 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
243 | | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
244 | | "%s: unable to clean up EVP cipher context with error: %s.", |
245 | | function, |
246 | | error_string ); |
247 | | |
248 | | result = -1; |
249 | | } |
250 | | /* Make sure the error state is removed otherwise OpenSSL will leak memory |
251 | | */ |
252 | | ERR_clear_error(); |
253 | | #else |
254 | | EVP_CIPHER_CTX_free( |
255 | | internal_tweaked_context->evp_cipher_context ); |
256 | | |
257 | | #endif /* defined( HAVE_EVP_CIPHER_CTX_CLEANUP ) */ |
258 | | |
259 | | internal_tweaked_context->evp_cipher_context = NULL; |
260 | | #else |
261 | 2.81k | if( libcaes_context_free( |
262 | 2.81k | &( internal_tweaked_context->tweak_context ), |
263 | 2.81k | error ) != 1 ) |
264 | 0 | { |
265 | 0 | libcerror_error_set( |
266 | 0 | error, |
267 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
268 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
269 | 0 | "%s: unable to free tweak context.", |
270 | 0 | function ); |
271 | |
|
272 | 0 | result = -1; |
273 | 0 | } |
274 | 2.81k | if( libcaes_context_free( |
275 | 2.81k | &( internal_tweaked_context->main_context ), |
276 | 2.81k | error ) != 1 ) |
277 | 0 | { |
278 | 0 | libcerror_error_set( |
279 | 0 | error, |
280 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
281 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
282 | 0 | "%s: unable to free main context.", |
283 | 0 | function ); |
284 | |
|
285 | 0 | result = -1; |
286 | 0 | } |
287 | 2.81k | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) */ |
288 | | |
289 | 2.81k | memory_free( |
290 | 2.81k | internal_tweaked_context ); |
291 | 2.81k | } |
292 | 2.81k | return( result ); |
293 | 2.81k | } |
294 | | |
295 | | /* Sets the keys |
296 | | * Returns 1 if successful or -1 on error |
297 | | */ |
298 | | int libcaes_tweaked_context_set_keys( |
299 | | libcaes_tweaked_context_t *tweaked_context, |
300 | | int mode, |
301 | | const uint8_t *key, |
302 | | size_t key_bit_size, |
303 | | const uint8_t *tweak_key, |
304 | | size_t tweak_key_bit_size, |
305 | | libcerror_error_t **error ) |
306 | 2.81k | { |
307 | 2.81k | libcaes_internal_tweaked_context_t *internal_tweaked_context = NULL; |
308 | 2.81k | static char *function = "libcaes_tweaked_context_set_key"; |
309 | | |
310 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) |
311 | | size_t key_byte_size = 0; |
312 | | #endif |
313 | | |
314 | 2.81k | if( tweaked_context == NULL ) |
315 | 0 | { |
316 | 0 | libcerror_error_set( |
317 | 0 | error, |
318 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
319 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
320 | 0 | "%s: invalid tweaked context.", |
321 | 0 | function ); |
322 | |
|
323 | 0 | return( -1 ); |
324 | 0 | } |
325 | 2.81k | internal_tweaked_context = (libcaes_internal_tweaked_context_t *) tweaked_context; |
326 | | |
327 | 2.81k | if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT ) |
328 | 184 | && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) ) |
329 | 0 | { |
330 | 0 | libcerror_error_set( |
331 | 0 | error, |
332 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
333 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
334 | 0 | "%s: unsupported mode.", |
335 | 0 | function ); |
336 | |
|
337 | 0 | return( -1 ); |
338 | 0 | } |
339 | 2.81k | if( ( key_bit_size != 128 ) |
340 | 134 | && ( key_bit_size != 256 ) ) |
341 | 0 | { |
342 | 0 | libcerror_error_set( |
343 | 0 | error, |
344 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
345 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
346 | 0 | "%s: unsupported key bit size.", |
347 | 0 | function ); |
348 | |
|
349 | 0 | return( -1 ); |
350 | 0 | } |
351 | 2.81k | if( tweak_key_bit_size != key_bit_size ) |
352 | 0 | { |
353 | 0 | libcerror_error_set( |
354 | 0 | error, |
355 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
356 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
357 | 0 | "%s: unsupported tweak key bit size.", |
358 | 0 | function ); |
359 | |
|
360 | 0 | return( -1 ); |
361 | 0 | } |
362 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) |
363 | | if( key == NULL ) |
364 | | { |
365 | | libcerror_error_set( |
366 | | error, |
367 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
368 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
369 | | "%s: invalid key.", |
370 | | function ); |
371 | | |
372 | | return( -1 ); |
373 | | } |
374 | | if( tweak_key == NULL ) |
375 | | { |
376 | | libcerror_error_set( |
377 | | error, |
378 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
379 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
380 | | "%s: invalid tweak key.", |
381 | | function ); |
382 | | |
383 | | return( -1 ); |
384 | | } |
385 | | key_byte_size = key_bit_size / 8; |
386 | | |
387 | | if( memory_copy( |
388 | | internal_tweaked_context->key, |
389 | | key, |
390 | | key_byte_size ) == NULL ) |
391 | | { |
392 | | libcerror_error_set( |
393 | | error, |
394 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
395 | | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
396 | | "%s: unable to copy key.", |
397 | | function ); |
398 | | |
399 | | return( -1 ); |
400 | | } |
401 | | if( memory_copy( |
402 | | &( internal_tweaked_context->key[ key_byte_size ] ), |
403 | | tweak_key, |
404 | | key_byte_size ) == NULL ) |
405 | | { |
406 | | libcerror_error_set( |
407 | | error, |
408 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
409 | | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
410 | | "%s: unable to copy tweak key.", |
411 | | function ); |
412 | | |
413 | | return( -1 ); |
414 | | } |
415 | | internal_tweaked_context->key_bit_size = key_bit_size; |
416 | | |
417 | | #else |
418 | 2.81k | if( libcaes_context_set_key( |
419 | 2.81k | internal_tweaked_context->main_context, |
420 | 2.81k | mode, |
421 | 2.81k | key, |
422 | 2.81k | key_bit_size, |
423 | 2.81k | error ) != 1 ) |
424 | 0 | { |
425 | 0 | libcerror_error_set( |
426 | 0 | error, |
427 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
428 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
429 | 0 | "%s: unable to set key in main context.", |
430 | 0 | function ); |
431 | |
|
432 | 0 | return( -1 ); |
433 | 0 | } |
434 | 2.81k | if( libcaes_context_set_key( |
435 | 2.81k | internal_tweaked_context->tweak_context, |
436 | 2.81k | LIBCAES_CRYPT_MODE_ENCRYPT, |
437 | 2.81k | tweak_key, |
438 | 2.81k | tweak_key_bit_size, |
439 | 2.81k | error ) != 1 ) |
440 | 0 | { |
441 | 0 | libcerror_error_set( |
442 | 0 | error, |
443 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
444 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
445 | 0 | "%s: unable to set tweak key in tweak context.", |
446 | 0 | function ); |
447 | |
|
448 | 0 | return( -1 ); |
449 | 0 | } |
450 | 2.81k | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) */ |
451 | | |
452 | 2.81k | return( 1 ); |
453 | 2.81k | } |
454 | | |
455 | | #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) |
456 | | |
457 | | /* De- or encrypts a block of data using AES-XTS (XEX-based tweaked-codebook mode with ciphertext stealing) using OpenSSL EVP |
458 | | * The size must be a multitude of the AES block size (16 byte) |
459 | | * Returns 1 if successful or -1 on error |
460 | | */ |
461 | | int libcaes_crypt_xts( |
462 | | libcaes_tweaked_context_t *tweaked_context, |
463 | | int mode, |
464 | | const uint8_t *tweak_value, |
465 | | size_t tweak_value_size, |
466 | | const uint8_t *input_data, |
467 | | size_t input_data_size, |
468 | | uint8_t *output_data, |
469 | | size_t output_data_size, |
470 | | libcerror_error_t **error ) |
471 | | { |
472 | | uint8_t block_data[ EVP_MAX_BLOCK_LENGTH ]; |
473 | | char error_string[ 256 ]; |
474 | | |
475 | | #if defined( HAVE_EVP_CIPHERINIT_EX2 ) |
476 | | OSSL_PARAM parameters[2]; |
477 | | |
478 | | EVP_CIPHER *cipher = NULL; |
479 | | const char *cipher_string = NULL; |
480 | | unsigned int padding = 0; |
481 | | #else |
482 | | const EVP_CIPHER *cipher = NULL; |
483 | | #endif |
484 | | |
485 | | libcaes_internal_tweaked_context_t *internal_tweaked_context = NULL; |
486 | | static char *function = "libcaes_crypt_xts"; |
487 | | unsigned long error_code = 0; |
488 | | int safe_output_data_size = 0; |
489 | | |
490 | | if( tweaked_context == NULL ) |
491 | | { |
492 | | libcerror_error_set( |
493 | | error, |
494 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
495 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
496 | | "%s: invalid tweaked context.", |
497 | | function ); |
498 | | |
499 | | return( -1 ); |
500 | | } |
501 | | internal_tweaked_context = (libcaes_internal_tweaked_context_t *) tweaked_context; |
502 | | |
503 | | if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT ) |
504 | | && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) ) |
505 | | { |
506 | | libcerror_error_set( |
507 | | error, |
508 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
509 | | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
510 | | "%s: unsupported mode.", |
511 | | function ); |
512 | | |
513 | | return( -1 ); |
514 | | } |
515 | | if( tweak_value == NULL ) |
516 | | { |
517 | | libcerror_error_set( |
518 | | error, |
519 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
520 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
521 | | "%s: invalid tweak value.", |
522 | | function ); |
523 | | |
524 | | return( -1 ); |
525 | | } |
526 | | if( tweak_value_size != 16 ) |
527 | | { |
528 | | libcerror_error_set( |
529 | | error, |
530 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
531 | | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
532 | | "%s: invalid tweak value size value out of bounds.", |
533 | | function ); |
534 | | |
535 | | return( -1 ); |
536 | | } |
537 | | if( input_data == NULL ) |
538 | | { |
539 | | libcerror_error_set( |
540 | | error, |
541 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
542 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
543 | | "%s: invalid input data.", |
544 | | function ); |
545 | | |
546 | | return( -1 ); |
547 | | } |
548 | | if( input_data_size > (size_t) INT_MAX ) |
549 | | { |
550 | | libcerror_error_set( |
551 | | error, |
552 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
553 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
554 | | "%s: invalid input data size value exceeds maximum.", |
555 | | function ); |
556 | | |
557 | | return( -1 ); |
558 | | } |
559 | | if( input_data_size < 16 ) |
560 | | { |
561 | | libcerror_error_set( |
562 | | error, |
563 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
564 | | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
565 | | "%s: invalid input data size value too small.", |
566 | | function ); |
567 | | |
568 | | return( -1 ); |
569 | | } |
570 | | if( output_data == NULL ) |
571 | | { |
572 | | libcerror_error_set( |
573 | | error, |
574 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
575 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
576 | | "%s: invalid output data.", |
577 | | function ); |
578 | | |
579 | | return( -1 ); |
580 | | } |
581 | | if( output_data_size > (size_t) INT_MAX ) |
582 | | { |
583 | | libcerror_error_set( |
584 | | error, |
585 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
586 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
587 | | "%s: invalid output data size value exceeds maximum.", |
588 | | function ); |
589 | | |
590 | | return( -1 ); |
591 | | } |
592 | | if( output_data_size < input_data_size ) |
593 | | { |
594 | | libcerror_error_set( |
595 | | error, |
596 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
597 | | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
598 | | "%s: invalid ouput data size smaller than input data size.", |
599 | | function ); |
600 | | |
601 | | return( -1 ); |
602 | | } |
603 | | if( memory_set( |
604 | | block_data, |
605 | | 0, |
606 | | EVP_MAX_BLOCK_LENGTH ) == NULL ) |
607 | | { |
608 | | libcerror_error_set( |
609 | | error, |
610 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
611 | | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
612 | | "%s: unable to clear input block data.", |
613 | | function ); |
614 | | |
615 | | goto on_error; |
616 | | } |
617 | | #if defined( HAVE_EVP_CIPHERINIT_EX2 ) |
618 | | if( EVP_CIPHER_CTX_reset( |
619 | | internal_tweaked_context->evp_cipher_context ) != 1 ) |
620 | | { |
621 | | error_code = ERR_get_error(); |
622 | | |
623 | | ERR_error_string_n( |
624 | | error_code, |
625 | | error_string, |
626 | | 256 ); |
627 | | |
628 | | libcerror_error_set( |
629 | | error, |
630 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
631 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
632 | | "%s: unable to reset cipher context with error: %s.", |
633 | | function, |
634 | | error_string ); |
635 | | |
636 | | goto on_error; |
637 | | } |
638 | | if( internal_tweaked_context->key_bit_size == 128 ) |
639 | | { |
640 | | cipher_string = "AES-128-XTS"; |
641 | | } |
642 | | else if( internal_tweaked_context->key_bit_size == 256 ) |
643 | | { |
644 | | cipher_string = "AES-256-XTS"; |
645 | | } |
646 | | cipher = EVP_CIPHER_fetch( |
647 | | NULL, |
648 | | cipher_string, |
649 | | NULL ); |
650 | | |
651 | | if( cipher == NULL ) |
652 | | { |
653 | | libcerror_error_set( |
654 | | error, |
655 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
656 | | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
657 | | "%s: missing cipher.", |
658 | | function ); |
659 | | |
660 | | goto on_error; |
661 | | } |
662 | | parameters[0] = OSSL_PARAM_construct_uint( |
663 | | OSSL_CIPHER_PARAM_PADDING, |
664 | | &padding ); |
665 | | |
666 | | parameters[1] = OSSL_PARAM_construct_end(); |
667 | | |
668 | | if( EVP_CipherInit_ex2( |
669 | | internal_tweaked_context->evp_cipher_context, |
670 | | cipher, |
671 | | (unsigned char *) internal_tweaked_context->key, |
672 | | (unsigned char *) tweak_value, |
673 | | mode, |
674 | | parameters ) != 1 ) |
675 | | { |
676 | | error_code = ERR_get_error(); |
677 | | |
678 | | ERR_error_string_n( |
679 | | error_code, |
680 | | error_string, |
681 | | 256 ); |
682 | | |
683 | | libcerror_error_set( |
684 | | error, |
685 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
686 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
687 | | "%s: unable to initialize cipher with error: %s.", |
688 | | function, |
689 | | error_string ); |
690 | | |
691 | | goto on_error; |
692 | | } |
693 | | #else |
694 | | if( internal_tweaked_context->key_bit_size == 128 ) |
695 | | { |
696 | | cipher = EVP_aes_128_xts(); |
697 | | } |
698 | | else if( internal_tweaked_context->key_bit_size == 256 ) |
699 | | { |
700 | | cipher = EVP_aes_256_xts(); |
701 | | } |
702 | | if( EVP_CipherInit_ex( |
703 | | internal_tweaked_context->evp_cipher_context, |
704 | | cipher, |
705 | | NULL, |
706 | | (unsigned char *) internal_tweaked_context->key, |
707 | | (unsigned char *) tweak_value, |
708 | | mode ) != 1 ) |
709 | | { |
710 | | error_code = ERR_get_error(); |
711 | | |
712 | | ERR_error_string_n( |
713 | | error_code, |
714 | | error_string, |
715 | | 256 ); |
716 | | |
717 | | libcerror_error_set( |
718 | | error, |
719 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
720 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
721 | | "%s: unable to initialize cipher with error: %s.", |
722 | | function, |
723 | | error_string ); |
724 | | |
725 | | goto on_error; |
726 | | } |
727 | | if( EVP_CIPHER_CTX_set_padding( |
728 | | internal_tweaked_context->evp_cipher_context, |
729 | | 1 ) != 1 ) |
730 | | { |
731 | | error_code = ERR_get_error(); |
732 | | |
733 | | ERR_error_string_n( |
734 | | error_code, |
735 | | error_string, |
736 | | 256 ); |
737 | | |
738 | | libcerror_error_set( |
739 | | error, |
740 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
741 | | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
742 | | "%s: unable to set padding in context with error: %s.", |
743 | | function, |
744 | | error_string ); |
745 | | |
746 | | goto on_error; |
747 | | } |
748 | | #endif /* defined( HAVE_EVP_CIPHERINIT_EX2 ) */ |
749 | | |
750 | | if( EVP_CipherUpdate( |
751 | | internal_tweaked_context->evp_cipher_context, |
752 | | (unsigned char *) output_data, |
753 | | &safe_output_data_size, |
754 | | (unsigned char *) input_data, |
755 | | input_data_size ) != 1 ) |
756 | | { |
757 | | error_code = ERR_get_error(); |
758 | | |
759 | | ERR_error_string_n( |
760 | | error_code, |
761 | | error_string, |
762 | | 256 ); |
763 | | |
764 | | libcerror_error_set( |
765 | | error, |
766 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
767 | | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
768 | | "%s: unable to update cipher with error: %s.", |
769 | | function, |
770 | | error_string ); |
771 | | |
772 | | goto on_error; |
773 | | } |
774 | | /* Just ignore the output of this function |
775 | | */ |
776 | | EVP_CipherFinal_ex( |
777 | | internal_tweaked_context->evp_cipher_context, |
778 | | (unsigned char *) block_data, |
779 | | &safe_output_data_size ); |
780 | | |
781 | | #if defined( HAVE_EVP_CIPHERINIT_EX2 ) |
782 | | EVP_CIPHER_free( |
783 | | cipher ); |
784 | | #endif |
785 | | return( 1 ); |
786 | | |
787 | | on_error: |
788 | | #if defined( HAVE_EVP_CIPHERINIT_EX2 ) |
789 | | if( cipher != NULL ) |
790 | | { |
791 | | EVP_CIPHER_free( |
792 | | cipher ); |
793 | | } |
794 | | #endif |
795 | | return( -1 ); |
796 | | } |
797 | | |
798 | | #else |
799 | | |
800 | | /* De- or encrypts a block of data using AES-XTS (XEX-based tweaked-codebook mode with ciphertext stealing) using fallback implementation |
801 | | * The size must be a multitude of the AES block size (16 byte) |
802 | | * Returns 1 if successful or -1 on error |
803 | | */ |
804 | | int libcaes_crypt_xts( |
805 | | libcaes_tweaked_context_t *tweaked_context, |
806 | | int mode, |
807 | | const uint8_t *tweak_value, |
808 | | size_t tweak_value_size, |
809 | | const uint8_t *input_data, |
810 | | size_t input_data_size, |
811 | | uint8_t *output_data, |
812 | | size_t output_data_size, |
813 | | libcerror_error_t **error ) |
814 | 272k | { |
815 | 272k | uint8_t encrypted_tweak_value[ 16 ]; |
816 | 272k | uint8_t encrypted_tweak_value_copy[ 16 ]; |
817 | | |
818 | 272k | libcaes_internal_tweaked_context_t *internal_tweaked_context = NULL; |
819 | 272k | static char *function = "libcaes_crypt_xts"; |
820 | 272k | size_t data_offset = 0; |
821 | 272k | size_t remaining_data_size = 0; |
822 | 272k | uint8_t block_index = 0; |
823 | 272k | uint8_t byte_value = 0; |
824 | 272k | uint8_t carry_bit = 0; |
825 | | |
826 | 272k | if( tweaked_context == NULL ) |
827 | 0 | { |
828 | 0 | libcerror_error_set( |
829 | 0 | error, |
830 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
831 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
832 | 0 | "%s: invalid tweaked context.", |
833 | 0 | function ); |
834 | |
|
835 | 0 | return( -1 ); |
836 | 0 | } |
837 | 272k | internal_tweaked_context = (libcaes_internal_tweaked_context_t *) tweaked_context; |
838 | | |
839 | 272k | if( ( mode != LIBCAES_CRYPT_MODE_DECRYPT ) |
840 | 0 | && ( mode != LIBCAES_CRYPT_MODE_ENCRYPT ) ) |
841 | 0 | { |
842 | 0 | libcerror_error_set( |
843 | 0 | error, |
844 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
845 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
846 | 0 | "%s: unsupported mode.", |
847 | 0 | function ); |
848 | |
|
849 | 0 | return( -1 ); |
850 | 0 | } |
851 | 272k | if( tweak_value == NULL ) |
852 | 0 | { |
853 | 0 | libcerror_error_set( |
854 | 0 | error, |
855 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
856 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
857 | 0 | "%s: invalid tweak value.", |
858 | 0 | function ); |
859 | |
|
860 | 0 | return( -1 ); |
861 | 0 | } |
862 | 272k | if( tweak_value_size != 16 ) |
863 | 0 | { |
864 | 0 | libcerror_error_set( |
865 | 0 | error, |
866 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
867 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
868 | 0 | "%s: invalid tweak value size value out of bounds.", |
869 | 0 | function ); |
870 | |
|
871 | 0 | return( -1 ); |
872 | 0 | } |
873 | 272k | if( input_data == NULL ) |
874 | 0 | { |
875 | 0 | libcerror_error_set( |
876 | 0 | error, |
877 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
878 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
879 | 0 | "%s: invalid input data.", |
880 | 0 | function ); |
881 | |
|
882 | 0 | return( -1 ); |
883 | 0 | } |
884 | 272k | if( input_data_size < 16 ) |
885 | 415 | { |
886 | 415 | libcerror_error_set( |
887 | 415 | error, |
888 | 415 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
889 | 415 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
890 | 415 | "%s: invalid input data size value too small.", |
891 | 415 | function ); |
892 | | |
893 | 415 | return( -1 ); |
894 | 415 | } |
895 | 271k | if( input_data_size > (size_t) SSIZE_MAX ) |
896 | 0 | { |
897 | 0 | libcerror_error_set( |
898 | 0 | error, |
899 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
900 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
901 | 0 | "%s: invalid input data size value exceeds maximum.", |
902 | 0 | function ); |
903 | |
|
904 | 0 | return( -1 ); |
905 | 0 | } |
906 | 271k | if( output_data == NULL ) |
907 | 0 | { |
908 | 0 | libcerror_error_set( |
909 | 0 | error, |
910 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
911 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
912 | 0 | "%s: invalid output data.", |
913 | 0 | function ); |
914 | |
|
915 | 0 | return( -1 ); |
916 | 0 | } |
917 | 271k | if( output_data_size > (size_t) SSIZE_MAX ) |
918 | 0 | { |
919 | 0 | libcerror_error_set( |
920 | 0 | error, |
921 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
922 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
923 | 0 | "%s: invalid output data size value exceeds maximum.", |
924 | 0 | function ); |
925 | |
|
926 | 0 | return( -1 ); |
927 | 0 | } |
928 | 271k | if( output_data_size < input_data_size ) |
929 | 0 | { |
930 | 0 | libcerror_error_set( |
931 | 0 | error, |
932 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
933 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
934 | 0 | "%s: invalid ouput data size smaller than input data size.", |
935 | 0 | function ); |
936 | |
|
937 | 0 | return( -1 ); |
938 | 0 | } |
939 | 271k | if( libcaes_crypt_ecb( |
940 | 271k | internal_tweaked_context->tweak_context, |
941 | 271k | LIBCAES_CRYPT_MODE_ENCRYPT, |
942 | 271k | tweak_value, |
943 | 271k | 16, |
944 | 271k | encrypted_tweak_value, |
945 | 271k | 16, |
946 | 271k | error ) != 1 ) |
947 | 0 | { |
948 | 0 | libcerror_error_set( |
949 | 0 | error, |
950 | 0 | LIBCERROR_ERROR_DOMAIN_ENCRYPTION, |
951 | 0 | LIBCERROR_ENCRYPTION_ERROR_GENERIC, |
952 | 0 | "%s: unable to encrypt tweak value.", |
953 | 0 | function ); |
954 | |
|
955 | 0 | goto on_error; |
956 | 0 | } |
957 | 271k | if( memory_set( |
958 | 271k | encrypted_tweak_value_copy, |
959 | 271k | 0, |
960 | 271k | 16 ) == NULL ) |
961 | 0 | { |
962 | 0 | libcerror_error_set( |
963 | 0 | error, |
964 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
965 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
966 | 0 | "%s: unable to clear encrypted tweak value copy.", |
967 | 0 | function ); |
968 | |
|
969 | 0 | goto on_error; |
970 | 0 | } |
971 | 271k | if( memory_copy( |
972 | 271k | output_data, |
973 | 271k | input_data, |
974 | 271k | input_data_size ) == NULL ) |
975 | 0 | { |
976 | 0 | libcerror_error_set( |
977 | 0 | error, |
978 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
979 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
980 | 0 | "%s: unable to copy input data to output data.", |
981 | 0 | function ); |
982 | |
|
983 | 0 | return( -1 ); |
984 | 0 | } |
985 | 271k | remaining_data_size = input_data_size; |
986 | | |
987 | 12.8M | while( data_offset <= ( input_data_size - 16 ) ) |
988 | 12.5M | { |
989 | 12.5M | if( ( remaining_data_size < 32 ) |
990 | 271k | && ( remaining_data_size != 16 ) ) |
991 | 1.39k | { |
992 | | /* If the input data size is not a multitude of 16 the remaining data needs to be handled differently |
993 | | */ |
994 | 1.39k | if( mode == LIBCAES_CRYPT_MODE_DECRYPT ) |
995 | 1.39k | { |
996 | 1.39k | if( memory_copy( |
997 | 1.39k | encrypted_tweak_value_copy, |
998 | 1.39k | encrypted_tweak_value, |
999 | 1.39k | 16 ) == NULL ) |
1000 | 0 | { |
1001 | 0 | libcerror_error_set( |
1002 | 0 | error, |
1003 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1004 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1005 | 0 | "%s: unable to copy encrypted tweak value.", |
1006 | 0 | function ); |
1007 | |
|
1008 | 0 | goto on_error; |
1009 | 0 | } |
1010 | | /* Update the encrypted tweak value for the next 16-byte block |
1011 | | */ |
1012 | 1.39k | carry_bit = 0; |
1013 | | |
1014 | 1.39k | for( block_index = 0; |
1015 | 23.7k | block_index < 16; |
1016 | 22.3k | block_index++ ) |
1017 | 22.3k | { |
1018 | 22.3k | byte_value = ( encrypted_tweak_value[ block_index ] << 1 ) | carry_bit; |
1019 | 22.3k | carry_bit = encrypted_tweak_value[ block_index ] >> 7; |
1020 | | |
1021 | 22.3k | encrypted_tweak_value[ block_index ] = byte_value; |
1022 | 22.3k | } |
1023 | 1.39k | if( carry_bit > 0 ) |
1024 | 623 | { |
1025 | 623 | encrypted_tweak_value[ 0 ] ^= 0x87; |
1026 | 623 | } |
1027 | 1.39k | } |
1028 | 1.39k | } |
1029 | 12.5M | #if defined( LIBCAES_UNFOLLED_LOOPS ) |
1030 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 0 ]; |
1031 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 1 ]; |
1032 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 2 ]; |
1033 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 3 ]; |
1034 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 4 ]; |
1035 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 5 ]; |
1036 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 6 ]; |
1037 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 7 ]; |
1038 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 8 ]; |
1039 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 9 ]; |
1040 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 10 ]; |
1041 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 11 ]; |
1042 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 12 ]; |
1043 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 13 ]; |
1044 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 14 ]; |
1045 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 15 ]; |
1046 | | #else |
1047 | | for( block_index = 0; |
1048 | | block_index < 16; |
1049 | | block_index++ ) |
1050 | | { |
1051 | | output_data[ data_offset++ ] ^= encrypted_tweak_value[ block_index ]; |
1052 | | } |
1053 | | #endif |
1054 | 12.5M | data_offset -= 16; |
1055 | | |
1056 | 12.5M | if( libcaes_crypt_ecb( |
1057 | 12.5M | internal_tweaked_context->main_context, |
1058 | 12.5M | mode, |
1059 | 12.5M | &( output_data[ data_offset ] ), |
1060 | 12.5M | 16, |
1061 | 12.5M | &( output_data[ data_offset ] ), |
1062 | 12.5M | 16, |
1063 | 12.5M | error ) != 1 ) |
1064 | 0 | { |
1065 | 0 | libcerror_error_set( |
1066 | 0 | error, |
1067 | 0 | LIBCERROR_ERROR_DOMAIN_ENCRYPTION, |
1068 | 0 | LIBCERROR_ENCRYPTION_ERROR_GENERIC, |
1069 | 0 | "%s: unable to de/encrypt data.", |
1070 | 0 | function ); |
1071 | |
|
1072 | 0 | goto on_error; |
1073 | 0 | } |
1074 | 12.5M | #if defined( LIBCAES_UNFOLLED_LOOPS ) |
1075 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 0 ]; |
1076 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 1 ]; |
1077 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 2 ]; |
1078 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 3 ]; |
1079 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 4 ]; |
1080 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 5 ]; |
1081 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 6 ]; |
1082 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 7 ]; |
1083 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 8 ]; |
1084 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 9 ]; |
1085 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 10 ]; |
1086 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 11 ]; |
1087 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 12 ]; |
1088 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 13 ]; |
1089 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 14 ]; |
1090 | 12.5M | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 15 ]; |
1091 | | #else |
1092 | | for( block_index = 0; |
1093 | | block_index < 16; |
1094 | | block_index++ ) |
1095 | | { |
1096 | | output_data[ data_offset++ ] ^= encrypted_tweak_value[ block_index ]; |
1097 | | } |
1098 | | #endif |
1099 | 12.5M | remaining_data_size -= 16; |
1100 | | |
1101 | | /* Update the encrypted tweak value for the next 16-byte block |
1102 | | */ |
1103 | 12.5M | carry_bit = 0; |
1104 | | |
1105 | 12.5M | for( block_index = 0; |
1106 | 213M | block_index < 16; |
1107 | 200M | block_index++ ) |
1108 | 200M | { |
1109 | 200M | byte_value = ( encrypted_tweak_value[ block_index ] << 1 ) | carry_bit; |
1110 | 200M | carry_bit = encrypted_tweak_value[ block_index ] >> 7; |
1111 | | |
1112 | 200M | encrypted_tweak_value[ block_index ] = byte_value; |
1113 | 200M | } |
1114 | 12.5M | if( carry_bit > 0 ) |
1115 | 6.25M | { |
1116 | 6.25M | encrypted_tweak_value[ 0 ] ^= 0x87; |
1117 | 6.25M | } |
1118 | 12.5M | } |
1119 | | /* Any remaining data needs to be handled differently |
1120 | | */ |
1121 | 271k | if( remaining_data_size > 0 ) |
1122 | 1.39k | { |
1123 | 1.39k | if( mode == LIBCAES_CRYPT_MODE_DECRYPT ) |
1124 | 1.39k | { |
1125 | 1.39k | if( memory_copy( |
1126 | 1.39k | encrypted_tweak_value, |
1127 | 1.39k | encrypted_tweak_value_copy, |
1128 | 1.39k | 16 ) == NULL ) |
1129 | 0 | { |
1130 | 0 | libcerror_error_set( |
1131 | 0 | error, |
1132 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1133 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1134 | 0 | "%s: unable to copy encrypted tweak value.", |
1135 | 0 | function ); |
1136 | |
|
1137 | 0 | goto on_error; |
1138 | 0 | } |
1139 | 1.39k | if( memory_set( |
1140 | 1.39k | encrypted_tweak_value_copy, |
1141 | 1.39k | 0, |
1142 | 1.39k | 16 ) == NULL ) |
1143 | 0 | { |
1144 | 0 | libcerror_error_set( |
1145 | 0 | error, |
1146 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1147 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
1148 | 0 | "%s: unable to clear encrypted tweak value copy.", |
1149 | 0 | function ); |
1150 | |
|
1151 | 0 | goto on_error; |
1152 | 0 | } |
1153 | 1.39k | } |
1154 | | /* Swap the data of the last 16-byte block with the remaining data |
1155 | | */ |
1156 | 1.39k | data_offset -= 16; |
1157 | | |
1158 | 1.39k | if( memory_copy( |
1159 | 1.39k | &( output_data[ data_offset + 16 ] ), |
1160 | 1.39k | &( output_data[ data_offset ] ), |
1161 | 1.39k | remaining_data_size ) == NULL ) |
1162 | 0 | { |
1163 | 0 | libcerror_error_set( |
1164 | 0 | error, |
1165 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1166 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1167 | 0 | "%s: unable to copy remaining output data.", |
1168 | 0 | function ); |
1169 | |
|
1170 | 0 | goto on_error; |
1171 | 0 | } |
1172 | 1.39k | if( memory_copy( |
1173 | 1.39k | &( output_data[ data_offset ] ), |
1174 | 1.39k | &( input_data[ data_offset + 16 ] ), |
1175 | 1.39k | remaining_data_size ) == NULL ) |
1176 | 0 | { |
1177 | 0 | libcerror_error_set( |
1178 | 0 | error, |
1179 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1180 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1181 | 0 | "%s: unable to copy input data to block data.", |
1182 | 0 | function ); |
1183 | |
|
1184 | 0 | goto on_error; |
1185 | 0 | } |
1186 | 1.39k | #if defined( LIBCAES_UNFOLLED_LOOPS ) |
1187 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 0 ]; |
1188 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 1 ]; |
1189 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 2 ]; |
1190 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 3 ]; |
1191 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 4 ]; |
1192 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 5 ]; |
1193 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 6 ]; |
1194 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 7 ]; |
1195 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 8 ]; |
1196 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 9 ]; |
1197 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 10 ]; |
1198 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 11 ]; |
1199 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 12 ]; |
1200 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 13 ]; |
1201 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 14 ]; |
1202 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 15 ]; |
1203 | | #else |
1204 | | for( block_index = 0; |
1205 | | block_index < 16; |
1206 | | block_index++ ) |
1207 | | { |
1208 | | output_data[ data_offset++ ] ^= encrypted_tweak_value[ block_index ]; |
1209 | | } |
1210 | | #endif |
1211 | 1.39k | data_offset -= 16; |
1212 | | |
1213 | 1.39k | if( libcaes_crypt_ecb( |
1214 | 1.39k | internal_tweaked_context->main_context, |
1215 | 1.39k | mode, |
1216 | 1.39k | &( output_data[ data_offset ] ), |
1217 | 1.39k | 16, |
1218 | 1.39k | &( output_data[ data_offset ] ), |
1219 | 1.39k | 16, |
1220 | 1.39k | error ) != 1 ) |
1221 | 0 | { |
1222 | 0 | libcerror_error_set( |
1223 | 0 | error, |
1224 | 0 | LIBCERROR_ERROR_DOMAIN_ENCRYPTION, |
1225 | 0 | LIBCERROR_ENCRYPTION_ERROR_GENERIC, |
1226 | 0 | "%s: unable to de/encrypt data.", |
1227 | 0 | function ); |
1228 | |
|
1229 | 0 | goto on_error; |
1230 | 0 | } |
1231 | 1.39k | #if defined( LIBCAES_UNFOLLED_LOOPS ) |
1232 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 0 ]; |
1233 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 1 ]; |
1234 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 2 ]; |
1235 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 3 ]; |
1236 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 4 ]; |
1237 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 5 ]; |
1238 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 6 ]; |
1239 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 7 ]; |
1240 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 8 ]; |
1241 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 9 ]; |
1242 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 10 ]; |
1243 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 11 ]; |
1244 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 12 ]; |
1245 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 13 ]; |
1246 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 14 ]; |
1247 | 1.39k | output_data[ data_offset++ ] ^= encrypted_tweak_value[ 15 ]; |
1248 | | #else |
1249 | | for( block_index = 0; |
1250 | | block_index < 16; |
1251 | | block_index++ ) |
1252 | | { |
1253 | | output_data[ data_offset++ ] ^= encrypted_tweak_value[ block_index ]; |
1254 | | } |
1255 | | #endif |
1256 | 1.39k | } |
1257 | 271k | if( memory_set( |
1258 | 271k | encrypted_tweak_value, |
1259 | 271k | 0, |
1260 | 271k | 16 ) == NULL ) |
1261 | 0 | { |
1262 | 0 | libcerror_error_set( |
1263 | 0 | error, |
1264 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1265 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
1266 | 0 | "%s: unable to clear encrypted tweak value.", |
1267 | 0 | function ); |
1268 | |
|
1269 | 0 | goto on_error; |
1270 | 0 | } |
1271 | 271k | return( 1 ); |
1272 | | |
1273 | 0 | on_error: |
1274 | 0 | memory_set( |
1275 | 0 | encrypted_tweak_value_copy, |
1276 | 0 | 0, |
1277 | 0 | 16 ); |
1278 | |
|
1279 | 0 | memory_set( |
1280 | 0 | encrypted_tweak_value, |
1281 | 0 | 0, |
1282 | 0 | 16 ); |
1283 | |
|
1284 | 0 | return( -1 ); |
1285 | 271k | } |
1286 | | |
1287 | | #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_CRYPTO_AES_XTS ) */ |
1288 | | |