Guest User

Untitled

a guest
Nov 8th, 2025
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/ioctl.h>
  6. #include <asm-x86/msr.h>
  7.  
  8. #define MSR_ADDR 0xC0011002
  9.  
  10. // Function to read an MSR
  11. uint64_t read_msr(int fd, uint32_t msr) {
  12.     uint64_t value;
  13.     if (pread(fd, &value, sizeof(value), msr) != sizeof(value)) {
  14.         perror("Error reading MSR");
  15.         return -1;
  16.     }
  17.     return value;
  18. }
  19.  
  20. // Function to write an MSR
  21. void write_msr(int fd, uint32_t msr, uint64_t value) {
  22.     if (pwrite(fd, &value, sizeof(value), msr) != sizeof(value)) {
  23.         perror("Error writing MSR");
  24.     }
  25. }
  26.  
  27. // Function to modify bit 18 of MSR 0xC0011002
  28. void set_msr_bit_18(int fd, int set) {
  29.     uint64_t msr_value = read_msr(fd, MSR_ADDR);
  30.     if (msr_value == (uint64_t)-1) {
  31.         return; // Error reading MSR
  32.     }
  33.  
  34.     if (set) {
  35.         // Set bit 18
  36.         msr_value |= (1ULL << 18);
  37.     } else {
  38.         // Clear bit 18
  39.         msr_value &= ~(1ULL << 18);
  40.     }
  41.  
  42.     write_msr(fd, MSR_ADDR, msr_value);
  43. }
  44.  
  45. int supports_rdseed() {
  46.     uint32_t eax, ebx, ecx, edx;
  47.     __asm__ (
  48.         "cpuid"
  49.         : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
  50.         : "a" (0x7), "c" (0)
  51.     );
  52.    
  53.     return ebx; // Check bit 18 (RDSEED flag)
  54. }
  55.  
  56. uint32_t rdseed() {
  57.     uint32_t result;
  58.     // Inline assembly to use RDSEED instruction
  59.     __asm__ __volatile__ (
  60.         "rdseed %0" // RDSEED instruction, stores result in result
  61.         : "=r" (result) // Output operand
  62.         : // No input operands
  63.         : "memory" // Clobbered register
  64.     );
  65.     return result;
  66. }
  67.  
  68. int main()
  69. {
  70.     //you need to disable each core or limit program to run at one core
  71.    
  72.     int msr_fd = open("/dev/cpu/0/msr", O_RDWR);
  73.     if (msr_fd == -1) {
  74.         perror("Error opening msr");
  75.         return 1;
  76.     }
  77.  
  78.     printf("RDSEED flag 0x%08x %u\n", supports_rdseed() , (supports_rdseed() >> 18) & 1 );
  79.    
  80. //    set_msr_bit_18(msr_fd, 1);
  81.  
  82.     printf("RDSEED flag 0x%08x %u\n", supports_rdseed() , (supports_rdseed() >> 18) & 1 );
  83.  
  84.     set_msr_bit_18(msr_fd, 0);
  85.  
  86.     printf("RDSEED flag 0x%08x %u\n", supports_rdseed() , (supports_rdseed() >> 18) & 1 );
  87.  
  88.     uint32_t random_value = rdseed();
  89.     printf("Random number: %u\n", random_value);
  90.    
  91.     return 0;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment