#include "efirng.h"
#include "efiboot.h"
static EFI_GUID RngProtocolGuid = EFI_RNG_PROTOCOL_GUID;
static EFI_GUID RngAlgorithmRawGuid = EFI_RNG_ALGORITHM_RAW;
static EFI_RNG_PROTOCOL *rng;
#ifndef EFIBOOT_DEBUG
#define DPRINT(...) __nothing
#else
#define DPRINT Print
#endif
static const struct {
EFI_GUID guid;
const CHAR16 *name;
} algname[] = {
{EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID,
L"NIST SP800-90 Hash_DRBG SHA-256"},
{EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID,
L"NIST SP800-90 HMAC_DRBG SHA-256"},
{EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID,
L"NIST SP800-90 CTR_DRBG AES-256"},
{EFI_RNG_ALGORITHM_X9_31_3DES_GUID, L"ANSI X9.31 3DES"},
{EFI_RNG_ALGORITHM_X9_31_AES_GUID, L"ANSI X9.31 AES"},
{EFI_RNG_ALGORITHM_RAW, L"raw"},
};
void
efi_rng_probe(void)
{
EFI_STATUS status;
status = LibLocateProtocol(&RngProtocolGuid, (void **)&rng);
if (EFI_ERROR(status)) {
DPRINT(L"efirng: protocol: %r\n", status);
rng = NULL;
return;
}
}
void
efi_rng_show(void)
{
EFI_RNG_ALGORITHM alglist[10];
UINTN i, j, alglistsz = sizeof(alglist);
EFI_STATUS status;
if (!efi_rng_available())
return;
command_printtab("RNG", "");
status = uefi_call_wrapper(rng->GetInfo, 3, rng, &alglistsz, alglist);
if (EFI_ERROR(status)) {
Print(L"GetInfo: %r\n", status);
return;
}
for (i = 0; i < alglistsz/sizeof(alglist[0]); i++) {
const CHAR16 *name = L"[unknown]";
for (j = 0; j < __arraycount(algname); j++) {
if (memcmp(&alglist[i], &algname[j].guid,
sizeof(EFI_GUID)) == 0) {
name = algname[j].name;
break;
}
}
Print(L"%s (%g)\n", name, &alglist[i]);
}
}
int
efi_rng_available(void)
{
return rng != NULL;
}
int
efi_rng(void *buf, UINTN len)
{
EFI_STATUS status;
if (!efi_rng_available())
return EIO;
status = uefi_call_wrapper(rng->GetRNG, 4, rng, &RngAlgorithmRawGuid,
len, buf);
if (status == EFI_UNSUPPORTED) {
status = uefi_call_wrapper(rng->GetRNG, 4, rng, NULL, len, buf);
}
if (EFI_ERROR(status)) {
DPRINT(L"efirng: GetRNG: %r\n", status);
return EIO;
}
return 0;
}