]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Ia32/RdRand.nasm
d818b6ef556824a03f4e3fb50a8c7c08e4747176
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / RdRand.nasm
1 ;------------------------------------------------------------------------------
2 ;
3 ; Copyright (c) 2015 - 2022, 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 eax ; generate a 16 bit RN into ax
29 ; CF=1 if RN generated ok, otherwise CF=0
30 jc rn16_ok ; jmp if CF=1
31 xor eax, eax ; reg=0 if CF=0
32 ret ; return with failure status
33 rn16_ok:
34 mov edx, dword [esp + 4]
35 mov [edx], ax
36 mov eax, 1
37 ret
38
39 ;------------------------------------------------------------------------------
40 ; Generates a 32 bit random number through RDRAND instruction.
41 ; Return TRUE if Rand generated successfully, or FALSE if not.
42 ;
43 ; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
44 ;------------------------------------------------------------------------------
45 global ASM_PFX(InternalX86RdRand32)
46 ASM_PFX(InternalX86RdRand32):
47 rdrand eax ; generate a 32 bit RN into eax
48 ; CF=1 if RN generated ok, otherwise CF=0
49 jc rn32_ok ; jmp if CF=1
50 xor eax, eax ; reg=0 if CF=0
51 ret ; return with failure status
52 rn32_ok:
53 mov edx, dword [esp + 4]
54 mov [edx], eax
55 mov eax, 1
56 ret
57
58 ;------------------------------------------------------------------------------
59 ; Generates a 64 bit random number through RDRAND instruction.
60 ; Return TRUE if Rand generated successfully, or FALSE if not.
61 ;
62 ; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);
63 ;------------------------------------------------------------------------------
64 global ASM_PFX(InternalX86RdRand64)
65 ASM_PFX(InternalX86RdRand64):
66 rdrand eax ; generate a 32 bit RN into eax
67 ; CF=1 if RN generated ok, otherwise CF=0
68 jnc rn64_ret ; jmp if CF=0
69 mov edx, dword [esp + 4]
70 mov [edx], eax
71
72 rdrand eax ; generate another 32 bit RN
73 jnc rn64_ret ; jmp if CF=0
74 mov [edx + 4], eax
75
76 mov eax, 1
77 ret
78 rn64_ret:
79 xor eax, eax
80 ret ; return with failure status
81