blob: 64ab1f71b32043e6976247548a67f2a6c3a8cd00 [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>
7#include <attestation_token.h>
8#include <debug.h>
9#include <granule.h>
10#include <measurement.h>
11#include <realm.h>
12#include <realm_attest.h>
13#include <smc-rsi.h>
14#include <smc.h>
15#include <string.h>
16#include <utils_def.h>
17
18#define MAX_EXTENDED_SIZE (64U)
19
20/*
21 * Return the Realm Personalization Value.
22 *
23 * Arguments:
24 * rd - The Realm descriptor.
25 * claim - The structure to return the Realm Personalization Value claim
26 */
27static void get_rpv(struct rd *rd, struct q_useful_buf_c *claim)
28{
29 claim->ptr = (uint8_t *)&(rd->rpv[0]);
30 claim->len = RPV_SIZE;
31}
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),
66 &(rec->rmm_realm_token));
67
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 };
104 struct q_useful_buf attest_token_buf;
105 size_t attest_token_len;
106
107 /*
108 * The refcount on rd and rec will protect from any changes
109 * while REC is running.
110 */
111 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
112
113 /*
114 * Translate realm granule IPA to PA. If returns with
115 * WALK_SUCCESS then the last level page table (llt),
116 * which holds the realm_att_token_buf mapping, is locked.
117 */
118 walk_status = realm_ipa_to_pa(rd, realm_att_token_ipa, &walk_res);
119 buffer_unmap(rd);
120
121 /* Walk parameter validity was checked by RSI_ATTESTATION_TOKEN_INIT */
122 assert(walk_status != WALK_INVALID_PARAMS);
123
124 if (walk_status == WALK_FAIL) {
Yousuf A62808152022-10-31 10:35:42 +0000125 if (s2_walk_result_match_ripas(&walk_res, RIPAS_EMPTY)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000126 res->smc_res.x[0] = RSI_ERROR_INPUT;
127 } else {
128 /*
129 * Translation failed, IPA is not mapped. Return to NS host to
130 * fix the issue.
131 */
132 res->walk_result.abort = true;
133 res->walk_result.rtt_level = walk_res.rtt_level;
134 res->smc_res.x[0] = RSI_INCOMPLETE;
135 }
136 return;
137 }
138
139 /* Map realm data granule to RMM address space */
140 gr = find_granule(walk_res.pa);
141 realm_att_token = granule_map(gr, SLOT_RSI_CALL);
142
143 attest_token_buf.ptr = realm_att_token;
144 attest_token_buf.len = ATTEST_TOKEN_BUFFER_SIZE;
145
146 attest_token_len = attest_cca_token_create(&attest_token_buf,
147 &rec->rmm_realm_token);
148
149 /* Unmap realm granule */
150 buffer_unmap(realm_att_token);
151
152 /* Unlock last level page table (walk_res.g_llt) */
153 granule_unlock(walk_res.llt);
154
155 /* Write output parameters */
156 if (attest_token_len == 0) {
157 res->smc_res.x[0] = RSI_ERROR_INPUT;
158 } else {
159 res->smc_res.x[0] = RSI_SUCCESS;
160 res->smc_res.x[1] = attest_token_len;
161 }
162
163 /* The signing has either succeeded or failed. Reset the state. */
164 rec->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED;
165}
166
167unsigned long handle_rsi_attest_token_init(struct rec *rec)
168{
169 struct rd *rd = NULL;
170 unsigned long ret;
171 unsigned long realm_buf_ipa = rec->regs[1];
172 struct q_useful_buf rmm_realm_token_buf = {
173 rec->rmm_realm_token_buf, sizeof(rec->rmm_realm_token_buf)};
174 struct q_useful_buf_c rpv;
175 int att_ret;
176
177 assert(rec != NULL);
178
179 /*
180 * Calling RSI_ATTESTATION_TOKEN_INIT any time aborts any ongoing
181 * operation.
182 * TODO: This can be moved to attestation lib
183 */
184 if (rec->token_sign_ctx.state != ATTEST_SIGN_NOT_STARTED) {
185 int restart;
186
187 rec->token_sign_ctx.state = ATTEST_SIGN_NOT_STARTED;
188 restart = attestation_heap_reinit_pe(rec->aux_data.attest_heap_buf,
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000189 REC_HEAP_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000190 if (restart != 0) {
191 /* There is no provision for this failure so panic */
192 panic();
193 }
194 }
195
196 if (!GRANULE_ALIGNED(realm_buf_ipa)) {
197 return RSI_ERROR_INPUT;
198 }
199
200 /*
201 * rd lock is acquired so that measurement cannot be updated
202 * simultaneously by another rec
203 */
204 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
205 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
206 if (!addr_in_par(rd, realm_buf_ipa)) {
207 ret = RSI_ERROR_INPUT;
208 goto out_unmap_rd;
209 }
210
211 /*
212 * Save the input parameters in the context for later iterations
213 * to check.
214 */
215 save_input_parameters(rec);
216
217 get_rpv(rd, &rpv);
218 att_ret = attest_realm_token_create(rd->algorithm, rd->measurement,
219 MEASUREMENT_SLOT_NR,
220 &rpv,
221 &rec->token_sign_ctx,
222 &rmm_realm_token_buf);
223 if (att_ret != 0) {
224 ERROR("FATAL_ERROR: Realm token creation failed,\n");
225 panic();
226 }
227
228 rec->token_sign_ctx.state = ATTEST_SIGN_IN_PROGRESS;
229 ret = RSI_SUCCESS;
230
231out_unmap_rd:
232 buffer_unmap(rd);
233 granule_unlock(rec->realm_info.g_rd);
234 return ret;
235}
236
Soby Mathewb4c6df42022-11-09 11:13:29 +0000237void handle_rsi_attest_token_continue(struct rec *rec,
238 struct attest_result *res)
239{
240 assert(rec != NULL);
241 assert(res != NULL);
242
243 /* Initialize attest_result */
244 res->incomplete = false;
245 res->walk_result.abort = false;
246
247 if (!verify_input_parameters_consistency(rec)) {
248 res->smc_res.x[0] = RSI_ERROR_INPUT;
249 return;
250 }
251
252 switch (rec->token_sign_ctx.state) {
253 case ATTEST_SIGN_NOT_STARTED:
254 /*
255 * Before this call the initial attestation token call
256 * (SMC_RSI_ATTEST_TOKEN_INIT) must have been executed
257 * successfully.
258 */
259 res->smc_res.x[0] = RSI_ERROR_STATE;
260 break;
261 case ATTEST_SIGN_IN_PROGRESS:
262 attest_token_continue_sign_state(rec, res);
263 break;
264 case ATTEST_SIGN_TOKEN_WRITE_IN_PROGRESS:
265 attest_token_continue_write_state(rec, res);
266 break;
267 default:
268 /* Any other state is considered an error. */
269 assert(false);
270 }
271}
272
273unsigned long handle_rsi_extend_measurement(struct rec *rec)
274{
275 struct granule *g_rd;
276 struct rd *rd;
277 unsigned long index;
278 unsigned long rd_addr;
279 size_t size;
280 unsigned long ret;
281 void *extend_measurement;
282 unsigned char *current_measurement;
283 int __unused meas_ret;
284
285 /*
286 * rd lock is acquired so that measurement cannot be updated
287 * simultaneously by another rec
288 */
289 rd_addr = granule_addr(rec->realm_info.g_rd);
290 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
291
292 assert(g_rd != NULL);
293
294 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
295
296 /*
297 * X1: index
298 * X2: size
299 * X3-X10: measurement value
300 */
301 index = rec->regs[1];
302
303 if ((index == RIM_MEASUREMENT_SLOT) ||
304 (index >= MEASUREMENT_SLOT_NR)) {
305 ret = RSI_ERROR_INPUT;
306 goto out_unmap_rd;
307 }
308
309 size = rec->regs[2];
310
311 if (size > MAX_EXTENDED_SIZE) {
312 ret = RSI_ERROR_INPUT;
313 goto out_unmap_rd;
314 }
315
316 extend_measurement = &rec->regs[3];
317 current_measurement = rd->measurement[index];
318
319 measurement_extend(rd->algorithm,
320 current_measurement,
321 extend_measurement,
322 size,
323 current_measurement);
324
325 ret = RSI_SUCCESS;
326
327out_unmap_rd:
328 buffer_unmap(rd);
329 granule_unlock(g_rd);
330 return ret;
331}
332
333unsigned long handle_rsi_read_measurement(struct rec *rec)
334{
335 struct rd *rd;
336 unsigned long idx;
337 size_t measurement_size;
338
339 assert(rec != NULL);
340
341 /* X1: Index */
342 idx = rec->regs[1];
343
344 if (idx >= MEASUREMENT_SLOT_NR) {
345 return RSI_ERROR_INPUT;
346 }
347
348 /*
349 * rd lock is acquired so that measurement cannot be updated
350 * simultaneously by another rec
351 */
352 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
353 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
354
355 measurement_size = measurement_get_size(rd->algorithm);
356
357 (void)memcpy(&rec->regs[1], rd->measurement[idx], measurement_size);
358
359 /* Zero-initialize the unused area */
360 if (measurement_size < MAX_MEASUREMENT_SIZE) {
361 (void)memset((char *)(&rec->regs[1]) + measurement_size,
362 0, MAX_MEASUREMENT_SIZE - measurement_size);
363 }
364
365 buffer_unmap(rd);
366 granule_unlock(rec->realm_info.g_rd);
367
368 return RSI_SUCCESS;
369}