Skip to content

Commit 4d4b82f

Browse files
committed
x86 asm: move rdrand from x86-assembly-cheat
1 parent 4ee1e06 commit 4d4b82f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

README.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12416,6 +12416,22 @@ link:userland/arch/x86_64/loop.S[LOOP]
1241612416

1241712417
Vs <<x86-jcc-instructions,Jcc>>: https://stackoverflow.com/questions/6805692/x86-assembly-programming-loops-with-ecx-and-loop-instruction-versus-jmp-jcond Holy CISC!
1241812418

12419+
=== x86 random number generator instructions
12420+
12421+
<<intel-manual-1>> 5.1.15 Random Number Generator Instructions
12422+
12423+
Example: link:userland/arch/x86_64/rdrand.S[RDRAND]
12424+
12425+
If you run that executable multiple times, it prints a random number every time to stdout.
12426+
12427+
RDRAND is a true random number generator!
12428+
12429+
This Intel engineer says its based on quantum effects: https://stackoverflow.com/questions/17616960/true-random-numbers-with-c11-and-rdrand/18004959#18004959
12430+
12431+
Generated some polemic when kernel devs wanted to use it as part of `/dev/random`, because it could be used as a cryptographic backdoor by Intel since it is a black box.
12432+
12433+
RDRAND sets the carry flag when data is ready so we must loop if the carry flag isn't set.
12434+
1241912435
=== x86 SIMD
1242012436

1242112437
History:

lkmc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ void lkmc_assert_memcmp(
5656
}
5757
}
5858

59+
void lkmc_print_hex_64(uint64_t x) {
60+
printf("0x%016" PRIx64, x);
61+
}
62+
63+
void lkmc_print_newline() {
64+
printf("\n");
65+
}
66+
5967
#if defined(__aarch64__)
6068
#define LKMC_SYSREG_READ_WRITE(type, name) \
6169
type LKMC_CONCAT(LKMC_CONCAT(LKMC_SYSREG_SYMBOL_PREFIX, name), _read(void)) { \

userland/arch/x86_64/rdrand.S

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-random-number-generator-instructions */
2+
3+
#include <lkmc.h>
4+
5+
LKMC_PROLOGUE
6+
1:
7+
rdrand %rdi
8+
jnc 1b
9+
call lkmc_print_hex_64
10+
call lkmc_print_newline
11+
LKMC_EPILOGUE

0 commit comments

Comments
 (0)