Page 1 of 2

RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 11:49
by Aacini
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.

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

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 11:59
by Squashman
Thought for sure it would work on my computer but it did not not.
Dell Latitude E5450
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
Processor: Intel(R) Core(TM) i5-5300U CPU @ 2.30GHz (4 CPUs), ~2.3GHz

I should add that the program completely crashes on this computer.

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 12:24
by SirJosh3917
I'm simply amazed you know assembly, I want to learn that language badly :shock:
Do you have any links to guides I can see to learn assembly?

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 13:00
by Squashman
I tested on one of my servers. Program does not crash but also does not output anything.

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 13:40
by Aacini
@SirJosh3917,

There are tons of info about Intel x86 assembly language in the web and in books. I can't recommend anyone of they because I learned assembly language about 30 years ago! Perhaps you may be interested in this thread.

PS - Did my RdRand.exe program correctly run in your computer?



Squashman wrote:Thought for sure it would work on my computer but it did not not.
...

I should add that the program completely crashes on this computer.

Could you give a more precise information? A pop-up box with "Windows encountered a problem..." appears?

Squashman wrote:I tested on one of my servers. Program does not crash but also does not output anything.

Remember that RdRand.exe returns the random number via ERRORLEVEL, so you must type ECHO %ERRORLEVEL% after it...


Antonio

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 13:49
by Squashman
Aacini wrote:Could you give a more precise information? A pop-up box with "Windows encountered a problem..." appears?

Yes. I got that. Hard to really determine what the message means. Just asks to send the reports to M$.

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 14:05
by jfl
This works for me on my laptop, thanks.

I had to make the following changes, so that the code builds with the ML.EXE assembler shipped with Visual Studio 2013:

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

   .model flat, stdcall
   option casemap :none

   extrn ExitProcess@4 : PROC

    .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   eax         ;get a 32-bits random number in EAX
   jc   returnRand      ;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
   jmp   terminate      ;      and terminate
   ;
returnRand:
   and   eax, 0FFFFH      ;clear 16 high-order bits (number 0-65535)
   ;
terminate:
   push   eax         ;pass EAX as parameter for ExitProcess
   call   ExitProcess@4    ;and return it as ERRORLEVEL
   end   start


To know if your processor supports the rdrand instruction, run my cpuid.exe tool, available in the SysTools.zip file released there:
https://github.com/JFLarvoire/SysToolsLib/releases

Code: Select all

cpuid -v

The rdrand support flag is ECX bit 30.

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 14:23
by SirJosh3917
@Aacini

Image

Can you point me towards the direction of AMD & Intel's guide for assembly?

Even if you learned it 30 years ago that's fine, what matters is you program in assembly.

EDIT: On the right is echo %errorlevel%, it just doesn't seem that way.

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 20 Oct 2016 23:43
by Aacini
:arrow: I found a small bug in the original code caused by a subtle inconsistence between the old MASM32 version I used and the new, current version. Please, delete your RdRand.* files and download again the .ZIP file at the first post of this thread, that now contains the fixed version.

Antonio

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 21 Oct 2016 02:20
by jfl
The code looks much better indeed with the proto directive, allowing to remove the ugly @4 decorations I had to add. Thanks.

As for the includlib directive, you should append the \masm32\lib pathname your LIB environment variable, and remove the absolute path in your source.

Code: Select all

        includelib      kernel32.lib

(On my system, I don't have this masm32 installed. Visual C++'s vcvars32.bat sets
LIB=C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\ATLMFC\LIB;C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x86;
and kernel32.lib is found in the last directory.)

Jean-Fran├žois, who did a lot of assembly language programming 20 to 30 years ago, but none ever since.

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 21 Oct 2016 02:22
by penpen
Actually your "RdRand.exe" returns 0 if the RDRAND instruction is not supported, which i think should be a bug.
You better should return a number outside the range of the generated random numbers [0 : 65535] for indicating RDRAND is not supported (for example -2).

Beside this, "RdRand.exe" is a nice tool.


penpen

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 21 Oct 2016 04:41
by misol101
Why are you looping? Is the random instruction only working on random occasions? :D

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 21 Oct 2016 07:15
by Squashman
Works on my laptop now.

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 22 Oct 2016 05:58
by aGerman
Aacini wrote: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.

The new version works (the old version didn't).
Intel(R) Atom(TM) CPU Z3735F @ 1.33GHz

Steffen

Re: RdRand.exe: Robust random numbers for Batch files

Posted: 22 Oct 2016 06:08
by ShadowThief
Working on my Intel i5-6600K