blob: c0c228857365e4779398ea8b67cc4f3de873bdbd [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>
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00007#include <buffer.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00008#include <debug.h>
9#include <granule.h>
10#include <measurement.h>
11#include <realm.h>
AlexeiFedorov5b186ad2023-04-26 14:43:18 +010012#include <rsi-handler.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000013#include <smc-rsi.h>
14#include <smc.h>
15#include <string.h>
16#include <utils_def.h>
17
AlexeiFedorovefe2aec2023-06-08 16:17:00 +010018#define MAX_EXTENDED_SIZE (64U)
AlexeiFedorov7b3c3042023-06-28 15:41:11 +010019#define MAX_MEASUREMENT_WORDS (MAX_MEASUREMENT_SIZE / sizeof(unsigned long))
Soby Mathewb4c6df42022-11-09 11:13:29 +000020/*
21 * Return the Realm Personalization Value.
22 *
23 * Arguments:
24 * rd - The Realm descriptor.
Mate Toth-Pal071aa562023-07-04 09:09:26 +020025 * claim_ptr - The start address of the Realm Personalization Value claim
26 * claim_len - The length of the Realm Personalization Value claim
Soby Mathewb4c6df42022-11-09 11:13:29 +000027 */
Mate Toth-Pal071aa562023-07-04 09:09:26 +020028static void get_rpv(struct rd *rd, void **claim_ptr, size_t *claim_len)
Soby Mathewb4c6df42022-11-09 11:13:29 +000029{
Mate Toth-Pal071aa562023-07-04 09:09:26 +020030 *claim_ptr = (uint8_t *)&(rd->rpv[0]);
31 *claim_len = RPV_SIZE;
Soby Mathewb4c6df42022-11-09 11:13:29 +000032}
33
34/*
AlexeiFedorov97844202023-04-27 15:17:35 +010035 * Function to continue with the token write operation
Soby Mathewb4c6df42022-11-09 11:13:29 +000036 */
37static void attest_token_continue_write_state(struct rec *rec,
AlexeiFedorov97844202023-04-27 15:17:35 +010038 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +000039{
Soby Mathewb4c6df42022-11-09 11:13:29 +000040 struct granule *gr;
AlexeiFedorovea68b552023-10-03 11:11:47 +010041 uintptr_t realm_att_token;
Soby Mathewb4c6df42022-11-09 11:13:29 +000042 unsigned long realm_att_token_ipa = rec->regs[1];
AlexeiFedorovea68b552023-10-03 11:11:47 +010043 unsigned long offset = rec->regs[2];
44 unsigned long size = rec->regs[3];
Soby Mathewb4c6df42022-11-09 11:13:29 +000045 enum s2_walk_status walk_status;
46 struct s2_walk_result walk_res = { 0UL };
AlexeiFedorovea68b552023-10-03 11:11:47 +010047 size_t attest_token_len, length;
AlexeiFedorovec35c542023-04-27 17:52:02 +010048 struct rec_attest_data *attest_data = rec->aux_data.attest_data;
AlexeiFedorovea68b552023-10-03 11:11:47 +010049 uintptr_t cca_token_buf = rec->aux_data.cca_token_buf;
Soby Mathewb4c6df42022-11-09 11:13:29 +000050
51 /*
Soby Mathewb4c6df42022-11-09 11:13:29 +000052 * Translate realm granule IPA to PA. If returns with
53 * WALK_SUCCESS then the last level page table (llt),
54 * which holds the realm_att_token_buf mapping, is locked.
55 */
AlexeiFedorovd2e1bbd2023-04-18 15:18:39 +010056 walk_status = realm_ipa_to_pa(rec, realm_att_token_ipa, &walk_res);
Soby Mathewb4c6df42022-11-09 11:13:29 +000057
58 /* Walk parameter validity was checked by RSI_ATTESTATION_TOKEN_INIT */
59 assert(walk_status != WALK_INVALID_PARAMS);
60
61 if (walk_status == WALK_FAIL) {
AlexeiFedorovd2e1bbd2023-04-18 15:18:39 +010062 if (walk_res.ripas_val == RIPAS_EMPTY) {
Soby Mathewb4c6df42022-11-09 11:13:29 +000063 res->smc_res.x[0] = RSI_ERROR_INPUT;
64 } else {
65 /*
AlexeiFedorov97844202023-04-27 15:17:35 +010066 * Translation failed, IPA is not mapped.
67 * Return to NS host to fix the issue.
Soby Mathewb4c6df42022-11-09 11:13:29 +000068 */
AlexeiFedorov97844202023-04-27 15:17:35 +010069 res->action = STAGE_2_TRANSLATION_FAULT;
70 res->rtt_level = walk_res.rtt_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +000071 }
72 return;
73 }
74
Soby Mathew19eb4332023-11-20 14:03:23 +000075 /* If size of buffer is 0, then return early. */
AlexeiFedorovea68b552023-10-03 11:11:47 +010076 if (size == 0UL) {
Soby Mathew19eb4332023-11-20 14:03:23 +000077 res->smc_res.x[0] = RSI_INCOMPLETE;
78 goto out_unlock;
AlexeiFedorovea68b552023-10-03 11:11:47 +010079 }
80
Soby Mathewb4c6df42022-11-09 11:13:29 +000081 /* Map realm data granule to RMM address space */
82 gr = find_granule(walk_res.pa);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +000083 realm_att_token = (uintptr_t)buffer_granule_map(gr, SLOT_RSI_CALL);
AlexeiFedorovea68b552023-10-03 11:11:47 +010084 assert(realm_att_token != 0UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +000085
AlexeiFedorovea68b552023-10-03 11:11:47 +010086 if (attest_data->token_sign_ctx.copied_len == 0UL) {
87 attest_token_len = attest_cca_token_create(
Soby Mathewf3622132024-07-19 07:31:40 +010088 &attest_data->token_sign_ctx,
AlexeiFedorovea68b552023-10-03 11:11:47 +010089 (void *)cca_token_buf,
90 REC_ATTEST_TOKEN_BUF_SIZE,
91 &attest_data->rmm_realm_token_buf,
92 attest_data->rmm_realm_token_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +000093
AlexeiFedorovea68b552023-10-03 11:11:47 +010094 if (attest_token_len == 0UL) {
95 res->smc_res.x[0] = RSI_ERROR_INPUT;
AlexeiFedorovea68b552023-10-03 11:11:47 +010096 goto out_unmap;
97 }
98
99 attest_data->token_sign_ctx.cca_token_len = attest_token_len;
100 } else {
101 attest_token_len = attest_data->token_sign_ctx.cca_token_len;
102 }
103
104 length = (size < attest_token_len) ? size : attest_token_len;
105
106 /* Copy attestation token */
107 (void)memcpy((void *)(realm_att_token + offset),
108 (void *)(cca_token_buf +
109 attest_data->token_sign_ctx.copied_len),
110 length);
111
112 attest_token_len -= length;
113
114 if (attest_token_len != 0UL) {
115 attest_data->token_sign_ctx.cca_token_len = attest_token_len;
116 attest_data->token_sign_ctx.copied_len += length;
117
118 res->smc_res.x[0] = RSI_INCOMPLETE;
119 } else {
AlexeiFedorovea68b552023-10-03 11:11:47 +0100120 res->smc_res.x[0] = RSI_SUCCESS;
121 }
122
123 res->smc_res.x[1] = length;
124
125out_unmap:
Soby Mathewb4c6df42022-11-09 11:13:29 +0000126 /* Unmap realm granule */
AlexeiFedorovea68b552023-10-03 11:11:47 +0100127 buffer_unmap((void *)realm_att_token);
Soby Mathew19eb4332023-11-20 14:03:23 +0000128out_unlock:
Soby Mathewb4c6df42022-11-09 11:13:29 +0000129 /* Unlock last level page table (walk_res.g_llt) */
130 granule_unlock(walk_res.llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000131}
132
AlexeiFedorov97844202023-04-27 15:17:35 +0100133void handle_rsi_attest_token_init(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000134{
AlexeiFedorovea68b552023-10-03 11:11:47 +0100135 struct rd *rd;
AlexeiFedorovec35c542023-04-27 17:52:02 +0100136 struct rec_attest_data *attest_data;
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200137 void *rpv_ptr;
138 size_t rpv_len;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000139 int att_ret;
140
141 assert(rec != NULL);
142
AlexeiFedorovec35c542023-04-27 17:52:02 +0100143 attest_data = rec->aux_data.attest_data;
AlexeiFedorov97844202023-04-27 15:17:35 +0100144 res->action = UPDATE_REC_RETURN_TO_REALM;
145
Soby Mathewb4c6df42022-11-09 11:13:29 +0000146 /*
147 * Calling RSI_ATTESTATION_TOKEN_INIT any time aborts any ongoing
148 * operation.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000149 */
Soby Mathewf3622132024-07-19 07:31:40 +0100150 att_ret = attest_token_ctx_init(&attest_data->token_sign_ctx,
151 rec->aux_data.attest_heap_buf,
152 REC_HEAP_SIZE);
153 if (att_ret != 0) {
154 /* There is no provision for this failure so panic */
155 panic();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000156 }
157
Soby Mathewf3622132024-07-19 07:31:40 +0100158 /* Initialize the final token len */
159 attest_data->rmm_realm_token_len = 0;
AlexeiFedorov2dcd79f2023-10-17 10:04:11 +0100160
Soby Mathewb4c6df42022-11-09 11:13:29 +0000161 /*
162 * rd lock is acquired so that measurement cannot be updated
163 * simultaneously by another rec
164 */
165 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000166 rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100167 assert(rd != NULL);
168
AlexeiFedorovea68b552023-10-03 11:11:47 +0100169 /* Save challenge value in the context */
170 (void)memcpy((void *)attest_data->token_sign_ctx.challenge,
171 (const void *)&rec->regs[1],
172 ATTEST_CHALLENGE_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000173
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200174 get_rpv(rd, &rpv_ptr, &rpv_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000175 att_ret = attest_realm_token_create(rd->algorithm, rd->measurement,
176 MEASUREMENT_SLOT_NR,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200177 rpv_ptr,
178 rpv_len,
AlexeiFedorovec35c542023-04-27 17:52:02 +0100179 &attest_data->token_sign_ctx,
180 attest_data->rmm_realm_token_buf,
181 sizeof(attest_data->rmm_realm_token_buf));
AlexeiFedorovea68b552023-10-03 11:11:47 +0100182 buffer_unmap(rd);
183 granule_unlock(rec->realm_info.g_rd);
184
Soby Mathewb4c6df42022-11-09 11:13:29 +0000185 if (att_ret != 0) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100186 ERROR("FATAL_ERROR: Realm token creation failed\n");
Soby Mathewb4c6df42022-11-09 11:13:29 +0000187 panic();
188 }
189
AlexeiFedorovea68b552023-10-03 11:11:47 +0100190 res->smc_res.x[0] = RSI_SUCCESS;
AlexeiFedorov755a70b2023-10-12 12:31:45 +0100191 res->smc_res.x[1] = REC_ATTEST_TOKEN_BUF_SIZE;
AlexeiFedorov97844202023-04-27 15:17:35 +0100192}
193
194/*
195 * Return 'false' if no IRQ is pending,
196 * return 'true' if there is an IRQ pending, and need to return to Host.
197 */
198static bool check_pending_irq(void)
199{
200 return (read_isr_el1() != 0UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000201}
202
Soby Mathewb4c6df42022-11-09 11:13:29 +0000203void handle_rsi_attest_token_continue(struct rec *rec,
AlexeiFedorov97844202023-04-27 15:17:35 +0100204 struct rmi_rec_exit *rec_exit,
205 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000206{
AlexeiFedorovec35c542023-04-27 17:52:02 +0100207 struct rec_attest_data *attest_data;
AlexeiFedorovea68b552023-10-03 11:11:47 +0100208 unsigned long realm_buf_ipa, offset, size;
AlexeiFedorovec35c542023-04-27 17:52:02 +0100209
Soby Mathewb4c6df42022-11-09 11:13:29 +0000210 assert(rec != NULL);
AlexeiFedorov97844202023-04-27 15:17:35 +0100211 assert(rec_exit != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000212
AlexeiFedorovec35c542023-04-27 17:52:02 +0100213 attest_data = rec->aux_data.attest_data;
AlexeiFedorov97844202023-04-27 15:17:35 +0100214 res->action = UPDATE_REC_RETURN_TO_REALM;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000215
AlexeiFedorovea68b552023-10-03 11:11:47 +0100216 realm_buf_ipa = rec->regs[1];
217 offset = rec->regs[2];
218 size = rec->regs[3];
219
220 if (!GRANULE_ALIGNED(realm_buf_ipa) ||
221 (offset >= GRANULE_SIZE) ||
222 ((offset + size) > GRANULE_SIZE) ||
223 ((offset + size) < offset)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000224 res->smc_res.x[0] = RSI_ERROR_INPUT;
225 return;
226 }
227
AlexeiFedorovea68b552023-10-03 11:11:47 +0100228 if (!addr_in_rec_par(rec, realm_buf_ipa)) {
229 res->smc_res.x[0] = RSI_ERROR_INPUT;
230 return;
231 }
AlexeiFedorov97844202023-04-27 15:17:35 +0100232
Soby Mathewf3622132024-07-19 07:31:40 +0100233 /* Sign the token */
234 while (attest_data->rmm_realm_token_len == 0U) {
235 enum attest_token_err_t ret;
AlexeiFedorovea68b552023-10-03 11:11:47 +0100236
Soby Mathewf3622132024-07-19 07:31:40 +0100237 ret = attest_realm_token_sign(&(attest_data->token_sign_ctx),
238 &(attest_data->rmm_realm_token_len));
239
240 if (ret == ATTEST_TOKEN_ERR_INVALID_STATE) {
241 /*
242 * Before this call the initial attestation token call
243 * (SMC_RSI_ATTEST_TOKEN_INIT) must have been executed
244 * successfully.
245 */
246 res->smc_res.x[0] = RSI_ERROR_STATE;
247 return;
248 } else if ((ret != ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS) &&
249 (ret != ATTEST_TOKEN_ERR_SUCCESS)) {
250 /* Accessible only in case of failure during token signing */
251 ERROR("FATAL_ERROR: Realm token sign failed\n");
252 panic();
253 }
254
255 res->smc_res.x[0] = RSI_INCOMPLETE;
256
257 /*
258 * Return to RSI handler function after each iteration
259 * to check is there anything else to do (pending IRQ)
260 * or next signing iteration can be executed.
261 */
AlexeiFedorovea68b552023-10-03 11:11:47 +0100262 if (check_pending_irq()) {
263 res->action = UPDATE_REC_EXIT_TO_HOST;
264 rec_exit->exit_reason = RMI_EXIT_IRQ;
265 return;
AlexeiFedorov97844202023-04-27 15:17:35 +0100266 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000267 }
AlexeiFedorovea68b552023-10-03 11:11:47 +0100268
AlexeiFedorovea68b552023-10-03 11:11:47 +0100269 attest_token_continue_write_state(rec, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000270}
271
AlexeiFedorov97844202023-04-27 15:17:35 +0100272void handle_rsi_measurement_extend(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000273{
274 struct granule *g_rd;
275 struct rd *rd;
276 unsigned long index;
277 unsigned long rd_addr;
278 size_t size;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000279 void *extend_measurement;
280 unsigned char *current_measurement;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000281
AlexeiFedorov97844202023-04-27 15:17:35 +0100282 assert(rec != NULL);
283
284 res->action = UPDATE_REC_RETURN_TO_REALM;
285
Soby Mathewb4c6df42022-11-09 11:13:29 +0000286 /*
287 * rd lock is acquired so that measurement cannot be updated
288 * simultaneously by another rec
289 */
290 rd_addr = granule_addr(rec->realm_info.g_rd);
291 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
292
293 assert(g_rd != NULL);
294
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000295 rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100296 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000297
298 /*
299 * X1: index
300 * X2: size
301 * X3-X10: measurement value
302 */
303 index = rec->regs[1];
304
305 if ((index == RIM_MEASUREMENT_SLOT) ||
306 (index >= MEASUREMENT_SLOT_NR)) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100307 res->smc_res.x[0] = RSI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000308 goto out_unmap_rd;
309 }
310
311 size = rec->regs[2];
312
313 if (size > MAX_EXTENDED_SIZE) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100314 res->smc_res.x[0] = RSI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000315 goto out_unmap_rd;
316 }
317
318 extend_measurement = &rec->regs[3];
319 current_measurement = rd->measurement[index];
320
321 measurement_extend(rd->algorithm,
322 current_measurement,
323 extend_measurement,
324 size,
325 current_measurement);
326
AlexeiFedorov97844202023-04-27 15:17:35 +0100327 res->smc_res.x[0] = RSI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000328
329out_unmap_rd:
330 buffer_unmap(rd);
331 granule_unlock(g_rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000332}
333
AlexeiFedorov97844202023-04-27 15:17:35 +0100334void handle_rsi_measurement_read(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000335{
336 struct rd *rd;
337 unsigned long idx;
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100338 unsigned int i, cnt;
Mate Toth-Pal59b52d02023-08-18 14:14:19 +0200339 unsigned long *measurement_value_part;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000340
341 assert(rec != NULL);
342
AlexeiFedorov97844202023-04-27 15:17:35 +0100343 res->action = UPDATE_REC_RETURN_TO_REALM;
344
Soby Mathewb4c6df42022-11-09 11:13:29 +0000345 /* X1: Index */
346 idx = rec->regs[1];
347
348 if (idx >= MEASUREMENT_SLOT_NR) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100349 res->smc_res.x[0] = RSI_ERROR_INPUT;
350 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000351 }
352
353 /*
354 * rd lock is acquired so that measurement cannot be updated
355 * simultaneously by another rec
356 */
357 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000358 rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100359 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000360
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100361 /* Number of 8-bytes words in measurement */
AlexeiFedorov4faab852023-08-30 15:06:49 +0100362 cnt = (unsigned int)(measurement_get_size(rd->algorithm) /
363 sizeof(unsigned long));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000364
AlexeiFedorovea68b552023-10-03 11:11:47 +0100365 assert(cnt >= (SMC_RESULT_REGS - 1U));
Mate Toth-Pal59b52d02023-08-18 14:14:19 +0200366 assert(cnt < ARRAY_LEN(rec->regs));
367
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100368 /* Copy the part of the measurement to res->smc_res.x[] */
AlexeiFedorovea68b552023-10-03 11:11:47 +0100369 for (i = 0U; i < (SMC_RESULT_REGS - 1U); i++) {
Mate Toth-Pal59b52d02023-08-18 14:14:19 +0200370 measurement_value_part = (unsigned long *)
371 &(rd->measurement[idx][i * sizeof(unsigned long)]);
372 res->smc_res.x[i + 1U] = *measurement_value_part;
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100373 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000374
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100375 /* Copy the rest of the measurement to the rec->regs[] */
376 for (; i < cnt; i++) {
Mate Toth-Pal59b52d02023-08-18 14:14:19 +0200377 measurement_value_part = (unsigned long *)
378 &(rd->measurement[idx][i * sizeof(unsigned long)]);
379 rec->regs[i + 1U] = *measurement_value_part;
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100380 }
381
382 /* Zero-initialize unused area */
383 for (; i < MAX_MEASUREMENT_WORDS; i++) {
384 rec->regs[i + 1U] = 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000385 }
386
387 buffer_unmap(rd);
388 granule_unlock(rec->realm_info.g_rd);
389
AlexeiFedorov97844202023-04-27 15:17:35 +0100390 res->smc_res.x[0] = RSI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000391}