crypto_baremetal.c (1250B)
1 /* Bare metal support functions for crypto library */ 2 #include <stdint.h> 3 4 /* RDTSC for x86 - CPU cycle counter */ 5 uint64_t baremetal_get_cycles(void) { 6 uint32_t lo, hi; 7 __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi)); 8 return ((uint64_t)hi << 32) | lo; 9 } 10 11 /* Use TSC for monotonic ticks (simple implementation) */ 12 uint64_t baremetal_get_ticks(void) { 13 /* In a real implementation, you'd calibrate TSC frequency 14 * For now, just return CPU cycles */ 15 return baremetal_get_cycles(); 16 } 17 18 /* Read CPU temperature from MSR (Model-Specific Register) */ 19 /* Returns temperature reading or 0 if unavailable */ 20 /* NOTE: RDMSR requires CPL 0 and may not work in all environments (e.g., QEMU) */ 21 /* For now, return 0 to avoid General Protection Faults */ 22 uint64_t baremetal_get_cpu_temp(void) { 23 /* TODO: Implement proper MSR reading with exception handling 24 * For now, just return a pseudo-random value based on TSC 25 * This still provides some entropy variation */ 26 uint64_t cycles1 = baremetal_get_cycles(); 27 /* Small delay */ 28 for (volatile int i = 0; i < 100; i++); 29 uint64_t cycles2 = baremetal_get_cycles(); 30 31 /* Return XOR of two cycle readings for timing-based entropy */ 32 return cycles1 ^ cycles2; 33 }