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