1 /* SPDX-License-Identifier: GPL-2.0-only */
6 * Intel recommends that applications attempt 10 retries in a tight loop
7 * in the unlikely event that the RDRAND instruction does not successfully
8 * return a random number. The odds of ten failures in a row would in fact
9 * be an indication of a larger CPU issue.
11 #define RDRAND_RETRY_LOOPS 10
14 * Generate a 32-bit random number through RDRAND instruction.
15 * Carry flag is set on RDRAND success and 0 on failure.
17 static inline uint8_t rdrand_32(uint32_t *rand
)
22 ".byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1"
23 : "=a" (*rand
), "=qm" (carry
));
29 * Generate a 64-bit random number through RDRAND instruction.
30 * Carry flag is set on RDRAND success and 0 on failure.
32 static inline uint8_t rdrand_64(uint64_t *rand
)
37 ".byte 0x48; .byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1"
38 : "=a" (*rand
), "=qm" (carry
));
43 int get_random_number_32(uint32_t *rand
)
47 /* Perform a loop call until RDRAND succeeds or returns failure. */
48 for (i
= 0; i
< RDRAND_RETRY_LOOPS
; i
++) {
55 int get_random_number_64(uint64_t *rand
)
58 uint32_t rand_high
, rand_low
;
60 /* Perform a loop call until RDRAND succeeds or returns failure. */
61 for (i
= 0; i
< RDRAND_RETRY_LOOPS
; i
++) {
66 if (rdrand_32(&rand_high
) && rdrand_32(&rand_low
)) {
67 *rand
= ((uint64_t)rand_high
<< 32) |