blob: 8ce74627cf8269945dd3f824ce7a6cf80b4ca4ed [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
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-Palc08e0112023-06-27 13:08:31 +020016#ifndef CBMC
Soby Mathewb4c6df42022-11-09 11:13:29 +000017#include <qcbor/qcbor.h>
Mate Toth-Palc08e0112023-06-27 13:08:31 +020018#endif /* CBMC */
19#include <stddef.h>
20#include <stdint.h>
21#ifndef CBMC
Soby Mathewb4c6df42022-11-09 11:13:29 +000022#include <t_cose/q_useful_buf.h>
Mate Toth-Palfda673a2023-06-13 12:25:43 +020023#include <t_cose/t_cose_sign_sign.h>
24#include <t_cose/t_cose_signature_sign_restart.h>
Mate Toth-Palc69951d2023-03-17 17:30:50 +010025#include <t_cose_psa_crypto.h>
Mate Toth-Palc08e0112023-06-27 13:08:31 +020026#endif /* CBMC */
27
Soby Mathewf3622132024-07-19 07:31:40 +010028/* The state of the CCA token generation */
Mate Toth-Palc08e0112023-06-27 13:08:31 +020029enum attest_token_gen_state_t {
Soby Mathewf3622132024-07-19 07:31:40 +010030 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-Palc08e0112023-06-27 13:08:31 +020034};
35
36#ifndef CBMC
Soby Mathewb4c6df42022-11-09 11:13:29 +000037
38#define ATTEST_TOKEN_BUFFER_SIZE GRANULE_SIZE
39
40enum attest_token_err_t {
41 /* Success */
42 ATTEST_TOKEN_ERR_SUCCESS = 0,
Soby Mathewf3622132024-07-19 07:31:40 +010043 /* The Attest token context state is incorrect */
44 ATTEST_TOKEN_ERR_INVALID_STATE,
Soby Mathewb4c6df42022-11-09 11:13:29 +000045 /* 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-Palc08e0112023-06-27 13:08:31 +020055 /*
56 * Signing is in progress, function should be called with the same
Soby Mathewb4c6df42022-11-09 11:13:29 +000057 * parameters again.
58 */
Juan Pablo Conde79ca2562024-08-12 17:05:41 -050059 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 Mathewb4c6df42022-11-09 11:13:29 +000064};
65
Soby Mathewb4c6df42022-11-09 11:13:29 +000066/*
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
78struct attest_token_encode_ctx {
79 /* Private data structure */
Mate Toth-Palc69951d2023-03-17 17:30:50 +010080 QCBOREncodeContext cbor_enc_ctx;
81 uint32_t opt_flags;
82 int32_t key_select;
Mate Toth-Palfda673a2023-06-13 12:25:43 +020083 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-Palc69951d2023-03-17 17:30:50 +010086 struct t_cose_psa_crypto_context crypto_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +000087};
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 */
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010096struct token_sign_cntxt {
Soby Mathewb4c6df42022-11-09 11:13:29 +000097 /*
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 Mathewb4c6df42022-11-09 11:13:29 +0000103};
104
Mate Toth-Palc08e0112023-06-27 13:08:31 +0200105#else /* CBMC */
106
107#define ATTEST_TOKEN_BUFFER_SIZE GRANULE_SIZE
108
109enum 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
119struct attest_token_encode_ctx {
120 uint32_t unused;
121};
122
123struct 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 Mathewb4c6df42022-11-09 11:13:29 +0000131/*
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 Mathewf3622132024-07-19 07:31:40 +0100140 * me Token Sign Context.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200141 * completed_token_len Length of the completed token.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000142 *
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 */
147enum attest_token_err_t
Soby Mathewf3622132024-07-19 07:31:40 +0100148attest_realm_token_sign(struct token_sign_cntxt *me,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200149 size_t *completed_token_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000150
151/*
152 * Combine realm token and platform token to top-level cca token
153 *
Soby Mathewf3622132024-07-19 07:31:40 +0100154 * me Token Sign Context.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200155 * attest_token_buf Pointer to the buffer where the token will be
AlexeiFedorov47165422023-09-13 11:47:57 +0100156 * written.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200157 * attest_token_buf_size Size of the buffer where the token will be
AlexeiFedorov47165422023-09-13 11:47:57 +0100158 * written.
Juan Pablo Conde79ca2562024-08-12 17:05:41 -0500159 * 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 Mathewb4c6df42022-11-09 11:13:29 +0000162 *
Juan Pablo Conde79ca2562024-08-12 17:05:41 -0500163 * Returns ATTEST_TOKEN_ERR_SUCCESS (0) if CCA top-level token is
164 * created. Otherwise, returns the proper error value.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000165 */
Juan Pablo Conde79ca2562024-08-12 17:05:41 -0500166enum attest_token_err_t
167attest_cca_token_create(struct token_sign_cntxt *me,
Soby Mathewf3622132024-07-19 07:31:40 +0100168 void *attest_token_buf,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200169 size_t attest_token_buf_size,
170 const void *realm_token_buf,
Juan Pablo Conde79ca2562024-08-12 17:05:41 -0500171 size_t realm_token_len,
172 size_t *cca_token_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000173
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-Pal071aa562023-07-04 09:09:26 +0200182 * rpv_buf - Pointer to the Realm Personalization value
183 * rpv_len - Length of the Realm Personalization value
Soby Mathewb4c6df42022-11-09 11:13:29 +0000184 * ctx - Token sign context, used for signing.
185 * realm_token_buf - Buffer where to assemble the attestation token.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200186 * realm_token_buf_size - size of the buffer where to assemble the attestation
187 * token.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000188 *
189 * Returns ATTEST_TOKEN_ERR_SUCCESS (0) on success or a negative error code
190 * otherwise.
191 */
192int attest_realm_token_create(enum hash_algo algorithm,
193 unsigned char measurements[][MAX_MEASUREMENT_SIZE],
194 unsigned int num_measurements,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200195 const void *rpv_buf,
196 size_t rpv_len,
Mate Toth-Pal4feff402024-08-30 10:53:32 +0200197 const void *challenge_buf,
198 size_t challenge_len,
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100199 struct token_sign_cntxt *ctx,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200200 void *realm_token_buf,
201 size_t realm_token_buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000202
Soby Mathewf3622132024-07-19 07:31:40 +0100203
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 *
Soby Mathew376ffa42024-10-04 14:01:59 +0100216 * Return code:
217 * ATTEST_TOKEN_ERR_SUCCESS (0) - Success.
218 * ATTEST_TOKEN_ERR_INVALID_STATE - Failed possibly due to invalid state.
Soby Mathewf3622132024-07-19 07:31:40 +0100219 */
220int attest_token_ctx_init(struct token_sign_cntxt *token_ctx,
221 unsigned char *heap_buf,
222 unsigned int heap_buf_len);
223
Soby Mathewb4c6df42022-11-09 11:13:29 +0000224#endif /* ATTESTATION_TOKEN_H */