Skip to content

Instantly share code, notes, and snippets.

@andrew-d
Created January 11, 2021 21:12
Show Gist options
  • Save andrew-d/bcbe477f7de9a03c7b8285bcee531196 to your computer and use it in GitHub Desktop.
Save andrew-d/bcbe477f7de9a03c7b8285bcee531196 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
int rdrand(uint32_t *out) {
char rc;
unsigned int val;
__asm__ volatile(
"rdrand %0 ; setc %1"
: "=r" (val), "=qm" (rc)
:
: "cc"
);
if (rc) {
*out = val;
} else {
*out = 0;
}
return rc;
}
int rdseed(uint32_t *out) {
char rc;
unsigned int val;
__asm__ volatile(
"rdseed %0 ; setc %1"
: "=r" (val), "=qm" (rc)
:
: "cc"
);
if (rc) {
*out = val;
} else {
*out = 0;
}
return rc;
}
int main(int argc, char *argv[])
{
int failures = 0;
for (int j, i = 0; i < 2000; ++i) {
for (j = 0; j < 10; ++j) {
uint32_t val = 0;
if (rdrand(&val)) {
printf("RDRAND() = 0x%08x\n", val);
break;
} else {
failures++;
}
}
if (j == 10) {
puts("RDRAND() = FAIL");
return 1;
}
}
printf("RDRAND: %d failures\n", failures);
/*
failures = 0;
for (int j, i = 0; i < 2000; ++i) {
for (j = 0; j < 10; ++j) {
uint32_t val = 0;
if (rdseed(&val)) {
printf("RDSEED() = 0x%08x\n", val);
break;
} else {
failures++;
}
}
if (j == 10) {
puts("RDSEED() = FAIL");
return 1;
}
}
printf("RDSEED: %d failures\n", failures);
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment