1*40814266SManish V Badarkhe /* 2*40814266SManish V Badarkhe * Copyright (c) 2022 Arm Limited. All rights reserved. 3*40814266SManish V Badarkhe * 4*40814266SManish V Badarkhe * SPDX-License-Identifier: BSD-3-Clause 5*40814266SManish V Badarkhe * 6*40814266SManish V Badarkhe * DRTM measurements into TPM PCRs. 7*40814266SManish V Badarkhe * 8*40814266SManish V Badarkhe * Authors: 9*40814266SManish V Badarkhe * Lucian Paul-Trifu <lucian.paultrifu@gmail.com> 10*40814266SManish V Badarkhe * 11*40814266SManish V Badarkhe */ 12*40814266SManish V Badarkhe #include <assert.h> 13*40814266SManish V Badarkhe 14*40814266SManish V Badarkhe #include <common/debug.h> 15*40814266SManish V Badarkhe #include <drivers/auth/crypto_mod.h> 16*40814266SManish V Badarkhe #include <drivers/measured_boot/event_log/event_log.h> 17*40814266SManish V Badarkhe #include "drtm_main.h" 18*40814266SManish V Badarkhe #include "drtm_measurements.h" 19*40814266SManish V Badarkhe #include <lib/xlat_tables/xlat_tables_v2.h> 20*40814266SManish V Badarkhe 21*40814266SManish V Badarkhe /* Event Log buffer */ 22*40814266SManish V Badarkhe static uint8_t drtm_event_log[PLAT_DRTM_EVENT_LOG_MAX_SIZE]; 23*40814266SManish V Badarkhe 24*40814266SManish V Badarkhe /* 25*40814266SManish V Badarkhe * Calculate and write hash of various payloads as per DRTM specification 26*40814266SManish V Badarkhe * to Event Log. 27*40814266SManish V Badarkhe * 28*40814266SManish V Badarkhe * @param[in] data_base Address of data 29*40814266SManish V Badarkhe * @param[in] data_size Size of data 30*40814266SManish V Badarkhe * @param[in] event_type Type of Event 31*40814266SManish V Badarkhe * @param[in] event_name Name of the Event 32*40814266SManish V Badarkhe * @return: 33*40814266SManish V Badarkhe * 0 = success 34*40814266SManish V Badarkhe * < 0 = error 35*40814266SManish V Badarkhe */ 36*40814266SManish V Badarkhe static int drtm_event_log_measure_and_record(uintptr_t data_base, 37*40814266SManish V Badarkhe uint32_t data_size, 38*40814266SManish V Badarkhe uint32_t event_type, 39*40814266SManish V Badarkhe const char *event_name, 40*40814266SManish V Badarkhe unsigned int pcr) 41*40814266SManish V Badarkhe { 42*40814266SManish V Badarkhe int rc; 43*40814266SManish V Badarkhe unsigned char hash_data[CRYPTO_MD_MAX_SIZE]; 44*40814266SManish V Badarkhe event_log_metadata_t metadata = {0}; 45*40814266SManish V Badarkhe 46*40814266SManish V Badarkhe metadata.name = event_name; 47*40814266SManish V Badarkhe metadata.pcr = pcr; 48*40814266SManish V Badarkhe 49*40814266SManish V Badarkhe /* 50*40814266SManish V Badarkhe * Measure the payloads requested by D-CRTM and DCE commponents 51*40814266SManish V Badarkhe * Hash algorithm decided by the Event Log driver at build-time 52*40814266SManish V Badarkhe */ 53*40814266SManish V Badarkhe rc = event_log_measure(data_base, data_size, hash_data); 54*40814266SManish V Badarkhe if (rc != 0) { 55*40814266SManish V Badarkhe return rc; 56*40814266SManish V Badarkhe } 57*40814266SManish V Badarkhe 58*40814266SManish V Badarkhe /* Record the mesasurement in the EventLog buffer */ 59*40814266SManish V Badarkhe event_log_record(hash_data, event_type, &metadata); 60*40814266SManish V Badarkhe 61*40814266SManish V Badarkhe return 0; 62*40814266SManish V Badarkhe } 63*40814266SManish V Badarkhe 64*40814266SManish V Badarkhe /* 65*40814266SManish V Badarkhe * Initialise Event Log global variables, used during the recording 66*40814266SManish V Badarkhe * of various payload measurements into the Event Log buffer 67*40814266SManish V Badarkhe * 68*40814266SManish V Badarkhe * @param[in] event_log_start Base address of Event Log buffer 69*40814266SManish V Badarkhe * @param[in] event_log_finish End address of Event Log buffer, 70*40814266SManish V Badarkhe * it is a first byte past end of the 71*40814266SManish V Badarkhe * buffer 72*40814266SManish V Badarkhe */ 73*40814266SManish V Badarkhe static void drtm_event_log_init(uint8_t *event_log_start, 74*40814266SManish V Badarkhe uint8_t *event_log_finish) 75*40814266SManish V Badarkhe { 76*40814266SManish V Badarkhe event_log_buf_init(event_log_start, event_log_finish); 77*40814266SManish V Badarkhe event_log_write_specid_event(); 78*40814266SManish V Badarkhe } 79*40814266SManish V Badarkhe 80*40814266SManish V Badarkhe enum drtm_retc drtm_take_measurements(const struct_drtm_dl_args *a) 81*40814266SManish V Badarkhe { 82*40814266SManish V Badarkhe int rc; 83*40814266SManish V Badarkhe uintptr_t dlme_img_mapping; 84*40814266SManish V Badarkhe uint64_t dlme_img_ep; 85*40814266SManish V Badarkhe size_t dlme_img_mapping_bytes; 86*40814266SManish V Badarkhe uint8_t drtm_null_data = 0U; 87*40814266SManish V Badarkhe uint8_t pcr_schema = DL_ARGS_GET_PCR_SCHEMA(a); 88*40814266SManish V Badarkhe const char *drtm_event_arm_sep_data = "ARM_DRTM"; 89*40814266SManish V Badarkhe 90*40814266SManish V Badarkhe /* Initialise the EventLog driver */ 91*40814266SManish V Badarkhe drtm_event_log_init(drtm_event_log, drtm_event_log + 92*40814266SManish V Badarkhe sizeof(drtm_event_log)); 93*40814266SManish V Badarkhe 94*40814266SManish V Badarkhe /** 95*40814266SManish V Badarkhe * Measurements extended into PCR-17. 96*40814266SManish V Badarkhe * 97*40814266SManish V Badarkhe * PCR-17: Measure the DCE image. Extend digest of (char)0 into PCR-17 98*40814266SManish V Badarkhe * since the D-CRTM and the DCE are not separate. 99*40814266SManish V Badarkhe */ 100*40814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)&drtm_null_data, 101*40814266SManish V Badarkhe sizeof(drtm_null_data), 102*40814266SManish V Badarkhe DRTM_EVENT_ARM_DCE, NULL, 103*40814266SManish V Badarkhe PCR_17); 104*40814266SManish V Badarkhe CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DCE)); 105*40814266SManish V Badarkhe 106*40814266SManish V Badarkhe /* PCR-17: Measure the PCR schema DRTM launch argument. */ 107*40814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)&pcr_schema, 108*40814266SManish V Badarkhe sizeof(pcr_schema), 109*40814266SManish V Badarkhe DRTM_EVENT_ARM_PCR_SCHEMA, 110*40814266SManish V Badarkhe NULL, PCR_17); 111*40814266SManish V Badarkhe CHECK_RC(rc, 112*40814266SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_PCR_SCHEMA)); 113*40814266SManish V Badarkhe 114*40814266SManish V Badarkhe /* PCR-17: Measure the enable state of external-debug, and trace. */ 115*40814266SManish V Badarkhe /* 116*40814266SManish V Badarkhe * TODO: Measure the enable state of external-debug and trace. This should 117*40814266SManish V Badarkhe * be returned through a platform-specific hook. 118*40814266SManish V Badarkhe */ 119*40814266SManish V Badarkhe 120*40814266SManish V Badarkhe /* PCR-17: Measure the security lifecycle state. */ 121*40814266SManish V Badarkhe /* 122*40814266SManish V Badarkhe * TODO: Measure the security lifecycle state. This is an implementation- 123*40814266SManish V Badarkhe * defined value, retrieved through an implementation-defined mechanisms. 124*40814266SManish V Badarkhe */ 125*40814266SManish V Badarkhe 126*40814266SManish V Badarkhe /* 127*40814266SManish V Badarkhe * PCR-17: Optionally measure the NWd DCE. 128*40814266SManish V Badarkhe * It is expected that such subsequent DCE stages are signed and verified. 129*40814266SManish V Badarkhe * Whether they are measured in addition to signing is implementation 130*40814266SManish V Badarkhe * -defined. 131*40814266SManish V Badarkhe * Here the choice is to not measure any NWd DCE, in favour of PCR value 132*40814266SManish V Badarkhe * resilience to any NWd DCE updates. 133*40814266SManish V Badarkhe */ 134*40814266SManish V Badarkhe 135*40814266SManish V Badarkhe /* PCR-17: End of DCE measurements. */ 136*40814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)drtm_event_arm_sep_data, 137*40814266SManish V Badarkhe strlen(drtm_event_arm_sep_data), 138*40814266SManish V Badarkhe DRTM_EVENT_ARM_SEPARATOR, NULL, 139*40814266SManish V Badarkhe PCR_17); 140*40814266SManish V Badarkhe CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_SEPARATOR)); 141*40814266SManish V Badarkhe 142*40814266SManish V Badarkhe /** 143*40814266SManish V Badarkhe * Measurements extended into PCR-18. 144*40814266SManish V Badarkhe * 145*40814266SManish V Badarkhe * PCR-18: Measure the PCR schema DRTM launch argument. 146*40814266SManish V Badarkhe */ 147*40814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)&pcr_schema, 148*40814266SManish V Badarkhe sizeof(pcr_schema), 149*40814266SManish V Badarkhe DRTM_EVENT_ARM_PCR_SCHEMA, 150*40814266SManish V Badarkhe NULL, PCR_18); 151*40814266SManish V Badarkhe CHECK_RC(rc, 152*40814266SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_PCR_SCHEMA)); 153*40814266SManish V Badarkhe 154*40814266SManish V Badarkhe /* 155*40814266SManish V Badarkhe * PCR-18: Measure the public key used to verify DCE image(s) signatures. 156*40814266SManish V Badarkhe * Extend digest of (char)0, since we do not expect the NWd DCE to be 157*40814266SManish V Badarkhe * present. 158*40814266SManish V Badarkhe */ 159*40814266SManish V Badarkhe assert(a->dce_nwd_size == 0); 160*40814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)&drtm_null_data, 161*40814266SManish V Badarkhe sizeof(drtm_null_data), 162*40814266SManish V Badarkhe DRTM_EVENT_ARM_DCE_PUBKEY, 163*40814266SManish V Badarkhe NULL, PCR_18); 164*40814266SManish V Badarkhe CHECK_RC(rc, 165*40814266SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DCE_PUBKEY)); 166*40814266SManish V Badarkhe 167*40814266SManish V Badarkhe /* PCR-18: Measure the DLME image. */ 168*40814266SManish V Badarkhe dlme_img_mapping_bytes = page_align(a->dlme_img_size, UP); 169*40814266SManish V Badarkhe rc = mmap_add_dynamic_region_alloc_va(a->dlme_paddr + a->dlme_img_off, 170*40814266SManish V Badarkhe &dlme_img_mapping, 171*40814266SManish V Badarkhe dlme_img_mapping_bytes, MT_RO_DATA | MT_NS); 172*40814266SManish V Badarkhe if (rc) { 173*40814266SManish V Badarkhe WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n", 174*40814266SManish V Badarkhe __func__, rc); 175*40814266SManish V Badarkhe return INTERNAL_ERROR; 176*40814266SManish V Badarkhe } 177*40814266SManish V Badarkhe 178*40814266SManish V Badarkhe rc = drtm_event_log_measure_and_record(dlme_img_mapping, a->dlme_img_size, 179*40814266SManish V Badarkhe DRTM_EVENT_ARM_DLME, NULL, 180*40814266SManish V Badarkhe PCR_18); 181*40814266SManish V Badarkhe CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DLME)); 182*40814266SManish V Badarkhe 183*40814266SManish V Badarkhe rc = mmap_remove_dynamic_region(dlme_img_mapping, dlme_img_mapping_bytes); 184*40814266SManish V Badarkhe CHECK_RC(rc, mmap_remove_dynamic_region); 185*40814266SManish V Badarkhe 186*40814266SManish V Badarkhe /* PCR-18: Measure the DLME image entry point. */ 187*40814266SManish V Badarkhe dlme_img_ep = DL_ARGS_GET_DLME_ENTRY_POINT(a); 188*40814266SManish V Badarkhe drtm_event_log_measure_and_record((uintptr_t)&dlme_img_ep, 189*40814266SManish V Badarkhe sizeof(dlme_img_ep), 190*40814266SManish V Badarkhe DRTM_EVENT_ARM_DLME_EP, NULL, 191*40814266SManish V Badarkhe PCR_18); 192*40814266SManish V Badarkhe CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DLME_EP)); 193*40814266SManish V Badarkhe 194*40814266SManish V Badarkhe /* PCR-18: End of DCE measurements. */ 195*40814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)drtm_event_arm_sep_data, 196*40814266SManish V Badarkhe strlen(drtm_event_arm_sep_data), 197*40814266SManish V Badarkhe DRTM_EVENT_ARM_SEPARATOR, NULL, 198*40814266SManish V Badarkhe PCR_18); 199*40814266SManish V Badarkhe CHECK_RC(rc, 200*40814266SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_SEPARATOR)); 201*40814266SManish V Badarkhe /* 202*40814266SManish V Badarkhe * If the DCE is unable to log a measurement because there is no available 203*40814266SManish V Badarkhe * space in the event log region, the DCE must extend a hash of the value 204*40814266SManish V Badarkhe * 0xFF (1 byte in size) into PCR[17] and PCR[18] and enter remediation. 205*40814266SManish V Badarkhe */ 206*40814266SManish V Badarkhe 207*40814266SManish V Badarkhe return SUCCESS; 208*40814266SManish V Badarkhe } 209*40814266SManish V Badarkhe 210*40814266SManish V Badarkhe void drtm_serialise_event_log(uint8_t *dst, size_t *event_log_size_out) 211*40814266SManish V Badarkhe { 212*40814266SManish V Badarkhe *event_log_size_out = event_log_get_cur_size(drtm_event_log); 213*40814266SManish V Badarkhe memcpy(dst, drtm_event_log, *event_log_size_out); 214*40814266SManish V Badarkhe } 215