blob: 6d91a7923c67d1d27d5e2c476b3149c71e9f5cc7 [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 */
59 ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS
60};
61
Soby Mathewb4c6df42022-11-09 11:13:29 +000062/*
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
74struct attest_token_encode_ctx {
75 /* Private data structure */
Mate Toth-Palc69951d2023-03-17 17:30:50 +010076 QCBOREncodeContext cbor_enc_ctx;
77 uint32_t opt_flags;
78 int32_t key_select;
Mate Toth-Palfda673a2023-06-13 12:25:43 +020079 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-Palc69951d2023-03-17 17:30:50 +010082 struct t_cose_psa_crypto_context crypto_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +000083};
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 */
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +010092struct token_sign_cntxt {
Soby Mathewb4c6df42022-11-09 11:13:29 +000093 /*
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;
Soby Mathewb4c6df42022-11-09 11:13:29 +000099};
100
Mate Toth-Palc08e0112023-06-27 13:08:31 +0200101#else /* CBMC */
102
103#define ATTEST_TOKEN_BUFFER_SIZE GRANULE_SIZE
104
105enum attest_token_err_t {
106 /* Success */
107 ATTEST_TOKEN_ERR_SUCCESS = 0,
108 /*
109 * Signing is in progress, function should be called with the same
110 * parameters again.
111 */
112 ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS
113};
114
115struct attest_token_encode_ctx {
116 uint32_t unused;
117};
118
119struct token_sign_cntxt {
120 enum attest_token_gen_state_t state;
121};
122
123#define ATTEST_CHALLENGE_SIZE (1)
124
125#endif /* CBMC */
126
Soby Mathewb4c6df42022-11-09 11:13:29 +0000127/*
128 * Sign the realm token and complete the CBOR encoding.
129 * This function returns ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS
130 * if signing is not complete and this function needs to be
131 * invoked again. ATTEST_TOKEN_ERR_SUCCESS is returned if
132 * signing is complete and `completed_token` is valid.
133 * Else returns one of the attest_token_err_t errors on
134 * any other error.
135 *
Soby Mathewf3622132024-07-19 07:31:40 +0100136 * me Token Sign Context.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200137 * completed_token_len Length of the completed token.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000138 *
139 * This completes the token after the payload has been added. When
140 * this is called the signing algorithm is run and the final
141 * formatting of the token is completed.
142 */
143enum attest_token_err_t
Soby Mathewf3622132024-07-19 07:31:40 +0100144attest_realm_token_sign(struct token_sign_cntxt *me,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200145 size_t *completed_token_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000146
147/*
148 * Combine realm token and platform token to top-level cca token
149 *
Soby Mathewf3622132024-07-19 07:31:40 +0100150 * me Token Sign Context.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200151 * attest_token_buf Pointer to the buffer where the token will be
AlexeiFedorov47165422023-09-13 11:47:57 +0100152 * written.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200153 * attest_token_buf_size Size of the buffer where the token will be
AlexeiFedorov47165422023-09-13 11:47:57 +0100154 * written.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200155 * realm_token_buf Pointer to the realm token.
156 * realm_token_len Length of the realm token.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000157 *
158 * Return 0 in case of error, the length of the cca token otherwise.
159 */
Soby Mathewf3622132024-07-19 07:31:40 +0100160size_t attest_cca_token_create(struct token_sign_cntxt *me,
161 void *attest_token_buf,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200162 size_t attest_token_buf_size,
163 const void *realm_token_buf,
164 size_t realm_token_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000165
166/*
167 * Assemble the Realm token in the buffer provided in realm_token_buf,
168 * except the signature.
169 *
170 * Arguments:
171 * Algorithm - Algorithm used during measurement.
172 * Measurement - Array of buffers containing all the measurements.
173 * num_measurements - Number of measurements to add to the token.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200174 * rpv_buf - Pointer to the Realm Personalization value
175 * rpv_len - Length of the Realm Personalization value
Soby Mathewb4c6df42022-11-09 11:13:29 +0000176 * ctx - Token sign context, used for signing.
177 * realm_token_buf - Buffer where to assemble the attestation token.
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200178 * realm_token_buf_size - size of the buffer where to assemble the attestation
179 * token.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000180 *
181 * Returns ATTEST_TOKEN_ERR_SUCCESS (0) on success or a negative error code
182 * otherwise.
183 */
184int attest_realm_token_create(enum hash_algo algorithm,
185 unsigned char measurements[][MAX_MEASUREMENT_SIZE],
186 unsigned int num_measurements,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200187 const void *rpv_buf,
188 size_t rpv_len,
Mate Toth-Pal4feff402024-08-30 10:53:32 +0200189 const void *challenge_buf,
190 size_t challenge_len,
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100191 struct token_sign_cntxt *ctx,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200192 void *realm_token_buf,
193 size_t realm_token_buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000194
Soby Mathewf3622132024-07-19 07:31:40 +0100195
196
197/*
198 * Initialize the token sign context and also the heap buffer used for the crypto.
199 * It is assumed that the heap alloc context has already been assigned to this
200 * CPU. If the token sign context has already been initialized, this API will
201 * not initialize again as an optimization.
202 *
203 * Arguments:
204 * token_ctx - Token sign context.
205 * heap_buf - Buffer to use as heap.
206 * heap_buf_len - Size of the buffer to use as heap.
207 *
208 * Returns 0 on success, negative error code on error.
209 */
210int attest_token_ctx_init(struct token_sign_cntxt *token_ctx,
211 unsigned char *heap_buf,
212 unsigned int heap_buf_len);
213
Soby Mathewb4c6df42022-11-09 11:13:29 +0000214#endif /* ATTESTATION_TOKEN_H */