LCOV - code coverage report
Current view: top level - include/internal - libspdm_common_lib.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 11 11
Test Date: 2026-01-18 08:10:21 Functions: 100.0 % 2 2

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

Generated by: LCOV version 2.0-1