Maulik Patel | d314270 | 2022-06-22 10:09:13 +0100 | [diff] [blame] | 1 | /* |
Jamie Fox | 6ab56cc | 2023-07-04 14:51:18 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2022-2023, Arm Limited. All rights reserved. |
Maulik Patel | d314270 | 2022-06-22 10:09:13 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <string.h> |
| 9 | #include "measured_boot.h" |
| 10 | #include "measured_boot_defs.h" |
| 11 | #include "tfm_sp_log.h" |
| 12 | |
| 13 | #include "psa/service.h" |
| 14 | #include "psa_manifest/pid.h" |
| 15 | #include "psa/crypto.h" |
| 16 | #include "psa_manifest/tfm_measured_boot.h" |
| 17 | |
Jamie Fox | 6ab56cc | 2023-07-04 14:51:18 +0100 | [diff] [blame^] | 18 | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
| 19 | |
Maulik Patel | d314270 | 2022-06-22 10:09:13 +0100 | [diff] [blame] | 20 | /* TODO: This info will be used later on as input to decide access control */ |
| 21 | static int32_t g_measured_boot_caller_id; |
| 22 | |
| 23 | static psa_status_t read_measurements(const psa_msg_t *msg) |
| 24 | { |
| 25 | struct measured_boot_read_iovec_in_t read_iov_in; |
| 26 | struct measured_boot_read_iovec_out_t read_iov_out; |
| 27 | size_t signer_id_len, measurement_value_len; |
| 28 | size_t num; |
| 29 | size_t signer_id_size, measurement_value_size; |
| 30 | uint8_t signer_id[SIGNER_ID_MAX_SIZE] = {0}; |
| 31 | uint8_t measurement_value[MEASUREMENT_VALUE_SIZE] = {0}; |
| 32 | psa_status_t status; |
| 33 | |
| 34 | /* store the client ID here for later use in service */ |
| 35 | g_measured_boot_caller_id = msg->client_id; |
| 36 | |
Jamie Fox | 6ab56cc | 2023-07-04 14:51:18 +0100 | [diff] [blame^] | 37 | /* Limit requested sizes to the size of the local buffers */ |
| 38 | signer_id_size = MIN(msg->out_size[1], sizeof(signer_id)); |
| 39 | measurement_value_size = MIN(msg->out_size[2], sizeof(measurement_value)); |
Maulik Patel | d314270 | 2022-06-22 10:09:13 +0100 | [diff] [blame] | 40 | |
| 41 | /* Check input parameter */ |
| 42 | if ((msg->in_size[0] != sizeof(struct measured_boot_read_iovec_in_t)) || |
| 43 | (msg->out_size[0] != sizeof(struct measured_boot_read_iovec_out_t))) { |
| 44 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 45 | } |
| 46 | |
| 47 | num = psa_read(msg->handle, 0, &read_iov_in, sizeof(read_iov_in)); |
| 48 | if (num != sizeof(read_iov_in)) { |
| 49 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 50 | } |
| 51 | |
| 52 | /* Validate requested slot number */ |
| 53 | if (read_iov_in.index >= NUM_OF_MEASUREMENT_SLOTS) { |
| 54 | return PSA_ERROR_INVALID_ARGUMENT; |
| 55 | } |
| 56 | |
| 57 | memset(&read_iov_out, 0, sizeof(read_iov_out)); |
| 58 | |
| 59 | status = measured_boot_read_measurement(read_iov_in.index, |
| 60 | &signer_id[0], |
| 61 | signer_id_size, |
| 62 | &signer_id_len, |
| 63 | &read_iov_out.version[0], |
| 64 | read_iov_in.version_size, |
| 65 | &read_iov_out.version_len, |
| 66 | &read_iov_out.measurement_algo, |
| 67 | &read_iov_out.sw_type[0], |
| 68 | read_iov_in.sw_type_size, |
| 69 | &read_iov_out.sw_type_len, |
| 70 | &measurement_value[0], |
| 71 | measurement_value_size, |
| 72 | &measurement_value_len, |
| 73 | &read_iov_out.is_locked); |
| 74 | |
| 75 | if (status == PSA_SUCCESS) { |
| 76 | psa_write(msg->handle, 0, &read_iov_out, sizeof(read_iov_out)); |
| 77 | psa_write(msg->handle, 1, &signer_id[0], signer_id_len); |
| 78 | psa_write(msg->handle, 2, &measurement_value[0], measurement_value_len); |
| 79 | } |
| 80 | |
| 81 | return status; |
| 82 | } |
| 83 | |
| 84 | static psa_status_t extend_measurement(const psa_msg_t *msg) |
| 85 | { |
| 86 | struct measured_boot_extend_iovec_t extend_iov; |
| 87 | size_t num; |
| 88 | size_t signer_id_size; |
| 89 | size_t version_size; |
| 90 | size_t measurement_value_size; |
| 91 | uint8_t signer_id[SIGNER_ID_MAX_SIZE] = {0}; |
| 92 | uint8_t version[VERSION_MAX_SIZE] = {0}; |
| 93 | uint8_t measurement_value[MEASUREMENT_VALUE_MAX_SIZE] = {0}; |
| 94 | |
| 95 | /* store the client ID here for later use in service */ |
| 96 | g_measured_boot_caller_id = msg->client_id; |
| 97 | |
| 98 | /* Check input parameter */ |
| 99 | if (msg->in_size[0] != sizeof(struct measured_boot_extend_iovec_t)) { |
| 100 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 101 | } |
| 102 | |
| 103 | signer_id_size = msg->in_size[1]; |
| 104 | version_size = msg->in_size[2]; |
| 105 | measurement_value_size = msg->in_size[3]; |
| 106 | |
| 107 | memset(&extend_iov, 0, sizeof(extend_iov)); |
| 108 | num = psa_read(msg->handle, 0, &extend_iov, |
| 109 | sizeof(struct measured_boot_extend_iovec_t)); |
| 110 | if (num != sizeof(struct measured_boot_extend_iovec_t)) { |
| 111 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 112 | } |
| 113 | |
| 114 | /* Validate size limits of input parameters */ |
| 115 | if ((signer_id_size < SIGNER_ID_MIN_SIZE) || |
| 116 | (signer_id_size > SIGNER_ID_MAX_SIZE) || |
| 117 | (version_size > VERSION_MAX_SIZE) || |
| 118 | (measurement_value_size < MEASUREMENT_VALUE_MIN_SIZE) || |
| 119 | (measurement_value_size > MEASUREMENT_VALUE_MAX_SIZE) || |
| 120 | (extend_iov.sw_type_size > SW_TYPE_MAX_SIZE)) { |
| 121 | return PSA_ERROR_INVALID_ARGUMENT; |
| 122 | } |
| 123 | |
| 124 | /* Validate requested slot number */ |
| 125 | if (extend_iov.index >= NUM_OF_MEASUREMENT_SLOTS) { |
| 126 | return PSA_ERROR_INVALID_ARGUMENT; |
| 127 | } |
| 128 | |
| 129 | num = psa_read(msg->handle, 1, signer_id, signer_id_size); |
| 130 | if (num != signer_id_size) { |
| 131 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 132 | } |
| 133 | |
| 134 | num = psa_read(msg->handle, 2, version, version_size); |
| 135 | if (num != version_size) { |
| 136 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 137 | } |
| 138 | |
| 139 | num = psa_read(msg->handle, 3, &measurement_value[0], |
| 140 | measurement_value_size); |
| 141 | if (num != measurement_value_size) { |
| 142 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 143 | } |
| 144 | |
| 145 | return measured_boot_extend_measurement(extend_iov.index, |
| 146 | signer_id, |
| 147 | signer_id_size, |
| 148 | version, |
| 149 | version_size, |
| 150 | extend_iov.measurement_algo, |
| 151 | &extend_iov.sw_type[0], |
| 152 | extend_iov.sw_type_size, |
| 153 | measurement_value, |
| 154 | measurement_value_size, |
| 155 | extend_iov.lock_measurement); |
| 156 | } |
| 157 | |
| 158 | static void measured_boot_signal_handle(psa_signal_t signal) |
| 159 | { |
| 160 | psa_status_t status; |
| 161 | psa_msg_t msg; |
| 162 | |
| 163 | /* Retrieve the message corresponding to the measured_boot service signal */ |
| 164 | status = psa_get(signal, &msg); |
| 165 | if (status != PSA_SUCCESS) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | /* Decode the message */ |
| 170 | switch (msg.type) { |
| 171 | case TFM_MEASURED_BOOT_READ: |
| 172 | status = read_measurements(&msg); |
| 173 | /* Reply with the message result status to unblock the client */ |
| 174 | psa_reply(msg.handle, status); |
| 175 | break; |
| 176 | case TFM_MEASURED_BOOT_EXTEND: |
| 177 | status = extend_measurement(&msg); |
| 178 | /* Reply with the message result status to unblock the client */ |
| 179 | psa_reply(msg.handle, status); |
| 180 | break; |
| 181 | default: |
| 182 | /* Invalid message type */ |
| 183 | status = PSA_ERROR_NOT_SUPPORTED; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * \brief The measured_boot partition's entry function. |
| 190 | */ |
| 191 | psa_status_t tfm_measured_boot_init(void) |
| 192 | { |
| 193 | psa_status_t status = PSA_SUCCESS; |
| 194 | |
| 195 | /* Initialise all measurements & related metadata */ |
| 196 | initialise_all_measurements(); |
| 197 | |
| 198 | LOG_DBGFMT("Measured Boot : selected algorithm: %x\r\n", |
| 199 | TFM_MEASURED_BOOT_HASH_ALG); |
| 200 | |
Jamie Fox | 5fb7a13 | 2022-08-01 17:26:03 +0100 | [diff] [blame] | 201 | #ifdef CONFIG_TFM_BOOT_STORE_MEASUREMENTS |
Maulik Patel | d314270 | 2022-06-22 10:09:13 +0100 | [diff] [blame] | 202 | status = collect_shared_measurements(); |
| 203 | #endif |
| 204 | |
| 205 | if (status != PSA_SUCCESS) { |
| 206 | psa_panic(); |
| 207 | } |
| 208 | |
| 209 | psa_signal_t signals = 0; |
| 210 | |
| 211 | while (1) { |
| 212 | signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK); |
| 213 | if (signals & TFM_MEASURED_BOOT_SIGNAL) { |
| 214 | measured_boot_signal_handle(TFM_MEASURED_BOOT_SIGNAL); |
| 215 | } else { |
| 216 | psa_panic(); |
| 217 | } |
| 218 | } |
| 219 | } |