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