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