]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/X64/RdRand.nasm
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BaseLib / X64 / 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 64-bit platform.
13 ;
14 ; Notes:
15 ;
16 ;------------------------------------------------------------------------------
17
18 DEFAULT REL
19 SECTION .text
20
21 ;------------------------------------------------------------------------------
22 ; Generates a 16 bit random number through RDRAND instruction.
23 ; Return TRUE if Rand generated successfully, or FALSE if not.
24 ;
25 ; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
26 ;------------------------------------------------------------------------------
27 global ASM_PFX(InternalX86RdRand16)
28 ASM_PFX(InternalX86RdRand16):
29 ; rdrand ax ; generate a 16 bit RN into eax,
30 ; CF=1 if RN generated ok, otherwise CF=0
31 db 0xf, 0xc7, 0xf0 ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"
32 jc rn16_ok ; jmp if CF=1
33 xor rax, rax ; reg=0 if CF=0
34 ret ; return with failure status
35 rn16_ok:
36 mov [rcx], ax
37 mov rax, 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 rax, rax ; reg=0 if CF=0
53 ret ; return with failure status
54 rn32_ok:
55 mov [rcx], eax
56 mov rax, 1
57 ret
58
59 ;------------------------------------------------------------------------------
60 ; Generates a 64 bit random number through one RDRAND instruction.
61 ; Return TRUE if Rand generated successfully, or FALSE if not.
62 ;
63 ; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Random);
64 ;------------------------------------------------------------------------------
65 global ASM_PFX(InternalX86RdRand64)
66 ASM_PFX(InternalX86RdRand64):
67 ; rdrand rax ; generate a 64 bit RN into rax,
68 ; CF=1 if RN generated ok, otherwise CF=0
69 db 0x48, 0xf, 0xc7, 0xf0 ; rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)"
70 jc rn64_ok ; jmp if CF=1
71 xor rax, rax ; reg=0 if CF=0
72 ret ; return with failure status
73 rn64_ok:
74 mov [rcx], rax
75 mov rax, 1
76 ret
77