/* Frees an encryption context * Returns 1 if successful or -1 on error */ int libbde_encryption_free( libbde_encryption_context_t **context, libcerror_error_t **error ) { static char *function = "libbde_encryption_free"; int result = 1; if( context == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid context.", function ); return( -1 ); } if( *context != NULL ) { if( ( *context )->fvek_decryption_context != NULL ) { if( libcaes_context_free( &( ( *context )->fvek_decryption_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable free FVEK decryption context.", function ); result = -1; } } if( ( *context )->fvek_encryption_context != NULL ) { if( libcaes_context_free( &( ( *context )->fvek_encryption_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable free FVEK encryption context.", function ); result = -1; } } if( ( *context )->tweak_decryption_context != NULL ) { if( libcaes_context_free( &( ( *context )->tweak_decryption_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable free TWEAK decryption context.", function ); result = -1; } } if( ( *context )->tweak_encryption_context != NULL ) { if( libcaes_context_free( &( ( *context )->tweak_encryption_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable free TWEAK encryption context.", function ); result = -1; } } if( ( *context )->fvek_decryption_tweaked_context != NULL ) { if( libcaes_tweaked_context_free( &( ( *context )->fvek_decryption_tweaked_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable free FVEK decryption tweaked context.", function ); result = -1; } } if( ( *context )->fvek_encryption_tweaked_context != NULL ) { if( libcaes_tweaked_context_free( &( ( *context )->fvek_encryption_tweaked_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable free FVEK encryption tweaked context.", function ); result = -1; } } memory_free( *context ); *context = NULL; } return( result ); }
/* Creates an encryption context * Make sure the value encryption context is referencing, is set to NULL * Returns 1 if successful or -1 on error */ int libbde_encryption_initialize( libbde_encryption_context_t **context, uint16_t method, libcerror_error_t **error ) { static char *function = "libbde_encryption_initialize"; if( context == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid context.", function ); return( -1 ); } if( *context != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid context value already set.", function ); return( -1 ); } if( ( method != LIBBDE_ENCRYPTION_METHOD_AES_128_CBC ) && ( method != LIBBDE_ENCRYPTION_METHOD_AES_128_CBC_DIFFUSER ) && ( method != LIBBDE_ENCRYPTION_METHOD_AES_256_CBC ) && ( method != LIBBDE_ENCRYPTION_METHOD_AES_256_CBC_DIFFUSER ) && ( method != LIBBDE_ENCRYPTION_METHOD_AES_128_XTS ) && ( method != LIBBDE_ENCRYPTION_METHOD_AES_256_XTS ) ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, "%s: unsupported method.", function ); return( -1 ); } *context = memory_allocate_structure( libbde_encryption_context_t ); if( *context == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create context.", function ); goto on_error; } if( memory_set( *context, 0, sizeof( libbde_encryption_context_t ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear context.", function ); memory_free( *context ); *context = NULL; return( -1 ); } if( ( method == LIBBDE_ENCRYPTION_METHOD_AES_128_CBC ) || ( method == LIBBDE_ENCRYPTION_METHOD_AES_128_CBC_DIFFUSER ) || ( method == LIBBDE_ENCRYPTION_METHOD_AES_256_CBC ) || ( method == LIBBDE_ENCRYPTION_METHOD_AES_256_CBC_DIFFUSER ) ) { if( libcaes_context_initialize( &( ( *context )->fvek_decryption_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize FVEK decryption context.", function ); goto on_error; } if( libcaes_context_initialize( &( ( *context )->fvek_encryption_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize FVEK encryption context.", function ); goto on_error; } } if( ( method == LIBBDE_ENCRYPTION_METHOD_AES_128_CBC_DIFFUSER ) || ( method == LIBBDE_ENCRYPTION_METHOD_AES_256_CBC_DIFFUSER ) ) { if( libcaes_context_initialize( &( ( *context )->tweak_decryption_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize TWEAK decryption context.", function ); goto on_error; } if( libcaes_context_initialize( &( ( *context )->tweak_encryption_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize TWEAK encryption context.", function ); goto on_error; } } if( ( method == LIBBDE_ENCRYPTION_METHOD_AES_128_XTS ) || ( method == LIBBDE_ENCRYPTION_METHOD_AES_256_XTS ) ) { if( libcaes_tweaked_context_initialize( &( ( *context )->fvek_decryption_tweaked_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize FVEK decryption tweaked context.", function ); goto on_error; } if( libcaes_tweaked_context_initialize( &( ( *context )->fvek_encryption_tweaked_context ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize FVEK encryption tweaked context.", function ); goto on_error; } } ( *context )->method = method; return( 1 ); on_error: if( *context != NULL ) { if( ( *context )->fvek_encryption_tweaked_context != NULL ) { libcaes_tweaked_context_free( &( ( *context )->fvek_encryption_tweaked_context ), NULL ); } if( ( *context )->fvek_decryption_tweaked_context != NULL ) { libcaes_tweaked_context_free( &( ( *context )->fvek_decryption_tweaked_context ), NULL ); } if( ( *context )->tweak_encryption_context != NULL ) { libcaes_context_free( &( ( *context )->tweak_encryption_context ), NULL ); } if( ( *context )->tweak_decryption_context != NULL ) { libcaes_context_free( &( ( *context )->tweak_decryption_context ), NULL ); } if( ( *context )->fvek_encryption_context != NULL ) { libcaes_context_free( &( ( *context )->fvek_encryption_context ), NULL ); } if( ( *context )->fvek_decryption_context != NULL ) { libcaes_context_free( &( ( *context )->fvek_decryption_context ), NULL ); } memory_free( *context ); *context = NULL; } return( -1 ); }
/* Frees a tweaked context object */ void pycaes_tweaked_context_free( pycaes_tweaked_context_t *pycaes_context ) { libcerror_error_t *error = NULL; struct _typeobject *ob_type = NULL; static char *function = "pycaes_tweaked_context_free"; int result = 0; if( pycaes_context == NULL ) { PyErr_Format( PyExc_ValueError, "%s: invalid tweaked context.", function ); return; } if( pycaes_context->context == NULL ) { PyErr_Format( PyExc_ValueError, "%s: invalid tweaked context - missing libcaes context.", function ); return; } ob_type = Py_TYPE( pycaes_context ); if( ob_type == NULL ) { PyErr_Format( PyExc_ValueError, "%s: missing ob_type.", function ); return; } if( ob_type->tp_free == NULL ) { PyErr_Format( PyExc_ValueError, "%s: invalid ob_type - missing tp_free.", function ); return; } Py_BEGIN_ALLOW_THREADS result = libcaes_tweaked_context_free( &( pycaes_context->context ), &error ); Py_END_ALLOW_THREADS if( result != 1 ) { pycaes_error_raise( error, PyExc_MemoryError, "%s: unable to free libcaes tweaked context.", function ); libcerror_error_free( &error ); } ob_type->tp_free( (PyObject*) pycaes_context ); }