RdRand.exe: Robust random numbers for Batch files
Posted: 20 Oct 2016 11:49
The processors of the x86 family produced by Intel using the Ivy Bridge microarchitecture, that encompasses several high-end processors built from 2011 on (like Quad-core, Xeon, Core i7 and others) include an on-chip entropy source that allows to generate highly robust random numbers compliant with security and cryptographic standards. A random number is generated via RDRAND CPU instruction, that can be easily used in an assembly language program. AMD processors added support for the RDRAND instruction in June 2015.
Below there is an assembly language program that use RDRAND instruction to get a non-zero 16-bits random number, so the generated number is in the 1-65535 range; the number is returned via ERRORLEVEL. Technical details on this program and on the random number generator itself are fully explained in this Intel document. Both this program and the executable .EXE one are included in the .ZIP file.
EDIT 2016/10/21: A small bug in the original program was fixed. Please, delete your RdRand.* files and download the .ZIP file again.
The bad news now: as said before, the RDRAND instruction does NOT work in all computers, but just in the most expensive ones. This program does not work in my old-and-cheap laptop, so I have not means to know if RdRand.exe works correctly! If the CPU does not support the RDRAND instruction, RdRand.exe returns 0. I'll appreciate it if you may confirm that RdRand.exe program correctly return random numbers in any computer; please, include the CPU model if you know it.
Antonio
Below there is an assembly language program that use RDRAND instruction to get a non-zero 16-bits random number, so the generated number is in the 1-65535 range; the number is returned via ERRORLEVEL. Technical details on this program and on the random number generator itself are fully explained in this Intel document. Both this program and the executable .EXE one are included in the .ZIP file.
EDIT 2016/10/21: A small bug in the original program was fixed. Please, delete your RdRand.* files and download the .ZIP file again.
Code: Select all
;RdRand.asm: Return a random number generated by RDRAND CPU instruction
;https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide/
;Antonio Perez Ayala
.686
.model flat, stdcall
option casemap :none
ExitProcess PROTO STDCALL :DWORD
includelib \masm32\lib\kernel32.lib
.code
start:
mov eax, 1 ;value for CPUID: returns feature information in ECX
cpuid ;get CPUID value in ECX
mov eax, 0 ;initialize return value = 0
test ecx, 40000000H ;is RDRAND instruction supported? (bit 30)
jz terminate ;no: terminate
;
mov cl, 10 ;set max. num. of retries
;
getRand:
rdrand ax ;get a 16-bits random number in AX
jc terminate ;random number available? return it
;
dec cl ;decrement retries
jnz getRand ;and go back if not zero
mov eax, -1 ;else: indicate an error condition
;
terminate:
push eax ;pass EAX as parameter for ExitProcess
call ExitProcess ;and return it as ERRORLEVEL
end start
The bad news now: as said before, the RDRAND instruction does NOT work in all computers, but just in the most expensive ones. This program does not work in my old-and-cheap laptop, so I have not means to know if RdRand.exe works correctly! If the CPU does not support the RDRAND instruction, RdRand.exe returns 0. I'll appreciate it if you may confirm that RdRand.exe program correctly return random numbers in any computer; please, include the CPU model if you know it.
Antonio