host/prandom.c
/** Fill buffer with 32 random bytes from a hardware random number generator. As
  * Juno-r2 board does not has a hardware random generator it is used the
  * TEE_GenerateRandom() function provided by OP-TEE and specified by Global
  * Platform in TEE Internal Core API Specification v1.1.
  *
  * It is to notice that the Juno platform does indeed include a
  * "Trusted Entropy Source" which provides a 128-bit random number:
  * http://infocenter.arm.com/help/topic/com.arm.doc.ddi0515f/index.html
  * It's documented as above in section 2.7.4 with the programmers' model in
  * section 3.13.
  *
  * The intent of the random number is to be used as a seed for a software
  * algorithm, some older documentation suggests using the 128-bit seed to
  * generate '512 different random numbers' and the TEE_GenerateRandom()
  * function uses the board random generator when available but at the moment
  * (10:24:35 WEST Sunday, 7 August 2016) the above hardware is not being used:
  * https://github.com/OP-TEE/optee_os/issues/923.
  *
  * \param buffer The buffer to fill. This should have enough space for 32
  *               bytes.
  * \return An estimate of the total number of bits (not bytes) of entropy in
  *         the buffer on success, or a negative number if the hardware random
  *         number generator failed in any way. This may also return 0 to tell
  *         the caller that more samples are needed in order to do any
  *         meaningful statistical testing. If this returns 0, the caller
  *         should continue to call this until it returns a non-zero value.
  */
Fill buffer with 32 random bytes from a hardware random number generator. As
Juno-r2 board does not has a hardware random generator it is used the
TEE_GenerateRandom() function provided by OP-TEE and specified by Global
Platform in TEE Internal Core API Specification v1.1.





\param buffer The buffer to fill. This should have enough space for 32
bytes.
\return An estimate of the total number of bits (not bytes) of entropy in
the buffer on success, or a negative number if the hardware random
number generator failed in any way. This may also return 0 to tell
the caller that more samples are needed in order to do any
meaningful statistical testing. If this returns 0, the caller
should continue to call this until it returns a non-zero value.
Fill buffer with 32 random bytes from a hardware random number generator . As Juno - r2 board does not has a hardware random generator it is used the TEE_GenerateRandom () function provided by OP - TEE and specified by Global Platform in TEE Internal Core API Specification v1 . 1 . \ param buffer The buffer to fill . This should have enough space for 32 bytes . \ return An estimate of the total number of bits ( not bytes ) of entropy in the buffer on success or a negative number if the hardware random number generator failed in any way . This may also return 0 to tell the caller that more samples are needed in order to do any meaningful statistical testing . If this returns 0 the caller should continue to call this until it returns a non - zero value .
int hardwareRandom32Bytes(uint8_t *buffer)
{
    uint16_t entropy;
    uint8_t i;
    uint16_t sample;
    entropy = 128;
    memset(buffer, 0, 32);
    if(!is_test)
    {
        generateRandomBytesTZ((uint8_t *)buffer, (uint32_t)32);
    }
    else
    {
        if (!broken_hwrng)
        {
            for (i = 0; i < 32; i++)
            {
                sample = (uint8_t)rand();
                buffer[i] = (uint8_t)((uint8_t)sample ^ (uint8_t)(sample >> 8));
            }
        }
    }
    return entropy;
}
int hardwareRandom32Bytes ( uint8_t * buffer ) { uint16_t entropy ; uint8_t i ; uint16_t sample ; entropy = 128 ; memset ( buffer , 0 , 32 ) ; if ( ! is_test ) { generateRandomBytesTZ ( ( uint8_t * ) buffer , ( uint32_t ) 32 ) ; } else { if ( ! broken_hwrng ) { for ( i = 0 ; i < 32 ; i ++ ) { sample = ( uint8_t ) rand ( ) ; buffer [ i ] = ( uint8_t ) ( ( uint8_t ) sample ^ ( uint8_t ) ( sample >> 8 ) ) ; } } } return entropy ; }
Fill buffer with 32 random bytes from a hardware random number generator.
Fill buffer with 32 random bytes from a hardware random number generator .
/*
     * At the moment (10:24:35 WEST Sunday, 7 August 2016) TEE_GenerateRandom()
     * was using the Fortuna (PRNG) algorithm . Here just assuming that each
     * sample has 4 bits of entropy.
     */ /*
                 * It does not really mater if this is not a good random
                 * generator because the most part of the values here generated
                 * are replaced or the seed is constant to create deterministic
                 * tests.
                 */