]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseRngLib/Rand/RdRand.c
09fb875ac3f95185c4bdb8a1ae563677830e8177
[mirror_edk2.git] / MdePkg / Library / BaseRngLib / Rand / RdRand.c
1 /** @file
2 Random number generator services that uses RdRand instruction access
3 to provide high-quality random numbers.
4
5 Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include <Uefi.h>
13 #include <Library/BaseLib.h>
14 #include <Library/DebugLib.h>
15
16 #include "BaseRngLibInternals.h"
17
18 //
19 // Bit mask used to determine if RdRand instruction is supported.
20 //
21 #define RDRAND_MASK BIT30
22
23
24 STATIC BOOLEAN mRdRandSupported;
25
26 /**
27 The constructor function checks whether or not RDRAND instruction is supported
28 by the host hardware.
29
30 The constructor function checks whether or not RDRAND instruction is supported.
31 It will ASSERT() if RDRAND instruction is not supported.
32 It will always return EFI_SUCCESS.
33
34 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
35
36 **/
37 EFI_STATUS
38 EFIAPI
39 BaseRngLibConstructor (
40 VOID
41 )
42 {
43 UINT32 RegEcx;
44
45 //
46 // Determine RDRAND support by examining bit 30 of the ECX register returned by
47 // CPUID. A value of 1 indicates that processor support RDRAND instruction.
48 //
49 AsmCpuid (1, 0, 0, &RegEcx, 0);
50 ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK);
51
52 mRdRandSupported = ((RegEcx & RDRAND_MASK) == RDRAND_MASK);
53
54 return EFI_SUCCESS;
55 }
56
57 /**
58 Generates a 16-bit random number.
59
60 @param[out] Rand Buffer pointer to store the 16-bit random value.
61
62 @retval TRUE Random number generated successfully.
63 @retval FALSE Failed to generate the random number.
64
65 **/
66 BOOLEAN
67 EFIAPI
68 ArchGetRandomNumber16 (
69 OUT UINT16 *Rand
70 )
71 {
72 return AsmRdRand16 (Rand);
73 }
74
75 /**
76 Generates a 32-bit random number.
77
78 @param[out] Rand Buffer pointer to store the 32-bit random value.
79
80 @retval TRUE Random number generated successfully.
81 @retval FALSE Failed to generate the random number.
82
83 **/
84 BOOLEAN
85 EFIAPI
86 ArchGetRandomNumber32 (
87 OUT UINT32 *Rand
88 )
89 {
90 return AsmRdRand32 (Rand);
91 }
92
93 /**
94 Generates a 64-bit random number.
95
96 @param[out] Rand Buffer pointer to store the 64-bit random value.
97
98 @retval TRUE Random number generated successfully.
99 @retval FALSE Failed to generate the random number.
100
101 **/
102 BOOLEAN
103 EFIAPI
104 ArchGetRandomNumber64 (
105 OUT UINT64 *Rand
106 )
107 {
108 return AsmRdRand64 (Rand);
109 }
110
111 /**
112 Checks whether RDRAND is supported.
113
114 @retval TRUE RDRAND is supported.
115 @retval FALSE RDRAND is not supported.
116
117 **/
118 BOOLEAN
119 EFIAPI
120 ArchIsRngSupported (
121 VOID
122 )
123 {
124 /*
125 Existing software depends on this always returning TRUE, so for
126 now hard-code it.
127
128 return mRdRandSupported;
129 */
130 return TRUE;
131 }