]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Ia32/RdRand.nasm
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / RdRand.nasm
1 ;------------------------------------------------------------------------------
2 ;
3 ; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
4 ; SPDX-License-Identifier: BSD-2-Clause-Patent
5 ;
6 ; Module Name:
7 ;
8 ; RdRand.nasm
9 ;
10 ; Abstract:
11 ;
12 ; Generates random number through CPU RdRand instruction under 32-bit platform.
13 ;
14 ; Notes:
15 ;
16 ;------------------------------------------------------------------------------
17
18 SECTION .text
19
20 ;------------------------------------------------------------------------------
21 ; Generates a 16 bit random number through RDRAND instruction.
22 ; Return TRUE if Rand generated successfully, or FALSE if not.
23 ;
24 ; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
25 ;------------------------------------------------------------------------------
26 global ASM_PFX(InternalX86RdRand16)
27 ASM_PFX(InternalX86RdRand16):
28 ; rdrand ax ; generate a 16 bit RN into ax
29 ; CF=1 if RN generated ok, otherwise CF=0
30 db 0xf, 0xc7, 0xf0 ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"
31 jc rn16_ok ; jmp if CF=1
32 xor eax, eax ; reg=0 if CF=0
33 ret ; return with failure status
34 rn16_ok:
35 mov edx, dword [esp + 4]
36 mov [edx], ax
37 mov eax, 1
38 ret
39
40 ;------------------------------------------------------------------------------
41 ; Generates a 32 bit random number through RDRAND instruction.
42 ; Return TRUE if Rand generated successfully, or FALSE if not.
43 ;
44 ; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
45 ;------------------------------------------------------------------------------
46 global ASM_PFX(InternalX86RdRand32)
47 ASM_PFX(InternalX86RdRand32):
48 ; rdrand eax ; generate a 32 bit RN into eax
49 ; CF=1 if RN generated ok, otherwise CF=0
50 db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"
51 jc rn32_ok ; jmp if CF=1
52 xor eax, eax ; reg=0 if CF=0
53 ret ; return with failure status
54 rn32_ok:
55 mov edx, dword [esp + 4]
56 mov [edx], eax
57 mov eax, 1
58 ret
59
60 ;------------------------------------------------------------------------------
61 ; Generates a 64 bit random number through RDRAND instruction.
62 ; Return TRUE if Rand generated successfully, or FALSE if not.
63 ;
64 ; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);
65 ;------------------------------------------------------------------------------
66 global ASM_PFX(InternalX86RdRand64)
67 ASM_PFX(InternalX86RdRand64):
68 ; rdrand eax ; generate a 32 bit RN into eax
69 ; CF=1 if RN generated ok, otherwise CF=0
70 db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"
71 jnc rn64_ret ; jmp if CF=0
72 mov edx, dword [esp + 4]
73 mov [edx], eax
74
75 db 0xf, 0xc7, 0xf0 ; generate another 32 bit RN
76 jnc rn64_ret ; jmp if CF=0
77 mov [edx + 4], eax
78
79 mov eax, 1
80 ret
81 rn64_ret:
82 xor eax, eax
83 ret ; return with failure status
84