RdRand.exe: Robust random numbers for Batch files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

RdRand.exe: Robust random numbers for Batch files

#1 Post by Aacini » 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.

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
Attachments
RdRand.zip
Assembly source code and executable versions of RdRand program, with a small bug fixed.
(1.13 KiB) Downloaded 1048 times

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 20 Oct 2016 11:59

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.

SirJosh3917
Posts: 36
Joined: 02 May 2016 18:59

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

#3 Post by SirJosh3917 » 20 Oct 2016 12:24

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?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#4 Post by Squashman » 20 Oct 2016 13:00

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#5 Post by Aacini » 20 Oct 2016 13:40

@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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#6 Post by Squashman » 20 Oct 2016 13:49

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$.

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

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

#7 Post by jfl » 20 Oct 2016 14:05

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.

SirJosh3917
Posts: 36
Joined: 02 May 2016 18:59

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

#8 Post by SirJosh3917 » 20 Oct 2016 14:23

@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.
Last edited by SirJosh3917 on 20 Oct 2016 20:00, edited 1 time in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#9 Post by Aacini » 20 Oct 2016 23:43

: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

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

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

#10 Post by jfl » 21 Oct 2016 02:20

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.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

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

#11 Post by penpen » 21 Oct 2016 02:22

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

misol101
Posts: 475
Joined: 02 May 2016 18:20

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

#12 Post by misol101 » 21 Oct 2016 04:41

Why are you looping? Is the random instruction only working on random occasions? :D

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#13 Post by Squashman » 21 Oct 2016 07:15

Works on my laptop now.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#14 Post by aGerman » 22 Oct 2016 05:58

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

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#15 Post by ShadowThief » 22 Oct 2016 06:08

Working on my Intel i5-6600K

Post Reply