ta/wallet_ta.c
/**
  * Derive a key using the specified password and salt, using HMAC-SHA512 as
  * the underlying pseudo-random function. The derived key length is fixed
  * at #SHA512_HASH_LENGTH bytes.
  *
  * This code here is based on section 5.3 ("PBKDF Specification") of
  * NIST SP 800-132 (obtained from
  * http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf on
  * 30 March 2013).
  * \param out A byte array where the resulting derived key will be written.
  *            This must have space for #SHA512_HASH_LENGTH bytes.
  * \param password Byte array specifying the password to use in PBKDF2.
  * \param password_length The length (in bytes) of the password.
  * \param salt Byte array specifying the salt to use in PBKDF2.
  * \param salt_length The length (in bytes) of the salt.
  * \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.
  * \warning salt cannot be too long; salt_length must be less than or equal
  *          to #SHA512_HASH_LENGTH - 4.
  */
Derive a key using the specified password and salt, using HMAC-SHA512 as
the underlying pseudo-random function. The derived key length is fixed
at #SHA512_HASH_LENGTH bytes.


Derive a key using the specified password and salt using HMAC - SHA512 as the underlying pseudo - random function . The derived key length is fixed at #SHA512_HASH_LENGTH bytes .
static TEE_Result pbkdf2_internal(Session_data * session_data, uint8_t *out, const uint8_t *password, const unsigned int password_length, const uint8_t *salt, const unsigned int salt_length)
{
	TEE_Result result = TEE_SUCCESS;
	uint8_t u[SHA512_HASH_LENGTH];
	uint8_t hmac_result[SHA512_HASH_LENGTH];
	unsigned int u_length;
	uint32_t i;
	unsigned int j;
	TEE_MemFill((uint8_t*)out, 0, (uint32_t)SHA512_HASH_LENGTH);
	TEE_MemFill((uint8_t*)u, 0, (uint32_t)sizeof(u));
	if (salt_length > (SHA512_HASH_LENGTH - 4))
		return TEE_ERROR_BAD_PARAMETERS;
	u_length = salt_length;
	TEE_MemMove((uint8_t*)u, (uint8_t*)salt, u_length);
	write_u32_big_endian_internal(&(u[u_length]), 1);
	u_length += 4;
	result = set_hmac_sha512_key_internal(session_data,
										(uint8_t*)password,
										(unsigned int)password_length);
	if (result != TEE_SUCCESS)
    {
    	#ifdef OP_TEE_TA
    	DMSG("Failed to set the HMAC-SHA-512 operation key: 0x%x", result);
    	#endif
    	goto cleanup1;
    }
	for (i = 0; i < PBKDF2_ITERATIONS; i++)
	{
		result = hmac_sha512_internal(session_data, hmac_result, u, u_length);
		if (result != TEE_SUCCESS)
	    {
	    	#ifdef OP_TEE_TA
	    	DMSG("Failed to set do HMAC-SHA-512 operation : 0x%x", result);
	    	#endif
	    	goto cleanup1;
	    }
		TEE_MemMove((uint8_t*)u, (uint8_t*)hmac_result, sizeof(u));
		u_length = SHA512_HASH_LENGTH;
		for (j = 0; j < SHA512_HASH_LENGTH; j++)
			out[j] ^= u[j];
	}
	cleanup1:
		return result;
}
static TEE_Result pbkdf2_internal ( Session_data * session_data , uint8_t * out , const uint8_t * password , const unsigned int password_length , const uint8_t * salt , const unsigned int salt_length ) { TEE_Result result = TEE_SUCCESS ; uint8_t u [ SHA512_HASH_LENGTH ] ; uint8_t hmac_result [ SHA512_HASH_LENGTH ] ; unsigned int u_length ; uint32_t i ; unsigned int j ; TEE_MemFill ( ( uint8_t * ) out , 0 , ( uint32_t ) SHA512_HASH_LENGTH ) ; TEE_MemFill ( ( uint8_t * ) u , 0 , ( uint32_t ) sizeof ( u ) ) ; if ( salt_length > ( SHA512_HASH_LENGTH - 4 ) ) return TEE_ERROR_BAD_PARAMETERS ; u_length = salt_length ; TEE_MemMove ( ( uint8_t * ) u , ( uint8_t * ) salt , u_length ) ; write_u32_big_endian_internal ( & ( u [ u_length ] ) , 1 ) ; u_length += 4 ; result = set_hmac_sha512_key_internal ( session_data , ( uint8_t * ) password , ( unsigned int ) password_length ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } for ( i = 0 ; i < PBKDF2_ITERATIONS ; i ++ ) { result = hmac_sha512_internal ( session_data , hmac_result , u , u_length ) ; if ( result != TEE_SUCCESS ) { #ifdef OP_TEE_TA DMSG ( " " , result ) ; #endif goto cleanup1 ; } TEE_MemMove ( ( uint8_t * ) u , ( uint8_t * ) hmac_result , sizeof ( u ) ) ; u_length = SHA512_HASH_LENGTH ; for ( j = 0 ; j < SHA512_HASH_LENGTH ; j ++ ) out [ j ] ^= u [ j ] ; } cleanup1 : return result ; }
Derive a key using the specified password and salt, using HMAC-SHA512 as
 the underlying pseudo-random function.
Derive a key using the specified password and salt using HMAC - SHA512 as the underlying pseudo - random function .
/* 'Clearing' the output buffer */ /* Checking if salt is too long */ /* Input text length */ /* Copy the salt */ /* Set the HMAC-SHA-512 key */ /* Resources cleanup */