LCOV - code coverage report
Current view: top level - unit_test/test_spdm_responder - psk_finish_rsp.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 731 731
Test Date: 2026-02-22 08:11:49 Functions: 100.0 % 17 17

            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              : #include "spdm_unit_test.h"
       8              : #include "internal/libspdm_responder_lib.h"
       9              : #include "internal/libspdm_secured_message_lib.h"
      10              : 
      11              : #if LIBSPDM_ENABLE_CAPABILITY_PSK_CAP
      12              : #pragma pack(1)
      13              : typedef struct {
      14              :     spdm_message_header_t header;
      15              :     uint8_t verify_data[LIBSPDM_MAX_HASH_SIZE];
      16              : } libspdm_psk_finish_request_mine_t;
      17              : 
      18              : typedef struct {
      19              :     spdm_message_header_t header;
      20              :     uint16_t opaque_data_size;
      21              :     uint8_t opaque_data[8];
      22              :     uint8_t verify_data[LIBSPDM_MAX_HASH_SIZE];
      23              : } libspdm_psk_finish_request_mine_14_t;
      24              : #pragma pack()
      25              : 
      26              : static libspdm_th_managed_buffer_t th_curr;
      27              : 
      28              : libspdm_psk_finish_request_mine_t m_libspdm_psk_finish_request1 = {
      29              :     { SPDM_MESSAGE_VERSION_11, SPDM_PSK_FINISH, 0, 0 },
      30              : };
      31              : size_t m_libspdm_psk_finish_request1_size = sizeof(m_libspdm_psk_finish_request1);
      32              : 
      33              : libspdm_psk_finish_request_mine_t m_libspdm_psk_finish_request2 = {
      34              :     { SPDM_MESSAGE_VERSION_11, SPDM_PSK_FINISH, 0, 0 },
      35              : };
      36              : size_t m_libspdm_psk_finish_request2_size = LIBSPDM_MAX_SPDM_MSG_SIZE;
      37              : 
      38              : libspdm_psk_finish_request_mine_14_t m_libspdm_psk_finish_request3 = {
      39              :     { SPDM_MESSAGE_VERSION_14, SPDM_PSK_FINISH, 0, 0 },
      40              : };
      41              : size_t m_libspdm_psk_finish_request3_size = sizeof(m_libspdm_psk_finish_request3);
      42              : 
      43              : static uint8_t m_libspdm_dummy_buffer[LIBSPDM_MAX_HASH_SIZE];
      44              : 
      45           15 : static void libspdm_secured_message_set_request_finished_key(
      46              :     void *spdm_secured_message_context, const void *key, size_t key_size)
      47              : {
      48              :     libspdm_secured_message_context_t *secured_message_context;
      49              : 
      50           15 :     secured_message_context = spdm_secured_message_context;
      51           15 :     LIBSPDM_ASSERT(key_size == secured_message_context->hash_size);
      52           15 :     libspdm_copy_mem(secured_message_context->handshake_secret.request_finished_key,
      53              :                      sizeof(secured_message_context->handshake_secret.request_finished_key),
      54              :                      key, secured_message_context->hash_size);
      55           15 : }
      56              : 
      57              : /**
      58              :  * Test 1: receiving a correct PSK_FINISH message from the requester with a
      59              :  * correct MAC.
      60              :  * Expected behavior: the responder accepts the request and produces a valid
      61              :  * PSK_FINISH_RSP response message.
      62              :  **/
      63            1 : static void rsp_psk_finish_rsp_case1(void **state)
      64              : {
      65              :     libspdm_return_t status;
      66              :     libspdm_test_context_t *spdm_test_context;
      67              :     libspdm_context_t *spdm_context;
      68              :     size_t response_size;
      69              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
      70              :     spdm_psk_finish_response_t *spdm_response;
      71              :     void *data1;
      72              :     size_t data_size1;
      73              :     uint8_t *ptr;
      74              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
      75              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
      76              :     libspdm_session_info_t *session_info;
      77              :     uint32_t session_id;
      78              :     uint32_t hash_size;
      79              :     uint32_t hmac_size;
      80              : 
      81            1 :     spdm_test_context = *state;
      82            1 :     spdm_context = spdm_test_context->spdm_context;
      83            1 :     spdm_test_context->case_id = 0x1;
      84            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
      85              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
      86            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
      87            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
      88            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
      89            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
      90            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
      91            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
      92            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
      93              :         m_libspdm_use_measurement_hash_algo;
      94            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
      95            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
      96            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
      97              :                                                     m_libspdm_use_asym_algo, &data1,
      98              :                                                     &data_size1, NULL, NULL);
      99            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     100            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     101              : 
     102            1 :     libspdm_reset_message_a(spdm_context);
     103              : 
     104            1 :     session_id = 0xFFFFFFFF;
     105            1 :     spdm_context->latest_session_id = session_id;
     106            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     107            1 :     spdm_context->last_spdm_request_session_id = session_id;
     108            1 :     session_info = &spdm_context->session_info[0];
     109            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     110              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     111            1 :     libspdm_session_info_set_psk_hint(session_info,
     112              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     113              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     114            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     115            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     116            1 :     libspdm_secured_message_set_request_finished_key(
     117              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     118              :         hash_size);
     119            1 :     libspdm_secured_message_set_session_state(
     120              :         session_info->secured_message_context,
     121              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     122              : 
     123            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     124            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     125            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     126            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     127              :     /* transcript.message_a size is 0
     128              :      * session_transcript.message_k is 0*/
     129            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
     130              :                                   sizeof(spdm_psk_finish_request_t));
     131            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     132            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     133              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     134            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     135              :                      request_finished_key, hash_size, ptr);
     136            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     137            1 :     response_size = sizeof(response);
     138            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     139              :                                              m_libspdm_psk_finish_request1_size,
     140              :                                              &m_libspdm_psk_finish_request1,
     141              :                                              &response_size, response);
     142            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     143            1 :     assert_int_equal(response_size, sizeof(spdm_psk_finish_response_t));
     144            1 :     spdm_response = (void *)response;
     145            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_PSK_FINISH_RSP);
     146            1 :     free(data1);
     147            1 : }
     148              : 
     149              : /**
     150              :  * Test 2: receiving a PSK_FINISH message larger than specified.
     151              :  * Expected behavior: the responder refuses the PSK_FINISH message and
     152              :  * produces an ERROR message indicating the InvalidRequest.
     153              :  **/
     154            1 : static void rsp_psk_finish_rsp_case2(void **state)
     155              : {
     156              :     libspdm_return_t status;
     157              :     libspdm_test_context_t *spdm_test_context;
     158              :     libspdm_context_t *spdm_context;
     159              :     size_t response_size;
     160              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     161              :     spdm_psk_finish_response_t *spdm_response;
     162              :     void *data1;
     163              :     size_t data_size1;
     164              :     uint8_t *ptr;
     165              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     166              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
     167              :     libspdm_session_info_t *session_info;
     168              :     uint32_t session_id;
     169              :     uint32_t hash_size;
     170              : 
     171            1 :     spdm_test_context = *state;
     172            1 :     spdm_context = spdm_test_context->spdm_context;
     173            1 :     spdm_test_context->case_id = 0x2;
     174            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     175              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     176            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     177            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     178            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     179            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     180            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     181            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     182            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     183              :         m_libspdm_use_measurement_hash_algo;
     184            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     185            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     186            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     187              :                                                     m_libspdm_use_asym_algo, &data1,
     188              :                                                     &data_size1, NULL, NULL);
     189            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     190            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     191              : 
     192            1 :     libspdm_reset_message_a(spdm_context);
     193              : 
     194            1 :     session_id = 0xFFFFFFFF;
     195            1 :     spdm_context->latest_session_id = session_id;
     196            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     197            1 :     spdm_context->last_spdm_request_session_id = session_id;
     198            1 :     session_info = &spdm_context->session_info[0];
     199            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     200              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     201            1 :     libspdm_session_info_set_psk_hint(session_info,
     202              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     203              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     204            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     205            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     206            1 :     libspdm_secured_message_set_request_finished_key(
     207              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     208              :         hash_size);
     209            1 :     libspdm_secured_message_set_session_state(
     210              :         session_info->secured_message_context,
     211              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     212              : 
     213            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     214            1 :     ptr = m_libspdm_psk_finish_request2.verify_data;
     215            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     216              :     /* transcript.message_a size is 0
     217              :      * session_transcript.message_k is 0*/
     218            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request2,
     219              :                                   sizeof(spdm_psk_finish_request_t));
     220            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     221            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     222              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     223            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     224              :                      request_finished_key, hash_size, ptr);
     225            1 :     response_size = sizeof(response);
     226            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     227              :                                              m_libspdm_psk_finish_request2_size,
     228              :                                              &m_libspdm_psk_finish_request2,
     229              :                                              &response_size, response);
     230            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     231            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
     232            1 :     spdm_response = (void *)response;
     233            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
     234            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
     235            1 :     assert_int_equal(spdm_response->header.param2, 0);
     236            1 :     free(data1);
     237            1 : }
     238              : 
     239              : /**
     240              :  * Test 3: receiving a correct PSK_FINISH from the requester, but the
     241              :  * responder is in a Busy state.
     242              :  * Expected behavior: the responder accepts the request, but produces an
     243              :  * ERROR message indicating the Busy state.
     244              :  **/
     245            1 : static void rsp_psk_finish_rsp_case3(void **state)
     246              : {
     247              :     libspdm_return_t status;
     248              :     libspdm_test_context_t *spdm_test_context;
     249              :     libspdm_context_t *spdm_context;
     250              :     size_t response_size;
     251              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     252              :     spdm_psk_finish_response_t *spdm_response;
     253              :     void *data1;
     254              :     size_t data_size1;
     255              :     uint8_t *ptr;
     256              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     257              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
     258              :     libspdm_session_info_t *session_info;
     259              :     uint32_t session_id;
     260              :     uint32_t hash_size;
     261              :     uint32_t hmac_size;
     262              : 
     263            1 :     spdm_test_context = *state;
     264            1 :     spdm_context = spdm_test_context->spdm_context;
     265            1 :     spdm_test_context->case_id = 0x3;
     266            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     267              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     268            1 :     spdm_context->response_state = LIBSPDM_RESPONSE_STATE_BUSY;
     269            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     270            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     271            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     272            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     273            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     274            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     275            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     276              :         m_libspdm_use_measurement_hash_algo;
     277            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     278            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     279            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     280              :                                                     m_libspdm_use_asym_algo, &data1,
     281              :                                                     &data_size1, NULL, NULL);
     282            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     283            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     284              : 
     285            1 :     libspdm_reset_message_a(spdm_context);
     286              : 
     287            1 :     session_id = 0xFFFFFFFF;
     288            1 :     spdm_context->latest_session_id = session_id;
     289            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     290            1 :     spdm_context->last_spdm_request_session_id = session_id;
     291            1 :     session_info = &spdm_context->session_info[0];
     292            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     293              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     294            1 :     libspdm_session_info_set_psk_hint(session_info,
     295              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     296              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     297            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     298            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     299            1 :     libspdm_secured_message_set_request_finished_key(
     300              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     301              :         hash_size);
     302            1 :     libspdm_secured_message_set_session_state(
     303              :         session_info->secured_message_context,
     304              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     305              : 
     306            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     307            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     308            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     309            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     310              :     /* transcript.message_a size is 0
     311              :      * session_transcript.message_k is 0*/
     312            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
     313              :                                   sizeof(spdm_psk_finish_request_t));
     314            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     315            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     316              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     317            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     318              :                      request_finished_key, hash_size, ptr);
     319            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     320            1 :     response_size = sizeof(response);
     321            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     322              :                                              m_libspdm_psk_finish_request1_size,
     323              :                                              &m_libspdm_psk_finish_request1,
     324              :                                              &response_size, response);
     325            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     326            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
     327            1 :     spdm_response = (void *)response;
     328            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
     329            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_BUSY);
     330            1 :     assert_int_equal(spdm_response->header.param2, 0);
     331            1 :     assert_int_equal(spdm_context->response_state, LIBSPDM_RESPONSE_STATE_BUSY);
     332            1 :     free(data1);
     333            1 : }
     334              : 
     335              : /**
     336              :  * Test 4: receiving a correct PSK_FINISH from the requester, but the
     337              :  * responder requires resynchronization with the requester.
     338              :  * Expected behavior: the responder accepts the request, but produces an
     339              :  * ERROR message indicating the NeedResynch state.
     340              :  **/
     341            1 : static void rsp_psk_finish_rsp_case4(void **state)
     342              : {
     343              :     libspdm_return_t status;
     344              :     libspdm_test_context_t *spdm_test_context;
     345              :     libspdm_context_t *spdm_context;
     346              :     size_t response_size;
     347              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     348              :     spdm_psk_finish_response_t *spdm_response;
     349              :     void *data1;
     350              :     size_t data_size1;
     351              :     uint8_t *ptr;
     352              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     353              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
     354              :     libspdm_session_info_t *session_info;
     355              :     uint32_t session_id;
     356              :     uint32_t hash_size;
     357              :     uint32_t hmac_size;
     358              : 
     359            1 :     spdm_test_context = *state;
     360            1 :     spdm_context = spdm_test_context->spdm_context;
     361            1 :     spdm_test_context->case_id = 0x4;
     362            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     363              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     364            1 :     spdm_context->response_state = LIBSPDM_RESPONSE_STATE_NEED_RESYNC;
     365            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     366            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     367            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     368            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     369            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     370            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     371            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     372              :         m_libspdm_use_measurement_hash_algo;
     373            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     374            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     375            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     376              :                                                     m_libspdm_use_asym_algo, &data1,
     377              :                                                     &data_size1, NULL, NULL);
     378            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     379            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     380              : 
     381            1 :     libspdm_reset_message_a(spdm_context);
     382              : 
     383            1 :     session_id = 0xFFFFFFFF;
     384            1 :     spdm_context->latest_session_id = session_id;
     385            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     386            1 :     spdm_context->last_spdm_request_session_id = session_id;
     387            1 :     session_info = &spdm_context->session_info[0];
     388            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     389              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     390            1 :     libspdm_session_info_set_psk_hint(session_info,
     391              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     392              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     393            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     394            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     395            1 :     libspdm_secured_message_set_request_finished_key(
     396              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     397              :         hash_size);
     398            1 :     libspdm_secured_message_set_session_state(
     399              :         session_info->secured_message_context,
     400              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     401              : 
     402            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     403            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     404            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     405            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     406              :     /* transcript.message_a size is 0
     407              :      * session_transcript.message_k is 0*/
     408            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
     409              :                                   sizeof(spdm_psk_finish_request_t));
     410            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     411            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     412              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     413            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     414              :                      request_finished_key, hash_size, ptr);
     415            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     416            1 :     response_size = sizeof(response);
     417            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     418              :                                              m_libspdm_psk_finish_request1_size,
     419              :                                              &m_libspdm_psk_finish_request1,
     420              :                                              &response_size, response);
     421            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     422            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
     423            1 :     spdm_response = (void *)response;
     424            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
     425            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_REQUEST_RESYNCH);
     426            1 :     assert_int_equal(spdm_response->header.param2, 0);
     427            1 :     assert_int_equal(spdm_context->response_state, LIBSPDM_RESPONSE_STATE_NEED_RESYNC);
     428            1 :     free(data1);
     429            1 : }
     430              : 
     431              : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
     432              : /**
     433              :  * Test 5: receiving a correct PSK_FINISH from the requester, but the
     434              :  * responder could not produce the response in time.
     435              :  * Expected behavior: the responder accepts the request, but produces an
     436              :  * ERROR message indicating the ResponseNotReady state.
     437              :  **/
     438            1 : static void rsp_psk_finish_rsp_case5(void **state)
     439              : {
     440              :     libspdm_return_t status;
     441              :     libspdm_test_context_t *spdm_test_context;
     442              :     libspdm_context_t *spdm_context;
     443              :     size_t response_size;
     444              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     445              :     spdm_psk_finish_response_t *spdm_response;
     446              :     void *data1;
     447              :     size_t data_size1;
     448              :     uint8_t *ptr;
     449              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     450              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
     451              :     libspdm_session_info_t *session_info;
     452              :     uint32_t session_id;
     453              :     uint32_t hash_size;
     454              :     uint32_t hmac_size;
     455              :     spdm_error_data_response_not_ready_t *error_data;
     456              : 
     457            1 :     spdm_test_context = *state;
     458            1 :     spdm_context = spdm_test_context->spdm_context;
     459            1 :     spdm_test_context->case_id = 0x5;
     460            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     461              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     462            1 :     spdm_context->response_state = LIBSPDM_RESPONSE_STATE_NOT_READY;
     463            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     464            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     465            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     466            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     467            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     468            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     469            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     470              :         m_libspdm_use_measurement_hash_algo;
     471            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     472            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     473            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     474              :                                                     m_libspdm_use_asym_algo, &data1,
     475              :                                                     &data_size1, NULL, NULL);
     476            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     477            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     478              : 
     479            1 :     libspdm_reset_message_a(spdm_context);
     480              : 
     481            1 :     session_id = 0xFFFFFFFF;
     482            1 :     spdm_context->latest_session_id = session_id;
     483            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     484            1 :     spdm_context->last_spdm_request_session_id = session_id;
     485            1 :     session_info = &spdm_context->session_info[0];
     486            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     487              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     488            1 :     libspdm_session_info_set_psk_hint(session_info,
     489              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     490              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     491            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     492            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     493            1 :     libspdm_secured_message_set_request_finished_key(
     494              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     495              :         hash_size);
     496            1 :     libspdm_secured_message_set_session_state(
     497              :         session_info->secured_message_context,
     498              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     499              : 
     500            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     501            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     502            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     503            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     504              :     /* transcript.message_a size is 0
     505              :      * session_transcript.message_k is 0*/
     506            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
     507              :                                   sizeof(spdm_psk_finish_request_t));
     508            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     509            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     510              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     511            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     512              :                      request_finished_key, hash_size, ptr);
     513            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     514            1 :     response_size = sizeof(response);
     515            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     516              :                                              m_libspdm_psk_finish_request1_size,
     517              :                                              &m_libspdm_psk_finish_request1,
     518              :                                              &response_size, response);
     519            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     520            1 :     assert_int_equal(response_size,
     521              :                      sizeof(spdm_error_response_t) +
     522              :                      sizeof(spdm_error_data_response_not_ready_t));
     523            1 :     spdm_response = (void *)response;
     524            1 :     error_data = (spdm_error_data_response_not_ready_t *)(spdm_response + 1);
     525            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
     526            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_RESPONSE_NOT_READY);
     527            1 :     assert_int_equal(spdm_response->header.param2, 0);
     528            1 :     assert_int_equal(spdm_context->response_state, LIBSPDM_RESPONSE_STATE_NOT_READY);
     529            1 :     assert_int_equal(error_data->request_code, SPDM_PSK_FINISH);
     530            1 :     free(data1);
     531            1 : }
     532              : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
     533              : 
     534              : /**
     535              :  * Test 6: receiving a correct PSK_FINISH from the requester, but the
     536              :  * responder is not set no receive a PSK-FINISH message because previous
     537              :  * messages (namely, GET_CAPABILITIES, NEGOTIATE_ALGORITHMS or
     538              :  * GET_DIGESTS) have not been received.
     539              :  * Expected behavior: the responder rejects the request, and produces an
     540              :  * ERROR message indicating the UnexpectedRequest.
     541              :  **/
     542            1 : static void rsp_psk_finish_rsp_case6(void **state)
     543              : {
     544              :     libspdm_return_t status;
     545              :     libspdm_test_context_t *spdm_test_context;
     546              :     libspdm_context_t *spdm_context;
     547              :     size_t response_size;
     548              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     549              :     spdm_psk_finish_response_t *spdm_response;
     550              :     void *data1;
     551              :     size_t data_size1;
     552              :     uint8_t *ptr;
     553              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     554              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
     555              :     libspdm_session_info_t *session_info;
     556              :     uint32_t session_id;
     557              :     uint32_t hash_size;
     558              :     uint32_t hmac_size;
     559              : 
     560            1 :     spdm_test_context = *state;
     561            1 :     spdm_context = spdm_test_context->spdm_context;
     562            1 :     spdm_test_context->case_id = 0x6;
     563            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     564              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     565            1 :     spdm_context->response_state = LIBSPDM_RESPONSE_STATE_NORMAL;
     566            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NOT_STARTED;
     567            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     568            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     569            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     570            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     571            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     572            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     573              :         m_libspdm_use_measurement_hash_algo;
     574            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     575            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     576            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     577              :                                                     m_libspdm_use_asym_algo, &data1,
     578              :                                                     &data_size1, NULL, NULL);
     579            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     580            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     581              : 
     582            1 :     libspdm_reset_message_a(spdm_context);
     583              : 
     584            1 :     session_id = 0xFFFFFFFF;
     585            1 :     spdm_context->latest_session_id = session_id;
     586            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     587            1 :     spdm_context->last_spdm_request_session_id = session_id;
     588            1 :     session_info = &spdm_context->session_info[0];
     589            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     590              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     591            1 :     libspdm_session_info_set_psk_hint(session_info,
     592              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     593              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     594            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     595            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     596            1 :     libspdm_secured_message_set_request_finished_key(
     597              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     598              :         hash_size);
     599            1 :     libspdm_secured_message_set_session_state(
     600              :         session_info->secured_message_context,
     601              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     602              : 
     603            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     604            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     605            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     606            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     607              :     /* transcript.message_a size is 0
     608              :      * session_transcript.message_k is 0*/
     609            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
     610              :                                   sizeof(spdm_psk_finish_request_t));
     611            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     612            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     613              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     614            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     615              :                      request_finished_key, hash_size, ptr);
     616            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     617            1 :     response_size = sizeof(response);
     618            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     619              :                                              m_libspdm_psk_finish_request1_size,
     620              :                                              &m_libspdm_psk_finish_request1,
     621              :                                              &response_size, response);
     622            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     623            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
     624            1 :     spdm_response = (void *)response;
     625            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
     626            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNEXPECTED_REQUEST);
     627            1 :     assert_int_equal(spdm_response->header.param2, 0);
     628            1 :     free(data1);
     629            1 : }
     630              : 
     631            1 : static void rsp_psk_finish_rsp_case7(void **state)
     632              : {
     633              :     libspdm_return_t status;
     634              :     libspdm_test_context_t *spdm_test_context;
     635              :     libspdm_context_t *spdm_context;
     636              :     size_t response_size;
     637              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     638              :     spdm_psk_finish_response_t *spdm_response;
     639              :     void *data1;
     640              :     size_t data_size1;
     641              :     uint8_t *ptr;
     642              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     643              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
     644              :     libspdm_session_info_t *session_info;
     645              :     uint32_t session_id;
     646              :     uint32_t hash_size;
     647              :     uint32_t hmac_size;
     648              : 
     649            1 :     spdm_test_context = *state;
     650            1 :     spdm_context = spdm_test_context->spdm_context;
     651            1 :     spdm_test_context->case_id = 0x7;
     652            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     653              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     654            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     655            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     656            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     657            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     658            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     659            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     660            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     661              :         m_libspdm_use_measurement_hash_algo;
     662            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     663            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     664            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     665              :                                                     m_libspdm_use_asym_algo, &data1,
     666              :                                                     &data_size1, NULL, NULL);
     667            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     668            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     669              : 
     670            1 :     libspdm_reset_message_a(spdm_context);
     671              : 
     672            1 :     session_id = 0xFFFFFFFF;
     673            1 :     spdm_context->latest_session_id = session_id;
     674            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     675            1 :     spdm_context->last_spdm_request_session_id = session_id;
     676            1 :     session_info = &spdm_context->session_info[0];
     677            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     678              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     679            1 :     libspdm_session_info_set_psk_hint(session_info,
     680              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     681              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     682            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     683            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     684            1 :     libspdm_secured_message_set_request_finished_key(
     685              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     686              :         hash_size);
     687            1 :     libspdm_secured_message_set_session_state(
     688              :         session_info->secured_message_context,
     689              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     690              : 
     691            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     692            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     693            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     694            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     695              :     /* transcript.message_a size is 0
     696              :      * session_transcript.message_k is 0*/
     697            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
     698              :                                   sizeof(spdm_psk_finish_request_t));
     699              : 
     700              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     701              :     session_info->session_transcript.message_m.buffer_size =
     702              :         session_info->session_transcript.message_m.max_buffer_size;
     703              :     spdm_context->transcript.message_b.buffer_size =
     704              :         spdm_context->transcript.message_b.max_buffer_size;
     705              :     spdm_context->transcript.message_c.buffer_size =
     706              :         spdm_context->transcript.message_c.max_buffer_size;
     707              :     spdm_context->transcript.message_mut_b.buffer_size =
     708              :         spdm_context->transcript.message_mut_b.max_buffer_size;
     709              :     spdm_context->transcript.message_mut_c.buffer_size =
     710              :         spdm_context->transcript.message_mut_c.max_buffer_size;
     711              : #endif
     712              : 
     713            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     714            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     715              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     716            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     717              :                      request_finished_key, hash_size, ptr);
     718            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     719            1 :     response_size = sizeof(response);
     720            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     721              :                                              m_libspdm_psk_finish_request1_size,
     722              :                                              &m_libspdm_psk_finish_request1,
     723              :                                              &response_size, response);
     724            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     725            1 :     assert_int_equal(response_size, sizeof(spdm_psk_finish_response_t));
     726            1 :     spdm_response = (void *)response;
     727            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_PSK_FINISH_RSP);
     728              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     729              :     assert_int_equal(session_info->session_transcript.message_m.buffer_size, 0);
     730              :     assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
     731              :     assert_int_equal(spdm_context->transcript.message_c.buffer_size, 0);
     732              :     assert_int_equal(spdm_context->transcript.message_mut_b.buffer_size, 0);
     733              :     assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
     734              : #endif
     735              : 
     736            1 :     free(data1);
     737            1 : }
     738              : 
     739              : /**
     740              :  * Test 8: receiving a correct PSK_FINISH message from the requester, but
     741              :  * the responder has no capabilities for pre-shared keys.
     742              :  * Expected behavior: the responder refuses the PSK_FINISH message and
     743              :  * produces an ERROR message indicating the UnsupportedRequest.
     744              :  **/
     745            1 : static void rsp_psk_finish_rsp_case8(void **state)
     746              : {
     747              :     libspdm_return_t status;
     748              :     libspdm_test_context_t *spdm_test_context;
     749              :     libspdm_context_t *spdm_context;
     750              :     size_t response_size;
     751              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     752              :     spdm_psk_finish_response_t *spdm_response;
     753              :     void *data1;
     754              :     size_t data_size1;
     755              :     uint8_t *ptr;
     756              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     757              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
     758              :     libspdm_session_info_t *session_info;
     759              :     uint32_t session_id;
     760              :     uint32_t hash_size;
     761              :     uint32_t hmac_size;
     762              : 
     763            1 :     spdm_test_context = *state;
     764            1 :     spdm_context = spdm_test_context->spdm_context;
     765            1 :     spdm_test_context->case_id = 0x8;
     766            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     767              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     768            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     769            1 :     spdm_context->connection_info.capability.flags &=
     770              :         ~(SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP);
     771            1 :     spdm_context->local_context.capability.flags &= ~(SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP);
     772            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     773            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     774            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     775            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     776              :         m_libspdm_use_measurement_hash_algo;
     777            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     778            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     779            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     780              :                                                     m_libspdm_use_asym_algo, &data1,
     781              :                                                     &data_size1, NULL, NULL);
     782            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     783            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     784              : 
     785            1 :     spdm_context->transcript.message_a.buffer_size = 0;
     786              : 
     787            1 :     session_id = 0xFFFFFFFF;
     788            1 :     spdm_context->latest_session_id = session_id;
     789            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     790            1 :     spdm_context->last_spdm_request_session_id = session_id;
     791            1 :     session_info = &spdm_context->session_info[0];
     792            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     793              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     794            1 :     libspdm_session_info_set_psk_hint(session_info,
     795              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     796              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     797            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     798            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     799            1 :     libspdm_secured_message_set_request_finished_key(
     800              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     801              :         hash_size);
     802            1 :     libspdm_secured_message_set_session_state(
     803              :         session_info->secured_message_context,
     804              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     805              : 
     806            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     807            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     808            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     809            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     810              :     /* transcript.message_a size is 0
     811              :      * session_transcript.message_k is 0*/
     812            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
     813              :                                   sizeof(spdm_psk_finish_request_t));
     814            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     815            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     816              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     817            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     818              :                      request_finished_key, hash_size, ptr);
     819            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     820            1 :     response_size = sizeof(response);
     821            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     822              :                                              m_libspdm_psk_finish_request1_size,
     823              :                                              &m_libspdm_psk_finish_request1,
     824              :                                              &response_size, response);
     825            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     826            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
     827            1 :     spdm_response = (void *)response;
     828            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
     829            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST);
     830            1 :     assert_int_equal(spdm_response->header.param2, SPDM_PSK_FINISH);
     831            1 :     free(data1);
     832            1 : }
     833              : 
     834              : /**
     835              :  * Test 9: receiving a correct PSK_FINISH message from the requester, but
     836              :  * the responder is not correctly setup by not initializing a session during
     837              :  * PSK_EXCHANGE.
     838              :  * Expected behavior: the responder refuses the PSK_FINISH message and
     839              :  * produces an ERROR message indicating the InvalidRequest.
     840              :  **/
     841            1 : static void rsp_psk_finish_rsp_case9(void **state)
     842              : {
     843              :     libspdm_return_t status;
     844              :     libspdm_test_context_t *spdm_test_context;
     845              :     libspdm_context_t *spdm_context;
     846              :     size_t response_size;
     847              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     848              :     spdm_psk_finish_response_t *spdm_response;
     849              :     void *data1;
     850              :     size_t data_size1;
     851              :     uint8_t *ptr;
     852              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     853              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
     854              :     libspdm_session_info_t *session_info;
     855              :     uint32_t session_id;
     856              :     uint32_t hash_size;
     857              :     uint32_t hmac_size;
     858              : 
     859            1 :     spdm_test_context = *state;
     860            1 :     spdm_context = spdm_test_context->spdm_context;
     861            1 :     spdm_test_context->case_id = 0x9;
     862            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     863              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     864            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     865            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     866            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     867            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     868            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     869            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     870            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     871              :         m_libspdm_use_measurement_hash_algo;
     872            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     873            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     874            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     875              :                                                     m_libspdm_use_asym_algo, &data1,
     876              :                                                     &data_size1, NULL, NULL);
     877            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     878            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     879              : 
     880            1 :     spdm_context->transcript.message_a.buffer_size = 0;
     881              : 
     882            1 :     session_id = 0xFFFFFFFF;
     883            1 :     spdm_context->latest_session_id = session_id;
     884            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     885            1 :     spdm_context->last_spdm_request_session_id = session_id;
     886            1 :     session_info = &spdm_context->session_info[0];
     887            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     888              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     889            1 :     libspdm_session_info_set_psk_hint(session_info,
     890              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     891              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     892            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     893            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     894            1 :     libspdm_secured_message_set_request_finished_key(
     895              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     896              :         hash_size);
     897            1 :     libspdm_secured_message_set_session_state(
     898              :         session_info->secured_message_context,
     899              :         LIBSPDM_SESSION_STATE_NOT_STARTED);
     900              : 
     901            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     902            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     903            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     904            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     905              :     /* transcript.message_a size is 0
     906              :      * session_transcript.message_k is 0*/
     907            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
     908              :                                   sizeof(spdm_psk_finish_request_t));
     909            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     910            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     911              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
     912            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     913              :                      request_finished_key, hash_size, ptr);
     914            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     915            1 :     response_size = sizeof(response);
     916            1 :     status = libspdm_get_response_psk_finish(spdm_context,
     917              :                                              m_libspdm_psk_finish_request1_size,
     918              :                                              &m_libspdm_psk_finish_request1,
     919              :                                              &response_size, response);
     920            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     921            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
     922            1 :     spdm_response = (void *)response;
     923            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
     924            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNEXPECTED_REQUEST);
     925            1 :     assert_int_equal(spdm_response->header.param2, 0);
     926            1 :     free(data1);
     927            1 : }
     928              : 
     929              : /**
     930              :  * Test 10: receiving a PSK_FINISH message from the requester with an
     931              :  * incorrect MAC (all-zero).
     932              :  * Expected behavior: the responder refuses the PSK_FINISH message and
     933              :  * produces an ERROR message indicating the DecryptError.
     934              :  **/
     935            1 : static void rsp_psk_finish_rsp_case10(void **state)
     936              : {
     937              :     libspdm_return_t status;
     938              :     libspdm_test_context_t *spdm_test_context;
     939              :     libspdm_context_t *spdm_context;
     940              :     size_t response_size;
     941              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     942              :     spdm_psk_finish_response_t *spdm_response;
     943              :     void *data1;
     944              :     size_t data_size1;
     945              :     uint8_t *ptr;
     946              :     libspdm_session_info_t *session_info;
     947              :     uint32_t session_id;
     948              :     uint32_t hash_size;
     949              :     uint32_t hmac_size;
     950              : 
     951            1 :     spdm_test_context = *state;
     952            1 :     spdm_context = spdm_test_context->spdm_context;
     953            1 :     spdm_test_context->case_id = 0xA;
     954            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     955              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     956            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     957            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     958            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     959            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     960            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     961            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
     962            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
     963              :         m_libspdm_use_measurement_hash_algo;
     964            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     965            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     966            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     967              :                                                     m_libspdm_use_asym_algo, &data1,
     968              :                                                     &data_size1, NULL, NULL);
     969            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
     970            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
     971              : 
     972            1 :     spdm_context->transcript.message_a.buffer_size = 0;
     973              : 
     974            1 :     session_id = 0xFFFFFFFF;
     975            1 :     spdm_context->latest_session_id = session_id;
     976            1 :     spdm_context->last_spdm_request_session_id_valid = true;
     977            1 :     spdm_context->last_spdm_request_session_id = session_id;
     978            1 :     session_info = &spdm_context->session_info[0];
     979            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
     980              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
     981            1 :     libspdm_session_info_set_psk_hint(session_info,
     982              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
     983              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
     984            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     985            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
     986            1 :     libspdm_secured_message_set_request_finished_key(
     987              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
     988              :         hash_size);
     989            1 :     libspdm_secured_message_set_session_state(
     990              :         session_info->secured_message_context,
     991              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
     992              : 
     993            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     994            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     995            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
     996            1 :     libspdm_set_mem(ptr, hmac_size, (uint8_t)(0x00)); /*all-zero MAC*/
     997            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
     998            1 :     response_size = sizeof(response);
     999            1 :     status = libspdm_get_response_psk_finish(spdm_context,
    1000              :                                              m_libspdm_psk_finish_request1_size,
    1001              :                                              &m_libspdm_psk_finish_request1,
    1002              :                                              &response_size, response);
    1003            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1004            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
    1005            1 :     spdm_response = (void *)response;
    1006            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
    1007            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_DECRYPT_ERROR);
    1008            1 :     assert_int_equal(spdm_response->header.param2, 0);
    1009            1 :     free(data1);
    1010            1 : }
    1011              : 
    1012              : /**
    1013              :  * Test 11: receiving a PSK_FINISH message from the requester with an
    1014              :  * incorrect MAC (arbitrary).
    1015              :  * Expected behavior: the responder refuses the PSK_FINISH message and
    1016              :  * produces an ERROR message indicating the DecryptError.
    1017              :  **/
    1018            1 : static void rsp_psk_finish_rsp_case11(void **state)
    1019              : {
    1020              :     libspdm_return_t status;
    1021              :     libspdm_test_context_t *spdm_test_context;
    1022              :     libspdm_context_t *spdm_context;
    1023              :     size_t response_size;
    1024              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
    1025              :     spdm_psk_finish_response_t *spdm_response;
    1026              :     void *data1;
    1027              :     size_t data_size1;
    1028              :     uint8_t *ptr;
    1029              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1030              :     uint8_t zero_data[LIBSPDM_MAX_HASH_SIZE];
    1031              :     libspdm_session_info_t *session_info;
    1032              :     uint32_t session_id;
    1033              :     uint32_t hash_size;
    1034              :     uint32_t hmac_size;
    1035              : 
    1036            1 :     spdm_test_context = *state;
    1037            1 :     spdm_context = spdm_test_context->spdm_context;
    1038            1 :     spdm_test_context->case_id = 0xB;
    1039            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1040              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1041            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1042            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1043            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1044            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    1045            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    1046            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
    1047            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    1048              :         m_libspdm_use_measurement_hash_algo;
    1049            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    1050            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    1051            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1052              :                                                     m_libspdm_use_asym_algo, &data1,
    1053              :                                                     &data_size1, NULL, NULL);
    1054            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
    1055            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
    1056              : 
    1057            1 :     spdm_context->transcript.message_a.buffer_size = 0;
    1058              : 
    1059            1 :     session_id = 0xFFFFFFFF;
    1060            1 :     spdm_context->latest_session_id = session_id;
    1061            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    1062            1 :     spdm_context->last_spdm_request_session_id = session_id;
    1063            1 :     session_info = &spdm_context->session_info[0];
    1064            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1065              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1066            1 :     libspdm_session_info_set_psk_hint(session_info,
    1067              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1068              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1069            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1070            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1071            1 :     libspdm_secured_message_set_request_finished_key(
    1072              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1073              :         hash_size);
    1074            1 :     libspdm_secured_message_set_session_state(
    1075              :         session_info->secured_message_context,
    1076              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1077              : 
    1078            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1079            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1080            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
    1081              :     /*arbitrary MAC*/
    1082            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1083            1 :     libspdm_set_mem(zero_data, hash_size, (uint8_t)(0x00));
    1084            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, zero_data, hash_size,
    1085              :                      request_finished_key, hash_size, ptr);
    1086            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
    1087            1 :     response_size = sizeof(response);
    1088            1 :     status = libspdm_get_response_psk_finish(spdm_context,
    1089              :                                              m_libspdm_psk_finish_request1_size,
    1090              :                                              &m_libspdm_psk_finish_request1,
    1091              :                                              &response_size, response);
    1092            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1093            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
    1094            1 :     spdm_response = (void *)response;
    1095            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
    1096            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_DECRYPT_ERROR);
    1097            1 :     assert_int_equal(spdm_response->header.param2, 0);
    1098            1 :     free(data1);
    1099            1 : }
    1100              : 
    1101              : /**
    1102              :  * Test 12: receiving a PSK_FINISH message from the requester with an
    1103              :  * incorrect MAC size (a correct MAC repeated twice).
    1104              :  * Expected behavior: the responder refuses the PSK_FINISH message and
    1105              :  * produces an ERROR message indicating the InvalidRequest.
    1106              :  **/
    1107            1 : static void rsp_psk_finish_rsp_case12(void **state)
    1108              : {
    1109              :     libspdm_return_t status;
    1110              :     libspdm_test_context_t *spdm_test_context;
    1111              :     libspdm_context_t *spdm_context;
    1112              :     size_t response_size;
    1113              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
    1114              :     spdm_psk_finish_response_t *spdm_response;
    1115              :     void *data1;
    1116              :     size_t data_size1;
    1117              :     uint8_t *ptr;
    1118              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1119              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1120              :     libspdm_session_info_t *session_info;
    1121              :     uint32_t session_id;
    1122              :     uint32_t hash_size;
    1123              :     uint32_t hmac_size;
    1124              : 
    1125            1 :     spdm_test_context = *state;
    1126            1 :     spdm_context = spdm_test_context->spdm_context;
    1127            1 :     spdm_test_context->case_id = 0xC;
    1128            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1129              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1130            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1131            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1132            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1133            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    1134            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    1135            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
    1136            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    1137              :         m_libspdm_use_measurement_hash_algo;
    1138            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    1139            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    1140            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1141              :                                                     m_libspdm_use_asym_algo, &data1,
    1142              :                                                     &data_size1, NULL, NULL);
    1143            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
    1144            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
    1145              : 
    1146            1 :     spdm_context->transcript.message_a.buffer_size = 0;
    1147              : 
    1148            1 :     session_id = 0xFFFFFFFF;
    1149            1 :     spdm_context->latest_session_id = session_id;
    1150            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    1151            1 :     spdm_context->last_spdm_request_session_id = session_id;
    1152            1 :     session_info = &spdm_context->session_info[0];
    1153            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1154              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1155            1 :     libspdm_session_info_set_psk_hint(session_info,
    1156              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1157              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1158            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1159            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1160            1 :     libspdm_secured_message_set_request_finished_key(
    1161              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1162              :         hash_size);
    1163            1 :     libspdm_secured_message_set_session_state(
    1164              :         session_info->secured_message_context,
    1165              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1166              : 
    1167            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1168            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1169            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
    1170            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1171              :     /* transcript.message_a size is 0
    1172              :      * session_transcript.message_k is 0*/
    1173            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
    1174              :                                   sizeof(spdm_psk_finish_request_t));
    1175            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1176            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1177              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1178            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1179              :                      request_finished_key, hash_size, ptr);
    1180            1 :     libspdm_copy_mem(ptr, sizeof(m_libspdm_psk_finish_request1.verify_data),
    1181            1 :                      ptr + hmac_size, hmac_size); /* 2x HMAC size*/
    1182            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + 2*hmac_size;
    1183            1 :     response_size = sizeof(response);
    1184            1 :     status = libspdm_get_response_psk_finish(spdm_context,
    1185              :                                              m_libspdm_psk_finish_request1_size,
    1186              :                                              &m_libspdm_psk_finish_request1,
    1187              :                                              &response_size, response);
    1188            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1189            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
    1190            1 :     spdm_response = (void *)response;
    1191            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
    1192            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
    1193            1 :     assert_int_equal(spdm_response->header.param2, 0);
    1194            1 :     free(data1);
    1195            1 : }
    1196              : 
    1197              : /**
    1198              :  * Test 13: receiving a PSK_FINISH message from the requester with an
    1199              :  * incorrect MAC size (only the correct first half of the MAC).
    1200              :  * Expected behavior: the responder refuses the PSK_FINISH message and
    1201              :  * produces an ERROR message indicating the InvalidRequest.
    1202              :  **/
    1203            1 : static void rsp_psk_finish_rsp_case13(void **state)
    1204              : {
    1205              :     libspdm_return_t status;
    1206              :     libspdm_test_context_t *spdm_test_context;
    1207              :     libspdm_context_t *spdm_context;
    1208              :     size_t response_size;
    1209              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
    1210              :     spdm_psk_finish_response_t *spdm_response;
    1211              :     void *data1;
    1212              :     size_t data_size1;
    1213              :     uint8_t *ptr;
    1214              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1215              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1216              :     libspdm_session_info_t *session_info;
    1217              :     uint32_t session_id;
    1218              :     uint32_t hash_size;
    1219              :     uint32_t hmac_size;
    1220              : 
    1221            1 :     spdm_test_context = *state;
    1222            1 :     spdm_context = spdm_test_context->spdm_context;
    1223            1 :     spdm_test_context->case_id = 0xD;
    1224            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1225              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1226            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1227            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1228            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1229            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    1230            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    1231            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
    1232            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    1233              :         m_libspdm_use_measurement_hash_algo;
    1234            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    1235            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    1236            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1237              :                                                     m_libspdm_use_asym_algo, &data1,
    1238              :                                                     &data_size1, NULL, NULL);
    1239            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
    1240            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
    1241              : 
    1242            1 :     spdm_context->transcript.message_a.buffer_size = 0;
    1243              : 
    1244            1 :     session_id = 0xFFFFFFFF;
    1245            1 :     spdm_context->latest_session_id = session_id;
    1246            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    1247            1 :     spdm_context->last_spdm_request_session_id = session_id;
    1248            1 :     session_info = &spdm_context->session_info[0];
    1249            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1250              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1251            1 :     libspdm_session_info_set_psk_hint(session_info,
    1252              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1253              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1254            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1255            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1256            1 :     libspdm_secured_message_set_request_finished_key(
    1257              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1258              :         hash_size);
    1259            1 :     libspdm_secured_message_set_session_state(
    1260              :         session_info->secured_message_context,
    1261              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1262              : 
    1263            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1264            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1265            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
    1266            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1267              :     /* transcript.message_a size is 0
    1268              :      * session_transcript.message_k is 0*/
    1269            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
    1270              :                                   sizeof(spdm_psk_finish_request_t));
    1271            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1272            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1273              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1274            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1275              :                      request_finished_key, hash_size, ptr);
    1276            1 :     libspdm_set_mem(ptr + hmac_size/2, hmac_size/2, (uint8_t) 0x00); /* half HMAC size*/
    1277            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size/2;
    1278            1 :     response_size = sizeof(response);
    1279            1 :     status = libspdm_get_response_psk_finish(spdm_context,
    1280              :                                              m_libspdm_psk_finish_request1_size,
    1281              :                                              &m_libspdm_psk_finish_request1,
    1282              :                                              &response_size, response);
    1283            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1284            1 :     assert_int_equal(response_size, sizeof(spdm_error_response_t));
    1285            1 :     spdm_response = (void *)response;
    1286            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
    1287            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
    1288            1 :     assert_int_equal(spdm_response->header.param2, 0);
    1289            1 :     free(data1);
    1290            1 : }
    1291              : 
    1292              : /**
    1293              :  * Test 14: receiving a correct PSK_FINISH from the requester.
    1294              :  * Expected behavior: the responder accepts the request and produces a valid PSK_FINISH
    1295              :  * response message, and buffer F receives the exchanged PSK_FINISH and PSK_FINISH_RSP messages.
    1296              :  **/
    1297            1 : static void rsp_psk_finish_rsp_case14(void **state)
    1298              : {
    1299              :     libspdm_return_t status;
    1300              :     libspdm_test_context_t *spdm_test_context;
    1301              :     libspdm_context_t *spdm_context;
    1302              :     size_t response_size;
    1303              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
    1304              :     spdm_psk_finish_response_t *spdm_response;
    1305              :     void *data1;
    1306              :     size_t data_size1;
    1307              :     uint8_t *ptr;
    1308              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1309              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1310              :     libspdm_session_info_t *session_info;
    1311              :     uint32_t session_id;
    1312              :     uint32_t hash_size;
    1313              :     uint32_t hmac_size;
    1314              : 
    1315            1 :     spdm_test_context = *state;
    1316            1 :     spdm_context = spdm_test_context->spdm_context;
    1317            1 :     spdm_test_context->case_id = 0xE;
    1318            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1319              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1320            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1321            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1322            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1323            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    1324            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    1325            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
    1326            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    1327              :         m_libspdm_use_measurement_hash_algo;
    1328            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    1329            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    1330            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1331              :                                                     m_libspdm_use_asym_algo, &data1,
    1332              :                                                     &data_size1, NULL, NULL);
    1333            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
    1334            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
    1335              : 
    1336            1 :     libspdm_reset_message_a(spdm_context);
    1337              : 
    1338            1 :     session_id = 0xFFFFFFFF;
    1339            1 :     spdm_context->latest_session_id = session_id;
    1340            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    1341            1 :     spdm_context->last_spdm_request_session_id = session_id;
    1342            1 :     session_info = &spdm_context->session_info[0];
    1343            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1344              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1345            1 :     libspdm_session_info_set_psk_hint(session_info,
    1346              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1347              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1348            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1349            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1350            1 :     libspdm_secured_message_set_request_finished_key(
    1351              :         session_info->secured_message_context, m_libspdm_dummy_buffer, hash_size);
    1352            1 :     libspdm_secured_message_set_session_state(
    1353              :         session_info->secured_message_context, LIBSPDM_SESSION_STATE_HANDSHAKING);
    1354              : 
    1355            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1356            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1357            1 :     ptr = m_libspdm_psk_finish_request1.verify_data;
    1358            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1359              :     /* transcript.message_a size is 0
    1360              :      * session_transcript.message_k is 0*/
    1361            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request1,
    1362              :                                   sizeof(spdm_psk_finish_request_t));
    1363            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1364            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1365              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1366            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1367              :                      request_finished_key, hash_size, ptr);
    1368            1 :     m_libspdm_psk_finish_request1_size = sizeof(spdm_psk_finish_request_t) + hmac_size;
    1369            1 :     response_size = sizeof(response);
    1370            1 :     status = libspdm_get_response_psk_finish(
    1371              :         spdm_context, m_libspdm_psk_finish_request1_size, &m_libspdm_psk_finish_request1,
    1372              :         &response_size, response);
    1373            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1374            1 :     assert_int_equal(response_size, sizeof(spdm_psk_finish_response_t));
    1375            1 :     spdm_response = (void *)response;
    1376            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_PSK_FINISH_RSP);
    1377              : 
    1378              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1379              :     assert_int_equal(spdm_context->session_info[0].session_transcript.message_f.buffer_size,
    1380              :                      m_libspdm_psk_finish_request1_size + response_size);
    1381              :     assert_memory_equal(spdm_context->session_info[0].session_transcript.message_f.buffer,
    1382              :                         &m_libspdm_psk_finish_request1, m_libspdm_psk_finish_request1_size);
    1383              :     assert_memory_equal(spdm_context->session_info[0].session_transcript.message_f.buffer +
    1384              :                         m_libspdm_psk_finish_request1_size, response, response_size);
    1385              : #endif
    1386              : 
    1387            1 :     free(data1);
    1388            1 : }
    1389              : 
    1390              : /**
    1391              :  * Test 15: SPDM version 1.4, with OpaqueData.
    1392              :  * Expected behavior: the responder accepts the request and produces a valid PSK_FINISH
    1393              :  * response message.
    1394              :  **/
    1395            1 : static void rsp_psk_finish_rsp_case15(void **state)
    1396              : {
    1397              :     libspdm_return_t status;
    1398              :     libspdm_test_context_t *spdm_test_context;
    1399              :     libspdm_context_t *spdm_context;
    1400              :     size_t response_size;
    1401              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
    1402              :     spdm_psk_finish_response_t *spdm_response;
    1403              :     void *data1;
    1404              :     size_t data_size1;
    1405              :     uint8_t *ptr;
    1406              :     uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1407              :     uint8_t request_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1408              :     libspdm_session_info_t *session_info;
    1409              :     uint32_t session_id;
    1410              :     uint32_t hash_size;
    1411              :     uint32_t hmac_size;
    1412              : 
    1413            1 :     spdm_test_context = *state;
    1414            1 :     spdm_context = spdm_test_context->spdm_context;
    1415            1 :     spdm_test_context->case_id = 0xF;
    1416            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_14 <<
    1417              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1418            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1419            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1420            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1421            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    1422            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    1423            1 :     spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
    1424            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    1425              :         m_libspdm_use_measurement_hash_algo;
    1426            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    1427            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    1428            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1429              :                                                     m_libspdm_use_asym_algo, &data1,
    1430              :                                                     &data_size1, NULL, NULL);
    1431            1 :     spdm_context->local_context.local_cert_chain_provision[0] = data1;
    1432            1 :     spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
    1433              : 
    1434            1 :     libspdm_reset_message_a(spdm_context);
    1435              : 
    1436            1 :     session_id = 0xFFFFFFFF;
    1437            1 :     spdm_context->latest_session_id = session_id;
    1438            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    1439            1 :     spdm_context->last_spdm_request_session_id = session_id;
    1440            1 :     session_info = &spdm_context->session_info[0];
    1441            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1442              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1443            1 :     libspdm_session_info_set_psk_hint(session_info,
    1444              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1445              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1446            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1447            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1448            1 :     libspdm_secured_message_set_request_finished_key(
    1449              :         session_info->secured_message_context, m_libspdm_dummy_buffer, hash_size);
    1450            1 :     libspdm_secured_message_set_session_state(
    1451              :         session_info->secured_message_context, LIBSPDM_SESSION_STATE_HANDSHAKING);
    1452              : 
    1453            1 :     m_libspdm_psk_finish_request3.opaque_data_size =
    1454              :         sizeof(m_libspdm_psk_finish_request3.opaque_data);
    1455            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1456            1 :     hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1457            1 :     ptr = m_libspdm_psk_finish_request3.verify_data;
    1458            1 :     libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1459              :     /* transcript.message_a size is 0
    1460              :      * session_transcript.message_k is 0*/
    1461            1 :     libspdm_append_managed_buffer(&th_curr, (uint8_t *)&m_libspdm_psk_finish_request3,
    1462              :                                   sizeof(spdm_psk_finish_request_t) + sizeof(uint16_t) +
    1463            1 :                                   m_libspdm_psk_finish_request3.opaque_data_size);
    1464            1 :     libspdm_set_mem(request_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1465            1 :     libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1466              :                      libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1467            1 :     libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1468              :                      request_finished_key, hash_size, ptr);
    1469            1 :     m_libspdm_psk_finish_request3_size = sizeof(spdm_psk_finish_request_t) + hmac_size +
    1470            1 :                                          sizeof(uint16_t) +
    1471            1 :                                          m_libspdm_psk_finish_request3.opaque_data_size;
    1472            1 :     response_size = sizeof(response);
    1473            1 :     status = libspdm_get_response_psk_finish(
    1474              :         spdm_context, m_libspdm_psk_finish_request3_size, &m_libspdm_psk_finish_request3,
    1475              :         &response_size, response);
    1476            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1477            1 :     ptr = (uint8_t *)response + sizeof(spdm_psk_finish_response_t);
    1478            1 :     assert_int_equal(response_size, sizeof(spdm_psk_finish_response_t) +
    1479              :                      sizeof(uint16_t) + libspdm_read_uint16(ptr));
    1480            1 :     spdm_response = (void *)response;
    1481            1 :     assert_int_equal(spdm_response->header.request_response_code, SPDM_PSK_FINISH_RSP);
    1482              : 
    1483            1 :     free(data1);
    1484            1 : }
    1485              : 
    1486            1 : int libspdm_rsp_psk_finish_rsp_test(void)
    1487              : {
    1488            1 :     const struct CMUnitTest test_cases[] = {
    1489              :         /* Success Case*/
    1490              :         cmocka_unit_test(rsp_psk_finish_rsp_case1),
    1491              :         /* Bad request size*/
    1492              :         cmocka_unit_test(rsp_psk_finish_rsp_case2),
    1493              :         /* response_state: SPDM_RESPONSE_STATE_BUSY*/
    1494              :         cmocka_unit_test(rsp_psk_finish_rsp_case3),
    1495              :         /* response_state: SPDM_RESPONSE_STATE_NEED_RESYNC*/
    1496              :         cmocka_unit_test(rsp_psk_finish_rsp_case4),
    1497              :         #if LIBSPDM_RESPOND_IF_READY_SUPPORT
    1498              :         /* response_state: SPDM_RESPONSE_STATE_NOT_READY*/
    1499              :         cmocka_unit_test(rsp_psk_finish_rsp_case5),
    1500              :         #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
    1501              :         /* connection_state Check*/
    1502              :         cmocka_unit_test(rsp_psk_finish_rsp_case6),
    1503              :         /* Buffer reset*/
    1504              :         cmocka_unit_test(rsp_psk_finish_rsp_case7),
    1505              :         /* Unsupported PSK capabilities*/
    1506              :         cmocka_unit_test(rsp_psk_finish_rsp_case8),
    1507              :         /* Uninitialized session*/
    1508              :         cmocka_unit_test(rsp_psk_finish_rsp_case9),
    1509              :         /* Incorrect MAC*/
    1510              :         cmocka_unit_test(rsp_psk_finish_rsp_case10),
    1511              :         cmocka_unit_test(rsp_psk_finish_rsp_case11),
    1512              :         /* Incorrect MAC size*/
    1513              :         cmocka_unit_test(rsp_psk_finish_rsp_case12),
    1514              :         cmocka_unit_test(rsp_psk_finish_rsp_case13),
    1515              :         /* Buffer verification*/
    1516              :         cmocka_unit_test(rsp_psk_finish_rsp_case14),
    1517              :         /* SPDM 1.4 with OpaqueData */
    1518              :         cmocka_unit_test(rsp_psk_finish_rsp_case15),
    1519              :     };
    1520              : 
    1521            1 :     libspdm_test_context_t test_context = {
    1522              :         LIBSPDM_TEST_CONTEXT_VERSION,
    1523              :         false,
    1524              :     };
    1525              : 
    1526            1 :     libspdm_setup_test_context(&test_context);
    1527              : 
    1528            1 :     return cmocka_run_group_tests(test_cases,
    1529              :                                   libspdm_unit_test_group_setup,
    1530              :                                   libspdm_unit_test_group_teardown);
    1531              : }
    1532              : 
    1533              : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP */
        

Generated by: LCOV version 2.0-1