ta/wallet_ta.c
/**
  * Using the specified password and UUID (as the salt), derive an encryption
  * key and begin using it.
  *
  * This needs to be in wallet.c because there are situations (creating and
  * restoring a wallet) when the wallet UUID is not known before the beginning
  * of the appropriate function call.
  * \param session_data A data pointer to a session context.
  * \param param_types The types of the four parameters.
  * \param params A pointer to an array of four parameters.
  * \return TEE_SUCCESS in case of success otherwise returns the result
  *         received from the failed operation. The information about all
  *         possible returns is present in TEE Client API Specification - 4.4.2
  *         published by GlobalPlatform.
  */
Using the specified password and UUID (as the salt), derive an encryption
key and begin using it.

This needs to be in wallet.c because there are situations (creating and
restoring a wallet) when the wallet UUID is not known before the beginning
of the appropriate function call.
\param session_data A data pointer to a session context.
\param param_types The types of the four parameters.
\param params A pointer to an array of four parameters.
\return TEE_SUCCESS in case of success otherwise returns the result
received from the failed operation. The information about all
possible returns is present in TEE Client API Specification - 4.4.2
published by GlobalPlatform.
Using the specified password and UUID ( as the salt ) derive an encryption key and begin using it . This needs to be in wallet . c because there are situations ( creating and restoring a wallet ) when the wallet UUID is not known before the beginning of the appropriate function call . \ param session_data A data pointer to a session context . \ param param_types The types of the four parameters . \ param params A pointer to an array of four parameters . \ return TEE_SUCCESS in case of success otherwise returns the result received from the failed operation . The information about all possible returns is present in TEE Client API Specification - 4 . 4 . 2 published by GlobalPlatform .
static TEE_Result derive_and_set_encryption_key(Session_data * session_data, uint32_t param_types, TEE_Param params[4])
{
	TEE_Result result = TEE_SUCCESS;
	uint32_t exp_param_types;
	uint8_t derived_key[SHA512_HASH_LENGTH];
	exp_param_types = TEE_PARAM_TYPES(
							TEE_PARAM_TYPE_MEMREF_INPUT,
							TEE_PARAM_TYPE_MEMREF_INPUT,
							TEE_PARAM_TYPE_NONE,
							TEE_PARAM_TYPE_NONE);
	if (param_types != exp_param_types
		|| params[0].memref.buffer == NULL)
	{
		return TEE_ERROR_BAD_PARAMETERS;
	}
    if (sizeof(derived_key) < WALLET_ENCRYPTION_KEY_LENGTH)
    {
    	return TEE_ERROR_CANCEL;
    }
    if (params[1].memref.size > 0)
    {
        result = pbkdf2_internal(
        				session_data,
        				derived_key,
        				(uint8_t*)(params[1].memref.buffer),
        				params[1].memref.size,
        				(uint8_t*)(params[0].memref.buffer),
        				UUID_LENGTH);
        if (result != TEE_SUCCESS)
        {
        	#ifdef OP_TEE_TA
        	DMSG("Failed to perform pbkdf2 operation: 0x%x", result);
        	#endif
    		goto cleanup1;
        }
        result = set_encryption_key_internal(session_data, derived_key);
        if (result != TEE_SUCCESS)
        {
        	#ifdef OP_TEE_TA
        	DMSG("Failed to set encryption key generated by pbkdf2: 0x%x", result);
        	#endif
    		goto cleanup1;
        }
    }
    else
    {
    	TEE_MemFill((uint8_t*)derived_key, 0, (uint32_t)sizeof(derived_key));
        result = set_encryption_key_internal(session_data, derived_key);
        if (result != TEE_SUCCESS)
        {
        	#ifdef OP_TEE_TA
        	DMSG("Failed to set encryption key generated by pbkdf2: 0x%x", result);
        	#endif
    		goto cleanup1;
        }
    }
    cleanup1:
    	return result;
}
static TEE_Result derive_and_set_encryption_key ( Session_data * session_data , uint32_t param_types , TEE_Param params [ 4 ] ) { TEE_Result result = TEE_SUCCESS ; uint32_t exp_param_types ; uint8_t derived_key [ SHA512_HASH_LENGTH ] ; exp_param_types = TEE_PARAM_TYPES ( TEE_PARAM_TYPE_MEMREF_INPUT , TEE_PARAM_TYPE_MEMREF_INPUT , TEE_PARAM_TYPE_NONE , TEE_PARAM_TYPE_NONE ) ; if ( param_types != exp_param_types || params [ 0 ] . memref . buffer == NULL ) { return TEE_ERROR_BAD_PARAMETERS ; } if ( sizeof ( derived_key ) < WALLET_ENCRYPTION_KEY_LENGTH ) { return TEE_ERROR_CANCEL ; } if ( params [ 1 ] . memref . size > 0 ) { result = pbkdf2_internal ( session_data , derived_key , ( uint8_t * ) ( params [ 1 ] . memref . buffer ) , params [ 1 ] . memref . size , ( uint8_t * ) ( params [ 0 ] . memref . buffer ) , UUID_LENGTH ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } result = set_encryption_key_internal ( session_data , derived_key ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } } else { TEE_MemFill ( ( uint8_t * ) derived_key , 0 , ( uint32_t ) sizeof ( derived_key ) ) ; result = set_encryption_key_internal ( session_data , derived_key ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } } cleanup1 : return result ; }
Using the specified password and UUID (as the salt), derive an encryption
 key and begin using it.
Using the specified password and UUID ( as the salt ) derive an encryption key and begin using it .
/*
	 * Expected:
	 * [IN]  params[0].memref.buffer -> UUID.
	 * [IN]  params[0].memref.size   -> Length of the UUID.
	 * [IN]  params[1].memref.buffer -> Password.
	 * [IN]  params[1].memref.size   -> Length of the password.
	 */ /* Some sanity checks */ /* This should never happen */ /*
		 * Here the return could be return TEE_ERROR_BAD_STATE;
		 * but as this will NOT be treated as an error by the CA the most safest
		 * way is to set as TEE_ERROR_CANCEL to signal an operation that failed
		 * WHIT the needing to exit the program
		 */ /* Create an key based on the password and on the UUID of the wallet */ /* Set the generated key */ /* No password i.e. wallet is unencrypted. */ /* Resources cleanup */