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