/* Intializes a tweaked context object
 * Returns 0 if successful or -1 on error
 */
int pycaes_tweaked_context_init(
     pycaes_tweaked_context_t *pycaes_context )
{
	static char *function    = "pycaes_tweaked_context_init";
	libcerror_error_t *error = NULL;

	if( pycaes_context == NULL )
	{
		PyErr_Format(
		 PyExc_ValueError,
		 "%s: invalid tweaked context.",
		 function );

		return( -1 );
	}
	pycaes_context->context = NULL;

	if( libcaes_tweaked_context_initialize(
	     &( pycaes_context->context ),
	     &error ) != 1 )
	{
		pycaes_error_raise(
		 error,
		 PyExc_MemoryError,
		 "%s: unable to initialize tweaked context.",
		 function );

		libcerror_error_free(
		 &error );

		return( -1 );
	}
	return( 0 );
}
Exemple #2
0
/* 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 );
}