blob: 51d9fce1fca029c309fe106f8c1b3aa50abe25fc [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright Laurence Lundblade.
4 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
5 */
6
7/*
8 * This file is derived from:
9 * trusted-firmware-m/secure_fw/partitions/initial_attestation/attest_token_encode.c
10 */
11
12#include <assert.h>
13#include <attestation.h>
14#include <attestation_defs_priv.h>
15#include <attestation_priv.h>
16#include <attestation_token.h>
17#include <debug.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000018#include <measurement.h>
19#include <qcbor/qcbor.h>
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000020#include <simd.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000021#include <t_cose/q_useful_buf.h>
22#include <t_cose/t_cose_common.h>
23#include <t_cose/t_cose_sign1_sign.h>
24#include <utils_def.h>
25
26/*
27 * According to IANA hash algorithm registry:
28 * - https://www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xml
29 */
30static void attest_get_hash_algo_text(enum hash_algo algo_id,
31 struct q_useful_buf_c *algo_text)
32{
33 const char *sha256 = "sha-256";
34 const char *sha512 = "sha-512";
35
36 switch (algo_id) {
37 case HASH_ALGO_SHA256:
38 *algo_text = UsefulBuf_FromSZ(sha256);
39 break;
40 case HASH_ALGO_SHA512:
41 *algo_text = UsefulBuf_FromSZ(sha512);
42 break;
43 default:
44 assert(false);
45 }
46}
47
48/*
49 * Outline of token creation. Much of this occurs inside
50 * t_cose_sign1_encode_parameters() and t_cose_sign1_encode_signature().
51 *
52 * - Create encoder context
53 * - Open the CBOR array that hold the COSE_Sign1
54 * - Write COSE Headers
55 * - Protected Header
56 * - Algorithm ID
57 * - Unprotected Headers
58 * - Key ID
59 * - Open payload bstr
60 * - Write payload data, maybe lots of it
61 * - Get bstr that is the encoded payload
62 * - Compute signature
63 * - Create a separate encoder context for Sig_structure
64 * - Encode CBOR context identifier
65 * - Encode protected headers
66 * - Encode two empty bstr
67 * - Add one more empty bstr that is a "fake payload"
68 * - Close off Sig_structure
69 * - Hash all but "fake payload" of Sig_structure
70 * - Get payload bstr ptr and length
71 * - Continue hash of the real encoded payload
72 * - Run ECDSA
73 * - Write signature into the CBOR output
74 * - Close CBOR array holding the COSE_Sign1
75 */
76static enum attest_token_err_t
77attest_token_encode_start(struct attest_token_encode_ctx *me,
78 uint32_t opt_flags,
79 int32_t key_select,
80 int32_t cose_alg_id,
81 const struct q_useful_buf *out_buf)
82{
83 enum t_cose_err_t cose_res;
Soby Mathew4d4c21a2023-06-29 14:52:41 +020084 uint32_t t_cose_options = 0;
Soby Mathewb4c6df42022-11-09 11:13:29 +000085 struct t_cose_key attest_key;
Mate Toth-Palc69951d2023-03-17 17:30:50 +010086 psa_key_handle_t key_handle;
Soby Mathew4d4c21a2023-06-29 14:52:41 +020087 struct q_useful_buf_c attest_key_id = NULL_Q_USEFUL_BUF_C;
Soby Mathewb4c6df42022-11-09 11:13:29 +000088
89 assert(me != NULL);
90 assert(out_buf != NULL);
91
92 /* Remember some of the configuration values */
93 me->opt_flags = opt_flags;
94 me->key_select = key_select;
95
Soby Mathew4d4c21a2023-06-29 14:52:41 +020096 t_cose_sign1_sign_init(&(me->signer_ctx),
97 t_cose_options,
98 cose_alg_id);
99
100 cose_res = t_cose_sign1_set_restart(&(me->signer_ctx),
101 &(me->signer_restart_ctx));
102 if (cose_res != T_COSE_SUCCESS) {
103 return ATTEST_TOKEN_ERR_COSE_ERROR;
104 }
105
106 t_cose_sign1_set_crypto_context(&(me->signer_ctx),
107 &(me->crypto_ctx));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000108
109
110 /*
111 * Get the reference to `mbedtls_ecp_keypair` and set it to t_cose.
112 */
Mate Toth-Palc69951d2023-03-17 17:30:50 +0100113 attest_get_realm_signing_key(&key_handle);
114 attest_key.key.handle = key_handle;
Soby Mathew4d4c21a2023-06-29 14:52:41 +0200115 t_cose_sign1_set_signing_key(&(me->signer_ctx),
116 attest_key,
117 attest_key_id);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000118
119 /* Spin up the CBOR encoder */
120 QCBOREncode_Init(&(me->cbor_enc_ctx), *out_buf);
121
122 /*
123 * This will cause the cose headers to be encoded and written into
124 * out_buf using me->cbor_enc_ctx
125 */
Soby Mathew4d4c21a2023-06-29 14:52:41 +0200126 cose_res = t_cose_sign1_encode_parameters(&(me->signer_ctx),
127 &(me->cbor_enc_ctx));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000128
129 if (cose_res != T_COSE_SUCCESS) {
130 return ATTEST_TOKEN_ERR_COSE_ERROR;
131 }
132
Soby Mathew4d4c21a2023-06-29 14:52:41 +0200133 QCBOREncode_OpenMap(&(me->cbor_enc_ctx));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000134 return ATTEST_TOKEN_ERR_SUCCESS;
135}
136
137enum attest_token_err_t
138attest_realm_token_sign(struct attest_token_encode_ctx *me,
139 struct q_useful_buf_c *completed_token)
140{
141 /* The completed and signed encoded cose_sign1 */
142 struct q_useful_buf_c completed_token_ub;
143 enum attest_token_err_t attest_res = ATTEST_TOKEN_ERR_SUCCESS;
144 QCBORError qcbor_res;
145 enum t_cose_err_t cose_res;
146
147 assert(me != NULL);
148 assert(completed_token != NULL);
149
150 /* Finish up the COSE_Sign1. This is where the signing happens */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000151 SIMD_FPU_ALLOW(
Soby Mathew4d4c21a2023-06-29 14:52:41 +0200152 cose_res = t_cose_sign1_encode_signature(&(me->signer_ctx),
153 &(me->cbor_enc_ctx)));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000154
155 if (cose_res == T_COSE_ERR_SIG_IN_PROGRESS) {
156 /* Token signing has not yet finished */
157 return ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS;
158 }
159
160 if (cose_res != T_COSE_SUCCESS) {
161 /* Main errors are invoking the hash or signature */
162 return ATTEST_TOKEN_ERR_COSE_ERROR;
163 }
164
165 /*
166 * Finally close off the CBOR formatting and get the pointer and length
167 * of the resulting COSE_Sign1
168 */
169 qcbor_res = QCBOREncode_Finish(&(me->cbor_enc_ctx),
170 &completed_token_ub);
171
172 switch (qcbor_res) {
173 case QCBOR_ERR_BUFFER_TOO_SMALL:
174 attest_res = ATTEST_TOKEN_ERR_TOO_SMALL;
175 break;
176 case QCBOR_SUCCESS:
177 *completed_token = completed_token_ub;
178 break;
179 default:
180 /* likely from array not closed, too many closes ... */
181 attest_res = ATTEST_TOKEN_ERR_CBOR_FORMATTING;
182 }
183
184 return attest_res;
185}
186
187size_t attest_cca_token_create(struct q_useful_buf *attest_token_buf,
188 const struct q_useful_buf_c *realm_token)
189{
190 struct q_useful_buf_c completed_token;
191 QCBOREncodeContext cbor_enc_ctx;
192 QCBORError qcbor_res;
193 struct q_useful_buf_c *rmm_platform_token;
194
195 __unused int ret;
196
197 /* Get the platform token */
198 ret = attest_get_platform_token(&rmm_platform_token);
199 assert(ret == 0);
200
201 QCBOREncode_Init(&cbor_enc_ctx, *attest_token_buf);
202
203 QCBOREncode_AddTag(&cbor_enc_ctx, TAG_CCA_TOKEN);
204
205 QCBOREncode_OpenMap(&cbor_enc_ctx);
206
207 QCBOREncode_AddBytesToMapN(&cbor_enc_ctx,
208 CCA_PLAT_TOKEN,
209 *rmm_platform_token);
210
211 QCBOREncode_AddBytesToMapN(&cbor_enc_ctx,
212 CCA_REALM_DELEGATED_TOKEN,
213 *realm_token);
214 QCBOREncode_CloseMap(&cbor_enc_ctx);
215
216 qcbor_res = QCBOREncode_Finish(&cbor_enc_ctx, &completed_token);
217
218 if (qcbor_res == QCBOR_ERR_BUFFER_TOO_SMALL) {
219 ERROR("CCA output token buffer too small\n");
220 return 0;
221 } else if (qcbor_res != QCBOR_SUCCESS) {
222 /* likely from array not closed, too many closes, ... */
223 assert(false);
224 } else {
225 return completed_token.len;
226 }
227 return 0;
228}
229
230/*
231 * Assemble the Realm Attestation Token in the buffer provided in
232 * realm_token_buf, except the signature.
233 *
234 * As per section A7.2.3.1 of RMM specfication, Realm Attestation token is
235 * composed of:
236 * - Realm Challenge
237 * - Realm Personalization Value
238 * - Realm Hash Algorithm Id
239 * - Realm Public Key
240 * - Realm Public Key Hash Algorithm Id
241 * - Realm Initial Measurement
242 * - Realm Extensible Measurements
243 */
244int attest_realm_token_create(enum hash_algo algorithm,
245 unsigned char measurements[][MAX_MEASUREMENT_SIZE],
246 unsigned int num_measurements,
247 struct q_useful_buf_c *rpv,
248 struct token_sign_ctx *ctx,
249 struct q_useful_buf *realm_token_buf)
250{
251 struct q_useful_buf_c buf;
252 size_t measurement_size;
253 enum attest_token_err_t token_ret;
254 int ret;
255
256 /* Can only be called in the init state */
257 assert(ctx->state == ATTEST_SIGN_NOT_STARTED);
258
259 assert(num_measurements == MEASUREMENT_SLOT_NR);
260
261 /*
262 * Get started creating the token. This sets up the CBOR and COSE
263 * contexts which causes the COSE headers to be constructed.
264 */
265 token_ret = attest_token_encode_start(&(ctx->ctx),
266 0, /* option_flags */
267 0, /* key_select */
268 T_COSE_ALGORITHM_ES384,
269 realm_token_buf);
270 if (token_ret != ATTEST_TOKEN_ERR_SUCCESS) {
271 return token_ret;
272 }
273
274 /* Add challenge value, which is the only input from the caller. */
275 buf.ptr = ctx->challenge;
276 buf.len = ATTEST_CHALLENGE_SIZE;
277 QCBOREncode_AddBytesToMapN(&(ctx->ctx.cbor_enc_ctx),
278 CCA_REALM_CHALLENGE,
279 buf);
280
281 QCBOREncode_AddBytesToMapN(&(ctx->ctx.cbor_enc_ctx),
282 CCA_REALM_PERSONALIZATION_VALUE,
283 *rpv);
284
285 ret = attest_get_realm_public_key(&buf);
286 if (ret != 0) {
287 return ret;
288 }
289
290 QCBOREncode_AddBytesToMapN(&(ctx->ctx.cbor_enc_ctx),
291 CCA_REALM_PUB_KEY,
292 buf);
293
294 attest_get_hash_algo_text(algorithm, &buf);
295 QCBOREncode_AddTextToMapN(&(ctx->ctx.cbor_enc_ctx),
296 CCA_REALM_HASH_ALGM_ID,
297 buf);
298
299 attest_get_hash_algo_text(attest_get_realm_public_key_hash_algo_id(),
300 &buf);
301 QCBOREncode_AddTextToMapN(&(ctx->ctx.cbor_enc_ctx),
302 CCA_REALM_PUB_KEY_HASH_ALGO_ID,
303 buf);
304
305 measurement_size = measurement_get_size(algorithm);
306 assert(measurement_size <= MAX_MEASUREMENT_SIZE);
307
308 /* RIM: 0, REM: 1..4 */
309 buf.ptr = &measurements[RIM_MEASUREMENT_SLOT];
310 buf.len = measurement_size;
311 QCBOREncode_AddBytesToMapN(&(ctx->ctx.cbor_enc_ctx),
312 CCA_REALM_INITIAL_MEASUREMENT,
313 buf);
314
315 QCBOREncode_OpenArrayInMapN(&(ctx->ctx.cbor_enc_ctx),
316 CCA_REALM_EXTENSIBLE_MEASUREMENTS);
317
318 for (unsigned int i = 1U; i < num_measurements; ++i) {
319 buf.ptr = &measurements[i];
320 buf.len = measurement_size;
321 QCBOREncode_AddBytes(&(ctx->ctx.cbor_enc_ctx), buf);
322 }
323
324 QCBOREncode_CloseArray(&(ctx->ctx.cbor_enc_ctx));
325 QCBOREncode_CloseMap(&(ctx->ctx.cbor_enc_ctx));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000326
327 return ATTEST_TOKEN_ERR_SUCCESS;
328}