]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
SecurityPkg/RngDxe: Documentation/include/parameter cleanup
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / RngDxe.c
1 /** @file
2 RNG Driver to produce the UEFI Random Number Generator protocol.
3
4 The driver uses CPU RNG instructions to produce high-quality,
5 high-performance entropy and random number.
6
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
14
15 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
16 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
17
18 SPDX-License-Identifier: BSD-2-Clause-Patent
19
20 **/
21
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>
27
28 #include "RngDxeInternals.h"
29
30 //
31 // The Random Number Generator (RNG) protocol
32 //
33 EFI_RNG_PROTOCOL mRngRdRand = {
34 RngGetInfo,
35 RngGetRNG
36 };
37
38 /**
39 The user Entry Point for the Random Number Generator (RNG) driver.
40
41 @param[in] ImageHandle The firmware allocated handle for the EFI image.
42 @param[in] SystemTable A pointer to the EFI System Table.
43
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.
47
48 **/
49 EFI_STATUS
50 EFIAPI
51 RngDriverEntry (
52 IN EFI_HANDLE ImageHandle,
53 IN EFI_SYSTEM_TABLE *SystemTable
54 )
55 {
56 EFI_STATUS Status;
57 EFI_HANDLE Handle;
58
59 //
60 // Install UEFI RNG (Random Number Generator) Protocol
61 //
62 Handle = NULL;
63 Status = gBS->InstallMultipleProtocolInterfaces (
64 &Handle,
65 &gEfiRngProtocolGuid,
66 &mRngRdRand,
67 NULL
68 );
69
70 return Status;
71 }
72
73 /**
74 Runs CPU RNG instruction to fill a buffer of arbitrary size with random bytes.
75
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.
78
79 @retval EFI_SUCCESS Random bytes generation succeeded.
80 @retval EFI_NOT_READY Failed to request random bytes.
81
82 **/
83 EFI_STATUS
84 EFIAPI
85 RngGetBytes (
86 IN UINTN Length,
87 OUT UINT8 *RandBuffer
88 )
89 {
90 BOOLEAN IsRandom;
91 UINT64 TempRand[2];
92
93 while (Length > 0) {
94 IsRandom = GetRandomNumber128 (TempRand);
95 if (!IsRandom) {
96 return EFI_NOT_READY;
97 }
98
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);
105 } else {
106 CopyMem (RandBuffer, TempRand, Length);
107 Length = 0;
108 }
109 }
110
111 return EFI_SUCCESS;
112 }