Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
58 Views

Linux SDK: sgx_read_rand not very random in software mode

Please have a look at the code below. When I run this in software emulation, i.e., not with SGX_MODE=HW, then the output is very predictable (0, 1, 2, 3, 0, 1, 2, 3, ...). Other values of x give me similarly predictable sequences. This makes debugging programs relying on randomness very tedious. Compiling and running with SGX_MODE=HW produces "real" randomness.

What am I missing?

 

  unsigned int r;
  unsigned int x = 4;
  for (int i=0 ;i<100;i++) {
    sgx_read_rand((unsigned char *) &r, sizeof(unsigned int));
    r = r % x;
    printf("%u\n", r);
  }

 

0 Kudos
2 Replies
Employee
58 Views

By default, the simulation library uses a pseudo-random generator instead of the RDRAND instruction, see #ifndef SE_SIM below.

static sgx_status_t  __do_get_rand32(uint32_t* rand_num)
{
#ifndef SE_SIM
    /* We expect the CPU has RDRAND support for HW mode. Otherwise, an exception will be thrown
    * do_rdrand() will try to call RDRAND for 10 times
    */
    if(0 == do_rdrand(rand_num))
        return SGX_ERROR_UNEXPECTED;
#else
    /*  use LCG in simulation mode */
    *rand_num = get_rand_lcg();
#endif
    return SGX_SUCCESS;
}

Enable that code section and build your tRTS library again.

0 Kudos
Highlighted
Beginner
58 Views

Ok, thanks!

0 Kudos