2 RNG Driver to produce the UEFI Random Number Generator protocol.
4 The driver uses CPU RNG instructions to produce high-quality,
5 high-performance entropy and random number.
7 RNG Algorithms defined in UEFI 2.4:
8 - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
9 - EFI_RNG_ALGORITHM_RAW
10 - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID
11 - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID
12 - EFI_RNG_ALGORITHM_X9_31_3DES_GUID
13 - EFI_RNG_ALGORITHM_X9_31_AES_GUID
15 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
16 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
18 SPDX-License-Identifier: BSD-2-Clause-Patent
22 #include <Library/BaseLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
25 #include <Library/RngLib.h>
26 #include <Protocol/Rng.h>
28 #include "RngDxeInternals.h"
31 // The Random Number Generator (RNG) protocol
33 EFI_RNG_PROTOCOL mRngRdRand
= {
39 The user Entry Point for the Random Number Generator (RNG) driver.
41 @param[in] ImageHandle The firmware allocated handle for the EFI image.
42 @param[in] SystemTable A pointer to the EFI System Table.
44 @retval EFI_SUCCESS The entry point is executed successfully.
45 @retval EFI_NOT_SUPPORTED Platform does not support RNG.
46 @retval Other Some error occurs when executing this entry point.
52 IN EFI_HANDLE ImageHandle
,
53 IN EFI_SYSTEM_TABLE
*SystemTable
60 // Install UEFI RNG (Random Number Generator) Protocol
63 Status
= gBS
->InstallMultipleProtocolInterfaces (
74 Runs CPU RNG instruction to fill a buffer of arbitrary size with random bytes.
76 @param[in] Length Size of the buffer, in bytes, to fill with.
77 @param[out] RandBuffer Pointer to the buffer to store the random result.
79 @retval EFI_SUCCESS Random bytes generation succeeded.
80 @retval EFI_NOT_READY Failed to request random bytes.
94 IsRandom
= GetRandomNumber128 (TempRand
);
99 if (Length
>= sizeof (TempRand
)) {
100 WriteUnaligned64 ((UINT64
*)RandBuffer
, TempRand
[0]);
101 RandBuffer
+= sizeof (UINT64
);
102 WriteUnaligned64 ((UINT64
*)RandBuffer
, TempRand
[1]);
103 RandBuffer
+= sizeof (UINT64
);
104 Length
-= sizeof (TempRand
);
106 CopyMem (RandBuffer
, TempRand
, Length
);