rdseed 0.1.0-beta.11

Rust interface for RDRAND / RDSEED CPU instructions
Documentation
// Rust interface to RDSEED / RDRAND
//
// Written by Radim Kolar <hsn@sendmail.cz> 2026
//
// This is free and unencumbered software released into the public domain.
// SPDX-License-Identifier: CC0-1.0 OR Unlicense

/// Attempts to get a 64-bit pseudo random number from the hardware.
///
/// ## Returns
///   Some(u64) if successful.
///   None if the hardware was busy or operation is not supported.
///
/// ## See also
///
/// 1. [is_available] - checks if hardware supports RDRAND instruction.
/// 1. [get32] - get 32-bit pseudo random number from the hardware.
/// 1. [get16] - get 16-bit pseudo random number from the hardware.
pub fn get64() -> Option<u64> {
   #[cfg(target_arch = "x86_64")]
   {
      use std::arch::x86_64::_rdrand64_step;

      // Check if the CPU supports RDRAND at runtime is recommended,
      // but for the raw instruction call:
      let mut rand = 0u64;
      unsafe {
         // _rdrand64_step returns 1 if a valid value was obtained
         if _rdrand64_step(&mut rand) == 1 {
            return Some(rand);
         }
      }
   }
   None
}

/// Attempts to get a 32-bit pseudo random number from the hardware.
///
/// ## Returns
///   Some(u32) if successful.
///   None if the hardware was busy or operation is not supported.
///
/// ## See also
///
/// 1. [is_available] - checks if hardware supports RDRAND instruction.
/// 1. [get64] - get 64-bit pseudo random number from the hardware.
/// 1. [get16] - get 16-bit pseudo random number from the hardware.
pub fn get32() -> Option<u32> {
   #[cfg(target_arch = "x86_64")]
   {
      use std::arch::x86_64::_rdrand32_step;

      // Check if the CPU supports RDRAND at runtime is recommended,
      // but for the raw instruction call:
      let mut rand = 0u32;
      unsafe {
         // _rdrand32_step returns 1 if a valid value was obtained
         if _rdrand32_step(&mut rand) == 1 {
            return Some(rand);
         }
      }
   }
   None
}

/// Attempts to get a 16-bit pseudo random number from the hardware.
///
/// ## Returns
///   Some(u16) if successful.
///   None if the hardware was busy or operation is not supported.
///
/// ## See also
///
/// 1. [is_available] - checks if hardware supports RDRAND instruction.
/// 1. [get64] - get 64-bit pseudo random number from the hardware.
/// 1. [get32] - get 32-bit pseudo random number from the hardware.
pub fn get16() -> Option<u16> {
   #[cfg(target_arch = "x86_64")]
   {
      use std::arch::x86_64::_rdrand16_step;

      // Check if the CPU supports RDRAND at runtime is recommended,
      // but for the raw instruction call:
      let mut rand = 0u16;
      unsafe {
         // _rdrand16_step returns 1 if a valid value was obtained
         if _rdrand16_step(&mut rand) == 1 {
            return Some(rand);
         }
      }
   }
   None
}

/// Checks if hardware supports RDRAND instruction.
///
/// ## Returns
///   true if the current CPU supports the RDRAND instruction.
pub fn is_available() -> bool {
   #[cfg(target_arch = "x86_64")]
   {
      // This macro performs a runtime check using the CPUID instruction
      if is_x86_feature_detected!("rdrand") {
         return true;
      }
   }

   // Always return false if not on x86_64 or if feature is missing
   false
}