rdseed/lib.rs
1// Rust interface to RDSEED / RDRAND
2//
3// Written by Radim Kolar <hsn@sendmail.cz> 2026
4//
5// SPDX-License-Identifier: CC0-1.0 OR Unlicense
6
7/// Attempts to get a 64-bit seed from the hardware.
8///
9/// ## Returns
10/// Some(u64) if successful
11/// None if the hardware was busy or operation is not supported
12///
13/// ## See also
14///
15/// 1. [is_available] - checks if hardware supports RDSEED instruction.
16/// 1. [get32] - get 32-bit seed from the hardware.
17/// 1. [get16] - get 16-bit seed from the hardware.
18pub fn get64() -> Option<u64> {
19 #[cfg(target_arch = "x86_64")]
20 {
21 use std::arch::x86_64::_rdseed64_step;
22
23 // Check if the CPU supports RDSEED at runtime is recommended,
24 // but for the raw instruction call:
25 let mut seed = 0u64;
26 unsafe {
27 // _rdseed64_step returns 1 if a valid value was obtained
28 if _rdseed64_step(&mut seed) == 1 {
29 return Some(seed);
30 }
31 }
32 }
33 None
34}
35
36/// Attempts to get a 32-bit seed from the hardware.
37///
38/// ## Returns
39/// Some(u32) if successful
40/// None if the hardware was busy or operation is not supported
41///
42/// ## See also
43///
44/// 1. [is_available] - checks if hardware supports RDSEED instruction.
45/// 1. [get64] - get 64-bit seed from the hardware.
46/// 1. [get16] - get 16-bit seed from the hardware.
47
48pub fn get32() -> Option<u32> {
49 #[cfg(target_arch = "x86_64")]
50 {
51 use std::arch::x86_64::_rdseed32_step;
52
53 // Check if the CPU supports RDSEED at runtime is recommended,
54 // but for the raw instruction call:
55 let mut seed = 0u32;
56 unsafe {
57 // _rdseed32_step returns 1 if a valid value was obtained
58 if _rdseed32_step(&mut seed) == 1 {
59 return Some(seed);
60 }
61 }
62 }
63 None
64}
65
66/// Attempts to get a 16-bit seed from the hardware.
67///
68/// ## Returns
69/// Some(u16) if successful
70/// None if the hardware was busy or operation is not supported
71///
72/// ## See also
73///
74/// 1. [is_available] - checks if hardware supports RDSEED instruction.
75/// 1. [get64] - get 64-bit seed from the hardware.
76/// 1. [get32] - get 32-bit seed from the hardware.
77pub fn get16() -> Option<u16> {
78 #[cfg(target_arch = "x86_64")]
79 {
80 use std::arch::x86_64::_rdseed16_step;
81
82 // Check if the CPU supports RDSEED at runtime is recommended,
83 // but for the raw instruction call:
84 let mut seed = 0u16;
85 unsafe {
86 // _rdseed16_step returns 1 if a valid value was obtained
87 if _rdseed16_step(&mut seed) == 1 {
88 return Some(seed);
89 }
90 }
91 }
92 None
93}
94
95
96/// Checks if hardware supports RDSEED instruction.
97///
98/// ## Returns
99/// true if the current CPU supports the RDSEED instruction.
100pub fn is_available() -> bool {
101 #[cfg(target_arch = "x86_64")]
102 {
103 // This macro performs a runtime check using the CPUID instruction
104 if is_x86_feature_detected!("rdseed") {
105 return true;
106 }
107 }
108
109 // Always return false if not on x86_64 or if feature is missing
110 false
111}
112
113#[path = "lib_rand.rs"]
114pub mod rand;