This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRandom.c
74 lines (61 loc) · 2.28 KB
/
Random.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/ShellCEntryLib.h>
#include <Library/ShellLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/EfiShell.h>
#include <Protocol/LoadedImage.h>
#include <Protocol/TcgService.h>
#include <IndustryStandard/UefiTcgPlatform.h>
#define NO_RANDOM_BYTES 24
#pragma pack(1)
typedef struct {
TPM_RQU_COMMAND_HDR Header;
UINT32 BytesRequested;
} TPM_COMMAND;
typedef struct {
TPM_RSP_COMMAND_HDR Header;
UINT32 RandomBytesSize;
UINT8 RandomBytes[NO_RANDOM_BYTES];
} TPM_RESPONSE;
#pragma pack()
EFI_STATUS
GetRandom(IN UINT32* Nounce)
{
EFI_STATUS Status = EFI_SUCCESS;
EFI_TCG_PROTOCOL *TcgProtocol;
EFI_GUID gEfiTcgProtocolGuid = EFI_TCG_PROTOCOL_GUID;
TPM_COMMAND InBuffer;
TPM_RESPONSE OutBuffer;
UINT32 InBufferSize;
UINT32 OutBufferSize;
Status = gBS->LocateProtocol( &gEfiTcgProtocolGuid,
NULL,
(VOID **) &TcgProtocol);
if (EFI_ERROR (Status)) {
Print(L"Failed to locate EFI_TCG_PROTOCOL [%d]\n", Status);
return Status;
}
InBufferSize = sizeof(TPM_COMMAND);
OutBufferSize = sizeof(TPM_RESPONSE);
InBuffer.Header.tag = SwapBytes16(TPM_TAG_RQU_COMMAND);
InBuffer.Header.paramSize = SwapBytes32(InBufferSize);
InBuffer.Header.ordinal = SwapBytes32(TPM_ORD_GetRandom);
InBuffer.BytesRequested = SwapBytes32(NO_RANDOM_BYTES);
Status = TcgProtocol->PassThroughToTpm( TcgProtocol,
InBufferSize,
(UINT8 *)&InBuffer,
OutBufferSize,
(UINT8 *)&OutBuffer);
if (EFI_ERROR (Status)) {
Print(L"ERROR: PassThroughToTpm failed [%d]\n", Status);
return Status;
}
if ((OutBuffer.Header.tag != SwapBytes16 (TPM_TAG_RSP_COMMAND)) || (OutBuffer.Header.returnCode != 0)) {
Print(L"ERROR: TPM command result [%d]\n", SwapBytes32(OutBuffer.Header.returnCode));
return EFI_DEVICE_ERROR;
}
*Nounce = SwapBytes32(*((UINT32*)OutBuffer.RandomBytes));
return Status;
}