Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 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 Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 18 | #include <measurement.h> |
| 19 | #include <qcbor/qcbor.h> |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 20 | #include <simd.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 21 | #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 | */ |
| 30 | static 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) { |
AlexeiFedorov | 4fc3b15 | 2023-06-13 12:08:01 +0100 | [diff] [blame] | 37 | case HASH_SHA_256: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 38 | *algo_text = UsefulBuf_FromSZ(sha256); |
| 39 | break; |
AlexeiFedorov | 4fc3b15 | 2023-06-13 12:08:01 +0100 | [diff] [blame] | 40 | case HASH_SHA_512: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 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 | */ |
| 76 | static enum attest_token_err_t |
| 77 | attest_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 Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 84 | struct t_cose_key attest_key; |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 85 | psa_key_handle_t key_handle; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 86 | |
| 87 | assert(me != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 88 | |
| 89 | /* Remember some of the configuration values */ |
| 90 | me->opt_flags = opt_flags; |
| 91 | me->key_select = key_select; |
| 92 | |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 93 | t_cose_signature_sign_restart_init(&me->restartable_signer_ctx, cose_alg_id); |
| 94 | t_cose_signature_sign_restart_set_crypto_context(&me->restartable_signer_ctx, &(me->crypto_ctx)); |
| 95 | t_cose_sign_sign_init(&me->sign_ctx, T_COSE_OPT_MESSAGE_TYPE_SIGN1); |
| 96 | t_cose_sign_add_signer(&me->sign_ctx, t_cose_signature_sign_from_restart(&me->restartable_signer_ctx)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | /* |
| 100 | * Get the reference to `mbedtls_ecp_keypair` and set it to t_cose. |
| 101 | */ |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 102 | attest_get_realm_signing_key(&key_handle); |
| 103 | attest_key.key.handle = key_handle; |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 104 | |
| 105 | t_cose_signature_sign_restart_set_signing_key(&me->restartable_signer_ctx, attest_key); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 106 | |
| 107 | /* Spin up the CBOR encoder */ |
| 108 | QCBOREncode_Init(&(me->cbor_enc_ctx), *out_buf); |
| 109 | |
| 110 | /* |
| 111 | * This will cause the cose headers to be encoded and written into |
| 112 | * out_buf using me->cbor_enc_ctx |
| 113 | */ |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 114 | cose_res = t_cose_sign_encode_start(&me->sign_ctx, &me->cbor_enc_ctx); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 115 | |
| 116 | if (cose_res != T_COSE_SUCCESS) { |
| 117 | return ATTEST_TOKEN_ERR_COSE_ERROR; |
| 118 | } |
| 119 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 120 | return ATTEST_TOKEN_ERR_SUCCESS; |
| 121 | } |
| 122 | |
| 123 | enum attest_token_err_t |
| 124 | attest_realm_token_sign(struct attest_token_encode_ctx *me, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 125 | size_t *completed_token_len) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 126 | { |
| 127 | /* The completed and signed encoded cose_sign1 */ |
| 128 | struct q_useful_buf_c completed_token_ub; |
| 129 | enum attest_token_err_t attest_res = ATTEST_TOKEN_ERR_SUCCESS; |
| 130 | QCBORError qcbor_res; |
| 131 | enum t_cose_err_t cose_res; |
| 132 | |
| 133 | assert(me != NULL); |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 134 | assert(completed_token_len != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 135 | |
| 136 | /* Finish up the COSE_Sign1. This is where the signing happens */ |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 137 | SIMD_FPU_ALLOW( |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 138 | cose_res = t_cose_sign_encode_finish(&me->sign_ctx, |
| 139 | NULL_Q_USEFUL_BUF_C, |
| 140 | me->signed_payload, |
| 141 | &me->cbor_enc_ctx)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 142 | |
| 143 | if (cose_res == T_COSE_ERR_SIG_IN_PROGRESS) { |
| 144 | /* Token signing has not yet finished */ |
| 145 | return ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS; |
| 146 | } |
| 147 | |
| 148 | if (cose_res != T_COSE_SUCCESS) { |
| 149 | /* Main errors are invoking the hash or signature */ |
| 150 | return ATTEST_TOKEN_ERR_COSE_ERROR; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Finally close off the CBOR formatting and get the pointer and length |
| 155 | * of the resulting COSE_Sign1 |
| 156 | */ |
| 157 | qcbor_res = QCBOREncode_Finish(&(me->cbor_enc_ctx), |
| 158 | &completed_token_ub); |
| 159 | |
| 160 | switch (qcbor_res) { |
| 161 | case QCBOR_ERR_BUFFER_TOO_SMALL: |
| 162 | attest_res = ATTEST_TOKEN_ERR_TOO_SMALL; |
| 163 | break; |
| 164 | case QCBOR_SUCCESS: |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 165 | *completed_token_len = completed_token_ub.len; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 166 | break; |
| 167 | default: |
| 168 | /* likely from array not closed, too many closes ... */ |
| 169 | attest_res = ATTEST_TOKEN_ERR_CBOR_FORMATTING; |
| 170 | } |
| 171 | |
| 172 | return attest_res; |
| 173 | } |
| 174 | |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 175 | size_t attest_cca_token_create(void *attest_token_buf, |
| 176 | size_t attest_token_buf_size, |
| 177 | const void *realm_token_buf, |
| 178 | size_t realm_token_len) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 179 | { |
| 180 | struct q_useful_buf_c completed_token; |
| 181 | QCBOREncodeContext cbor_enc_ctx; |
| 182 | QCBORError qcbor_res; |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 183 | struct q_useful_buf_c rmm_platform_token; |
| 184 | struct q_useful_buf attest_token_ub = {attest_token_buf, attest_token_buf_size}; |
| 185 | struct q_useful_buf_c realm_token_ub = {realm_token_buf, realm_token_len}; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 186 | |
| 187 | __unused int ret; |
| 188 | |
| 189 | /* Get the platform token */ |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 190 | ret = attest_get_platform_token(&rmm_platform_token.ptr, |
| 191 | &rmm_platform_token.len); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 192 | assert(ret == 0); |
| 193 | |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 194 | QCBOREncode_Init(&cbor_enc_ctx, attest_token_ub); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 195 | |
| 196 | QCBOREncode_AddTag(&cbor_enc_ctx, TAG_CCA_TOKEN); |
| 197 | |
| 198 | QCBOREncode_OpenMap(&cbor_enc_ctx); |
| 199 | |
| 200 | QCBOREncode_AddBytesToMapN(&cbor_enc_ctx, |
| 201 | CCA_PLAT_TOKEN, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 202 | rmm_platform_token); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 203 | |
| 204 | QCBOREncode_AddBytesToMapN(&cbor_enc_ctx, |
| 205 | CCA_REALM_DELEGATED_TOKEN, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 206 | realm_token_ub); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 207 | QCBOREncode_CloseMap(&cbor_enc_ctx); |
| 208 | |
| 209 | qcbor_res = QCBOREncode_Finish(&cbor_enc_ctx, &completed_token); |
| 210 | |
| 211 | if (qcbor_res == QCBOR_ERR_BUFFER_TOO_SMALL) { |
| 212 | ERROR("CCA output token buffer too small\n"); |
| 213 | return 0; |
| 214 | } else if (qcbor_res != QCBOR_SUCCESS) { |
| 215 | /* likely from array not closed, too many closes, ... */ |
| 216 | assert(false); |
| 217 | } else { |
| 218 | return completed_token.len; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Assemble the Realm Attestation Token in the buffer provided in |
| 225 | * realm_token_buf, except the signature. |
| 226 | * |
| 227 | * As per section A7.2.3.1 of RMM specfication, Realm Attestation token is |
| 228 | * composed of: |
| 229 | * - Realm Challenge |
| 230 | * - Realm Personalization Value |
| 231 | * - Realm Hash Algorithm Id |
| 232 | * - Realm Public Key |
| 233 | * - Realm Public Key Hash Algorithm Id |
| 234 | * - Realm Initial Measurement |
| 235 | * - Realm Extensible Measurements |
| 236 | */ |
| 237 | int attest_realm_token_create(enum hash_algo algorithm, |
| 238 | unsigned char measurements[][MAX_MEASUREMENT_SIZE], |
| 239 | unsigned int num_measurements, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 240 | const void *rpv_buf, |
| 241 | size_t rpv_len, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 242 | struct token_sign_ctx *ctx, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 243 | void *realm_token_buf, |
| 244 | size_t realm_token_buf_size) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 245 | { |
| 246 | struct q_useful_buf_c buf; |
| 247 | size_t measurement_size; |
| 248 | enum attest_token_err_t token_ret; |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 249 | struct q_useful_buf realm_token_ub = {realm_token_buf, realm_token_buf_size}; |
| 250 | struct q_useful_buf_c rpv_ub = {rpv_buf, rpv_len}; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 251 | int ret; |
| 252 | |
| 253 | /* Can only be called in the init state */ |
| 254 | assert(ctx->state == ATTEST_SIGN_NOT_STARTED); |
| 255 | |
| 256 | assert(num_measurements == MEASUREMENT_SLOT_NR); |
| 257 | |
| 258 | /* |
| 259 | * Get started creating the token. This sets up the CBOR and COSE |
| 260 | * contexts which causes the COSE headers to be constructed. |
| 261 | */ |
| 262 | token_ret = attest_token_encode_start(&(ctx->ctx), |
| 263 | 0, /* option_flags */ |
| 264 | 0, /* key_select */ |
| 265 | T_COSE_ALGORITHM_ES384, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 266 | &realm_token_ub); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 267 | if (token_ret != ATTEST_TOKEN_ERR_SUCCESS) { |
| 268 | return token_ret; |
| 269 | } |
| 270 | |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 271 | QCBOREncode_BstrWrap(&(ctx->ctx.cbor_enc_ctx)); |
| 272 | QCBOREncode_OpenMap(&(ctx->ctx.cbor_enc_ctx)); |
| 273 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 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, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 283 | rpv_ub); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 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)); |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 326 | QCBOREncode_CloseBstrWrap2(&(ctx->ctx.cbor_enc_ctx), false, |
| 327 | &(ctx->ctx.signed_payload)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 328 | |
| 329 | return ATTEST_TOKEN_ERR_SUCCESS; |
| 330 | } |