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

Using random library within enclave

swarup
Beginner
2,597 Views

Hello,

I need to generate random numbers (integers or double) and use binomial distribution within an enclave. The user guide mentions that <random.h> is not included for SGX, while a random number can be generated using sgx_read_rand function in <sgx_trts.h>.

First, how do I use sgx_read_rand to generate random integers? It seems only unsigned char is added to the input buffer.

Second, I want to use the binomial_distribution and beta_distribution class available in the standard <random.h>. How do I invoke this within the enclave?

Any help is appreciated.

0 Kudos
1 Solution
Surenthar_S_Intel
2,597 Views

Hi Swarup,

1. The sgx_read_rand() function populates a buffer with random bytes. To turn these into an integer, you simply populate a buffer of the corresponding size (1 byte for an 8-bit integer, 2 bytes for a 16-bit integer, 4 bytes for a 32-bit integer, and 8 bytes for a 64-bit integer) and recast it to the appropriate integer type.

A quick and easy way of doing this is:
                  uint32_t val; 
                  sgx_read_rand((unsigned char *) &val, 4);  And you have an unsigned, 32-bit random number in val. (Converting that to a number from 0 to N is an exercise for the reader. There are plenty of references online for unbiased algorithms that do this.)
 
2. std::random is part of the C++11 standard. At this time, enclaves only support the C++03 standard.

Thanks and Reagrds,
Surenthar Selvaraj

View solution in original post

1 Reply
Surenthar_S_Intel
2,598 Views

Hi Swarup,

1. The sgx_read_rand() function populates a buffer with random bytes. To turn these into an integer, you simply populate a buffer of the corresponding size (1 byte for an 8-bit integer, 2 bytes for a 16-bit integer, 4 bytes for a 32-bit integer, and 8 bytes for a 64-bit integer) and recast it to the appropriate integer type.

A quick and easy way of doing this is:
                  uint32_t val; 
                  sgx_read_rand((unsigned char *) &val, 4);  And you have an unsigned, 32-bit random number in val. (Converting that to a number from 0 to N is an exercise for the reader. There are plenty of references online for unbiased algorithms that do this.)
 
2. std::random is part of the C++11 standard. At this time, enclaves only support the C++03 standard.

Thanks and Reagrds,
Surenthar Selvaraj

Reply