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.h |
| 10 | */ |
| 11 | |
| 12 | #ifndef ATTESTATION_TOKEN_H |
| 13 | #define ATTESTATION_TOKEN_H |
| 14 | |
| 15 | #include <measurement.h> |
Mate Toth-Pal | c08e011 | 2023-06-27 13:08:31 +0200 | [diff] [blame] | 16 | #ifndef CBMC |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 17 | #include <qcbor/qcbor.h> |
Mate Toth-Pal | c08e011 | 2023-06-27 13:08:31 +0200 | [diff] [blame] | 18 | #endif /* CBMC */ |
| 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
| 21 | #ifndef CBMC |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 22 | #include <t_cose/q_useful_buf.h> |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 23 | #include <t_cose/t_cose_sign_sign.h> |
| 24 | #include <t_cose/t_cose_signature_sign_restart.h> |
Soby Mathew | 9ca5755 | 2024-10-03 12:21:36 +0100 | [diff] [blame] | 25 | #include <t_cose_el3_token_sign.h> |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 26 | #include <t_cose_psa_crypto.h> |
Mate Toth-Pal | c08e011 | 2023-06-27 13:08:31 +0200 | [diff] [blame] | 27 | #endif /* CBMC */ |
| 28 | |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 29 | /* The state of the CCA token generation */ |
Mate Toth-Pal | c08e011 | 2023-06-27 13:08:31 +0200 | [diff] [blame] | 30 | enum attest_token_gen_state_t { |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 31 | ATTEST_TOKEN_NOT_STARTED, /* Initial phase */ |
| 32 | ATTEST_TOKEN_INIT, /* Initialized */ |
| 33 | ATTEST_TOKEN_SIGN, /* Realm token sign */ |
| 34 | ATTEST_TOKEN_CREATE, /* CCA Token create */ |
Mate Toth-Pal | c08e011 | 2023-06-27 13:08:31 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | #ifndef CBMC |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 38 | |
| 39 | #define ATTEST_TOKEN_BUFFER_SIZE GRANULE_SIZE |
| 40 | |
| 41 | enum attest_token_err_t { |
| 42 | /* Success */ |
| 43 | ATTEST_TOKEN_ERR_SUCCESS = 0, |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 44 | /* The Attest token context state is incorrect */ |
| 45 | ATTEST_TOKEN_ERR_INVALID_STATE, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 46 | /* The buffer passed in to receive the output is too small. */ |
| 47 | ATTEST_TOKEN_ERR_TOO_SMALL, |
| 48 | /* |
| 49 | * Something went wrong formatting the CBOR, most likely the |
| 50 | * payload has maps or arrays that are not closed. |
| 51 | */ |
| 52 | ATTEST_TOKEN_ERR_CBOR_FORMATTING, |
| 53 | /* Signing key is not found or of wrong type. */ |
| 54 | ATTEST_TOKEN_ERR_SIGNING_KEY, |
| 55 | ATTEST_TOKEN_ERR_COSE_ERROR, |
Mate Toth-Pal | c08e011 | 2023-06-27 13:08:31 +0200 | [diff] [blame] | 56 | /* |
| 57 | * Signing is in progress, function should be called with the same |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 58 | * parameters again. |
| 59 | */ |
Juan Pablo Conde | 79ca256 | 2024-08-12 17:05:41 -0500 | [diff] [blame] | 60 | ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS, |
| 61 | /* |
| 62 | * Error code to return when CCA token creation fails. |
| 63 | */ |
| 64 | ATTEST_TOKEN_ERR_CCA_TOKEN_CREATE |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 67 | /* |
| 68 | * The context for creating an attestation token. The caller of |
| 69 | * attest_token_encode must create one of these and pass it to the functions |
| 70 | * here. It is small enough that it can go on the stack. It is most of |
| 71 | * the memory needed to create a token except the output buffer and |
| 72 | * any memory requirements for the cryptographic operations. |
| 73 | * |
| 74 | * The structure is opaque for the caller. |
| 75 | * |
| 76 | * This is roughly 148 + 8 + 32 = 188 bytes |
| 77 | */ |
| 78 | |
| 79 | struct attest_token_encode_ctx { |
| 80 | /* Private data structure */ |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 81 | QCBOREncodeContext cbor_enc_ctx; |
| 82 | uint32_t opt_flags; |
| 83 | int32_t key_select; |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 84 | struct q_useful_buf_c signed_payload; |
| 85 | struct t_cose_sign_sign_ctx sign_ctx; |
| 86 | struct t_cose_signature_sign_restart restartable_signer_ctx; |
Soby Mathew | 9ca5755 | 2024-10-03 12:21:36 +0100 | [diff] [blame] | 87 | #if ATTEST_EL3_TOKEN_SIGN |
| 88 | struct t_cose_el3_token_sign_ctx crypto_ctx; |
| 89 | #else |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 90 | struct t_cose_psa_crypto_context crypto_ctx; |
Soby Mathew | 9ca5755 | 2024-10-03 12:21:36 +0100 | [diff] [blame] | 91 | #endif |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | #define ATTEST_CHALLENGE_SIZE (64) |
| 95 | |
| 96 | /* |
| 97 | * The context for signing an attestation token. Each REC contains one context |
| 98 | * that is passed to the attestation library during attestation token creation |
| 99 | * to keep track of the signing state. |
| 100 | */ |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 101 | struct token_sign_cntxt { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 102 | /* |
| 103 | * 'state' is used to implement a state machine |
| 104 | * to track the current state of signing. |
| 105 | */ |
| 106 | enum attest_token_gen_state_t state; |
| 107 | struct attest_token_encode_ctx ctx; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Mate Toth-Pal | c08e011 | 2023-06-27 13:08:31 +0200 | [diff] [blame] | 110 | #else /* CBMC */ |
| 111 | |
| 112 | #define ATTEST_TOKEN_BUFFER_SIZE GRANULE_SIZE |
| 113 | |
| 114 | enum attest_token_err_t { |
| 115 | /* Success */ |
| 116 | ATTEST_TOKEN_ERR_SUCCESS = 0, |
| 117 | /* |
| 118 | * Signing is in progress, function should be called with the same |
| 119 | * parameters again. |
| 120 | */ |
| 121 | ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS |
| 122 | }; |
| 123 | |
| 124 | struct attest_token_encode_ctx { |
| 125 | uint32_t unused; |
| 126 | }; |
| 127 | |
| 128 | struct token_sign_cntxt { |
| 129 | enum attest_token_gen_state_t state; |
| 130 | }; |
| 131 | |
| 132 | #define ATTEST_CHALLENGE_SIZE (1) |
| 133 | |
| 134 | #endif /* CBMC */ |
| 135 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Sign the realm token and complete the CBOR encoding. |
| 138 | * This function returns ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS |
| 139 | * if signing is not complete and this function needs to be |
| 140 | * invoked again. ATTEST_TOKEN_ERR_SUCCESS is returned if |
| 141 | * signing is complete and `completed_token` is valid. |
| 142 | * Else returns one of the attest_token_err_t errors on |
| 143 | * any other error. |
| 144 | * |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 145 | * me Token Sign Context. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 146 | * completed_token_len Length of the completed token. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 147 | * |
| 148 | * This completes the token after the payload has been added. When |
| 149 | * this is called the signing algorithm is run and the final |
| 150 | * formatting of the token is completed. |
| 151 | */ |
| 152 | enum attest_token_err_t |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 153 | attest_realm_token_sign(struct token_sign_cntxt *me, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 154 | size_t *completed_token_len); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * Combine realm token and platform token to top-level cca token |
| 158 | * |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 159 | * me Token Sign Context. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 160 | * attest_token_buf Pointer to the buffer where the token will be |
AlexeiFedorov | 4716542 | 2023-09-13 11:47:57 +0100 | [diff] [blame] | 161 | * written. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 162 | * attest_token_buf_size Size of the buffer where the token will be |
AlexeiFedorov | 4716542 | 2023-09-13 11:47:57 +0100 | [diff] [blame] | 163 | * written. |
Juan Pablo Conde | 79ca256 | 2024-08-12 17:05:41 -0500 | [diff] [blame] | 164 | * realm_token_buf Pointer to the realm token. |
| 165 | * realm_token_len Length of the realm token. |
| 166 | * cca_token_len Returns the length of top-level CCA token |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 167 | * |
Juan Pablo Conde | 79ca256 | 2024-08-12 17:05:41 -0500 | [diff] [blame] | 168 | * Returns ATTEST_TOKEN_ERR_SUCCESS (0) if CCA top-level token is |
| 169 | * created. Otherwise, returns the proper error value. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 170 | */ |
Juan Pablo Conde | 79ca256 | 2024-08-12 17:05:41 -0500 | [diff] [blame] | 171 | enum attest_token_err_t |
| 172 | attest_cca_token_create(struct token_sign_cntxt *me, |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 173 | void *attest_token_buf, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 174 | size_t attest_token_buf_size, |
| 175 | const void *realm_token_buf, |
Juan Pablo Conde | 79ca256 | 2024-08-12 17:05:41 -0500 | [diff] [blame] | 176 | size_t realm_token_len, |
| 177 | size_t *cca_token_len); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Assemble the Realm token in the buffer provided in realm_token_buf, |
| 181 | * except the signature. |
| 182 | * |
| 183 | * Arguments: |
| 184 | * Algorithm - Algorithm used during measurement. |
| 185 | * Measurement - Array of buffers containing all the measurements. |
| 186 | * num_measurements - Number of measurements to add to the token. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 187 | * rpv_buf - Pointer to the Realm Personalization value |
| 188 | * rpv_len - Length of the Realm Personalization value |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 189 | * ctx - Token sign context, used for signing. |
| 190 | * realm_token_buf - Buffer where to assemble the attestation token. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 191 | * realm_token_buf_size - size of the buffer where to assemble the attestation |
| 192 | * token. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 193 | * |
| 194 | * Returns ATTEST_TOKEN_ERR_SUCCESS (0) on success or a negative error code |
| 195 | * otherwise. |
| 196 | */ |
| 197 | int attest_realm_token_create(enum hash_algo algorithm, |
| 198 | unsigned char measurements[][MAX_MEASUREMENT_SIZE], |
| 199 | unsigned int num_measurements, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 200 | const void *rpv_buf, |
| 201 | size_t rpv_len, |
Mate Toth-Pal | 4feff40 | 2024-08-30 10:53:32 +0200 | [diff] [blame] | 202 | const void *challenge_buf, |
| 203 | size_t challenge_len, |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 204 | struct token_sign_cntxt *ctx, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 205 | void *realm_token_buf, |
| 206 | size_t realm_token_buf_size); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 207 | |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 208 | /* |
| 209 | * Initialize the token sign context and also the heap buffer used for the crypto. |
| 210 | * It is assumed that the heap alloc context has already been assigned to this |
| 211 | * CPU. If the token sign context has already been initialized, this API will |
| 212 | * not initialize again as an optimization. |
| 213 | * |
| 214 | * Arguments: |
| 215 | * token_ctx - Token sign context. |
| 216 | * heap_buf - Buffer to use as heap. |
| 217 | * heap_buf_len - Size of the buffer to use as heap. |
Soby Mathew | 9ca5755 | 2024-10-03 12:21:36 +0100 | [diff] [blame] | 218 | * cookie - Unique cookie to associate with the context (ex rec granule PA) |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 219 | * |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 220 | * Return code: |
| 221 | * ATTEST_TOKEN_ERR_SUCCESS (0) - Success. |
| 222 | * ATTEST_TOKEN_ERR_INVALID_STATE - Failed possibly due to invalid state. |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 223 | */ |
| 224 | int attest_token_ctx_init(struct token_sign_cntxt *token_ctx, |
| 225 | unsigned char *heap_buf, |
Soby Mathew | 9ca5755 | 2024-10-03 12:21:36 +0100 | [diff] [blame] | 226 | unsigned int heap_buf_len, |
| 227 | uintptr_t cookie); |
| 228 | |
| 229 | /* |
| 230 | * Pull the response from EL3 into the per cpu response buffer. The function |
| 231 | * returns the cookie associated with the response. The response could correspond |
| 232 | * to current REC or another REC which had requested the EL3 service. |
| 233 | * |
| 234 | * Arguments: |
| 235 | * cookie - Pointer to storage of cookie to return the value from |
| 236 | * response. |
| 237 | * |
| 238 | * Return code: |
| 239 | * 0 - Success |
| 240 | * -EAGAIN - Response not ready. Call this API again. |
| 241 | * -ENOTSUP - Other error including EL3_TOKEN_SIGN not supported in |
| 242 | * EL3 firmware. |
| 243 | */ |
| 244 | int attest_el3_token_sign_pull_response_from_el3(uintptr_t *cookie); |
| 245 | |
| 246 | /* |
| 247 | * Write the response from EL3 to the context. The response is written only if the context |
| 248 | * is valid and the response is for the right request. If the function returns an error |
| 249 | * the caller must treat it as a fatal error. The cookie is checked against the per cpu |
| 250 | * response buffer to ensure that the response is for the right request. |
| 251 | * The caller must ensure that the REC granule lock is held so that it cannot be deleted |
| 252 | * while the response is being written. |
| 253 | * |
| 254 | * Arguments: |
| 255 | * ctx - Pointer to token_sign_cntxt |
| 256 | * cookie - Pointer to storage of cookie to return the value from |
| 257 | * response. |
| 258 | * |
| 259 | * Returns 0 on success or a negative error code otherwise. |
| 260 | */ |
| 261 | int attest_el3_token_write_response_to_ctx(struct token_sign_cntxt *sign_ctx, uintptr_t cookie); |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 262 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 263 | #endif /* ATTESTATION_TOKEN_H */ |