blob: 1da5968848c36755aaa1bb92c9e88488a2ec04f7 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#include <attestation.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00007#include <debug.h>
8#include <granule.h>
9#include <measurement.h>
10#include <realm.h>
AlexeiFedorov5b186ad2023-04-26 14:43:18 +010011#include <rsi-handler.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000012#include <smc-rsi.h>
13#include <smc.h>
14#include <string.h>
15#include <utils_def.h>
16
AlexeiFedorovefe2aec2023-06-08 16:17:00 +010017#define MAX_EXTENDED_SIZE (64U)
18#define MAX_MEASUREMENT_WORDS (MAX_MEASUREMENT_SIZE / sizeof(unsigned long))
Soby Mathewb4c6df42022-11-09 11:13:29 +000019/*
20 * Return the Realm Personalization Value.
21 *
22 * Arguments:
23 * rd - The Realm descriptor.
Mate Toth-Pal071aa562023-07-04 09:09:26 +020024 * claim_ptr - The start address of the Realm Personalization Value claim
25 * claim_len - The length of the Realm Personalization Value claim
Soby Mathewb4c6df42022-11-09 11:13:29 +000026 */
Mate Toth-Pal071aa562023-07-04 09:09:26 +020027static void get_rpv(struct rd *rd, void **claim_ptr, size_t *claim_len)
Soby Mathewb4c6df42022-11-09 11:13:29 +000028{
Mate Toth-Pal071aa562023-07-04 09:09:26 +020029 *claim_ptr = (uint8_t *)&(rd->rpv[0]);
30 *claim_len = RPV_SIZE;
Soby Mathewb4c6df42022-11-09 11:13:29 +000031}
32
33/*
34 * Save the input parameters in the context for later iterations to check for
35 * consistency.
36 */
37static void save_input_parameters(struct rec *rec)
38{
AlexeiFedorovec35c542023-04-27 17:52:02 +010039 struct rec_attest_data *attest_data = rec->aux_data.attest_data;
40
41 attest_data->token_sign_ctx.token_ipa = rec->regs[1];
42 (void)memcpy(attest_data->token_sign_ctx.challenge, &rec->regs[2],
Soby Mathewb4c6df42022-11-09 11:13:29 +000043 ATTEST_CHALLENGE_SIZE);
44}
45
46/*
47 * Verify that in all the iterations the input parameters are the same
48 * as in the initial call.
49 */
50static bool verify_input_parameters_consistency(struct rec *rec)
51{
AlexeiFedorovec35c542023-04-27 17:52:02 +010052 struct rec_attest_data *attest_data = rec->aux_data.attest_data;
53
54 return attest_data->token_sign_ctx.token_ipa == rec->regs[1];
Soby Mathewb4c6df42022-11-09 11:13:29 +000055}
56
57/*
AlexeiFedorov97844202023-04-27 15:17:35 +010058 * Function to continue with the sign operation
Soby Mathewb4c6df42022-11-09 11:13:29 +000059 */
AlexeiFedorovec35c542023-04-27 17:52:02 +010060static void attest_token_continue_sign_state(
61 struct rec_attest_data *attest_data,
62 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +000063{
64 /*
65 * Sign and finish creating the token.
66 */
67 enum attest_token_err_t ret =
AlexeiFedorovec35c542023-04-27 17:52:02 +010068 attest_realm_token_sign(&(attest_data->token_sign_ctx.ctx),
69 &(attest_data->rmm_realm_token_len));
Soby Mathewb4c6df42022-11-09 11:13:29 +000070
71 if ((ret == ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS) ||
72 (ret == ATTEST_TOKEN_ERR_SUCCESS)) {
73 /*
74 * Return to RSI handler function after each iteration
75 * to check is there anything else to do (pending IRQ)
76 * or next signing iteration can be executed.
77 */
Soby Mathewb4c6df42022-11-09 11:13:29 +000078 res->smc_res.x[0] = RSI_INCOMPLETE;
79
80 /* If this was the last signing cycle */
81 if (ret == ATTEST_TOKEN_ERR_SUCCESS) {
AlexeiFedorovec35c542023-04-27 17:52:02 +010082 attest_data->token_sign_ctx.state =
Soby Mathewb4c6df42022-11-09 11:13:29 +000083 ATTEST_SIGN_TOKEN_WRITE_IN_PROGRESS;
84 }
85 } else {
86 /* Accessible only in case of failure during token signing */
87 ERROR("FATAL_ERROR: Realm token creation failed\n");
88 panic();
89 }
90}
91
92/*
AlexeiFedorov97844202023-04-27 15:17:35 +010093 * Function to continue with the token write operation
Soby Mathewb4c6df42022-11-09 11:13:29 +000094 */
95static void attest_token_continue_write_state(struct rec *rec,
AlexeiFedorov97844202023-04-27 15:17:35 +010096 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +000097{
Soby Mathewb4c6df42022-11-09 11:13:29 +000098 struct granule *gr;
99 uint8_t *realm_att_token;
100 unsigned long realm_att_token_ipa = rec->regs[1];
101 enum s2_walk_status walk_status;
102 struct s2_walk_result walk_res = { 0UL };
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200103 size_t attest_token_len;
AlexeiFedorovec35c542023-04-27 17:52:02 +0100104 struct rec_attest_data *attest_data = rec->aux_data.attest_data;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000105
106 /*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000107 * Translate realm granule IPA to PA. If returns with
108 * WALK_SUCCESS then the last level page table (llt),
109 * which holds the realm_att_token_buf mapping, is locked.
110 */
AlexeiFedorovd2e1bbd2023-04-18 15:18:39 +0100111 walk_status = realm_ipa_to_pa(rec, realm_att_token_ipa, &walk_res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000112
113 /* Walk parameter validity was checked by RSI_ATTESTATION_TOKEN_INIT */
114 assert(walk_status != WALK_INVALID_PARAMS);
115
116 if (walk_status == WALK_FAIL) {
AlexeiFedorovd2e1bbd2023-04-18 15:18:39 +0100117 if (walk_res.ripas_val == RIPAS_EMPTY) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000118 res->smc_res.x[0] = RSI_ERROR_INPUT;
119 } else {
120 /*
AlexeiFedorov97844202023-04-27 15:17:35 +0100121 * Translation failed, IPA is not mapped.
122 * Return to NS host to fix the issue.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000123 */
AlexeiFedorov97844202023-04-27 15:17:35 +0100124 res->action = STAGE_2_TRANSLATION_FAULT;
125 res->rtt_level = walk_res.rtt_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000126 }
127 return;
128 }
129
130 /* Map realm data granule to RMM address space */
131 gr = find_granule(walk_res.pa);
132 realm_att_token = granule_map(gr, SLOT_RSI_CALL);
133
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200134 attest_token_len = attest_cca_token_create(realm_att_token,
AlexeiFedorovec35c542023-04-27 17:52:02 +0100135 ATTEST_TOKEN_BUFFER_SIZE,
136 &attest_data->rmm_realm_token_buf,
137 attest_data->rmm_realm_token_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000138
139 /* Unmap realm granule */
140 buffer_unmap(realm_att_token);
141
142 /* Unlock last level page table (walk_res.g_llt) */
143 granule_unlock(walk_res.llt);
144
145 /* Write output parameters */
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100146 if (attest_token_len == 0UL) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000147 res->smc_res.x[0] = RSI_ERROR_INPUT;
148 } else {
149 res->smc_res.x[0] = RSI_SUCCESS;
150 res->smc_res.x[1] = attest_token_len;
151 }
152
153 /* The signing has either succeeded or failed. Reset the state. */
AlexeiFedorovec35c542023-04-27 17:52:02 +0100154 attest_data->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000155}
156
AlexeiFedorov97844202023-04-27 15:17:35 +0100157void handle_rsi_attest_token_init(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000158{
159 struct rd *rd = NULL;
AlexeiFedorovec35c542023-04-27 17:52:02 +0100160 unsigned long realm_buf_ipa;
161 struct rec_attest_data *attest_data;
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200162 void *rpv_ptr;
163 size_t rpv_len;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000164 int att_ret;
165
166 assert(rec != NULL);
167
AlexeiFedorovec35c542023-04-27 17:52:02 +0100168 realm_buf_ipa = rec->regs[1];
169 attest_data = rec->aux_data.attest_data;
170
AlexeiFedorov97844202023-04-27 15:17:35 +0100171 res->action = UPDATE_REC_RETURN_TO_REALM;
172
Soby Mathewb4c6df42022-11-09 11:13:29 +0000173 /*
174 * Calling RSI_ATTESTATION_TOKEN_INIT any time aborts any ongoing
175 * operation.
176 * TODO: This can be moved to attestation lib
177 */
AlexeiFedorovec35c542023-04-27 17:52:02 +0100178 if (attest_data->token_sign_ctx.state != ATTEST_SIGN_NOT_STARTED) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000179 int restart;
180
AlexeiFedorovec35c542023-04-27 17:52:02 +0100181 attest_data->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000182 restart = attestation_heap_reinit_pe(rec->aux_data.attest_heap_buf,
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000183 REC_HEAP_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000184 if (restart != 0) {
185 /* There is no provision for this failure so panic */
186 panic();
187 }
188 }
189
190 if (!GRANULE_ALIGNED(realm_buf_ipa)) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100191 res->smc_res.x[0] = RSI_ERROR_INPUT;
192 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000193 }
194
195 /*
196 * rd lock is acquired so that measurement cannot be updated
197 * simultaneously by another rec
198 */
199 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
200 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
201 if (!addr_in_par(rd, realm_buf_ipa)) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100202 res->smc_res.x[0] = RSI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000203 goto out_unmap_rd;
204 }
205
206 /*
207 * Save the input parameters in the context for later iterations
208 * to check.
209 */
210 save_input_parameters(rec);
211
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200212 get_rpv(rd, &rpv_ptr, &rpv_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000213 att_ret = attest_realm_token_create(rd->algorithm, rd->measurement,
214 MEASUREMENT_SLOT_NR,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200215 rpv_ptr,
216 rpv_len,
AlexeiFedorovec35c542023-04-27 17:52:02 +0100217 &attest_data->token_sign_ctx,
218 attest_data->rmm_realm_token_buf,
219 sizeof(attest_data->rmm_realm_token_buf));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000220 if (att_ret != 0) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100221 ERROR("FATAL_ERROR: Realm token creation failed\n");
Soby Mathewb4c6df42022-11-09 11:13:29 +0000222 panic();
223 }
224
AlexeiFedorovec35c542023-04-27 17:52:02 +0100225 attest_data->token_sign_ctx.state = ATTEST_SIGN_IN_PROGRESS;
AlexeiFedorov97844202023-04-27 15:17:35 +0100226 res->smc_res.x[0] = RSI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000227
228out_unmap_rd:
229 buffer_unmap(rd);
230 granule_unlock(rec->realm_info.g_rd);
AlexeiFedorov97844202023-04-27 15:17:35 +0100231}
232
233/*
234 * Return 'false' if no IRQ is pending,
235 * return 'true' if there is an IRQ pending, and need to return to Host.
236 */
237static bool check_pending_irq(void)
238{
239 return (read_isr_el1() != 0UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000240}
241
Soby Mathewb4c6df42022-11-09 11:13:29 +0000242void handle_rsi_attest_token_continue(struct rec *rec,
AlexeiFedorov97844202023-04-27 15:17:35 +0100243 struct rmi_rec_exit *rec_exit,
244 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000245{
AlexeiFedorovec35c542023-04-27 17:52:02 +0100246 struct rec_attest_data *attest_data;
247
Soby Mathewb4c6df42022-11-09 11:13:29 +0000248 assert(rec != NULL);
AlexeiFedorov97844202023-04-27 15:17:35 +0100249 assert(rec_exit != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000250
AlexeiFedorovec35c542023-04-27 17:52:02 +0100251 attest_data = rec->aux_data.attest_data;
AlexeiFedorov97844202023-04-27 15:17:35 +0100252 res->action = UPDATE_REC_RETURN_TO_REALM;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000253
254 if (!verify_input_parameters_consistency(rec)) {
255 res->smc_res.x[0] = RSI_ERROR_INPUT;
256 return;
257 }
258
AlexeiFedorov97844202023-04-27 15:17:35 +0100259 while (true) {
AlexeiFedorovec35c542023-04-27 17:52:02 +0100260 switch (attest_data->token_sign_ctx.state) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100261 case ATTEST_SIGN_NOT_STARTED:
262 /*
263 * Before this call the initial attestation token call
264 * (SMC_RSI_ATTEST_TOKEN_INIT) must have been executed
265 * successfully.
266 */
267 res->smc_res.x[0] = RSI_ERROR_STATE;
268 break;
269 case ATTEST_SIGN_IN_PROGRESS:
AlexeiFedorovec35c542023-04-27 17:52:02 +0100270 attest_token_continue_sign_state(attest_data, res);
AlexeiFedorov97844202023-04-27 15:17:35 +0100271 break;
272 case ATTEST_SIGN_TOKEN_WRITE_IN_PROGRESS:
273 attest_token_continue_write_state(rec, res);
274 break;
275 default:
276 /* Any other state is considered an error */
277 panic();
278 }
279
280 if (res->smc_res.x[0] == RSI_INCOMPLETE) {
281 if (check_pending_irq()) {
282 res->action = UPDATE_REC_EXIT_TO_HOST;
283 res->smc_res.x[0] = RSI_INCOMPLETE;
284 rec_exit->exit_reason = RMI_EXIT_IRQ;
285 break;
286 }
287 if ((res->action & FLAG_EXIT_TO_HOST) != 0) {
288 break;
289 }
290 } else {
291 break;
292 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000293 }
294}
295
AlexeiFedorov97844202023-04-27 15:17:35 +0100296void handle_rsi_measurement_extend(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000297{
298 struct granule *g_rd;
299 struct rd *rd;
300 unsigned long index;
301 unsigned long rd_addr;
302 size_t size;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000303 void *extend_measurement;
304 unsigned char *current_measurement;
305 int __unused meas_ret;
306
AlexeiFedorov97844202023-04-27 15:17:35 +0100307 assert(rec != NULL);
308
309 res->action = UPDATE_REC_RETURN_TO_REALM;
310
Soby Mathewb4c6df42022-11-09 11:13:29 +0000311 /*
312 * rd lock is acquired so that measurement cannot be updated
313 * simultaneously by another rec
314 */
315 rd_addr = granule_addr(rec->realm_info.g_rd);
316 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
317
318 assert(g_rd != NULL);
319
320 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
321
322 /*
323 * X1: index
324 * X2: size
325 * X3-X10: measurement value
326 */
327 index = rec->regs[1];
328
329 if ((index == RIM_MEASUREMENT_SLOT) ||
330 (index >= MEASUREMENT_SLOT_NR)) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100331 res->smc_res.x[0] = RSI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000332 goto out_unmap_rd;
333 }
334
335 size = rec->regs[2];
336
337 if (size > MAX_EXTENDED_SIZE) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100338 res->smc_res.x[0] = RSI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000339 goto out_unmap_rd;
340 }
341
342 extend_measurement = &rec->regs[3];
343 current_measurement = rd->measurement[index];
344
345 measurement_extend(rd->algorithm,
346 current_measurement,
347 extend_measurement,
348 size,
349 current_measurement);
350
AlexeiFedorov97844202023-04-27 15:17:35 +0100351 res->smc_res.x[0] = RSI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000352
353out_unmap_rd:
354 buffer_unmap(rd);
355 granule_unlock(g_rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000356}
357
AlexeiFedorov97844202023-04-27 15:17:35 +0100358void handle_rsi_measurement_read(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000359{
360 struct rd *rd;
361 unsigned long idx;
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100362 unsigned int i, cnt;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000363
364 assert(rec != NULL);
365
AlexeiFedorov97844202023-04-27 15:17:35 +0100366 res->action = UPDATE_REC_RETURN_TO_REALM;
367
Soby Mathewb4c6df42022-11-09 11:13:29 +0000368 /* X1: Index */
369 idx = rec->regs[1];
370
371 if (idx >= MEASUREMENT_SLOT_NR) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100372 res->smc_res.x[0] = RSI_ERROR_INPUT;
373 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000374 }
375
376 /*
377 * rd lock is acquired so that measurement cannot be updated
378 * simultaneously by another rec
379 */
380 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
381 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
382
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100383 /* Number of 8-bytes words in measurement */
384 cnt = (unsigned int)measurement_get_size(rd->algorithm) /
385 sizeof(unsigned long);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000386
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100387 /* Copy the part of the measurement to res->smc_res.x[] */
388 for (i = 0U; i < SMC_RESULT_REGS - 1U; i++) {
389 res->smc_res.x[i + 1U] = rd->measurement[idx][i];
390 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000391
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100392 /* Copy the rest of the measurement to the rec->regs[] */
393 for (; i < cnt; i++) {
394 rec->regs[i + 1U] = rd->measurement[idx][i];
395 }
396
397 /* Zero-initialize unused area */
398 for (; i < MAX_MEASUREMENT_WORDS; i++) {
399 rec->regs[i + 1U] = 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000400 }
401
402 buffer_unmap(rd);
403 granule_unlock(rec->realm_info.g_rd);
404
AlexeiFedorov97844202023-04-27 15:17:35 +0100405 res->smc_res.x[0] = RSI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000406}