blob: f28bdf2eb60bb526600645d0f102f0f56d67c49a [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>
11#include <realm_attest.h>
12#include <smc-rsi.h>
13#include <smc.h>
14#include <string.h>
15#include <utils_def.h>
16
17#define MAX_EXTENDED_SIZE (64U)
18
19/*
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{
39 rec->token_sign_ctx.token_ipa = rec->regs[1];
40 (void)memcpy(rec->token_sign_ctx.challenge, &rec->regs[2],
41 ATTEST_CHALLENGE_SIZE);
42}
43
44/*
45 * Verify that in all the iterations the input parameters are the same
46 * as in the initial call.
47 */
48static bool verify_input_parameters_consistency(struct rec *rec)
49{
50 return rec->token_sign_ctx.token_ipa == rec->regs[1];
51}
52
53/*
54 * Function to continue with the sign operation.
55 * It returns void as the result will be updated in the
56 * struct attest_result passed as argument.
57 */
58static void attest_token_continue_sign_state(struct rec *rec,
59 struct attest_result *res)
60{
61 /*
62 * Sign and finish creating the token.
63 */
64 enum attest_token_err_t ret =
65 attest_realm_token_sign(&(rec->token_sign_ctx.ctx),
Mate Toth-Pal071aa562023-07-04 09:09:26 +020066 &(rec->rmm_realm_token_len));
Soby Mathewb4c6df42022-11-09 11:13:29 +000067
68 if ((ret == ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS) ||
69 (ret == ATTEST_TOKEN_ERR_SUCCESS)) {
70 /*
71 * Return to RSI handler function after each iteration
72 * to check is there anything else to do (pending IRQ)
73 * or next signing iteration can be executed.
74 */
75 res->incomplete = true;
76 res->smc_res.x[0] = RSI_INCOMPLETE;
77
78 /* If this was the last signing cycle */
79 if (ret == ATTEST_TOKEN_ERR_SUCCESS) {
80 rec->token_sign_ctx.state =
81 ATTEST_SIGN_TOKEN_WRITE_IN_PROGRESS;
82 }
83 } else {
84 /* Accessible only in case of failure during token signing */
85 ERROR("FATAL_ERROR: Realm token creation failed\n");
86 panic();
87 }
88}
89
90/*
91 * Function to continue with the token write operation.
92 * It returns void as the result will be updated in the
93 * struct attest_result passed as argument.
94 */
95static void attest_token_continue_write_state(struct rec *rec,
96 struct attest_result *res)
97{
98 struct rd *rd = NULL;
99 struct granule *gr;
100 uint8_t *realm_att_token;
101 unsigned long realm_att_token_ipa = rec->regs[1];
102 enum s2_walk_status walk_status;
103 struct s2_walk_result walk_res = { 0UL };
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200104 size_t attest_token_len;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000105
106 /*
107 * The refcount on rd and rec will protect from any changes
108 * while REC is running.
109 */
110 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
111
112 /*
113 * Translate realm granule IPA to PA. If returns with
114 * WALK_SUCCESS then the last level page table (llt),
115 * which holds the realm_att_token_buf mapping, is locked.
116 */
117 walk_status = realm_ipa_to_pa(rd, realm_att_token_ipa, &walk_res);
118 buffer_unmap(rd);
119
120 /* Walk parameter validity was checked by RSI_ATTESTATION_TOKEN_INIT */
121 assert(walk_status != WALK_INVALID_PARAMS);
122
123 if (walk_status == WALK_FAIL) {
Yousuf A62808152022-10-31 10:35:42 +0000124 if (s2_walk_result_match_ripas(&walk_res, RIPAS_EMPTY)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000125 res->smc_res.x[0] = RSI_ERROR_INPUT;
126 } else {
127 /*
128 * Translation failed, IPA is not mapped. Return to NS host to
129 * fix the issue.
130 */
131 res->walk_result.abort = true;
132 res->walk_result.rtt_level = walk_res.rtt_level;
133 res->smc_res.x[0] = RSI_INCOMPLETE;
134 }
135 return;
136 }
137
138 /* Map realm data granule to RMM address space */
139 gr = find_granule(walk_res.pa);
140 realm_att_token = granule_map(gr, SLOT_RSI_CALL);
141
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200142 attest_token_len = attest_cca_token_create(realm_att_token,
143 ATTEST_TOKEN_BUFFER_SIZE,
144 (void *)rec->rmm_realm_token_buf,
145 rec->rmm_realm_token_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000146
147 /* Unmap realm granule */
148 buffer_unmap(realm_att_token);
149
150 /* Unlock last level page table (walk_res.g_llt) */
151 granule_unlock(walk_res.llt);
152
153 /* Write output parameters */
154 if (attest_token_len == 0) {
155 res->smc_res.x[0] = RSI_ERROR_INPUT;
156 } else {
157 res->smc_res.x[0] = RSI_SUCCESS;
158 res->smc_res.x[1] = attest_token_len;
159 }
160
161 /* The signing has either succeeded or failed. Reset the state. */
162 rec->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED;
163}
164
165unsigned long handle_rsi_attest_token_init(struct rec *rec)
166{
167 struct rd *rd = NULL;
168 unsigned long ret;
169 unsigned long realm_buf_ipa = rec->regs[1];
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200170 void *rpv_ptr;
171 size_t rpv_len;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000172 int att_ret;
173
174 assert(rec != NULL);
175
176 /*
177 * Calling RSI_ATTESTATION_TOKEN_INIT any time aborts any ongoing
178 * operation.
179 * TODO: This can be moved to attestation lib
180 */
181 if (rec->token_sign_ctx.state != ATTEST_SIGN_NOT_STARTED) {
182 int restart;
183
184 rec->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED;
185 restart = attestation_heap_reinit_pe(rec->aux_data.attest_heap_buf,
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000186 REC_HEAP_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000187 if (restart != 0) {
188 /* There is no provision for this failure so panic */
189 panic();
190 }
191 }
192
193 if (!GRANULE_ALIGNED(realm_buf_ipa)) {
194 return RSI_ERROR_INPUT;
195 }
196
197 /*
198 * rd lock is acquired so that measurement cannot be updated
199 * simultaneously by another rec
200 */
201 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
202 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
203 if (!addr_in_par(rd, realm_buf_ipa)) {
204 ret = RSI_ERROR_INPUT;
205 goto out_unmap_rd;
206 }
207
208 /*
209 * Save the input parameters in the context for later iterations
210 * to check.
211 */
212 save_input_parameters(rec);
213
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200214 get_rpv(rd, &rpv_ptr, &rpv_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000215 att_ret = attest_realm_token_create(rd->algorithm, rd->measurement,
216 MEASUREMENT_SLOT_NR,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200217 rpv_ptr,
218 rpv_len,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000219 &rec->token_sign_ctx,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200220 rec->rmm_realm_token_buf,
221 sizeof(rec->rmm_realm_token_buf));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000222 if (att_ret != 0) {
223 ERROR("FATAL_ERROR: Realm token creation failed,\n");
224 panic();
225 }
226
227 rec->token_sign_ctx.state = ATTEST_SIGN_IN_PROGRESS;
228 ret = RSI_SUCCESS;
229
230out_unmap_rd:
231 buffer_unmap(rd);
232 granule_unlock(rec->realm_info.g_rd);
233 return ret;
234}
235
Soby Mathewb4c6df42022-11-09 11:13:29 +0000236void handle_rsi_attest_token_continue(struct rec *rec,
237 struct attest_result *res)
238{
239 assert(rec != NULL);
240 assert(res != NULL);
241
242 /* Initialize attest_result */
243 res->incomplete = false;
244 res->walk_result.abort = false;
245
246 if (!verify_input_parameters_consistency(rec)) {
247 res->smc_res.x[0] = RSI_ERROR_INPUT;
248 return;
249 }
250
251 switch (rec->token_sign_ctx.state) {
252 case ATTEST_SIGN_NOT_STARTED:
253 /*
254 * Before this call the initial attestation token call
255 * (SMC_RSI_ATTEST_TOKEN_INIT) must have been executed
256 * successfully.
257 */
258 res->smc_res.x[0] = RSI_ERROR_STATE;
259 break;
260 case ATTEST_SIGN_IN_PROGRESS:
261 attest_token_continue_sign_state(rec, res);
262 break;
263 case ATTEST_SIGN_TOKEN_WRITE_IN_PROGRESS:
264 attest_token_continue_write_state(rec, res);
265 break;
266 default:
267 /* Any other state is considered an error. */
268 assert(false);
269 }
270}
271
272unsigned long handle_rsi_extend_measurement(struct rec *rec)
273{
274 struct granule *g_rd;
275 struct rd *rd;
276 unsigned long index;
277 unsigned long rd_addr;
278 size_t size;
279 unsigned long ret;
280 void *extend_measurement;
281 unsigned char *current_measurement;
282 int __unused meas_ret;
283
284 /*
285 * rd lock is acquired so that measurement cannot be updated
286 * simultaneously by another rec
287 */
288 rd_addr = granule_addr(rec->realm_info.g_rd);
289 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
290
291 assert(g_rd != NULL);
292
293 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
294
295 /*
296 * X1: index
297 * X2: size
298 * X3-X10: measurement value
299 */
300 index = rec->regs[1];
301
302 if ((index == RIM_MEASUREMENT_SLOT) ||
303 (index >= MEASUREMENT_SLOT_NR)) {
304 ret = RSI_ERROR_INPUT;
305 goto out_unmap_rd;
306 }
307
308 size = rec->regs[2];
309
310 if (size > MAX_EXTENDED_SIZE) {
311 ret = RSI_ERROR_INPUT;
312 goto out_unmap_rd;
313 }
314
315 extend_measurement = &rec->regs[3];
316 current_measurement = rd->measurement[index];
317
318 measurement_extend(rd->algorithm,
319 current_measurement,
320 extend_measurement,
321 size,
322 current_measurement);
323
324 ret = RSI_SUCCESS;
325
326out_unmap_rd:
327 buffer_unmap(rd);
328 granule_unlock(g_rd);
329 return ret;
330}
331
332unsigned long handle_rsi_read_measurement(struct rec *rec)
333{
334 struct rd *rd;
335 unsigned long idx;
336 size_t measurement_size;
337
338 assert(rec != NULL);
339
340 /* X1: Index */
341 idx = rec->regs[1];
342
343 if (idx >= MEASUREMENT_SLOT_NR) {
344 return RSI_ERROR_INPUT;
345 }
346
347 /*
348 * rd lock is acquired so that measurement cannot be updated
349 * simultaneously by another rec
350 */
351 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
352 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
353
354 measurement_size = measurement_get_size(rd->algorithm);
355
356 (void)memcpy(&rec->regs[1], rd->measurement[idx], measurement_size);
357
358 /* Zero-initialize the unused area */
359 if (measurement_size < MAX_MEASUREMENT_SIZE) {
360 (void)memset((char *)(&rec->regs[1]) + measurement_size,
361 0, MAX_MEASUREMENT_SIZE - measurement_size);
362 }
363
364 buffer_unmap(rd);
365 granule_unlock(rec->realm_info.g_rd);
366
367 return RSI_SUCCESS;
368}