16
16
#include <vmx.h>
17
17
#include <security.h>
18
18
#include <logmsg.h>
19
+ #include <seed.h>
19
20
20
21
#define TRUSTY_VERSION 1U
21
22
#define TRUSTY_VERSION_2 2U
@@ -30,13 +31,6 @@ struct trusty_mem {
30
31
/* The left memory is for trusty's code/data/heap/stack */
31
32
} __aligned (PAGE_SIZE );
32
33
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
-
40
34
/**
41
35
* @defgroup trusty_apis Trusty APIs
42
36
*
@@ -317,106 +311,60 @@ void switch_world(struct acrn_vcpu *vcpu, int32_t next_world)
317
311
arch -> cur_context = next_world ;
318
312
}
319
313
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
-
360
314
/* Put key_info and trusty_startup_param in the first Page of Trusty
361
315
* runtime memory
362
316
*/
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 )
365
318
{
366
319
bool ret = true;
367
- uint32_t i ;
368
320
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 ;
370
323
371
- mem = ( struct trusty_mem * )( hpa2hva ( mem_base_hpa ));
324
+ ( void ) memset ( & key_info , 0U , sizeof ( key_info ));
372
325
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 ;
377
329
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 );
395
333
}
396
334
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 ));
397
339
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) {
404
343
/* 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 ;
409
348
410
349
/* According to trusty boot protocol, it will use RDI as the
411
350
* address(GPA) of startup_param on boot. Currently, the startup_param
412
351
* is put in the first page of trusty memory just followed by key_info.
413
352
*/
414
353
vcpu -> arch .contexts [SECURE_WORLD ].run_ctx .guest_cpu_regs .regs .rdi
415
354
= (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 ();
416
363
}
417
364
}
418
365
419
- clac ();
366
+ (void )memset (& key_info , 0U , sizeof (key_info ));
367
+
420
368
return ret ;
421
369
}
422
370
@@ -428,7 +376,8 @@ static bool setup_trusty_info(struct acrn_vcpu *vcpu,
428
376
static bool init_secure_world_env (struct acrn_vcpu * vcpu ,
429
377
uint64_t entry_gpa ,
430
378
uint64_t base_hpa ,
431
- uint32_t size )
379
+ uint32_t size ,
380
+ uint8_t * rpmb_key )
432
381
{
433
382
uint32_t i ;
434
383
@@ -445,25 +394,24 @@ static bool init_secure_world_env(struct acrn_vcpu *vcpu,
445
394
vcpu -> arch .contexts [SECURE_WORLD ].world_msrs [i ] = vcpu -> arch .guest_msrs [i ];
446
395
}
447
396
448
- return setup_trusty_info (vcpu , size , base_hpa );
397
+ return setup_trusty_info (vcpu , size , base_hpa , rpmb_key );
449
398
}
450
399
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 )
452
401
{
453
402
bool ret = true;
454
403
uint64_t trusty_entry_gpa , trusty_base_gpa , trusty_base_hpa ;
455
404
uint32_t trusty_mem_size ;
456
405
struct acrn_vm * vm = vcpu -> vm ;
406
+ uint8_t * rpmb_key = NULL ;
457
407
458
408
switch (boot_param -> version ) {
459
409
case TRUSTY_VERSION_2 :
460
410
trusty_entry_gpa = ((uint64_t )boot_param -> entry_point ) |
461
411
(((uint64_t )boot_param -> entry_point_high ) << 32U );
462
412
trusty_base_gpa = ((uint64_t )boot_param -> base_addr ) |
463
413
(((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 ;
467
415
break ;
468
416
case TRUSTY_VERSION :
469
417
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
495
443
/* init secure world environment */
496
444
if (init_secure_world_env (vcpu ,
497
445
(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 )) {
499
447
500
448
/* switch to Secure World */
501
449
vcpu -> arch .cur_context = SECURE_WORLD ;
@@ -508,23 +456,6 @@ bool initialize_trusty(struct acrn_vcpu *vcpu, const struct trusty_boot_param *b
508
456
return ret ;
509
457
}
510
458
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
-
528
459
void save_sworld_context (struct acrn_vcpu * vcpu )
529
460
{
530
461
(void )memcpy_s (& vcpu -> vm -> sworld_snapshot ,
0 commit comments