46 void cpuid (
cpuid_t *info,
unsigned int leaf,
unsigned int subleaf)
50 :
"=a" (info->eax),
"=b" (info->ebx),
"=c" (info->ecx),
"=d" (info->edx)
51 :
"a" (leaf),
"c" (subleaf)
64 static int intel_cpu= -1;
68 if ( intel_cpu == -1 ) {
72 memcmp((
char *) &info.ebx,
"Genu", 4) ||
73 memcmp((
char *) &info.edx,
"ineI", 4) ||
74 memcmp((
char *) &info.ecx,
"ntel", 4)
87 #define DRNG_NO_SUPPORT 0x0 88 #define DRNG_HAS_RDRAND 0x1 89 #define DRNG_HAS_RDSEED 0x2 100 static int drng_features= -1;
105 if ( drng_features == -1 ) {
106 drng_features= DRNG_NO_SUPPORT;
113 if ( (info.ecx & 0x40000000) == 0x40000000 ) {
114 drng_features|= DRNG_HAS_RDRAND;
119 if ( (info.ebx & 0x40000) == 0x40000 ) {
120 drng_features|= DRNG_HAS_RDSEED;
125 return drng_features;
128 #if MPHELL_USE_RDSEED == 1 137 int rdseed16_step (uint16_t *seed)
141 asm volatile (
"rdseed %0; setc %1" 142 :
"=r" (*seed),
"=qm" (ok));
156 int rdseed16_with_retry (uint16_t *seed, uint8_t nb_retry)
162 success=rdseed16_step(seed);
172 #elif MPHELL_USE_RDSEED == 0 182 int rdseed16_with_retry (uint16_t *seed, uint8_t nb_retry)
202 char *text =
"intel_rdrand : intel rdseed not available";
203 ret->info = malloc(strlen(text));
204 memcpy(ret->info, text, strlen(text));
205 memset(data, 0, length);
209 uint16_t nb = ceil((
float)length / 2);
213 for(i = 0; i < nb; i++)
215 if(rdseed16_with_retry(tmp + i, 10) != 1)
218 char *text =
"intel_rdseed : unable to generate bytes";
220 ret->info = malloc(strlen(text));
221 memcpy(ret->info, text, strlen(text));
222 memset(data, 0, length);
226 memcpy(data, tmp, length);
242 fd = fopen(
"/dev/urandom",
"rb");
247 char *text =
"dev_urandom : file /dev/urandom not available";
248 ret->info = malloc(strlen(text));
249 memcpy(ret->info, text, strlen(text));
250 memset(data, 0, length);
254 if(fread(data, 1, length, fd) != length)
257 char *text =
"dev_urandom : unable to generate bytes";
258 ret->info = malloc(strlen(text));
259 memcpy(ret->info, text, strlen(text));
260 memset(data, 0, length);
279 fd = fopen(
"/dev/random",
"rb");
284 char *text =
"dev_random : file /dev/random not available";
285 ret->info = malloc(strlen(text));
286 memcpy(ret->info, text, strlen(text));
287 memset(data, 0, length);
291 if(fread(data, 1, length, fd) != length)
294 char *text =
"dev_random : unable to generate bytes";
295 ret->info = malloc(strlen(text));
296 memcpy(ret->info, text, strlen(text));
297 memset(data, 0, length);
325 char *text =
"get_entropy_input : Invalid entropy src";
326 ret->info = malloc(strlen(text));
327 memcpy(ret->info, text, strlen(text));
void mphell_error(char *expr)
Write in stderr, filename, line and expr, free mphell.
void get_entropy_input(mphell_status ret, uint8_t *data, const entropy_type entropy_src, const uint16_t length)
Get length bit of entropy from the entropy source selected.
void cpuid(cpuid_t *info, unsigned int leaf, unsigned int subleaf)
Get cpuid informations, code comes from the "Intel® Digital Random Number Generator (DRNG) Software I...
void dev_random(mphell_status ret, uint8_t *data, const uint16_t length)
Get length bit of entropy from dev/random.
void dev_urandom(mphell_status ret, uint8_t *data, const uint16_t length)
Get length bit of entropy from /dev/urandom.
mphell_status_t mphell_status[1]
The status is a couple (flag, information)
void intel_rdseed(mphell_status ret, uint8_t *data, const uint16_t length)
Get length bit of entropy from intel RDSEED instruction.
int _is_intel_cpu()
Define if the cpu is an intel cpu or not, code comes from the "Intel® Digital Random Number Generator...
enum entropy_source entropy_type
Define the entropy source.
Declaration of entropy functions.
To store registers, code comes from the "Intel® Digital Random Number Generator (DRNG) Software Imple...
int get_drng_support()
Test if the cpu support RDRAND and / or RDSEED, code comes from the "Intel® Digital Random Number Gen...