Patrick Georgi | 11f0079 | 2020-03-04 15:10:45 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Robbie Zhang | 1879231 | 2017-02-13 13:44:14 -0800 | [diff] [blame] | 2 | |
| 3 | #include <random.h> |
Robbie Zhang | 1879231 | 2017-02-13 13:44:14 -0800 | [diff] [blame] | 4 | |
| 5 | /* |
| 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. |
| 10 | */ |
| 11 | #define RDRAND_RETRY_LOOPS 10 |
| 12 | |
| 13 | /* |
| 14 | * Generate a 32-bit random number through RDRAND instruction. |
| 15 | * Carry flag is set on RDRAND success and 0 on failure. |
| 16 | */ |
| 17 | static inline uint8_t rdrand_32(uint32_t *rand) |
| 18 | { |
| 19 | uint8_t carry; |
| 20 | |
| 21 | __asm__ __volatile__( |
| 22 | ".byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1" |
| 23 | : "=a" (*rand), "=qm" (carry)); |
| 24 | return carry; |
| 25 | } |
| 26 | |
Stefan Reinauer | 0380e0a | 2017-06-21 12:40:15 -0700 | [diff] [blame] | 27 | #if ENV_X86_64 |
Robbie Zhang | 1879231 | 2017-02-13 13:44:14 -0800 | [diff] [blame] | 28 | /* |
| 29 | * Generate a 64-bit random number through RDRAND instruction. |
| 30 | * Carry flag is set on RDRAND success and 0 on failure. |
| 31 | */ |
| 32 | static inline uint8_t rdrand_64(uint64_t *rand) |
| 33 | { |
| 34 | uint8_t carry; |
| 35 | |
| 36 | __asm__ __volatile__( |
| 37 | ".byte 0x48; .byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1" |
| 38 | : "=a" (*rand), "=qm" (carry)); |
| 39 | return carry; |
| 40 | } |
Stefan Reinauer | 0380e0a | 2017-06-21 12:40:15 -0700 | [diff] [blame] | 41 | #endif |
Robbie Zhang | 1879231 | 2017-02-13 13:44:14 -0800 | [diff] [blame] | 42 | |
| 43 | int get_random_number_32(uint32_t *rand) |
| 44 | { |
| 45 | int i; |
| 46 | |
| 47 | /* Perform a loop call until RDRAND succeeds or returns failure. */ |
| 48 | for (i = 0; i < RDRAND_RETRY_LOOPS; i++) { |
| 49 | if (rdrand_32(rand)) |
| 50 | return 0; |
| 51 | } |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | int get_random_number_64(uint64_t *rand) |
| 56 | { |
| 57 | int i; |
| 58 | uint32_t rand_high, rand_low; |
| 59 | |
| 60 | /* Perform a loop call until RDRAND succeeds or returns failure. */ |
| 61 | for (i = 0; i < RDRAND_RETRY_LOOPS; i++) { |
Stefan Reinauer | 0380e0a | 2017-06-21 12:40:15 -0700 | [diff] [blame] | 62 | #if ENV_X86_64 |
| 63 | if (rdrand_64(rand)) |
Robbie Zhang | 1879231 | 2017-02-13 13:44:14 -0800 | [diff] [blame] | 64 | return 0; |
Stefan Reinauer | 0380e0a | 2017-06-21 12:40:15 -0700 | [diff] [blame] | 65 | #endif |
| 66 | if (rdrand_32(&rand_high) && rdrand_32(&rand_low)) { |
Robbie Zhang | 1879231 | 2017-02-13 13:44:14 -0800 | [diff] [blame] | 67 | *rand = ((uint64_t)rand_high << 32) | |
| 68 | (uint64_t)rand_low; |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | return -1; |
| 73 | } |