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 | */ |
| 59 | ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS |
| 60 | }; |
| 61 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 62 | /* |
| 63 | * The context for creating an attestation token. The caller of |
| 64 | * attest_token_encode must create one of these and pass it to the functions |
| 65 | * here. It is small enough that it can go on the stack. It is most of |
| 66 | * the memory needed to create a token except the output buffer and |
| 67 | * any memory requirements for the cryptographic operations. |
| 68 | * |
| 69 | * The structure is opaque for the caller. |
| 70 | * |
| 71 | * This is roughly 148 + 8 + 32 = 188 bytes |
| 72 | */ |
| 73 | |
| 74 | struct attest_token_encode_ctx { |
| 75 | /* Private data structure */ |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 76 | QCBOREncodeContext cbor_enc_ctx; |
| 77 | uint32_t opt_flags; |
| 78 | int32_t key_select; |
Mate Toth-Pal | fda673a | 2023-06-13 12:25:43 +0200 | [diff] [blame] | 79 | struct q_useful_buf_c signed_payload; |
| 80 | struct t_cose_sign_sign_ctx sign_ctx; |
| 81 | struct t_cose_signature_sign_restart restartable_signer_ctx; |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 82 | struct t_cose_psa_crypto_context crypto_ctx; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | #define ATTEST_CHALLENGE_SIZE (64) |
| 86 | |
| 87 | /* |
| 88 | * The context for signing an attestation token. Each REC contains one context |
| 89 | * that is passed to the attestation library during attestation token creation |
| 90 | * to keep track of the signing state. |
| 91 | */ |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 92 | struct token_sign_cntxt { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 93 | /* |
| 94 | * 'state' is used to implement a state machine |
| 95 | * to track the current state of signing. |
| 96 | */ |
| 97 | enum attest_token_gen_state_t state; |
| 98 | struct attest_token_encode_ctx ctx; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 99 | |
| 100 | /* Number of CCA token bytes left to copy to the Realm */ |
| 101 | size_t cca_token_len; |
| 102 | |
| 103 | /* Number of CCA token bytes copied to the Realm */ |
| 104 | size_t copied_len; |
| 105 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 106 | unsigned char challenge[ATTEST_CHALLENGE_SIZE]; |
| 107 | }; |
| 108 | |
Mate Toth-Pal | c08e011 | 2023-06-27 13:08:31 +0200 | [diff] [blame] | 109 | #else /* CBMC */ |
| 110 | |
| 111 | #define ATTEST_TOKEN_BUFFER_SIZE GRANULE_SIZE |
| 112 | |
| 113 | enum attest_token_err_t { |
| 114 | /* Success */ |
| 115 | ATTEST_TOKEN_ERR_SUCCESS = 0, |
| 116 | /* |
| 117 | * Signing is in progress, function should be called with the same |
| 118 | * parameters again. |
| 119 | */ |
| 120 | ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS |
| 121 | }; |
| 122 | |
| 123 | struct attest_token_encode_ctx { |
| 124 | uint32_t unused; |
| 125 | }; |
| 126 | |
| 127 | struct token_sign_cntxt { |
| 128 | enum attest_token_gen_state_t state; |
| 129 | }; |
| 130 | |
| 131 | #define ATTEST_CHALLENGE_SIZE (1) |
| 132 | |
| 133 | #endif /* CBMC */ |
| 134 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 135 | /* |
| 136 | * Sign the realm token and complete the CBOR encoding. |
| 137 | * This function returns ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS |
| 138 | * if signing is not complete and this function needs to be |
| 139 | * invoked again. ATTEST_TOKEN_ERR_SUCCESS is returned if |
| 140 | * signing is complete and `completed_token` is valid. |
| 141 | * Else returns one of the attest_token_err_t errors on |
| 142 | * any other error. |
| 143 | * |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame^] | 144 | * me Token Sign Context. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 145 | * completed_token_len Length of the completed token. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 146 | * |
| 147 | * This completes the token after the payload has been added. When |
| 148 | * this is called the signing algorithm is run and the final |
| 149 | * formatting of the token is completed. |
| 150 | */ |
| 151 | enum attest_token_err_t |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame^] | 152 | attest_realm_token_sign(struct token_sign_cntxt *me, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 153 | size_t *completed_token_len); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * Combine realm token and platform token to top-level cca token |
| 157 | * |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame^] | 158 | * me Token Sign Context. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 159 | * attest_token_buf Pointer to the buffer where the token will be |
AlexeiFedorov | 4716542 | 2023-09-13 11:47:57 +0100 | [diff] [blame] | 160 | * written. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 161 | * attest_token_buf_size Size of the buffer where the token will be |
AlexeiFedorov | 4716542 | 2023-09-13 11:47:57 +0100 | [diff] [blame] | 162 | * written. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 163 | * realm_token_buf Pointer to the realm token. |
| 164 | * realm_token_len Length of the realm token. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 165 | * |
| 166 | * Return 0 in case of error, the length of the cca token otherwise. |
| 167 | */ |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame^] | 168 | size_t attest_cca_token_create(struct token_sign_cntxt *me, |
| 169 | void *attest_token_buf, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 170 | size_t attest_token_buf_size, |
| 171 | const void *realm_token_buf, |
| 172 | size_t realm_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, |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 197 | struct token_sign_cntxt *ctx, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 198 | void *realm_token_buf, |
| 199 | size_t realm_token_buf_size); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 200 | |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame^] | 201 | |
| 202 | |
| 203 | /* |
| 204 | * Initialize the token sign context and also the heap buffer used for the crypto. |
| 205 | * It is assumed that the heap alloc context has already been assigned to this |
| 206 | * CPU. If the token sign context has already been initialized, this API will |
| 207 | * not initialize again as an optimization. |
| 208 | * |
| 209 | * Arguments: |
| 210 | * token_ctx - Token sign context. |
| 211 | * heap_buf - Buffer to use as heap. |
| 212 | * heap_buf_len - Size of the buffer to use as heap. |
| 213 | * |
| 214 | * Returns 0 on success, negative error code on error. |
| 215 | */ |
| 216 | int attest_token_ctx_init(struct token_sign_cntxt *token_ctx, |
| 217 | unsigned char *heap_buf, |
| 218 | unsigned int heap_buf_len); |
| 219 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 220 | #endif /* ATTESTATION_TOKEN_H */ |