Code coverage report for DMTF/libspdm
Current view: top level - include/internal - libspdm_common_lib.h (source / functions) Coverage Total Hit
Test: 36148b912f76d231c18d216d6dee5c0858575bb1 Lines: 100.0 % 11 11
Test Date: 2026-10-25 08:40:22 Functions: 100.0 % 2 2
Branches: 100.0 % 4 4

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

Generated by: LCOV version 2.0-1