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