Skip to content

Commit 95d1e40

Browse files
YadongQiwenlingz
authored andcommitted
hv: refactor seed management
New component to maintain seed retrieval and derivation: seed. 1. Retrieve seed from bootloader in Hypervisor's boot stage. 2. Derive virtual seed for Guest/Trusty if need. Tracked-On: #2724 Signed-off-by: Qi Yadong <yadong.qi@intel.com> Reviewed-by: Zhu Bing <bing.zhu@intel.com>
1 parent 4d0419e commit 95d1e40

File tree

11 files changed

+573
-132
lines changed

11 files changed

+573
-132
lines changed

hypervisor/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ C_SRCS += boot/reloc.c
131131
# initilization component
132132
C_SRCS += arch/x86/init.c
133133

134+
C_SRCS += arch/x86/seed/seed.c
135+
C_SRCS += arch/x86/seed/seed_abl.c
136+
C_SRCS += arch/x86/seed/seed_sbl.c
137+
134138
# configuration component
135139
C_SRCS += arch/x86/configs/vm_config.c
136140
ifeq ($(CONFIG_PARTITION_MODE),y)
@@ -234,8 +238,6 @@ C_SRCS += bsp/const_dmar.c
234238

235239
C_SRCS += boot/uefi/uefi_boot.c
236240
C_SRCS += boot/sbl/multiboot.c
237-
C_SRCS += boot/sbl/sbl_seed_parse.c
238-
C_SRCS += boot/sbl/abl_seed_parse.c
239241

240242
# retpoline support
241243
ifeq (true, $(shell [ $(GCC_MAJOR) -eq 7 ] && [ $(GCC_MINOR) -ge 3 ] && echo true))

hypervisor/arch/x86/guest/trusty.c

Lines changed: 39 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vmx.h>
1717
#include <security.h>
1818
#include <logmsg.h>
19+
#include <seed.h>
1920

2021
#define TRUSTY_VERSION 1U
2122
#define TRUSTY_VERSION_2 2U
@@ -30,13 +31,6 @@ struct trusty_mem {
3031
/* The left memory is for trusty's code/data/heap/stack */
3132
} __aligned(PAGE_SIZE);
3233

33-
static struct trusty_key_info g_key_info = {
34-
.size_of_this_struct = sizeof(g_key_info),
35-
.version = 0U,
36-
.platform = 3U,
37-
.num_seeds = 1U
38-
};
39-
4034
/**
4135
* @defgroup trusty_apis Trusty APIs
4236
*
@@ -317,106 +311,60 @@ void switch_world(struct acrn_vcpu *vcpu, int32_t next_world)
317311
arch->cur_context = next_world;
318312
}
319313

320-
static inline uint32_t get_max_svn_index(void)
321-
{
322-
uint32_t i, max_svn_idx = 0U;
323-
324-
for (i = 1U; i < g_key_info.num_seeds; i++) {
325-
if (g_key_info.dseed_list[i].cse_svn > g_key_info.dseed_list[i - 1U].cse_svn) {
326-
max_svn_idx = i;
327-
}
328-
}
329-
330-
return max_svn_idx;
331-
}
332-
333-
static bool derive_aek(uint8_t *attkb_key)
334-
{
335-
bool ret = true;
336-
const int8_t salt[] = "Attestation Keybox Encryption Key";
337-
const uint8_t *ikm;
338-
uint32_t ikm_len;
339-
uint32_t max_svn_idx;
340-
341-
if ((attkb_key == NULL) || (g_key_info.num_seeds == 0U) ||
342-
(g_key_info.num_seeds > BOOTLOADER_SEED_MAX_ENTRIES)) {
343-
ret = false;
344-
} else {
345-
max_svn_idx = get_max_svn_index();
346-
ikm = g_key_info.dseed_list[max_svn_idx].seed;
347-
/* only the low 32 bits of seed are valid */
348-
ikm_len = 32U;
349-
350-
if (hmac_sha256(attkb_key, ikm, ikm_len,
351-
(const uint8_t *)salt, sizeof(salt)) != 1) {
352-
pr_err("%s: failed to derive key!\n", __func__);
353-
ret = false;
354-
}
355-
}
356-
357-
return ret;
358-
}
359-
360314
/* Put key_info and trusty_startup_param in the first Page of Trusty
361315
* runtime memory
362316
*/
363-
static bool setup_trusty_info(struct acrn_vcpu *vcpu,
364-
uint32_t mem_size, uint64_t mem_base_hpa)
317+
static bool setup_trusty_info(struct acrn_vcpu *vcpu, uint32_t mem_size, uint64_t mem_base_hpa, uint8_t *rkey)
365318
{
366319
bool ret = true;
367-
uint32_t i;
368320
struct trusty_mem *mem;
369-
struct trusty_key_info *key_info;
321+
struct trusty_key_info key_info;
322+
struct trusty_startup_param startup_param;
370323

371-
mem = (struct trusty_mem *)(hpa2hva(mem_base_hpa));
324+
(void)memset(&key_info, 0U, sizeof(key_info));
372325

373-
stac();
374-
/* copy key_info to the first page of trusty memory */
375-
(void)memcpy_s(&mem->first_page.key_info, sizeof(g_key_info),
376-
&g_key_info, sizeof(g_key_info));
326+
key_info.size_of_this_struct = sizeof(struct trusty_key_info);
327+
key_info.version = 0U;
328+
key_info.platform = 3U;
377329

378-
(void)memset(&mem->first_page.key_info.dseed_list, 0U,
379-
sizeof(mem->first_page.key_info.dseed_list));
380-
/* Derive dvseed from dseed for Trusty */
381-
key_info = &mem->first_page.key_info;
382-
for (i = 0U; i < g_key_info.num_seeds; i++) {
383-
if (hkdf_sha256(key_info->dseed_list[i].seed,
384-
BUP_MKHI_BOOTLOADER_SEED_LEN,
385-
g_key_info.dseed_list[i].seed,
386-
BUP_MKHI_BOOTLOADER_SEED_LEN,
387-
NULL, 0U,
388-
vcpu->vm->GUID, sizeof(vcpu->vm->GUID)) == 0) {
389-
(void)memset(key_info, 0U, sizeof(struct trusty_key_info));
390-
pr_err("%s: derive dvseed failed!", __func__);
391-
ret = false;
392-
break;
393-
}
394-
key_info->dseed_list[i].cse_svn = g_key_info.dseed_list[i].cse_svn;
330+
if (rkey != NULL) {
331+
(void)memcpy_s(key_info.rpmb_key, 64U, rkey, 64U);
332+
(void)memset(rkey, 0U, 64U);
395333
}
396334

335+
/* Derive dvseed from dseed for Trusty */
336+
ret = derive_virtual_seed(&key_info.dseed_list[0U], &key_info.num_seeds,
337+
NULL, 0U,
338+
vcpu->vm->GUID, sizeof(vcpu->vm->GUID));
397339
if (ret == true) {
398-
/* Derive decryption key of attestation keybox from dseed */
399-
if (!derive_aek(key_info->attkb_enc_key)) {
400-
(void)memset(key_info, 0U, sizeof(struct trusty_key_info));
401-
pr_err("%s: derive key of att keybox failed!", __func__);
402-
ret = false;
403-
} else {
340+
/* Derive encryption key of attestation keybox from dseed */
341+
ret = derive_attkb_enc_key(key_info.attkb_enc_key);
342+
if (ret == true) {
404343
/* Prepare trusty startup param */
405-
mem->first_page.startup_param.size_of_this_struct = sizeof(struct trusty_startup_param);
406-
mem->first_page.startup_param.mem_size = mem_size;
407-
mem->first_page.startup_param.tsc_per_ms = CYCLES_PER_MS;
408-
mem->first_page.startup_param.trusty_mem_base = TRUSTY_EPT_REBASE_GPA;
344+
startup_param.size_of_this_struct = sizeof(struct trusty_startup_param);
345+
startup_param.mem_size = mem_size;
346+
startup_param.tsc_per_ms = CYCLES_PER_MS;
347+
startup_param.trusty_mem_base = TRUSTY_EPT_REBASE_GPA;
409348

410349
/* According to trusty boot protocol, it will use RDI as the
411350
* address(GPA) of startup_param on boot. Currently, the startup_param
412351
* is put in the first page of trusty memory just followed by key_info.
413352
*/
414353
vcpu->arch.contexts[SECURE_WORLD].run_ctx.guest_cpu_regs.regs.rdi
415354
= (uint64_t)TRUSTY_EPT_REBASE_GPA + sizeof(struct trusty_key_info);
355+
356+
stac();
357+
mem = (struct trusty_mem *)(hpa2hva(mem_base_hpa));
358+
(void)memcpy_s(&mem->first_page.key_info, sizeof(struct trusty_key_info),
359+
&key_info, sizeof(key_info));
360+
(void)memcpy_s(&mem->first_page.startup_param, sizeof(struct trusty_startup_param),
361+
&startup_param, sizeof(startup_param));
362+
clac();
416363
}
417364
}
418365

419-
clac();
366+
(void)memset(&key_info, 0U, sizeof(key_info));
367+
420368
return ret;
421369
}
422370

@@ -428,7 +376,8 @@ static bool setup_trusty_info(struct acrn_vcpu *vcpu,
428376
static bool init_secure_world_env(struct acrn_vcpu *vcpu,
429377
uint64_t entry_gpa,
430378
uint64_t base_hpa,
431-
uint32_t size)
379+
uint32_t size,
380+
uint8_t *rpmb_key)
432381
{
433382
uint32_t i;
434383

@@ -445,25 +394,24 @@ static bool init_secure_world_env(struct acrn_vcpu *vcpu,
445394
vcpu->arch.contexts[SECURE_WORLD].world_msrs[i] = vcpu->arch.guest_msrs[i];
446395
}
447396

448-
return setup_trusty_info(vcpu, size, base_hpa);
397+
return setup_trusty_info(vcpu, size, base_hpa, rpmb_key);
449398
}
450399

451-
bool initialize_trusty(struct acrn_vcpu *vcpu, const struct trusty_boot_param *boot_param)
400+
bool initialize_trusty(struct acrn_vcpu *vcpu, struct trusty_boot_param *boot_param)
452401
{
453402
bool ret = true;
454403
uint64_t trusty_entry_gpa, trusty_base_gpa, trusty_base_hpa;
455404
uint32_t trusty_mem_size;
456405
struct acrn_vm *vm = vcpu->vm;
406+
uint8_t *rpmb_key = NULL;
457407

458408
switch (boot_param->version) {
459409
case TRUSTY_VERSION_2:
460410
trusty_entry_gpa = ((uint64_t)boot_param->entry_point) |
461411
(((uint64_t)boot_param->entry_point_high) << 32U);
462412
trusty_base_gpa = ((uint64_t)boot_param->base_addr) |
463413
(((uint64_t)boot_param->base_addr_high) << 32U);
464-
465-
/* copy rpmb_key from OSloader */
466-
(void)memcpy_s(&g_key_info.rpmb_key[0][0], 64U, &boot_param->rpmb_key[0], 64U);
414+
rpmb_key = boot_param->rpmb_key;
467415
break;
468416
case TRUSTY_VERSION:
469417
trusty_entry_gpa = (uint64_t)boot_param->entry_point;
@@ -495,7 +443,7 @@ bool initialize_trusty(struct acrn_vcpu *vcpu, const struct trusty_boot_param *b
495443
/* init secure world environment */
496444
if (init_secure_world_env(vcpu,
497445
(trusty_entry_gpa - trusty_base_gpa) + TRUSTY_EPT_REBASE_GPA,
498-
trusty_base_hpa, trusty_mem_size)) {
446+
trusty_base_hpa, trusty_mem_size, rpmb_key)) {
499447

500448
/* switch to Secure World */
501449
vcpu->arch.cur_context = SECURE_WORLD;
@@ -508,23 +456,6 @@ bool initialize_trusty(struct acrn_vcpu *vcpu, const struct trusty_boot_param *b
508456
return ret;
509457
}
510458

511-
void trusty_set_dseed(const void *dseed, uint8_t dseed_num)
512-
{
513-
/* Use fake seed if input param is invalid */
514-
if ((dseed == NULL) || (dseed_num == 0U) ||
515-
(dseed_num > BOOTLOADER_SEED_MAX_ENTRIES)) {
516-
517-
g_key_info.num_seeds = 1U;
518-
(void)memset(&g_key_info.dseed_list[0].seed, 0xA5U,
519-
sizeof(g_key_info.dseed_list[0].seed));
520-
} else {
521-
g_key_info.num_seeds = dseed_num;
522-
(void)memcpy_s(&g_key_info.dseed_list,
523-
sizeof(struct seed_info) * dseed_num,
524-
dseed, sizeof(struct seed_info) * dseed_num);
525-
}
526-
}
527-
528459
void save_sworld_context(struct acrn_vcpu *vcpu)
529460
{
530461
(void)memcpy_s(&vcpu->vm->sworld_snapshot,

hypervisor/arch/x86/init.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <vm.h>
1616
#include <logmsg.h>
1717
#include <firmware.h>
18+
#include <seed.h>
1819

1920
/* Push sp magic to top of stack for call trace */
2021
#define SWITCH_TO(rsp, to) \
@@ -70,6 +71,8 @@ static void init_primary_cpu_post(void)
7071

7172
init_cpu_post(BOOT_CPU_ID);
7273

74+
init_seed();
75+
7376
init_debug_post(BOOT_CPU_ID);
7477

7578
enter_guest_mode(BOOT_CPU_ID);

0 commit comments

Comments
 (0)