ta/wallet_ta.c
/** Wrapper around write_cache_wallet_storage() which also encrypts data
  * using aes_xts_internal(). Because this uses encryption, it is much slower
  * than write_cache_wallet_storage(). The parameters and return values are
  * identical to that of write_cache_wallet_storage(). It is to be
  * used internally (only by the functions of the TA itself).
  * \param session_data A data pointer to a session context.
  * \param data A pointer to the data to be written.
  * \param length The number of bytes to write.
  * \param address Byte offset specifying where in the partition to
  *                start writing to.
  * \param nv_error NonVolatilereturn error.
  * \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.
  */
Wrapper around write_cache_wallet_storage() which also encrypts data
using aes_xts_internal(). Because this uses encryption, it is much slower
than write_cache_wallet_storage(). The parameters and return values are
identical to that of write_cache_wallet_storage(). It is to be
used internally (only by the functions of the TA itself).
\param session_data A data pointer to a session context.
\param data A pointer to the data to be written.
\param length The number of bytes to write.
\param address Byte offset specifying where in the partition to
start writing to.
Wrapper around write_cache_wallet_storage () which also encrypts data using aes_xts_internal () . Because this uses encryption it is much slower than write_cache_wallet_storage () . The parameters and return values are identical to that of write_cache_wallet_storage () . It is to be used internally ( only by the functions of the TA itself ) . \ param session_data A data pointer to a session context . \ param data A pointer to the data to be written . \ param length The number of bytes to write . \ param address Byte offset specifying where in the partition to start writing to .
static TEE_Result encrypted_write_wallet_storage_internal(Session_data * session_data, uint8_t *data, uint32_t length, uint32_t address, NonVolatileReturn * nv_error)
{
	TEE_Result result = TEE_SUCCESS;
	uint8_t ciphertext[length];
	uint8_t plaintext[length];
	TEE_Param read_params[4];
	uint32_t read_param_types;
	TEE_Param write_params[4];
	uint32_t write_param_types;
	read_param_types = TEE_PARAM_TYPES(
									TEE_PARAM_TYPE_MEMREF_OUTPUT,
									TEE_PARAM_TYPE_VALUE_INOUT,
									TEE_PARAM_TYPE_NONE,
									TEE_PARAM_TYPE_NONE);
	write_param_types = TEE_PARAM_TYPES(
									TEE_PARAM_TYPE_MEMREF_INPUT,
									TEE_PARAM_TYPE_VALUE_INOUT,
									TEE_PARAM_TYPE_NONE,
									TEE_PARAM_TYPE_NONE);
	*nv_error = NV_IO_ERROR;
	if ((address + length) < address)
	{
		return TEE_ERROR_BAD_STATE;
	}
	read_params[0].memref.buffer = (uint8_t *)ciphertext;
	read_params[0].memref.size = length;
	read_params[1].value.a = address;
	result = read_cache_wallet_storage(session_data,
										read_param_types,
										read_params);
	if (result != TEE_SUCCESS)
	{
		#ifdef OP_TEE_TA
		DMSG("Read wallet storage failed : 0x%x", result);
		#endif
		goto cleanup1;
	}
	result = aes_xts_internal(session_data,
							TEE_MODE_DECRYPT,
							ciphertext,
							length,
							plaintext,
							length);
	if (result != TEE_SUCCESS)
	{
		#ifdef OP_TEE_TA
		DMSG("AES XTS Decrypt failed : 0x%x", result);
		#endif
		goto cleanup1;
	}
	TEE_MemMove((uint8_t*)plaintext, (uint8_t*)data, length);
	result = aes_xts_internal(session_data,
							TEE_MODE_ENCRYPT,
							plaintext,
							length,
							ciphertext,
							length);
	if (result != TEE_SUCCESS)
	{
		#ifdef OP_TEE_TA
		DMSG("AES XTS Encrypt failed : 0x%x", result);
		#endif
		goto cleanup1;
	}
	write_params[0].memref.buffer = (uint8_t *)ciphertext;
	write_params[0].memref.size = length;
	write_params[1].value.a = address;
	result = write_cache_wallet_storage(session_data,
										write_param_types,
										write_params);
	*nv_error = (NonVolatileReturn)(write_params[1].value.b);
	if (result != TEE_SUCCESS)
	{
		#ifdef OP_TEE_TA
		DMSG("Write wallet storage failed : 0x%x", result);
		#endif
		goto cleanup1;
	}
	cleanup1:
		return result;
}
static TEE_Result encrypted_write_wallet_storage_internal ( Session_data * session_data , uint8_t * data , uint32_t length , uint32_t address , NonVolatileReturn * nv_error ) { TEE_Result result = TEE_SUCCESS ; uint8_t ciphertext [ length ] ; uint8_t plaintext [ length ] ; TEE_Param read_params [ 4 ] ; uint32_t read_param_types ; TEE_Param write_params [ 4 ] ; uint32_t write_param_types ; read_param_types = TEE_PARAM_TYPES ( TEE_PARAM_TYPE_MEMREF_OUTPUT , TEE_PARAM_TYPE_VALUE_INOUT , TEE_PARAM_TYPE_NONE , TEE_PARAM_TYPE_NONE ) ; write_param_types = TEE_PARAM_TYPES ( TEE_PARAM_TYPE_MEMREF_INPUT , TEE_PARAM_TYPE_VALUE_INOUT , TEE_PARAM_TYPE_NONE , TEE_PARAM_TYPE_NONE ) ; * nv_error = NV_IO_ERROR ; if ( ( address + length ) < address ) { return TEE_ERROR_BAD_STATE ; } read_params [ 0 ] . memref . buffer = ( uint8_t * ) ciphertext ; read_params [ 0 ] . memref . size = length ; read_params [ 1 ] . value . a = address ; result = read_cache_wallet_storage ( session_data , read_param_types , read_params ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } result = aes_xts_internal ( session_data , TEE_MODE_DECRYPT , ciphertext , length , plaintext , length ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } TEE_MemMove ( ( uint8_t * ) plaintext , ( uint8_t * ) data , length ) ; result = aes_xts_internal ( session_data , TEE_MODE_ENCRYPT , plaintext , length , ciphertext , length ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } write_params [ 0 ] . memref . buffer = ( uint8_t * ) ciphertext ; write_params [ 0 ] . memref . size = length ; write_params [ 1 ] . value . a = address ; result = write_cache_wallet_storage ( session_data , write_param_types , write_params ) ; * nv_error = ( NonVolatileReturn ) ( write_params [ 1 ] . value . b ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } cleanup1 : return result ; }
Wrapper around write_cache_wallet_storage() which also encrypts data
 using aes_xts_internal().
Wrapper around write_cache_wallet_storage () which also encrypts data using aes_xts_internal () .
/* Declare parameters that will be used in read operations */ /* Declare parameters that will be used in write operations */ /*
	 * Read parameters:
	 * [OUT] params[0].memref.buffer -> Buffer to store the read data.
	 * [OUT] params[0].memref.size   -> Size of the buffer.
	 * [IN]  params[1].value.a       -> Starting address of the reading.
	 * [OUT] params[1].value.b       -> Size actually read.
	 */ /*
	 * Write parameters:
	 * [IN]  params[0].memref.buffer -> Buffer with data to write in the storage.
	 * [IN]  params[0].memref.size   -> Size of the buffer to be wrote.
	 * [IN]  params[1].value.a       -> Starting address of the writing.
	 * [OUT] params[1].value.b       -> NonVolatileReturn error.
	 */ /*
		 * Here the return should be return TEE_ERROR_BAD_PARAMETERS;
		 * but as this will be treated as an error by the CA the most safest way
		 * is to set as TEE_ERROR_BAD_STATE so signal an operation that failed
		 * but without the needing to exit the program
		 */ /* Define the read operation parameters */ /* Fetch the data that is currently in the position to be write */ /* Decrypt the data fetched */ /* Replace the data decrypted with the new data to be written */ /* Encrypt the data to be written */ /* Define the write operation parameters */ /* Write the data */