Crypto++  8.8
Free C++ class library of cryptographic schemes
rdrand.asm
1 ;; rdrand.asm - written and placed in public domain by Jeffrey Walton and Uri Blumenthal.
2 ;; Copyright assigned to the Crypto++ project.
3 
4 ;; This ASM file provides RDRAND to downlevel Microsoft tool chains.
5 ;; Everything "just works" under Visual Studio. Other platforms will
6 ;; have to run MASM/MASM-64 and then link to the object files.
7 
8 ;; set ASFLAGS=/nologo /D_M_X86 /W3 /Cx /Zi /safeseh
9 ;; set ASFLAGS64=/nologo /D_M_X64 /W3 /Cx /Zi
10 ;; "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\ml.exe" %ASFLAGS% /Fo rdrand-x86.obj /c rdrand.asm
11 ;; "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\amd64\ml64.exe" %ASFLAGS64% /Fo rdrand-x64.obj /c rdrand.asm
12 
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 
16 TITLE MASM_RDRAND_GenerateBlock source file
17 SUBTITLE Microsoft specific ASM code to utilize RDRAND for down level Microsoft toolchains
18 
19 PUBLIC MASM_RDRAND_GenerateBlock
20 
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23 
24 ;; C/C++ Function prototypes (both are fastcall)
25 ;; X86:
26 ;; extern "C" void __fastcall MASM_RDRAND_GenerateBlock(byte* ptr, size_t size);
27 ;; X64:
28 ;; extern "C" void __fastcall MASM_RDRAND_GenerateBlock(byte* ptr, size_t size);
29 
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 
33 IFDEF _M_X86 ;; Set via the command line
34 
35 .486
36 .MODEL FLAT
37 
38 ;; Fastcall calling conventions exports
39 ALIAS <@MASM_RDRAND_GenerateBlock@8> = <MASM_RDRAND_GenerateBlock>
40 
41 ENDIF
42 
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 
46 IFDEF _M_X86 ;; Set via the command line
47 
48 .CODE
49 ALIGN 8
50 OPTION PROLOGUE:NONE
51 OPTION EPILOGUE:NONE
52 
53 ;; No need for Load_Arguments due to fastcall
54 ;; ECX (in): arg1, byte* buffer
55 ;; EDX (in): arg2, size_t bsize
56 
57 MASM_RDRAND_GenerateBlock PROC ;; arg1:DWORD, arg2:DWORD
58 
59  MWSIZE EQU 04h ;; machine word size
60  buffer EQU ecx
61  bsize EQU edx
62 
63  ;; Top of While loop
64 RDRAND_GenerateBlock_Top:
65 
66  ;; Check remaining size
67  cmp bsize, 0
68  je RDRAND_GenerateBlock_Return
69 
70 RDRAND_Call_EAX:
71  ;; RDRAND is not available prior to VS2012. Just emit
72  ;; the byte codes using DB. This is `rdrand eax`.
73  DB 0Fh, 0C7h, 0F0h
74 
75  ;; If CF=1, the number returned by RDRAND is valid.
76  ;; If CF=0, a random number was not available.
77 
78  ;; Retry immediately
79  jnc RDRAND_Call_EAX
80 
81 RDRAND_succeeded:
82 
83  cmp bsize, MWSIZE
84  jb RDRAND_Partial_Machine_Word
85 
86 RDRAND_Full_Machine_Word:
87 
88  mov DWORD PTR [buffer], eax
89  add buffer, MWSIZE ;; No need for Intel Core 2 slow workarounds, like
90  sub bsize, MWSIZE ;; `lea buffer,[buffer+MWSIZE]` for faster adds
91 
92  ;; Continue
93  jmp RDRAND_GenerateBlock_Top
94 
95  ;; 1,2,3 bytes remain
96 RDRAND_Partial_Machine_Word:
97 
98  ;; Test bit 1 to see if size is at least 2
99  test bsize, 2
100  jz RDRAND_Bit_1_Not_Set
101 
102  mov WORD PTR [buffer], ax
103  shr eax, 16
104  add buffer, 2
105 
106 RDRAND_Bit_1_Not_Set:
107 
108  ;; Test bit 0 to see if size is at least 1
109  test bsize, 1
110  jz RDRAND_Bit_0_Not_Set
111 
112  mov BYTE PTR [buffer], al
113  ;; shr ax, 8
114  ;; add buffer, 1
115 
116 RDRAND_Bit_0_Not_Set:
117 
118  ;; We've hit all the bits
119 
120 RDRAND_GenerateBlock_Return:
121 
122  ;; Clear artifacts
123  xor eax, eax
124  ret
125 
126 MASM_RDRAND_GenerateBlock ENDP
127 
128 ENDIF ;; _M_X86
129 
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 
133 IFDEF _M_X64 ;; Set via the command line
134 
135 .CODE
136 ALIGN 16
137 OPTION PROLOGUE:NONE
138 OPTION EPILOGUE:NONE
139 
140 ;; No need for Load_Arguments due to fastcall
141 ;; RCX (in): arg1, byte* buffer
142 ;; RDX (in): arg2, size_t bsize
143 
144 MASM_RDRAND_GenerateBlock PROC ;; arg1:QWORD, arg2:QWORD
145 
146  MWSIZE EQU 08h ;; machine word size
147  buffer EQU rcx
148  bsize EQU rdx
149 
150  ;; Top of While loop
151 RDRAND_GenerateBlock_Top:
152 
153  ;; Check remaining size
154  cmp bsize, 0
155  je RDRAND_GenerateBlock_Return
156 
157 RDRAND_Call_RAX:
158  ;; RDRAND is not available prior to VS2012. Just emit
159  ;; the byte codes using DB. This is `rdrand rax`.
160  DB 048h, 0Fh, 0C7h, 0F0h
161 
162  ;; If CF=1, the number returned by RDRAND is valid.
163  ;; If CF=0, a random number was not available.
164 
165  ;; Retry immediately
166  jnc RDRAND_Call_RAX
167 
168 RDRAND_succeeded:
169 
170  cmp bsize, MWSIZE
171  jb RDRAND_Partial_Machine_Word
172 
173 RDRAND_Full_Machine_Word:
174 
175  mov QWORD PTR [buffer], rax
176  add buffer, MWSIZE
177  sub bsize, MWSIZE
178 
179  ;; Continue
180  jmp RDRAND_GenerateBlock_Top
181 
182  ;; 1,2,3,4,5,6,7 bytes remain
183 RDRAND_Partial_Machine_Word:
184 
185  ;; Test bit 2 to see if size is at least 4
186  test bsize, 4
187  jz RDRAND_Bit_2_Not_Set
188 
189  mov DWORD PTR [buffer], eax
190  shr rax, 32
191  add buffer, 4
192 
193 RDRAND_Bit_2_Not_Set:
194 
195  ;; Test bit 1 to see if size is at least 2
196  test bsize, 2
197  jz RDRAND_Bit_1_Not_Set
198 
199  mov WORD PTR [buffer], ax
200  shr eax, 16
201  add buffer, 2
202 
203 RDRAND_Bit_1_Not_Set:
204 
205  ;; Test bit 0 to see if size is at least 1
206  test bsize, 1
207  jz RDRAND_Bit_0_Not_Set
208 
209  mov BYTE PTR [buffer], al
210  ;; shr ax, 8
211  ;; add buffer, 1
212 
213 RDRAND_Bit_0_Not_Set:
214 
215  ;; We've hit all the bits
216 
217 RDRAND_GenerateBlock_Return:
218 
219  ;; Clear artifacts
220  xor rax, rax
221  ret
222 
223 MASM_RDRAND_GenerateBlock ENDP
224 
225 ENDIF ;; _M_X64
226 
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
229 
230 END