Branch data Line data Source code
1 : : /**
2 : : * Copyright Notice:
3 : : * Copyright 2021-2026 DMTF. All rights reserved.
4 : : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : : **/
6 : :
7 : : #ifndef SPDM_COMMON_LIB_INTERNAL_H
8 : : #define SPDM_COMMON_LIB_INTERNAL_H
9 : :
10 : : #include "library/spdm_common_lib.h"
11 : : #include "library/spdm_secured_message_lib.h"
12 : : #include "library/spdm_return_status.h"
13 : : #include "library/spdm_crypt_lib.h"
14 : : #include "hal/library/debuglib.h"
15 : : #include "hal/library/memlib.h"
16 : : #include "hal/library/requester/reqasymsignlib.h"
17 : : #include "hal/library/requester/psklib.h"
18 : : #include "hal/library/responder/asymsignlib.h"
19 : : #include "hal/library/responder/csrlib.h"
20 : : #include "hal/library/responder/measlib.h"
21 : : #include "hal/library/responder/keyexlib.h"
22 : : #include "hal/library/responder/key_pair_info.h"
23 : : #include "hal/library/responder/psklib.h"
24 : : #include "hal/library/responder/setcertlib.h"
25 : : #include "hal/library/endpointinfolib.h"
26 : : #include "hal/library/eventlib.h"
27 : : #include "hal/library/cryptlib.h"
28 : :
29 : : /* Redeclare libspdm_debug_print so that the compiler checks the core library's format
30 : : * strings against their arguments. The public declaration is left alone so that an
31 : : * Integrator's own debug implementation is not constrained. */
32 : : #if LIBSPDM_DEBUG_PRINT_ENABLE && (defined(__GNUC__) || defined(__clang__))
33 : : #if defined(__MINGW32__) && !defined(__clang__)
34 : : /* MinGW's GCC checks the printf archetype against msvcrt, which lacks C99 format specifiers. */
35 : : extern void libspdm_debug_print(size_t error_level, const char *format, ...)
36 : : __attribute__((format(gnu_printf, 2, 3)));
37 : : #else
38 : : extern void libspdm_debug_print(size_t error_level, const char *format, ...)
39 : : __attribute__((format(printf, 2, 3)));
40 : : #endif /* defined(__MINGW32__) && !defined(__clang__) */
41 : : #endif /* LIBSPDM_DEBUG_PRINT_ENABLE && (defined(__GNUC__) || defined(__clang__)) */
42 : :
43 : : #define INVALID_SESSION_ID LIBSPDM_INVALID_SESSION_ID
44 : : /* The SPDM specification does not limit the values of CTExponent and RDTExponent.
45 : : * libspdm artificially limits their values to 31, which corresponds to approximately 35 minutes
46 : : * for CT and RDT. If an endpoint takes longer than 35 minutes to generate an SPDM message then
47 : : * libspdm assumes the Integrator would not want to interact with such an endpoint. A maximum value
48 : : * of 31 also means that, when calculating CT and RDT, a left-shift will not result in C undefined
49 : : * behavior.
50 : : */
51 : : #define LIBSPDM_MAX_CT_EXPONENT 31
52 : : #define LIBSPDM_MAX_RDT_EXPONENT 31
53 : :
54 : : #define LIBSPDM_MAX_SPDM_SESSION_SEQUENCE_NUMBER 0xFFFFFFFFFFFFFFFFull
55 : :
56 : : typedef struct {
57 : : uint8_t spdm_version_count;
58 : : spdm_version_number_t spdm_version[SPDM_MAX_VERSION_COUNT];
59 : : } libspdm_device_version_t;
60 : :
61 : : typedef struct {
62 : : uint8_t secured_message_version_count;
63 : : spdm_version_number_t secured_message_version[SECURED_SPDM_MAX_VERSION_COUNT];
64 : : } libspdm_secured_message_version_t;
65 : :
66 : : typedef struct {
67 : : uint8_t ct_exponent;
68 : : uint64_t rtt;
69 : : uint32_t st1;
70 : : uint32_t flags;
71 : : uint16_t ext_flags;
72 : : uint32_t data_transfer_size;
73 : : uint32_t sender_data_transfer_size;
74 : : uint32_t max_spdm_msg_size;
75 : : uint32_t transport_header_size;
76 : : uint32_t transport_tail_size;
77 : : } libspdm_device_capability_t;
78 : :
79 : : typedef struct {
80 : : uint8_t measurement_spec;
81 : : uint8_t other_params_support;
82 : : uint8_t mel_spec;
83 : : uint32_t measurement_hash_algo;
84 : : uint32_t base_asym_algo;
85 : : uint32_t base_hash_algo;
86 : : uint16_t dhe_named_group;
87 : : uint16_t aead_cipher_suite;
88 : : uint16_t req_base_asym_alg;
89 : : uint16_t key_schedule;
90 : : uint32_t pqc_asym_algo;
91 : : uint32_t req_pqc_asym_alg;
92 : : uint32_t kem_alg;
93 : : bool pqc_first;
94 : : } libspdm_device_algorithm_t;
95 : :
96 : : typedef struct {
97 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
98 : : uint8_t buffer[LIBSPDM_MAX_CERT_CHAIN_SIZE];
99 : : size_t buffer_size;
100 : : #else
101 : : uint8_t buffer_hash[LIBSPDM_MAX_HASH_SIZE];
102 : : uint32_t buffer_hash_size;
103 : : /* leaf cert public key of the peer */
104 : : void *leaf_cert_public_key;
105 : : #endif
106 : : } libspdm_peer_used_cert_chain_t;
107 : :
108 : : typedef struct {
109 : : /* Local device info */
110 : : libspdm_device_version_t version;
111 : : libspdm_device_capability_t capability;
112 : : libspdm_device_algorithm_t algorithm;
113 : : libspdm_secured_message_version_t secured_message_version;
114 : :
115 : : /* Local certificates */
116 : : const void *local_cert_chain_provision[SPDM_MAX_SLOT_COUNT];
117 : : size_t local_cert_chain_provision_size[SPDM_MAX_SLOT_COUNT];
118 : : uint8_t local_supported_slot_mask;
119 : : uint8_t cert_slot_reset_mask;
120 : : spdm_key_pair_id_t local_key_pair_id[SPDM_MAX_SLOT_COUNT];
121 : : spdm_certificate_info_t local_cert_info[SPDM_MAX_SLOT_COUNT];
122 : : spdm_key_usage_bit_mask_t local_key_usage_bit_mask[SPDM_MAX_SLOT_COUNT];
123 : : /* Local raw public key (slot_id - 0xFF) */
124 : : const void *local_public_key_provision;
125 : : size_t local_public_key_provision_size;
126 : :
127 : : #if LIBSPDM_CERT_PARSE_SUPPORT
128 : : /* Peer Root Certificate */
129 : : const void *peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT];
130 : : size_t peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT];
131 : : #endif /* LIBSPDM_CERT_PARSE_SUPPORT */
132 : : /* Peer raw public key (slot_id - 0xFF) */
133 : : const void *peer_public_key_provision;
134 : : size_t peer_public_key_provision_size;
135 : :
136 : : /* Peer Cert verify*/
137 : : libspdm_verify_spdm_cert_chain_func verify_peer_spdm_cert_chain;
138 : :
139 : : /* Responder policy*/
140 : : bool basic_mut_auth_requested;
141 : : uint8_t heartbeat_period;
142 : :
143 : : /*The device role*/
144 : : bool is_requester;
145 : : } libspdm_local_context_t;
146 : :
147 : : typedef struct {
148 : : /* Connection State */
149 : : libspdm_connection_state_t connection_state;
150 : :
151 : : /* Peer device info (negotiated) */
152 : : spdm_version_number_t version;
153 : : libspdm_device_capability_t capability;
154 : : libspdm_device_algorithm_t algorithm;
155 : :
156 : : /* Peer digests buffer */
157 : : uint8_t peer_provisioned_slot_mask;
158 : : uint8_t peer_supported_slot_mask;
159 : :
160 : : spdm_key_pair_id_t peer_key_pair_id[SPDM_MAX_SLOT_COUNT];
161 : : spdm_certificate_info_t peer_cert_info[SPDM_MAX_SLOT_COUNT];
162 : : spdm_key_usage_bit_mask_t peer_key_usage_bit_mask[SPDM_MAX_SLOT_COUNT];
163 : :
164 : : /* Peer CertificateChain */
165 : : libspdm_peer_used_cert_chain_t peer_used_cert_chain[SPDM_MAX_SLOT_COUNT];
166 : :
167 : : /* Specifies whether the cached negotiated state should be invalidated. (responder only)
168 : : * This is a "sticky" bit wherein if it is set to 1 then it cannot be set to 0. */
169 : : uint8_t end_session_attributes;
170 : :
171 : : /* multi-key negotiated result */
172 : : bool multi_key_conn_req;
173 : : bool multi_key_conn_rsp;
174 : :
175 : : /* Sticky: set once a non-GET_DIGESTS request is processed after NEGOTIATED. A DIGESTS is added
176 : : * to message_d only when this is false (DIGESTS immediately follows the VCA). */
177 : : bool digest_transcript_window_closed;
178 : : } libspdm_connection_info_t;
179 : :
180 : : typedef struct {
181 : : size_t max_buffer_size;
182 : : size_t buffer_size;
183 : : /*uint8_t buffer[max_buffer_size];*/
184 : : } libspdm_managed_buffer_t;
185 : :
186 : : typedef struct {
187 : : size_t max_buffer_size;
188 : : size_t buffer_size;
189 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_VCA_BUFFER_SIZE];
190 : : } libspdm_vca_managed_buffer_t;
191 : :
192 : : /*
193 : : * +--------------------------+------------------------------------------+---------+
194 : : * | DIGESTS 1.4 | 4 + (H [+ 4]) * SlotNum = [36, 548] | [1, 18] |
195 : : * +--------------------------+------------------------------------------+---------+
196 : : * It is for multi-key.
197 : : */
198 : : #define LIBSPDM_MAX_MESSAGE_D_BUFFER_SIZE (4 + \
199 : : (LIBSPDM_MAX_HASH_SIZE + 4) * SPDM_MAX_SLOT_COUNT)
200 : :
201 : : typedef struct {
202 : : size_t max_buffer_size;
203 : : size_t buffer_size;
204 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_D_BUFFER_SIZE];
205 : : } libspdm_message_d_managed_buffer_t;
206 : :
207 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
208 : :
209 : : /*
210 : : * +--------------------------+------------------------------------------+---------+
211 : : * | GET_DIGESTS 1.4 | 4 | 1 |
212 : : * | DIGESTS 1.4 | 4 + (H [+ 4]) * SlotNum = [36, 548] | [1, 18] |
213 : : * +--------------------------+------------------------------------------+---------+
214 : : * | GET_CERTIFICATE 1.4 | 16 | 1 |
215 : : * | CERTIFICATE 1.4 | 16 + PortionLen | [1, ] |
216 : : * +--------------------------+------------------------------------------+---------+
217 : : */
218 : : #define LIBSPDM_MAX_MESSAGE_B_BUFFER_SIZE (40 + \
219 : : (LIBSPDM_MAX_HASH_SIZE + 4) * SPDM_MAX_SLOT_COUNT + \
220 : : LIBSPDM_MAX_CERT_CHAIN_SIZE)
221 : :
222 : : /*
223 : : * +--------------------------+------------------------------------------+---------+
224 : : * | CHALLENGE 1.4 | 44 | 1 |
225 : : * | CHALLENGE_AUTH 1.4 | 46 + H * 2 + S [+ O] = [166, 678] | [6, 23] |
226 : : * +--------------------------+------------------------------------------+---------+
227 : : */
228 : : #define LIBSPDM_MAX_MESSAGE_C_BUFFER_SIZE (90 + \
229 : : LIBSPDM_MAX_HASH_SIZE * 2 + \
230 : : LIBSPDM_RSP_SIGNATURE_DATA_MAX_SIZE + \
231 : : SPDM_MAX_OPAQUE_DATA_SIZE)
232 : :
233 : : /*
234 : : * +--------------------------+------------------------------------------+---------+
235 : : * | GET_MEASUREMENTS 1.4 | 13 + Nonce (0 or 32) | 1 |
236 : : * | MEASUREMENTS 1.4 | 50 + MeasRecLen (+ S) [+ O] = [106, 554] | [4, 19] |
237 : : * +--------------------------+------------------------------------------+---------+
238 : : */
239 : : #define LIBSPDM_MAX_MESSAGE_M_BUFFER_SIZE (63 + SPDM_NONCE_SIZE + \
240 : : LIBSPDM_MAX_MEASUREMENT_RECORD_SIZE + \
241 : : LIBSPDM_RSP_SIGNATURE_DATA_MAX_SIZE + \
242 : : SPDM_MAX_OPAQUE_DATA_SIZE)
243 : :
244 : : /*
245 : : * +--------------------------+------------------------------------------+---------+
246 : : * | KEY_EXCHANGE 1.4 | 42 + D [+ O] = [106, 554] | [4, 19] |
247 : : * | KEY_EXCHANGE_RSP 1.4 | 42 + D + H + S (+ H) [+ O] = [234, 1194] | [8, 40] |
248 : : * +--------------------------+------------------------------------------+---------+
249 : : * | PSK_EXCHANGE 1.4 | 12 [+ PSKHint] + R [+ O] = 44 | 2 |
250 : : * | PSK_EXCHANGE_RSP 1.4 | 12 + R + H (+ H) [+ O] = [108, 172] | [4, 6] |
251 : : * +--------------------------+------------------------------------------+---------+
252 : : */
253 : : #define LIBSPDM_MAX_MESSAGE_K_BUFFER_SIZE (84 + LIBSPDM_REQ_EXCHANGE_DATA_MAX_SIZE + \
254 : : LIBSPDM_RSP_EXCHANGE_DATA_MAX_SIZE + \
255 : : LIBSPDM_MAX_HASH_SIZE * 2 + \
256 : : LIBSPDM_RSP_SIGNATURE_DATA_MAX_SIZE + \
257 : : SPDM_MAX_OPAQUE_DATA_SIZE * 2)
258 : :
259 : : /*
260 : : * +--------------------------+------------------------------------------+---------+
261 : : * | FINISH 1.4 | 6 (+ S) + H [+ O] = [100, 580] | [4, 20] |
262 : : * | FINISH_RSP 1.4 | 6 (+ H) [+ O] = [36, 69] | [1, 3] |
263 : : * +--------------------------+------------------------------------------+---------+
264 : : * | PSK_FINISH 1.4 | 6 + H [+ O] = [36, 68] | [1, 3] |
265 : : * | PSK_FINISH_RSP 1.4 | 6 [+ O] | 1 |
266 : : * +--------------------------+------------------------------------------+---------+
267 : : */
268 : : #define LIBSPDM_MAX_MESSAGE_F_BUFFER_SIZE (12 + LIBSPDM_MAX_HASH_SIZE * 2 + \
269 : : LIBSPDM_REQ_SIGNATURE_DATA_MAX_SIZE + \
270 : : SPDM_MAX_OPAQUE_DATA_SIZE * 2)
271 : :
272 : : /*
273 : : * +--------------------------+------------------------------------------+---------+
274 : : * | GET_EP_INFO 1.4 | 8 + Nonce (0 or 32) = [8, 40] | 1 |
275 : : * | EP_INFO 1.4 | 12 + Nonce + EPInfoLen (+ S) = [12, 1024]| [1, 25] |
276 : : * +--------------------------+------------------------------------------+---------+
277 : : */
278 : : #define LIBSPDM_MAX_MESSAGE_E_BUFFER_SIZE (20 + SPDM_NONCE_SIZE * 2 + \
279 : : LIBSPDM_MAX_ENDPOINT_INFO_LENGTH + \
280 : : LIBSPDM_RSP_SIGNATURE_DATA_MAX_SIZE)
281 : :
282 : : #define LIBSPDM_MAX_MESSAGE_L1L2_BUFFER_SIZE \
283 : : (LIBSPDM_MAX_MESSAGE_VCA_BUFFER_SIZE + LIBSPDM_MAX_MESSAGE_M_BUFFER_SIZE)
284 : :
285 : : #define LIBSPDM_MAX_MESSAGE_M1M2_BUFFER_SIZE \
286 : : (LIBSPDM_MAX_MESSAGE_VCA_BUFFER_SIZE + \
287 : : LIBSPDM_MAX_MESSAGE_B_BUFFER_SIZE + LIBSPDM_MAX_MESSAGE_C_BUFFER_SIZE)
288 : :
289 : : #define LIBSPDM_MAX_MESSAGE_TH_BUFFER_SIZE \
290 : : (LIBSPDM_MAX_MESSAGE_VCA_BUFFER_SIZE + \
291 : : LIBSPDM_MAX_MESSAGE_D_BUFFER_SIZE + \
292 : : LIBSPDM_MAX_HASH_SIZE + LIBSPDM_MAX_MESSAGE_K_BUFFER_SIZE + \
293 : : LIBSPDM_MAX_MESSAGE_D_BUFFER_SIZE + \
294 : : LIBSPDM_MAX_HASH_SIZE + LIBSPDM_MAX_MESSAGE_F_BUFFER_SIZE)
295 : :
296 : : #define LIBSPDM_MAX_MESSAGE_IL1IL2_BUFFER_SIZE \
297 : : (LIBSPDM_MAX_MESSAGE_VCA_BUFFER_SIZE + LIBSPDM_MAX_MESSAGE_E_BUFFER_SIZE)
298 : :
299 : : typedef struct {
300 : : size_t max_buffer_size;
301 : : size_t buffer_size;
302 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_B_BUFFER_SIZE];
303 : : } libspdm_message_b_managed_buffer_t;
304 : :
305 : : typedef struct {
306 : : size_t max_buffer_size;
307 : : size_t buffer_size;
308 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_C_BUFFER_SIZE];
309 : : } libspdm_message_c_managed_buffer_t;
310 : :
311 : : typedef struct {
312 : : size_t max_buffer_size;
313 : : size_t buffer_size;
314 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_M_BUFFER_SIZE];
315 : : } libspdm_message_m_managed_buffer_t;
316 : :
317 : : typedef struct {
318 : : size_t max_buffer_size;
319 : : size_t buffer_size;
320 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_K_BUFFER_SIZE];
321 : : } libspdm_message_k_managed_buffer_t;
322 : :
323 : : typedef struct {
324 : : size_t max_buffer_size;
325 : : size_t buffer_size;
326 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_F_BUFFER_SIZE];
327 : : } libspdm_message_f_managed_buffer_t;
328 : :
329 : : typedef struct {
330 : : size_t max_buffer_size;
331 : : size_t buffer_size;
332 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_E_BUFFER_SIZE];
333 : : } libspdm_message_e_managed_buffer_t;
334 : :
335 : : typedef struct {
336 : : size_t max_buffer_size;
337 : : size_t buffer_size;
338 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_L1L2_BUFFER_SIZE];
339 : : } libspdm_l1l2_managed_buffer_t;
340 : :
341 : : typedef struct {
342 : : size_t max_buffer_size;
343 : : size_t buffer_size;
344 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_M1M2_BUFFER_SIZE];
345 : : } libspdm_m1m2_managed_buffer_t;
346 : :
347 : : typedef struct {
348 : : size_t max_buffer_size;
349 : : size_t buffer_size;
350 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_IL1IL2_BUFFER_SIZE];
351 : : } libspdm_il1il2_managed_buffer_t;
352 : :
353 : : typedef struct {
354 : : size_t max_buffer_size;
355 : : size_t buffer_size;
356 : : uint8_t buffer[LIBSPDM_MAX_MESSAGE_TH_BUFFER_SIZE];
357 : : } libspdm_th_managed_buffer_t;
358 : :
359 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
360 : :
361 : : /* signature = Sign(SK, hash(M1))
362 : : * Verify(PK, hash(M2), signature)*/
363 : :
364 : : /* M1/M2 = Concatenate (A, B, C)
365 : : * A = Concatenate (GET_VERSION, VERSION, GET_CAPABILITIES, CAPABILITIES, NEGOTIATE_ALGORITHMS, ALGORITHMS)
366 : : * B = Concatenate (GET_DIGESTS, DIGESTS, GET_CERTIFICATE, CERTIFICATE)
367 : : * C = Concatenate (CHALLENGE, CHALLENGE_AUTH\signature)*/
368 : :
369 : : /* Mut M1/M2 = Concatenate (MutB, MutC)
370 : : * MutB = Concatenate (GET_DIGESTS, DIGESTS, GET_CERTIFICATE, CERTIFICATE)
371 : : * MutC = Concatenate (CHALLENGE, CHALLENGE_AUTH\signature)*/
372 : :
373 : : /* signature = Sign(SK, hash(L1))
374 : : * Verify(PK, hash(L2), signature)*/
375 : :
376 : : /* L1/L2 = Concatenate (M)
377 : : * M = Concatenate (GET_MEASUREMENTS, MEASUREMENTS\signature)*/
378 : :
379 : : /* IL1/IL2 = Concatenate (A, E)
380 : : * E = Concatenate (GET_ENDPOINT_INFO, ENDPOINT_INFO\signature)*/
381 : :
382 : : /* Encap IL1/IL2 = Concatenate (A, Encap E)
383 : : * Encap E = Concatenate (GET_ENDPOINT_INFO, ENDPOINT_INFO\signature)*/
384 : :
385 : : typedef struct {
386 : : /* the message_a must be plain text because libspdm does not know the algorithm yet.*/
387 : : libspdm_vca_managed_buffer_t message_a;
388 : : /* Set when message_a is appended to and cleared when M1/M2 is set to null. For SPDM 1.0 and
389 : : * 1.1, VCA is part of M1/M2 only while this is set. */
390 : : bool message_a_in_m1m2;
391 : : libspdm_message_d_managed_buffer_t message_d;
392 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
393 : : libspdm_message_b_managed_buffer_t message_b;
394 : : libspdm_message_c_managed_buffer_t message_c;
395 : : libspdm_message_b_managed_buffer_t message_mut_b;
396 : : libspdm_message_c_managed_buffer_t message_mut_c;
397 : : libspdm_message_m_managed_buffer_t message_m;
398 : : libspdm_message_e_managed_buffer_t message_e;
399 : : libspdm_message_e_managed_buffer_t message_encap_e;
400 : : #else
401 : : void *digest_context_m1m2;
402 : : void *digest_context_mut_m1m2;
403 : : void *digest_context_l1l2;
404 : : void *digest_context_il1il2;
405 : : void *digest_context_encap_il1il2;
406 : : #endif
407 : : } libspdm_transcript_t;
408 : :
409 : : /* TH for KEY_EXCHANGE response signature: Concatenate (A, D, Ct, K)
410 : : * D = DIGESTS, if MULTI_KEY_CONN_RSP
411 : : * Ct = certificate chain
412 : : * K = Concatenate (KEY_EXCHANGE request, KEY_EXCHANGE response\signature+verify_data)*/
413 : :
414 : : /* TH for KEY_EXCHANGE response HMAC: Concatenate (A, D, Ct, K)
415 : : * D = DIGESTS, if MULTI_KEY_CONN_RSP
416 : : * Ct = certificate chain
417 : : * K = Concatenate (KEY_EXCHANGE request, KEY_EXCHANGE response\verify_data)*/
418 : :
419 : : /* TH for FINISH request signature: Concatenate (A, D, Ct, K, EncapD, CM, F)
420 : : * D = DIGESTS, if MULTI_KEY_CONN_RSP
421 : : * Ct = certificate chain
422 : : * K = Concatenate (KEY_EXCHANGE request, KEY_EXCHANGE response)
423 : : * EncapD = Encap DIGESTS, if MULTI_KEY_CONN_REQ
424 : : * CM = mutual certificate chain
425 : : * F = Concatenate (FINISH request\signature+verify_data)*/
426 : :
427 : : /* TH for FINISH response HMAC: Concatenate (A, D, Ct, K, EncapD, CM, F)
428 : : * D = DIGESTS, if MULTI_KEY_CONN_RSP
429 : : * Ct = certificate chain
430 : : * K = Concatenate (KEY_EXCHANGE request, KEY_EXCHANGE response)
431 : : * EncapD = Encap DIGESTS, if MULTI_KEY_CONN_REQ
432 : : * CM = mutual certificate chain, if MutAuth
433 : : * F = Concatenate (FINISH request\verify_data)*/
434 : :
435 : : /* th1: Concatenate (A, D, Ct, K)
436 : : * D = DIGESTS, if MULTI_KEY_CONN_RSP
437 : : * Ct = certificate chain
438 : : * K = Concatenate (KEY_EXCHANGE request, KEY_EXCHANGE response)*/
439 : :
440 : : /* th2: Concatenate (A, D, Ct, K, EncapD, CM, F)
441 : : * D = DIGESTS, if MULTI_KEY_CONN_RSP
442 : : * Ct = certificate chain
443 : : * K = Concatenate (KEY_EXCHANGE request, KEY_EXCHANGE response)
444 : : * EncapD = Encap DIGESTS, if MULTI_KEY_CONN_REQ
445 : : * CM = mutual certificate chain, if MutAuth
446 : : * F = Concatenate (FINISH request, FINISH response)*/
447 : :
448 : : /* TH for PSK_EXCHANGE response HMAC: Concatenate (A, K)
449 : : * K = Concatenate (PSK_EXCHANGE request, PSK_EXCHANGE response\verify_data)*/
450 : :
451 : : /* TH for PSK_FINISH response HMAC: Concatenate (A, K, F)
452 : : * K = Concatenate (PSK_EXCHANGE request, PSK_EXCHANGE response)
453 : : * F = Concatenate (PSK_FINISH request\verify_data)*/
454 : :
455 : : /* TH1_PSK1: Concatenate (A, K)
456 : : * K = Concatenate (PSK_EXCHANGE request, PSK_EXCHANGE response\verify_data)*/
457 : :
458 : : /* TH1_PSK2: Concatenate (A, K, F)
459 : : * K = Concatenate (PSK_EXCHANGE request, PSK_EXCHANGE response)
460 : : * F = Concatenate (PSK_FINISH request\verify_data)*/
461 : :
462 : : /* TH2_PSK: Concatenate (A, K, F)
463 : : * K = Concatenate (PSK_EXCHANGE request, PSK_EXCHANGE response)
464 : : * F = Concatenate (PSK_FINISH request, PSK_FINISH response)*/
465 : :
466 : : typedef struct {
467 : : libspdm_message_d_managed_buffer_t message_encap_d;
468 : : /* Sticky: set once a non-GET_DIGESTS encapsulated request is processed first. An encapsulated
469 : : * DIGESTS is added to message_encap_d only when this is false (it is the first encap response). */
470 : : bool encap_digest_window_closed;
471 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
472 : : libspdm_message_k_managed_buffer_t message_k;
473 : : libspdm_message_f_managed_buffer_t message_f;
474 : : libspdm_message_m_managed_buffer_t message_m;
475 : : libspdm_message_e_managed_buffer_t message_e;
476 : : libspdm_message_e_managed_buffer_t message_encap_e;
477 : : #else
478 : : bool message_f_initialized;
479 : : void *digest_context_th;
480 : : void *digest_context_l1l2;
481 : : void *digest_context_il1il2;
482 : : void *digest_context_encap_il1il2;
483 : : /* this is a backup for message F reset.*/
484 : : void *digest_context_th_backup;
485 : : #endif
486 : : } libspdm_session_transcript_t;
487 : :
488 : : typedef struct {
489 : : uint8_t request_id;
490 : : uint8_t req_slot_id;
491 : : uint8_t mut_auth_req_slot_id;
492 : : spdm_message_header_t last_encap_request_header;
493 : : /* Whether this flow has issued an encapsulated request, whose header is then in
494 : : * last_encap_request_header. The GET_DIGESTS that KEY_EXCHANGE_RSP implies counts as
495 : : * issued once the Requester answers it. */
496 : : bool has_last_encap_request;
497 : : uint8_t req_context[SPDM_REQ_CONTEXT_SIZE];
498 : : uint8_t req_attributes;
499 : : uint32_t cert_chain_total_len;
500 : : /* The Integrator's buffer for the payload that the outstanding encapsulated request
501 : : * retrieves, such as a certificate chain or endpoint information. Only one encapsulated
502 : : * request is outstanding at a time, so a single buffer serves every payload type. */
503 : : void *payload_buffer;
504 : : size_t payload_buffer_size;
505 : : size_t payload_buffer_max_size;
506 : : bool use_large_cert_chain;
507 : : /* The certificate model of the certificate chain that GET_CERTIFICATE is retrieving. */
508 : : uint8_t cert_model;
509 : : /* The slots whose certificate chains this flow has recorded, which are discarded if mutual
510 : : * authentication fails. */
511 : : uint8_t recorded_cert_chain_slot_mask;
512 : : libspdm_encap_flow_type_t flow_type;
513 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
514 : : bool response_not_ready;
515 : : libspdm_encap_flow_type_t response_not_ready_flow_type;
516 : : spdm_error_data_response_not_ready_t response_not_ready_data;
517 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
518 : : } libspdm_encap_context_t;
519 : :
520 : : typedef struct {
521 : : uint32_t session_id;
522 : : bool use_psk;
523 : : uint8_t mut_auth_requested;
524 : : uint8_t end_session_attributes;
525 : : uint8_t session_policy;
526 : : uint8_t heartbeat_period;
527 : : libspdm_session_transcript_t session_transcript;
528 : : /* Register for the last KEY_UPDATE token and operation (responder only)*/
529 : : spdm_key_update_request_t last_key_update_request;
530 : : void *secured_message_context;
531 : : /* Only present in session info as it is currently only used within a secure session. */
532 : : uint8_t local_used_cert_chain_slot_id;
533 : : uint8_t peer_used_cert_chain_slot_id;
534 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
535 : : libspdm_encap_context_t encap_context;
536 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
537 : : } libspdm_session_info_t;
538 : :
539 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
540 : : typedef struct {
541 : : bool chunk_in_use;
542 : : uint8_t chunk_handle;
543 : : /* The session that started the transfer. Only that session can continue it. */
544 : : bool session_id_valid;
545 : : uint32_t session_id;
546 : : uint32_t chunk_seq_no;
547 : : size_t chunk_bytes_transferred;
548 : :
549 : : void* large_message;
550 : : size_t large_message_size;
551 : : size_t large_message_capacity;
552 : : } libspdm_chunk_info_t;
553 : :
554 : : typedef struct {
555 : : libspdm_chunk_info_t send;
556 : : libspdm_chunk_info_t get;
557 : : } libspdm_chunk_context_t;
558 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
559 : :
560 : : #if LIBSPDM_ENABLE_MSG_LOG
561 : : typedef struct {
562 : : void *buffer;
563 : : size_t max_buffer_size;
564 : : uint32_t mode;
565 : : size_t buffer_size;
566 : : uint32_t status;
567 : : } libspdm_msg_log_t;
568 : : #endif /* LIBSPDM_ENABLE_MSG_LOG */
569 : :
570 : : #if LIBSPDM_FIPS_MODE
571 : : typedef struct {
572 : : /**
573 : : * Tested algo flag: 0 represents that the algo is not tested.
574 : : * See LIBSPDM_FIPS_SELF_TEST_xxx;
575 : : **/
576 : : uint32_t tested_algo;
577 : : /**
578 : : * Flag for the result of run algo self_test, 0 represents the result is failed.
579 : : * See LIBSPDM_FIPS_SELF_TEST_xxx;
580 : : **/
581 : : uint32_t self_test_result;
582 : : /**
583 : : * Buffer provided by integrator to hold large intermediate results.
584 : : **/
585 : : void *selftest_buffer;
586 : : size_t selftest_buffer_size;
587 : : } libspdm_fips_selftest_context_t;
588 : : #endif /* LIBSPDM_FIPS_MODE */
589 : :
590 : : #define LIBSPDM_CONTEXT_STRUCT_VERSION 0x3
591 : :
592 : : typedef struct {
593 : : uint32_t version;
594 : :
595 : : /* IO information */
596 : : libspdm_device_send_message_func send_message;
597 : : libspdm_device_receive_message_func receive_message;
598 : :
599 : : /*
600 : : * reserved for request and response in the main dispatch function in SPDM responder.
601 : : * this buffer is the transport message received from spdm_context->receive_message()
602 : : * or sent to spdm_context->send_message().
603 : : * This message may be SPDM transport message or secured SPDM transport message.
604 : : **/
605 : : libspdm_device_acquire_sender_buffer_func acquire_sender_buffer;
606 : : libspdm_device_release_sender_buffer_func release_sender_buffer;
607 : : libspdm_device_acquire_receiver_buffer_func acquire_receiver_buffer;
608 : : libspdm_device_release_receiver_buffer_func release_receiver_buffer;
609 : :
610 : : /* Transport Layer information */
611 : : libspdm_transport_encode_message_func transport_encode_message;
612 : : libspdm_transport_decode_message_func transport_decode_message;
613 : :
614 : : /* Cached plain text command
615 : : * If the command is cipher text, decrypt then cache it. */
616 : : void *last_spdm_request;
617 : : size_t last_spdm_request_size;
618 : :
619 : : /* Buffers used for data processing and transport. */
620 : : void *scratch_buffer;
621 : : size_t scratch_buffer_size;
622 : : void *sender_buffer;
623 : : size_t sender_buffer_size;
624 : : void *receiver_buffer;
625 : : size_t receiver_buffer_size;
626 : :
627 : : /* Cache session_id in this spdm_message, only valid for secured message. */
628 : : uint32_t last_spdm_request_session_id;
629 : : bool last_spdm_request_session_id_valid;
630 : :
631 : : /* Cache the error in libspdm_process_request. It is handled in libspdm_build_response. */
632 : : libspdm_error_struct_t last_spdm_error;
633 : :
634 : : /* Register GetResponse function (responder only) */
635 : : void *get_response_func;
636 : :
637 : : /* Register GetEncapResponse function (requester only) */
638 : : void *get_encap_response_func;
639 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
640 : : libspdm_encap_context_t encap_context;
641 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
642 : :
643 : : /* Register spdm_session_state_callback function (responder only)
644 : : * Register can know the state after StartSession / EndSession. */
645 : : void *spdm_session_state_callback;
646 : :
647 : : /* Register spdm_connection_state_callback function (responder only)
648 : : * Register can know the connection state such as negotiated. */
649 : : void *spdm_connection_state_callback;
650 : :
651 : : /* Register libspdm_key_update_callback function (responder only)
652 : : * Register can know when session keys are updated during KEY_UPDATE operations. */
653 : : void *spdm_key_update_callback;
654 : :
655 : : /* Callback function so that Integrator can specify encapsulated requests (responder only) */
656 : : void *encap_flow_handler_callback;
657 : :
658 : : libspdm_local_context_t local_context;
659 : :
660 : : libspdm_connection_info_t connection_info;
661 : : libspdm_transcript_t transcript;
662 : :
663 : : libspdm_session_info_t session_info[LIBSPDM_MAX_SESSION_COUNT];
664 : :
665 : : /* Cache latest session ID for HANDSHAKE_IN_THE_CLEAR */
666 : : uint32_t latest_session_id;
667 : :
668 : : /* A session to terminate once the response to the current request has been encoded, or
669 : : * INVALID_SESSION_ID. */
670 : : uint32_t session_id_to_terminate;
671 : :
672 : : /* Register for Responder state, be initial to Normal (responder only) */
673 : : libspdm_response_state_t response_state;
674 : :
675 : : /* Cached data for SPDM_ERROR_CODE_RESPONSE_NOT_READY/SPDM_RESPOND_IF_READY */
676 : : spdm_error_data_response_not_ready_t error_data;
677 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
678 : : void *cache_spdm_request;
679 : : size_t cache_spdm_request_size;
680 : : /* Session context of the original request that triggered the ResponseNotReady flow. The validity
681 : : * of the RESPOND_IF_READY request is defined by that original request, so the RESPOND_IF_READY
682 : : * must arrive in the same session context (same in-session/out-of-session, and if in a session
683 : : * the same session_id). */
684 : : bool cache_spdm_request_session_id_valid;
685 : : uint32_t cache_spdm_request_session_id;
686 : : #endif
687 : : uint8_t current_token;
688 : :
689 : : /* Register for the retry times when receive "BUSY" Error response (requester only) */
690 : : uint8_t retry_times;
691 : : /* Register for the delay time in microseconds between retry requests
692 : : * when receive "BUSY" Error response (requester only) */
693 : : uint64_t retry_delay_time;
694 : : bool crypto_request;
695 : :
696 : : /* App context data for use by application */
697 : : void *app_context_data_ptr;
698 : :
699 : : /* See LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY_*. */
700 : : uint8_t handle_error_return_policy;
701 : :
702 : : /* Max session count for DHE session and PSK session
703 : : * Set via LIBSPDM_DATA_MAX_DHE_SESSION_COUNT and LIBSPDM_DATA_MAX_PSK_SESSION_COUNT */
704 : : uint32_t max_dhe_session_count;
705 : : uint32_t max_psk_session_count;
706 : :
707 : : /* Current session count for DHE session and PSK session */
708 : : uint32_t current_dhe_session_count;
709 : : uint32_t current_psk_session_count;
710 : :
711 : : /* see LIBSPDM_DATA_MAX_SPDM_SESSION_SEQUENCE_NUMBER */
712 : : uint64_t max_spdm_session_sequence_number;
713 : :
714 : : uint8_t sequence_number_endian;
715 : :
716 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
717 : : /* Chunk specific context */
718 : : libspdm_chunk_context_t chunk_context;
719 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
720 : :
721 : : #if LIBSPDM_ENABLE_MSG_LOG
722 : : libspdm_msg_log_t msg_log;
723 : : #endif /* LIBSPDM_ENABLE_MSG_LOG */
724 : :
725 : : #if LIBSPDM_FIPS_MODE
726 : : libspdm_fips_selftest_context_t fips_selftest_context;
727 : : #endif /* LIBSPDM_FIPS_MODE */
728 : :
729 : : /* Endianness (BE/LE/Both) to use for signature verification on SPDM 1.0 and 1.1
730 : : * This field is ignored for other SPDM versions */
731 : : uint8_t spdm_10_11_verify_signature_endian;
732 : : /* The value set by the Integrator. Verification narrows BIG_OR_LITTLE to the endianness of the
733 : : * peer, so libspdm_reset_context restores spdm_10_11_verify_signature_endian from this. */
734 : : uint8_t spdm_10_11_verify_signature_endian_setting;
735 : :
736 : : #if LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES
737 : : libspdm_vendor_response_callback_func vendor_response_callback;
738 : : #endif /* LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES */
739 : :
740 : : #if LIBSPDM_EVENT_RECIPIENT_SUPPORT
741 : : libspdm_process_event_func process_event;
742 : : #endif /* LIBSPDM_EVENT_RECIPIENT_SUPPORT */
743 : :
744 : : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
745 : : libspdm_meas_log_reset_callback_func spdm_meas_log_reset_callback;
746 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
747 : : } libspdm_context_t;
748 : :
749 : : #define LIBSPDM_CONTEXT_SIZE_WITHOUT_SECURED_CONTEXT (sizeof(libspdm_context_t))
750 : : #define LIBSPDM_CONTEXT_SIZE_ALL (LIBSPDM_CONTEXT_SIZE_WITHOUT_SECURED_CONTEXT + \
751 : : LIBSPDM_SECURED_MESSAGE_CONTEXT_SIZE * LIBSPDM_MAX_SESSION_COUNT)
752 : :
753 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
754 : : /**
755 : : * Return the encapsulated context for the given session.
756 : : *
757 : : * Each secure session has its own encapsulated context, and there is one additional context for
758 : : * encapsulated flows that occur outside of a session.
759 : : *
760 : : * @param spdm_context A pointer to the SPDM context.
761 : : * @param session_id If NULL then the context outside of a session is returned. Otherwise the
762 : : * context for the given session is returned.
763 : : *
764 : : * @return The encapsulated context, or NULL if session_id does not identify a valid session.
765 : : **/
766 : : libspdm_encap_context_t *libspdm_get_encap_context(libspdm_context_t *spdm_context,
767 : : const uint32_t *session_id);
768 : :
769 : : /**
770 : : * Return the session that the encapsulated flow on the current channel belongs to.
771 : : *
772 : : * This is normally the session that the message currently being processed arrived on. When both
773 : : * endpoints have set HANDSHAKE_IN_THE_CLEAR_CAP the session-based mutual authentication flow is
774 : : * conducted outside of a session, but it still belongs to the session that KEY_EXCHANGE_RSP
775 : : * established, so that session is returned instead.
776 : : *
777 : : * @param spdm_context A pointer to the SPDM context.
778 : : *
779 : : * @return The session ID, or NULL if the flow is not associated with a session.
780 : : **/
781 : : const uint32_t *libspdm_get_encap_session_id_via_last_request(libspdm_context_t *spdm_context);
782 : :
783 : : /**
784 : : * Return the encapsulated context for the channel that the message currently being processed
785 : : * arrived on.
786 : : *
787 : : * If the message arrived within a session then that session's encapsulated context is returned,
788 : : * otherwise the context outside of a session is returned. See
789 : : * libspdm_get_encap_session_id_via_last_request for the handshake-in-the-clear exception.
790 : : *
791 : : * @param spdm_context A pointer to the SPDM context.
792 : : *
793 : : * @return The encapsulated context.
794 : : **/
795 : : libspdm_encap_context_t *libspdm_get_encap_context_via_last_request(
796 : : libspdm_context_t *spdm_context);
797 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
798 : :
799 : : #if LIBSPDM_DEBUG_PRINT_ENABLE
800 : : /**
801 : : * Return the request code name based on given request code.
802 : : *
803 : : * @param request_code The SPDM request code.
804 : : *
805 : : * @return request code name according to the request code.
806 : : **/
807 : : const char *libspdm_get_code_str(uint8_t request_code);
808 : :
809 : : #ifdef LIBSPDM_INTERNAL_DUMP_HEX_STR_OVERRIDE
810 : : extern void LIBSPDM_INTERNAL_DUMP_HEX_STR_OVERRIDE(const uint8_t *data, size_t size);
811 : : #define LIBSPDM_INTERNAL_DUMP_HEX_STR(data, size) LIBSPDM_INTERNAL_DUMP_HEX_STR_OVERRIDE(data, size)
812 : : #else
813 : : /**
814 : : * This function dump raw data.
815 : : *
816 : : * @param data raw data
817 : : * @param size raw data size
818 : : **/
819 : : void libspdm_internal_dump_hex_str(const uint8_t *data, size_t size);
820 : : #define LIBSPDM_INTERNAL_DUMP_HEX_STR(data, size) libspdm_internal_dump_hex_str(data, size)
821 : : #endif /* LIBSPDM_INTERNAL_DUMP_HEX_STR_OVERRIDE */
822 : :
823 : : #ifdef LIBSPDM_INTERNAL_DUMP_DATA_OVERRIDE
824 : : extern void LIBSPDM_INTERNAL_DUMP_DATA_OVERRIDE(const uint8_t *data, size_t size);
825 : : #define LIBSPDM_INTERNAL_DUMP_DATA(data, size) LIBSPDM_INTERNAL_DUMP_DATA_OVERRIDE(data, size)
826 : : #else
827 : : /**
828 : : * This function dump raw data.
829 : : *
830 : : * @param data raw data
831 : : * @param size raw data size
832 : : **/
833 : : void libspdm_internal_dump_data(const uint8_t *data, size_t size);
834 : : #define LIBSPDM_INTERNAL_DUMP_DATA(data, size) libspdm_internal_dump_data(data, size)
835 : : #endif /* LIBSPDM_INTERNAL_DUMP_DATA_OVERRIDE */
836 : :
837 : : #ifdef LIBSPDM_INTERNAL_DUMP_HEX_OVERRIDE
838 : : extern void LIBSPDM_INTERNAL_DUMP_HEX_OVERRIDE(const uint8_t *data, size_t size);
839 : : #define LIBSPDM_INTERNAL_DUMP_HEX(data, size) LIBSPDM_INTERNAL_DUMP_HEX_OVERRIDE(data, size)
840 : : #else
841 : : /**
842 : : * This function dump raw data with column format.
843 : : *
844 : : * @param data raw data
845 : : * @param size raw data size
846 : : **/
847 : : void libspdm_internal_dump_hex(const uint8_t *data, size_t size);
848 : : #define LIBSPDM_INTERNAL_DUMP_HEX(data, size) libspdm_internal_dump_hex(data, size)
849 : : #endif /* LIBSPDM_INTERNAL_DUMP_HEX_OVERRIDE */
850 : :
851 : : #else /* LIBSPDM_DEBUG_PRINT_ENABLE */
852 : : #define LIBSPDM_INTERNAL_DUMP_HEX(data, size)
853 : : #define LIBSPDM_INTERNAL_DUMP_HEX_STR(data, size)
854 : : #define LIBSPDM_INTERNAL_DUMP_DATA(data, size)
855 : : #endif /* LIBSPDM_DEBUG_PRINT_ENABLE */
856 : :
857 : : /* Required scratch buffer size for libspdm internal usage.
858 : : * It may be used to hold the encrypted/decrypted message and/or last sent/received message.
859 : : * It may be used to hold the large request/response and intermediate send/receive buffer
860 : : * in case of chunking.
861 : : *
862 : : * If chunking is not supported, it should be at least below.
863 : : * +--------------------------+-----------------+-----------------+
864 : : * | SENDER_RECEIVER |MAX_SPDM_MSG_SIZE|MAX_SPDM_MSG_SIZE|
865 : : * +--------------------------+-----------------+-----------------+
866 : : * |<- Secure msg ->|<-last request ->|<-cache request->|
867 : : *
868 : : *
869 : : * If chunking is supported, it should be at least below.
870 : : * +---------------+--------------+--------------------------+------------------------------+-----------------+-----------------+
871 : : * |SECURE_MESSAGE |LARGE_MESSAGE | SENDER_RECEIVER | LARGE SENDER_RECEIVER |MAX_SPDM_MSG_SIZE|MAX_SPDM_MSG_SIZE|
872 : : * +---------------+--------------+--------------------------+------------------------------+-----------------+-----------------+
873 : : * |<-Secure msg ->|<-Large msg ->|<-Snd/Rcv buf for chunk ->|<-Snd/Rcv buf for large msg ->|<-last request ->|<-cache request->|
874 : : *
875 : : * The cache request section is present only if LIBSPDM_RESPOND_IF_READY_SUPPORT is set.
876 : : *
877 : : * The value is configurable based on max_spdm_msg_size.
878 : : * The value MAY be changed in different libspdm version.
879 : : * It is exposed here, just in case the libspdm consumer wants to configure the setting at build time.
880 : : */
881 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
882 : : /* first section */
883 : : uint32_t libspdm_get_scratch_buffer_secure_message_offset(void);
884 : : uint32_t libspdm_get_scratch_buffer_secure_message_capacity(libspdm_context_t *spdm_context);
885 : :
886 : : /* second section */
887 : : uint32_t libspdm_get_scratch_buffer_large_message_offset(libspdm_context_t *spdm_context);
888 : : uint32_t libspdm_get_scratch_buffer_large_message_capacity(libspdm_context_t *spdm_context);
889 : : #endif
890 : :
891 : : /* third section */
892 : : uint32_t libspdm_get_scratch_buffer_sender_receiver_offset(libspdm_context_t *spdm_context);
893 : : uint32_t libspdm_get_scratch_buffer_sender_receiver_capacity(libspdm_context_t *spdm_context);
894 : :
895 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
896 : : /* fourth section */
897 : : uint32_t libspdm_get_scratch_buffer_large_sender_receiver_offset(libspdm_context_t *spdm_context);
898 : : uint32_t libspdm_get_scratch_buffer_large_sender_receiver_capacity(libspdm_context_t *spdm_context);
899 : : #endif
900 : :
901 : : /* fifth section */
902 : : uint32_t libspdm_get_scratch_buffer_last_spdm_request_offset(libspdm_context_t *spdm_context);
903 : : uint32_t libspdm_get_scratch_buffer_last_spdm_request_capacity(libspdm_context_t *spdm_context);
904 : :
905 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
906 : : /* sixth section */
907 : : uint32_t libspdm_get_scratch_buffer_cache_spdm_request_offset(libspdm_context_t *spdm_context);
908 : : uint32_t libspdm_get_scratch_buffer_cache_spdm_request_capacity(libspdm_context_t *spdm_context);
909 : : #endif
910 : :
911 : : /* combination */
912 : : uint32_t libspdm_get_scratch_buffer_capacity(libspdm_context_t *spdm_context);
913 : :
914 : : /**
915 : : * Append a new data buffer to the managed buffer.
916 : : *
917 : : * @param managed_buffer The managed buffer to be appended.
918 : : * @param buffer The address of the data buffer to be appended to the managed buffer.
919 : : * @param buffer_size The size in bytes of the data buffer to be appended to the managed buffer.
920 : : *
921 : : * @retval LIBSPDM_STATUS_SUCCESS The new data buffer is appended to the managed buffer.
922 : : * @retval LIBSPDM_STATUS_BUFFER_FULL The managed buffer is too small to be appended.
923 : : **/
924 : : libspdm_return_t libspdm_append_managed_buffer(void *managed_buffer,
925 : : const void *buffer, size_t buffer_size);
926 : :
927 : : /**
928 : : * Reset the managed buffer.
929 : : * The buffer_size is reset to 0.
930 : : * The max_buffer_size is unchanged.
931 : : * The buffer is not freed.
932 : : *
933 : : * @param managed_buffer The managed buffer.
934 : : **/
935 : : void libspdm_reset_managed_buffer(void *managed_buffer);
936 : :
937 : : /**
938 : : * Return the size of managed buffer.
939 : : *
940 : : * @param managed_buffer The managed buffer.
941 : : *
942 : : * @return the size of managed buffer.
943 : : **/
944 : : size_t libspdm_get_managed_buffer_size(void *managed_buffer);
945 : :
946 : : /**
947 : : * Return the address of managed buffer.
948 : : *
949 : : * @param managed_buffer The managed buffer.
950 : : *
951 : : * @return the address of managed buffer.
952 : : **/
953 : : void *libspdm_get_managed_buffer(void *managed_buffer);
954 : :
955 : : /**
956 : : * Init the managed buffer.
957 : : *
958 : : * @param managed_buffer The managed buffer.
959 : : * @param max_buffer_size The maximum size in bytes of the managed buffer.
960 : : **/
961 : : void libspdm_init_managed_buffer(void *managed_buffer, size_t max_buffer_size);
962 : :
963 : : /**
964 : : * Reset message buffer in SPDM context according to request code.
965 : : *
966 : : * @param context A pointer to the SPDM context.
967 : : * @param session_info A pointer to the SPDM session context.
968 : : * @param request_code The SPDM request code.
969 : : */
970 : : void libspdm_reset_message_buffer_via_request_code(void *context, void *session_info,
971 : : uint8_t request_code);
972 : :
973 : : /**
974 : : * Reset message buffer according to an encapsulated request code. Encapsulated-flow counterpart to
975 : : * libspdm_reset_message_buffer_via_request_code.
976 : : *
977 : : * @param context A pointer to the SPDM context.
978 : : * @param session_info A pointer to the SPDM session context.
979 : : * @param request_code The encapsulated SPDM request code.
980 : : */
981 : : void libspdm_reset_message_buffer_via_encap_request_code(void *context, void *session_info,
982 : : uint8_t request_code);
983 : :
984 : : /**
985 : : * This function initializes the session info.
986 : : *
987 : : * @param spdm_context A pointer to the SPDM context.
988 : : * @param session_id The SPDM session ID.
989 : : **/
990 : : void libspdm_session_info_init(libspdm_context_t *spdm_context,
991 : : libspdm_session_info_t *session_info,
992 : : uint32_t session_id, spdm_version_number_t secured_message_version,
993 : : bool use_psk);
994 : :
995 : : #if LIBSPDM_ENABLE_CAPABILITY_PSK_CAP
996 : : /**
997 : : * Set the psk_hint to a session info.
998 : : *
999 : : * @param session_info A pointer to a session info.
1000 : : * @param psk_hint Indicate the PSK hint.
1001 : : * @param psk_hint_size The size in bytes of the PSK hint.
1002 : : */
1003 : : void libspdm_session_info_set_psk_hint(libspdm_session_info_t *session_info,
1004 : : const void *psk_hint,
1005 : : size_t psk_hint_size);
1006 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP */
1007 : :
1008 : : /**
1009 : : * This function returns if a given version is supported based upon the GET_VERSION/VERSION.
1010 : : *
1011 : : * @param spdm_context A pointer to the SPDM context.
1012 : : * @param version The SPDM version.
1013 : : *
1014 : : * @retval true the version is supported.
1015 : : * @retval false the version is not supported.
1016 : : **/
1017 : : bool libspdm_is_version_supported(const libspdm_context_t *spdm_context, uint8_t version);
1018 : :
1019 : : /**
1020 : : * This function returns connection version negotiated by GET_VERSION/VERSION.
1021 : : *
1022 : : * @param spdm_context A pointer to the SPDM context.
1023 : : *
1024 : : * @return the connection version.
1025 : : **/
1026 : : uint8_t libspdm_get_connection_version(const libspdm_context_t *spdm_context);
1027 : :
1028 : : /**
1029 : : * This function returns if a capabilities flag is supported in current SPDM connection.
1030 : : *
1031 : : * @param spdm_context A pointer to the SPDM context.
1032 : : * @param is_requester Is the function called from a requester.
1033 : : * @param requester_capabilities_flag The requester capabilities flag to be checked
1034 : : * @param responder_capabilities_flag The responder capabilities flag to be checked
1035 : : *
1036 : : * @retval true the capabilities flag is supported.
1037 : : * @retval false the capabilities flag is not supported.
1038 : : **/
1039 : : bool libspdm_is_capabilities_flag_supported(const libspdm_context_t *spdm_context,
1040 : : bool is_requester,
1041 : : uint32_t requester_capabilities_flag,
1042 : : uint32_t responder_capabilities_flag);
1043 : :
1044 : : /**
1045 : : * This function returns if a capabilities extended flag is supported in current SPDM connection.
1046 : : *
1047 : : * @param spdm_context A pointer to the SPDM context.
1048 : : * @param is_requester Is the function called from a requester.
1049 : : * @param requester_capabilities_ext_flag The requester capabilities extended flag to be checked
1050 : : * @param responder_capabilities_ext_flag The responder capabilities extended flag to be checked
1051 : : *
1052 : : * @retval true the capabilities extended flag is supported.
1053 : : * @retval false the capabilities extended flag is not supported.
1054 : : **/
1055 : : bool libspdm_is_capabilities_ext_flag_supported(const libspdm_context_t *spdm_context,
1056 : : bool is_requester,
1057 : : uint16_t requester_capabilities_ext_flag,
1058 : : uint16_t responder_capabilities_ext_flag);
1059 : :
1060 : : /**
1061 : : * Checks the negotiated SPDM version and endpoint capabilities to determine if encapsulated
1062 : : * messages are supported or not.
1063 : : *
1064 : : * @param spdm_context A pointer to the SPDM context.
1065 : : *
1066 : : * @retval true Both endpoints support encapsulated messages.
1067 : : * @retval false At least one endpoint does not support encapsulated messages.
1068 : : **/
1069 : : bool libspdm_is_encap_supported(const libspdm_context_t *spdm_context);
1070 : :
1071 : : /**
1072 : : * This function generates the certificate chain hash.
1073 : : *
1074 : : * @param spdm_context A pointer to the SPDM context.
1075 : : * @param slot_id The slot index of the certificate chain.
1076 : : * @param hash The buffer to store the certificate chain hash.
1077 : : *
1078 : : * @retval true certificate chain hash is generated.
1079 : : * @retval false certificate chain hash is not generated.
1080 : : **/
1081 : : bool libspdm_generate_cert_chain_hash(libspdm_context_t *spdm_context,
1082 : : size_t slot_id, uint8_t *hash);
1083 : :
1084 : : /**
1085 : : * This function generates the public key hash.
1086 : : *
1087 : : * @param spdm_context A pointer to the SPDM context.
1088 : : * @param hash The buffer to store the public key hash.
1089 : : *
1090 : : * @retval true public key hash is generated.
1091 : : * @retval false public key hash is not generated.
1092 : : **/
1093 : : bool libspdm_generate_public_key_hash(libspdm_context_t *spdm_context,
1094 : : uint8_t *hash);
1095 : :
1096 : : /**
1097 : : * This function verifies the integrity of peer certificate chain buffer including
1098 : : * spdm_cert_chain_t header.
1099 : : *
1100 : : * @param spdm_context A pointer to the SPDM context.
1101 : : * @param cert_chain_buffer Certificate chain buffer including spdm_cert_chain_t header.
1102 : : * @param cert_chain_buffer_size size in bytes of the certificate chain buffer.
1103 : : *
1104 : : * @retval true Peer certificate chain buffer integrity verification passed.
1105 : : * @retval false Peer certificate chain buffer integrity verification failed.
1106 : : **/
1107 : : bool libspdm_verify_peer_cert_chain_buffer_integrity(libspdm_context_t *spdm_context,
1108 : : const void *cert_chain_buffer,
1109 : : size_t cert_chain_buffer_size);
1110 : :
1111 : : /**
1112 : : * This function verifies peer certificate chain authority.
1113 : : *
1114 : : * @param spdm_context A pointer to the SPDM context.
1115 : : * @param cert_chain_buffer Certificate chain buffer including spdm_cert_chain_t header.
1116 : : * @param cert_chain_buffer_size size in bytes of the certificate chain buffer.
1117 : : * @param trust_anchor A buffer to hold the trust_anchor which is used to validate the peer certificate, if not NULL.
1118 : : * @param trust_anchor_size A buffer to hold the trust_anchor_size, if not NULL.
1119 : : *
1120 : : * @retval true Peer certificate chain buffer authority verification passed.
1121 : : * @retval false Peer certificate chain buffer authority verification failed, including when no
1122 : : * root certificate is provisioned.
1123 : : **/
1124 : : bool libspdm_verify_peer_cert_chain_buffer_authority(libspdm_context_t *spdm_context,
1125 : : const void *cert_chain_buffer,
1126 : : size_t cert_chain_buffer_size,
1127 : : const void **trust_anchor,
1128 : : size_t *trust_anchor_size);
1129 : : /**
1130 : : * This function generates the challenge signature based upon m1m2 for authentication.
1131 : : *
1132 : : * @param spdm_context A pointer to the SPDM context.
1133 : : * @param is_requester Indicate of the signature generation for a requester or a responder.
1134 : : * @param signature The buffer to store the challenge signature.
1135 : : *
1136 : : * @retval true challenge signature is generated.
1137 : : * @retval false challenge signature is not generated.
1138 : : **/
1139 : : bool libspdm_generate_challenge_auth_signature(libspdm_context_t *spdm_context,
1140 : : bool is_requester,
1141 : : uint8_t slot_id,
1142 : : uint8_t *signature);
1143 : :
1144 : : /**
1145 : : * This function verifies the certificate chain hash.
1146 : : *
1147 : : * @param spdm_context A pointer to the SPDM context.
1148 : : * @param certificate_chain_hash The certificate chain hash data buffer.
1149 : : * @param certificate_chain_hash_size size in bytes of the certificate chain hash data buffer.
1150 : : *
1151 : : * @retval true hash verification pass.
1152 : : * @retval false hash verification fail.
1153 : : **/
1154 : : bool libspdm_verify_certificate_chain_hash(libspdm_context_t *spdm_context,
1155 : : uint8_t slot_id,
1156 : : const void *certificate_chain_hash,
1157 : : size_t certificate_chain_hash_size);
1158 : :
1159 : : /**
1160 : : * This function verifies the public key hash.
1161 : : *
1162 : : * @param spdm_context A pointer to the SPDM context.
1163 : : * @param public_key_hash The public key hash data buffer.
1164 : : * @param public_key_hash_size size in bytes of the public key hash data buffer.
1165 : : *
1166 : : * @retval true hash verification pass.
1167 : : * @retval false hash verification fail.
1168 : : **/
1169 : : bool libspdm_verify_public_key_hash(libspdm_context_t *spdm_context,
1170 : : const void *public_key_hash,
1171 : : size_t public_key_hash_size);
1172 : :
1173 : : /**
1174 : : * This function verifies the challenge signature based upon m1m2.
1175 : : *
1176 : : * @param spdm_context A pointer to the SPDM context.
1177 : : * @param is_requester Indicate of the signature verification for a requester or a responder.
1178 : : * @param sign_data The signature data buffer.
1179 : : * @param sign_data_size size in bytes of the signature data buffer.
1180 : : *
1181 : : * @retval true signature verification pass.
1182 : : * @retval false signature verification fail.
1183 : : **/
1184 : : bool libspdm_verify_challenge_auth_signature(libspdm_context_t *spdm_context,
1185 : : bool is_requester,
1186 : : uint8_t slot_id,
1187 : : const void *sign_data,
1188 : : size_t sign_data_size);
1189 : :
1190 : : /**
1191 : : * This function calculate the measurement summary hash size.
1192 : : *
1193 : : * @param spdm_context A pointer to the SPDM context.
1194 : : * @param is_requester Is the function called from a requester.
1195 : : * @param measurement_summary_hash_type The type of the measurement summary hash.
1196 : : *
1197 : : * @return 0 measurement summary hash type is invalid, NO_MEAS hash type or no MEAS capabilities.
1198 : : * @return measurement summary hash size according to type.
1199 : : **/
1200 : : uint32_t libspdm_get_measurement_summary_hash_size(libspdm_context_t *spdm_context,
1201 : : bool is_requester,
1202 : : uint8_t measurement_summary_hash_type);
1203 : :
1204 : : /**
1205 : : * This function generates the endpoint info signature based upon il1il2 for authentication.
1206 : : *
1207 : : * @param spdm_context A pointer to the SPDM context.
1208 : : * @param session_info A pointer to the SPDM session context.
1209 : : * @param is_requester Indicate of the signature generation for a requester or a responder.
1210 : : * @param signature The buffer to store the endpoint info signature.
1211 : : *
1212 : : * @retval true endpoint info signature is generated.
1213 : : * @retval false endpoint info signature is not generated.
1214 : : **/
1215 : : bool libspdm_generate_endpoint_info_signature(libspdm_context_t *spdm_context,
1216 : : libspdm_session_info_t *session_info,
1217 : : bool is_requester,
1218 : : uint8_t slot_id,
1219 : : uint8_t *signature);
1220 : :
1221 : : /**
1222 : : * This function verifies the endpoint info signature based upon il1il2.
1223 : : *
1224 : : * @param spdm_context A pointer to the SPDM context.
1225 : : * @param session_info A pointer to the SPDM session context.
1226 : : * @param is_requester Indicate of the signature verification for a requester or a responder.
1227 : : * @param sign_data The signature data buffer.
1228 : : * @param sign_data_size size in bytes of the signature data buffer.
1229 : : *
1230 : : * @retval true signature verification pass.
1231 : : * @retval false signature verification fail.
1232 : : **/
1233 : : bool libspdm_verify_endpoint_info_signature(libspdm_context_t *spdm_context,
1234 : : libspdm_session_info_t *session_info,
1235 : : bool is_requester,
1236 : : uint8_t slot_id,
1237 : : const void *sign_data,
1238 : : size_t sign_data_size);
1239 : :
1240 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
1241 : : /**
1242 : : * Free the cached public key of the peer's leaf certificate in a slot, if there is one.
1243 : : *
1244 : : * The key is freed with the negotiated algorithm that it was parsed with, so this must be called
1245 : : * before the negotiated algorithms change.
1246 : : *
1247 : : * @param context A pointer to the SPDM context.
1248 : : * @param slot_id The slot of the peer certificate chain.
1249 : : **/
1250 : : void libspdm_free_peer_leaf_cert_public_key(libspdm_context_t *context, uint8_t slot_id);
1251 : : #endif /* !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) */
1252 : :
1253 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1254 : : /*
1255 : : * This function calculates l1l2.
1256 : : * If session_info is NULL, this function will use M cache of SPDM context,
1257 : : * else will use M cache of SPDM session context.
1258 : : *
1259 : : * @param spdm_context A pointer to the SPDM context.
1260 : : * @param session_info A pointer to the SPDM session context.
1261 : : * @param l1l2 The buffer to store the l1l2.
1262 : : *
1263 : : * @retval true l1l2 is calculated.
1264 : : */
1265 : : bool libspdm_calculate_l1l2(libspdm_context_t *spdm_context,
1266 : : void *session_info,
1267 : : libspdm_l1l2_managed_buffer_t *l1l2);
1268 : : #else
1269 : : /*
1270 : : * This function calculates l1l2 hash.
1271 : : * If session_info is NULL, this function will use M cache of SPDM context,
1272 : : * else will use M cache of SPDM session context.
1273 : : *
1274 : : * @param spdm_context A pointer to the SPDM context.
1275 : : * @param session_info A pointer to the SPDM session context.
1276 : : * @param l1l2_hash_size size in bytes of the l1l2 hash
1277 : : * @param l1l2_hash The buffer to store the l1l2 hash
1278 : : *
1279 : : * @retval true l1l2 is calculated.
1280 : : */
1281 : : bool libspdm_calculate_l1l2_hash(libspdm_context_t *spdm_context,
1282 : : void *session_info,
1283 : : size_t *l1l2_hash_size, void *l1l2_hash);
1284 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
1285 : :
1286 : : /**
1287 : : * Get element from multi element opaque data by element id and element index
1288 : : *
1289 : : * @param[in] spdm_context A pointer to the SPDM context.
1290 : : * @param[in] data_in_size Size of multi element opaque data.
1291 : : * @param[in] data_in A pointer to the multi element opaque data.
1292 : : * @param[in] element_id Element id.
1293 : : * @param[in] element_index Element index, start from 0.
1294 : : * It is used to get the nth element with the same element id.
1295 : : * @param[out] total_matched_element_cnt Pointer to store total matched element count with the same element id.
1296 : : * @param[out] get_element_ptr Pointer to store found element.
1297 : : * @param[out] get_element_len Pointer to length of found element.
1298 : : *
1299 : : * @retval true Get element successfully
1300 : : * @retval false Get element failed
1301 : : **/
1302 : : bool libspdm_get_element_from_opaque_data_with_element_id (libspdm_context_t *spdm_context,
1303 : : size_t data_in_size, const void *data_in,
1304 : : uint8_t element_id, uint8_t element_index,
1305 : : uint8_t *total_matched_element_cnt,
1306 : : const void **get_element_ptr, size_t *get_element_len);
1307 : :
1308 : : /**
1309 : : * Get secured message opaque data element from multi element opaque data by sm data id
1310 : : *
1311 : : * This function should be called in
1312 : : * libspdm_process_opaque_data_supported_version_data/libspdm_process_opaque_data_version_selection_data.
1313 : : *
1314 : : * @param[in] spdm_context A pointer to the SPDM context.
1315 : : * @param[in] data_in_size Size of multi element opaque data.
1316 : : * @param[in] data_in A pointer to the multi element opaque data.
1317 : : * @param[in] sm_data_id ID for the Secured Message data type.
1318 : : * @param[out] get_element_ptr Pointer to store found element.
1319 : : * @param[out] get_element_len Pointer to length of found element.
1320 : : *
1321 : : * @retval true Get element successfully
1322 : : * @retval false Get element failed
1323 : : **/
1324 : : bool libspdm_get_sm_data_element_from_opaque_data (libspdm_context_t *spdm_context,
1325 : : size_t data_in_size, const void *data_in,
1326 : : uint8_t sm_data_id,
1327 : : const void **get_element_ptr, size_t *get_element_len);
1328 : :
1329 : : /**
1330 : : * Process opaque data version selection.
1331 : : *
1332 : : * This function should be called in KEY_EXCHANGE/PSK_EXCHANGE response parsing in requester.
1333 : : *
1334 : : * @param data_in_size Size in bytes of the data_in.
1335 : : * @param data_in A pointer to the buffer to store the opaque data version selection.
1336 : : **/
1337 : : libspdm_return_t libspdm_process_opaque_data_version_selection_data(
1338 : : libspdm_context_t *spdm_context, size_t data_in_size, void *data_in,
1339 : : spdm_version_number_t *secured_message_version);
1340 : :
1341 : : /**
1342 : : * Process general opaque data check
1343 : : *
1344 : : * @param data_in_size size in bytes of the data_in.
1345 : : * @param data_in A pointer to the opaque data to check.
1346 : : *
1347 : : * @retval true check opaque data successfully
1348 : : * @retval false check opaque data failed
1349 : : **/
1350 : : bool libspdm_process_general_opaque_data_check(libspdm_context_t *spdm_context,
1351 : : size_t data_in_size,
1352 : : const void *data_in);
1353 : :
1354 : : /**
1355 : : * Return the size in bytes of opaque data supported version.
1356 : : *
1357 : : * This function should be called in libspdm_process_opaque_data_supported_version_data.
1358 : : *
1359 : : * @param version_count Secure version count.
1360 : : *
1361 : : * @return The size in bytes of opaque data supported version.
1362 : : **/
1363 : : size_t libspdm_get_untrusted_opaque_data_supported_version_data_size(
1364 : : libspdm_context_t *spdm_context, uint8_t version_count);
1365 : :
1366 : : /**
1367 : : * Return the size in bytes of opaque data supported version.
1368 : : *
1369 : : * This function should be called in KEY_EXCHANGE/PSK_EXCHANGE request generation.
1370 : : *
1371 : : * @return the size in bytes of opaque data supported version.
1372 : : **/
1373 : : size_t libspdm_get_opaque_data_supported_version_data_size(libspdm_context_t *spdm_context);
1374 : :
1375 : : /**
1376 : : * Return the size in bytes of opaque data version selection.
1377 : : *
1378 : : * This function should be called in KEY_EXCHANGE/PSK_EXCHANGE response generation.
1379 : : *
1380 : : * @return the size in bytes of opaque data version selection.
1381 : : **/
1382 : : size_t libspdm_get_opaque_data_version_selection_data_size(const libspdm_context_t *spdm_context);
1383 : :
1384 : : /**
1385 : : * Return the highest secured message version this endpoint offers in its local supported list, in
1386 : : * version-number form (e.g. SECURED_SPDM_VERSION_13 << SPDM_VERSION_NUMBER_SHIFT_BIT), or 0 if the
1387 : : * list is empty.
1388 : : *
1389 : : * @param spdm_context A pointer to the SPDM context.
1390 : : *
1391 : : * @return the highest locally-offered secured message version (version-number form).
1392 : : **/
1393 : : spdm_version_number_t libspdm_local_max_secured_message_version(
1394 : : const libspdm_context_t *spdm_context);
1395 : :
1396 : : /**
1397 : : * Return the size in bytes of the DSP0277 1.3 AEAD limit opaque element when appended on its own,
1398 : : * i.e. the size of a single secured-message opaque element carrying the AEADlimitOE data, with
1399 : : * padding. This is the incremental size that the AEADlimitOE element adds to an opaque data blob
1400 : : * that already has a general opaque data table header and at least one other element.
1401 : : *
1402 : : * The AEADlimitOE element is only defined for secured message version 1.3 and later; for an older
1403 : : * secured_message_version this returns 0.
1404 : : *
1405 : : * @param spdm_context A pointer to the SPDM context.
1406 : : * @param secured_message_version The secured message version this opaque data targets (the
1407 : : * negotiated version for a response, or the highest locally-offered
1408 : : * version for a request).
1409 : : *
1410 : : * @return the size in bytes of the AEAD limit opaque element (0 if the version is older than 1.3).
1411 : : **/
1412 : : size_t libspdm_get_opaque_data_aead_limit_element_size(const libspdm_context_t *spdm_context,
1413 : : spdm_version_number_t
1414 : : secured_message_version);
1415 : :
1416 : : /**
1417 : : * Build the DSP0277 1.3 AEAD limit opaque element (AEADlimitOE) into an opaque data blob that was
1418 : : * built by libspdm_build_opaque_data_supported_version_data (requester) or
1419 : : * libspdm_build_opaque_data_version_selection_data (responder). The general opaque data table
1420 : : * header's total_elements is incremented and the new element is written after the existing
1421 : : * element(s).
1422 : : *
1423 : : * The AEADlimitOE element is only defined for secured message version 1.3 and later; for an older
1424 : : * secured_message_version this is a no-op (nothing is added and *data_out_size is set to the size
1425 : : * of the existing blob).
1426 : : *
1427 : : * @param spdm_context A pointer to the SPDM context.
1428 : : * @param secured_message_version The secured message version this opaque data targets.
1429 : : * @param data_out_size On input, the size in bytes of the data_out buffer. On output,
1430 : : * the total size in bytes of the opaque data blob.
1431 : : * @param data_out The opaque data blob to extend in place.
1432 : : **/
1433 : : void libspdm_build_opaque_data_aead_limit_element(libspdm_context_t *spdm_context,
1434 : : spdm_version_number_t secured_message_version,
1435 : : size_t *data_out_size, void *data_out);
1436 : :
1437 : : /**
1438 : : * Parse the DSP0277 1.3 AEAD limit opaque element (AEADlimitOE) from received opaque data.
1439 : : *
1440 : : * The AEADlimitOE element is only defined for secured message version 1.3 and later. When the
1441 : : * negotiated secured message version is older, the element is ignored (treated as absent) even if
1442 : : * a peer included it, and the default exponent (64) is returned.
1443 : : *
1444 : : * @param spdm_context A pointer to the SPDM context.
1445 : : * @param secured_message_version The negotiated secured message version (version-number form).
1446 : : * @param data_in_size size in bytes of data_in.
1447 : : * @param data_in The received opaque data.
1448 : : * @param aead_limit_exponent On output, the peer's AeadLimitExponent. If the element is absent
1449 : : * or ignored, this is set to the default (64).
1450 : : *
1451 : : * @retval LIBSPDM_STATUS_SUCCESS parsed successfully (or absent/ignored -> default).
1452 : : * @retval LIBSPDM_STATUS_INVALID_MSG_FIELD the element is malformed or the exponent is > 64.
1453 : : **/
1454 : : libspdm_return_t libspdm_process_opaque_data_aead_limit(libspdm_context_t *spdm_context,
1455 : : spdm_version_number_t
1456 : : secured_message_version,
1457 : : size_t data_in_size, const void *data_in,
1458 : : uint8_t *aead_limit_exponent);
1459 : :
1460 : : /**
1461 : : * Apply the negotiated AEAD limit to a session's secured-message context, setting the maximum
1462 : : * allowed session sequence number to min(integrator-configured cap, (2 ^ effective_exponent) - 1),
1463 : : * where effective_exponent = min(local exponent, peer exponent) and exponent 64 maps to the
1464 : : * all-ones max. Only takes effect when the session's secured message version is >= 1.3.
1465 : : *
1466 : : * @param spdm_context A pointer to the SPDM context.
1467 : : * @param session_info The session to configure.
1468 : : * @param peer_aead_limit_exponent The peer's AeadLimitExponent (64 if not advertised).
1469 : : **/
1470 : : void libspdm_apply_aead_limit_to_session(libspdm_context_t *spdm_context,
1471 : : void *session_info,
1472 : : uint8_t peer_aead_limit_exponent);
1473 : :
1474 : : /**
1475 : : * Return the SPDMversion field of the version number struct.
1476 : : *
1477 : : * @param ver Spdm version number struct.
1478 : : *
1479 : : * @return the SPDMversion of the version number struct.
1480 : : **/
1481 : : uint8_t libspdm_get_version_from_version_number(spdm_version_number_t ver);
1482 : :
1483 : : /**
1484 : : * Sort SPDMversion in descending order.
1485 : : *
1486 : : * @param ver_set A pointer to the version set.
1487 : : * @param ver_num Version number.
1488 : : */
1489 : : void libspdm_version_number_sort(spdm_version_number_t *ver_set, size_t ver_num);
1490 : :
1491 : : /**
1492 : : * Negotiate SPDMversion for connection.
1493 : : * req_ver_set is the local version set of requester, res_ver_set is the version set of responder.
1494 : : *
1495 : : * @param common_version A pointer to store the common version.
1496 : : * @param req_ver_set A pointer to the requester version set.
1497 : : * @param req_ver_num Version number of requester.
1498 : : * @param res_ver_set A pointer to the responder version set.
1499 : : * @param res_ver_num Version number of responder.
1500 : : *
1501 : : * @retval true Negotiation successfully, connect version be saved to common_version.
1502 : : * @retval false Negotiation failed.
1503 : : */
1504 : : bool libspdm_negotiate_connection_version(spdm_version_number_t *common_version,
1505 : : spdm_version_number_t *req_ver_set,
1506 : : size_t req_ver_num,
1507 : : const spdm_version_number_t *res_ver_set,
1508 : : size_t res_ver_num);
1509 : :
1510 : : /**
1511 : : * Acquire a device sender buffer for transport layer message.
1512 : : *
1513 : : * @param spdm_context A pointer to the SPDM context.
1514 : : * @param max_msg_size size in bytes of the maximum size of sender buffer.
1515 : : * @param msg_buf_ptr A pointer to a sender buffer.
1516 : : *
1517 : : * @retval LIBSPDM_STATUS_SUCCESS The sender buffer is acquired.
1518 : : **/
1519 : : libspdm_return_t libspdm_acquire_sender_buffer (
1520 : : libspdm_context_t *spdm_context, size_t *max_msg_size, void **msg_buf_ptr);
1521 : :
1522 : : /**
1523 : : * Release a device sender buffer for transport layer message.
1524 : : *
1525 : : * @param spdm_context A pointer to the SPDM context.
1526 : : **/
1527 : : void libspdm_release_sender_buffer (libspdm_context_t *spdm_context);
1528 : :
1529 : : /**
1530 : : * Get the sender buffer.
1531 : : *
1532 : : * @param spdm_context A pointer to the SPDM context.
1533 : : * @param sender_buffer Buffer address of the sender buffer.
1534 : : * @param sender_buffer_size Size of the sender buffer.
1535 : : *
1536 : : **/
1537 : : void libspdm_get_sender_buffer (
1538 : : libspdm_context_t *spdm_context,
1539 : : void **sender_buffer,
1540 : : size_t *sender_buffer_size);
1541 : :
1542 : : /**
1543 : : * Acquire a device receiver buffer for transport layer message.
1544 : : *
1545 : : * @param spdm_context A pointer to the SPDM context.
1546 : : * @param max_msg_size size in bytes of the maximum size of receiver buffer.
1547 : : * @param msg_buf_ptr A pointer to a receiver buffer.
1548 : : *
1549 : : * @retval LIBSPDM_STATUS_SUCCESS The receiver buffer is acquired.
1550 : : **/
1551 : : libspdm_return_t libspdm_acquire_receiver_buffer (
1552 : : libspdm_context_t *spdm_context, size_t *max_msg_size, void **msg_buf_ptr);
1553 : :
1554 : : /**
1555 : : * Release a device receiver buffer for transport layer message.
1556 : : *
1557 : : * @param spdm_context A pointer to the SPDM context.
1558 : : **/
1559 : : void libspdm_release_receiver_buffer (libspdm_context_t *spdm_context);
1560 : :
1561 : : /**
1562 : : * Get the receiver buffer.
1563 : : *
1564 : : * @param spdm_context A pointer to the SPDM context.
1565 : : * @param receiver_buffer Buffer address of the receiver buffer.
1566 : : * @param receiver_buffer_size Size of the receiver buffer.
1567 : : *
1568 : : **/
1569 : : void libspdm_get_receiver_buffer (
1570 : : libspdm_context_t *spdm_context,
1571 : : void **receiver_buffer,
1572 : : size_t *receiver_buffer_size);
1573 : :
1574 : : /**
1575 : : * Get the certificate slot mask
1576 : : *
1577 : : * @param[in] spdm_context A pointer to the SPDM context.
1578 : : *
1579 : : * @return slot_mask get slot mask
1580 : : **/
1581 : : uint8_t libspdm_get_cert_slot_mask (libspdm_context_t *spdm_context);
1582 : :
1583 : : /**
1584 : : * Get the certificate slot count
1585 : : *
1586 : : * @param[in] spdm_context A pointer to the SPDM context.
1587 : : *
1588 : : * @return slot_count get slot count
1589 : : **/
1590 : : uint8_t libspdm_get_cert_slot_count(libspdm_context_t *spdm_context);
1591 : :
1592 : : #if LIBSPDM_ENABLE_MSG_LOG
1593 : : void libspdm_append_msg_log(libspdm_context_t *spdm_context, void *message, size_t message_size);
1594 : : #endif
1595 : :
1596 : : /**
1597 : : * Reset message A cache in SPDM context.
1598 : : *
1599 : : * @param spdm_context A pointer to the SPDM context.
1600 : : **/
1601 : : void libspdm_reset_message_a(libspdm_context_t *spdm_context);
1602 : :
1603 : : /**
1604 : : * Reset message D cache in SPDM context.
1605 : : *
1606 : : * @param spdm_context A pointer to the SPDM context.
1607 : : **/
1608 : : void libspdm_reset_message_d(libspdm_context_t *spdm_context);
1609 : :
1610 : : /**
1611 : : * Reset message B cache in SPDM context.
1612 : : *
1613 : : * @param spdm_context A pointer to the SPDM context.
1614 : : **/
1615 : : void libspdm_reset_message_b(libspdm_context_t *spdm_context);
1616 : :
1617 : : /**
1618 : : * Reset message C cache in SPDM context.
1619 : : *
1620 : : * @param spdm_context A pointer to the SPDM context.
1621 : : **/
1622 : : void libspdm_reset_message_c(libspdm_context_t *spdm_context);
1623 : :
1624 : : /**
1625 : : * Reset message MutB cache in SPDM context.
1626 : : *
1627 : : * @param spdm_context A pointer to the SPDM context.
1628 : : **/
1629 : : void libspdm_reset_message_mut_b(libspdm_context_t *spdm_context);
1630 : :
1631 : : /**
1632 : : * Reset message MutC cache in SPDM context.
1633 : : *
1634 : : * @param spdm_context A pointer to the SPDM context.
1635 : : **/
1636 : : void libspdm_reset_message_mut_c(libspdm_context_t *spdm_context);
1637 : :
1638 : : /**
1639 : : * Reset message M cache in SPDM context.
1640 : : * If session_info is NULL, this function will use M cache of SPDM context,
1641 : : * else will use M cache of SPDM session context.
1642 : : *
1643 : : * @param spdm_context A pointer to the SPDM context.
1644 : : * @param session_info A pointer to the SPDM session context.
1645 : : **/
1646 : : void libspdm_reset_message_m(libspdm_context_t *spdm_context, void *session_info);
1647 : :
1648 : : /**
1649 : : * Reset message K cache in SPDM context.
1650 : : *
1651 : : * @param spdm_context A pointer to the SPDM context.
1652 : : * @param spdm_session_info A pointer to the SPDM session context.
1653 : : **/
1654 : : void libspdm_reset_message_k(libspdm_context_t *spdm_context, void *spdm_session_info);
1655 : :
1656 : : /**
1657 : : * Reset message EncapD cache in SPDM context.
1658 : : *
1659 : : * @param spdm_session_info A pointer to the SPDM session context.
1660 : : **/
1661 : : void libspdm_reset_message_encap_d(void *spdm_session_info);
1662 : :
1663 : : /**
1664 : : * Reset message F cache in SPDM context.
1665 : : *
1666 : : * @param spdm_context A pointer to the SPDM context.
1667 : : * @param spdm_session_info A pointer to the SPDM session context.
1668 : : **/
1669 : : void libspdm_reset_message_f(libspdm_context_t *spdm_context, void *spdm_session_info);
1670 : :
1671 : : /**
1672 : : * Reset message E cache in SPDM context.
1673 : : * If session_info is NULL, this function will use E cache of SPDM context,
1674 : : * else will use E cache of SPDM session context.
1675 : : *
1676 : : * @param spdm_context A pointer to the SPDM context.
1677 : : * @param session_info A pointer to the SPDM session context.
1678 : : **/
1679 : : void libspdm_reset_message_e(libspdm_context_t *spdm_context, void *session_info);
1680 : :
1681 : : /**
1682 : : * Reset message encap E cache in SPDM context.
1683 : : * If session_info is NULL, this function will use encap E cache of SPDM context,
1684 : : * else will use encap E cache of SPDM session context.
1685 : : *
1686 : : * @param spdm_context A pointer to the SPDM context.
1687 : : * @param session_info A pointer to the SPDM session context.
1688 : : **/
1689 : : void libspdm_reset_message_encap_e(libspdm_context_t *spdm_context, void *session_info);
1690 : :
1691 : : /**
1692 : : * Append message A cache in SPDM context.
1693 : : *
1694 : : * @param spdm_context A pointer to the SPDM context.
1695 : : * @param message Message buffer.
1696 : : * @param message_size Size in bytes of message buffer.
1697 : : *
1698 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1699 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1700 : : **/
1701 : : libspdm_return_t libspdm_append_message_a(libspdm_context_t *spdm_context, const void *message,
1702 : : size_t message_size);
1703 : :
1704 : : /**
1705 : : * Append message D cache in SPDM context.
1706 : : *
1707 : : * @param spdm_context A pointer to the SPDM context.
1708 : : * @param message Message buffer.
1709 : : * @param message_size Size in bytes of message buffer.
1710 : : *
1711 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1712 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1713 : : **/
1714 : : libspdm_return_t libspdm_append_message_d(libspdm_context_t *spdm_context, const void *message,
1715 : : size_t message_size);
1716 : :
1717 : : /**
1718 : : * Append message B cache in SPDM context.
1719 : : *
1720 : : * @param spdm_context A pointer to the SPDM context.
1721 : : * @param message Message buffer.
1722 : : * @param message_size Size in bytes of message buffer.
1723 : : *
1724 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1725 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1726 : : **/
1727 : : libspdm_return_t libspdm_append_message_b(libspdm_context_t *spdm_context, const void *message,
1728 : : size_t message_size);
1729 : :
1730 : : /**
1731 : : * Append message C cache in SPDM context.
1732 : : *
1733 : : * @param spdm_context A pointer to the SPDM context.
1734 : : * @param message Message buffer.
1735 : : * @param message_size Size in bytes of message buffer.
1736 : : *
1737 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1738 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1739 : : **/
1740 : : libspdm_return_t libspdm_append_message_c(libspdm_context_t *spdm_context, const void *message,
1741 : : size_t message_size);
1742 : :
1743 : : /**
1744 : : * Append message MutB cache in SPDM context.
1745 : : *
1746 : : * @param spdm_context A pointer to the SPDM context.
1747 : : * @param message Message buffer.
1748 : : * @param message_size Size in bytes of message buffer.
1749 : : *
1750 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1751 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1752 : : **/
1753 : : libspdm_return_t libspdm_append_message_mut_b(libspdm_context_t *spdm_context, const void *message,
1754 : : size_t message_size);
1755 : :
1756 : : /**
1757 : : * Append message MutC cache in SPDM context.
1758 : : *
1759 : : * @param spdm_context A pointer to the SPDM context.
1760 : : * @param message Message buffer.
1761 : : * @param message_size Size in bytes of message buffer.
1762 : : *
1763 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1764 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1765 : : **/
1766 : : libspdm_return_t libspdm_append_message_mut_c(libspdm_context_t *spdm_context, const void *message,
1767 : : size_t message_size);
1768 : :
1769 : : /**
1770 : : * Append message M cache in SPDM context.
1771 : : * If session_info is NULL, this function will use M cache of SPDM context,
1772 : : * else will use M cache of SPDM session context.
1773 : : *
1774 : : * @param spdm_context A pointer to the SPDM context.
1775 : : * @param session_info A pointer to the SPDM session context.
1776 : : * @param message Message buffer.
1777 : : * @param message_size Size in bytes of message buffer.
1778 : : *
1779 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1780 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1781 : : **/
1782 : : libspdm_return_t libspdm_append_message_m(libspdm_context_t *spdm_context,
1783 : : void *session_info,
1784 : : const void *message, size_t message_size);
1785 : :
1786 : : /**
1787 : : * Append message K cache in SPDM context.
1788 : : *
1789 : : * @param spdm_context A pointer to the SPDM context.
1790 : : * @param spdm_session_info A pointer to the SPDM session context.
1791 : : * @param is_requester Indicate of the key generation for a requester or a responder.
1792 : : * @param message Message buffer.
1793 : : * @param message_size Size in bytes of message buffer.
1794 : : *
1795 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1796 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1797 : : **/
1798 : : libspdm_return_t libspdm_append_message_k(libspdm_context_t *spdm_context,
1799 : : void *spdm_session_info,
1800 : : bool is_requester, const void *message,
1801 : : size_t message_size);
1802 : :
1803 : : /**
1804 : : * Append message EncapD cache in SPDM context.
1805 : : *
1806 : : * @param spdm_session_info A pointer to the SPDM session context.
1807 : : * @param message Message buffer.
1808 : : * @param message_size Size in bytes of message buffer.
1809 : : *
1810 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1811 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1812 : : **/
1813 : : libspdm_return_t libspdm_append_message_encap_d(void *spdm_session_info,
1814 : : const void *message,
1815 : : size_t message_size);
1816 : :
1817 : : /**
1818 : : * Append message F cache in SPDM context.
1819 : : *
1820 : : * @param spdm_context A pointer to the SPDM context.
1821 : : * @param spdm_session_info A pointer to the SPDM session context.
1822 : : * @param is_requester Indicate of the key generation for a requester or a responder.
1823 : : * @param message Message buffer.
1824 : : * @param message_size Size in bytes of message buffer.
1825 : : *
1826 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1827 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1828 : : **/
1829 : : libspdm_return_t libspdm_append_message_f(libspdm_context_t *spdm_context,
1830 : : void *spdm_session_info,
1831 : : bool is_requester, const void *message,
1832 : : size_t message_size);
1833 : :
1834 : : /**
1835 : : * Append message E cache in SPDM context.
1836 : : * If session_info is NULL, this function will use E cache of SPDM context,
1837 : : * else will use E cache of SPDM session context.
1838 : : *
1839 : : * @param spdm_context A pointer to the SPDM context.
1840 : : * @param session_info A pointer to the SPDM session context.
1841 : : * @param message message buffer.
1842 : : * @param message_size size in bytes of message buffer.
1843 : : *
1844 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1845 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1846 : : **/
1847 : : libspdm_return_t libspdm_append_message_e(libspdm_context_t *spdm_context, void *session_info,
1848 : : const void *message, size_t message_size);
1849 : :
1850 : : /**
1851 : : * Append message encap E cache in SPDM context.
1852 : : * If session_info is NULL, this function will use encap E cache of SPDM context,
1853 : : * else will use encap E cache of SPDM session context.
1854 : : *
1855 : : * @param spdm_context A pointer to the SPDM context.
1856 : : * @param session_info A pointer to the SPDM session context.
1857 : : * @param message message buffer.
1858 : : * @param message_size size in bytes of message buffer.
1859 : : *
1860 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1861 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1862 : : **/
1863 : : libspdm_return_t libspdm_append_message_encap_e(libspdm_context_t *spdm_context, void *session_info,
1864 : : const void *message, size_t message_size);
1865 : :
1866 : : /**
1867 : : * This function generates a session ID by concatenating req_session_id and rsp_session_id.
1868 : : *
1869 : : * @param[in] req_session_id
1870 : : * @param[in] rsp_session_id
1871 : : *
1872 : : * @return Session ID.
1873 : : **/
1874 : : uint32_t libspdm_generate_session_id(uint16_t req_session_id, uint16_t rsp_session_id);
1875 : :
1876 : : /**
1877 : : * This function assigns a new session ID.
1878 : : *
1879 : : * @param spdm_context A pointer to the SPDM context.
1880 : : * @param session_id The SPDM session ID.
1881 : : *
1882 : : * @return session info associated with this new session ID.
1883 : : **/
1884 : : libspdm_session_info_t *libspdm_assign_session_id(libspdm_context_t *spdm_context,
1885 : : uint32_t session_id,
1886 : : spdm_version_number_t secured_message_version,
1887 : : bool use_psk);
1888 : :
1889 : : /**
1890 : : * This function frees a session ID.
1891 : : *
1892 : : * @param spdm_context A pointer to the SPDM context.
1893 : : * @param session_id The SPDM session ID.
1894 : : **/
1895 : : void libspdm_free_session_id(libspdm_context_t *spdm_context, uint32_t session_id);
1896 : :
1897 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
1898 : : /**
1899 : : * This function ends a chunk transfer and clears its buffer.
1900 : : *
1901 : : * @param chunk_info The chunk transfer to reset.
1902 : : **/
1903 : : void libspdm_reset_chunk_info(libspdm_chunk_info_t *chunk_info);
1904 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
1905 : :
1906 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1907 : : /*
1908 : : * This function calculates current TH data with message A and message K.
1909 : : *
1910 : : * @param spdm_context A pointer to the SPDM context.
1911 : : * @param spdm_session_info A pointer to the SPDM session context.
1912 : : * @param cert_chain_buffer Certificate chain buffer with spdm_cert_chain_t header.
1913 : : * @param cert_chain_buffer_size Size in bytes of the certificate chain buffer.
1914 : : * @param th_curr The managed buffer to store the TH data.
1915 : : *
1916 : : * @retval true current TH data is calculated.
1917 : : */
1918 : : bool libspdm_calculate_th_for_exchange(
1919 : : libspdm_context_t *spdm_context, void *spdm_session_info,
1920 : : const uint8_t *cert_chain_buffer, size_t cert_chain_buffer_size,
1921 : : libspdm_th_managed_buffer_t *th_curr);
1922 : : #else
1923 : : /*
1924 : : * This function calculates current TH hash with message A and message K.
1925 : : *
1926 : : * @param spdm_context A pointer to the SPDM context.
1927 : : * @param spdm_session_info A pointer to the SPDM session context.
1928 : : * @param th_hash_buffer_size Size in bytes of the th_hash_buffer
1929 : : * @param th_hash_buffer The buffer to store the th_hash_buffer
1930 : : *
1931 : : * @retval true current TH hash is calculated.
1932 : : */
1933 : : bool libspdm_calculate_th_hash_for_exchange(
1934 : : libspdm_context_t *spdm_context, void *spdm_session_info,
1935 : : size_t *th_hash_buffer_size, void *th_hash_buffer);
1936 : :
1937 : : /*
1938 : : * This function calculates current TH hmac with message A and message K, with response finished_key.
1939 : : *
1940 : : * @param spdm_context A pointer to the SPDM context.
1941 : : * @param spdm_session_info A pointer to the SPDM session context.
1942 : : * @param th_hmac_buffer_size Size in bytes of the th_hmac_buffer
1943 : : * @param th_hmac_buffer The buffer to store the th_hmac_buffer
1944 : : *
1945 : : * @retval true current TH hmac is calculated.
1946 : : */
1947 : : bool libspdm_calculate_th_hmac_for_exchange_rsp(
1948 : : libspdm_context_t *spdm_context, void *spdm_session_info,
1949 : : size_t *th_hmac_buffer_size, void *th_hmac_buffer);
1950 : : #endif
1951 : :
1952 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1953 : : /*
1954 : : * This function calculates current TH data with message A, message K and message F.
1955 : : *
1956 : : * @param spdm_context A pointer to the SPDM context.
1957 : : * @param spdm_session_info A pointer to the SPDM session context.
1958 : : * @param cert_chain_buffer Certificate chain buffer with spdm_cert_chain_t header.
1959 : : * @param cert_chain_buffer_size Size in bytes of the certificate chain buffer.
1960 : : * @param mut_cert_chain_buffer Certificate chain buffer with spdm_cert_chain_t header in mutual authentication.
1961 : : * @param mut_cert_chain_buffer_size Size in bytes of the certificate chain buffer in mutual authentication.
1962 : : * @param th_curr The managed buffer to store the TH data.
1963 : : *
1964 : : * @retval true current TH data is calculated.
1965 : : */
1966 : : bool libspdm_calculate_th_for_finish(libspdm_context_t *spdm_context,
1967 : : void *spdm_session_info,
1968 : : const uint8_t *cert_chain_buffer,
1969 : : size_t cert_chain_buffer_size,
1970 : : const uint8_t *mut_cert_chain_buffer,
1971 : : size_t mut_cert_chain_buffer_size,
1972 : : libspdm_th_managed_buffer_t *th_curr);
1973 : : #else
1974 : : /*
1975 : : * This function calculates current TH hash with message A, message K and message F.
1976 : : *
1977 : : * @param spdm_context A pointer to the SPDM context.
1978 : : * @param spdm_session_info A pointer to the SPDM session context.
1979 : : * @param th_hash_buffer_size Size in bytes of the th_hash_buffer
1980 : : * @param th_hash_buffer The buffer to store the th_hash_buffer
1981 : : *
1982 : : * @retval true current TH hash is calculated.
1983 : : */
1984 : : bool libspdm_calculate_th_hash_for_finish(libspdm_context_t *spdm_context,
1985 : : void *spdm_session_info,
1986 : : size_t *th_hash_buffer_size,
1987 : : void *th_hash_buffer);
1988 : :
1989 : : /*
1990 : : * This function calculates current TH hmac with message A, message K and message F, with response finished_key.
1991 : : *
1992 : : * @param spdm_context A pointer to the SPDM context.
1993 : : * @param spdm_session_info A pointer to the SPDM session context.
1994 : : * @param th_hmac_buffer_size Size in bytes of the th_hmac_buffer
1995 : : * @param th_hmac_buffer The buffer to store the th_hmac_buffer
1996 : : *
1997 : : * @retval true current TH hmac is calculated.
1998 : : */
1999 : : bool libspdm_calculate_th_hmac_for_finish_rsp(libspdm_context_t *spdm_context,
2000 : : void *spdm_session_info,
2001 : : size_t *th_hmac_buffer_size,
2002 : : void *th_hmac_buffer);
2003 : :
2004 : : /*
2005 : : * This function calculates current TH hmac with message A, message K and message F, with request finished_key.
2006 : : *
2007 : : * @param spdm_context A pointer to the SPDM context.
2008 : : * @param spdm_session_info A pointer to the SPDM session context.
2009 : : * @param th_hmac_buffer_size Size in bytes of the th_hmac_buffer
2010 : : * @param th_hmac_buffer The buffer to store the th_hmac_buffer
2011 : : *
2012 : : * @retval true current TH hmac is calculated.
2013 : : */
2014 : : bool libspdm_calculate_th_hmac_for_finish_req(libspdm_context_t *spdm_context,
2015 : : void *spdm_session_info,
2016 : : size_t *th_hmac_buffer_size,
2017 : : void *th_hmac_buffer);
2018 : : #endif
2019 : :
2020 : : /*
2021 : : * This function calculates th1 hash.
2022 : : *
2023 : : * @param spdm_context A pointer to the SPDM context.
2024 : : * @param spdm_session_info A pointer to the SPDM session context.
2025 : : * @param is_requester Indicate of the key generation for a requester or a responder.
2026 : : * @param th1_hash_data Th1 hash.
2027 : : *
2028 : : * @retval true th1 hash is calculated.
2029 : : */
2030 : : bool libspdm_calculate_th1_hash(libspdm_context_t *spdm_context,
2031 : : void *spdm_session_info,
2032 : : bool is_requester,
2033 : : uint8_t *th1_hash_data);
2034 : :
2035 : : /*
2036 : : * This function calculates th2 hash.
2037 : : *
2038 : : * @param spdm_context A pointer to the SPDM context.
2039 : : * @param spdm_session_info A pointer to the SPDM session context.
2040 : : * @param is_requester Indicate of the key generation for a requester or a responder.
2041 : : * @param th2_hash_data Th2 hash
2042 : : *
2043 : : * @retval true th2 hash is calculated.
2044 : : */
2045 : : bool libspdm_calculate_th2_hash(libspdm_context_t *spdm_context,
2046 : : void *spdm_session_info,
2047 : : bool is_requester,
2048 : : uint8_t *th2_hash_data);
2049 : :
2050 : : /**
2051 : : * Determine if bitmask has at most one bit set.
2052 : : *
2053 : : * @param mask The bitmask to be tested.
2054 : : *
2055 : : * @retval true At most one bit is set.
2056 : : * @retval false More than one bit is set.
2057 : : */
2058 : 1160 : static inline bool libspdm_onehot0(uint32_t mask)
2059 : : {
2060 [ + + + + ]: 1160 : return !mask || !(mask & (mask - 1));
2061 : : }
2062 : :
2063 : 24 : static inline uint64_t libspdm_byte_swap_64(uint64_t value)
2064 : : {
2065 : 24 : return (((value & 0x00000000000000ff) << 56) |
2066 : 24 : ((value & 0x000000000000ff00) << 40) |
2067 : 24 : ((value & 0x0000000000ff0000) << 24) |
2068 : 24 : ((value & 0x00000000ff000000) << 8) |
2069 : 24 : ((value & 0x000000ff00000000) >> 8) |
2070 : 24 : ((value & 0x0000ff0000000000) >> 24) |
2071 : 48 : ((value & 0x00ff000000000000) >> 40) |
2072 : 24 : ((value & 0xff00000000000000) >> 56));
2073 : : }
2074 : :
2075 : : static inline uint32_t libspdm_byte_swap_32(uint32_t value)
2076 : : {
2077 : : return ((value & 0x000000FF) << 24) |
2078 : : ((value & 0x0000FF00) << 8) |
2079 : : ((value & 0x00FF0000) >> 8) |
2080 : : ((value & 0xFF000000) >> 24);
2081 : : }
2082 : :
2083 : : static inline uint16_t libspdm_byte_swap_16(uint16_t value)
2084 : : {
2085 : : return ((value & 0x00FF) << 8) |
2086 : : ((value & 0xFF00) >> 8);
2087 : : }
2088 : :
2089 : : /**
2090 : : * Return capability flags that are masked by the negotiated SPDM version.
2091 : : *
2092 : : * @param spdm_context A pointer to the SPDM context.
2093 : : * @param is_request_flags If true then flags are from a request message or Requester.
2094 : : * If false then flags are from a response message or Responder.
2095 : : * @param flags A bitmask of capability flags.
2096 : : *
2097 : : * @return The masked capability flags.
2098 : : */
2099 : : uint32_t libspdm_mask_capability_flags(libspdm_context_t *spdm_context,
2100 : : bool is_request_flags, uint32_t flags);
2101 : :
2102 : : /**
2103 : : * Return capability extended flags that are masked by the negotiated SPDM version.
2104 : : *
2105 : : * @param spdm_context A pointer to the SPDM context.
2106 : : * @param is_request_flags If true then flags are from a request message or Requester.
2107 : : * If false then flags are from a response message or Responder.
2108 : : * @param ext_flags A bitmask of capability extended flags.
2109 : : *
2110 : : * @return The masked capability extended flags.
2111 : : */
2112 : : uint16_t libspdm_mask_capability_ext_flags(libspdm_context_t *spdm_context,
2113 : : bool is_request_flags, uint16_t ext_flags);
2114 : :
2115 : : /**
2116 : : * Return BaseHashAlgo that is masked by the negotiated SPDM version.
2117 : : *
2118 : : * @param spdm_context A pointer to the SPDM context.
2119 : : * @param base_hash_algo Unmasked BaseHashAlgo.
2120 : : *
2121 : : * @return The masked BaseHashAlgo.
2122 : : */
2123 : : uint32_t libspdm_mask_base_hash_algo(libspdm_context_t *spdm_context, uint32_t base_hash_algo);
2124 : :
2125 : : /**
2126 : : * Return MeasurementHashAlgo that is masked by the negotiated SPDM version.
2127 : : *
2128 : : * @param spdm_context A pointer to the SPDM context.
2129 : : * @param measurement_hash_algo Unmasked MeasurementHashAlgo.
2130 : : *
2131 : : * @return The masked MeasurementHashAlgo.
2132 : : */
2133 : : uint32_t libspdm_mask_measurement_hash_algo(libspdm_context_t *spdm_context,
2134 : : uint32_t measurement_hash_algo);
2135 : :
2136 : : /**
2137 : : * Return MeasurementSpecification that is masked by the negotiated SPDM version.
2138 : : *
2139 : : * @param measurement_specification Unmasked MeasurementSpecification.
2140 : : *
2141 : : * @return The masked MeasurementSpecification.
2142 : : */
2143 : : uint8_t libspdm_mask_measurement_specification(uint8_t measurement_specification);
2144 : :
2145 : : /**
2146 : : * Return MELspecification that is masked by the negotiated SPDM version.
2147 : : *
2148 : : * @param spdm_context A pointer to the SPDM context.
2149 : : * @param mel_specification Unmasked MELspecification.
2150 : : *
2151 : : * @return The masked MELspecification.
2152 : : */
2153 : : uint8_t libspdm_mask_mel_specification(libspdm_context_t *spdm_context, uint8_t mel_specification);
2154 : :
2155 : : /**
2156 : : * Return BaseAsymAlgo that is masked by the negotiated SPDM version.
2157 : : *
2158 : : * @param spdm_context A pointer to the SPDM context.
2159 : : * @param base_asym_algo Unmasked BaseAsymAlgo.
2160 : : *
2161 : : * @return The masked BaseAsymAlgo.
2162 : : */
2163 : : uint32_t libspdm_mask_base_asym_algo(libspdm_context_t *spdm_context, uint32_t base_asym_algo);
2164 : :
2165 : : /**
2166 : : * Returns the AlgSupported of an AlgStruct that is masked by the negotiated SPDM version.
2167 : : *
2168 : : * @param spdm_context A pointer to the SPDM context.
2169 : : * @param alg_type AlgType of the AlgStruct.
2170 : : * @param alg_supported Unmasked AlgSupported.
2171 : : *
2172 : : * @return The masked AlgSupported.
2173 : : */
2174 : : uint16_t libspdm_mask_alg_supported(libspdm_context_t *spdm_context, uint8_t alg_type,
2175 : : uint16_t alg_supported);
2176 : :
2177 : : /**
2178 : : * Check if the combination of SVH ID and VendorIDLen are legal.
2179 : : *
2180 : : * @param id Registry or standards body identifier (SPDM_REGISTRY_ID_*).
2181 : : * Its size is two bytes due to the vendor-defined messages.
2182 : : * @param vendor_id_len Length, in bytes, of the VendorID field.
2183 : : * @retval true The ID and VendorIDLen are legal.
2184 : : * @retval false The ID and VendorIDLen are illegal.
2185 : : */
2186 : : bool libspdm_validate_svh_vendor_id_len(uint16_t id, uint8_t vendor_id_len);
2187 : :
2188 : : /**
2189 : : * Map slot ID to key pair ID.
2190 : : *
2191 : : * @param spdm_context A pointer to the SPDM context.
2192 : : * @param slot_id The slot ID.
2193 : : * @param is_requester Indicate of the key generation for a requester or a responder.
2194 : : *
2195 : : * @return key pair ID.
2196 : : */
2197 : : uint8_t libspdm_slot_id_to_key_pair_id (
2198 : : void *spdm_context,
2199 : : uint8_t slot_id,
2200 : : bool is_requester);
2201 : :
2202 : : #if LIBSPDM_EVENT_RECIPIENT_SUPPORT
2203 : : /**
2204 : : * Check if the combination of DMTF EventTypeId and EventDetailLen is legal in a SEND_EVENT message.
2205 : : *
2206 : : * @param event_type_id Value of the DMTF EventTypeId.
2207 : : * @param event_detail_len Size, in bytes, of EventDetail.
2208 : : *
2209 : : * @retval true The EventTypeId and EventDetailLen are legal.
2210 : : * @retval false The EventTypeId and EventDetailLen are illegal.
2211 : : */
2212 : : bool libspdm_validate_dmtf_event_type(uint16_t event_type_id, uint16_t event_detail_len);
2213 : :
2214 : : /**
2215 : : * Given a list of events, finds the event identified by the target EventInstanceID.
2216 : : *
2217 : : * @param events_list_start Pointer to list of events.
2218 : : * @param event_count Number of events in the list.
2219 : : * @param target_event_instance_id EventInstanceID to be found.
2220 : : *
2221 : : * @retval NULL Could not find the EventInstanceID.
2222 : : * @retval non-NULL Pointer to the event corresponding to the target EventInstanceID
2223 : : */
2224 : : const void *libspdm_find_event_instance_id(const void *events_list_start, uint32_t event_count,
2225 : : uint32_t target_event_instance_id);
2226 : : /**
2227 : : * Parses and sends an event to the Integrator. This function shall not be called if the Integrator
2228 : : * has not registered an event handler via libspdm_register_event_callback.
2229 : : *
2230 : : * @param context A pointer to the SPDM context.
2231 : : * @param session_id Secure session identifier.
2232 : : * @param event_data A pointer to the event to be parsed and sent to Integrator.
2233 : : * @param next_event_data On output, returns a pointer to the next event in event_data.
2234 : : *
2235 : : * @retval true The event was successfully parsed and sent to the Integrator.
2236 : : * @retval false Unable to parse the event or the Integrator returned an error for the event.
2237 : : */
2238 : : bool libspdm_parse_and_send_event(libspdm_context_t *context, uint32_t session_id,
2239 : : const void *event_data, const void **next_event_data);
2240 : : #endif /* LIBSPDM_EVENT_RECIPIENT_SUPPORT */
2241 : :
2242 : : /**
2243 : : * Given a buffer that spans from ptr to end_ptr, check if ptr + increment is within the buffer.
2244 : : *
2245 : : * @retval true There is enough space in the buffer.
2246 : : * @retval false There is not enough space in the buffer.
2247 : : */
2248 : : bool libspdm_check_for_space(const uint8_t *ptr, const uint8_t *end_ptr, size_t increment);
2249 : :
2250 : : #endif /* SPDM_COMMON_LIB_INTERNAL_H */
|