Max Shvetsov | 06dba29 | 2019-12-06 11:50:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, ARM Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <stdint.h> |
| 9 | #include <string.h> |
| 10 | |
Max Shvetsov | 06dba29 | 2019-12-06 11:50:12 +0000 | [diff] [blame] | 11 | #include <plat/arm/common/plat_arm.h> |
| 12 | #include <plat/common/common_def.h> |
| 13 | #include <plat/common/platform.h> |
| 14 | |
| 15 | #if (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_REGS_ID) |
| 16 | |
| 17 | static unsigned char rotpk_hash_der[ARM_ROTPK_HEADER_LEN + ARM_ROTPK_HASH_LEN]; |
| 18 | |
| 19 | extern unsigned char arm_rotpk_header[]; |
| 20 | |
| 21 | /* |
| 22 | * Return the ROTPK hash stored in the registers of Juno board. |
| 23 | */ |
| 24 | static int juno_get_rotpk_info_regs(void **key_ptr, unsigned int *key_len, |
| 25 | unsigned int *flags) |
| 26 | { |
| 27 | uint8_t *dst; |
| 28 | uint32_t *src, tmp; |
| 29 | unsigned int words, i; |
| 30 | |
| 31 | assert(key_ptr != NULL); |
| 32 | assert(key_len != NULL); |
| 33 | assert(flags != NULL); |
| 34 | |
| 35 | /* Copy the DER header */ |
| 36 | memcpy(rotpk_hash_der, arm_rotpk_header, ARM_ROTPK_HEADER_LEN); |
| 37 | dst = (uint8_t *)&rotpk_hash_der[ARM_ROTPK_HEADER_LEN]; |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * Append the hash from Trusted Root-Key Storage registers. The hash has |
| 42 | * not been written linearly into the registers, so we have to do a bit |
| 43 | * of byte swapping: |
| 44 | * |
| 45 | * 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C |
| 46 | * +---------------------------------------------------------------+ |
| 47 | * | Reg0 | Reg1 | Reg2 | Reg3 | Reg4 | Reg5 | Reg6 | Reg7 | |
| 48 | * +---------------------------------------------------------------+ |
| 49 | * | ... ... | | ... ... | |
| 50 | * | +--------------------+ | +-------+ |
| 51 | * | | | | |
| 52 | * +----------------------------+ +----------------------------+ |
| 53 | * | | | | |
| 54 | * +-------+ | +--------------------+ | |
| 55 | * | | | | |
| 56 | * v v v v |
| 57 | * +---------------------------------------------------------------+ |
| 58 | * | | | |
| 59 | * +---------------------------------------------------------------+ |
| 60 | * 0 15 16 31 |
| 61 | * |
| 62 | * Additionally, we have to access the registers in 32-bit words |
| 63 | */ |
| 64 | words = ARM_ROTPK_HASH_LEN >> 3; |
| 65 | |
| 66 | /* Swap bytes 0-15 (first four registers) */ |
| 67 | src = (uint32_t *)TZ_PUB_KEY_HASH_BASE; |
| 68 | for (i = 0 ; i < words ; i++) { |
| 69 | tmp = src[words - 1 - i]; |
| 70 | /* Words are read in little endian */ |
| 71 | *dst++ = (uint8_t)((tmp >> 24) & 0xFF); |
| 72 | *dst++ = (uint8_t)((tmp >> 16) & 0xFF); |
| 73 | *dst++ = (uint8_t)((tmp >> 8) & 0xFF); |
| 74 | *dst++ = (uint8_t)(tmp & 0xFF); |
| 75 | } |
| 76 | |
| 77 | /* Swap bytes 16-31 (last four registers) */ |
| 78 | src = (uint32_t *)(TZ_PUB_KEY_HASH_BASE + ARM_ROTPK_HASH_LEN / 2); |
| 79 | for (i = 0 ; i < words ; i++) { |
| 80 | tmp = src[words - 1 - i]; |
| 81 | *dst++ = (uint8_t)((tmp >> 24) & 0xFF); |
| 82 | *dst++ = (uint8_t)((tmp >> 16) & 0xFF); |
| 83 | *dst++ = (uint8_t)((tmp >> 8) & 0xFF); |
| 84 | *dst++ = (uint8_t)(tmp & 0xFF); |
| 85 | } |
| 86 | |
| 87 | *key_ptr = (void *)rotpk_hash_der; |
| 88 | *key_len = (unsigned int)sizeof(rotpk_hash_der); |
| 89 | *flags = ROTPK_IS_HASH; |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | #endif |
| 94 | |
| 95 | /* |
| 96 | * Return the ROTPK hash in the following ASN.1 structure in DER format: |
| 97 | * |
| 98 | * AlgorithmIdentifier ::= SEQUENCE { |
| 99 | * algorithm OBJECT IDENTIFIER, |
| 100 | * parameters ANY DEFINED BY algorithm OPTIONAL |
| 101 | * } |
| 102 | * |
| 103 | * DigestInfo ::= SEQUENCE { |
| 104 | * digestAlgorithm AlgorithmIdentifier, |
| 105 | * digest OCTET STRING |
| 106 | * } |
| 107 | */ |
| 108 | int plat_get_rotpk_info(void *cookie, void **key_ptr, unsigned int *key_len, |
| 109 | unsigned int *flags) |
| 110 | { |
Max Shvetsov | 06dba29 | 2019-12-06 11:50:12 +0000 | [diff] [blame] | 111 | #if (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_RSA_ID) || \ |
| 112 | (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_ECDSA_ID) |
| 113 | return arm_get_rotpk_info_dev(key_ptr, key_len, flags); |
| 114 | #elif (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_REGS_ID) |
| 115 | return juno_get_rotpk_info_regs(key_ptr, key_len, flags); |
| 116 | #else |
| 117 | return 1; |
| 118 | #endif |
Max Shvetsov | 06dba29 | 2019-12-06 11:50:12 +0000 | [diff] [blame] | 119 | } |