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