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 : : /* My Certificate */
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 : : /* My 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 we do 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 : : libspdm_encap_flow_type_t flow_type;
508 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
509 : : bool response_not_ready;
510 : : libspdm_encap_flow_type_t response_not_ready_flow_type;
511 : : spdm_error_data_response_not_ready_t response_not_ready_data;
512 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
513 : : } libspdm_encap_context_t;
514 : :
515 : : typedef struct {
516 : : uint32_t session_id;
517 : : bool use_psk;
518 : : uint8_t mut_auth_requested;
519 : : uint8_t end_session_attributes;
520 : : uint8_t session_policy;
521 : : uint8_t heartbeat_period;
522 : : libspdm_session_transcript_t session_transcript;
523 : : /* Register for the last KEY_UPDATE token and operation (responder only)*/
524 : : spdm_key_update_request_t last_key_update_request;
525 : : void *secured_message_context;
526 : : /* Only present in session info as it is currently only used within a secure session. */
527 : : uint8_t local_used_cert_chain_slot_id;
528 : : uint8_t peer_used_cert_chain_slot_id;
529 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
530 : : libspdm_encap_context_t encap_context;
531 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
532 : : } libspdm_session_info_t;
533 : :
534 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
535 : : typedef struct {
536 : : bool chunk_in_use;
537 : : uint8_t chunk_handle;
538 : : /* The session that started the transfer. Only that session can continue it. */
539 : : bool session_id_valid;
540 : : uint32_t session_id;
541 : : uint32_t chunk_seq_no;
542 : : size_t chunk_bytes_transferred;
543 : :
544 : : void* large_message;
545 : : size_t large_message_size;
546 : : size_t large_message_capacity;
547 : : } libspdm_chunk_info_t;
548 : :
549 : : typedef struct {
550 : : libspdm_chunk_info_t send;
551 : : libspdm_chunk_info_t get;
552 : : } libspdm_chunk_context_t;
553 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
554 : :
555 : : #if LIBSPDM_ENABLE_MSG_LOG
556 : : typedef struct {
557 : : void *buffer;
558 : : size_t max_buffer_size;
559 : : uint32_t mode;
560 : : size_t buffer_size;
561 : : uint32_t status;
562 : : } libspdm_msg_log_t;
563 : : #endif /* LIBSPDM_ENABLE_MSG_LOG */
564 : :
565 : : #if LIBSPDM_FIPS_MODE
566 : : typedef struct {
567 : : /**
568 : : * Tested algo flag: 0 represents that the algo is not tested.
569 : : * See LIBSPDM_FIPS_SELF_TEST_xxx;
570 : : **/
571 : : uint32_t tested_algo;
572 : : /**
573 : : * Flag for the result of run algo self_test, 0 represents the result is failed.
574 : : * See LIBSPDM_FIPS_SELF_TEST_xxx;
575 : : **/
576 : : uint32_t self_test_result;
577 : : /**
578 : : * Buffer provided by integrator to hold large intermediate results.
579 : : **/
580 : : void *selftest_buffer;
581 : : size_t selftest_buffer_size;
582 : : } libspdm_fips_selftest_context_t;
583 : : #endif /* LIBSPDM_FIPS_MODE */
584 : :
585 : : #define LIBSPDM_CONTEXT_STRUCT_VERSION 0x3
586 : :
587 : : typedef struct {
588 : : uint32_t version;
589 : :
590 : : /* IO information */
591 : : libspdm_device_send_message_func send_message;
592 : : libspdm_device_receive_message_func receive_message;
593 : :
594 : : /*
595 : : * reserved for request and response in the main dispatch function in SPDM responder.
596 : : * this buffer is the transport message received from spdm_context->receive_message()
597 : : * or sent to spdm_context->send_message().
598 : : * This message may be SPDM transport message or secured SPDM transport message.
599 : : **/
600 : : libspdm_device_acquire_sender_buffer_func acquire_sender_buffer;
601 : : libspdm_device_release_sender_buffer_func release_sender_buffer;
602 : : libspdm_device_acquire_receiver_buffer_func acquire_receiver_buffer;
603 : : libspdm_device_release_receiver_buffer_func release_receiver_buffer;
604 : :
605 : : /* Transport Layer information */
606 : : libspdm_transport_encode_message_func transport_encode_message;
607 : : libspdm_transport_decode_message_func transport_decode_message;
608 : :
609 : : /* Cached plain text command
610 : : * If the command is cipher text, decrypt then cache it. */
611 : : void *last_spdm_request;
612 : : size_t last_spdm_request_size;
613 : :
614 : : /* Buffers used for data processing and transport. */
615 : : void *scratch_buffer;
616 : : size_t scratch_buffer_size;
617 : : void *sender_buffer;
618 : : size_t sender_buffer_size;
619 : : void *receiver_buffer;
620 : : size_t receiver_buffer_size;
621 : :
622 : : /* Cache session_id in this spdm_message, only valid for secured message. */
623 : : uint32_t last_spdm_request_session_id;
624 : : bool last_spdm_request_session_id_valid;
625 : :
626 : : /* Cache the error in libspdm_process_request. It is handled in libspdm_build_response. */
627 : : libspdm_error_struct_t last_spdm_error;
628 : :
629 : : /* Register GetResponse function (responder only) */
630 : : void *get_response_func;
631 : :
632 : : /* Register GetEncapResponse function (requester only) */
633 : : void *get_encap_response_func;
634 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
635 : : libspdm_encap_context_t encap_context;
636 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
637 : :
638 : : /* Register spdm_session_state_callback function (responder only)
639 : : * Register can know the state after StartSession / EndSession. */
640 : : void *spdm_session_state_callback;
641 : :
642 : : /* Register spdm_connection_state_callback function (responder only)
643 : : * Register can know the connection state such as negotiated. */
644 : : void *spdm_connection_state_callback;
645 : :
646 : : /* Register libspdm_key_update_callback function (responder only)
647 : : * Register can know when session keys are updated during KEY_UPDATE operations. */
648 : : void *spdm_key_update_callback;
649 : :
650 : : /* Callback function so that Integrator can specify encapsulated requests (responder only) */
651 : : void *encap_flow_handler_callback;
652 : :
653 : : libspdm_local_context_t local_context;
654 : :
655 : : libspdm_connection_info_t connection_info;
656 : : libspdm_transcript_t transcript;
657 : :
658 : : libspdm_session_info_t session_info[LIBSPDM_MAX_SESSION_COUNT];
659 : :
660 : : /* Cache latest session ID for HANDSHAKE_IN_THE_CLEAR */
661 : : uint32_t latest_session_id;
662 : :
663 : : /* Register for Responder state, be initial to Normal (responder only) */
664 : : libspdm_response_state_t response_state;
665 : :
666 : : /* Cached data for SPDM_ERROR_CODE_RESPONSE_NOT_READY/SPDM_RESPOND_IF_READY */
667 : : spdm_error_data_response_not_ready_t error_data;
668 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
669 : : void *cache_spdm_request;
670 : : size_t cache_spdm_request_size;
671 : : /* Session context of the original request that triggered the ResponseNotReady flow. The validity
672 : : * of the RESPOND_IF_READY request is defined by that original request, so the RESPOND_IF_READY
673 : : * must arrive in the same session context (same in-session/out-of-session, and if in a session
674 : : * the same session_id). */
675 : : bool cache_spdm_request_session_id_valid;
676 : : uint32_t cache_spdm_request_session_id;
677 : : #endif
678 : : uint8_t current_token;
679 : :
680 : : /* Register for the retry times when receive "BUSY" Error response (requester only) */
681 : : uint8_t retry_times;
682 : : /* Register for the delay time in microseconds between retry requests
683 : : * when receive "BUSY" Error response (requester only) */
684 : : uint64_t retry_delay_time;
685 : : bool crypto_request;
686 : :
687 : : /* App context data for use by application */
688 : : void *app_context_data_ptr;
689 : :
690 : : /* See LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY_*. */
691 : : uint8_t handle_error_return_policy;
692 : :
693 : : /* Max session count for DHE session and PSK session
694 : : * Set via LIBSPDM_DATA_MAX_DHE_SESSION_COUNT and LIBSPDM_DATA_MAX_PSK_SESSION_COUNT */
695 : : uint32_t max_dhe_session_count;
696 : : uint32_t max_psk_session_count;
697 : :
698 : : /* Current session count for DHE session and PSK session */
699 : : uint32_t current_dhe_session_count;
700 : : uint32_t current_psk_session_count;
701 : :
702 : : /* see LIBSPDM_DATA_MAX_SPDM_SESSION_SEQUENCE_NUMBER */
703 : : uint64_t max_spdm_session_sequence_number;
704 : :
705 : : uint8_t sequence_number_endian;
706 : :
707 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
708 : : /* Chunk specific context */
709 : : libspdm_chunk_context_t chunk_context;
710 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
711 : :
712 : : #if LIBSPDM_ENABLE_MSG_LOG
713 : : libspdm_msg_log_t msg_log;
714 : : #endif /* LIBSPDM_ENABLE_MSG_LOG */
715 : :
716 : : #if LIBSPDM_FIPS_MODE
717 : : libspdm_fips_selftest_context_t fips_selftest_context;
718 : : #endif /* LIBSPDM_FIPS_MODE */
719 : :
720 : : /* Endianness (BE/LE/Both) to use for signature verification on SPDM 1.0 and 1.1
721 : : * This field is ignored for other SPDM versions */
722 : : uint8_t spdm_10_11_verify_signature_endian;
723 : : /* The value set by the Integrator. Verification narrows BIG_OR_LITTLE to the endianness of the
724 : : * peer, so libspdm_reset_context restores spdm_10_11_verify_signature_endian from this. */
725 : : uint8_t spdm_10_11_verify_signature_endian_setting;
726 : :
727 : : #if LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES
728 : : libspdm_vendor_response_callback_func vendor_response_callback;
729 : : #endif /* LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES */
730 : :
731 : : #if LIBSPDM_EVENT_RECIPIENT_SUPPORT
732 : : libspdm_process_event_func process_event;
733 : : #endif /* LIBSPDM_EVENT_RECIPIENT_SUPPORT */
734 : :
735 : : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
736 : : libspdm_meas_log_reset_callback_func spdm_meas_log_reset_callback;
737 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
738 : : } libspdm_context_t;
739 : :
740 : : #define LIBSPDM_CONTEXT_SIZE_WITHOUT_SECURED_CONTEXT (sizeof(libspdm_context_t))
741 : : #define LIBSPDM_CONTEXT_SIZE_ALL (LIBSPDM_CONTEXT_SIZE_WITHOUT_SECURED_CONTEXT + \
742 : : LIBSPDM_SECURED_MESSAGE_CONTEXT_SIZE * LIBSPDM_MAX_SESSION_COUNT)
743 : :
744 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
745 : : /**
746 : : * Return the encapsulated context for the given session.
747 : : *
748 : : * Each secure session has its own encapsulated context, and there is one additional context for
749 : : * encapsulated flows that occur outside of a session.
750 : : *
751 : : * @param spdm_context A pointer to the SPDM context.
752 : : * @param session_id If NULL then the context outside of a session is returned. Otherwise the
753 : : * context for the given session is returned.
754 : : *
755 : : * @return The encapsulated context, or NULL if session_id does not identify a valid session.
756 : : **/
757 : : libspdm_encap_context_t *libspdm_get_encap_context(libspdm_context_t *spdm_context,
758 : : const uint32_t *session_id);
759 : :
760 : : /**
761 : : * Return the session that the encapsulated flow on the current channel belongs to.
762 : : *
763 : : * This is normally the session that the message currently being processed arrived on. When both
764 : : * endpoints have set HANDSHAKE_IN_THE_CLEAR_CAP the session-based mutual authentication flow is
765 : : * conducted outside of a session, but it still belongs to the session that KEY_EXCHANGE_RSP
766 : : * established, so that session is returned instead.
767 : : *
768 : : * @param spdm_context A pointer to the SPDM context.
769 : : *
770 : : * @return The session ID, or NULL if the flow is not associated with a session.
771 : : **/
772 : : const uint32_t *libspdm_get_encap_session_id_via_last_request(libspdm_context_t *spdm_context);
773 : :
774 : : /**
775 : : * Return the encapsulated context for the channel that the message currently being processed
776 : : * arrived on.
777 : : *
778 : : * If the message arrived within a session then that session's encapsulated context is returned,
779 : : * otherwise the context outside of a session is returned. See
780 : : * libspdm_get_encap_session_id_via_last_request for the handshake-in-the-clear exception.
781 : : *
782 : : * @param spdm_context A pointer to the SPDM context.
783 : : *
784 : : * @return The encapsulated context.
785 : : **/
786 : : libspdm_encap_context_t *libspdm_get_encap_context_via_last_request(
787 : : libspdm_context_t *spdm_context);
788 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
789 : :
790 : : #if LIBSPDM_DEBUG_PRINT_ENABLE
791 : : /**
792 : : * Return the request code name based on given request code.
793 : : *
794 : : * @param request_code The SPDM request code.
795 : : *
796 : : * @return request code name according to the request code.
797 : : **/
798 : : const char *libspdm_get_code_str(uint8_t request_code);
799 : :
800 : : #ifdef LIBSPDM_INTERNAL_DUMP_HEX_STR_OVERRIDE
801 : : extern void LIBSPDM_INTERNAL_DUMP_HEX_STR_OVERRIDE(const uint8_t *data, size_t size);
802 : : #define LIBSPDM_INTERNAL_DUMP_HEX_STR(data, size) LIBSPDM_INTERNAL_DUMP_HEX_STR_OVERRIDE(data, size)
803 : : #else
804 : : /**
805 : : * This function dump raw data.
806 : : *
807 : : * @param data raw data
808 : : * @param size raw data size
809 : : **/
810 : : void libspdm_internal_dump_hex_str(const uint8_t *data, size_t size);
811 : : #define LIBSPDM_INTERNAL_DUMP_HEX_STR(data, size) libspdm_internal_dump_hex_str(data, size)
812 : : #endif /* LIBSPDM_INTERNAL_DUMP_HEX_STR_OVERRIDE */
813 : :
814 : : #ifdef LIBSPDM_INTERNAL_DUMP_DATA_OVERRIDE
815 : : extern void LIBSPDM_INTERNAL_DUMP_DATA_OVERRIDE(const uint8_t *data, size_t size);
816 : : #define LIBSPDM_INTERNAL_DUMP_DATA(data, size) LIBSPDM_INTERNAL_DUMP_DATA_OVERRIDE(data, size)
817 : : #else
818 : : /**
819 : : * This function dump raw data.
820 : : *
821 : : * @param data raw data
822 : : * @param size raw data size
823 : : **/
824 : : void libspdm_internal_dump_data(const uint8_t *data, size_t size);
825 : : #define LIBSPDM_INTERNAL_DUMP_DATA(data, size) libspdm_internal_dump_data(data, size)
826 : : #endif /* LIBSPDM_INTERNAL_DUMP_DATA_OVERRIDE */
827 : :
828 : : #ifdef LIBSPDM_INTERNAL_DUMP_HEX_OVERRIDE
829 : : extern void LIBSPDM_INTERNAL_DUMP_HEX_OVERRIDE(const uint8_t *data, size_t size);
830 : : #define LIBSPDM_INTERNAL_DUMP_HEX(data, size) LIBSPDM_INTERNAL_DUMP_HEX_OVERRIDE(data, size)
831 : : #else
832 : : /**
833 : : * This function dump raw data with column format.
834 : : *
835 : : * @param data raw data
836 : : * @param size raw data size
837 : : **/
838 : : void libspdm_internal_dump_hex(const uint8_t *data, size_t size);
839 : : #define LIBSPDM_INTERNAL_DUMP_HEX(data, size) libspdm_internal_dump_hex(data, size)
840 : : #endif /* LIBSPDM_INTERNAL_DUMP_HEX_OVERRIDE */
841 : :
842 : : #else /* LIBSPDM_DEBUG_PRINT_ENABLE */
843 : : #define LIBSPDM_INTERNAL_DUMP_HEX(data, size)
844 : : #define LIBSPDM_INTERNAL_DUMP_HEX_STR(data, size)
845 : : #define LIBSPDM_INTERNAL_DUMP_DATA(data, size)
846 : : #endif /* LIBSPDM_DEBUG_PRINT_ENABLE */
847 : :
848 : : /* Required scratch buffer size for libspdm internal usage.
849 : : * It may be used to hold the encrypted/decrypted message and/or last sent/received message.
850 : : * It may be used to hold the large request/response and intermediate send/receive buffer
851 : : * in case of chunking.
852 : : *
853 : : * If chunking is not supported, it should be at least below.
854 : : * +--------------------------+-----------------+-----------------+
855 : : * | SENDER_RECEIVER |MAX_SPDM_MSG_SIZE|MAX_SPDM_MSG_SIZE|
856 : : * +--------------------------+-----------------+-----------------+
857 : : * |<- Secure msg ->|<-last request ->|<-cache request->|
858 : : *
859 : : *
860 : : * If chunking is supported, it should be at least below.
861 : : * +---------------+--------------+--------------------------+------------------------------+-----------------+-----------------+
862 : : * |SECURE_MESSAGE |LARGE_MESSAGE | SENDER_RECEIVER | LARGE SENDER_RECEIVER |MAX_SPDM_MSG_SIZE|MAX_SPDM_MSG_SIZE|
863 : : * +---------------+--------------+--------------------------+------------------------------+-----------------+-----------------+
864 : : * |<-Secure msg ->|<-Large msg ->|<-Snd/Rcv buf for chunk ->|<-Snd/Rcv buf for large msg ->|<-last request ->|<-cache request->|
865 : : *
866 : : * The cache request section is present only if LIBSPDM_RESPOND_IF_READY_SUPPORT is set.
867 : : *
868 : : * The value is configurable based on max_spdm_msg_size.
869 : : * The value MAY be changed in different libspdm version.
870 : : * It is exposed here, just in case the libspdm consumer wants to configure the setting at build time.
871 : : */
872 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
873 : : /* first section */
874 : : uint32_t libspdm_get_scratch_buffer_secure_message_offset(void);
875 : : uint32_t libspdm_get_scratch_buffer_secure_message_capacity(libspdm_context_t *spdm_context);
876 : :
877 : : /* second section */
878 : : uint32_t libspdm_get_scratch_buffer_large_message_offset(libspdm_context_t *spdm_context);
879 : : uint32_t libspdm_get_scratch_buffer_large_message_capacity(libspdm_context_t *spdm_context);
880 : : #endif
881 : :
882 : : /* third section */
883 : : uint32_t libspdm_get_scratch_buffer_sender_receiver_offset(libspdm_context_t *spdm_context);
884 : : uint32_t libspdm_get_scratch_buffer_sender_receiver_capacity(libspdm_context_t *spdm_context);
885 : :
886 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
887 : : /* fourth section */
888 : : uint32_t libspdm_get_scratch_buffer_large_sender_receiver_offset(libspdm_context_t *spdm_context);
889 : : uint32_t libspdm_get_scratch_buffer_large_sender_receiver_capacity(libspdm_context_t *spdm_context);
890 : : #endif
891 : :
892 : : /* fifth section */
893 : : uint32_t libspdm_get_scratch_buffer_last_spdm_request_offset(libspdm_context_t *spdm_context);
894 : : uint32_t libspdm_get_scratch_buffer_last_spdm_request_capacity(libspdm_context_t *spdm_context);
895 : :
896 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
897 : : /* sixth section */
898 : : uint32_t libspdm_get_scratch_buffer_cache_spdm_request_offset(libspdm_context_t *spdm_context);
899 : : uint32_t libspdm_get_scratch_buffer_cache_spdm_request_capacity(libspdm_context_t *spdm_context);
900 : : #endif
901 : :
902 : : /* combination */
903 : : uint32_t libspdm_get_scratch_buffer_capacity(libspdm_context_t *spdm_context);
904 : :
905 : : /**
906 : : * Append a new data buffer to the managed buffer.
907 : : *
908 : : * @param managed_buffer The managed buffer to be appended.
909 : : * @param buffer The address of the data buffer to be appended to the managed buffer.
910 : : * @param buffer_size The size in bytes of the data buffer to be appended to the managed buffer.
911 : : *
912 : : * @retval LIBSPDM_STATUS_SUCCESS The new data buffer is appended to the managed buffer.
913 : : * @retval LIBSPDM_STATUS_BUFFER_FULL The managed buffer is too small to be appended.
914 : : **/
915 : : libspdm_return_t libspdm_append_managed_buffer(void *managed_buffer,
916 : : const void *buffer, size_t buffer_size);
917 : :
918 : : /**
919 : : * Reset the managed buffer.
920 : : * The buffer_size is reset to 0.
921 : : * The max_buffer_size is unchanged.
922 : : * The buffer is not freed.
923 : : *
924 : : * @param managed_buffer The managed buffer.
925 : : **/
926 : : void libspdm_reset_managed_buffer(void *managed_buffer);
927 : :
928 : : /**
929 : : * Return the size of managed buffer.
930 : : *
931 : : * @param managed_buffer The managed buffer.
932 : : *
933 : : * @return the size of managed buffer.
934 : : **/
935 : : size_t libspdm_get_managed_buffer_size(void *managed_buffer);
936 : :
937 : : /**
938 : : * Return the address of managed buffer.
939 : : *
940 : : * @param managed_buffer The managed buffer.
941 : : *
942 : : * @return the address of managed buffer.
943 : : **/
944 : : void *libspdm_get_managed_buffer(void *managed_buffer);
945 : :
946 : : /**
947 : : * Init the managed buffer.
948 : : *
949 : : * @param managed_buffer The managed buffer.
950 : : * @param max_buffer_size The maximum size in bytes of the managed buffer.
951 : : **/
952 : : void libspdm_init_managed_buffer(void *managed_buffer, size_t max_buffer_size);
953 : :
954 : : /**
955 : : * Reset message buffer in SPDM context according to request code.
956 : : *
957 : : * @param context A pointer to the SPDM context.
958 : : * @param session_info A pointer to the SPDM session context.
959 : : * @param request_code The SPDM request code.
960 : : */
961 : : void libspdm_reset_message_buffer_via_request_code(void *context, void *session_info,
962 : : uint8_t request_code);
963 : :
964 : : /**
965 : : * Reset message buffer according to an encapsulated request code. Encapsulated-flow counterpart to
966 : : * libspdm_reset_message_buffer_via_request_code.
967 : : *
968 : : * @param context A pointer to the SPDM context.
969 : : * @param session_info A pointer to the SPDM session context.
970 : : * @param request_code The encapsulated SPDM request code.
971 : : */
972 : : void libspdm_reset_message_buffer_via_encap_request_code(void *context, void *session_info,
973 : : uint8_t request_code);
974 : :
975 : : /**
976 : : * This function initializes the session info.
977 : : *
978 : : * @param spdm_context A pointer to the SPDM context.
979 : : * @param session_id The SPDM session ID.
980 : : **/
981 : : void libspdm_session_info_init(libspdm_context_t *spdm_context,
982 : : libspdm_session_info_t *session_info,
983 : : uint32_t session_id, spdm_version_number_t secured_message_version,
984 : : bool use_psk);
985 : :
986 : : #if LIBSPDM_ENABLE_CAPABILITY_PSK_CAP
987 : : /**
988 : : * Set the psk_hint to a session info.
989 : : *
990 : : * @param session_info A pointer to a session info.
991 : : * @param psk_hint Indicate the PSK hint.
992 : : * @param psk_hint_size The size in bytes of the PSK hint.
993 : : */
994 : : void libspdm_session_info_set_psk_hint(libspdm_session_info_t *session_info,
995 : : const void *psk_hint,
996 : : size_t psk_hint_size);
997 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP */
998 : :
999 : : /**
1000 : : * This function returns if a given version is supported based upon the GET_VERSION/VERSION.
1001 : : *
1002 : : * @param spdm_context A pointer to the SPDM context.
1003 : : * @param version The SPDM version.
1004 : : *
1005 : : * @retval true the version is supported.
1006 : : * @retval false the version is not supported.
1007 : : **/
1008 : : bool libspdm_is_version_supported(const libspdm_context_t *spdm_context, uint8_t version);
1009 : :
1010 : : /**
1011 : : * This function returns connection version negotiated by GET_VERSION/VERSION.
1012 : : *
1013 : : * @param spdm_context A pointer to the SPDM context.
1014 : : *
1015 : : * @return the connection version.
1016 : : **/
1017 : : uint8_t libspdm_get_connection_version(const libspdm_context_t *spdm_context);
1018 : :
1019 : : /**
1020 : : * This function returns if a capabilities flag is supported in current SPDM connection.
1021 : : *
1022 : : * @param spdm_context A pointer to the SPDM context.
1023 : : * @param is_requester Is the function called from a requester.
1024 : : * @param requester_capabilities_flag The requester capabilities flag to be checked
1025 : : * @param responder_capabilities_flag The responder capabilities flag to be checked
1026 : : *
1027 : : * @retval true the capabilities flag is supported.
1028 : : * @retval false the capabilities flag is not supported.
1029 : : **/
1030 : : bool libspdm_is_capabilities_flag_supported(const libspdm_context_t *spdm_context,
1031 : : bool is_requester,
1032 : : uint32_t requester_capabilities_flag,
1033 : : uint32_t responder_capabilities_flag);
1034 : :
1035 : : /**
1036 : : * This function returns if a capabilities extended flag is supported in current SPDM connection.
1037 : : *
1038 : : * @param spdm_context A pointer to the SPDM context.
1039 : : * @param is_requester Is the function called from a requester.
1040 : : * @param requester_capabilities_ext_flag The requester capabilities extended flag to be checked
1041 : : * @param responder_capabilities_ext_flag The responder capabilities extended flag to be checked
1042 : : *
1043 : : * @retval true the capabilities extended flag is supported.
1044 : : * @retval false the capabilities extended flag is not supported.
1045 : : **/
1046 : : bool libspdm_is_capabilities_ext_flag_supported(const libspdm_context_t *spdm_context,
1047 : : bool is_requester,
1048 : : uint16_t requester_capabilities_ext_flag,
1049 : : uint16_t responder_capabilities_ext_flag);
1050 : :
1051 : : /**
1052 : : * Checks the negotiated SPDM version and endpoint capabilities to determine if encapsulated
1053 : : * messages are supported or not.
1054 : : *
1055 : : * @param spdm_context A pointer to the SPDM context.
1056 : : *
1057 : : * @retval true Both endpoints support encapsulated messages.
1058 : : * @retval false At least one endpoint does not support encapsulated messages.
1059 : : **/
1060 : : bool libspdm_is_encap_supported(const libspdm_context_t *spdm_context);
1061 : :
1062 : : /**
1063 : : * This function generates the certificate chain hash.
1064 : : *
1065 : : * @param spdm_context A pointer to the SPDM context.
1066 : : * @param slot_id The slot index of the certificate chain.
1067 : : * @param hash The buffer to store the certificate chain hash.
1068 : : *
1069 : : * @retval true certificate chain hash is generated.
1070 : : * @retval false certificate chain hash is not generated.
1071 : : **/
1072 : : bool libspdm_generate_cert_chain_hash(libspdm_context_t *spdm_context,
1073 : : size_t slot_id, uint8_t *hash);
1074 : :
1075 : : /**
1076 : : * This function generates the public key hash.
1077 : : *
1078 : : * @param spdm_context A pointer to the SPDM context.
1079 : : * @param hash The buffer to store the public key hash.
1080 : : *
1081 : : * @retval true public key hash is generated.
1082 : : * @retval false public key hash is not generated.
1083 : : **/
1084 : : bool libspdm_generate_public_key_hash(libspdm_context_t *spdm_context,
1085 : : uint8_t *hash);
1086 : :
1087 : : /**
1088 : : * This function verifies the integrity of peer certificate chain buffer including
1089 : : * spdm_cert_chain_t header.
1090 : : *
1091 : : * @param spdm_context A pointer to the SPDM context.
1092 : : * @param cert_chain_buffer Certificate chain buffer including spdm_cert_chain_t header.
1093 : : * @param cert_chain_buffer_size size in bytes of the certificate chain buffer.
1094 : : *
1095 : : * @retval true Peer certificate chain buffer integrity verification passed.
1096 : : * @retval false Peer certificate chain buffer integrity verification failed.
1097 : : **/
1098 : : bool libspdm_verify_peer_cert_chain_buffer_integrity(libspdm_context_t *spdm_context,
1099 : : const void *cert_chain_buffer,
1100 : : size_t cert_chain_buffer_size);
1101 : :
1102 : : /**
1103 : : * This function verifies peer certificate chain authority.
1104 : : *
1105 : : * @param spdm_context A pointer to the SPDM context.
1106 : : * @param cert_chain_buffer Certificate chain buffer including spdm_cert_chain_t header.
1107 : : * @param cert_chain_buffer_size size in bytes of the certificate chain buffer.
1108 : : * @param trust_anchor A buffer to hold the trust_anchor which is used to validate the peer certificate, if not NULL.
1109 : : * @param trust_anchor_size A buffer to hold the trust_anchor_size, if not NULL.
1110 : : *
1111 : : * @retval true Peer certificate chain buffer authority verification passed.
1112 : : * Or there is no root_cert in local_context.
1113 : : * @retval false Peer certificate chain buffer authority verification failed.
1114 : : **/
1115 : : bool libspdm_verify_peer_cert_chain_buffer_authority(libspdm_context_t *spdm_context,
1116 : : const void *cert_chain_buffer,
1117 : : size_t cert_chain_buffer_size,
1118 : : const void **trust_anchor,
1119 : : size_t *trust_anchor_size);
1120 : : /**
1121 : : * This function generates the challenge signature based upon m1m2 for authentication.
1122 : : *
1123 : : * @param spdm_context A pointer to the SPDM context.
1124 : : * @param is_requester Indicate of the signature generation for a requester or a responder.
1125 : : * @param signature The buffer to store the challenge signature.
1126 : : *
1127 : : * @retval true challenge signature is generated.
1128 : : * @retval false challenge signature is not generated.
1129 : : **/
1130 : : bool libspdm_generate_challenge_auth_signature(libspdm_context_t *spdm_context,
1131 : : bool is_requester,
1132 : : uint8_t slot_id,
1133 : : uint8_t *signature);
1134 : :
1135 : : /**
1136 : : * This function verifies the certificate chain hash.
1137 : : *
1138 : : * @param spdm_context A pointer to the SPDM context.
1139 : : * @param certificate_chain_hash The certificate chain hash data buffer.
1140 : : * @param certificate_chain_hash_size size in bytes of the certificate chain hash data buffer.
1141 : : *
1142 : : * @retval true hash verification pass.
1143 : : * @retval false hash verification fail.
1144 : : **/
1145 : : bool libspdm_verify_certificate_chain_hash(libspdm_context_t *spdm_context,
1146 : : uint8_t slot_id,
1147 : : const void *certificate_chain_hash,
1148 : : size_t certificate_chain_hash_size);
1149 : :
1150 : : /**
1151 : : * This function verifies the public key hash.
1152 : : *
1153 : : * @param spdm_context A pointer to the SPDM context.
1154 : : * @param public_key_hash The public key hash data buffer.
1155 : : * @param public_key_hash_size size in bytes of the public key hash data buffer.
1156 : : *
1157 : : * @retval true hash verification pass.
1158 : : * @retval false hash verification fail.
1159 : : **/
1160 : : bool libspdm_verify_public_key_hash(libspdm_context_t *spdm_context,
1161 : : const void *public_key_hash,
1162 : : size_t public_key_hash_size);
1163 : :
1164 : : /**
1165 : : * This function verifies the challenge signature based upon m1m2.
1166 : : *
1167 : : * @param spdm_context A pointer to the SPDM context.
1168 : : * @param is_requester Indicate of the signature verification for a requester or a responder.
1169 : : * @param sign_data The signature data buffer.
1170 : : * @param sign_data_size size in bytes of the signature data buffer.
1171 : : *
1172 : : * @retval true signature verification pass.
1173 : : * @retval false signature verification fail.
1174 : : **/
1175 : : bool libspdm_verify_challenge_auth_signature(libspdm_context_t *spdm_context,
1176 : : bool is_requester,
1177 : : uint8_t slot_id,
1178 : : const void *sign_data,
1179 : : size_t sign_data_size);
1180 : :
1181 : : /**
1182 : : * This function calculate the measurement summary hash size.
1183 : : *
1184 : : * @param spdm_context A pointer to the SPDM context.
1185 : : * @param is_requester Is the function called from a requester.
1186 : : * @param measurement_summary_hash_type The type of the measurement summary hash.
1187 : : *
1188 : : * @return 0 measurement summary hash type is invalid, NO_MEAS hash type or no MEAS capabilities.
1189 : : * @return measurement summary hash size according to type.
1190 : : **/
1191 : : uint32_t libspdm_get_measurement_summary_hash_size(libspdm_context_t *spdm_context,
1192 : : bool is_requester,
1193 : : uint8_t measurement_summary_hash_type);
1194 : :
1195 : : /**
1196 : : * This function generates the endpoint info signature based upon il1il2 for authentication.
1197 : : *
1198 : : * @param spdm_context A pointer to the SPDM context.
1199 : : * @param session_info A pointer to the SPDM session context.
1200 : : * @param is_requester Indicate of the signature generation for a requester or a responder.
1201 : : * @param signature The buffer to store the endpoint info signature.
1202 : : *
1203 : : * @retval true endpoint info signature is generated.
1204 : : * @retval false endpoint info signature is not generated.
1205 : : **/
1206 : : bool libspdm_generate_endpoint_info_signature(libspdm_context_t *spdm_context,
1207 : : libspdm_session_info_t *session_info,
1208 : : bool is_requester,
1209 : : uint8_t slot_id,
1210 : : uint8_t *signature);
1211 : :
1212 : : /**
1213 : : * This function verifies the endpoint info signature based upon il1il2.
1214 : : *
1215 : : * @param spdm_context A pointer to the SPDM context.
1216 : : * @param session_info A pointer to the SPDM session context.
1217 : : * @param is_requester Indicate of the signature verification for a requester or a responder.
1218 : : * @param sign_data The signature data buffer.
1219 : : * @param sign_data_size size in bytes of the signature data buffer.
1220 : : *
1221 : : * @retval true signature verification pass.
1222 : : * @retval false signature verification fail.
1223 : : **/
1224 : : bool libspdm_verify_endpoint_info_signature(libspdm_context_t *spdm_context,
1225 : : libspdm_session_info_t *session_info,
1226 : : bool is_requester,
1227 : : uint8_t slot_id,
1228 : : const void *sign_data,
1229 : : size_t sign_data_size);
1230 : :
1231 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
1232 : : /**
1233 : : * Free the cached public key of the peer's leaf certificate in a slot, if there is one.
1234 : : *
1235 : : * The key is freed with the negotiated algorithm that it was parsed with, so this must be called
1236 : : * before the negotiated algorithms change.
1237 : : *
1238 : : * @param context A pointer to the SPDM context.
1239 : : * @param slot_id The slot of the peer certificate chain.
1240 : : **/
1241 : : void libspdm_free_peer_leaf_cert_public_key(libspdm_context_t *context, uint8_t slot_id);
1242 : : #endif /* !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) */
1243 : :
1244 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1245 : : /*
1246 : : * This function calculates l1l2.
1247 : : * If session_info is NULL, this function will use M cache of SPDM context,
1248 : : * else will use M cache of SPDM session context.
1249 : : *
1250 : : * @param spdm_context A pointer to the SPDM context.
1251 : : * @param session_info A pointer to the SPDM session context.
1252 : : * @param l1l2 The buffer to store the l1l2.
1253 : : *
1254 : : * @retval true l1l2 is calculated.
1255 : : */
1256 : : bool libspdm_calculate_l1l2(libspdm_context_t *spdm_context,
1257 : : void *session_info,
1258 : : libspdm_l1l2_managed_buffer_t *l1l2);
1259 : : #else
1260 : : /*
1261 : : * This function calculates l1l2 hash.
1262 : : * If session_info is NULL, this function will use M cache of SPDM context,
1263 : : * else will use M cache of SPDM session context.
1264 : : *
1265 : : * @param spdm_context A pointer to the SPDM context.
1266 : : * @param session_info A pointer to the SPDM session context.
1267 : : * @param l1l2_hash_size size in bytes of the l1l2 hash
1268 : : * @param l1l2_hash The buffer to store the l1l2 hash
1269 : : *
1270 : : * @retval true l1l2 is calculated.
1271 : : */
1272 : : bool libspdm_calculate_l1l2_hash(libspdm_context_t *spdm_context,
1273 : : void *session_info,
1274 : : size_t *l1l2_hash_size, void *l1l2_hash);
1275 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
1276 : :
1277 : : /**
1278 : : * Get element from multi element opaque data by element id and element index
1279 : : *
1280 : : * @param[in] spdm_context A pointer to the SPDM context.
1281 : : * @param[in] data_in_size Size of multi element opaque data.
1282 : : * @param[in] data_in A pointer to the multi element opaque data.
1283 : : * @param[in] element_id Element id.
1284 : : * @param[in] element_index Element index, start from 0.
1285 : : * It is used to get the nth element with the same element id.
1286 : : * @param[out] total_matched_element_cnt Pointer to store total matched element count with the same element id.
1287 : : * @param[out] get_element_ptr Pointer to store found element.
1288 : : * @param[out] get_element_len Pointer to length of found element.
1289 : : *
1290 : : * @retval true Get element successfully
1291 : : * @retval false Get element failed
1292 : : **/
1293 : : bool libspdm_get_element_from_opaque_data_with_element_id (libspdm_context_t *spdm_context,
1294 : : size_t data_in_size, const void *data_in,
1295 : : uint8_t element_id, uint8_t element_index,
1296 : : uint8_t *total_matched_element_cnt,
1297 : : const void **get_element_ptr, size_t *get_element_len);
1298 : :
1299 : : /**
1300 : : * Get secured message opaque data element from multi element opaque data by sm data id
1301 : : *
1302 : : * This function should be called in
1303 : : * libspdm_process_opaque_data_supported_version_data/libspdm_process_opaque_data_version_selection_data.
1304 : : *
1305 : : * @param[in] spdm_context A pointer to the SPDM context.
1306 : : * @param[in] data_in_size Size of multi element opaque data.
1307 : : * @param[in] data_in A pointer to the multi element opaque data.
1308 : : * @param[in] sm_data_id ID for the Secured Message data type.
1309 : : * @param[out] get_element_ptr Pointer to store found element.
1310 : : * @param[out] get_element_len Pointer to length of found element.
1311 : : *
1312 : : * @retval true Get element successfully
1313 : : * @retval false Get element failed
1314 : : **/
1315 : : bool libspdm_get_sm_data_element_from_opaque_data (libspdm_context_t *spdm_context,
1316 : : size_t data_in_size, const void *data_in,
1317 : : uint8_t sm_data_id,
1318 : : const void **get_element_ptr, size_t *get_element_len);
1319 : :
1320 : : /**
1321 : : * Process opaque data version selection.
1322 : : *
1323 : : * This function should be called in KEY_EXCHANGE/PSK_EXCHANGE response parsing in requester.
1324 : : *
1325 : : * @param data_in_size Size in bytes of the data_in.
1326 : : * @param data_in A pointer to the buffer to store the opaque data version selection.
1327 : : **/
1328 : : libspdm_return_t libspdm_process_opaque_data_version_selection_data(
1329 : : libspdm_context_t *spdm_context, size_t data_in_size, void *data_in,
1330 : : spdm_version_number_t *secured_message_version);
1331 : :
1332 : : /**
1333 : : * Process general opaque data check
1334 : : *
1335 : : * @param data_in_size size in bytes of the data_in.
1336 : : * @param data_in A pointer to the opaque data to check.
1337 : : *
1338 : : * @retval true check opaque data successfully
1339 : : * @retval false check opaque data failed
1340 : : **/
1341 : : bool libspdm_process_general_opaque_data_check(libspdm_context_t *spdm_context,
1342 : : size_t data_in_size,
1343 : : const void *data_in);
1344 : :
1345 : : /**
1346 : : * Return the size in bytes of opaque data supported version.
1347 : : *
1348 : : * This function should be called in libspdm_process_opaque_data_supported_version_data.
1349 : : *
1350 : : * @param version_count Secure version count.
1351 : : *
1352 : : * @return The size in bytes of opaque data supported version.
1353 : : **/
1354 : : size_t libspdm_get_untrusted_opaque_data_supported_version_data_size(
1355 : : libspdm_context_t *spdm_context, uint8_t version_count);
1356 : :
1357 : : /**
1358 : : * Return the size in bytes of opaque data supported version.
1359 : : *
1360 : : * This function should be called in KEY_EXCHANGE/PSK_EXCHANGE request generation.
1361 : : *
1362 : : * @return the size in bytes of opaque data supported version.
1363 : : **/
1364 : : size_t libspdm_get_opaque_data_supported_version_data_size(libspdm_context_t *spdm_context);
1365 : :
1366 : : /**
1367 : : * Return the size in bytes of opaque data version selection.
1368 : : *
1369 : : * This function should be called in KEY_EXCHANGE/PSK_EXCHANGE response generation.
1370 : : *
1371 : : * @return the size in bytes of opaque data version selection.
1372 : : **/
1373 : : size_t libspdm_get_opaque_data_version_selection_data_size(const libspdm_context_t *spdm_context);
1374 : :
1375 : : /**
1376 : : * Return the highest secured message version this endpoint offers in its local supported list, in
1377 : : * version-number form (e.g. SECURED_SPDM_VERSION_13 << SPDM_VERSION_NUMBER_SHIFT_BIT), or 0 if the
1378 : : * list is empty.
1379 : : *
1380 : : * @param spdm_context A pointer to the SPDM context.
1381 : : *
1382 : : * @return the highest locally-offered secured message version (version-number form).
1383 : : **/
1384 : : spdm_version_number_t libspdm_local_max_secured_message_version(
1385 : : const libspdm_context_t *spdm_context);
1386 : :
1387 : : /**
1388 : : * Return the size in bytes of the DSP0277 1.3 AEAD limit opaque element when appended on its own,
1389 : : * i.e. the size of a single secured-message opaque element carrying the AEADlimitOE data, with
1390 : : * padding. This is the incremental size that the AEADlimitOE element adds to an opaque data blob
1391 : : * that already has a general opaque data table header and at least one other element.
1392 : : *
1393 : : * The AEADlimitOE element is only defined for secured message version 1.3 and later; for an older
1394 : : * secured_message_version this returns 0.
1395 : : *
1396 : : * @param spdm_context A pointer to the SPDM context.
1397 : : * @param secured_message_version The secured message version this opaque data targets (the
1398 : : * negotiated version for a response, or the highest locally-offered
1399 : : * version for a request).
1400 : : *
1401 : : * @return the size in bytes of the AEAD limit opaque element (0 if the version is older than 1.3).
1402 : : **/
1403 : : size_t libspdm_get_opaque_data_aead_limit_element_size(const libspdm_context_t *spdm_context,
1404 : : spdm_version_number_t
1405 : : secured_message_version);
1406 : :
1407 : : /**
1408 : : * Build the DSP0277 1.3 AEAD limit opaque element (AEADlimitOE) into an opaque data blob that was
1409 : : * built by libspdm_build_opaque_data_supported_version_data (requester) or
1410 : : * libspdm_build_opaque_data_version_selection_data (responder). The general opaque data table
1411 : : * header's total_elements is incremented and the new element is written after the existing
1412 : : * element(s).
1413 : : *
1414 : : * The AEADlimitOE element is only defined for secured message version 1.3 and later; for an older
1415 : : * secured_message_version this is a no-op (nothing is added and *data_out_size is set to the size
1416 : : * of the existing blob).
1417 : : *
1418 : : * @param spdm_context A pointer to the SPDM context.
1419 : : * @param secured_message_version The secured message version this opaque data targets.
1420 : : * @param data_out_size On input, the size in bytes of the data_out buffer. On output,
1421 : : * the total size in bytes of the opaque data blob.
1422 : : * @param data_out The opaque data blob to extend in place.
1423 : : **/
1424 : : void libspdm_build_opaque_data_aead_limit_element(libspdm_context_t *spdm_context,
1425 : : spdm_version_number_t secured_message_version,
1426 : : size_t *data_out_size, void *data_out);
1427 : :
1428 : : /**
1429 : : * Parse the DSP0277 1.3 AEAD limit opaque element (AEADlimitOE) from received opaque data.
1430 : : *
1431 : : * The AEADlimitOE element is only defined for secured message version 1.3 and later. When the
1432 : : * negotiated secured message version is older, the element is ignored (treated as absent) even if
1433 : : * a peer included it, and the default exponent (64) is returned.
1434 : : *
1435 : : * @param spdm_context A pointer to the SPDM context.
1436 : : * @param secured_message_version The negotiated secured message version (version-number form).
1437 : : * @param data_in_size size in bytes of data_in.
1438 : : * @param data_in The received opaque data.
1439 : : * @param aead_limit_exponent On output, the peer's AeadLimitExponent. If the element is absent
1440 : : * or ignored, this is set to the default (64).
1441 : : *
1442 : : * @retval LIBSPDM_STATUS_SUCCESS parsed successfully (or absent/ignored -> default).
1443 : : * @retval LIBSPDM_STATUS_INVALID_MSG_FIELD the element is malformed or the exponent is > 64.
1444 : : **/
1445 : : libspdm_return_t libspdm_process_opaque_data_aead_limit(libspdm_context_t *spdm_context,
1446 : : spdm_version_number_t
1447 : : secured_message_version,
1448 : : size_t data_in_size, const void *data_in,
1449 : : uint8_t *aead_limit_exponent);
1450 : :
1451 : : /**
1452 : : * Apply the negotiated AEAD limit to a session's secured-message context, setting the maximum
1453 : : * allowed session sequence number to min(integrator-configured cap, (2 ^ effective_exponent) - 1),
1454 : : * where effective_exponent = min(local exponent, peer exponent) and exponent 64 maps to the
1455 : : * all-ones max. Only takes effect when the session's secured message version is >= 1.3.
1456 : : *
1457 : : * @param spdm_context A pointer to the SPDM context.
1458 : : * @param session_info The session to configure.
1459 : : * @param peer_aead_limit_exponent The peer's AeadLimitExponent (64 if not advertised).
1460 : : **/
1461 : : void libspdm_apply_aead_limit_to_session(libspdm_context_t *spdm_context,
1462 : : void *session_info,
1463 : : uint8_t peer_aead_limit_exponent);
1464 : :
1465 : : /**
1466 : : * Return the SPDMversion field of the version number struct.
1467 : : *
1468 : : * @param ver Spdm version number struct.
1469 : : *
1470 : : * @return the SPDMversion of the version number struct.
1471 : : **/
1472 : : uint8_t libspdm_get_version_from_version_number(spdm_version_number_t ver);
1473 : :
1474 : : /**
1475 : : * Sort SPDMversion in descending order.
1476 : : *
1477 : : * @param ver_set A pointer to the version set.
1478 : : * @param ver_num Version number.
1479 : : */
1480 : : void libspdm_version_number_sort(spdm_version_number_t *ver_set, size_t ver_num);
1481 : :
1482 : : /**
1483 : : * Negotiate SPDMversion for connection.
1484 : : * req_ver_set is the local version set of requester, res_ver_set is the version set of responder.
1485 : : *
1486 : : * @param common_version A pointer to store the common version.
1487 : : * @param req_ver_set A pointer to the requester version set.
1488 : : * @param req_ver_num Version number of requester.
1489 : : * @param res_ver_set A pointer to the responder version set.
1490 : : * @param res_ver_num Version number of responder.
1491 : : *
1492 : : * @retval true Negotiation successfully, connect version be saved to common_version.
1493 : : * @retval false Negotiation failed.
1494 : : */
1495 : : bool libspdm_negotiate_connection_version(spdm_version_number_t *common_version,
1496 : : spdm_version_number_t *req_ver_set,
1497 : : size_t req_ver_num,
1498 : : const spdm_version_number_t *res_ver_set,
1499 : : size_t res_ver_num);
1500 : :
1501 : : /**
1502 : : * Acquire a device sender buffer for transport layer message.
1503 : : *
1504 : : * @param spdm_context A pointer to the SPDM context.
1505 : : * @param max_msg_size size in bytes of the maximum size of sender buffer.
1506 : : * @param msg_buf_ptr A pointer to a sender buffer.
1507 : : *
1508 : : * @retval LIBSPDM_STATUS_SUCCESS The sender buffer is acquired.
1509 : : **/
1510 : : libspdm_return_t libspdm_acquire_sender_buffer (
1511 : : libspdm_context_t *spdm_context, size_t *max_msg_size, void **msg_buf_ptr);
1512 : :
1513 : : /**
1514 : : * Release a device sender buffer for transport layer message.
1515 : : *
1516 : : * @param spdm_context A pointer to the SPDM context.
1517 : : **/
1518 : : void libspdm_release_sender_buffer (libspdm_context_t *spdm_context);
1519 : :
1520 : : /**
1521 : : * Get the sender buffer.
1522 : : *
1523 : : * @param spdm_context A pointer to the SPDM context.
1524 : : * @param sender_buffer Buffer address of the sender buffer.
1525 : : * @param sender_buffer_size Size of the sender buffer.
1526 : : *
1527 : : **/
1528 : : void libspdm_get_sender_buffer (
1529 : : libspdm_context_t *spdm_context,
1530 : : void **sender_buffer,
1531 : : size_t *sender_buffer_size);
1532 : :
1533 : : /**
1534 : : * Acquire a device receiver buffer for transport layer message.
1535 : : *
1536 : : * @param spdm_context A pointer to the SPDM context.
1537 : : * @param max_msg_size size in bytes of the maximum size of receiver buffer.
1538 : : * @param msg_buf_ptr A pointer to a receiver buffer.
1539 : : *
1540 : : * @retval LIBSPDM_STATUS_SUCCESS The receiver buffer is acquired.
1541 : : **/
1542 : : libspdm_return_t libspdm_acquire_receiver_buffer (
1543 : : libspdm_context_t *spdm_context, size_t *max_msg_size, void **msg_buf_ptr);
1544 : :
1545 : : /**
1546 : : * Release a device receiver buffer for transport layer message.
1547 : : *
1548 : : * @param spdm_context A pointer to the SPDM context.
1549 : : **/
1550 : : void libspdm_release_receiver_buffer (libspdm_context_t *spdm_context);
1551 : :
1552 : : /**
1553 : : * Get the receiver buffer.
1554 : : *
1555 : : * @param spdm_context A pointer to the SPDM context.
1556 : : * @param receiver_buffer Buffer address of the receiver buffer.
1557 : : * @param receiver_buffer_size Size of the receiver buffer.
1558 : : *
1559 : : **/
1560 : : void libspdm_get_receiver_buffer (
1561 : : libspdm_context_t *spdm_context,
1562 : : void **receiver_buffer,
1563 : : size_t *receiver_buffer_size);
1564 : :
1565 : : /**
1566 : : * Get the certificate slot mask
1567 : : *
1568 : : * @param[in] spdm_context A pointer to the SPDM context.
1569 : : *
1570 : : * @return slot_mask get slot mask
1571 : : **/
1572 : : uint8_t libspdm_get_cert_slot_mask (libspdm_context_t *spdm_context);
1573 : :
1574 : : /**
1575 : : * Get the certificate slot count
1576 : : *
1577 : : * @param[in] spdm_context A pointer to the SPDM context.
1578 : : *
1579 : : * @return slot_count get slot count
1580 : : **/
1581 : : uint8_t libspdm_get_cert_slot_count(libspdm_context_t *spdm_context);
1582 : :
1583 : : #if LIBSPDM_ENABLE_MSG_LOG
1584 : : void libspdm_append_msg_log(libspdm_context_t *spdm_context, void *message, size_t message_size);
1585 : : #endif
1586 : :
1587 : : /**
1588 : : * Reset message A cache in SPDM context.
1589 : : *
1590 : : * @param spdm_context A pointer to the SPDM context.
1591 : : **/
1592 : : void libspdm_reset_message_a(libspdm_context_t *spdm_context);
1593 : :
1594 : : /**
1595 : : * Reset message D cache in SPDM context.
1596 : : *
1597 : : * @param spdm_context A pointer to the SPDM context.
1598 : : **/
1599 : : void libspdm_reset_message_d(libspdm_context_t *spdm_context);
1600 : :
1601 : : /**
1602 : : * Reset message B cache in SPDM context.
1603 : : *
1604 : : * @param spdm_context A pointer to the SPDM context.
1605 : : **/
1606 : : void libspdm_reset_message_b(libspdm_context_t *spdm_context);
1607 : :
1608 : : /**
1609 : : * Reset message C cache in SPDM context.
1610 : : *
1611 : : * @param spdm_context A pointer to the SPDM context.
1612 : : **/
1613 : : void libspdm_reset_message_c(libspdm_context_t *spdm_context);
1614 : :
1615 : : /**
1616 : : * Reset message MutB cache in SPDM context.
1617 : : *
1618 : : * @param spdm_context A pointer to the SPDM context.
1619 : : **/
1620 : : void libspdm_reset_message_mut_b(libspdm_context_t *spdm_context);
1621 : :
1622 : : /**
1623 : : * Reset message MutC cache in SPDM context.
1624 : : *
1625 : : * @param spdm_context A pointer to the SPDM context.
1626 : : **/
1627 : : void libspdm_reset_message_mut_c(libspdm_context_t *spdm_context);
1628 : :
1629 : : /**
1630 : : * Reset message M cache in SPDM context.
1631 : : * If session_info is NULL, this function will use M cache of SPDM context,
1632 : : * else will use M cache of SPDM session context.
1633 : : *
1634 : : * @param spdm_context A pointer to the SPDM context.
1635 : : * @param session_info A pointer to the SPDM session context.
1636 : : **/
1637 : : void libspdm_reset_message_m(libspdm_context_t *spdm_context, void *session_info);
1638 : :
1639 : : /**
1640 : : * Reset message K cache in SPDM context.
1641 : : *
1642 : : * @param spdm_context A pointer to the SPDM context.
1643 : : * @param spdm_session_info A pointer to the SPDM session context.
1644 : : **/
1645 : : void libspdm_reset_message_k(libspdm_context_t *spdm_context, void *spdm_session_info);
1646 : :
1647 : : /**
1648 : : * Reset message EncapD cache in SPDM context.
1649 : : *
1650 : : * @param spdm_session_info A pointer to the SPDM session context.
1651 : : **/
1652 : : void libspdm_reset_message_encap_d(void *spdm_session_info);
1653 : :
1654 : : /**
1655 : : * Reset message F cache in SPDM context.
1656 : : *
1657 : : * @param spdm_context A pointer to the SPDM context.
1658 : : * @param spdm_session_info A pointer to the SPDM session context.
1659 : : **/
1660 : : void libspdm_reset_message_f(libspdm_context_t *spdm_context, void *spdm_session_info);
1661 : :
1662 : : /**
1663 : : * Reset message E cache in SPDM context.
1664 : : * If session_info is NULL, this function will use E cache of SPDM context,
1665 : : * else will use E cache of SPDM session context.
1666 : : *
1667 : : * @param spdm_context A pointer to the SPDM context.
1668 : : * @param session_info A pointer to the SPDM session context.
1669 : : **/
1670 : : void libspdm_reset_message_e(libspdm_context_t *spdm_context, void *session_info);
1671 : :
1672 : : /**
1673 : : * Reset message encap E cache in SPDM context.
1674 : : * If session_info is NULL, this function will use encap E cache of SPDM context,
1675 : : * else will use encap E cache of SPDM session context.
1676 : : *
1677 : : * @param spdm_context A pointer to the SPDM context.
1678 : : * @param session_info A pointer to the SPDM session context.
1679 : : **/
1680 : : void libspdm_reset_message_encap_e(libspdm_context_t *spdm_context, void *session_info);
1681 : :
1682 : : /**
1683 : : * Append message A cache in SPDM context.
1684 : : *
1685 : : * @param spdm_context A pointer to the SPDM context.
1686 : : * @param message Message buffer.
1687 : : * @param message_size Size in bytes of message buffer.
1688 : : *
1689 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1690 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1691 : : **/
1692 : : libspdm_return_t libspdm_append_message_a(libspdm_context_t *spdm_context, const void *message,
1693 : : size_t message_size);
1694 : :
1695 : : /**
1696 : : * Append message D cache in SPDM context.
1697 : : *
1698 : : * @param spdm_context A pointer to the SPDM context.
1699 : : * @param message Message buffer.
1700 : : * @param message_size Size in bytes of message buffer.
1701 : : *
1702 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1703 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1704 : : **/
1705 : : libspdm_return_t libspdm_append_message_d(libspdm_context_t *spdm_context, const void *message,
1706 : : size_t message_size);
1707 : :
1708 : : /**
1709 : : * Append message B cache in SPDM context.
1710 : : *
1711 : : * @param spdm_context A pointer to the SPDM context.
1712 : : * @param message Message buffer.
1713 : : * @param message_size Size in bytes of message buffer.
1714 : : *
1715 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1716 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1717 : : **/
1718 : : libspdm_return_t libspdm_append_message_b(libspdm_context_t *spdm_context, const void *message,
1719 : : size_t message_size);
1720 : :
1721 : : /**
1722 : : * Append message C cache in SPDM context.
1723 : : *
1724 : : * @param spdm_context A pointer to the SPDM context.
1725 : : * @param message Message buffer.
1726 : : * @param message_size Size in bytes of message buffer.
1727 : : *
1728 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1729 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1730 : : **/
1731 : : libspdm_return_t libspdm_append_message_c(libspdm_context_t *spdm_context, const void *message,
1732 : : size_t message_size);
1733 : :
1734 : : /**
1735 : : * Append message MutB cache in SPDM context.
1736 : : *
1737 : : * @param spdm_context A pointer to the SPDM context.
1738 : : * @param message Message buffer.
1739 : : * @param message_size Size in bytes of message buffer.
1740 : : *
1741 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1742 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1743 : : **/
1744 : : libspdm_return_t libspdm_append_message_mut_b(libspdm_context_t *spdm_context, const void *message,
1745 : : size_t message_size);
1746 : :
1747 : : /**
1748 : : * Append message MutC cache in SPDM context.
1749 : : *
1750 : : * @param spdm_context A pointer to the SPDM context.
1751 : : * @param message Message buffer.
1752 : : * @param message_size Size in bytes of message buffer.
1753 : : *
1754 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1755 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1756 : : **/
1757 : : libspdm_return_t libspdm_append_message_mut_c(libspdm_context_t *spdm_context, const void *message,
1758 : : size_t message_size);
1759 : :
1760 : : /**
1761 : : * Append message M cache in SPDM context.
1762 : : * If session_info is NULL, this function will use M cache of SPDM context,
1763 : : * else will use M cache of SPDM session context.
1764 : : *
1765 : : * @param spdm_context A pointer to the SPDM context.
1766 : : * @param session_info A pointer to the SPDM session context.
1767 : : * @param message Message buffer.
1768 : : * @param message_size Size in bytes of message buffer.
1769 : : *
1770 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1771 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1772 : : **/
1773 : : libspdm_return_t libspdm_append_message_m(libspdm_context_t *spdm_context,
1774 : : void *session_info,
1775 : : const void *message, size_t message_size);
1776 : :
1777 : : /**
1778 : : * Append message K cache in SPDM context.
1779 : : *
1780 : : * @param spdm_context A pointer to the SPDM context.
1781 : : * @param spdm_session_info A pointer to the SPDM session context.
1782 : : * @param is_requester Indicate of the key generation for a requester or a responder.
1783 : : * @param message Message buffer.
1784 : : * @param message_size Size in bytes of message buffer.
1785 : : *
1786 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1787 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1788 : : **/
1789 : : libspdm_return_t libspdm_append_message_k(libspdm_context_t *spdm_context,
1790 : : void *spdm_session_info,
1791 : : bool is_requester, const void *message,
1792 : : size_t message_size);
1793 : :
1794 : : /**
1795 : : * Append message EncapD cache in SPDM context.
1796 : : *
1797 : : * @param spdm_session_info A pointer to the SPDM session context.
1798 : : * @param message Message buffer.
1799 : : * @param message_size Size in bytes of message buffer.
1800 : : *
1801 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1802 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1803 : : **/
1804 : : libspdm_return_t libspdm_append_message_encap_d(void *spdm_session_info,
1805 : : const void *message,
1806 : : size_t message_size);
1807 : :
1808 : : /**
1809 : : * Append message F cache in SPDM context.
1810 : : *
1811 : : * @param spdm_context A pointer to the SPDM context.
1812 : : * @param spdm_session_info A pointer to the SPDM session context.
1813 : : * @param is_requester Indicate of the key generation for a requester or a responder.
1814 : : * @param message Message buffer.
1815 : : * @param message_size Size in bytes of message buffer.
1816 : : *
1817 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1818 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1819 : : **/
1820 : : libspdm_return_t libspdm_append_message_f(libspdm_context_t *spdm_context,
1821 : : void *spdm_session_info,
1822 : : bool is_requester, const void *message,
1823 : : size_t message_size);
1824 : :
1825 : : /**
1826 : : * Append message E cache in SPDM context.
1827 : : * If session_info is NULL, this function will use E cache of SPDM context,
1828 : : * else will use E cache of SPDM session context.
1829 : : *
1830 : : * @param spdm_context A pointer to the SPDM context.
1831 : : * @param session_info A pointer to the SPDM session context.
1832 : : * @param message message buffer.
1833 : : * @param message_size size in bytes of message buffer.
1834 : : *
1835 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1836 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1837 : : **/
1838 : : libspdm_return_t libspdm_append_message_e(libspdm_context_t *spdm_context, void *session_info,
1839 : : const void *message, size_t message_size);
1840 : :
1841 : : /**
1842 : : * Append message encap E cache in SPDM context.
1843 : : * If session_info is NULL, this function will use encap E cache of SPDM context,
1844 : : * else will use encap E cache of SPDM session context.
1845 : : *
1846 : : * @param spdm_context A pointer to the SPDM context.
1847 : : * @param session_info A pointer to the SPDM session context.
1848 : : * @param message message buffer.
1849 : : * @param message_size size in bytes of message buffer.
1850 : : *
1851 : : * @retval LIBSPDM_STATUS_SUCCESS message is appended.
1852 : : * @retval LIBSPDM_STATUS_BUFFER_FULL message is not appended because the internal cache is full.
1853 : : **/
1854 : : libspdm_return_t libspdm_append_message_encap_e(libspdm_context_t *spdm_context, void *session_info,
1855 : : const void *message, size_t message_size);
1856 : :
1857 : : /**
1858 : : * This function generates a session ID by concatenating req_session_id and rsp_session_id.
1859 : : *
1860 : : * @param[in] req_session_id
1861 : : * @param[in] rsp_session_id
1862 : : *
1863 : : * @return Session ID.
1864 : : **/
1865 : : uint32_t libspdm_generate_session_id(uint16_t req_session_id, uint16_t rsp_session_id);
1866 : :
1867 : : /**
1868 : : * This function assigns a new session ID.
1869 : : *
1870 : : * @param spdm_context A pointer to the SPDM context.
1871 : : * @param session_id The SPDM session ID.
1872 : : *
1873 : : * @return session info associated with this new session ID.
1874 : : **/
1875 : : libspdm_session_info_t *libspdm_assign_session_id(libspdm_context_t *spdm_context,
1876 : : uint32_t session_id,
1877 : : spdm_version_number_t secured_message_version,
1878 : : bool use_psk);
1879 : :
1880 : : /**
1881 : : * This function frees a session ID.
1882 : : *
1883 : : * @param spdm_context A pointer to the SPDM context.
1884 : : * @param session_id The SPDM session ID.
1885 : : **/
1886 : : void libspdm_free_session_id(libspdm_context_t *spdm_context, uint32_t session_id);
1887 : :
1888 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
1889 : : /**
1890 : : * This function ends a chunk transfer and clears its buffer.
1891 : : *
1892 : : * @param chunk_info The chunk transfer to reset.
1893 : : **/
1894 : : void libspdm_reset_chunk_info(libspdm_chunk_info_t *chunk_info);
1895 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
1896 : :
1897 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1898 : : /*
1899 : : * This function calculates current TH data with message A and message K.
1900 : : *
1901 : : * @param spdm_context A pointer to the SPDM context.
1902 : : * @param spdm_session_info A pointer to the SPDM session context.
1903 : : * @param cert_chain_buffer Certificate chain buffer with spdm_cert_chain_t header.
1904 : : * @param cert_chain_buffer_size Size in bytes of the certificate chain buffer.
1905 : : * @param th_curr The managed buffer to store the TH data.
1906 : : *
1907 : : * @retval true current TH data is calculated.
1908 : : */
1909 : : bool libspdm_calculate_th_for_exchange(
1910 : : libspdm_context_t *spdm_context, void *spdm_session_info,
1911 : : const uint8_t *cert_chain_buffer, size_t cert_chain_buffer_size,
1912 : : libspdm_th_managed_buffer_t *th_curr);
1913 : : #else
1914 : : /*
1915 : : * This function calculates current TH hash with message A and message K.
1916 : : *
1917 : : * @param spdm_context A pointer to the SPDM context.
1918 : : * @param spdm_session_info A pointer to the SPDM session context.
1919 : : * @param th_hash_buffer_size Size in bytes of the th_hash_buffer
1920 : : * @param th_hash_buffer The buffer to store the th_hash_buffer
1921 : : *
1922 : : * @retval true current TH hash is calculated.
1923 : : */
1924 : : bool libspdm_calculate_th_hash_for_exchange(
1925 : : libspdm_context_t *spdm_context, void *spdm_session_info,
1926 : : size_t *th_hash_buffer_size, void *th_hash_buffer);
1927 : :
1928 : : /*
1929 : : * This function calculates current TH hmac with message A and message K, with response finished_key.
1930 : : *
1931 : : * @param spdm_context A pointer to the SPDM context.
1932 : : * @param spdm_session_info A pointer to the SPDM session context.
1933 : : * @param th_hmac_buffer_size Size in bytes of the th_hmac_buffer
1934 : : * @param th_hmac_buffer The buffer to store the th_hmac_buffer
1935 : : *
1936 : : * @retval true current TH hmac is calculated.
1937 : : */
1938 : : bool libspdm_calculate_th_hmac_for_exchange_rsp(
1939 : : libspdm_context_t *spdm_context, void *spdm_session_info,
1940 : : size_t *th_hmac_buffer_size, void *th_hmac_buffer);
1941 : : #endif
1942 : :
1943 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1944 : : /*
1945 : : * This function calculates current TH data with message A, message K and message F.
1946 : : *
1947 : : * @param spdm_context A pointer to the SPDM context.
1948 : : * @param spdm_session_info A pointer to the SPDM session context.
1949 : : * @param cert_chain_buffer Certificate chain buffer with spdm_cert_chain_t header.
1950 : : * @param cert_chain_buffer_size Size in bytes of the certificate chain buffer.
1951 : : * @param mut_cert_chain_buffer Certificate chain buffer with spdm_cert_chain_t header in mutual authentication.
1952 : : * @param mut_cert_chain_buffer_size Size in bytes of the certificate chain buffer in mutual authentication.
1953 : : * @param th_curr The managed buffer to store the TH data.
1954 : : *
1955 : : * @retval true current TH data is calculated.
1956 : : */
1957 : : bool libspdm_calculate_th_for_finish(libspdm_context_t *spdm_context,
1958 : : void *spdm_session_info,
1959 : : const uint8_t *cert_chain_buffer,
1960 : : size_t cert_chain_buffer_size,
1961 : : const uint8_t *mut_cert_chain_buffer,
1962 : : size_t mut_cert_chain_buffer_size,
1963 : : libspdm_th_managed_buffer_t *th_curr);
1964 : : #else
1965 : : /*
1966 : : * This function calculates current TH hash with message A, message K and message F.
1967 : : *
1968 : : * @param spdm_context A pointer to the SPDM context.
1969 : : * @param spdm_session_info A pointer to the SPDM session context.
1970 : : * @param th_hash_buffer_size Size in bytes of the th_hash_buffer
1971 : : * @param th_hash_buffer The buffer to store the th_hash_buffer
1972 : : *
1973 : : * @retval true current TH hash is calculated.
1974 : : */
1975 : : bool libspdm_calculate_th_hash_for_finish(libspdm_context_t *spdm_context,
1976 : : void *spdm_session_info,
1977 : : size_t *th_hash_buffer_size,
1978 : : void *th_hash_buffer);
1979 : :
1980 : : /*
1981 : : * This function calculates current TH hmac with message A, message K and message F, with response finished_key.
1982 : : *
1983 : : * @param spdm_context A pointer to the SPDM context.
1984 : : * @param spdm_session_info A pointer to the SPDM session context.
1985 : : * @param th_hmac_buffer_size Size in bytes of the th_hmac_buffer
1986 : : * @param th_hmac_buffer The buffer to store the th_hmac_buffer
1987 : : *
1988 : : * @retval true current TH hmac is calculated.
1989 : : */
1990 : : bool libspdm_calculate_th_hmac_for_finish_rsp(libspdm_context_t *spdm_context,
1991 : : void *spdm_session_info,
1992 : : size_t *th_hmac_buffer_size,
1993 : : void *th_hmac_buffer);
1994 : :
1995 : : /*
1996 : : * This function calculates current TH hmac with message A, message K and message F, with request finished_key.
1997 : : *
1998 : : * @param spdm_context A pointer to the SPDM context.
1999 : : * @param spdm_session_info A pointer to the SPDM session context.
2000 : : * @param th_hmac_buffer_size Size in bytes of the th_hmac_buffer
2001 : : * @param th_hmac_buffer The buffer to store the th_hmac_buffer
2002 : : *
2003 : : * @retval true current TH hmac is calculated.
2004 : : */
2005 : : bool libspdm_calculate_th_hmac_for_finish_req(libspdm_context_t *spdm_context,
2006 : : void *spdm_session_info,
2007 : : size_t *th_hmac_buffer_size,
2008 : : void *th_hmac_buffer);
2009 : : #endif
2010 : :
2011 : : /*
2012 : : * This function calculates th1 hash.
2013 : : *
2014 : : * @param spdm_context A pointer to the SPDM context.
2015 : : * @param spdm_session_info A pointer to the SPDM session context.
2016 : : * @param is_requester Indicate of the key generation for a requester or a responder.
2017 : : * @param th1_hash_data Th1 hash.
2018 : : *
2019 : : * @retval true th1 hash is calculated.
2020 : : */
2021 : : bool libspdm_calculate_th1_hash(libspdm_context_t *spdm_context,
2022 : : void *spdm_session_info,
2023 : : bool is_requester,
2024 : : uint8_t *th1_hash_data);
2025 : :
2026 : : /*
2027 : : * This function calculates th2 hash.
2028 : : *
2029 : : * @param spdm_context A pointer to the SPDM context.
2030 : : * @param spdm_session_info A pointer to the SPDM session context.
2031 : : * @param is_requester Indicate of the key generation for a requester or a responder.
2032 : : * @param th2_hash_data Th2 hash
2033 : : *
2034 : : * @retval true th2 hash is calculated.
2035 : : */
2036 : : bool libspdm_calculate_th2_hash(libspdm_context_t *spdm_context,
2037 : : void *spdm_session_info,
2038 : : bool is_requester,
2039 : : uint8_t *th2_hash_data);
2040 : :
2041 : : /**
2042 : : * Reads a 24-bit value from memory that may be unaligned.
2043 : : *
2044 : : * @param buffer The pointer to a 24-bit value that may be unaligned.
2045 : : *
2046 : : * @return The 24-bit value read from buffer.
2047 : : **/
2048 : : uint32_t libspdm_read_uint24(const uint8_t *buffer);
2049 : :
2050 : : /**
2051 : : * Writes a 24-bit value to memory that may be unaligned.
2052 : : *
2053 : : * @param buffer The pointer to a 24-bit value that may be unaligned.
2054 : : * @param value 24-bit value to write to buffer.
2055 : : **/
2056 : : void libspdm_write_uint24(uint8_t *buffer, uint32_t value);
2057 : :
2058 : : /**
2059 : : * Reads a 16-bit value from memory that may be unaligned.
2060 : : *
2061 : : * @param buffer The pointer to a 16-bit value that may be unaligned.
2062 : : *
2063 : : * @return The 16-bit value read from buffer.
2064 : : **/
2065 : : uint16_t libspdm_read_uint16(const uint8_t *buffer);
2066 : :
2067 : : /**
2068 : : * Writes a 16-bit value to memory that may be unaligned.
2069 : : *
2070 : : * @param buffer The pointer to a 16-bit value that may be unaligned.
2071 : : * @param value 16-bit value to write to buffer.
2072 : : **/
2073 : : void libspdm_write_uint16(uint8_t *buffer, uint16_t value);
2074 : :
2075 : : /**
2076 : : * Reads a 32-bit value from memory that may be unaligned.
2077 : : *
2078 : : * @param buffer The pointer to a 32-bit value that may be unaligned.
2079 : : *
2080 : : * @return The 32-bit value read from buffer.
2081 : : **/
2082 : : uint32_t libspdm_read_uint32(const uint8_t *buffer);
2083 : :
2084 : : /**
2085 : : * Writes a 32-bit value to memory that may be unaligned.
2086 : : *
2087 : : * @param buffer The pointer to a 32-bit value that may be unaligned.
2088 : : * @param value 32-bit value to write to buffer.
2089 : : **/
2090 : : void libspdm_write_uint32(uint8_t *buffer, uint32_t value);
2091 : :
2092 : : /**
2093 : : * Reads a 64-bit value from memory that may be unaligned.
2094 : : *
2095 : : * @param buffer The pointer to a 64-bit value that may be unaligned.
2096 : : *
2097 : : * @return The 64-bit value read from buffer.
2098 : : **/
2099 : : uint64_t libspdm_read_uint64(const uint8_t *buffer);
2100 : :
2101 : : /**
2102 : : * Writes a 64-bit value to memory that may be unaligned.
2103 : : *
2104 : : * @param buffer The pointer to a 64-bit value that may be unaligned.
2105 : : * @param value 64-bit value to write to buffer.
2106 : : **/
2107 : : void libspdm_write_uint64(uint8_t *buffer, uint64_t value);
2108 : :
2109 : : /**
2110 : : * Determine if bitmask has at most one bit set.
2111 : : *
2112 : : * @param mask The bitmask to be tested.
2113 : : *
2114 : : * @retval true At most one bit is set.
2115 : : * @retval false More than one bit is set.
2116 : : */
2117 : 1152 : static inline bool libspdm_onehot0(uint32_t mask)
2118 : : {
2119 [ + + + + ]: 1152 : return !mask || !(mask & (mask - 1));
2120 : : }
2121 : :
2122 : 24 : static inline uint64_t libspdm_byte_swap_64(uint64_t value)
2123 : : {
2124 : 24 : return (((value & 0x00000000000000ff) << 56) |
2125 : 24 : ((value & 0x000000000000ff00) << 40) |
2126 : 24 : ((value & 0x0000000000ff0000) << 24) |
2127 : 24 : ((value & 0x00000000ff000000) << 8) |
2128 : 24 : ((value & 0x000000ff00000000) >> 8) |
2129 : 24 : ((value & 0x0000ff0000000000) >> 24) |
2130 : 48 : ((value & 0x00ff000000000000) >> 40) |
2131 : 24 : ((value & 0xff00000000000000) >> 56));
2132 : : }
2133 : :
2134 : : static inline uint32_t libspdm_byte_swap_32(uint32_t value)
2135 : : {
2136 : : return ((value & 0x000000FF) << 24) |
2137 : : ((value & 0x0000FF00) << 8) |
2138 : : ((value & 0x00FF0000) >> 8) |
2139 : : ((value & 0xFF000000) >> 24);
2140 : : }
2141 : :
2142 : : static inline uint16_t libspdm_byte_swap_16(uint16_t value)
2143 : : {
2144 : : return ((value & 0x00FF) << 8) |
2145 : : ((value & 0xFF00) >> 8);
2146 : : }
2147 : :
2148 : : /**
2149 : : * Return capability flags that are masked by the negotiated SPDM version.
2150 : : *
2151 : : * @param spdm_context A pointer to the SPDM context.
2152 : : * @param is_request_flags If true then flags are from a request message or Requester.
2153 : : * If false then flags are from a response message or Responder.
2154 : : * @param flags A bitmask of capability flags.
2155 : : *
2156 : : * @return The masked capability flags.
2157 : : */
2158 : : uint32_t libspdm_mask_capability_flags(libspdm_context_t *spdm_context,
2159 : : bool is_request_flags, uint32_t flags);
2160 : :
2161 : : /**
2162 : : * Return capability extended flags that are masked by the negotiated SPDM version.
2163 : : *
2164 : : * @param spdm_context A pointer to the SPDM context.
2165 : : * @param is_request_flags If true then flags are from a request message or Requester.
2166 : : * If false then flags are from a response message or Responder.
2167 : : * @param ext_flags A bitmask of capability extended flags.
2168 : : *
2169 : : * @return The masked capability extended flags.
2170 : : */
2171 : : uint16_t libspdm_mask_capability_ext_flags(libspdm_context_t *spdm_context,
2172 : : bool is_request_flags, uint16_t ext_flags);
2173 : :
2174 : : /**
2175 : : * Return BaseHashAlgo that is masked by the negotiated SPDM version.
2176 : : *
2177 : : * @param spdm_context A pointer to the SPDM context.
2178 : : * @param base_hash_algo Unmasked BaseHashAlgo.
2179 : : *
2180 : : * @return The masked BaseHashAlgo.
2181 : : */
2182 : : uint32_t libspdm_mask_base_hash_algo(libspdm_context_t *spdm_context, uint32_t base_hash_algo);
2183 : :
2184 : : /**
2185 : : * Return MeasurementHashAlgo that is masked by the negotiated SPDM version.
2186 : : *
2187 : : * @param spdm_context A pointer to the SPDM context.
2188 : : * @param measurement_hash_algo Unmasked MeasurementHashAlgo.
2189 : : *
2190 : : * @return The masked MeasurementHashAlgo.
2191 : : */
2192 : : uint32_t libspdm_mask_measurement_hash_algo(libspdm_context_t *spdm_context,
2193 : : uint32_t measurement_hash_algo);
2194 : :
2195 : : /**
2196 : : * Return MeasurementSpecification that is masked by the negotiated SPDM version.
2197 : : *
2198 : : * @param measurement_specification Unmasked MeasurementSpecification.
2199 : : *
2200 : : * @return The masked MeasurementSpecification.
2201 : : */
2202 : : uint8_t libspdm_mask_measurement_specification(uint8_t measurement_specification);
2203 : :
2204 : : /**
2205 : : * Return MELspecification that is masked by the negotiated SPDM version.
2206 : : *
2207 : : * @param spdm_context A pointer to the SPDM context.
2208 : : * @param mel_specification Unmasked MELspecification.
2209 : : *
2210 : : * @return The masked MELspecification.
2211 : : */
2212 : : uint8_t libspdm_mask_mel_specification(libspdm_context_t *spdm_context, uint8_t mel_specification);
2213 : :
2214 : : /**
2215 : : * Return BaseAsymAlgo that is masked by the negotiated SPDM version.
2216 : : *
2217 : : * @param spdm_context A pointer to the SPDM context.
2218 : : * @param base_asym_algo Unmasked BaseAsymAlgo.
2219 : : *
2220 : : * @return The masked BaseAsymAlgo.
2221 : : */
2222 : : uint32_t libspdm_mask_base_asym_algo(libspdm_context_t *spdm_context, uint32_t base_asym_algo);
2223 : :
2224 : : /**
2225 : : * Returns the AlgSupported of an AlgStruct that is masked by the negotiated SPDM version.
2226 : : *
2227 : : * @param spdm_context A pointer to the SPDM context.
2228 : : * @param alg_type AlgType of the AlgStruct.
2229 : : * @param alg_supported Unmasked AlgSupported.
2230 : : *
2231 : : * @return The masked AlgSupported.
2232 : : */
2233 : : uint16_t libspdm_mask_alg_supported(libspdm_context_t *spdm_context, uint8_t alg_type,
2234 : : uint16_t alg_supported);
2235 : :
2236 : : /**
2237 : : * Check if the combination of SVH ID and VendorIDLen are legal.
2238 : : *
2239 : : * @param id Registry or standards body identifier (SPDM_REGISTRY_ID_*).
2240 : : * Its size is two bytes due to the vendor-defined messages.
2241 : : * @param vendor_id_len Length, in bytes, of the VendorID field.
2242 : : * @retval true The ID and VendorIDLen are legal.
2243 : : * @retval false The ID and VendorIDLen are illegal.
2244 : : */
2245 : : bool libspdm_validate_svh_vendor_id_len(uint16_t id, uint8_t vendor_id_len);
2246 : :
2247 : : /**
2248 : : * Map slot ID to key pair ID.
2249 : : *
2250 : : * @param spdm_context A pointer to the SPDM context.
2251 : : * @param slot_id The slot ID.
2252 : : * @param is_requester Indicate of the key generation for a requester or a responder.
2253 : : *
2254 : : * @return key pair ID.
2255 : : */
2256 : : uint8_t libspdm_slot_id_to_key_pair_id (
2257 : : void *spdm_context,
2258 : : uint8_t slot_id,
2259 : : bool is_requester);
2260 : :
2261 : : #if LIBSPDM_EVENT_RECIPIENT_SUPPORT
2262 : : /**
2263 : : * Check if the combination of DMTF EventTypeId and EventDetailLen is legal in a SEND_EVENT message.
2264 : : *
2265 : : * @param event_type_id Value of the DMTF EventTypeId.
2266 : : * @param event_detail_len Size, in bytes, of EventDetail.
2267 : : *
2268 : : * @retval true The EventTypeId and EventDetailLen are legal.
2269 : : * @retval false The EventTypeId and EventDetailLen are illegal.
2270 : : */
2271 : : bool libspdm_validate_dmtf_event_type(uint16_t event_type_id, uint16_t event_detail_len);
2272 : :
2273 : : /**
2274 : : * Given a list of events, finds the event identified by the target EventInstanceID.
2275 : : *
2276 : : * @param events_list_start Pointer to list of events.
2277 : : * @param event_count Number of events in the list.
2278 : : * @param target_event_instance_id EventInstanceID to be found.
2279 : : *
2280 : : * @retval NULL Could not find the EventInstanceID.
2281 : : * @retval non-NULL Pointer to the event corresponding to the target EventInstanceID
2282 : : */
2283 : : const void *libspdm_find_event_instance_id(const void *events_list_start, uint32_t event_count,
2284 : : uint32_t target_event_instance_id);
2285 : : /**
2286 : : * Parses and sends an event to the Integrator. This function shall not be called if the Integrator
2287 : : * has not registered an event handler via libspdm_register_event_callback.
2288 : : *
2289 : : * @param context A pointer to the SPDM context.
2290 : : * @param session_id Secure session identifier.
2291 : : * @param event_data A pointer to the event to be parsed and sent to Integrator.
2292 : : * @param next_event_data On output, returns a pointer to the next event in event_data.
2293 : : *
2294 : : * @retval true The event was successfully parsed and sent to the Integrator.
2295 : : * @retval false Unable to parse the event or the Integrator returned an error for the event.
2296 : : */
2297 : : bool libspdm_parse_and_send_event(libspdm_context_t *context, uint32_t session_id,
2298 : : const void *event_data, const void **next_event_data);
2299 : : #endif /* LIBSPDM_EVENT_RECIPIENT_SUPPORT */
2300 : :
2301 : : /**
2302 : : * Given a buffer that spans from ptr to end_ptr, check if ptr + increment is within the buffer.
2303 : : *
2304 : : * @retval true There is enough space in the buffer.
2305 : : * @retval false There is not enough space in the buffer.
2306 : : */
2307 : : bool libspdm_check_for_space(const uint8_t *ptr, const uint8_t *end_ptr, size_t increment);
2308 : :
2309 : : #endif /* SPDM_COMMON_LIB_INTERNAL_H */
|