Skip to content

Commit 98b60a4

Browse files
Add support for Measured Boot on ARM32-FirmwareTPM (canonical#53)
Signed-off-by: Javier Almansa Sobrino <[email protected]>
1 parent 1b35000 commit 98b60a4

File tree

9 files changed

+1089
-26
lines changed

9 files changed

+1089
-26
lines changed

Samples/ARM32-FirmwareTPM/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ See the [optee_os documentation](https://github.com/OP-TEE/optee_os/blob/master/
3030
See instructions [here](https://docs.microsoft.com/en-us/windows/wsl/install-win10):
3131

3232
#### 2. Launch Bash
33-
Search for "bash" in the start menu, OR press Windows key + 'R', then type bash.
33+
Search for "bash" in the start menu, OR press Windows key + 'R', then type bash.
3434
Update if needed.
3535

3636
In WSL:
@@ -99,3 +99,14 @@ Debugging options you may want to add:
9999

100100
`CFG_TA_DEBUG=y` Turns on debug output from the TAs, and enables extra correctness checks in the fTPM TA.
101101

102+
#### 2. Measured Boot support
103+
The fTPM Trusted Application includes support for Measured Boot. This feature allows the TA to read a TPM Event Log compatible with the specification in Section 5 of the
104+
[TCG EFI Protocol Specification](https://trustedcomputinggroup.org/wp-content/uploads/EFI-Protocol-Specification-rev13-160330final.pdf). The event log is read and extended during the TA initialization.
105+
106+
Measure Boot support requires OpTEE System Call ```PTA_SYSTEM_GET_TPM_EVENT_LOG```, available since [OpTEE 3.10.0](https://github.com/OP-TEE/optee_os/tree/3.10.0).
107+
108+
Flags related to Measured Boot support:
109+
110+
`CFG_TA_MEASURED_BOOT`: Controls whether Measured Boot is enabled (`CFG_TA_MEASURED_BOOT=y`) or disabled (by default).
111+
`CFG_TA_EVENT_LOG_SIZE`: Maximum size in bytes allowed for the Event Log. Defaults to 1024 bytes.
112+

Samples/ARM32-FirmwareTPM/optee_ta/fTPM/fTPM.c

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* under this license.
77
*
88
* Copyright (c) Microsoft Corporation
9+
* Copyright (c) Arm Limited.
910
*
1011
* All rights reserved.
1112
*
@@ -38,6 +39,9 @@
3839
#include <tee_internal_api.h>
3940
#include <tee_internal_api_extensions.h>
4041
#include <string.h>
42+
#include <pta_system.h>
43+
#include <fTPM_helpers.h>
44+
#include <fTPM_event_log.h>
4145

4246
#include "fTPM.h"
4347

@@ -66,25 +70,6 @@ typedef uint32_t TPM_RC;
6670
#define TPM_RC_SUCCESS (TPM_RC) (0x000)
6771
#define TPM_RC_FAILURE (TPM_RC) (RC_VER1+0x001)
6872

69-
//
70-
// Helper functions for byte ordering of TPM commands/responses
71-
//
72-
static uint16_t SwapBytes16(uint16_t Value)
73-
{
74-
return (uint16_t)((Value << 8) | (Value >> 8));
75-
}
76-
77-
static uint32_t SwapBytes32(uint32_t Value)
78-
{
79-
uint32_t LowerBytes;
80-
uint32_t HigherBytes;
81-
82-
LowerBytes = (uint32_t)SwapBytes16((uint16_t)Value);
83-
HigherBytes = (uint32_t)SwapBytes16((uint16_t)(Value >> 16));
84-
85-
return (LowerBytes << 16 | HigherBytes);
86-
}
87-
8873
//
8974
// Helper function to read response codes from TPM responses
9075
//
@@ -111,9 +96,42 @@ static uint32_t fTPMResponseCode(uint32_t ResponseSize,
11196
return ResponseCode;
11297
}
11398

114-
//
99+
#ifdef MEASURED_BOOT
100+
static TEE_Result get_tpm_event_log(unsigned char *buf, size_t *len)
101+
{
102+
const TEE_UUID system_uuid = PTA_SYSTEM_UUID;
103+
TEE_TASessionHandle session = TEE_HANDLE_NULL;
104+
TEE_Result res = TEE_ERROR_GENERIC;
105+
uint32_t ret_origin = 0;
106+
const uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_OUTPUT,
107+
TEE_PARAM_TYPE_NONE,
108+
TEE_PARAM_TYPE_NONE,
109+
TEE_PARAM_TYPE_NONE);
110+
TEE_Param params[TEE_NUM_PARAMS] = {0};
111+
112+
res = TEE_OpenTASession(&system_uuid, TEE_TIMEOUT_INFINITE,
113+
0, NULL, &session, &ret_origin);
114+
if (res != TEE_SUCCESS)
115+
return res;
116+
117+
params[0].memref.buffer = (void *)buf;
118+
params[0].memref.size = *len;
119+
120+
res = TEE_InvokeTACommand(session, TEE_TIMEOUT_INFINITE,
121+
PTA_SYSTEM_GET_TPM_EVENT_LOG,
122+
param_types, params, &ret_origin);
123+
124+
*len = params[0].memref.size;
125+
126+
TEE_CloseTASession(session);
127+
128+
return res;
129+
}
130+
#endif // MEASURED_BOOT
131+
132+
//
115133
// Called when TA instance is created. This is the first call to the TA.
116-
//
134+
//
117135
TEE_Result TA_CreateEntryPoint(void)
118136
{
119137
#define STARTUP_SIZE 0x0C
@@ -124,14 +142,18 @@ TEE_Result TA_CreateEntryPoint(void)
124142
0x00, 0x00, 0x01, 0x44, 0x00, 0x01 };
125143
uint32_t respLen;
126144
uint8_t *respBuf;
145+
#ifdef MEASURED_BOOT
146+
unsigned char tpm_event_log_buf[EVENT_LOG_SIZE];
147+
size_t tpm_event_log_len = EVENT_LOG_SIZE;
148+
#endif
127149

128150
#ifdef fTPMDebug
129151
DMSG("Entry Point\n");
130152
#endif
131153

132154
// If we've been here before, don't init again.
133155
if (fTPMInitialized) {
134-
// We may have had TA_DestroyEntryPoint called but we didn't
156+
// We may have had TA_DestroyEntryPoint called but we didn't
135157
// actually get torn down. Re-NVEnable, just in case.
136158
if (_plat__NVEnable(NULL) == 0) {
137159
TEE_Panic(TEE_ERROR_BAD_STATE);
@@ -209,6 +231,24 @@ TEE_Result TA_CreateEntryPoint(void)
209231
// Initialization complete
210232
fTPMInitialized = true;
211233

234+
#ifdef MEASURED_BOOT
235+
// Extend existing TPM Event Log.
236+
if (get_tpm_event_log(tpm_event_log_buf,
237+
&tpm_event_log_len) == TEE_SUCCESS)
238+
{
239+
240+
#ifdef fTPMDebug
241+
// Dump the event log
242+
unsigned char* buff = tpm_event_log_buf;
243+
size_t buff_len = tpm_event_log_len;
244+
MSG("Preparing to extend the following TPM Event Log:");
245+
dump_event_log(tpm_event_log_buf, tpm_event_log_len);
246+
#endif
247+
process_eventlog(tpm_event_log_buf, tpm_event_log_len);
248+
249+
}
250+
#endif
251+
212252
return TEE_SUCCESS;
213253
}
214254

@@ -436,4 +476,4 @@ TEE_Result TA_InvokeCommandEntryPoint(void *sess_ctx,
436476
return TEE_ERROR_BAD_PARAMETERS;
437477
}
438478
}
439-
}
479+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2021, Arm Limited. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#ifndef _FTPM_EVENT_LOG_
8+
#define _FTPM_EVENT_LOG_
9+
10+
bool process_eventlog(const unsigned char *const buf, const size_t log_size);
11+
void dump_event_log(uint8_t *log_addr, size_t log_size);
12+
13+
#endif /* _FTPM_EVENT_LOG_*/

0 commit comments

Comments
 (0)