Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #include <attestation.h> |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 7 | #include <buffer.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 8 | #include <debug.h> |
| 9 | #include <granule.h> |
| 10 | #include <measurement.h> |
| 11 | #include <realm.h> |
AlexeiFedorov | 5b186ad | 2023-04-26 14:43:18 +0100 | [diff] [blame] | 12 | #include <rsi-handler.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 13 | #include <smc-rsi.h> |
| 14 | #include <smc.h> |
| 15 | #include <string.h> |
| 16 | #include <utils_def.h> |
| 17 | |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 18 | #define MAX_EXTENDED_SIZE (64U) |
AlexeiFedorov | 7b3c304 | 2023-06-28 15:41:11 +0100 | [diff] [blame] | 19 | #define MAX_MEASUREMENT_WORDS (MAX_MEASUREMENT_SIZE / sizeof(unsigned long)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Return the Realm Personalization Value. |
| 22 | * |
| 23 | * Arguments: |
| 24 | * rd - The Realm descriptor. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 25 | * claim_ptr - The start address of the Realm Personalization Value claim |
| 26 | * claim_len - The length of the Realm Personalization Value claim |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 27 | */ |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 28 | static void get_rpv(struct rd *rd, void **claim_ptr, size_t *claim_len) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 29 | { |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 30 | *claim_ptr = (uint8_t *)&(rd->rpv[0]); |
| 31 | *claim_len = RPV_SIZE; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 35 | * Function to continue with the sign operation |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 36 | */ |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 37 | static void attest_token_continue_sign_state( |
| 38 | struct rec_attest_data *attest_data, |
| 39 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 40 | { |
| 41 | /* |
| 42 | * Sign and finish creating the token. |
| 43 | */ |
| 44 | enum attest_token_err_t ret = |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 45 | attest_realm_token_sign(&(attest_data->token_sign_ctx.ctx), |
| 46 | &(attest_data->rmm_realm_token_len)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 47 | if ((ret == ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS) || |
| 48 | (ret == ATTEST_TOKEN_ERR_SUCCESS)) { |
| 49 | /* |
| 50 | * Return to RSI handler function after each iteration |
| 51 | * to check is there anything else to do (pending IRQ) |
| 52 | * or next signing iteration can be executed. |
| 53 | */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 54 | res->smc_res.x[0] = RSI_INCOMPLETE; |
| 55 | |
| 56 | /* If this was the last signing cycle */ |
| 57 | if (ret == ATTEST_TOKEN_ERR_SUCCESS) { |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 58 | attest_data->token_sign_ctx.state = |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 59 | ATTEST_SIGN_TOKEN_WRITE_IN_PROGRESS; |
| 60 | } |
| 61 | } else { |
| 62 | /* Accessible only in case of failure during token signing */ |
| 63 | ERROR("FATAL_ERROR: Realm token creation failed\n"); |
| 64 | panic(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 69 | * Function to continue with the token write operation |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 70 | */ |
| 71 | static void attest_token_continue_write_state(struct rec *rec, |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 72 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 73 | { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 74 | struct granule *gr; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 75 | uintptr_t realm_att_token; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 76 | unsigned long realm_att_token_ipa = rec->regs[1]; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 77 | unsigned long offset = rec->regs[2]; |
| 78 | unsigned long size = rec->regs[3]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 79 | enum s2_walk_status walk_status; |
| 80 | struct s2_walk_result walk_res = { 0UL }; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 81 | size_t attest_token_len, length; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 82 | struct rec_attest_data *attest_data = rec->aux_data.attest_data; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 83 | uintptr_t cca_token_buf = rec->aux_data.cca_token_buf; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 86 | * Translate realm granule IPA to PA. If returns with |
| 87 | * WALK_SUCCESS then the last level page table (llt), |
| 88 | * which holds the realm_att_token_buf mapping, is locked. |
| 89 | */ |
AlexeiFedorov | d2e1bbd | 2023-04-18 15:18:39 +0100 | [diff] [blame] | 90 | walk_status = realm_ipa_to_pa(rec, realm_att_token_ipa, &walk_res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 91 | |
| 92 | /* Walk parameter validity was checked by RSI_ATTESTATION_TOKEN_INIT */ |
| 93 | assert(walk_status != WALK_INVALID_PARAMS); |
| 94 | |
| 95 | if (walk_status == WALK_FAIL) { |
AlexeiFedorov | d2e1bbd | 2023-04-18 15:18:39 +0100 | [diff] [blame] | 96 | if (walk_res.ripas_val == RIPAS_EMPTY) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 97 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 98 | } else { |
| 99 | /* |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 100 | * Translation failed, IPA is not mapped. |
| 101 | * Return to NS host to fix the issue. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 102 | */ |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 103 | res->action = STAGE_2_TRANSLATION_FAULT; |
| 104 | res->rtt_level = walk_res.rtt_level; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 105 | } |
| 106 | return; |
| 107 | } |
| 108 | |
Soby Mathew | 19eb433 | 2023-11-20 14:03:23 +0000 | [diff] [blame] | 109 | /* If size of buffer is 0, then return early. */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 110 | if (size == 0UL) { |
Soby Mathew | 19eb433 | 2023-11-20 14:03:23 +0000 | [diff] [blame] | 111 | res->smc_res.x[0] = RSI_INCOMPLETE; |
| 112 | goto out_unlock; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 115 | /* Map realm data granule to RMM address space */ |
| 116 | gr = find_granule(walk_res.pa); |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 117 | realm_att_token = (uintptr_t)buffer_granule_map(gr, SLOT_RSI_CALL); |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 118 | assert(realm_att_token != 0UL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 119 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 120 | if (attest_data->token_sign_ctx.copied_len == 0UL) { |
| 121 | attest_token_len = attest_cca_token_create( |
| 122 | (void *)cca_token_buf, |
| 123 | REC_ATTEST_TOKEN_BUF_SIZE, |
| 124 | &attest_data->rmm_realm_token_buf, |
| 125 | attest_data->rmm_realm_token_len); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 126 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 127 | if (attest_token_len == 0UL) { |
| 128 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 129 | |
| 130 | /* The signing has failed. Reset the state. */ |
| 131 | attest_data->token_sign_ctx.state = |
| 132 | ATTEST_SIGN_NOT_STARTED; |
| 133 | goto out_unmap; |
| 134 | } |
| 135 | |
| 136 | attest_data->token_sign_ctx.cca_token_len = attest_token_len; |
| 137 | } else { |
| 138 | attest_token_len = attest_data->token_sign_ctx.cca_token_len; |
| 139 | } |
| 140 | |
| 141 | length = (size < attest_token_len) ? size : attest_token_len; |
| 142 | |
| 143 | /* Copy attestation token */ |
| 144 | (void)memcpy((void *)(realm_att_token + offset), |
| 145 | (void *)(cca_token_buf + |
| 146 | attest_data->token_sign_ctx.copied_len), |
| 147 | length); |
| 148 | |
| 149 | attest_token_len -= length; |
| 150 | |
| 151 | if (attest_token_len != 0UL) { |
| 152 | attest_data->token_sign_ctx.cca_token_len = attest_token_len; |
| 153 | attest_data->token_sign_ctx.copied_len += length; |
| 154 | |
| 155 | res->smc_res.x[0] = RSI_INCOMPLETE; |
| 156 | } else { |
| 157 | |
| 158 | /* The signing has succeeded. Reset the state. */ |
| 159 | attest_data->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED; |
| 160 | res->smc_res.x[0] = RSI_SUCCESS; |
| 161 | } |
| 162 | |
| 163 | res->smc_res.x[1] = length; |
| 164 | |
| 165 | out_unmap: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 166 | /* Unmap realm granule */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 167 | buffer_unmap((void *)realm_att_token); |
Soby Mathew | 19eb433 | 2023-11-20 14:03:23 +0000 | [diff] [blame] | 168 | out_unlock: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 169 | /* Unlock last level page table (walk_res.g_llt) */ |
| 170 | granule_unlock(walk_res.llt); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 171 | } |
| 172 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 173 | void handle_rsi_attest_token_init(struct rec *rec, struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 174 | { |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 175 | struct rd *rd; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 176 | struct rec_attest_data *attest_data; |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 177 | void *rpv_ptr; |
| 178 | size_t rpv_len; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 179 | int att_ret; |
| 180 | |
| 181 | assert(rec != NULL); |
| 182 | |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 183 | attest_data = rec->aux_data.attest_data; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 184 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 185 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 186 | /* |
| 187 | * Calling RSI_ATTESTATION_TOKEN_INIT any time aborts any ongoing |
| 188 | * operation. |
| 189 | * TODO: This can be moved to attestation lib |
| 190 | */ |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 191 | if (attest_data->token_sign_ctx.state != ATTEST_SIGN_NOT_STARTED) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 192 | int restart; |
| 193 | |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 194 | attest_data->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 195 | restart = attestation_heap_reinit_pe(rec->aux_data.attest_heap_buf, |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 196 | REC_HEAP_SIZE); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 197 | if (restart != 0) { |
| 198 | /* There is no provision for this failure so panic */ |
| 199 | panic(); |
| 200 | } |
| 201 | } |
| 202 | |
AlexeiFedorov | 2dcd79f | 2023-10-17 10:04:11 +0100 | [diff] [blame] | 203 | /* Clear context for signing an attestation token */ |
| 204 | (void)memset(&attest_data->token_sign_ctx, 0, |
| 205 | sizeof(struct token_sign_cntxt)); |
| 206 | |
| 207 | attest_data->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED; |
| 208 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 209 | /* |
| 210 | * rd lock is acquired so that measurement cannot be updated |
| 211 | * simultaneously by another rec |
| 212 | */ |
| 213 | granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD); |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 214 | rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 215 | assert(rd != NULL); |
| 216 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 217 | /* Save challenge value in the context */ |
| 218 | (void)memcpy((void *)attest_data->token_sign_ctx.challenge, |
| 219 | (const void *)&rec->regs[1], |
| 220 | ATTEST_CHALLENGE_SIZE); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 221 | |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 222 | get_rpv(rd, &rpv_ptr, &rpv_len); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 223 | att_ret = attest_realm_token_create(rd->algorithm, rd->measurement, |
| 224 | MEASUREMENT_SLOT_NR, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 225 | rpv_ptr, |
| 226 | rpv_len, |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 227 | &attest_data->token_sign_ctx, |
| 228 | attest_data->rmm_realm_token_buf, |
| 229 | sizeof(attest_data->rmm_realm_token_buf)); |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 230 | buffer_unmap(rd); |
| 231 | granule_unlock(rec->realm_info.g_rd); |
| 232 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 233 | if (att_ret != 0) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 234 | ERROR("FATAL_ERROR: Realm token creation failed\n"); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 235 | panic(); |
| 236 | } |
| 237 | |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 238 | attest_data->token_sign_ctx.state = ATTEST_SIGN_IN_PROGRESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 239 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 240 | res->smc_res.x[0] = RSI_SUCCESS; |
AlexeiFedorov | 755a70b | 2023-10-12 12:31:45 +0100 | [diff] [blame] | 241 | res->smc_res.x[1] = REC_ATTEST_TOKEN_BUF_SIZE; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Return 'false' if no IRQ is pending, |
| 246 | * return 'true' if there is an IRQ pending, and need to return to Host. |
| 247 | */ |
| 248 | static bool check_pending_irq(void) |
| 249 | { |
| 250 | return (read_isr_el1() != 0UL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 253 | void handle_rsi_attest_token_continue(struct rec *rec, |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 254 | struct rmi_rec_exit *rec_exit, |
| 255 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 256 | { |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 257 | struct rec_attest_data *attest_data; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 258 | unsigned long realm_buf_ipa, offset, size; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 259 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 260 | assert(rec != NULL); |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 261 | assert(rec_exit != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 262 | |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 263 | attest_data = rec->aux_data.attest_data; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 264 | res->action = UPDATE_REC_RETURN_TO_REALM; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 265 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 266 | realm_buf_ipa = rec->regs[1]; |
| 267 | offset = rec->regs[2]; |
| 268 | size = rec->regs[3]; |
| 269 | |
| 270 | if (!GRANULE_ALIGNED(realm_buf_ipa) || |
| 271 | (offset >= GRANULE_SIZE) || |
| 272 | ((offset + size) > GRANULE_SIZE) || |
| 273 | ((offset + size) < offset)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 274 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 275 | return; |
| 276 | } |
| 277 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 278 | if (!addr_in_rec_par(rec, realm_buf_ipa)) { |
| 279 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 280 | return; |
| 281 | } |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 282 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 283 | if (attest_data->token_sign_ctx.state == ATTEST_SIGN_NOT_STARTED) { |
| 284 | /* |
| 285 | * Before this call the initial attestation token call |
| 286 | * (SMC_RSI_ATTEST_TOKEN_INIT) must have been executed |
| 287 | * successfully. |
| 288 | */ |
| 289 | res->smc_res.x[0] = RSI_ERROR_STATE; |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | while (attest_data->token_sign_ctx.state == ATTEST_SIGN_IN_PROGRESS) { |
| 294 | attest_token_continue_sign_state(attest_data, res); |
| 295 | if (check_pending_irq()) { |
| 296 | res->action = UPDATE_REC_EXIT_TO_HOST; |
| 297 | rec_exit->exit_reason = RMI_EXIT_IRQ; |
| 298 | return; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 299 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 300 | } |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 301 | |
| 302 | /* Any other state is considered an error */ |
| 303 | assert(attest_data->token_sign_ctx.state == |
| 304 | ATTEST_SIGN_TOKEN_WRITE_IN_PROGRESS); |
| 305 | |
| 306 | attest_token_continue_write_state(rec, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 307 | } |
| 308 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 309 | void handle_rsi_measurement_extend(struct rec *rec, struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 310 | { |
| 311 | struct granule *g_rd; |
| 312 | struct rd *rd; |
| 313 | unsigned long index; |
| 314 | unsigned long rd_addr; |
| 315 | size_t size; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 316 | void *extend_measurement; |
| 317 | unsigned char *current_measurement; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 318 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 319 | assert(rec != NULL); |
| 320 | |
| 321 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 322 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 323 | /* |
| 324 | * rd lock is acquired so that measurement cannot be updated |
| 325 | * simultaneously by another rec |
| 326 | */ |
| 327 | rd_addr = granule_addr(rec->realm_info.g_rd); |
| 328 | g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD); |
| 329 | |
| 330 | assert(g_rd != NULL); |
| 331 | |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 332 | rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 333 | assert(rd != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * X1: index |
| 337 | * X2: size |
| 338 | * X3-X10: measurement value |
| 339 | */ |
| 340 | index = rec->regs[1]; |
| 341 | |
| 342 | if ((index == RIM_MEASUREMENT_SLOT) || |
| 343 | (index >= MEASUREMENT_SLOT_NR)) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 344 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 345 | goto out_unmap_rd; |
| 346 | } |
| 347 | |
| 348 | size = rec->regs[2]; |
| 349 | |
| 350 | if (size > MAX_EXTENDED_SIZE) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 351 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 352 | goto out_unmap_rd; |
| 353 | } |
| 354 | |
| 355 | extend_measurement = &rec->regs[3]; |
| 356 | current_measurement = rd->measurement[index]; |
| 357 | |
| 358 | measurement_extend(rd->algorithm, |
| 359 | current_measurement, |
| 360 | extend_measurement, |
| 361 | size, |
| 362 | current_measurement); |
| 363 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 364 | res->smc_res.x[0] = RSI_SUCCESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 365 | |
| 366 | out_unmap_rd: |
| 367 | buffer_unmap(rd); |
| 368 | granule_unlock(g_rd); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 369 | } |
| 370 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 371 | void handle_rsi_measurement_read(struct rec *rec, struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 372 | { |
| 373 | struct rd *rd; |
| 374 | unsigned long idx; |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 375 | unsigned int i, cnt; |
Mate Toth-Pal | 59b52d0 | 2023-08-18 14:14:19 +0200 | [diff] [blame] | 376 | unsigned long *measurement_value_part; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 377 | |
| 378 | assert(rec != NULL); |
| 379 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 380 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 381 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 382 | /* X1: Index */ |
| 383 | idx = rec->regs[1]; |
| 384 | |
| 385 | if (idx >= MEASUREMENT_SLOT_NR) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 386 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 387 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
| 391 | * rd lock is acquired so that measurement cannot be updated |
| 392 | * simultaneously by another rec |
| 393 | */ |
| 394 | granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD); |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 395 | rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 396 | assert(rd != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 397 | |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 398 | /* Number of 8-bytes words in measurement */ |
AlexeiFedorov | 4faab85 | 2023-08-30 15:06:49 +0100 | [diff] [blame] | 399 | cnt = (unsigned int)(measurement_get_size(rd->algorithm) / |
| 400 | sizeof(unsigned long)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 401 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 402 | assert(cnt >= (SMC_RESULT_REGS - 1U)); |
Mate Toth-Pal | 59b52d0 | 2023-08-18 14:14:19 +0200 | [diff] [blame] | 403 | assert(cnt < ARRAY_LEN(rec->regs)); |
| 404 | |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 405 | /* Copy the part of the measurement to res->smc_res.x[] */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 406 | for (i = 0U; i < (SMC_RESULT_REGS - 1U); i++) { |
Mate Toth-Pal | 59b52d0 | 2023-08-18 14:14:19 +0200 | [diff] [blame] | 407 | measurement_value_part = (unsigned long *) |
| 408 | &(rd->measurement[idx][i * sizeof(unsigned long)]); |
| 409 | res->smc_res.x[i + 1U] = *measurement_value_part; |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 410 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 411 | |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 412 | /* Copy the rest of the measurement to the rec->regs[] */ |
| 413 | for (; i < cnt; i++) { |
Mate Toth-Pal | 59b52d0 | 2023-08-18 14:14:19 +0200 | [diff] [blame] | 414 | measurement_value_part = (unsigned long *) |
| 415 | &(rd->measurement[idx][i * sizeof(unsigned long)]); |
| 416 | rec->regs[i + 1U] = *measurement_value_part; |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* Zero-initialize unused area */ |
| 420 | for (; i < MAX_MEASUREMENT_WORDS; i++) { |
| 421 | rec->regs[i + 1U] = 0UL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | buffer_unmap(rd); |
| 425 | granule_unlock(rec->realm_info.g_rd); |
| 426 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 427 | res->smc_res.x[0] = RSI_SUCCESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 428 | } |