From a48e9fe948c0ad1d205f39e40e02a52aff20de9e Mon Sep 17 00:00:00 2001 From: Margarita Glushkin Date: Tue, 18 Jul 2023 08:52:53 +0300 Subject: [PATCH] plat-nuvoton: added HUK reading Implements HUK reading from DME PCR0 located in PCI mailbox Co-developed-by: Hila Miranda-Kuzi Signed-off-by: Hila Miranda-Kuzi Signed-off-by: Margarita Glushkin --- core/arch/arm/plat-nuvoton/main.c | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/core/arch/arm/plat-nuvoton/main.c b/core/arch/arm/plat-nuvoton/main.c index 2d80f185322..3e5466f60f3 100755 --- a/core/arch/arm/plat-nuvoton/main.c +++ b/core/arch/arm/plat-nuvoton/main.c @@ -10,8 +10,10 @@ #include #include #include +#include #include #include +#include #include #define COLOR_NORMAL "\x1B[0m" @@ -23,11 +25,16 @@ #define COLOR_CYAN "\x1B[36m" #define COLOR_WHITE "\x1B[37m" +#define NPCM_MEASURE_BASE 0xF0848000 +#define NPCM_MEASURE_DME 0x0E0 +#define NPCM_MEASURE_SIZE 64 + static struct ns16550_data console_data __nex_bss; register_phys_mem_pgdir(MEM_AREA_IO_SEC, CONSOLE_UART_BASE, UART_REG_SIZE); register_phys_mem_pgdir(MEM_AREA_IO_SEC, GICD_BASE, GIC_DIST_REG_SIZE); register_phys_mem_pgdir(MEM_AREA_IO_SEC, GICC_BASE, GIC_DIST_REG_SIZE); +register_phys_mem_pgdir(MEM_AREA_RAM_NSEC, NPCM_MEASURE_BASE, SMALL_PAGE_SIZE); register_ddr(DRAM0_BASE, DRAM0_SIZE); @@ -53,3 +60,33 @@ void console_init(void) ns16550_init(&console_data, CONSOLE_UART_BASE, IO_WIDTH_U32, 2); register_serial_console(&console_data.chip); } + +TEE_Result tee_otp_get_hw_unique_key(struct tee_hw_unique_key *hwkey) +{ + void *vaddr; + TEE_Result res = TEE_SUCCESS; + uint8_t buf[NPCM_MEASURE_SIZE] = {0}; + uint32_t bin[1 + HW_UNIQUE_KEY_LENGTH / sizeof(uint32_t)]; + uint8_t *bin_val = (uint8_t *)(&bin[1]); + + vaddr = phys_to_virt(NPCM_MEASURE_BASE + NPCM_MEASURE_DME, + MEM_AREA_RAM_NSEC, NPCM_MEASURE_SIZE); + if (!vaddr) { + EMSG("Not enough memory mapped"); + return TEE_ERROR_SECURITY; + } + + memcpy(buf, vaddr, NPCM_MEASURE_SIZE); + res = tee_hash_createdigest(TEE_ALG_SHA256, buf, NPCM_MEASURE_SIZE, + bin_val, HW_UNIQUE_KEY_LENGTH); + if (res != TEE_SUCCESS) { + EMSG("Can't create a digest for HUK"); + return TEE_ERROR_SECURITY; + } + + memcpy(&hwkey->data[0], bin, HW_UNIQUE_KEY_LENGTH); + + IMSG("HUK Initialized"); + + return res; +}