2018-06-27 15:13:09 +02:00
|
|
|
/** @file
|
2013-11-19 02:49:49 +01:00
|
|
|
RNG Driver to produce the UEFI Random Number Generator protocol.
|
|
|
|
|
2021-05-10 23:53:08 +02:00
|
|
|
The driver uses CPU RNG instructions to produce high-quality,
|
|
|
|
high-performance entropy and random number.
|
2013-11-19 02:49:49 +01:00
|
|
|
|
2020-02-07 02:08:15 +01:00
|
|
|
RNG Algorithms defined in UEFI 2.4:
|
2021-05-10 23:53:08 +02:00
|
|
|
- EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
|
|
|
|
- EFI_RNG_ALGORITHM_RAW
|
|
|
|
- EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID
|
|
|
|
- EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID
|
|
|
|
- EFI_RNG_ALGORITHM_X9_31_3DES_GUID
|
|
|
|
- EFI_RNG_ALGORITHM_X9_31_AES_GUID
|
2013-11-19 02:49:49 +01:00
|
|
|
|
2018-06-27 15:13:09 +02:00
|
|
|
Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
|
2015-10-09 08:03:26 +02:00
|
|
|
(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
|
2021-05-10 23:53:08 +02:00
|
|
|
|
2019-04-04 01:06:56 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2013-11-19 02:49:49 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
2021-05-10 23:53:08 +02:00
|
|
|
#include <Library/BaseLib.h>
|
|
|
|
#include <Library/BaseMemoryLib.h>
|
|
|
|
#include <Library/UefiBootServicesTableLib.h>
|
|
|
|
#include <Library/RngLib.h>
|
|
|
|
#include <Library/TimerLib.h>
|
|
|
|
#include <Protocol/Rng.h>
|
2013-11-19 02:49:49 +01:00
|
|
|
|
2021-05-10 23:53:08 +02:00
|
|
|
#include "RngDxeInternals.h"
|
2013-11-19 02:49:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns information about the random number generation implementation.
|
|
|
|
|
|
|
|
@param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
|
2018-06-27 15:13:09 +02:00
|
|
|
@param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
|
2013-11-19 02:49:49 +01:00
|
|
|
On output with a return code of EFI_SUCCESS, the size
|
|
|
|
in bytes of the data returned in RNGAlgorithmList. On output
|
|
|
|
with a return code of EFI_BUFFER_TOO_SMALL,
|
|
|
|
the size of RNGAlgorithmList required to obtain the list.
|
|
|
|
@param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
|
|
|
|
with one EFI_RNG_ALGORITHM element for each supported
|
|
|
|
RNG algorithm. The list must not change across multiple
|
|
|
|
calls to the same driver. The first algorithm in the list
|
|
|
|
is the default algorithm for the driver.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The RNG algorithm list was returned successfully.
|
|
|
|
@retval EFI_UNSUPPORTED The services is not supported by this driver.
|
|
|
|
@retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
|
|
|
|
hardware or firmware error.
|
|
|
|
@retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
|
|
|
|
@retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
RngGetInfo (
|
2021-12-05 23:54:12 +01:00
|
|
|
IN EFI_RNG_PROTOCOL *This,
|
|
|
|
IN OUT UINTN *RNGAlgorithmListSize,
|
|
|
|
OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
|
2013-11-19 02:49:49 +01:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:12 +01:00
|
|
|
EFI_STATUS Status;
|
2013-11-19 02:49:49 +01:00
|
|
|
|
|
|
|
if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-05-10 23:53:08 +02:00
|
|
|
// Return algorithm list supported by driver.
|
2013-11-19 02:49:49 +01:00
|
|
|
//
|
2021-05-10 23:53:08 +02:00
|
|
|
if (RNGAlgorithmList != NULL) {
|
|
|
|
Status = ArchGetSupportedRngAlgorithms (RNGAlgorithmListSize, RNGAlgorithmList);
|
|
|
|
} else {
|
|
|
|
Status = EFI_INVALID_PARAMETER;
|
2013-11-19 02:49:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// The Random Number Generator (RNG) protocol
|
|
|
|
//
|
2021-12-05 23:54:12 +01:00
|
|
|
EFI_RNG_PROTOCOL mRngRdRand = {
|
2013-11-19 02:49:49 +01:00
|
|
|
RngGetInfo,
|
|
|
|
RngGetRNG
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
The user Entry Point for the Random Number Generator (RNG) driver.
|
|
|
|
|
|
|
|
@param[in] ImageHandle The firmware allocated handle for the EFI image.
|
|
|
|
@param[in] SystemTable A pointer to the EFI System Table.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The entry point is executed successfully.
|
|
|
|
@retval EFI_NOT_SUPPORTED Platform does not support RNG.
|
|
|
|
@retval Other Some error occurs when executing this entry point.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
RngDriverEntry (
|
2021-12-05 23:54:12 +01:00
|
|
|
IN EFI_HANDLE ImageHandle,
|
|
|
|
IN EFI_SYSTEM_TABLE *SystemTable
|
2013-11-19 02:49:49 +01:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:12 +01:00
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_HANDLE Handle;
|
2013-11-19 02:49:49 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Install UEFI RNG (Random Number Generator) Protocol
|
|
|
|
//
|
|
|
|
Handle = NULL;
|
|
|
|
Status = gBS->InstallMultipleProtocolInterfaces (
|
|
|
|
&Handle,
|
|
|
|
&gEfiRngProtocolGuid,
|
|
|
|
&mRngRdRand,
|
|
|
|
NULL
|
|
|
|
);
|
2015-10-09 08:03:26 +02:00
|
|
|
|
2013-11-19 02:49:49 +01:00
|
|
|
return Status;
|
|
|
|
}
|
2021-05-10 23:53:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Calls RDRAND to fill a buffer of arbitrary size with random bytes.
|
|
|
|
|
|
|
|
@param[in] Length Size of the buffer, in bytes, to fill with.
|
|
|
|
@param[out] RandBuffer Pointer to the buffer to store the random result.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS Random bytes generation succeeded.
|
|
|
|
@retval EFI_NOT_READY Failed to request random bytes.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
RngGetBytes (
|
2021-12-05 23:54:12 +01:00
|
|
|
IN UINTN Length,
|
|
|
|
OUT UINT8 *RandBuffer
|
2021-05-10 23:53:08 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:12 +01:00
|
|
|
BOOLEAN IsRandom;
|
|
|
|
UINT64 TempRand[2];
|
2021-05-10 23:53:08 +02:00
|
|
|
|
|
|
|
while (Length > 0) {
|
|
|
|
IsRandom = GetRandomNumber128 (TempRand);
|
|
|
|
if (!IsRandom) {
|
|
|
|
return EFI_NOT_READY;
|
|
|
|
}
|
2021-12-05 23:54:12 +01:00
|
|
|
|
2021-05-10 23:53:08 +02:00
|
|
|
if (Length >= sizeof (TempRand)) {
|
2021-12-05 23:54:12 +01:00
|
|
|
WriteUnaligned64 ((UINT64 *)RandBuffer, TempRand[0]);
|
2021-05-10 23:53:08 +02:00
|
|
|
RandBuffer += sizeof (UINT64);
|
2021-12-05 23:54:12 +01:00
|
|
|
WriteUnaligned64 ((UINT64 *)RandBuffer, TempRand[1]);
|
2021-05-10 23:53:08 +02:00
|
|
|
RandBuffer += sizeof (UINT64);
|
2021-12-05 23:54:12 +01:00
|
|
|
Length -= sizeof (TempRand);
|
2021-05-10 23:53:08 +02:00
|
|
|
} else {
|
|
|
|
CopyMem (RandBuffer, TempRand, Length);
|
|
|
|
Length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|