diff options
author | Christian Weisgerber <naddy@cvs.openbsd.org> | 2020-06-15 19:25:17 +0000 |
---|---|---|
committer | Christian Weisgerber <naddy@cvs.openbsd.org> | 2020-06-15 19:25:17 +0000 |
commit | e5ad29fdac93959936ff50e2c8de4e2cbd3b0e88 (patch) | |
tree | 49262a716700e89120191b2d68286cbb0bb96ae2 | |
parent | 6c95aa41f8c67a5b1263eb8c4dbf5cdf28487cd0 (diff) |
Check rdrand for success and try up to ten times, as recommended by Intel.
Do the same for rdseed.
ok deraadt@
-rw-r--r-- | sys/arch/amd64/stand/libsa/mdrandom.c | 22 | ||||
-rw-r--r-- | sys/arch/i386/stand/libsa/mdrandom.c | 22 |
2 files changed, 38 insertions, 6 deletions
diff --git a/sys/arch/amd64/stand/libsa/mdrandom.c b/sys/arch/amd64/stand/libsa/mdrandom.c index 80634bde2a6..62cb7e0dd30 100644 --- a/sys/arch/amd64/stand/libsa/mdrandom.c +++ b/sys/arch/amd64/stand/libsa/mdrandom.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdrandom.c,v 1.1 2020/06/14 16:00:11 deraadt Exp $ */ +/* $OpenBSD: mdrandom.c,v 1.2 2020/06/15 19:25:16 naddy Exp $ */ /* * Copyright (c) 2020 Theo de Raadt @@ -40,9 +40,17 @@ mdrandom(char *buf, size_t buflen) CPUID(1, eax, ebx, ecx, edx); if (ecx & CPUIDECX_RDRAND) { unsigned long rand; + int retries; + uint8_t valid; for (i = 0; i < buflen / sizeof(rand); i++) { - __asm volatile("rdrand %0\n" : "=r" (rand)); + retries = 10; + do { + __asm volatile( + "rdrand %0;" + "setc %1;" + : "=r" (rand), "=qm" (valid)); + } while (!valid && --retries > 0); ((unsigned long *)buf)[i] ^= rand; } } @@ -52,9 +60,17 @@ mdrandom(char *buf, size_t buflen) CPUID_LEAF(7, 0, eax, ebx, ecx, edx); if (ebx & SEFF0EBX_RDSEED) { unsigned long rand; + int retries; + uint8_t valid; for (i = 0; i < buflen / sizeof(rand); i++) { - __asm volatile("rdseed %0\n" : "=r" (rand)); + retries = 10; + do { + __asm volatile( + "rdseed %0;" + "setc %1;" + : "=r" (rand), "=qm" (valid)); + } while (!valid && --retries > 0); ((unsigned long *)buf)[i] ^= rand; } } diff --git a/sys/arch/i386/stand/libsa/mdrandom.c b/sys/arch/i386/stand/libsa/mdrandom.c index 91b80992214..3be1f35be3f 100644 --- a/sys/arch/i386/stand/libsa/mdrandom.c +++ b/sys/arch/i386/stand/libsa/mdrandom.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdrandom.c,v 1.1 2020/06/14 16:00:11 deraadt Exp $ */ +/* $OpenBSD: mdrandom.c,v 1.2 2020/06/15 19:25:16 naddy Exp $ */ /* * Copyright (c) 2020 Theo de Raadt @@ -46,9 +46,17 @@ mdrandom(char *buf, size_t buflen) } if (ecx & CPUIDECX_RDRAND) { unsigned long rand; + int retries; + uint8_t valid; for (i = 0; i < buflen / sizeof(rand); i++) { - __asm volatile("rdrand %0\n" : "=r" (rand)); + retries = 10; + do { + __asm volatile( + "rdrand %0;" + "setc %1;" + : "=r" (rand), "=qm" (valid)); + } while (!valid && --retries > 0); ((unsigned long *)buf)[i] ^= rand; } ret = 0; @@ -58,9 +66,17 @@ mdrandom(char *buf, size_t buflen) CPUID_LEAF(7, 0, eax, ebx, ecx, edx); if (ebx & SEFF0EBX_RDSEED) { unsigned long rand; + int retries; + uint8_t valid; for (i = 0; i < buflen / sizeof(rand); i++) { - __asm volatile("rdseed %0\n" : "=r" (rand)); + retries = 10; + do { + __asm volatile( + "rdseed %0;" + "setc %1;" + : "=r" (rand), "=qm" (valid)); + } while (!valid && --retries > 0); ((unsigned long *)buf)[i] ^= rand; } ret = 0; |