Intel® Software Guard Extensions (Intel® SGX)
Discussion board focused on hardware-based isolation and memory encryption to provide extended code protection in solutions.

Linux SDK: sgx_read_rand not very random in software mode

E_-O__B_
Beginner
818 Views

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
Juan_d_Intel
Employee
818 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
E_-O__B_
Beginner
818 Views

Ok, thanks!

0 Kudos
Reply