LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester/error_test - key_exchange_err.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 73.6 % 2869 2113
Test Date: 2025-10-12 08:10:56 Functions: 100.0 % 35 35

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2021-2025 DMTF. All rights reserved.
       4              :  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
       5              :  **/
       6              : 
       7              : #include "spdm_unit_test.h"
       8              : #include "internal/libspdm_requester_lib.h"
       9              : #include "internal/libspdm_responder_lib.h"
      10              : 
      11              : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
      12              : 
      13              : static size_t m_libspdm_local_buffer_size;
      14              : static uint8_t m_libspdm_local_buffer[LIBSPDM_MAX_MESSAGE_TH_BUFFER_SIZE];
      15              : static uint8_t m_libspdm_zero_filled_buffer[LIBSPDM_MAX_HASH_SIZE];
      16              : 
      17              : static libspdm_th_managed_buffer_t th_curr;
      18              : 
      19           40 : static size_t libspdm_test_get_key_exchange_request_size(const void *spdm_context,
      20              :                                                          const void *buffer,
      21              :                                                          size_t buffer_size)
      22              : {
      23              :     const spdm_key_exchange_request_t *spdm_request;
      24              :     size_t message_size;
      25              :     size_t dhe_key_size;
      26              :     uint16_t opaque_length;
      27              : 
      28           40 :     spdm_request = buffer;
      29           40 :     message_size = sizeof(spdm_message_header_t);
      30           40 :     if (buffer_size < message_size) {
      31            0 :         return buffer_size;
      32              :     }
      33              : 
      34           40 :     if (spdm_request->header.request_response_code != SPDM_KEY_EXCHANGE) {
      35            1 :         return buffer_size;
      36              :     }
      37              : 
      38           39 :     message_size = sizeof(spdm_key_exchange_request_t);
      39           39 :     if (buffer_size < message_size) {
      40            0 :         return buffer_size;
      41              :     }
      42              : 
      43           39 :     dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
      44           39 :     message_size += dhe_key_size + sizeof(uint16_t);
      45           39 :     if (buffer_size < message_size) {
      46            0 :         return buffer_size;
      47              :     }
      48              : 
      49           39 :     opaque_length =
      50           39 :         *(uint16_t *)((size_t)buffer +
      51           39 :                       sizeof(spdm_key_exchange_request_t) + dhe_key_size);
      52           39 :     message_size += opaque_length;
      53           39 :     if (buffer_size < message_size) {
      54            0 :         return buffer_size;
      55              :     }
      56              : 
      57              :     /* Good message, return actual size*/
      58           39 :     return message_size;
      59              : }
      60              : 
      61           41 : static libspdm_return_t send_message(
      62              :     void *spdm_context, size_t request_size, const void *request, uint64_t timeout)
      63              : {
      64              :     libspdm_test_context_t *spdm_test_context;
      65              :     size_t header_size;
      66              :     size_t message_size;
      67              : 
      68           41 :     spdm_test_context = libspdm_get_test_context();
      69           41 :     header_size = sizeof(libspdm_test_message_header_t);
      70           41 :     switch (spdm_test_context->case_id) {
      71            1 :     case 0x1:
      72            1 :         return LIBSPDM_STATUS_SEND_FAIL;
      73            1 :     case 0x2:
      74            1 :         m_libspdm_local_buffer_size = 0;
      75            1 :         message_size = libspdm_test_get_key_exchange_request_size(
      76              :             spdm_context, (const uint8_t *)request + header_size,
      77              :             request_size - header_size);
      78            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
      79              :                          (const uint8_t *)request + header_size, message_size);
      80            1 :         m_libspdm_local_buffer_size += message_size;
      81            1 :         return LIBSPDM_STATUS_SUCCESS;
      82            0 :     case 0x3:
      83            0 :         m_libspdm_local_buffer_size = 0;
      84            0 :         message_size = libspdm_test_get_key_exchange_request_size(
      85              :             spdm_context, (const uint8_t *)request + header_size,
      86              :             request_size - header_size);
      87            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
      88              :                          (const uint8_t *)request + header_size, message_size);
      89            0 :         m_libspdm_local_buffer_size += message_size;
      90            0 :         return LIBSPDM_STATUS_SUCCESS;
      91            1 :     case 0x4:
      92            1 :         m_libspdm_local_buffer_size = 0;
      93            1 :         message_size = libspdm_test_get_key_exchange_request_size(
      94              :             spdm_context, (const uint8_t *)request + header_size,
      95              :             request_size - header_size);
      96            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
      97              :                          (const uint8_t *)request + header_size, message_size);
      98            1 :         m_libspdm_local_buffer_size += message_size;
      99            1 :         return LIBSPDM_STATUS_SUCCESS;
     100            1 :     case 0x5:
     101            1 :         m_libspdm_local_buffer_size = 0;
     102            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     103              :             spdm_context, (const uint8_t *)request + header_size,
     104              :             request_size - header_size);
     105            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     106              :                          (const uint8_t *)request + header_size, message_size);
     107            1 :         m_libspdm_local_buffer_size += message_size;
     108            1 :         return LIBSPDM_STATUS_SUCCESS;
     109            0 :     case 0x6:
     110            0 :         m_libspdm_local_buffer_size = 0;
     111            0 :         message_size = libspdm_test_get_key_exchange_request_size(
     112              :             spdm_context, (const uint8_t *)request + header_size,
     113              :             request_size - header_size);
     114            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     115              :                          (const uint8_t *)request + header_size, message_size);
     116            0 :         m_libspdm_local_buffer_size += message_size;
     117            0 :         return LIBSPDM_STATUS_SUCCESS;
     118            1 :     case 0x7:
     119            1 :         m_libspdm_local_buffer_size = 0;
     120            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     121              :             spdm_context, (const uint8_t *)request + header_size,
     122              :             request_size - header_size);
     123            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     124              :                          (const uint8_t *)request + header_size, message_size);
     125            1 :         m_libspdm_local_buffer_size += message_size;
     126            1 :         return LIBSPDM_STATUS_SUCCESS;
     127            2 :     case 0x8:
     128            2 :         m_libspdm_local_buffer_size = 0;
     129            2 :         message_size = libspdm_test_get_key_exchange_request_size(
     130              :             spdm_context, (const uint8_t *)request + header_size,
     131              :             request_size - header_size);
     132            2 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     133              :                          (const uint8_t *)request + header_size, message_size);
     134            2 :         m_libspdm_local_buffer_size += message_size;
     135            2 :         return LIBSPDM_STATUS_SUCCESS;
     136            0 :     case 0x9: {
     137              :         static size_t sub_index = 0;
     138            0 :         if (sub_index == 0) {
     139            0 :             m_libspdm_local_buffer_size = 0;
     140            0 :             message_size = libspdm_test_get_key_exchange_request_size(
     141              :                 spdm_context, (const uint8_t *)request + header_size,
     142              :                 request_size - header_size);
     143            0 :             libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     144              :                              (const uint8_t *)request + header_size, message_size);
     145            0 :             m_libspdm_local_buffer_size += message_size;
     146            0 :             sub_index++;
     147              :         }
     148              :     }
     149            0 :         return LIBSPDM_STATUS_SUCCESS;
     150           18 :     case 0xA:
     151           18 :         m_libspdm_local_buffer_size = 0;
     152           18 :         message_size = libspdm_test_get_key_exchange_request_size(
     153              :             spdm_context, (const uint8_t *)request + header_size,
     154              :             request_size - header_size);
     155           18 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     156              :                          (const uint8_t *)request + header_size, message_size);
     157           18 :         m_libspdm_local_buffer_size += message_size;
     158           18 :         return LIBSPDM_STATUS_SUCCESS;
     159            1 :     case 0xB:
     160            1 :         m_libspdm_local_buffer_size = 0;
     161            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     162              :             spdm_context, (const uint8_t *)request + header_size,
     163              :             request_size - header_size);
     164            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     165              :                          (const uint8_t *)request + header_size, message_size);
     166            1 :         m_libspdm_local_buffer_size += message_size;
     167            1 :         return LIBSPDM_STATUS_SUCCESS;
     168            1 :     case 0xC:
     169            1 :         m_libspdm_local_buffer_size = 0;
     170            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     171              :             spdm_context, (const uint8_t *)request + header_size,
     172              :             request_size - header_size);
     173            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     174              :                          (const uint8_t *)request + header_size, message_size);
     175            1 :         m_libspdm_local_buffer_size += message_size;
     176            1 :         return LIBSPDM_STATUS_SUCCESS;
     177            1 :     case 0xD:
     178            1 :         m_libspdm_local_buffer_size = 0;
     179            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     180              :             spdm_context, (const uint8_t *)request + header_size,
     181              :             request_size - header_size);
     182            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     183              :                          (const uint8_t *)request + header_size, message_size);
     184            1 :         m_libspdm_local_buffer_size += message_size;
     185            1 :         return LIBSPDM_STATUS_SUCCESS;
     186            1 :     case 0xE:
     187            1 :         m_libspdm_local_buffer_size = 0;
     188            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     189              :             spdm_context, (const uint8_t *)request + header_size,
     190              :             request_size - header_size);
     191            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     192              :                          (const uint8_t *)request + header_size, message_size);
     193            1 :         m_libspdm_local_buffer_size += message_size;
     194            1 :         return LIBSPDM_STATUS_SUCCESS;
     195            1 :     case 0xF:
     196            1 :         m_libspdm_local_buffer_size = 0;
     197            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     198              :             spdm_context, (const uint8_t *)request + header_size,
     199              :             request_size - header_size);
     200            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     201              :                          (const uint8_t *)request + header_size, message_size);
     202            1 :         m_libspdm_local_buffer_size += message_size;
     203            1 :         return LIBSPDM_STATUS_SUCCESS;
     204            1 :     case 0x10:
     205            1 :         m_libspdm_local_buffer_size = 0;
     206            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     207              :             spdm_context, (const uint8_t *)request + header_size,
     208              :             request_size - header_size);
     209            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     210              :                          (const uint8_t *)request + header_size, message_size);
     211            1 :         m_libspdm_local_buffer_size += message_size;
     212            1 :         return LIBSPDM_STATUS_SUCCESS;
     213            1 :     case 0x11:
     214            1 :         m_libspdm_local_buffer_size = 0;
     215            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     216              :             spdm_context, (const uint8_t *)request + header_size,
     217              :             request_size - header_size);
     218            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     219              :                          (const uint8_t *)request + header_size, message_size);
     220            1 :         m_libspdm_local_buffer_size += message_size;
     221            1 :         return LIBSPDM_STATUS_SUCCESS;
     222            1 :     case 0x12:
     223            1 :         m_libspdm_local_buffer_size = 0;
     224            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     225              :             spdm_context, (const uint8_t *)request + header_size,
     226              :             request_size - header_size);
     227            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     228              :                          (const uint8_t *)request + header_size, message_size);
     229            1 :         m_libspdm_local_buffer_size += message_size;
     230            1 :         return LIBSPDM_STATUS_SUCCESS;
     231            0 :     case 0x13:
     232            0 :         m_libspdm_local_buffer_size = 0;
     233            0 :         message_size = libspdm_test_get_key_exchange_request_size(
     234              :             spdm_context, (const uint8_t *)request + header_size,
     235              :             request_size - header_size);
     236            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     237              :                          (const uint8_t *)request + header_size, message_size);
     238            0 :         m_libspdm_local_buffer_size += message_size;
     239            0 :         return LIBSPDM_STATUS_SUCCESS;
     240            1 :     case 0x14:
     241            1 :         m_libspdm_local_buffer_size = 0;
     242            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     243              :             spdm_context, (const uint8_t *)request + header_size,
     244              :             request_size - header_size);
     245            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     246              :                          (const uint8_t *)request + header_size, message_size);
     247            1 :         m_libspdm_local_buffer_size += message_size;
     248            1 :         return LIBSPDM_STATUS_SUCCESS;
     249            1 :     case 0x15:
     250            1 :         m_libspdm_local_buffer_size = 0;
     251            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     252              :             spdm_context, (const uint8_t *)request + header_size,
     253              :             request_size - header_size);
     254            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     255              :                          (const uint8_t *)request + header_size, message_size);
     256            1 :         m_libspdm_local_buffer_size += message_size;
     257            1 :         return LIBSPDM_STATUS_SUCCESS;
     258            1 :     case 0x16:
     259            1 :         m_libspdm_local_buffer_size = 0;
     260            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     261              :             spdm_context, (const uint8_t *)request + header_size,
     262              :             request_size - header_size);
     263            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     264              :                          (const uint8_t *)request + header_size, message_size);
     265            1 :         m_libspdm_local_buffer_size += message_size;
     266            1 :         return LIBSPDM_STATUS_SUCCESS;
     267            1 :     case 0x17:
     268            1 :         m_libspdm_local_buffer_size = 0;
     269            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     270              :             spdm_context, (const uint8_t *)request + header_size,
     271              :             request_size - header_size);
     272            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     273              :                          (const uint8_t *)request + header_size, message_size);
     274            1 :         m_libspdm_local_buffer_size += message_size;
     275            1 :         return LIBSPDM_STATUS_SUCCESS;
     276            0 :     case 0x18:
     277            0 :         m_libspdm_local_buffer_size = 0;
     278            0 :         message_size = libspdm_test_get_key_exchange_request_size(
     279              :             spdm_context, (const uint8_t *)request + header_size,
     280              :             request_size - header_size);
     281            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     282              :                          (const uint8_t *)request + header_size, message_size);
     283            0 :         m_libspdm_local_buffer_size += message_size;
     284            0 :         return LIBSPDM_STATUS_SUCCESS;
     285            0 :     case 0x19:
     286            0 :         m_libspdm_local_buffer_size = 0;
     287            0 :         message_size = libspdm_test_get_key_exchange_request_size(
     288              :             spdm_context, (const uint8_t *)request + header_size,
     289              :             request_size - header_size);
     290            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     291              :                          (const uint8_t *)request + header_size, message_size);
     292            0 :         m_libspdm_local_buffer_size += message_size;
     293            0 :         return LIBSPDM_STATUS_SUCCESS;
     294            1 :     case 0x1A:
     295            1 :         m_libspdm_local_buffer_size = 0;
     296            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     297              :             spdm_context, (const uint8_t *)request + header_size,
     298              :             request_size - header_size);
     299            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     300              :                          (const uint8_t *)request + header_size, message_size);
     301            1 :         m_libspdm_local_buffer_size += message_size;
     302            1 :         return LIBSPDM_STATUS_SUCCESS;
     303            1 :     case 0x1B:
     304            1 :         m_libspdm_local_buffer_size = 0;
     305            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     306              :             spdm_context, (const uint8_t *)request + header_size,
     307              :             request_size - header_size);
     308            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     309              :                          (const uint8_t *)request + header_size, message_size);
     310            1 :         m_libspdm_local_buffer_size += message_size;
     311            1 :         return LIBSPDM_STATUS_SUCCESS;
     312            1 :     case 0x1C:
     313            1 :         m_libspdm_local_buffer_size = 0;
     314            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     315              :             spdm_context, (const uint8_t *)request + header_size,
     316              :             request_size - header_size);
     317            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     318              :                          (const uint8_t *)request + header_size, message_size);
     319            1 :         m_libspdm_local_buffer_size += message_size;
     320            1 :         return LIBSPDM_STATUS_SUCCESS;
     321            0 :     case 0x1D:
     322            0 :         m_libspdm_local_buffer_size = 0;
     323            0 :         message_size = libspdm_test_get_key_exchange_request_size(
     324              :             spdm_context, (const uint8_t *)request + header_size,
     325              :             request_size - header_size);
     326            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     327              :                          (const uint8_t *)request + header_size,  message_size);
     328            0 :         m_libspdm_local_buffer_size += message_size;
     329            0 :         return LIBSPDM_STATUS_SUCCESS;
     330            0 :     case 0x1E:
     331            0 :         m_libspdm_local_buffer_size = 0;
     332            0 :         message_size = libspdm_test_get_key_exchange_request_size(
     333              :             spdm_context, (const uint8_t *)request + header_size,
     334              :             request_size - header_size);
     335            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     336              :                          (const uint8_t *)request + header_size, message_size);
     337            0 :         m_libspdm_local_buffer_size += message_size;
     338            0 :         return LIBSPDM_STATUS_SUCCESS;
     339            1 :     case 0x1F:
     340            1 :         m_libspdm_local_buffer_size = 0;
     341            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     342              :             spdm_context, (const uint8_t *)request + header_size,
     343              :             request_size - header_size);
     344            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     345              :                          (const uint8_t *)request + header_size, message_size);
     346            1 :         m_libspdm_local_buffer_size += message_size;
     347            1 :         return LIBSPDM_STATUS_SUCCESS;
     348            0 :     default:
     349            0 :         return LIBSPDM_STATUS_SEND_FAIL;
     350              :     }
     351              : }
     352              : 
     353           39 : static libspdm_return_t receive_message(
     354              :     void *spdm_context, size_t *response_size, void **response, uint64_t timeout)
     355              : {
     356              :     libspdm_test_context_t *spdm_test_context;
     357              : 
     358           39 :     spdm_test_context = libspdm_get_test_context();
     359           39 :     switch (spdm_test_context->case_id) {
     360            0 :     case 0x1:
     361            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     362              : 
     363            1 :     case 0x2: {
     364              :         spdm_key_exchange_response_t *spdm_response;
     365              :         size_t dhe_key_size;
     366              :         uint32_t hash_size;
     367              :         size_t signature_size;
     368              :         uint32_t hmac_size;
     369              :         uint8_t *ptr;
     370              :         void *dhe_context;
     371              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
     372              :         size_t final_key_size;
     373              :         size_t opaque_key_exchange_rsp_size;
     374              :         void *data;
     375              :         size_t data_size;
     376              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     377              :         uint8_t *cert_buffer;
     378              :         size_t cert_buffer_size;
     379              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     380              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
     381              :         uint8_t bin_str0[128];
     382              :         size_t bin_str0_size;
     383              :         uint8_t bin_str2[128];
     384              :         size_t bin_str2_size;
     385              :         uint8_t bin_str7[128];
     386              :         size_t bin_str7_size;
     387              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     388              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     389              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     390              :         size_t spdm_response_size;
     391              :         size_t transport_header_size;
     392              : 
     393              : 
     394              :         ((libspdm_context_t *)spdm_context)
     395            1 :         ->connection_info.algorithm.base_asym_algo =
     396              :             m_libspdm_use_asym_algo;
     397              :         ((libspdm_context_t *)spdm_context)
     398            1 :         ->connection_info.algorithm.base_hash_algo =
     399              :             m_libspdm_use_hash_algo;
     400              :         ((libspdm_context_t *)spdm_context)
     401            1 :         ->connection_info.algorithm.dhe_named_group =
     402              :             m_libspdm_use_dhe_algo;
     403              :         ((libspdm_context_t *)spdm_context)
     404            1 :         ->connection_info.algorithm.measurement_hash_algo =
     405              :             m_libspdm_use_measurement_hash_algo;
     406            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
     407            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     408            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     409            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
     410            1 :         opaque_key_exchange_rsp_size =
     411            1 :             libspdm_get_opaque_data_version_selection_data_size(
     412              :                 spdm_context);
     413            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
     414            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
     415            1 :                              opaque_key_exchange_rsp_size + signature_size +
     416              :                              hmac_size;
     417            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     418            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     419              : 
     420            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     421            1 :         spdm_response->header.request_response_code =
     422              :             SPDM_KEY_EXCHANGE_RSP;
     423            1 :         spdm_response->header.param1 = 0;
     424            1 :         spdm_response->rsp_session_id =
     425            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
     426            1 :         spdm_response->mut_auth_requested = 0;
     427            1 :         spdm_response->req_slot_id_param = 0;
     428            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
     429            1 :                                   spdm_response->random_data);
     430            1 :         ptr = (void *)(spdm_response + 1);
     431            1 :         dhe_context = libspdm_dhe_new(
     432            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     433              :                 m_libspdm_use_dhe_algo,
     434              :                 true);
     435            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
     436              :                                  &dhe_key_size);
     437            1 :         final_key_size = sizeof(final_key);
     438            1 :         libspdm_dhe_compute_key(
     439              :             m_libspdm_use_dhe_algo, dhe_context,
     440            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
     441              :             sizeof(spdm_key_exchange_request_t),
     442              :             dhe_key_size, final_key, &final_key_size);
     443            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
     444            1 :         ptr += dhe_key_size;
     445              :         /* libspdm_zero_mem (ptr, hash_size);
     446              :          * ptr += hash_size;*/
     447            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
     448            1 :         ptr += sizeof(uint16_t);
     449            1 :         libspdm_build_opaque_data_version_selection_data(
     450              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
     451              :                 &opaque_key_exchange_rsp_size, ptr);
     452            1 :         ptr += opaque_key_exchange_rsp_size;
     453            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     454              :                                                         m_libspdm_use_asym_algo, &data,
     455              :                                                         &data_size, NULL, NULL);
     456            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     457              :                          sizeof(m_libspdm_local_buffer)
     458            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     459              :                             m_libspdm_local_buffer),
     460            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
     461            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
     462            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
     463              :                        m_libspdm_local_buffer_size));
     464            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
     465            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     466            1 :         cert_buffer = (uint8_t *)data;
     467            1 :         cert_buffer_size = data_size;
     468            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     469              :                          cert_buffer_hash);
     470              :         /* transcript.message_a size is 0*/
     471            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     472            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     473              :                                       m_libspdm_local_buffer_size);
     474            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     475              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     476            1 :         free(data);
     477            1 :         libspdm_responder_data_sign(
     478              :             spdm_context,
     479            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     480              :                 SPDM_KEY_EXCHANGE_RSP,
     481              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
     482            1 :                 false, libspdm_get_managed_buffer(&th_curr),
     483              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
     484              :                 &signature_size);
     485            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     486              :                          sizeof(m_libspdm_local_buffer)
     487            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     488              :                             m_libspdm_local_buffer),
     489              :                          ptr, signature_size);
     490            1 :         m_libspdm_local_buffer_size += signature_size;
     491            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
     492            1 :         ptr += signature_size;
     493            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     494              :                          libspdm_get_managed_buffer_size(&th_curr),
     495              :                          th_curr_hash_data);
     496            1 :         bin_str0_size = sizeof(bin_str0);
     497            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     498              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
     499            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
     500              :                            &bin_str0_size);
     501            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
     502              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
     503            1 :         bin_str2_size = sizeof(bin_str2);
     504            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     505              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
     506            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
     507              :                            bin_str2, &bin_str2_size);
     508            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
     509              :                             bin_str2, bin_str2_size,
     510              :                             response_handshake_secret, hash_size);
     511            1 :         bin_str7_size = sizeof(bin_str7);
     512            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     513              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
     514            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
     515              :                            &bin_str7_size);
     516            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
     517              :                             hash_size, bin_str7, bin_str7_size,
     518              :                             response_finished_key, hash_size);
     519            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     520              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     521            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     522              :                          response_finished_key, hash_size, ptr);
     523            1 :         ptr += hmac_size;
     524              : 
     525            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     526              :                                               false, spdm_response_size,
     527              :                                               spdm_response, response_size,
     528              :                                               response);
     529              :     }
     530            1 :         return LIBSPDM_STATUS_SUCCESS;
     531              : 
     532            0 :     case 0x3: {
     533              :         spdm_key_exchange_response_t *spdm_response;
     534              :         size_t dhe_key_size;
     535              :         uint32_t hash_size;
     536              :         size_t signature_size;
     537              :         uint32_t hmac_size;
     538              :         uint8_t *ptr;
     539              :         void *dhe_context;
     540              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
     541              :         size_t final_key_size;
     542              :         size_t opaque_key_exchange_rsp_size;
     543              :         void *data;
     544              :         size_t data_size;
     545              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     546              :         uint8_t *cert_buffer;
     547              :         size_t cert_buffer_size;
     548              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     549              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
     550              :         uint8_t bin_str0[128];
     551              :         size_t bin_str0_size;
     552              :         uint8_t bin_str2[128];
     553              :         size_t bin_str2_size;
     554              :         uint8_t bin_str7[128];
     555              :         size_t bin_str7_size;
     556              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     557              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     558              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     559              :         size_t spdm_response_size;
     560              :         size_t transport_header_size;
     561              : 
     562              :         ((libspdm_context_t *)spdm_context)
     563            0 :         ->connection_info.algorithm.base_asym_algo =
     564              :             m_libspdm_use_asym_algo;
     565              :         ((libspdm_context_t *)spdm_context)
     566            0 :         ->connection_info.algorithm.base_hash_algo =
     567              :             m_libspdm_use_hash_algo;
     568              :         ((libspdm_context_t *)spdm_context)
     569            0 :         ->connection_info.algorithm.dhe_named_group =
     570              :             m_libspdm_use_dhe_algo;
     571              :         ((libspdm_context_t *)spdm_context)
     572            0 :         ->connection_info.algorithm.measurement_hash_algo =
     573              :             m_libspdm_use_measurement_hash_algo;
     574            0 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
     575            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     576            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     577            0 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
     578            0 :         opaque_key_exchange_rsp_size =
     579            0 :             libspdm_get_opaque_data_version_selection_data_size(
     580              :                 spdm_context);
     581            0 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
     582            0 :                              dhe_key_size + 0 + sizeof(uint16_t) +
     583            0 :                              opaque_key_exchange_rsp_size + signature_size +
     584              :                              hmac_size;
     585            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     586            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     587              : 
     588            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     589            0 :         spdm_response->header.request_response_code =
     590              :             SPDM_KEY_EXCHANGE_RSP;
     591            0 :         spdm_response->header.param1 = 0;
     592            0 :         spdm_response->rsp_session_id =
     593            0 :             libspdm_allocate_rsp_session_id(spdm_context, false);
     594            0 :         spdm_response->mut_auth_requested = 0;
     595            0 :         spdm_response->req_slot_id_param = 0;
     596            0 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
     597            0 :                                   spdm_response->random_data);
     598            0 :         ptr = (void *)(spdm_response + 1);
     599            0 :         dhe_context = libspdm_dhe_new(
     600            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     601              :                 m_libspdm_use_dhe_algo,
     602              :                 true);
     603            0 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
     604              :                                  &dhe_key_size);
     605            0 :         final_key_size = sizeof(final_key);
     606            0 :         libspdm_dhe_compute_key(
     607              :             m_libspdm_use_dhe_algo, dhe_context,
     608            0 :             (uint8_t *)&m_libspdm_local_buffer[0] +
     609              :             sizeof(spdm_key_exchange_request_t),
     610              :             dhe_key_size, final_key, &final_key_size);
     611            0 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
     612            0 :         ptr += dhe_key_size;
     613              :         /* libspdm_zero_mem (ptr, hash_size);
     614              :          * ptr += hash_size;*/
     615            0 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
     616            0 :         ptr += sizeof(uint16_t);
     617            0 :         libspdm_build_opaque_data_version_selection_data(
     618              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
     619              :                 &opaque_key_exchange_rsp_size, ptr);
     620            0 :         ptr += opaque_key_exchange_rsp_size;
     621            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     622              :                                                         m_libspdm_use_asym_algo, &data,
     623              :                                                         &data_size, NULL, NULL);
     624            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     625              :                          sizeof(m_libspdm_local_buffer)
     626            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     627              :                             m_libspdm_local_buffer),
     628            0 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
     629            0 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
     630            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
     631              :                        m_libspdm_local_buffer_size));
     632            0 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
     633            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     634            0 :         cert_buffer = (uint8_t *)data;
     635            0 :         cert_buffer_size = data_size;
     636            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     637              :                          cert_buffer_hash);
     638              :         /* transcript.message_a size is 0*/
     639            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     640            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     641              :                                       m_libspdm_local_buffer_size);
     642            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     643              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     644            0 :         free(data);
     645            0 :         libspdm_responder_data_sign(
     646              :             spdm_context,
     647            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     648              :                 SPDM_KEY_EXCHANGE_RSP,
     649              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
     650            0 :                 false, libspdm_get_managed_buffer(&th_curr),
     651              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
     652              :                 &signature_size);
     653            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     654              :                          sizeof(m_libspdm_local_buffer)
     655            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     656              :                             m_libspdm_local_buffer),
     657              :                          ptr, signature_size);
     658            0 :         m_libspdm_local_buffer_size += signature_size;
     659            0 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
     660            0 :         ptr += signature_size;
     661            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     662              :                          libspdm_get_managed_buffer_size(&th_curr),
     663              :                          th_curr_hash_data);
     664            0 :         bin_str0_size = sizeof(bin_str0);
     665            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     666              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
     667            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
     668              :                            &bin_str0_size);
     669            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
     670              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
     671            0 :         bin_str2_size = sizeof(bin_str2);
     672            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     673              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
     674            0 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
     675              :                            bin_str2, &bin_str2_size);
     676            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
     677              :                             bin_str2, bin_str2_size,
     678              :                             response_handshake_secret, hash_size);
     679            0 :         bin_str7_size = sizeof(bin_str7);
     680            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     681              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
     682            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
     683              :                            &bin_str7_size);
     684            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
     685              :                             hash_size, bin_str7, bin_str7_size,
     686              :                             response_finished_key, hash_size);
     687            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     688              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     689            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     690              :                          response_finished_key, hash_size, ptr);
     691            0 :         ptr += hmac_size;
     692              : 
     693            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     694              :                                               false, spdm_response_size,
     695              :                                               spdm_response, response_size,
     696              :                                               response);
     697              :     }
     698            0 :         return LIBSPDM_STATUS_SUCCESS;
     699              : 
     700            1 :     case 0x4: {
     701              :         spdm_error_response_t *spdm_response;
     702              :         size_t spdm_response_size;
     703              :         size_t transport_header_size;
     704              : 
     705            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     706            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     707            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     708              : 
     709            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     710            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     711            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_INVALID_REQUEST;
     712            1 :         spdm_response->header.param2 = 0;
     713              : 
     714            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     715              :                                               false, spdm_response_size,
     716              :                                               spdm_response,
     717              :                                               response_size, response);
     718              :     }
     719            1 :         return LIBSPDM_STATUS_SUCCESS;
     720              : 
     721            1 :     case 0x5: {
     722              :         spdm_error_response_t *spdm_response;
     723              :         size_t spdm_response_size;
     724              :         size_t transport_header_size;
     725              : 
     726            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     727            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     728            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     729              : 
     730            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     731            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     732            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     733            1 :         spdm_response->header.param2 = 0;
     734              : 
     735            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     736              :                                               false, spdm_response_size,
     737              :                                               spdm_response,
     738              :                                               response_size, response);
     739              :     }
     740            1 :         return LIBSPDM_STATUS_SUCCESS;
     741              : 
     742            0 :     case 0x6: {
     743              :         static size_t sub_index1 = 0;
     744            0 :         if (sub_index1 == 0) {
     745              :             spdm_error_response_t *spdm_response;
     746              :             size_t spdm_response_size;
     747              :             size_t transport_header_size;
     748              : 
     749            0 :             spdm_response_size = sizeof(spdm_error_response_t);
     750            0 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     751            0 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     752              : 
     753            0 :             spdm_response->header.spdm_version =
     754              :                 SPDM_MESSAGE_VERSION_11;
     755            0 :             spdm_response->header.request_response_code = SPDM_ERROR;
     756            0 :             spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     757            0 :             spdm_response->header.param2 = 0;
     758              : 
     759            0 :             libspdm_transport_test_encode_message(
     760              :                 spdm_context, NULL, false, false,
     761              :                 spdm_response_size, spdm_response,
     762              :                 response_size, response);
     763            0 :             sub_index1++;
     764            0 :         } else if (sub_index1 == 1) {
     765              :             spdm_key_exchange_response_t *spdm_response;
     766              :             size_t dhe_key_size;
     767              :             uint32_t hash_size;
     768              :             size_t signature_size;
     769              :             uint32_t hmac_size;
     770              :             uint8_t *ptr;
     771              :             void *dhe_context;
     772              :             uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
     773              :             size_t final_key_size;
     774              :             size_t opaque_key_exchange_rsp_size;
     775              :             void *data;
     776              :             size_t data_size;
     777              :             uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     778              :             uint8_t *cert_buffer;
     779              :             size_t cert_buffer_size;
     780              :             uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     781              :             uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
     782              :             uint8_t bin_str0[128];
     783              :             size_t bin_str0_size;
     784              :             uint8_t bin_str2[128];
     785              :             size_t bin_str2_size;
     786              :             uint8_t bin_str7[128];
     787              :             size_t bin_str7_size;
     788              :             uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     789              :             uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     790              :             uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     791              :             size_t spdm_response_size;
     792              :             size_t transport_header_size;
     793              : 
     794              :             ((libspdm_context_t *)spdm_context)
     795            0 :             ->connection_info.algorithm.base_asym_algo =
     796              :                 m_libspdm_use_asym_algo;
     797              :             ((libspdm_context_t *)spdm_context)
     798            0 :             ->connection_info.algorithm.base_hash_algo =
     799              :                 m_libspdm_use_hash_algo;
     800              :             ((libspdm_context_t *)spdm_context)
     801            0 :             ->connection_info.algorithm.dhe_named_group =
     802              :                 m_libspdm_use_dhe_algo;
     803              :             ((libspdm_context_t *)spdm_context)
     804              :             ->connection_info.algorithm
     805            0 :             .measurement_hash_algo =
     806              :                 m_libspdm_use_measurement_hash_algo;
     807            0 :             signature_size =
     808            0 :                 libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
     809            0 :             hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     810            0 :             hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     811            0 :             dhe_key_size =
     812            0 :                 libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
     813            0 :             opaque_key_exchange_rsp_size =
     814            0 :                 libspdm_get_opaque_data_version_selection_data_size(
     815              :                     spdm_context);
     816            0 :             spdm_response_size = sizeof(spdm_key_exchange_response_t) +
     817            0 :                                  dhe_key_size + 0 + sizeof(uint16_t) +
     818            0 :                                  opaque_key_exchange_rsp_size +
     819            0 :                                  signature_size + hmac_size;
     820            0 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     821            0 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     822              : 
     823            0 :             spdm_response->header.spdm_version =
     824              :                 SPDM_MESSAGE_VERSION_11;
     825            0 :             spdm_response->header.request_response_code =
     826              :                 SPDM_KEY_EXCHANGE_RSP;
     827            0 :             spdm_response->header.param1 = 0;
     828            0 :             spdm_response->rsp_session_id =
     829            0 :                 libspdm_allocate_rsp_session_id(spdm_context, false);
     830            0 :             spdm_response->mut_auth_requested = 0;
     831            0 :             spdm_response->req_slot_id_param = 0;
     832            0 :             libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
     833            0 :                                       spdm_response->random_data);
     834            0 :             ptr = (void *)(spdm_response + 1);
     835            0 :             dhe_context = libspdm_dhe_new(
     836            0 :                 spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     837              :                     m_libspdm_use_dhe_algo, true);
     838            0 :             libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
     839              :                                      &dhe_key_size);
     840            0 :             final_key_size = sizeof(final_key);
     841            0 :             libspdm_dhe_compute_key(
     842              :                 m_libspdm_use_dhe_algo, dhe_context,
     843            0 :                 (uint8_t *)&m_libspdm_local_buffer[0] +
     844              :                 sizeof(spdm_key_exchange_request_t),
     845              :                 dhe_key_size, final_key, &final_key_size);
     846            0 :             libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
     847            0 :             ptr += dhe_key_size;
     848              :             /* libspdm_zero_mem (ptr, hash_size);
     849              :              * ptr += hash_size;*/
     850            0 :             *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
     851            0 :             ptr += sizeof(uint16_t);
     852            0 :             libspdm_build_opaque_data_version_selection_data(
     853              :                 spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
     854              :                     &opaque_key_exchange_rsp_size, ptr);
     855            0 :             ptr += opaque_key_exchange_rsp_size;
     856            0 :             libspdm_read_responder_public_certificate_chain(
     857              :                 m_libspdm_use_hash_algo, m_libspdm_use_asym_algo, &data,
     858              :                 &data_size, NULL, NULL);
     859            0 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     860              :                              sizeof(m_libspdm_local_buffer)
     861            0 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     862              :                                 m_libspdm_local_buffer),
     863            0 :                              spdm_response, (size_t)ptr - (size_t)spdm_response);
     864            0 :             m_libspdm_local_buffer_size +=
     865            0 :                 ((size_t)ptr - (size_t)spdm_response);
     866            0 :             LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
     867              :                            m_libspdm_local_buffer_size));
     868            0 :             libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
     869            0 :             libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     870            0 :             cert_buffer = (uint8_t *)data;
     871            0 :             cert_buffer_size = data_size;
     872            0 :             libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer,
     873              :                              cert_buffer_size, cert_buffer_hash);
     874              :             /* transcript.message_a size is 0*/
     875            0 :             libspdm_append_managed_buffer(&th_curr, cert_buffer_hash,
     876              :                                           hash_size);
     877            0 :             libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     878              :                                           m_libspdm_local_buffer_size);
     879            0 :             libspdm_hash_all(m_libspdm_use_hash_algo,
     880            0 :                              libspdm_get_managed_buffer(&th_curr),
     881              :                              libspdm_get_managed_buffer_size(&th_curr),
     882              :                              hash_data);
     883            0 :             free(data);
     884            0 :             libspdm_responder_data_sign(
     885              :                 spdm_context,
     886            0 :                 spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     887              :                     SPDM_KEY_EXCHANGE_RSP,
     888              :                     m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
     889            0 :                     false, libspdm_get_managed_buffer(&th_curr),
     890              :                     libspdm_get_managed_buffer_size(&th_curr), ptr,
     891              :                     &signature_size);
     892            0 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     893              :                              sizeof(m_libspdm_local_buffer)
     894            0 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     895              :                                 m_libspdm_local_buffer),
     896              :                              ptr, signature_size);
     897            0 :             m_libspdm_local_buffer_size += signature_size;
     898            0 :             libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
     899            0 :             ptr += signature_size;
     900            0 :             libspdm_hash_all(m_libspdm_use_hash_algo,
     901            0 :                              libspdm_get_managed_buffer(&th_curr),
     902              :                              libspdm_get_managed_buffer_size(&th_curr),
     903              :                              th_curr_hash_data);
     904            0 :             bin_str0_size = sizeof(bin_str0);
     905            0 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     906              :                                SPDM_BIN_STR_0_LABEL,
     907              :                                sizeof(SPDM_BIN_STR_0_LABEL) - 1, NULL,
     908            0 :                                (uint16_t)hash_size, hash_size, bin_str0,
     909              :                                &bin_str0_size);
     910            0 :             libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
     911              :                              m_libspdm_zero_filled_buffer, hash_size, handshake_secret);
     912            0 :             bin_str2_size = sizeof(bin_str2);
     913            0 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     914              :                                SPDM_BIN_STR_2_LABEL,
     915              :                                sizeof(SPDM_BIN_STR_2_LABEL) - 1,
     916            0 :                                th_curr_hash_data, (uint16_t)hash_size,
     917              :                                hash_size, bin_str2, &bin_str2_size);
     918            0 :             libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret,
     919              :                                 hash_size, bin_str2, bin_str2_size,
     920              :                                 response_handshake_secret, hash_size);
     921            0 :             bin_str7_size = sizeof(bin_str7);
     922            0 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     923              :                                SPDM_BIN_STR_7_LABEL,
     924              :                                sizeof(SPDM_BIN_STR_7_LABEL) - 1, NULL,
     925            0 :                                (uint16_t)hash_size, hash_size, bin_str7,
     926              :                                &bin_str7_size);
     927            0 :             libspdm_hkdf_expand(m_libspdm_use_hash_algo,
     928              :                                 response_handshake_secret, hash_size,
     929              :                                 bin_str7, bin_str7_size,
     930              :                                 response_finished_key, hash_size);
     931            0 :             libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     932              :                              libspdm_get_managed_buffer_size(&th_curr), hash_data);
     933            0 :             libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     934              :                              response_finished_key, hash_size, ptr);
     935            0 :             ptr += hmac_size;
     936              : 
     937            0 :             libspdm_transport_test_encode_message(
     938              :                 spdm_context, NULL, false, false, spdm_response_size,
     939              :                 spdm_response, response_size, response);
     940              :         }
     941              :     }
     942            0 :         return LIBSPDM_STATUS_SUCCESS;
     943              : 
     944            1 :     case 0x7: {
     945              :         spdm_error_response_t *spdm_response;
     946              :         size_t spdm_response_size;
     947              :         size_t transport_header_size;
     948              : 
     949            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     950            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     951            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     952              : 
     953            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     954            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     955            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
     956            1 :         spdm_response->header.param2 = 0;
     957              : 
     958            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     959              :                                               false, spdm_response_size,
     960              :                                               spdm_response,
     961              :                                               response_size, response);
     962              :     }
     963            1 :         return LIBSPDM_STATUS_SUCCESS;
     964              : 
     965            2 :     case 0x8: {
     966              :         spdm_error_response_data_response_not_ready_t *spdm_response;
     967              :         size_t spdm_response_size;
     968              :         size_t transport_header_size;
     969              : 
     970            2 :         spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
     971            2 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     972            2 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     973              : 
     974            2 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     975            2 :         spdm_response->header.request_response_code = SPDM_ERROR;
     976            2 :         spdm_response->header.param1 =
     977              :             SPDM_ERROR_CODE_RESPONSE_NOT_READY;
     978            2 :         spdm_response->header.param2 = 0;
     979            2 :         spdm_response->extend_error_data.rd_exponent = 1;
     980            2 :         spdm_response->extend_error_data.rd_tm = 2;
     981            2 :         spdm_response->extend_error_data.request_code =
     982              :             SPDM_KEY_EXCHANGE;
     983            2 :         spdm_response->extend_error_data.token = 0;
     984              : 
     985            2 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     986              :                                               false, spdm_response_size,
     987              :                                               spdm_response,
     988              :                                               response_size, response);
     989              :     }
     990            2 :         return LIBSPDM_STATUS_SUCCESS;
     991              : 
     992            0 :     case 0x9: {
     993              :         static size_t sub_index2 = 0;
     994            0 :         if (sub_index2 == 0) {
     995              :             spdm_error_response_data_response_not_ready_t
     996              :             *spdm_response;
     997              :             size_t spdm_response_size;
     998              :             size_t transport_header_size;
     999              : 
    1000            0 :             spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
    1001            0 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1002            0 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1003              : 
    1004            0 :             spdm_response->header.spdm_version =
    1005              :                 SPDM_MESSAGE_VERSION_11;
    1006            0 :             spdm_response->header.request_response_code = SPDM_ERROR;
    1007            0 :             spdm_response->header.param1 =
    1008              :                 SPDM_ERROR_CODE_RESPONSE_NOT_READY;
    1009            0 :             spdm_response->header.param2 = 0;
    1010            0 :             spdm_response->extend_error_data.rd_exponent = 1;
    1011            0 :             spdm_response->extend_error_data.rd_tm = 2;
    1012            0 :             spdm_response->extend_error_data.request_code =
    1013              :                 SPDM_KEY_EXCHANGE;
    1014            0 :             spdm_response->extend_error_data.token = 1;
    1015              : 
    1016            0 :             libspdm_transport_test_encode_message(
    1017              :                 spdm_context, NULL, false, false,
    1018              :                 spdm_response_size, spdm_response,
    1019              :                 response_size, response);
    1020            0 :             sub_index2++;
    1021            0 :         } else if (sub_index2 == 1) {
    1022              :             spdm_key_exchange_response_t *spdm_response;
    1023              :             size_t dhe_key_size;
    1024              :             uint32_t hash_size;
    1025              :             size_t signature_size;
    1026              :             uint32_t hmac_size;
    1027              :             uint8_t *ptr;
    1028              :             void *dhe_context;
    1029              :             uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1030              :             size_t final_key_size;
    1031              :             size_t opaque_key_exchange_rsp_size;
    1032              :             void *data;
    1033              :             size_t data_size;
    1034              :             uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1035              :             uint8_t *cert_buffer;
    1036              :             size_t cert_buffer_size;
    1037              :             uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1038              :             uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1039              :             uint8_t bin_str0[128];
    1040              :             size_t bin_str0_size;
    1041              :             uint8_t bin_str2[128];
    1042              :             size_t bin_str2_size;
    1043              :             uint8_t bin_str7[128];
    1044              :             size_t bin_str7_size;
    1045              :             uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1046              :             uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1047              :             uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1048              :             size_t spdm_response_size;
    1049              :             size_t transport_header_size;
    1050              : 
    1051              :             ((libspdm_context_t *)spdm_context)
    1052            0 :             ->connection_info.algorithm.base_asym_algo =
    1053              :                 m_libspdm_use_asym_algo;
    1054              :             ((libspdm_context_t *)spdm_context)
    1055            0 :             ->connection_info.algorithm.base_hash_algo =
    1056              :                 m_libspdm_use_hash_algo;
    1057              :             ((libspdm_context_t *)spdm_context)
    1058            0 :             ->connection_info.algorithm.dhe_named_group =
    1059              :                 m_libspdm_use_dhe_algo;
    1060              :             ((libspdm_context_t *)spdm_context)
    1061              :             ->connection_info.algorithm
    1062            0 :             .measurement_hash_algo =
    1063              :                 m_libspdm_use_measurement_hash_algo;
    1064            0 :             signature_size =
    1065            0 :                 libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1066            0 :             hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1067            0 :             hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1068            0 :             dhe_key_size =
    1069            0 :                 libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1070            0 :             opaque_key_exchange_rsp_size =
    1071            0 :                 libspdm_get_opaque_data_version_selection_data_size(
    1072              :                     spdm_context);
    1073            0 :             spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1074            0 :                                  dhe_key_size + 0 + sizeof(uint16_t) +
    1075            0 :                                  opaque_key_exchange_rsp_size +
    1076            0 :                                  signature_size + hmac_size;
    1077            0 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1078            0 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1079              : 
    1080            0 :             spdm_response->header.spdm_version =
    1081              :                 SPDM_MESSAGE_VERSION_11;
    1082            0 :             spdm_response->header.request_response_code =
    1083              :                 SPDM_KEY_EXCHANGE_RSP;
    1084            0 :             spdm_response->header.param1 = 0;
    1085            0 :             spdm_response->rsp_session_id =
    1086            0 :                 libspdm_allocate_rsp_session_id(spdm_context, false);
    1087            0 :             spdm_response->mut_auth_requested = 0;
    1088            0 :             spdm_response->req_slot_id_param = 0;
    1089            0 :             libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1090            0 :                                       spdm_response->random_data);
    1091            0 :             ptr = (void *)(spdm_response + 1);
    1092            0 :             dhe_context = libspdm_dhe_new(
    1093            0 :                 spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1094              :                     m_libspdm_use_dhe_algo, true);
    1095            0 :             libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1096              :                                      &dhe_key_size);
    1097            0 :             final_key_size = sizeof(final_key);
    1098            0 :             libspdm_dhe_compute_key(
    1099              :                 m_libspdm_use_dhe_algo, dhe_context,
    1100            0 :                 (uint8_t *)&m_libspdm_local_buffer[0] +
    1101              :                 sizeof(spdm_key_exchange_request_t),
    1102              :                 dhe_key_size, final_key, &final_key_size);
    1103            0 :             libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1104            0 :             ptr += dhe_key_size;
    1105              :             /* libspdm_zero_mem (ptr, hash_size);
    1106              :              * ptr += hash_size;*/
    1107            0 :             *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1108            0 :             ptr += sizeof(uint16_t);
    1109            0 :             libspdm_build_opaque_data_version_selection_data(
    1110              :                 spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1111              :                     &opaque_key_exchange_rsp_size, ptr);
    1112            0 :             ptr += opaque_key_exchange_rsp_size;
    1113            0 :             libspdm_read_responder_public_certificate_chain(
    1114              :                 m_libspdm_use_hash_algo, m_libspdm_use_asym_algo, &data,
    1115              :                 &data_size, NULL, NULL);
    1116            0 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1117              :                              sizeof(m_libspdm_local_buffer)
    1118            0 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1119              :                                 m_libspdm_local_buffer),
    1120            0 :                              spdm_response, (size_t)ptr - (size_t)spdm_response);
    1121            0 :             m_libspdm_local_buffer_size +=
    1122            0 :                 ((size_t)ptr - (size_t)spdm_response);
    1123            0 :             LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1124              :                            m_libspdm_local_buffer_size));
    1125            0 :             libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1126            0 :             libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1127            0 :             cert_buffer = (uint8_t *)data;
    1128            0 :             cert_buffer_size = data_size;
    1129            0 :             libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer,
    1130              :                              cert_buffer_size, cert_buffer_hash);
    1131              :             /* transcript.message_a size is 0*/
    1132            0 :             libspdm_append_managed_buffer(&th_curr, cert_buffer_hash,
    1133              :                                           hash_size);
    1134            0 :             libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1135              :                                           m_libspdm_local_buffer_size);
    1136            0 :             libspdm_hash_all(m_libspdm_use_hash_algo,
    1137            0 :                              libspdm_get_managed_buffer(&th_curr),
    1138              :                              libspdm_get_managed_buffer_size(&th_curr),
    1139              :                              hash_data);
    1140            0 :             free(data);
    1141            0 :             libspdm_responder_data_sign(
    1142              :                 spdm_context,
    1143            0 :                 spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1144              :                     SPDM_KEY_EXCHANGE_RSP,
    1145              :                     m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1146            0 :                     false, libspdm_get_managed_buffer(&th_curr),
    1147              :                     libspdm_get_managed_buffer_size(&th_curr), ptr,
    1148              :                     &signature_size);
    1149            0 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1150              :                              sizeof(m_libspdm_local_buffer)
    1151            0 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1152              :                                 m_libspdm_local_buffer),
    1153              :                              ptr, signature_size);
    1154            0 :             m_libspdm_local_buffer_size += signature_size;
    1155            0 :             libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1156            0 :             ptr += signature_size;
    1157            0 :             libspdm_hash_all(m_libspdm_use_hash_algo,
    1158            0 :                              libspdm_get_managed_buffer(&th_curr),
    1159              :                              libspdm_get_managed_buffer_size(&th_curr),
    1160              :                              th_curr_hash_data);
    1161            0 :             bin_str0_size = sizeof(bin_str0);
    1162            0 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1163              :                                SPDM_BIN_STR_0_LABEL,
    1164              :                                sizeof(SPDM_BIN_STR_0_LABEL) - 1, NULL,
    1165            0 :                                (uint16_t)hash_size, hash_size, bin_str0,
    1166              :                                &bin_str0_size);
    1167            0 :             libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1168              :                              m_libspdm_zero_filled_buffer, hash_size, handshake_secret);
    1169            0 :             bin_str2_size = sizeof(bin_str2);
    1170            0 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1171              :                                SPDM_BIN_STR_2_LABEL,
    1172              :                                sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1173            0 :                                th_curr_hash_data, (uint16_t)hash_size,
    1174              :                                hash_size, bin_str2, &bin_str2_size);
    1175            0 :             libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret,
    1176              :                                 hash_size, bin_str2, bin_str2_size,
    1177              :                                 response_handshake_secret, hash_size);
    1178            0 :             bin_str7_size = sizeof(bin_str7);
    1179            0 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1180              :                                SPDM_BIN_STR_7_LABEL,
    1181              :                                sizeof(SPDM_BIN_STR_7_LABEL) - 1, NULL,
    1182            0 :                                (uint16_t)hash_size, hash_size, bin_str7,
    1183              :                                &bin_str7_size);
    1184            0 :             libspdm_hkdf_expand(m_libspdm_use_hash_algo,
    1185              :                                 response_handshake_secret, hash_size,
    1186              :                                 bin_str7, bin_str7_size,
    1187              :                                 response_finished_key, hash_size);
    1188            0 :             libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1189              :                              libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1190            0 :             libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1191              :                              response_finished_key, hash_size, ptr);
    1192            0 :             ptr += hmac_size;
    1193              : 
    1194            0 :             libspdm_transport_test_encode_message(
    1195              :                 spdm_context, NULL, false, false, spdm_response_size,
    1196              :                 spdm_response, response_size, response);
    1197              :         }
    1198              :     }
    1199            0 :         return LIBSPDM_STATUS_SUCCESS;
    1200              : 
    1201           18 :     case 0xA:
    1202              :     {
    1203              :         static uint16_t error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
    1204              : 
    1205              :         spdm_error_response_t *spdm_response;
    1206              :         size_t spdm_response_size;
    1207              :         size_t transport_header_size;
    1208              : 
    1209           18 :         spdm_response_size = sizeof(spdm_error_response_t);
    1210           18 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1211           18 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1212              : 
    1213           18 :         if(error_code <= 0xff) {
    1214           18 :             libspdm_zero_mem (spdm_response, spdm_response_size);
    1215           18 :             spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1216           18 :             spdm_response->header.request_response_code = SPDM_ERROR;
    1217           18 :             spdm_response->header.param1 = (uint8_t) error_code;
    1218           18 :             spdm_response->header.param2 = 0;
    1219              : 
    1220           18 :             libspdm_transport_test_encode_message (spdm_context, NULL, false, false,
    1221              :                                                    spdm_response_size, spdm_response,
    1222              :                                                    response_size, response);
    1223              :         }
    1224              : 
    1225           18 :         error_code++;
    1226           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
    1227            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
    1228              :         }
    1229           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
    1230            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
    1231              :         }
    1232           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
    1233            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
    1234              :         }
    1235              :     }
    1236           18 :         return LIBSPDM_STATUS_SUCCESS;
    1237            0 :     case 0xB: {
    1238              :         spdm_key_exchange_response_t *spdm_response;
    1239              :         size_t dhe_key_size;
    1240              :         uint32_t hash_size;
    1241              :         size_t signature_size;
    1242              :         uint32_t hmac_size;
    1243              :         uint8_t *ptr;
    1244              :         void *dhe_context;
    1245              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1246              :         size_t final_key_size;
    1247              :         size_t opaque_key_exchange_rsp_size;
    1248              :         void *data;
    1249              :         size_t data_size;
    1250              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1251              :         uint8_t *cert_buffer;
    1252              :         size_t cert_buffer_size;
    1253              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1254              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1255              :         uint8_t bin_str0[128];
    1256              :         size_t bin_str0_size;
    1257              :         uint8_t bin_str2[128];
    1258              :         size_t bin_str2_size;
    1259              :         uint8_t bin_str7[128];
    1260              :         size_t bin_str7_size;
    1261              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1262              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1263              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1264              :         size_t spdm_response_size;
    1265              :         size_t transport_header_size;
    1266              : 
    1267              :         ((libspdm_context_t *)spdm_context)
    1268            0 :         ->connection_info.algorithm.base_asym_algo =
    1269              :             m_libspdm_use_asym_algo;
    1270              :         ((libspdm_context_t *)spdm_context)
    1271            0 :         ->connection_info.algorithm.base_hash_algo =
    1272              :             m_libspdm_use_hash_algo;
    1273              :         ((libspdm_context_t *)spdm_context)
    1274            0 :         ->connection_info.algorithm.dhe_named_group =
    1275              :             m_libspdm_use_dhe_algo;
    1276              :         ((libspdm_context_t *)spdm_context)
    1277            0 :         ->connection_info.algorithm.measurement_hash_algo =
    1278              :             m_libspdm_use_measurement_hash_algo;
    1279            0 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1280            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1281            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1282            0 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1283            0 :         opaque_key_exchange_rsp_size =
    1284            0 :             libspdm_get_opaque_data_version_selection_data_size(
    1285              :                 spdm_context);
    1286            0 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1287            0 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    1288            0 :                              opaque_key_exchange_rsp_size + signature_size +
    1289              :                              hmac_size;
    1290            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1291            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1292              : 
    1293            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1294            0 :         spdm_response->header.request_response_code =
    1295              :             SPDM_KEY_EXCHANGE_RSP;
    1296            0 :         spdm_response->header.param1 = 0;
    1297            0 :         spdm_response->rsp_session_id =
    1298            0 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    1299            0 :         spdm_response->mut_auth_requested = 0;
    1300            0 :         spdm_response->req_slot_id_param = 0;
    1301            0 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1302            0 :                                   spdm_response->random_data);
    1303            0 :         ptr = (void *)(spdm_response + 1);
    1304            0 :         dhe_context = libspdm_dhe_new(
    1305            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1306              :                 m_libspdm_use_dhe_algo,
    1307              :                 true);
    1308            0 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1309              :                                  &dhe_key_size);
    1310            0 :         final_key_size = sizeof(final_key);
    1311            0 :         libspdm_dhe_compute_key(
    1312              :             m_libspdm_use_dhe_algo, dhe_context,
    1313            0 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    1314              :             sizeof(spdm_key_exchange_request_t),
    1315              :             dhe_key_size, final_key, &final_key_size);
    1316            0 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1317            0 :         ptr += dhe_key_size;
    1318              :         /* libspdm_zero_mem (ptr, hash_size);
    1319              :          * ptr += hash_size;*/
    1320            0 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1321            0 :         ptr += sizeof(uint16_t);
    1322            0 :         libspdm_build_opaque_data_version_selection_data(
    1323              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1324              :                 &opaque_key_exchange_rsp_size, ptr);
    1325            0 :         ptr += opaque_key_exchange_rsp_size;
    1326            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1327              :                                                         m_libspdm_use_asym_algo, &data,
    1328              :                                                         &data_size, NULL, NULL);
    1329            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1330              :                          sizeof(m_libspdm_local_buffer)
    1331            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1332              :                             m_libspdm_local_buffer),
    1333            0 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    1334            0 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    1335            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1336              :                        m_libspdm_local_buffer_size));
    1337            0 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1338            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1339            0 :         cert_buffer = (uint8_t *)data;
    1340            0 :         cert_buffer_size = data_size;
    1341            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1342              :                          cert_buffer_hash);
    1343              :         /* transcript.message_a size is 0*/
    1344            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1345            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1346              :                                       m_libspdm_local_buffer_size);
    1347            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1348              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1349            0 :         free(data);
    1350            0 :         libspdm_responder_data_sign(
    1351              :             spdm_context,
    1352            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1353              :                 SPDM_KEY_EXCHANGE_RSP,
    1354              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1355            0 :                 false, libspdm_get_managed_buffer(&th_curr),
    1356              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    1357              :                 &signature_size);
    1358            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1359              :                          sizeof(m_libspdm_local_buffer)
    1360            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1361              :                             m_libspdm_local_buffer),
    1362              :                          ptr, signature_size);
    1363            0 :         m_libspdm_local_buffer_size += signature_size;
    1364            0 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1365            0 :         ptr += signature_size;
    1366            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1367              :                          libspdm_get_managed_buffer_size(&th_curr),
    1368              :                          th_curr_hash_data);
    1369            0 :         bin_str0_size = sizeof(bin_str0);
    1370            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1371              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    1372            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    1373              :                            &bin_str0_size);
    1374            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1375              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    1376            0 :         bin_str2_size = sizeof(bin_str2);
    1377            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1378              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1379            0 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    1380              :                            bin_str2, &bin_str2_size);
    1381            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    1382              :                             bin_str2, bin_str2_size,
    1383              :                             response_handshake_secret, hash_size);
    1384            0 :         bin_str7_size = sizeof(bin_str7);
    1385            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1386              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    1387            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    1388              :                            &bin_str7_size);
    1389            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    1390              :                             hash_size, bin_str7, bin_str7_size,
    1391              :                             response_finished_key, hash_size);
    1392            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1393              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1394            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1395              :                          response_finished_key, hash_size, ptr);
    1396            0 :         ptr += hmac_size;
    1397              : 
    1398            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1399              :                                               false, spdm_response_size,
    1400              :                                               spdm_response, response_size,
    1401              :                                               response);
    1402              :     }
    1403            0 :         return LIBSPDM_STATUS_SUCCESS;
    1404            1 :     case 0xC: {
    1405              :         spdm_key_exchange_response_t *spdm_response;
    1406              :         size_t dhe_key_size;
    1407              :         uint32_t hash_size;
    1408              :         uint32_t measurement_hash_size;
    1409              :         size_t signature_size;
    1410              :         uint32_t hmac_size;
    1411              :         uint8_t *ptr;
    1412              :         void *dhe_context;
    1413              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1414              :         size_t final_key_size;
    1415              :         size_t opaque_key_exchange_rsp_size;
    1416              :         void *data;
    1417              :         size_t data_size;
    1418              :         uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1419              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1420              :         uint8_t *cert_buffer;
    1421              :         size_t cert_buffer_size;
    1422              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1423              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1424              :         uint8_t bin_str0[128];
    1425              :         size_t bin_str0_size;
    1426              :         uint8_t bin_str2[128];
    1427              :         size_t bin_str2_size;
    1428              :         uint8_t bin_str7[128];
    1429              :         size_t bin_str7_size;
    1430              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1431              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1432              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1433              :         size_t spdm_response_size;
    1434              :         size_t transport_header_size;
    1435              : 
    1436              :         ((libspdm_context_t *)spdm_context)
    1437            1 :         ->connection_info.algorithm.base_asym_algo =
    1438              :             m_libspdm_use_asym_algo;
    1439              :         ((libspdm_context_t *)spdm_context)
    1440            1 :         ->connection_info.algorithm.base_hash_algo =
    1441              :             m_libspdm_use_hash_algo;
    1442              :         ((libspdm_context_t *)spdm_context)
    1443            1 :         ->connection_info.algorithm.dhe_named_group =
    1444              :             m_libspdm_use_dhe_algo;
    1445              :         ((libspdm_context_t *)spdm_context)
    1446            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1447              :             m_libspdm_use_measurement_hash_algo;
    1448            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1449            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1450            1 :         measurement_hash_size = libspdm_get_hash_size(
    1451              :             m_libspdm_use_hash_algo);
    1452            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1453            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1454            1 :         opaque_key_exchange_rsp_size =
    1455            1 :             libspdm_get_opaque_data_version_selection_data_size(
    1456              :                 spdm_context);
    1457            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1458            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    1459            1 :                              opaque_key_exchange_rsp_size + signature_size +
    1460              :                              hmac_size;
    1461            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1462            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1463            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    1464              : 
    1465              :         /* Incorrect version. Should be 1.1. */
    1466            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
    1467            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    1468            1 :         spdm_response->header.param1 = 0;
    1469            1 :         spdm_response->rsp_session_id =
    1470            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    1471            1 :         spdm_response->mut_auth_requested = 0;
    1472            1 :         spdm_response->req_slot_id_param = 0;
    1473            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1474            1 :                                   spdm_response->random_data);
    1475            1 :         ptr = (void *)(spdm_response + 1);
    1476            1 :         dhe_context = libspdm_dhe_new(
    1477            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1478              :                 m_libspdm_use_dhe_algo,
    1479              :                 true);
    1480            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1481              :                                  &dhe_key_size);
    1482            1 :         final_key_size = sizeof(final_key);
    1483            1 :         libspdm_dhe_compute_key(
    1484              :             m_libspdm_use_dhe_algo, dhe_context,
    1485            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    1486              :             sizeof(spdm_key_exchange_request_t),
    1487              :             dhe_key_size, final_key, &final_key_size);
    1488            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1489            1 :         ptr += dhe_key_size;
    1490              :         /*Mock measurement hash as TCB*/
    1491            1 :         libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
    1492              :                          m_libspdm_use_tcb_hash_value, measurement_hash_size);
    1493            1 :         libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
    1494              :                          measurement_hash_data, measurement_hash_size);
    1495              :         /*libspdm_zero_mem (ptr, measurement_hash_size);*/
    1496            1 :         ptr += measurement_hash_size;
    1497            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1498            1 :         ptr += sizeof(uint16_t);
    1499            1 :         libspdm_build_opaque_data_version_selection_data(
    1500              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1501              :                 &opaque_key_exchange_rsp_size, ptr);
    1502            1 :         ptr += opaque_key_exchange_rsp_size;
    1503            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1504              :                                                         m_libspdm_use_asym_algo, &data,
    1505              :                                                         &data_size, NULL, NULL);
    1506            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1507              :                          sizeof(m_libspdm_local_buffer)
    1508            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1509              :                             m_libspdm_local_buffer),
    1510            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    1511            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    1512            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1513              :                        m_libspdm_local_buffer_size));
    1514            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1515            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1516            1 :         cert_buffer =  (uint8_t *)data;
    1517            1 :         cert_buffer_size = data_size;
    1518            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1519              :                          cert_buffer_hash);
    1520              :         /* transcript.message_a size is 0*/
    1521            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1522            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1523              :                                       m_libspdm_local_buffer_size);
    1524            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1525              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1526            1 :         free(data);
    1527            1 :         libspdm_responder_data_sign(
    1528              :             spdm_context,
    1529            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1530              :                 SPDM_KEY_EXCHANGE_RSP,
    1531              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1532            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    1533              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    1534              :                 &signature_size);
    1535            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1536              :                          sizeof(m_libspdm_local_buffer)
    1537            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1538              :                             m_libspdm_local_buffer),
    1539              :                          ptr, signature_size);
    1540            1 :         m_libspdm_local_buffer_size += signature_size;
    1541            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1542            1 :         ptr += signature_size;
    1543            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1544              :                          libspdm_get_managed_buffer_size(&th_curr),
    1545              :                          th_curr_hash_data);
    1546            1 :         bin_str0_size = sizeof(bin_str0);
    1547            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1548              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    1549            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    1550              :                            &bin_str0_size);
    1551            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1552              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    1553            1 :         bin_str2_size = sizeof(bin_str2);
    1554            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1555              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1556            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    1557              :                            bin_str2, &bin_str2_size);
    1558            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    1559              :                             bin_str2, bin_str2_size,
    1560              :                             response_handshake_secret, hash_size);
    1561            1 :         bin_str7_size = sizeof(bin_str7);
    1562            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1563              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    1564            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    1565              :                            &bin_str7_size);
    1566            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    1567              :                             hash_size, bin_str7, bin_str7_size,
    1568              :                             response_finished_key, hash_size);
    1569            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1570              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1571            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1572              :                          response_finished_key, hash_size, ptr);
    1573            1 :         ptr += hmac_size;
    1574              : 
    1575            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1576              :                                               false, spdm_response_size,
    1577              :                                               spdm_response, response_size,
    1578              :                                               response);
    1579              :     }
    1580            1 :         return LIBSPDM_STATUS_SUCCESS;
    1581              : 
    1582            1 :     case 0xD: {
    1583              :         spdm_key_exchange_response_t *spdm_response;
    1584              :         size_t dhe_key_size;
    1585              :         uint32_t hash_size;
    1586              :         uint32_t measurement_hash_size;
    1587              :         size_t signature_size;
    1588              :         uint32_t hmac_size;
    1589              :         uint8_t *ptr;
    1590              :         void *dhe_context;
    1591              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1592              :         size_t final_key_size;
    1593              :         size_t opaque_key_exchange_rsp_size;
    1594              :         void *data;
    1595              :         size_t data_size;
    1596              :         uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1597              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1598              :         uint8_t *cert_buffer;
    1599              :         size_t cert_buffer_size;
    1600              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1601              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1602              :         uint8_t bin_str0[128];
    1603              :         size_t bin_str0_size;
    1604              :         uint8_t bin_str2[128];
    1605              :         size_t bin_str2_size;
    1606              :         uint8_t bin_str7[128];
    1607              :         size_t bin_str7_size;
    1608              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1609              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1610              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1611              :         size_t spdm_response_size;
    1612              :         size_t transport_header_size;
    1613              : 
    1614              :         ((libspdm_context_t *)spdm_context)
    1615            1 :         ->connection_info.algorithm.base_asym_algo =
    1616              :             m_libspdm_use_asym_algo;
    1617              :         ((libspdm_context_t *)spdm_context)
    1618            1 :         ->connection_info.algorithm.base_hash_algo =
    1619              :             m_libspdm_use_hash_algo;
    1620              :         ((libspdm_context_t *)spdm_context)
    1621            1 :         ->connection_info.algorithm.dhe_named_group =
    1622              :             m_libspdm_use_dhe_algo;
    1623              :         ((libspdm_context_t *)spdm_context)
    1624            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1625              :             m_libspdm_use_measurement_hash_algo;
    1626            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1627            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1628            1 :         measurement_hash_size = libspdm_get_hash_size(
    1629              :             m_libspdm_use_hash_algo);
    1630            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1631            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1632            1 :         opaque_key_exchange_rsp_size =
    1633            1 :             libspdm_get_opaque_data_version_selection_data_size(
    1634              :                 spdm_context);
    1635            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1636            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    1637            1 :                              opaque_key_exchange_rsp_size + signature_size +
    1638              :                              hmac_size;
    1639            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1640            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1641            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    1642              : 
    1643            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1644              :         /* Incorrect response code. */
    1645            1 :         spdm_response->header.request_response_code = SPDM_DIGESTS;
    1646            1 :         spdm_response->header.param1 = 0;
    1647            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    1648            1 :         spdm_response->mut_auth_requested = 0;
    1649            1 :         spdm_response->req_slot_id_param = 0;
    1650            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
    1651            1 :         ptr = (void *)(spdm_response + 1);
    1652            1 :         dhe_context = libspdm_dhe_new(
    1653            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1654              :                 m_libspdm_use_dhe_algo, true);
    1655            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    1656            1 :         final_key_size = sizeof(final_key);
    1657            1 :         libspdm_dhe_compute_key(
    1658              :             m_libspdm_use_dhe_algo, dhe_context,
    1659            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    1660              :             sizeof(spdm_key_exchange_request_t),
    1661              :             dhe_key_size, final_key, &final_key_size);
    1662            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1663            1 :         ptr += dhe_key_size;
    1664              :         /*Mock measurement hash as 0x00 array*/
    1665            1 :         libspdm_zero_mem(measurement_hash_data, measurement_hash_size);
    1666            1 :         libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
    1667              :                          measurement_hash_data, measurement_hash_size);
    1668              :         /*libspdm_zero_mem (ptr, measurement_hash_size);*/
    1669            1 :         ptr += measurement_hash_size;
    1670            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1671            1 :         ptr += sizeof(uint16_t);
    1672            1 :         libspdm_build_opaque_data_version_selection_data(
    1673              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1674              :                 &opaque_key_exchange_rsp_size, ptr);
    1675            1 :         ptr += opaque_key_exchange_rsp_size;
    1676            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1677              :                                                         m_libspdm_use_asym_algo, &data,
    1678              :                                                         &data_size, NULL, NULL);
    1679            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1680              :                          sizeof(m_libspdm_local_buffer)
    1681            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1682              :                             m_libspdm_local_buffer),
    1683            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    1684            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    1685            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1686              :                        m_libspdm_local_buffer_size));
    1687            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1688            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1689            1 :         cert_buffer = (uint8_t *)data;
    1690            1 :         cert_buffer_size =  data_size;
    1691            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1692              :                          cert_buffer_hash);
    1693              :         /* transcript.message_a size is 0*/
    1694            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1695            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1696              :                                       m_libspdm_local_buffer_size);
    1697            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1698              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1699            1 :         free(data);
    1700            1 :         libspdm_responder_data_sign(
    1701              :             spdm_context,
    1702            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1703              :                 SPDM_KEY_EXCHANGE_RSP,
    1704              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1705            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    1706              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    1707              :                 &signature_size);
    1708            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1709              :                          sizeof(m_libspdm_local_buffer)
    1710            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1711              :                             m_libspdm_local_buffer),
    1712              :                          ptr, signature_size);
    1713            1 :         m_libspdm_local_buffer_size += signature_size;
    1714            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1715            1 :         ptr += signature_size;
    1716            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1717              :                          libspdm_get_managed_buffer_size(&th_curr),
    1718              :                          th_curr_hash_data);
    1719            1 :         bin_str0_size = sizeof(bin_str0);
    1720            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1721              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    1722            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    1723              :                            &bin_str0_size);
    1724            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1725              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    1726            1 :         bin_str2_size = sizeof(bin_str2);
    1727            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1728              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1729            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    1730              :                            bin_str2, &bin_str2_size);
    1731            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    1732              :                             bin_str2, bin_str2_size,
    1733              :                             response_handshake_secret, hash_size);
    1734            1 :         bin_str7_size = sizeof(bin_str7);
    1735            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1736              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    1737            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    1738              :                            &bin_str7_size);
    1739            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    1740              :                             hash_size, bin_str7, bin_str7_size,
    1741              :                             response_finished_key, hash_size);
    1742            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1743              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1744            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1745              :                          response_finished_key, hash_size, ptr);
    1746            1 :         ptr += hmac_size;
    1747              : 
    1748            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1749              :                                               false, spdm_response_size,
    1750              :                                               spdm_response, response_size,
    1751              :                                               response);
    1752              :     }
    1753            1 :         return LIBSPDM_STATUS_SUCCESS;
    1754              : 
    1755            1 :     case 0xE: {
    1756              :         spdm_key_exchange_response_t *spdm_response;
    1757              :         size_t dhe_key_size;
    1758              :         uint32_t hash_size;
    1759              :         uint32_t measurement_hash_size;
    1760              :         size_t signature_size;
    1761              :         uint32_t hmac_size;
    1762              :         uint8_t *ptr;
    1763              :         void *dhe_context;
    1764              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1765              :         size_t final_key_size;
    1766              :         size_t opaque_key_exchange_rsp_size;
    1767              :         void *data;
    1768              :         size_t data_size;
    1769              :         uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1770              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1771              :         uint8_t *cert_buffer;
    1772              :         size_t cert_buffer_size;
    1773              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1774              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1775              :         uint8_t bin_str0[128];
    1776              :         size_t bin_str0_size;
    1777              :         uint8_t bin_str2[128];
    1778              :         size_t bin_str2_size;
    1779              :         uint8_t bin_str7[128];
    1780              :         size_t bin_str7_size;
    1781              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1782              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1783              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1784              :         size_t spdm_response_size;
    1785              :         size_t transport_header_size;
    1786              : 
    1787              :         ((libspdm_context_t *)spdm_context)
    1788            1 :         ->connection_info.algorithm.base_asym_algo =
    1789              :             m_libspdm_use_asym_algo;
    1790              :         ((libspdm_context_t *)spdm_context)
    1791            1 :         ->connection_info.algorithm.base_hash_algo =
    1792              :             m_libspdm_use_hash_algo;
    1793              :         ((libspdm_context_t *)spdm_context)
    1794            1 :         ->connection_info.algorithm.dhe_named_group =
    1795              :             m_libspdm_use_dhe_algo;
    1796              :         ((libspdm_context_t *)spdm_context)
    1797            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1798              :             m_libspdm_use_measurement_hash_algo;
    1799            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1800            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1801            1 :         measurement_hash_size = libspdm_get_hash_size(
    1802              :             m_libspdm_use_hash_algo);
    1803            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1804            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1805            1 :         opaque_key_exchange_rsp_size =
    1806            1 :             libspdm_get_opaque_data_version_selection_data_size(
    1807              :                 spdm_context);
    1808            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1809            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    1810            1 :                              opaque_key_exchange_rsp_size + signature_size +
    1811              :                              hmac_size;
    1812            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1813            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1814            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    1815              : 
    1816            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1817            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    1818            1 :         spdm_response->header.param1 = 0;
    1819            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    1820              :         /* Mutual authentication is requested even though it is not supported. */
    1821            1 :         spdm_response->mut_auth_requested =
    1822              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
    1823            1 :         spdm_response->req_slot_id_param = 0;
    1824            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
    1825            1 :         ptr = (void *)(spdm_response + 1);
    1826            1 :         dhe_context = libspdm_dhe_new(
    1827            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1828              :                 m_libspdm_use_dhe_algo,
    1829              :                 true);
    1830            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1831              :                                  &dhe_key_size);
    1832            1 :         final_key_size = sizeof(final_key);
    1833            1 :         libspdm_dhe_compute_key(
    1834              :             m_libspdm_use_dhe_algo, dhe_context,
    1835            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    1836              :             sizeof(spdm_key_exchange_request_t),
    1837              :             dhe_key_size, final_key, &final_key_size);
    1838            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1839            1 :         ptr += dhe_key_size;
    1840              :         /*Mock measurement hash*/
    1841            1 :         libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
    1842              :                          m_libspdm_use_tcb_hash_value, measurement_hash_size);
    1843            1 :         libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
    1844              :                          measurement_hash_data, measurement_hash_size);
    1845              :         /*libspdm_zero_mem (ptr, measurement_hash_size);*/
    1846            1 :         ptr += measurement_hash_size;
    1847            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1848            1 :         ptr += sizeof(uint16_t);
    1849            1 :         libspdm_build_opaque_data_version_selection_data(
    1850              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1851              :                 &opaque_key_exchange_rsp_size, ptr);
    1852            1 :         ptr += opaque_key_exchange_rsp_size;
    1853            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1854              :                                                         m_libspdm_use_asym_algo, &data,
    1855              :                                                         &data_size, NULL, NULL);
    1856            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1857              :                          sizeof(m_libspdm_local_buffer)
    1858            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1859              :                             m_libspdm_local_buffer),
    1860            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    1861            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    1862            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1863              :                        m_libspdm_local_buffer_size));
    1864            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1865            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1866            1 :         cert_buffer =  (uint8_t *)data;
    1867            1 :         cert_buffer_size = data_size;
    1868            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1869              :                          cert_buffer_hash);
    1870              :         /* transcript.message_a size is 0*/
    1871            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1872            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1873              :                                       m_libspdm_local_buffer_size);
    1874            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1875              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1876            1 :         free(data);
    1877            1 :         libspdm_responder_data_sign(
    1878              :             spdm_context,
    1879            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1880              :                 SPDM_KEY_EXCHANGE_RSP,
    1881              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1882            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    1883              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    1884              :                 &signature_size);
    1885            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1886              :                          sizeof(m_libspdm_local_buffer)
    1887            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1888              :                             m_libspdm_local_buffer),
    1889              :                          ptr, signature_size);
    1890            1 :         m_libspdm_local_buffer_size += signature_size;
    1891            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1892            1 :         ptr += signature_size;
    1893            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1894              :                          libspdm_get_managed_buffer_size(&th_curr),
    1895              :                          th_curr_hash_data);
    1896            1 :         bin_str0_size = sizeof(bin_str0);
    1897            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1898              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    1899            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    1900              :                            &bin_str0_size);
    1901            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1902              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    1903            1 :         bin_str2_size = sizeof(bin_str2);
    1904            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1905              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1906            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    1907              :                            bin_str2, &bin_str2_size);
    1908            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    1909              :                             bin_str2, bin_str2_size,
    1910              :                             response_handshake_secret, hash_size);
    1911            1 :         bin_str7_size = sizeof(bin_str7);
    1912            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1913              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    1914            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    1915              :                            &bin_str7_size);
    1916            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    1917              :                             hash_size, bin_str7, bin_str7_size,
    1918              :                             response_finished_key, hash_size);
    1919            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1920              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1921            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1922              :                          response_finished_key, hash_size, ptr);
    1923            1 :         ptr += hmac_size;
    1924              : 
    1925            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1926              :                                               false, spdm_response_size,
    1927              :                                               spdm_response, response_size,
    1928              :                                               response);
    1929              :     }
    1930            1 :         return LIBSPDM_STATUS_SUCCESS;
    1931              : 
    1932            1 :     case 0xF: {
    1933              :         spdm_key_exchange_response_t *spdm_response;
    1934              :         size_t dhe_key_size;
    1935              :         uint32_t hash_size;
    1936              :         size_t signature_size;
    1937              :         uint32_t hmac_size;
    1938              :         uint8_t *ptr;
    1939              :         void *dhe_context;
    1940              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1941              :         size_t final_key_size;
    1942              :         size_t opaque_key_exchange_rsp_size;
    1943              :         void *data;
    1944              :         size_t data_size;
    1945              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1946              :         uint8_t *cert_buffer;
    1947              :         size_t cert_buffer_size;
    1948              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1949              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1950              :         uint8_t bin_str0[128];
    1951              :         size_t bin_str0_size;
    1952              :         uint8_t bin_str2[128];
    1953              :         size_t bin_str2_size;
    1954              :         uint8_t bin_str7[128];
    1955              :         size_t bin_str7_size;
    1956              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1957              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1958              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1959              :         size_t spdm_response_size;
    1960              :         size_t transport_header_size;
    1961              : 
    1962              :         ((libspdm_context_t *)spdm_context)
    1963            1 :         ->connection_info.algorithm.base_asym_algo =
    1964              :             m_libspdm_use_asym_algo;
    1965              :         ((libspdm_context_t *)spdm_context)
    1966            1 :         ->connection_info.algorithm.base_hash_algo =
    1967              :             m_libspdm_use_hash_algo;
    1968              :         ((libspdm_context_t *)spdm_context)
    1969            1 :         ->connection_info.algorithm.dhe_named_group =
    1970              :             m_libspdm_use_dhe_algo;
    1971              :         ((libspdm_context_t *)spdm_context)
    1972            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1973              :             m_libspdm_use_measurement_hash_algo;
    1974            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1975            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1976            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1977            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1978            1 :         opaque_key_exchange_rsp_size =
    1979            1 :             libspdm_get_opaque_data_version_selection_data_size(
    1980              :                 spdm_context);
    1981            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1982            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    1983            1 :                              opaque_key_exchange_rsp_size + signature_size +
    1984              :                              hmac_size;
    1985            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1986            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1987            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    1988              : 
    1989            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1990            1 :         spdm_response->header.request_response_code =
    1991              :             SPDM_KEY_EXCHANGE_RSP;
    1992            1 :         spdm_response->header.param1 = 0;
    1993            1 :         spdm_response->rsp_session_id =
    1994            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    1995            1 :         spdm_response->mut_auth_requested = 0;
    1996            1 :         spdm_response->req_slot_id_param = 0;
    1997            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1998            1 :                                   spdm_response->random_data);
    1999            1 :         ptr = (void *)(spdm_response + 1);
    2000            1 :         dhe_context = libspdm_dhe_new(
    2001            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2002              :                 m_libspdm_use_dhe_algo,
    2003              :                 true);
    2004            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2005              :                                  &dhe_key_size);
    2006            1 :         final_key_size = sizeof(final_key);
    2007            1 :         libspdm_dhe_compute_key(
    2008              :             m_libspdm_use_dhe_algo, dhe_context,
    2009            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2010              :             sizeof(spdm_key_exchange_request_t),
    2011              :             dhe_key_size, final_key, &final_key_size);
    2012            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2013            1 :         ptr += dhe_key_size;
    2014              :         /* libspdm_zero_mem (ptr, hash_size);
    2015              :          * ptr += hash_size;*/
    2016            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2017            1 :         ptr += sizeof(uint16_t);
    2018            1 :         libspdm_build_opaque_data_version_selection_data(
    2019              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2020              :                 &opaque_key_exchange_rsp_size, ptr);
    2021            1 :         ptr += opaque_key_exchange_rsp_size;
    2022            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2023              :                                                         m_libspdm_use_asym_algo, &data,
    2024              :                                                         &data_size, NULL, NULL);
    2025            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2026              :                          sizeof(m_libspdm_local_buffer)
    2027            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2028              :                             m_libspdm_local_buffer),
    2029            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2030            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2031            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2032              :                        m_libspdm_local_buffer_size));
    2033            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2034            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2035            1 :         cert_buffer =  (uint8_t *)data;
    2036            1 :         cert_buffer_size = data_size;
    2037            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2038              :                          cert_buffer_hash);
    2039              :         /* transcript.message_a size is 0*/
    2040            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2041            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2042              :                                       m_libspdm_local_buffer_size);
    2043            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2044              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2045            1 :         free(data);
    2046            1 :         libspdm_responder_data_sign(
    2047              :             spdm_context,
    2048            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2049              :                 SPDM_KEY_EXCHANGE_RSP,
    2050              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2051            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2052              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2053              :                 &signature_size);
    2054            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2055              :                          sizeof(m_libspdm_local_buffer)
    2056            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2057              :                             m_libspdm_local_buffer),
    2058              :                          ptr, signature_size);
    2059            1 :         m_libspdm_local_buffer_size += signature_size;
    2060            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2061            1 :         ptr += signature_size;
    2062            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2063              :                          libspdm_get_managed_buffer_size(&th_curr),
    2064              :                          th_curr_hash_data);
    2065            1 :         bin_str0_size = sizeof(bin_str0);
    2066            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2067              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2068            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2069              :                            &bin_str0_size);
    2070            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2071              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2072            1 :         bin_str2_size = sizeof(bin_str2);
    2073            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2074              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2075            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2076              :                            bin_str2, &bin_str2_size);
    2077            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2078              :                             bin_str2, bin_str2_size,
    2079              :                             response_handshake_secret, hash_size);
    2080            1 :         bin_str7_size = sizeof(bin_str7);
    2081            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2082              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2083            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2084              :                            &bin_str7_size);
    2085            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2086              :                             hash_size, bin_str7, bin_str7_size,
    2087              :                             response_finished_key, hash_size);
    2088            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2089              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2090            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2091              :                          response_finished_key, hash_size, ptr);
    2092            1 :         ptr += hmac_size;
    2093              : 
    2094            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2095              :                                               false, spdm_response_size,
    2096              :                                               spdm_response, response_size,
    2097              :                                               response);
    2098              :     }
    2099            1 :         return LIBSPDM_STATUS_SUCCESS;
    2100              : 
    2101            1 :     case 0x10: {
    2102              :         spdm_key_exchange_response_t *spdm_response;
    2103              :         size_t dhe_key_size;
    2104              :         uint32_t hash_size;
    2105              :         size_t signature_size;
    2106              :         uint32_t hmac_size;
    2107              :         uint8_t *ptr;
    2108              :         void *dhe_context;
    2109              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2110              :         size_t final_key_size;
    2111              :         size_t opaque_key_exchange_rsp_size;
    2112              :         void *data;
    2113              :         size_t data_size;
    2114              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2115              :         uint8_t *cert_buffer;
    2116              :         size_t cert_buffer_size;
    2117              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2118              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2119              :         uint8_t bin_str0[128];
    2120              :         size_t bin_str0_size;
    2121              :         uint8_t bin_str2[128];
    2122              :         size_t bin_str2_size;
    2123              :         uint8_t bin_str7[128];
    2124              :         size_t bin_str7_size;
    2125              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2126              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2127              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2128              :         size_t spdm_response_size;
    2129              :         size_t transport_header_size;
    2130              : 
    2131              :         ((libspdm_context_t *)spdm_context)
    2132            1 :         ->connection_info.algorithm.base_asym_algo =
    2133              :             m_libspdm_use_asym_algo;
    2134              :         ((libspdm_context_t *)spdm_context)
    2135            1 :         ->connection_info.algorithm.base_hash_algo =
    2136              :             m_libspdm_use_hash_algo;
    2137              :         ((libspdm_context_t *)spdm_context)
    2138            1 :         ->connection_info.algorithm.dhe_named_group =
    2139              :             m_libspdm_use_dhe_algo;
    2140              :         ((libspdm_context_t *)spdm_context)
    2141            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2142              :             m_libspdm_use_measurement_hash_algo;
    2143            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2144            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2145            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2146            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2147            1 :         opaque_key_exchange_rsp_size =
    2148            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2149              :                 spdm_context);
    2150            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2151            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    2152            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2153              :                              hmac_size;
    2154            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2155            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2156            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2157              : 
    2158            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2159            1 :         spdm_response->header.request_response_code =
    2160              :             SPDM_KEY_EXCHANGE_RSP;
    2161            1 :         spdm_response->header.param1 = 0;
    2162            1 :         spdm_response->rsp_session_id =
    2163            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2164            1 :         spdm_response->mut_auth_requested = 0;
    2165            1 :         spdm_response->req_slot_id_param = 0;
    2166            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2167            1 :                                   spdm_response->random_data);
    2168            1 :         ptr = (void *)(spdm_response + 1);
    2169            1 :         dhe_context = libspdm_dhe_new(
    2170            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2171              :                 m_libspdm_use_dhe_algo,
    2172              :                 true);
    2173            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2174              :                                  &dhe_key_size);
    2175            1 :         final_key_size = sizeof(final_key);
    2176            1 :         libspdm_dhe_compute_key(
    2177              :             m_libspdm_use_dhe_algo, dhe_context,
    2178            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2179              :             sizeof(spdm_key_exchange_request_t),
    2180              :             dhe_key_size, final_key, &final_key_size);
    2181            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2182            1 :         ptr += dhe_key_size;
    2183              :         /* libspdm_zero_mem (ptr, hash_size);
    2184              :          * ptr += hash_size;*/
    2185            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2186            1 :         ptr += sizeof(uint16_t);
    2187            1 :         libspdm_build_opaque_data_version_selection_data(
    2188              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2189              :                 &opaque_key_exchange_rsp_size, ptr);
    2190            1 :         ptr += opaque_key_exchange_rsp_size;
    2191            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2192              :                                                         m_libspdm_use_asym_algo, &data,
    2193              :                                                         &data_size, NULL, NULL);
    2194            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2195              :                          sizeof(m_libspdm_local_buffer)
    2196            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2197              :                             m_libspdm_local_buffer),
    2198            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2199            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2200            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2201              :                        m_libspdm_local_buffer_size));
    2202            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2203            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2204            1 :         cert_buffer =  (uint8_t *)data;
    2205            1 :         cert_buffer_size = data_size;
    2206            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2207              :                          cert_buffer_hash);
    2208              :         /* transcript.message_a size is 0*/
    2209            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2210            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2211              :                                       m_libspdm_local_buffer_size);
    2212            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2213              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2214            1 :         free(data);
    2215            1 :         libspdm_responder_data_sign(
    2216              :             spdm_context,
    2217            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2218              :                 SPDM_KEY_EXCHANGE_RSP,
    2219              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2220            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2221              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2222              :                 &signature_size);
    2223            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2224              :                          sizeof(m_libspdm_local_buffer)
    2225            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2226              :                             m_libspdm_local_buffer),
    2227              :                          ptr, signature_size);
    2228            1 :         m_libspdm_local_buffer_size += signature_size;
    2229            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2230            1 :         ptr += signature_size;
    2231            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2232              :                          libspdm_get_managed_buffer_size(&th_curr),
    2233              :                          th_curr_hash_data);
    2234            1 :         bin_str0_size = sizeof(bin_str0);
    2235            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2236              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2237            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2238              :                            &bin_str0_size);
    2239            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2240              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2241            1 :         bin_str2_size = sizeof(bin_str2);
    2242            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2243              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2244            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2245              :                            bin_str2, &bin_str2_size);
    2246            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2247              :                             bin_str2, bin_str2_size,
    2248              :                             response_handshake_secret, hash_size);
    2249            1 :         bin_str7_size = sizeof(bin_str7);
    2250            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2251              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2252            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2253              :                            &bin_str7_size);
    2254            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2255              :                             hash_size, bin_str7, bin_str7_size,
    2256              :                             response_finished_key, hash_size);
    2257            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2258              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2259            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2260              :                          response_finished_key, hash_size, ptr);
    2261            1 :         ptr += hmac_size;
    2262              : 
    2263            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2264              :                                               false, spdm_response_size,
    2265              :                                               spdm_response, response_size,
    2266              :                                               response);
    2267              :     }
    2268            1 :         return LIBSPDM_STATUS_SUCCESS;
    2269              : 
    2270            1 :     case 0x11: {
    2271              :         spdm_key_exchange_response_t *spdm_response;
    2272              :         size_t dhe_key_size;
    2273              :         uint32_t hash_size;
    2274              :         uint32_t measurement_hash_size;
    2275              :         size_t signature_size;
    2276              :         uint32_t hmac_size;
    2277              :         uint8_t *ptr;
    2278              :         void *dhe_context;
    2279              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2280              :         size_t final_key_size;
    2281              :         size_t opaque_key_exchange_rsp_size;
    2282              :         void *data;
    2283              :         size_t data_size;
    2284              :         uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2285              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2286              :         uint8_t *cert_buffer;
    2287              :         size_t cert_buffer_size;
    2288              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2289              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2290              :         uint8_t bin_str0[128];
    2291              :         size_t bin_str0_size;
    2292              :         uint8_t bin_str2[128];
    2293              :         size_t bin_str2_size;
    2294              :         uint8_t bin_str7[128];
    2295              :         size_t bin_str7_size;
    2296              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2297              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2298              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2299              :         size_t spdm_response_size;
    2300              :         size_t transport_header_size;
    2301              : 
    2302              :         ((libspdm_context_t *)spdm_context)
    2303            1 :         ->connection_info.algorithm.base_asym_algo =
    2304              :             m_libspdm_use_asym_algo;
    2305              :         ((libspdm_context_t *)spdm_context)
    2306            1 :         ->connection_info.algorithm.base_hash_algo =
    2307              :             m_libspdm_use_hash_algo;
    2308              :         ((libspdm_context_t *)spdm_context)
    2309            1 :         ->connection_info.algorithm.dhe_named_group =
    2310              :             m_libspdm_use_dhe_algo;
    2311              :         ((libspdm_context_t *)spdm_context)
    2312            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2313              :             m_libspdm_use_measurement_hash_algo;
    2314            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2315            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2316            1 :         measurement_hash_size = libspdm_get_hash_size(
    2317              :             m_libspdm_use_hash_algo);
    2318            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2319            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2320            1 :         opaque_key_exchange_rsp_size =
    2321            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2322              :                 spdm_context);
    2323            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2324            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    2325            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2326              :                              hmac_size;
    2327            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2328            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2329            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2330              : 
    2331            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2332            1 :         spdm_response->header.request_response_code =
    2333              :             SPDM_KEY_EXCHANGE_RSP;
    2334            1 :         spdm_response->header.param1 = 0;
    2335            1 :         spdm_response->rsp_session_id =
    2336            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2337            1 :         spdm_response->mut_auth_requested = 0;
    2338            1 :         spdm_response->req_slot_id_param = 0;
    2339            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2340            1 :                                   spdm_response->random_data);
    2341            1 :         ptr = (void *)(spdm_response + 1);
    2342            1 :         dhe_context = libspdm_dhe_new(
    2343            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2344              :                 m_libspdm_use_dhe_algo,
    2345              :                 true);
    2346            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2347              :                                  &dhe_key_size);
    2348            1 :         final_key_size = sizeof(final_key);
    2349            1 :         libspdm_dhe_compute_key(
    2350              :             m_libspdm_use_dhe_algo, dhe_context,
    2351            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2352              :             sizeof(spdm_key_exchange_request_t),
    2353              :             dhe_key_size, final_key, &final_key_size);
    2354            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2355            1 :         ptr += dhe_key_size;
    2356              :         /*Mock measurement hash as TCB*/
    2357            1 :         libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
    2358              :                          m_libspdm_use_tcb_hash_value, measurement_hash_size);
    2359            1 :         libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
    2360              :                          measurement_hash_data, measurement_hash_size);
    2361              :         /*libspdm_zero_mem (ptr, measurement_hash_size);*/
    2362            1 :         ptr += measurement_hash_size;
    2363            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2364            1 :         ptr += sizeof(uint16_t);
    2365            1 :         libspdm_build_opaque_data_version_selection_data(
    2366              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2367              :                 &opaque_key_exchange_rsp_size, ptr);
    2368            1 :         ptr += opaque_key_exchange_rsp_size;
    2369            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2370              :                                                         m_libspdm_use_asym_algo, &data,
    2371              :                                                         &data_size, NULL, NULL);
    2372            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2373              :                          sizeof(m_libspdm_local_buffer)
    2374            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2375              :                             m_libspdm_local_buffer),
    2376            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2377            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2378            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2379              :                        m_libspdm_local_buffer_size));
    2380            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2381            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2382            1 :         cert_buffer =  (uint8_t *)data;
    2383            1 :         cert_buffer_size = data_size;
    2384            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2385              :                          cert_buffer_hash);
    2386              :         /* transcript.message_a size is 0*/
    2387            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2388            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2389              :                                       m_libspdm_local_buffer_size);
    2390            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2391              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2392            1 :         free(data);
    2393            1 :         libspdm_responder_data_sign(
    2394              :             spdm_context,
    2395            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2396              :                 SPDM_KEY_EXCHANGE_RSP,
    2397              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2398            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2399              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2400              :                 &signature_size);
    2401            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2402              :                          sizeof(m_libspdm_local_buffer)
    2403            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2404              :                             m_libspdm_local_buffer),
    2405              :                          ptr, signature_size);
    2406            1 :         m_libspdm_local_buffer_size += signature_size;
    2407            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2408            1 :         ptr += signature_size;
    2409            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2410              :                          libspdm_get_managed_buffer_size(&th_curr),
    2411              :                          th_curr_hash_data);
    2412            1 :         bin_str0_size = sizeof(bin_str0);
    2413            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2414              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2415            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2416              :                            &bin_str0_size);
    2417            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2418              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2419            1 :         bin_str2_size = sizeof(bin_str2);
    2420            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2421              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2422            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2423              :                            bin_str2, &bin_str2_size);
    2424            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2425              :                             bin_str2, bin_str2_size,
    2426              :                             response_handshake_secret, hash_size);
    2427            1 :         bin_str7_size = sizeof(bin_str7);
    2428            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2429              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2430            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2431              :                            &bin_str7_size);
    2432            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2433              :                             hash_size, bin_str7, bin_str7_size,
    2434              :                             response_finished_key, hash_size);
    2435            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2436              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2437            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2438              :                          response_finished_key, hash_size, ptr);
    2439            1 :         ptr += hmac_size;
    2440              : 
    2441            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2442              :                                               false, spdm_response_size,
    2443              :                                               spdm_response, response_size,
    2444              :                                               response);
    2445              :     }
    2446            1 :         return LIBSPDM_STATUS_SUCCESS;
    2447              : 
    2448            1 :     case 0x12: {
    2449              :         spdm_key_exchange_response_t *spdm_response;
    2450              :         size_t dhe_key_size;
    2451              :         uint32_t hash_size;
    2452              :         uint32_t measurement_hash_size;
    2453              :         size_t signature_size;
    2454              :         uint32_t hmac_size;
    2455              :         uint8_t *ptr;
    2456              :         void *dhe_context;
    2457              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2458              :         size_t final_key_size;
    2459              :         size_t opaque_key_exchange_rsp_size;
    2460              :         void *data;
    2461              :         size_t data_size;
    2462              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2463              :         uint8_t *cert_buffer;
    2464              :         size_t cert_buffer_size;
    2465              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2466              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2467              :         uint8_t bin_str0[128];
    2468              :         size_t bin_str0_size;
    2469              :         uint8_t bin_str2[128];
    2470              :         size_t bin_str2_size;
    2471              :         uint8_t bin_str7[128];
    2472              :         size_t bin_str7_size;
    2473              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2474              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2475              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2476              :         size_t spdm_response_size;
    2477              :         size_t transport_header_size;
    2478              : 
    2479              :         ((libspdm_context_t *)spdm_context)
    2480            1 :         ->connection_info.algorithm.base_asym_algo =
    2481              :             m_libspdm_use_asym_algo;
    2482              :         ((libspdm_context_t *)spdm_context)
    2483            1 :         ->connection_info.algorithm.base_hash_algo =
    2484              :             m_libspdm_use_hash_algo;
    2485              :         ((libspdm_context_t *)spdm_context)
    2486            1 :         ->connection_info.algorithm.dhe_named_group =
    2487              :             m_libspdm_use_dhe_algo;
    2488              :         ((libspdm_context_t *)spdm_context)
    2489            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2490              :             m_libspdm_use_measurement_hash_algo;
    2491            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2492            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2493            1 :         measurement_hash_size = 0;
    2494            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2495            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2496            1 :         opaque_key_exchange_rsp_size =
    2497            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2498              :                 spdm_context);
    2499            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2500            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    2501            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2502              :                              hmac_size;
    2503            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2504            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2505            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2506              : 
    2507            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2508            1 :         spdm_response->header.request_response_code =
    2509              :             SPDM_KEY_EXCHANGE_RSP;
    2510            1 :         spdm_response->header.param1 = 0;
    2511            1 :         spdm_response->rsp_session_id =
    2512            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2513            1 :         spdm_response->mut_auth_requested = 0;
    2514            1 :         spdm_response->req_slot_id_param = 0;
    2515            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2516            1 :                                   spdm_response->random_data);
    2517            1 :         ptr = (void *)(spdm_response + 1);
    2518            1 :         dhe_context = libspdm_dhe_new(
    2519            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2520              :                 m_libspdm_use_dhe_algo,
    2521              :                 true);
    2522            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2523              :                                  &dhe_key_size);
    2524            1 :         final_key_size = sizeof(final_key);
    2525            1 :         libspdm_dhe_compute_key(
    2526              :             m_libspdm_use_dhe_algo, dhe_context,
    2527            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2528              :             sizeof(spdm_key_exchange_request_t),
    2529              :             dhe_key_size, final_key, &final_key_size);
    2530            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2531            1 :         ptr += dhe_key_size;
    2532              : 
    2533            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2534            1 :         ptr += sizeof(uint16_t);
    2535            1 :         libspdm_build_opaque_data_version_selection_data(
    2536              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2537              :                 &opaque_key_exchange_rsp_size, ptr);
    2538            1 :         ptr += opaque_key_exchange_rsp_size;
    2539            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2540              :                                                         m_libspdm_use_asym_algo, &data,
    2541              :                                                         &data_size, NULL, NULL);
    2542            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2543              :                          sizeof(m_libspdm_local_buffer)
    2544            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2545              :                             m_libspdm_local_buffer),
    2546            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2547            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2548            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2549              :                        m_libspdm_local_buffer_size));
    2550            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2551            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2552            1 :         cert_buffer =  (uint8_t *)data;
    2553            1 :         cert_buffer_size = data_size;
    2554            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2555              :                          cert_buffer_hash);
    2556              :         /* transcript.message_a size is 0*/
    2557            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2558            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2559              :                                       m_libspdm_local_buffer_size);
    2560            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2561              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2562            1 :         free(data);
    2563            1 :         libspdm_responder_data_sign(
    2564              :             spdm_context,
    2565            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2566              :                 SPDM_KEY_EXCHANGE_RSP,
    2567              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2568            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2569              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2570              :                 &signature_size);
    2571            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2572              :                          sizeof(m_libspdm_local_buffer)
    2573            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2574              :                             m_libspdm_local_buffer),
    2575              :                          ptr, signature_size);
    2576            1 :         m_libspdm_local_buffer_size += signature_size;
    2577            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2578            1 :         ptr += signature_size-1;
    2579            1 :         *ptr ^= 0xFF;
    2580            1 :         ptr++;
    2581            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2582              :                          libspdm_get_managed_buffer_size(&th_curr),
    2583              :                          th_curr_hash_data);
    2584            1 :         bin_str0_size = sizeof(bin_str0);
    2585            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2586              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2587            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2588              :                            &bin_str0_size);
    2589            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2590              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2591            1 :         bin_str2_size = sizeof(bin_str2);
    2592            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2593              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2594            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2595              :                            bin_str2, &bin_str2_size);
    2596            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2597              :                             bin_str2, bin_str2_size,
    2598              :                             response_handshake_secret, hash_size);
    2599            1 :         bin_str7_size = sizeof(bin_str7);
    2600            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2601              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2602            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2603              :                            &bin_str7_size);
    2604            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2605              :                             hash_size, bin_str7, bin_str7_size,
    2606              :                             response_finished_key, hash_size);
    2607            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2608              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2609            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2610              :                          response_finished_key, hash_size, ptr);
    2611            1 :         ptr += hmac_size;
    2612              : 
    2613            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2614              :                                               false, spdm_response_size,
    2615              :                                               spdm_response, response_size,
    2616              :                                               response);
    2617              :     }
    2618            1 :         return LIBSPDM_STATUS_SUCCESS;
    2619              : 
    2620            0 :     case 0x13: {
    2621              :     }
    2622            0 :         return LIBSPDM_STATUS_SUCCESS;
    2623              : 
    2624            1 :     case 0x14: {
    2625              :         spdm_key_exchange_response_t *spdm_response;
    2626              :         size_t dhe_key_size;
    2627              :         uint32_t hash_size;
    2628              :         size_t signature_size;
    2629              :         uint32_t hmac_size;
    2630              :         uint8_t *ptr;
    2631              :         void *dhe_context;
    2632              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2633              :         size_t final_key_size;
    2634              :         size_t opaque_key_exchange_rsp_size;
    2635              :         void *data;
    2636              :         size_t data_size;
    2637              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2638              :         uint8_t *cert_buffer;
    2639              :         size_t cert_buffer_size;
    2640              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2641              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2642              :         uint8_t bin_str0[128];
    2643              :         size_t bin_str0_size;
    2644              :         uint8_t bin_str2[128];
    2645              :         size_t bin_str2_size;
    2646              :         uint8_t bin_str7[128];
    2647              :         size_t bin_str7_size;
    2648              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2649              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2650              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2651              :         size_t spdm_response_size;
    2652              :         size_t transport_header_size;
    2653              : 
    2654              :         ((libspdm_context_t *)spdm_context)
    2655            1 :         ->connection_info.algorithm.base_asym_algo =
    2656              :             m_libspdm_use_asym_algo;
    2657              :         ((libspdm_context_t *)spdm_context)
    2658            1 :         ->connection_info.algorithm.base_hash_algo =
    2659              :             m_libspdm_use_hash_algo;
    2660              :         ((libspdm_context_t *)spdm_context)
    2661            1 :         ->connection_info.algorithm.dhe_named_group =
    2662              :             m_libspdm_use_dhe_algo;
    2663              :         ((libspdm_context_t *)spdm_context)
    2664            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2665              :             m_libspdm_use_measurement_hash_algo;
    2666            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2667            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2668            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2669            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2670            1 :         opaque_key_exchange_rsp_size =
    2671            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2672              :                 spdm_context);
    2673            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2674            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    2675            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2676              :                              hmac_size;
    2677            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2678            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2679            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2680              : 
    2681            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2682            1 :         spdm_response->header.request_response_code =
    2683              :             SPDM_KEY_EXCHANGE_RSP;
    2684            1 :         spdm_response->header.param1 = 5;
    2685            1 :         spdm_response->rsp_session_id =
    2686            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2687            1 :         spdm_response->mut_auth_requested = 0;
    2688            1 :         spdm_response->req_slot_id_param = 0;
    2689            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2690            1 :                                   spdm_response->random_data);
    2691            1 :         ptr = (void *)(spdm_response + 1);
    2692            1 :         dhe_context = libspdm_dhe_new(
    2693            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2694              :                 m_libspdm_use_dhe_algo,
    2695              :                 true);
    2696            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2697              :                                  &dhe_key_size);
    2698            1 :         final_key_size = sizeof(final_key);
    2699            1 :         libspdm_dhe_compute_key(
    2700              :             m_libspdm_use_dhe_algo, dhe_context,
    2701            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2702              :             sizeof(spdm_key_exchange_request_t),
    2703              :             dhe_key_size, final_key, &final_key_size);
    2704            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2705            1 :         ptr += dhe_key_size;
    2706              :         /* libspdm_zero_mem (ptr, hash_size);
    2707              :          * ptr += hash_size;*/
    2708            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2709            1 :         ptr += sizeof(uint16_t);
    2710            1 :         libspdm_build_opaque_data_version_selection_data(
    2711              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2712              :                 &opaque_key_exchange_rsp_size, ptr);
    2713            1 :         ptr += opaque_key_exchange_rsp_size;
    2714            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2715              :                                                         m_libspdm_use_asym_algo, &data,
    2716              :                                                         &data_size, NULL, NULL);
    2717            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2718              :                          sizeof(m_libspdm_local_buffer)
    2719            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2720              :                             m_libspdm_local_buffer),
    2721            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2722            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2723            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2724              :                        m_libspdm_local_buffer_size));
    2725            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2726            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2727            1 :         cert_buffer =  (uint8_t *)data;
    2728            1 :         cert_buffer_size = data_size;
    2729            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2730              :                          cert_buffer_hash);
    2731              :         /* transcript.message_a size is 0*/
    2732            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2733            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2734              :                                       m_libspdm_local_buffer_size);
    2735            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2736              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2737            1 :         free(data);
    2738            1 :         libspdm_responder_data_sign(
    2739              :             spdm_context,
    2740            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2741              :                 SPDM_KEY_EXCHANGE_RSP,
    2742              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2743            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2744              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2745              :                 &signature_size);
    2746            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2747              :                          sizeof(m_libspdm_local_buffer)
    2748            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2749              :                             m_libspdm_local_buffer),
    2750              :                          ptr, signature_size);
    2751            1 :         m_libspdm_local_buffer_size += signature_size;
    2752            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2753            1 :         ptr += signature_size;
    2754            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2755              :                          libspdm_get_managed_buffer_size(&th_curr),
    2756              :                          th_curr_hash_data);
    2757            1 :         bin_str0_size = sizeof(bin_str0);
    2758            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2759              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2760            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2761              :                            &bin_str0_size);
    2762            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2763              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2764            1 :         bin_str2_size = sizeof(bin_str2);
    2765            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2766              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2767            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2768              :                            bin_str2, &bin_str2_size);
    2769            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2770              :                             bin_str2, bin_str2_size,
    2771              :                             response_handshake_secret, hash_size);
    2772            1 :         bin_str7_size = sizeof(bin_str7);
    2773            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2774              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2775            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2776              :                            &bin_str7_size);
    2777            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2778              :                             hash_size, bin_str7, bin_str7_size,
    2779              :                             response_finished_key, hash_size);
    2780            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2781              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2782            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2783              :                          response_finished_key, hash_size, ptr);
    2784            1 :         ptr += hmac_size;
    2785              : 
    2786            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2787              :                                               false, spdm_response_size,
    2788              :                                               spdm_response, response_size,
    2789              :                                               response);
    2790              :     }
    2791            1 :         return LIBSPDM_STATUS_SUCCESS;
    2792              : 
    2793            1 :     case 0x15: {
    2794              :         spdm_key_exchange_response_t *spdm_response;
    2795              :         size_t dhe_key_size;
    2796              :         uint32_t hash_size;
    2797              :         size_t signature_size;
    2798              :         uint32_t hmac_size;
    2799              :         uint8_t *ptr;
    2800              :         void *dhe_context;
    2801              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2802              :         size_t final_key_size;
    2803              :         size_t opaque_key_exchange_rsp_size;
    2804              :         void *data;
    2805              :         size_t data_size;
    2806              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2807              :         uint8_t *cert_buffer;
    2808              :         size_t cert_buffer_size;
    2809              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2810              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2811              :         uint8_t bin_str0[128];
    2812              :         size_t bin_str0_size;
    2813              :         uint8_t bin_str2[128];
    2814              :         size_t bin_str2_size;
    2815              :         uint8_t bin_str7[128];
    2816              :         size_t bin_str7_size;
    2817              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2818              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2819              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2820              :         size_t spdm_response_size;
    2821              :         size_t transport_header_size;
    2822              : 
    2823              :         ((libspdm_context_t *)spdm_context)
    2824            1 :         ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    2825              :         ((libspdm_context_t *)spdm_context)
    2826            1 :         ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    2827              :         ((libspdm_context_t *)spdm_context)
    2828            1 :         ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    2829              :         ((libspdm_context_t *)spdm_context)
    2830            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2831              :             m_libspdm_use_measurement_hash_algo;
    2832            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2833            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2834            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2835            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2836            1 :         opaque_key_exchange_rsp_size =
    2837            1 :             libspdm_get_opaque_data_version_selection_data_size(spdm_context);
    2838            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2839            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    2840            1 :                              opaque_key_exchange_rsp_size + signature_size + hmac_size;
    2841            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2842            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2843            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2844              : 
    2845            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2846            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    2847            1 :         spdm_response->header.param1 = 0;
    2848            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    2849            1 :         spdm_response->mut_auth_requested = 0;
    2850            1 :         spdm_response->req_slot_id_param = 0;
    2851            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
    2852            1 :         ptr = (void *)(spdm_response + 1);
    2853            1 :         dhe_context = libspdm_dhe_new(
    2854            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2855              :                 m_libspdm_use_dhe_algo, true);
    2856            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    2857            1 :         final_key_size = sizeof(final_key);
    2858            1 :         libspdm_dhe_compute_key(
    2859              :             m_libspdm_use_dhe_algo, dhe_context,
    2860            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2861              :             sizeof(spdm_key_exchange_request_t),
    2862              :             dhe_key_size, final_key, &final_key_size);
    2863            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2864            1 :         ptr += dhe_key_size;
    2865              : 
    2866            1 :         *(uint16_t *)ptr = SPDM_MAX_OPAQUE_DATA_SIZE + 1;
    2867            1 :         ptr += sizeof(uint16_t);
    2868            1 :         libspdm_build_opaque_data_version_selection_data(
    2869              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2870              :                 &opaque_key_exchange_rsp_size, ptr);
    2871            1 :         ptr += opaque_key_exchange_rsp_size;
    2872            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2873              :                                                         m_libspdm_use_asym_algo, &data,
    2874              :                                                         &data_size, NULL, NULL);
    2875            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2876              :                          sizeof(m_libspdm_local_buffer)
    2877            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2878              :                             m_libspdm_local_buffer),
    2879            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2880            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2881            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2882              :                        m_libspdm_local_buffer_size));
    2883            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2884            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2885            1 :         cert_buffer = (uint8_t *)data;
    2886            1 :         cert_buffer_size =  data_size;
    2887            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2888              :                          cert_buffer_hash);
    2889              :         /* transcript.message_a size is 0*/
    2890            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2891            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2892              :                                       m_libspdm_local_buffer_size);
    2893            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2894              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2895            1 :         free(data);
    2896            1 :         libspdm_responder_data_sign(
    2897              :             spdm_context,
    2898            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2899              :                 SPDM_KEY_EXCHANGE_RSP,
    2900              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2901            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2902              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2903              :                 &signature_size);
    2904            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2905              :                          sizeof(m_libspdm_local_buffer)
    2906            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2907              :                             m_libspdm_local_buffer),
    2908              :                          ptr, signature_size);
    2909            1 :         m_libspdm_local_buffer_size += signature_size;
    2910            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2911            1 :         ptr += signature_size;
    2912            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2913              :                          libspdm_get_managed_buffer_size(&th_curr),
    2914              :                          th_curr_hash_data);
    2915            1 :         bin_str0_size = sizeof(bin_str0);
    2916            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2917              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2918            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2919              :                            &bin_str0_size);
    2920            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2921              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2922            1 :         bin_str2_size = sizeof(bin_str2);
    2923            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2924              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2925            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2926              :                            bin_str2, &bin_str2_size);
    2927            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2928              :                             bin_str2, bin_str2_size,
    2929              :                             response_handshake_secret, hash_size);
    2930            1 :         bin_str7_size = sizeof(bin_str7);
    2931            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2932              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2933            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2934              :                            &bin_str7_size);
    2935            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2936              :                             hash_size, bin_str7, bin_str7_size,
    2937              :                             response_finished_key, hash_size);
    2938            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2939              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2940            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2941              :                          response_finished_key, hash_size, ptr);
    2942            1 :         ptr += hmac_size;
    2943              : 
    2944            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2945              :                                               false, spdm_response_size,
    2946              :                                               spdm_response, response_size,
    2947              :                                               response);
    2948              :     }
    2949            1 :         return LIBSPDM_STATUS_SUCCESS;
    2950              : 
    2951            1 :     case 0x16: {
    2952              :         spdm_key_exchange_response_t *spdm_response;
    2953              :         size_t dhe_key_size;
    2954              :         uint32_t hash_size;
    2955              :         size_t signature_size;
    2956              :         uint32_t hmac_size;
    2957              :         uint8_t *ptr;
    2958              :         void *dhe_context;
    2959              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2960              :         size_t final_key_size;
    2961              :         size_t opaque_key_exchange_rsp_size;
    2962              :         void *data;
    2963              :         size_t data_size;
    2964              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2965              :         uint8_t *cert_buffer;
    2966              :         size_t cert_buffer_size;
    2967              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2968              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2969              :         uint8_t bin_str0[128];
    2970              :         size_t bin_str0_size;
    2971              :         uint8_t bin_str2[128];
    2972              :         size_t bin_str2_size;
    2973              :         uint8_t bin_str7[128];
    2974              :         size_t bin_str7_size;
    2975              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2976              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2977              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2978              :         size_t spdm_response_size;
    2979              :         size_t transport_header_size;
    2980              : 
    2981              :         ((libspdm_context_t *)spdm_context)
    2982            1 :         ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    2983              :         ((libspdm_context_t *)spdm_context)
    2984            1 :         ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    2985              :         ((libspdm_context_t *)spdm_context)
    2986            1 :         ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    2987              :         ((libspdm_context_t *)spdm_context)
    2988            1 :         ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
    2989            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2990            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2991            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2992            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2993            1 :         opaque_key_exchange_rsp_size =
    2994            1 :             libspdm_get_opaque_data_version_selection_data_size(spdm_context);
    2995            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2996            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    2997            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2998              :                              hmac_size;
    2999            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3000            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3001            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3002              : 
    3003            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3004            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    3005            1 :         spdm_response->header.param1 = 0;
    3006            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    3007              :         /* Requester's PUB_KEY_ID_CAP is set and Responder requests the encapsulated flow. */
    3008            1 :         spdm_response->mut_auth_requested =
    3009              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
    3010            1 :         spdm_response->req_slot_id_param = 0;
    3011            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
    3012            1 :         ptr = (void *)(spdm_response + 1);
    3013            1 :         dhe_context = libspdm_dhe_new(
    3014            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3015              :                 m_libspdm_use_dhe_algo,
    3016              :                 true);
    3017            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    3018            1 :         final_key_size = sizeof(final_key);
    3019            1 :         libspdm_dhe_compute_key(
    3020              :             m_libspdm_use_dhe_algo, dhe_context,
    3021            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3022              :             sizeof(spdm_key_exchange_request_t),
    3023              :             dhe_key_size, final_key, &final_key_size);
    3024            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3025            1 :         ptr += dhe_key_size;
    3026              :         /* libspdm_zero_mem (ptr, hash_size);
    3027              :          * ptr += hash_size;*/
    3028            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3029            1 :         ptr += sizeof(uint16_t);
    3030            1 :         libspdm_build_opaque_data_version_selection_data(
    3031              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3032              :                 &opaque_key_exchange_rsp_size, ptr);
    3033            1 :         ptr += opaque_key_exchange_rsp_size;
    3034            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3035              :                                                         m_libspdm_use_asym_algo, &data,
    3036              :                                                         &data_size, NULL, NULL);
    3037            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3038              :                          sizeof(m_libspdm_local_buffer)
    3039            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3040              :                             m_libspdm_local_buffer),
    3041            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3042            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3043            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3044              :                        m_libspdm_local_buffer_size));
    3045            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3046            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3047            1 :         cert_buffer =  (uint8_t *)data;
    3048            1 :         cert_buffer_size = data_size;
    3049            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3050              :                          cert_buffer_hash);
    3051              :         /* transcript.message_a size is 0*/
    3052            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3053            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3054              :                                       m_libspdm_local_buffer_size);
    3055            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3056              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3057            1 :         free(data);
    3058            1 :         libspdm_responder_data_sign(
    3059              :             spdm_context,
    3060            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3061              :                 SPDM_KEY_EXCHANGE_RSP,
    3062              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3063            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3064              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3065              :                 &signature_size);
    3066            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3067              :                          sizeof(m_libspdm_local_buffer)
    3068            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3069              :                             m_libspdm_local_buffer),
    3070              :                          ptr, signature_size);
    3071            1 :         m_libspdm_local_buffer_size += signature_size;
    3072            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3073            1 :         ptr += signature_size;
    3074            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3075              :                          libspdm_get_managed_buffer_size(&th_curr),
    3076              :                          th_curr_hash_data);
    3077            1 :         bin_str0_size = sizeof(bin_str0);
    3078            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3079              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3080            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3081              :                            &bin_str0_size);
    3082            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3083              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3084            1 :         bin_str2_size = sizeof(bin_str2);
    3085            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3086              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3087            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3088              :                            bin_str2, &bin_str2_size);
    3089            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3090              :                             bin_str2, bin_str2_size,
    3091              :                             response_handshake_secret, hash_size);
    3092            1 :         bin_str7_size = sizeof(bin_str7);
    3093            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3094              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3095            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3096              :                            &bin_str7_size);
    3097            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3098              :                             hash_size, bin_str7, bin_str7_size,
    3099              :                             response_finished_key, hash_size);
    3100            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3101              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3102            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3103              :                          response_finished_key, hash_size, ptr);
    3104            1 :         ptr += hmac_size;
    3105              : 
    3106            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3107              :                                               false, spdm_response_size,
    3108              :                                               spdm_response, response_size,
    3109              :                                               response);
    3110              :     }
    3111            1 :         return LIBSPDM_STATUS_SUCCESS;
    3112              : 
    3113            1 :     case 0x17: {
    3114              :         spdm_key_exchange_response_t *spdm_response;
    3115              :         size_t dhe_key_size;
    3116              :         uint32_t hash_size;
    3117              :         size_t signature_size;
    3118              :         uint32_t hmac_size;
    3119              :         uint8_t *ptr;
    3120              :         void *dhe_context;
    3121              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3122              :         size_t final_key_size;
    3123              :         size_t opaque_key_exchange_rsp_size;
    3124              :         void *data;
    3125              :         size_t data_size;
    3126              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3127              :         uint8_t *cert_buffer;
    3128              :         size_t cert_buffer_size;
    3129              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3130              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3131              :         uint8_t bin_str0[128];
    3132              :         size_t bin_str0_size;
    3133              :         uint8_t bin_str2[128];
    3134              :         size_t bin_str2_size;
    3135              :         uint8_t bin_str7[128];
    3136              :         size_t bin_str7_size;
    3137              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3138              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3139              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3140              :         size_t spdm_response_size;
    3141              :         size_t transport_header_size;
    3142              : 
    3143              :         ((libspdm_context_t *)spdm_context)
    3144            1 :         ->connection_info.algorithm.base_asym_algo =
    3145              :             m_libspdm_use_asym_algo;
    3146              :         ((libspdm_context_t *)spdm_context)
    3147            1 :         ->connection_info.algorithm.base_hash_algo =
    3148              :             m_libspdm_use_hash_algo;
    3149              :         ((libspdm_context_t *)spdm_context)
    3150            1 :         ->connection_info.algorithm.dhe_named_group =
    3151              :             m_libspdm_use_dhe_algo;
    3152              :         ((libspdm_context_t *)spdm_context)
    3153            1 :         ->connection_info.algorithm.measurement_hash_algo =
    3154              :             m_libspdm_use_measurement_hash_algo;
    3155            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3156            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3157            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3158            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3159            1 :         opaque_key_exchange_rsp_size =
    3160            1 :             libspdm_get_opaque_data_version_selection_data_size(
    3161              :                 spdm_context);
    3162            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3163            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3164            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3165              :                              hmac_size;
    3166            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3167            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3168            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3169              : 
    3170            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3171            1 :         spdm_response->header.request_response_code =
    3172              :             SPDM_KEY_EXCHANGE_RSP;
    3173            1 :         spdm_response->header.param1 = 0;
    3174            1 :         spdm_response->rsp_session_id =
    3175            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3176            1 :         spdm_response->mut_auth_requested =
    3177              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
    3178            1 :         spdm_response->req_slot_id_param = 0x0;
    3179            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3180            1 :                                   spdm_response->random_data);
    3181            1 :         ptr = (void *)(spdm_response + 1);
    3182            1 :         dhe_context = libspdm_dhe_new(
    3183            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3184              :                 m_libspdm_use_dhe_algo,
    3185              :                 true);
    3186            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3187              :                                  &dhe_key_size);
    3188            1 :         final_key_size = sizeof(final_key);
    3189            1 :         libspdm_dhe_compute_key(
    3190              :             m_libspdm_use_dhe_algo, dhe_context,
    3191            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3192              :             sizeof(spdm_key_exchange_request_t),
    3193              :             dhe_key_size, final_key, &final_key_size);
    3194            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3195            1 :         ptr += dhe_key_size;
    3196              :         /* libspdm_zero_mem (ptr, hash_size);
    3197              :          * ptr += hash_size;*/
    3198            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3199            1 :         ptr += sizeof(uint16_t);
    3200            1 :         libspdm_build_opaque_data_version_selection_data(
    3201              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3202              :                 &opaque_key_exchange_rsp_size, ptr);
    3203            1 :         ptr += opaque_key_exchange_rsp_size;
    3204            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3205              :                                                         m_libspdm_use_asym_algo, &data,
    3206              :                                                         &data_size, NULL, NULL);
    3207            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3208              :                          sizeof(m_libspdm_local_buffer)
    3209            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3210              :                             m_libspdm_local_buffer),
    3211            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3212            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3213            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3214              :                        m_libspdm_local_buffer_size));
    3215            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3216            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3217            1 :         cert_buffer =  (uint8_t *)data;
    3218            1 :         cert_buffer_size = data_size;
    3219            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3220              :                          cert_buffer_hash);
    3221              :         /* transcript.message_a size is 0*/
    3222            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3223            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3224              :                                       m_libspdm_local_buffer_size);
    3225            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3226              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3227            1 :         free(data);
    3228            1 :         libspdm_responder_data_sign(
    3229              :             spdm_context,
    3230            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3231              :                 SPDM_KEY_EXCHANGE_RSP,
    3232              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3233            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3234              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3235              :                 &signature_size);
    3236            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3237              :                          sizeof(m_libspdm_local_buffer)
    3238            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3239              :                             m_libspdm_local_buffer),
    3240              :                          ptr, signature_size);
    3241            1 :         m_libspdm_local_buffer_size += signature_size;
    3242            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3243            1 :         ptr += signature_size;
    3244            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3245              :                          libspdm_get_managed_buffer_size(&th_curr),
    3246              :                          th_curr_hash_data);
    3247            1 :         bin_str0_size = sizeof(bin_str0);
    3248            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3249              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3250            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3251              :                            &bin_str0_size);
    3252            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3253              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3254            1 :         bin_str2_size = sizeof(bin_str2);
    3255            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3256              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3257            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3258              :                            bin_str2, &bin_str2_size);
    3259            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3260              :                             bin_str2, bin_str2_size,
    3261              :                             response_handshake_secret, hash_size);
    3262            1 :         bin_str7_size = sizeof(bin_str7);
    3263            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3264              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3265            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3266              :                            &bin_str7_size);
    3267            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3268              :                             hash_size, bin_str7, bin_str7_size,
    3269              :                             response_finished_key, hash_size);
    3270            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3271              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3272            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3273              :                          response_finished_key, hash_size, ptr);
    3274            1 :         ptr += hmac_size;
    3275              : 
    3276            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3277              :                                               false, spdm_response_size,
    3278              :                                               spdm_response, response_size,
    3279              :                                               response);
    3280              :     }
    3281            1 :         return LIBSPDM_STATUS_SUCCESS;
    3282              : 
    3283            0 :     case 0x18: {
    3284              :         spdm_key_exchange_response_t *spdm_response;
    3285              :         size_t dhe_key_size;
    3286              :         uint32_t hash_size;
    3287              :         size_t signature_size;
    3288              :         uint32_t hmac_size;
    3289              :         uint8_t *ptr;
    3290              :         void *dhe_context;
    3291              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3292              :         size_t final_key_size;
    3293              :         size_t opaque_key_exchange_rsp_size;
    3294              :         void *data;
    3295              :         size_t data_size;
    3296              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3297              :         uint8_t *cert_buffer;
    3298              :         size_t cert_buffer_size;
    3299              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3300              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3301              :         uint8_t bin_str0[128];
    3302              :         size_t bin_str0_size;
    3303              :         uint8_t bin_str2[128];
    3304              :         size_t bin_str2_size;
    3305              :         uint8_t bin_str7[128];
    3306              :         size_t bin_str7_size;
    3307              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3308              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3309              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3310              :         size_t spdm_response_size;
    3311              :         size_t transport_header_size;
    3312              : 
    3313              :         ((libspdm_context_t *)spdm_context)
    3314            0 :         ->connection_info.algorithm.base_asym_algo =
    3315              :             m_libspdm_use_asym_algo;
    3316              :         ((libspdm_context_t *)spdm_context)
    3317            0 :         ->connection_info.algorithm.base_hash_algo =
    3318              :             m_libspdm_use_hash_algo;
    3319              :         ((libspdm_context_t *)spdm_context)
    3320            0 :         ->connection_info.algorithm.dhe_named_group =
    3321              :             m_libspdm_use_dhe_algo;
    3322              :         ((libspdm_context_t *)spdm_context)
    3323            0 :         ->connection_info.algorithm.measurement_hash_algo =
    3324              :             m_libspdm_use_measurement_hash_algo;
    3325            0 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3326            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3327            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3328            0 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3329            0 :         opaque_key_exchange_rsp_size =
    3330            0 :             libspdm_get_opaque_data_version_selection_data_size(
    3331              :                 spdm_context);
    3332            0 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3333            0 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3334            0 :                              opaque_key_exchange_rsp_size + signature_size +
    3335              :                              hmac_size;
    3336            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3337            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3338            0 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3339              : 
    3340            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3341            0 :         spdm_response->header.request_response_code =
    3342              :             SPDM_KEY_EXCHANGE_RSP;
    3343            0 :         spdm_response->header.param1 = 0;
    3344            0 :         spdm_response->rsp_session_id =
    3345            0 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3346            0 :         spdm_response->mut_auth_requested =
    3347              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
    3348            0 :         spdm_response->req_slot_id_param = 0xF;
    3349            0 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3350            0 :                                   spdm_response->random_data);
    3351            0 :         ptr = (void *)(spdm_response + 1);
    3352            0 :         dhe_context = libspdm_dhe_new(
    3353            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3354              :                 m_libspdm_use_dhe_algo,
    3355              :                 true);
    3356            0 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3357              :                                  &dhe_key_size);
    3358            0 :         final_key_size = sizeof(final_key);
    3359            0 :         libspdm_dhe_compute_key(
    3360              :             m_libspdm_use_dhe_algo, dhe_context,
    3361            0 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3362              :             sizeof(spdm_key_exchange_request_t),
    3363              :             dhe_key_size, final_key, &final_key_size);
    3364            0 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3365            0 :         ptr += dhe_key_size;
    3366              :         /* libspdm_zero_mem (ptr, hash_size);
    3367              :          * ptr += hash_size;*/
    3368            0 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3369            0 :         ptr += sizeof(uint16_t);
    3370            0 :         libspdm_build_opaque_data_version_selection_data(
    3371              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3372              :                 &opaque_key_exchange_rsp_size, ptr);
    3373            0 :         ptr += opaque_key_exchange_rsp_size;
    3374            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3375              :                                                         m_libspdm_use_asym_algo, &data,
    3376              :                                                         &data_size, NULL, NULL);
    3377            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3378              :                          sizeof(m_libspdm_local_buffer)
    3379            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3380              :                             m_libspdm_local_buffer),
    3381            0 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3382            0 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3383            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3384              :                        m_libspdm_local_buffer_size));
    3385            0 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3386            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3387            0 :         cert_buffer =  (uint8_t *)data;
    3388            0 :         cert_buffer_size = data_size;
    3389            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3390              :                          cert_buffer_hash);
    3391              :         /* transcript.message_a size is 0*/
    3392            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3393            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3394              :                                       m_libspdm_local_buffer_size);
    3395            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3396              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3397            0 :         free(data);
    3398            0 :         libspdm_responder_data_sign(
    3399              :             spdm_context,
    3400            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3401              :                 SPDM_KEY_EXCHANGE_RSP,
    3402              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3403            0 :                 false, libspdm_get_managed_buffer(&th_curr),
    3404              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3405              :                 &signature_size);
    3406            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3407              :                          sizeof(m_libspdm_local_buffer)
    3408            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3409              :                             m_libspdm_local_buffer),
    3410              :                          ptr, signature_size);
    3411            0 :         m_libspdm_local_buffer_size += signature_size;
    3412            0 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3413            0 :         ptr += signature_size;
    3414            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3415              :                          libspdm_get_managed_buffer_size(&th_curr),
    3416              :                          th_curr_hash_data);
    3417            0 :         bin_str0_size = sizeof(bin_str0);
    3418            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3419              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3420            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3421              :                            &bin_str0_size);
    3422            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3423              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3424            0 :         bin_str2_size = sizeof(bin_str2);
    3425            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3426              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3427            0 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3428              :                            bin_str2, &bin_str2_size);
    3429            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3430              :                             bin_str2, bin_str2_size,
    3431              :                             response_handshake_secret, hash_size);
    3432            0 :         bin_str7_size = sizeof(bin_str7);
    3433            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3434              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3435            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3436              :                            &bin_str7_size);
    3437            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3438              :                             hash_size, bin_str7, bin_str7_size,
    3439              :                             response_finished_key, hash_size);
    3440            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3441              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3442            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3443              :                          response_finished_key, hash_size, ptr);
    3444            0 :         ptr += hmac_size;
    3445              : 
    3446            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3447              :                                               false, spdm_response_size,
    3448              :                                               spdm_response, response_size,
    3449              :                                               response);
    3450              :     }
    3451            0 :         return LIBSPDM_STATUS_SUCCESS;
    3452              : 
    3453            0 :     case 0x19: {
    3454              :         spdm_key_exchange_response_t *spdm_response;
    3455              :         size_t dhe_key_size;
    3456              :         uint32_t hash_size;
    3457              :         size_t signature_size;
    3458              :         uint32_t hmac_size;
    3459              :         uint8_t *ptr;
    3460              :         void *dhe_context;
    3461              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3462              :         size_t final_key_size;
    3463              :         size_t opaque_key_exchange_rsp_size;
    3464              :         void *data;
    3465              :         size_t data_size;
    3466              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3467              :         uint8_t *cert_buffer;
    3468              :         size_t cert_buffer_size;
    3469              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3470              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3471              :         uint8_t bin_str0[128];
    3472              :         size_t bin_str0_size;
    3473              :         uint8_t bin_str2[128];
    3474              :         size_t bin_str2_size;
    3475              :         uint8_t bin_str7[128];
    3476              :         size_t bin_str7_size;
    3477              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3478              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3479              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3480              :         size_t spdm_response_size;
    3481              :         size_t transport_header_size;
    3482              : 
    3483              :         ((libspdm_context_t *)spdm_context)
    3484            0 :         ->connection_info.algorithm.base_asym_algo =
    3485              :             m_libspdm_use_asym_algo;
    3486              :         ((libspdm_context_t *)spdm_context)
    3487            0 :         ->connection_info.algorithm.base_hash_algo =
    3488              :             m_libspdm_use_hash_algo;
    3489              :         ((libspdm_context_t *)spdm_context)
    3490            0 :         ->connection_info.algorithm.dhe_named_group =
    3491              :             m_libspdm_use_dhe_algo;
    3492              :         ((libspdm_context_t *)spdm_context)
    3493            0 :         ->connection_info.algorithm.measurement_hash_algo =
    3494              :             m_libspdm_use_measurement_hash_algo;
    3495            0 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3496            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3497            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3498            0 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3499            0 :         opaque_key_exchange_rsp_size =
    3500            0 :             libspdm_get_opaque_data_version_selection_data_size(
    3501              :                 spdm_context);
    3502            0 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3503            0 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3504            0 :                              opaque_key_exchange_rsp_size + signature_size +
    3505              :                              hmac_size;
    3506            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3507            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3508            0 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3509              : 
    3510            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3511            0 :         spdm_response->header.request_response_code =
    3512              :             SPDM_KEY_EXCHANGE_RSP;
    3513            0 :         spdm_response->header.param1 = 0;
    3514            0 :         spdm_response->rsp_session_id =
    3515            0 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3516            0 :         spdm_response->mut_auth_requested =
    3517              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
    3518            0 :         spdm_response->req_slot_id_param = 0xF;
    3519            0 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3520            0 :                                   spdm_response->random_data);
    3521            0 :         ptr = (void *)(spdm_response + 1);
    3522            0 :         dhe_context = libspdm_dhe_new(
    3523            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3524              :                 m_libspdm_use_dhe_algo,
    3525              :                 true);
    3526            0 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3527              :                                  &dhe_key_size);
    3528            0 :         final_key_size = sizeof(final_key);
    3529            0 :         libspdm_dhe_compute_key(
    3530              :             m_libspdm_use_dhe_algo, dhe_context,
    3531            0 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3532              :             sizeof(spdm_key_exchange_request_t),
    3533              :             dhe_key_size, final_key, &final_key_size);
    3534            0 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3535            0 :         ptr += dhe_key_size;
    3536              :         /* libspdm_zero_mem (ptr, hash_size);
    3537              :          * ptr += hash_size;*/
    3538            0 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3539            0 :         ptr += sizeof(uint16_t);
    3540            0 :         libspdm_build_opaque_data_version_selection_data(
    3541              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3542              :                 &opaque_key_exchange_rsp_size, ptr);
    3543            0 :         ptr += opaque_key_exchange_rsp_size;
    3544            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3545              :                                                         m_libspdm_use_asym_algo, &data,
    3546              :                                                         &data_size, NULL, NULL);
    3547            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3548              :                          sizeof(m_libspdm_local_buffer)
    3549            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3550              :                             m_libspdm_local_buffer),
    3551            0 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3552            0 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3553            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3554              :                        m_libspdm_local_buffer_size));
    3555            0 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3556            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3557            0 :         cert_buffer =  (uint8_t *)data;
    3558            0 :         cert_buffer_size = data_size;
    3559            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3560              :                          cert_buffer_hash);
    3561              :         /* transcript.message_a size is 0*/
    3562            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3563            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3564              :                                       m_libspdm_local_buffer_size);
    3565            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3566              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3567            0 :         free(data);
    3568            0 :         libspdm_responder_data_sign(
    3569              :             spdm_context,
    3570            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3571              :                 SPDM_KEY_EXCHANGE_RSP,
    3572              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3573            0 :                 false, libspdm_get_managed_buffer(&th_curr),
    3574              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3575              :                 &signature_size);
    3576            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3577              :                          sizeof(m_libspdm_local_buffer)
    3578            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3579              :                             m_libspdm_local_buffer),
    3580              :                          ptr, signature_size);
    3581            0 :         m_libspdm_local_buffer_size += signature_size;
    3582            0 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3583            0 :         ptr += signature_size;
    3584            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3585              :                          libspdm_get_managed_buffer_size(&th_curr),
    3586              :                          th_curr_hash_data);
    3587            0 :         bin_str0_size = sizeof(bin_str0);
    3588            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3589              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3590            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3591              :                            &bin_str0_size);
    3592            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3593              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3594            0 :         bin_str2_size = sizeof(bin_str2);
    3595            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3596              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3597            0 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3598              :                            bin_str2, &bin_str2_size);
    3599            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3600              :                             bin_str2, bin_str2_size,
    3601              :                             response_handshake_secret, hash_size);
    3602            0 :         bin_str7_size = sizeof(bin_str7);
    3603            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3604              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3605            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3606              :                            &bin_str7_size);
    3607            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3608              :                             hash_size, bin_str7, bin_str7_size,
    3609              :                             response_finished_key, hash_size);
    3610            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3611              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3612            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3613              :                          response_finished_key, hash_size, ptr);
    3614            0 :         ptr += hmac_size;
    3615              : 
    3616            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3617              :                                               false, spdm_response_size,
    3618              :                                               spdm_response, response_size,
    3619              :                                               response);
    3620              :     }
    3621            0 :         return LIBSPDM_STATUS_SUCCESS;
    3622              : 
    3623            1 :     case 0x1A: {
    3624              :         spdm_key_exchange_response_t *spdm_response;
    3625              :         size_t dhe_key_size;
    3626              :         uint32_t hash_size;
    3627              :         size_t signature_size;
    3628              :         uint32_t hmac_size;
    3629              :         uint8_t *ptr;
    3630              :         void *dhe_context;
    3631              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3632              :         size_t final_key_size;
    3633              :         size_t opaque_key_exchange_rsp_size;
    3634              :         void *data;
    3635              :         size_t data_size;
    3636              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3637              :         uint8_t *cert_buffer;
    3638              :         size_t cert_buffer_size;
    3639              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3640              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3641              :         uint8_t bin_str0[128];
    3642              :         size_t bin_str0_size;
    3643              :         uint8_t bin_str2[128];
    3644              :         size_t bin_str2_size;
    3645              :         uint8_t bin_str7[128];
    3646              :         size_t bin_str7_size;
    3647              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3648              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3649              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3650              :         size_t spdm_response_size;
    3651              :         size_t transport_header_size;
    3652              : 
    3653              :         ((libspdm_context_t *)spdm_context)
    3654            1 :         ->connection_info.algorithm.base_asym_algo =
    3655              :             m_libspdm_use_asym_algo;
    3656              :         ((libspdm_context_t *)spdm_context)
    3657            1 :         ->connection_info.algorithm.base_hash_algo =
    3658              :             m_libspdm_use_hash_algo;
    3659              :         ((libspdm_context_t *)spdm_context)
    3660            1 :         ->connection_info.algorithm.dhe_named_group =
    3661              :             m_libspdm_use_dhe_algo;
    3662              :         ((libspdm_context_t *)spdm_context)
    3663            1 :         ->connection_info.algorithm.measurement_hash_algo =
    3664              :             m_libspdm_use_measurement_hash_algo;
    3665            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3666            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3667            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3668            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3669            1 :         opaque_key_exchange_rsp_size =
    3670            1 :             libspdm_get_opaque_data_version_selection_data_size(
    3671              :                 spdm_context);
    3672            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3673            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3674            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3675              :                              hmac_size;
    3676            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3677            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3678            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3679              : 
    3680            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3681            1 :         spdm_response->header.request_response_code =
    3682              :             SPDM_KEY_EXCHANGE_RSP;
    3683            1 :         spdm_response->header.param1 = 0;
    3684            1 :         spdm_response->rsp_session_id =
    3685            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3686              :         /* Illegal combination. At most one bit can be set. */
    3687            1 :         spdm_response->mut_auth_requested =
    3688              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED |
    3689              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
    3690            1 :         spdm_response->req_slot_id_param = 0xF;
    3691            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3692            1 :                                   spdm_response->random_data);
    3693            1 :         ptr = (void *)(spdm_response + 1);
    3694            1 :         dhe_context = libspdm_dhe_new(
    3695            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3696              :                 m_libspdm_use_dhe_algo,
    3697              :                 true);
    3698            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3699              :                                  &dhe_key_size);
    3700            1 :         final_key_size = sizeof(final_key);
    3701            1 :         libspdm_dhe_compute_key(
    3702              :             m_libspdm_use_dhe_algo, dhe_context,
    3703            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3704              :             sizeof(spdm_key_exchange_request_t),
    3705              :             dhe_key_size, final_key, &final_key_size);
    3706            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3707            1 :         ptr += dhe_key_size;
    3708              :         /* libspdm_zero_mem (ptr, hash_size);
    3709              :          * ptr += hash_size;*/
    3710            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3711            1 :         ptr += sizeof(uint16_t);
    3712            1 :         libspdm_build_opaque_data_version_selection_data(
    3713              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3714              :                 &opaque_key_exchange_rsp_size, ptr);
    3715            1 :         ptr += opaque_key_exchange_rsp_size;
    3716            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3717              :                                                         m_libspdm_use_asym_algo, &data,
    3718              :                                                         &data_size, NULL, NULL);
    3719            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3720              :                          sizeof(m_libspdm_local_buffer)
    3721            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3722              :                             m_libspdm_local_buffer),
    3723            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3724            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3725            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3726              :                        m_libspdm_local_buffer_size));
    3727            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3728            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3729            1 :         cert_buffer =  (uint8_t *)data;
    3730            1 :         cert_buffer_size = data_size;
    3731            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3732              :                          cert_buffer_hash);
    3733              :         /* transcript.message_a size is 0*/
    3734            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3735            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3736              :                                       m_libspdm_local_buffer_size);
    3737            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3738              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3739            1 :         free(data);
    3740            1 :         libspdm_responder_data_sign(
    3741              :             spdm_context,
    3742            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3743              :                 SPDM_KEY_EXCHANGE_RSP,
    3744              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3745            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3746              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3747              :                 &signature_size);
    3748            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3749              :                          sizeof(m_libspdm_local_buffer)
    3750            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3751              :                             m_libspdm_local_buffer),
    3752              :                          ptr, signature_size);
    3753            1 :         m_libspdm_local_buffer_size += signature_size;
    3754            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3755            1 :         ptr += signature_size;
    3756            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3757              :                          libspdm_get_managed_buffer_size(&th_curr),
    3758              :                          th_curr_hash_data);
    3759            1 :         bin_str0_size = sizeof(bin_str0);
    3760            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3761              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3762            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3763              :                            &bin_str0_size);
    3764            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3765              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3766            1 :         bin_str2_size = sizeof(bin_str2);
    3767            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3768              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3769            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3770              :                            bin_str2, &bin_str2_size);
    3771            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3772              :                             bin_str2, bin_str2_size,
    3773              :                             response_handshake_secret, hash_size);
    3774            1 :         bin_str7_size = sizeof(bin_str7);
    3775            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3776              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3777            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3778              :                            &bin_str7_size);
    3779            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3780              :                             hash_size, bin_str7, bin_str7_size,
    3781              :                             response_finished_key, hash_size);
    3782            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3783              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3784            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3785              :                          response_finished_key, hash_size, ptr);
    3786            1 :         ptr += hmac_size;
    3787              : 
    3788            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3789              :                                               false, spdm_response_size,
    3790              :                                               spdm_response, response_size,
    3791              :                                               response);
    3792              :     }
    3793            1 :         return LIBSPDM_STATUS_SUCCESS;
    3794              : 
    3795            1 :     case 0x1B: {
    3796              :         spdm_key_exchange_response_t *spdm_response;
    3797              :         size_t dhe_key_size;
    3798              :         uint32_t hash_size;
    3799              :         size_t signature_size;
    3800              :         uint32_t hmac_size;
    3801              :         uint8_t *ptr;
    3802              :         void *dhe_context;
    3803              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3804              :         size_t final_key_size;
    3805              :         size_t opaque_key_exchange_rsp_size;
    3806              :         void *data;
    3807              :         size_t data_size;
    3808              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3809              :         uint8_t *cert_buffer;
    3810              :         size_t cert_buffer_size;
    3811              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3812              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3813              :         uint8_t bin_str0[128];
    3814              :         size_t bin_str0_size;
    3815              :         uint8_t bin_str2[128];
    3816              :         size_t bin_str2_size;
    3817              :         uint8_t bin_str7[128];
    3818              :         size_t bin_str7_size;
    3819              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3820              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3821              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3822              :         size_t spdm_response_size;
    3823              :         size_t transport_header_size;
    3824              : 
    3825              :         ((libspdm_context_t *)spdm_context)
    3826            1 :         ->connection_info.algorithm.base_asym_algo =
    3827              :             m_libspdm_use_asym_algo;
    3828              :         ((libspdm_context_t *)spdm_context)
    3829            1 :         ->connection_info.algorithm.base_hash_algo =
    3830              :             m_libspdm_use_hash_algo;
    3831              :         ((libspdm_context_t *)spdm_context)
    3832            1 :         ->connection_info.algorithm.dhe_named_group =
    3833              :             m_libspdm_use_dhe_algo;
    3834              :         ((libspdm_context_t *)spdm_context)
    3835            1 :         ->connection_info.algorithm.measurement_hash_algo =
    3836              :             m_libspdm_use_measurement_hash_algo;
    3837            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3838            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3839            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3840            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3841            1 :         opaque_key_exchange_rsp_size =
    3842            1 :             libspdm_get_opaque_data_version_selection_data_size(
    3843              :                 spdm_context);
    3844            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3845            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3846            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3847              :                              hmac_size;
    3848            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3849            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3850            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3851              : 
    3852            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3853            1 :         spdm_response->header.request_response_code =
    3854              :             SPDM_KEY_EXCHANGE_RSP;
    3855            1 :         spdm_response->header.param1 = 0;
    3856            1 :         spdm_response->rsp_session_id =
    3857            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3858              :         /* Illegal combination. At most one bit can be set. */
    3859            1 :         spdm_response->mut_auth_requested =
    3860              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED |
    3861              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
    3862            1 :         spdm_response->req_slot_id_param = 0xF;
    3863            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3864            1 :                                   spdm_response->random_data);
    3865            1 :         ptr = (void *)(spdm_response + 1);
    3866            1 :         dhe_context = libspdm_dhe_new(
    3867            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3868              :                 m_libspdm_use_dhe_algo,
    3869              :                 true);
    3870            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3871              :                                  &dhe_key_size);
    3872            1 :         final_key_size = sizeof(final_key);
    3873            1 :         libspdm_dhe_compute_key(
    3874              :             m_libspdm_use_dhe_algo, dhe_context,
    3875            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3876              :             sizeof(spdm_key_exchange_request_t),
    3877              :             dhe_key_size, final_key, &final_key_size);
    3878            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3879            1 :         ptr += dhe_key_size;
    3880              :         /* libspdm_zero_mem (ptr, hash_size);
    3881              :          * ptr += hash_size;*/
    3882            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3883            1 :         ptr += sizeof(uint16_t);
    3884            1 :         libspdm_build_opaque_data_version_selection_data(
    3885              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3886              :                 &opaque_key_exchange_rsp_size, ptr);
    3887            1 :         ptr += opaque_key_exchange_rsp_size;
    3888            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3889              :                                                         m_libspdm_use_asym_algo, &data,
    3890              :                                                         &data_size, NULL, NULL);
    3891            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3892              :                          sizeof(m_libspdm_local_buffer)
    3893            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3894              :                             m_libspdm_local_buffer),
    3895            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3896            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3897            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3898              :                        m_libspdm_local_buffer_size));
    3899            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3900            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3901            1 :         cert_buffer =  (uint8_t *)data;
    3902            1 :         cert_buffer_size = data_size;
    3903            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3904              :                          cert_buffer_hash);
    3905              :         /* transcript.message_a size is 0*/
    3906            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3907            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3908              :                                       m_libspdm_local_buffer_size);
    3909            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3910              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3911            1 :         free(data);
    3912            1 :         libspdm_responder_data_sign(
    3913              :             spdm_context,
    3914            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3915              :                 SPDM_KEY_EXCHANGE_RSP,
    3916              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3917            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3918              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3919              :                 &signature_size);
    3920            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3921              :                          sizeof(m_libspdm_local_buffer)
    3922            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3923              :                             m_libspdm_local_buffer),
    3924              :                          ptr, signature_size);
    3925            1 :         m_libspdm_local_buffer_size += signature_size;
    3926            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3927            1 :         ptr += signature_size;
    3928            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3929              :                          libspdm_get_managed_buffer_size(&th_curr),
    3930              :                          th_curr_hash_data);
    3931            1 :         bin_str0_size = sizeof(bin_str0);
    3932            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3933              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3934            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3935              :                            &bin_str0_size);
    3936            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3937              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3938            1 :         bin_str2_size = sizeof(bin_str2);
    3939            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3940              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3941            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3942              :                            bin_str2, &bin_str2_size);
    3943            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3944              :                             bin_str2, bin_str2_size,
    3945              :                             response_handshake_secret, hash_size);
    3946            1 :         bin_str7_size = sizeof(bin_str7);
    3947            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3948              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3949            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3950              :                            &bin_str7_size);
    3951            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3952              :                             hash_size, bin_str7, bin_str7_size,
    3953              :                             response_finished_key, hash_size);
    3954            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3955              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3956            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3957              :                          response_finished_key, hash_size, ptr);
    3958            1 :         ptr += hmac_size;
    3959              : 
    3960            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3961              :                                               false, spdm_response_size,
    3962              :                                               spdm_response, response_size,
    3963              :                                               response);
    3964              :     }
    3965            1 :         return LIBSPDM_STATUS_SUCCESS;
    3966              : 
    3967            1 :     case 0x1C: {
    3968              :         spdm_key_exchange_response_t *spdm_response;
    3969              :         size_t dhe_key_size;
    3970              :         uint32_t hash_size;
    3971              :         size_t signature_size;
    3972              :         uint32_t hmac_size;
    3973              :         uint8_t *ptr;
    3974              :         void *dhe_context;
    3975              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3976              :         size_t final_key_size;
    3977              :         size_t opaque_key_exchange_rsp_size;
    3978              :         void *data;
    3979              :         size_t data_size;
    3980              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3981              :         uint8_t *cert_buffer;
    3982              :         size_t cert_buffer_size;
    3983              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3984              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3985              :         uint8_t bin_str0[128];
    3986              :         size_t bin_str0_size;
    3987              :         uint8_t bin_str2[128];
    3988              :         size_t bin_str2_size;
    3989              :         uint8_t bin_str7[128];
    3990              :         size_t bin_str7_size;
    3991              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3992              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3993              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3994              :         size_t spdm_response_size;
    3995              :         size_t transport_header_size;
    3996              : 
    3997              :         ((libspdm_context_t *)spdm_context)
    3998            1 :         ->connection_info.algorithm.base_asym_algo =
    3999              :             m_libspdm_use_asym_algo;
    4000              :         ((libspdm_context_t *)spdm_context)
    4001            1 :         ->connection_info.algorithm.base_hash_algo =
    4002              :             m_libspdm_use_hash_algo;
    4003              :         ((libspdm_context_t *)spdm_context)
    4004            1 :         ->connection_info.algorithm.dhe_named_group =
    4005              :             m_libspdm_use_dhe_algo;
    4006              :         ((libspdm_context_t *)spdm_context)
    4007            1 :         ->connection_info.algorithm.measurement_hash_algo =
    4008              :             m_libspdm_use_measurement_hash_algo;
    4009            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4010            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4011            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4012            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4013            1 :         opaque_key_exchange_rsp_size =
    4014            1 :             libspdm_get_opaque_data_version_selection_data_size(
    4015              :                 spdm_context);
    4016            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4017            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4018            1 :                              opaque_key_exchange_rsp_size + signature_size +
    4019              :                              hmac_size;
    4020            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4021            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4022            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    4023              : 
    4024            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    4025            1 :         spdm_response->header.request_response_code =
    4026              :             SPDM_KEY_EXCHANGE_RSP;
    4027            1 :         spdm_response->header.param1 = 0;
    4028            1 :         spdm_response->rsp_session_id =
    4029            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    4030              :         /* Illegal combination. At most one bit can be set. */
    4031            1 :         spdm_response->mut_auth_requested =
    4032              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST |
    4033              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
    4034            1 :         spdm_response->req_slot_id_param = 0xF;
    4035            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    4036            1 :                                   spdm_response->random_data);
    4037            1 :         ptr = (void *)(spdm_response + 1);
    4038            1 :         dhe_context = libspdm_dhe_new(
    4039            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4040              :                 m_libspdm_use_dhe_algo,
    4041              :                 true);
    4042            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    4043              :                                  &dhe_key_size);
    4044            1 :         final_key_size = sizeof(final_key);
    4045            1 :         libspdm_dhe_compute_key(
    4046              :             m_libspdm_use_dhe_algo, dhe_context,
    4047            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    4048              :             sizeof(spdm_key_exchange_request_t),
    4049              :             dhe_key_size, final_key, &final_key_size);
    4050            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4051            1 :         ptr += dhe_key_size;
    4052              :         /* libspdm_zero_mem (ptr, hash_size);
    4053              :          * ptr += hash_size;*/
    4054            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4055            1 :         ptr += sizeof(uint16_t);
    4056            1 :         libspdm_build_opaque_data_version_selection_data(
    4057              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4058              :                 &opaque_key_exchange_rsp_size, ptr);
    4059            1 :         ptr += opaque_key_exchange_rsp_size;
    4060            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4061              :                                                         m_libspdm_use_asym_algo, &data,
    4062              :                                                         &data_size, NULL, NULL);
    4063            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4064              :                          sizeof(m_libspdm_local_buffer)
    4065            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4066              :                             m_libspdm_local_buffer),
    4067            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4068            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4069            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    4070              :                        m_libspdm_local_buffer_size));
    4071            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4072            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4073            1 :         cert_buffer =  (uint8_t *)data;
    4074            1 :         cert_buffer_size = data_size;
    4075            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4076              :                          cert_buffer_hash);
    4077              :         /* transcript.message_a size is 0*/
    4078            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4079            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4080              :                                       m_libspdm_local_buffer_size);
    4081            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4082              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4083            1 :         free(data);
    4084            1 :         libspdm_responder_data_sign(
    4085              :             spdm_context,
    4086            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4087              :                 SPDM_KEY_EXCHANGE_RSP,
    4088              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4089            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    4090              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    4091              :                 &signature_size);
    4092            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4093              :                          sizeof(m_libspdm_local_buffer)
    4094            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4095              :                             m_libspdm_local_buffer),
    4096              :                          ptr, signature_size);
    4097            1 :         m_libspdm_local_buffer_size += signature_size;
    4098            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4099            1 :         ptr += signature_size;
    4100            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4101              :                          libspdm_get_managed_buffer_size(&th_curr),
    4102              :                          th_curr_hash_data);
    4103            1 :         bin_str0_size = sizeof(bin_str0);
    4104            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4105              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4106            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    4107              :                            &bin_str0_size);
    4108            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4109              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4110            1 :         bin_str2_size = sizeof(bin_str2);
    4111            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4112              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4113            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4114              :                            bin_str2, &bin_str2_size);
    4115            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4116              :                             bin_str2, bin_str2_size,
    4117              :                             response_handshake_secret, hash_size);
    4118            1 :         bin_str7_size = sizeof(bin_str7);
    4119            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4120              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4121            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4122              :                            &bin_str7_size);
    4123            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4124              :                             hash_size, bin_str7, bin_str7_size,
    4125              :                             response_finished_key, hash_size);
    4126            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4127              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4128            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4129              :                          response_finished_key, hash_size, ptr);
    4130            1 :         ptr += hmac_size;
    4131              : 
    4132            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4133              :                                               false, spdm_response_size,
    4134              :                                               spdm_response, response_size,
    4135              :                                               response);
    4136              :     }
    4137            1 :         return LIBSPDM_STATUS_SUCCESS;
    4138              : 
    4139            0 :     case 0x1D: {
    4140              :         spdm_key_exchange_response_t *spdm_response;
    4141              :         size_t dhe_key_size;
    4142              :         uint32_t hash_size;
    4143              :         size_t signature_size;
    4144              :         uint32_t hmac_size;
    4145              :         uint8_t *ptr;
    4146              :         void *dhe_context;
    4147              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4148              :         size_t final_key_size;
    4149              :         size_t opaque_key_exchange_rsp_size;
    4150              :         void *data;
    4151              :         size_t data_size;
    4152              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4153              :         uint8_t *cert_buffer;
    4154              :         size_t cert_buffer_size;
    4155              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4156              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4157              :         uint8_t bin_str0[128];
    4158              :         size_t bin_str0_size;
    4159              :         uint8_t bin_str2[128];
    4160              :         size_t bin_str2_size;
    4161              :         uint8_t bin_str7[128];
    4162              :         size_t bin_str7_size;
    4163              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4164              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4165              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4166              :         size_t spdm_response_size;
    4167              :         size_t transport_header_size;
    4168              : 
    4169            0 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_asym_algo =
    4170              :             m_libspdm_use_asym_algo;
    4171            0 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_hash_algo =
    4172              :             m_libspdm_use_hash_algo;
    4173            0 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.dhe_named_group =
    4174              :             m_libspdm_use_dhe_algo;
    4175            0 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.measurement_hash_algo =
    4176              :             m_libspdm_use_measurement_hash_algo;
    4177            0 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4178            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4179            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4180            0 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4181            0 :         opaque_key_exchange_rsp_size =
    4182            0 :             libspdm_get_opaque_data_version_selection_data_size(spdm_context);
    4183            0 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4184            0 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4185            0 :                              opaque_key_exchange_rsp_size + signature_size +
    4186              :                              hmac_size;
    4187            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4188            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4189              : 
    4190            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    4191            0 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    4192            0 :         spdm_response->header.param1 = 0;
    4193            0 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    4194            0 :         spdm_response->mut_auth_requested = 0;
    4195            0 :         spdm_response->req_slot_id_param = 0;
    4196            0 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
    4197            0 :         ptr = (void *)(spdm_response + 1);
    4198            0 :         dhe_context = libspdm_dhe_new(
    4199            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4200              :                 m_libspdm_use_dhe_algo, true);
    4201            0 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    4202            0 :         final_key_size = sizeof(final_key);
    4203            0 :         libspdm_dhe_compute_key(
    4204              :             m_libspdm_use_dhe_algo, dhe_context,
    4205            0 :             (uint8_t *)&m_libspdm_local_buffer[0] + sizeof(spdm_key_exchange_request_t),
    4206              :             dhe_key_size, final_key, &final_key_size);
    4207            0 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4208            0 :         ptr += dhe_key_size;
    4209              :         /* libspdm_zero_mem (ptr, hash_size);
    4210              :          * ptr += hash_size;*/
    4211            0 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4212            0 :         ptr += sizeof(uint16_t);
    4213            0 :         libspdm_build_opaque_data_version_selection_data(
    4214              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4215              :                 &opaque_key_exchange_rsp_size, ptr);
    4216            0 :         ptr += opaque_key_exchange_rsp_size;
    4217            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4218              :                                                         m_libspdm_use_asym_algo, &data,
    4219              :                                                         &data_size, NULL, NULL);
    4220            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4221              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
    4222            0 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4223            0 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4224            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer (0x%zx):\n",
    4225              :                        m_libspdm_local_buffer_size));
    4226            0 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4227            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4228            0 :         cert_buffer = (uint8_t *)data;
    4229            0 :         cert_buffer_size = data_size;
    4230            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4231              :                          cert_buffer_hash);
    4232              :         /* transcript.message_a size is 0*/
    4233            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4234            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4235              :                                       m_libspdm_local_buffer_size);
    4236            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4237              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4238            0 :         free(data);
    4239            0 :         libspdm_responder_data_sign(
    4240              :             spdm_context,
    4241            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4242              :                 SPDM_KEY_EXCHANGE_RSP,
    4243              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4244            0 :                 false, libspdm_get_managed_buffer(&th_curr),
    4245              :                 libspdm_get_managed_buffer_size(&th_curr), ptr, &signature_size);
    4246            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4247              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
    4248              :                          ptr, signature_size);
    4249            0 :         m_libspdm_local_buffer_size += signature_size;
    4250            0 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4251            0 :         ptr += signature_size;
    4252            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4253              :                          libspdm_get_managed_buffer_size(&th_curr), th_curr_hash_data);
    4254            0 :         bin_str0_size = sizeof(bin_str0);
    4255            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4256              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4257            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0, &bin_str0_size);
    4258            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4259              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4260            0 :         bin_str2_size = sizeof(bin_str2);
    4261            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4262              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4263            0 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4264              :                            bin_str2, &bin_str2_size);
    4265            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4266              :                             bin_str2, bin_str2_size,
    4267              :                             response_handshake_secret, hash_size);
    4268            0 :         bin_str7_size = sizeof(bin_str7);
    4269            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4270              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4271            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4272              :                            &bin_str7_size);
    4273            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4274              :                             hash_size, bin_str7, bin_str7_size,
    4275              :                             response_finished_key, hash_size);
    4276            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4277              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4278            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4279              :                          response_finished_key, hash_size, ptr);
    4280            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4281              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
    4282              :                          ptr, hmac_size);
    4283            0 :         m_libspdm_local_buffer_size += hmac_size;
    4284            0 :         ptr += hmac_size;
    4285              : 
    4286            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4287              :                                               false, spdm_response_size,
    4288              :                                               spdm_response, response_size,
    4289              :                                               response);
    4290              :     }
    4291            0 :         return LIBSPDM_STATUS_SUCCESS;
    4292            0 :     case 0x1E: {
    4293              :         spdm_key_exchange_response_t *spdm_response;
    4294              :         size_t dhe_key_size;
    4295              :         uint32_t hash_size;
    4296              :         size_t signature_size;
    4297              :         uint32_t hmac_size;
    4298              :         uint8_t *ptr;
    4299              :         void *dhe_context;
    4300              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4301              :         size_t final_key_size;
    4302              :         size_t opaque_key_exchange_rsp_size;
    4303              :         void *data;
    4304              :         size_t data_size;
    4305              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4306              :         uint8_t *cert_buffer;
    4307              :         size_t cert_buffer_size;
    4308              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4309              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4310              :         uint8_t bin_str0[128];
    4311              :         size_t bin_str0_size;
    4312              :         uint8_t bin_str2[128];
    4313              :         size_t bin_str2_size;
    4314              :         uint8_t bin_str7[128];
    4315              :         size_t bin_str7_size;
    4316              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4317              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4318              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4319              :         size_t spdm_response_size;
    4320              :         size_t transport_header_size;
    4321              : 
    4322              : 
    4323              :         ((libspdm_context_t *)spdm_context)
    4324            0 :         ->connection_info.algorithm.base_asym_algo =
    4325              :             m_libspdm_use_asym_algo;
    4326              :         ((libspdm_context_t *)spdm_context)
    4327            0 :         ->connection_info.algorithm.base_hash_algo =
    4328              :             m_libspdm_use_hash_algo;
    4329              :         ((libspdm_context_t *)spdm_context)
    4330            0 :         ->connection_info.algorithm.dhe_named_group =
    4331              :             m_libspdm_use_dhe_algo;
    4332              :         ((libspdm_context_t *)spdm_context)
    4333            0 :         ->connection_info.algorithm.measurement_hash_algo =
    4334              :             m_libspdm_use_measurement_hash_algo;
    4335            0 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4336            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4337            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4338            0 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4339            0 :         opaque_key_exchange_rsp_size =
    4340            0 :             libspdm_get_opaque_data_version_selection_data_size(
    4341              :                 spdm_context);
    4342            0 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4343            0 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4344            0 :                              opaque_key_exchange_rsp_size + signature_size +
    4345              :                              hmac_size;
    4346            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4347            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4348              : 
    4349            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
    4350            0 :         spdm_response->header.request_response_code =
    4351              :             SPDM_KEY_EXCHANGE_RSP;
    4352            0 :         spdm_response->header.param1 = 0;
    4353            0 :         spdm_response->rsp_session_id =
    4354            0 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    4355            0 :         spdm_response->mut_auth_requested = 0;
    4356            0 :         spdm_response->req_slot_id_param = 0;
    4357            0 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    4358            0 :                                   spdm_response->random_data);
    4359            0 :         ptr = (void *)(spdm_response + 1);
    4360            0 :         dhe_context = libspdm_dhe_new(
    4361            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4362              :                 m_libspdm_use_dhe_algo,
    4363              :                 true);
    4364            0 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    4365              :                                  &dhe_key_size);
    4366            0 :         final_key_size = sizeof(final_key);
    4367            0 :         libspdm_dhe_compute_key(
    4368              :             m_libspdm_use_dhe_algo, dhe_context,
    4369            0 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    4370              :             sizeof(spdm_key_exchange_request_t),
    4371              :             dhe_key_size, final_key, &final_key_size);
    4372            0 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4373            0 :         ptr += dhe_key_size;
    4374              :         /* libspdm_zero_mem (ptr, hash_size);
    4375              :          * ptr += hash_size;*/
    4376            0 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4377            0 :         ptr += sizeof(uint16_t);
    4378            0 :         libspdm_build_opaque_data_version_selection_data(
    4379              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4380              :                 &opaque_key_exchange_rsp_size, ptr);
    4381            0 :         ptr += opaque_key_exchange_rsp_size;
    4382            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4383              :                                                         m_libspdm_use_asym_algo, &data,
    4384              :                                                         &data_size, NULL, NULL);
    4385            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4386              :                          sizeof(m_libspdm_local_buffer)
    4387            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4388              :                             m_libspdm_local_buffer),
    4389            0 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4390            0 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4391            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    4392              :                        m_libspdm_local_buffer_size));
    4393            0 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4394            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4395            0 :         cert_buffer = (uint8_t *)data;
    4396            0 :         cert_buffer_size = data_size;
    4397            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4398              :                          cert_buffer_hash);
    4399              :         /* transcript.message_a size is 0*/
    4400            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4401            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4402              :                                       m_libspdm_local_buffer_size);
    4403            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4404              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4405            0 :         free(data);
    4406            0 :         libspdm_responder_data_sign(
    4407              :             spdm_context,
    4408            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4409              :                 SPDM_KEY_EXCHANGE_RSP,
    4410              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4411            0 :                 false, libspdm_get_managed_buffer(&th_curr),
    4412              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    4413              :                 &signature_size);
    4414            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4415              :                          sizeof(m_libspdm_local_buffer)
    4416            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4417              :                             m_libspdm_local_buffer),
    4418              :                          ptr, signature_size);
    4419            0 :         m_libspdm_local_buffer_size += signature_size;
    4420            0 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4421            0 :         ptr += signature_size;
    4422            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4423              :                          libspdm_get_managed_buffer_size(&th_curr),
    4424              :                          th_curr_hash_data);
    4425            0 :         bin_str0_size = sizeof(bin_str0);
    4426            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4427              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4428            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    4429              :                            &bin_str0_size);
    4430            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4431              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4432            0 :         bin_str2_size = sizeof(bin_str2);
    4433            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4434              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4435            0 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4436              :                            bin_str2, &bin_str2_size);
    4437            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4438              :                             bin_str2, bin_str2_size,
    4439              :                             response_handshake_secret, hash_size);
    4440            0 :         bin_str7_size = sizeof(bin_str7);
    4441            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4442              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4443            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4444              :                            &bin_str7_size);
    4445            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4446              :                             hash_size, bin_str7, bin_str7_size,
    4447              :                             response_finished_key, hash_size);
    4448            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4449              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4450            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4451              :                          response_finished_key, hash_size, ptr);
    4452            0 :         ptr += hmac_size;
    4453              : 
    4454            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4455              :                                               false, spdm_response_size,
    4456              :                                               spdm_response, response_size,
    4457              :                                               response);
    4458              :     }
    4459            0 :         return LIBSPDM_STATUS_SUCCESS;
    4460              : 
    4461            1 :     case 0x1F: {
    4462              :         spdm_key_exchange_response_t *spdm_response;
    4463              :         size_t dhe_key_size;
    4464              :         uint32_t hash_size;
    4465              :         size_t signature_size;
    4466              :         uint32_t hmac_size;
    4467              :         uint8_t *ptr;
    4468              :         void *dhe_context;
    4469              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4470              :         size_t final_key_size;
    4471              :         size_t opaque_key_exchange_rsp_size;
    4472              :         void *data;
    4473              :         size_t data_size;
    4474              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4475              :         uint8_t *cert_buffer;
    4476              :         size_t cert_buffer_size;
    4477              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4478              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4479              :         uint8_t bin_str0[128];
    4480              :         size_t bin_str0_size;
    4481              :         uint8_t bin_str2[128];
    4482              :         size_t bin_str2_size;
    4483              :         uint8_t bin_str7[128];
    4484              :         size_t bin_str7_size;
    4485              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4486              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4487              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4488              :         size_t spdm_response_size;
    4489              :         size_t transport_header_size;
    4490              : 
    4491              :         ((libspdm_context_t *)spdm_context)
    4492            1 :         ->connection_info.algorithm.base_asym_algo =
    4493              :             m_libspdm_use_asym_algo;
    4494              :         ((libspdm_context_t *)spdm_context)
    4495            1 :         ->connection_info.algorithm.base_hash_algo =
    4496              :             m_libspdm_use_hash_algo;
    4497              :         ((libspdm_context_t *)spdm_context)
    4498            1 :         ->connection_info.algorithm.dhe_named_group =
    4499              :             m_libspdm_use_dhe_algo;
    4500              :         ((libspdm_context_t *)spdm_context)
    4501            1 :         ->connection_info.algorithm.measurement_hash_algo =
    4502              :             m_libspdm_use_measurement_hash_algo;
    4503            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4504            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4505            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4506            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4507            1 :         opaque_key_exchange_rsp_size =
    4508            1 :             libspdm_get_opaque_data_version_selection_data_size(
    4509              :                 spdm_context);
    4510            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4511            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4512            1 :                              opaque_key_exchange_rsp_size + signature_size +
    4513              :                              hmac_size;
    4514            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4515            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4516            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    4517              : 
    4518            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    4519            1 :         spdm_response->header.request_response_code =
    4520              :             SPDM_KEY_EXCHANGE_RSP;
    4521            1 :         spdm_response->header.param1 = 0;
    4522            1 :         spdm_response->rsp_session_id =
    4523            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    4524            1 :         spdm_response->mut_auth_requested =
    4525              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED;
    4526            1 :         spdm_response->req_slot_id_param = 9;  /* Invalid SlotID */
    4527            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    4528            1 :                                   spdm_response->random_data);
    4529            1 :         ptr = (void *)(spdm_response + 1);
    4530            1 :         dhe_context = libspdm_dhe_new(
    4531            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4532              :                 m_libspdm_use_dhe_algo,
    4533              :                 true);
    4534            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    4535              :                                  &dhe_key_size);
    4536            1 :         final_key_size = sizeof(final_key);
    4537            1 :         libspdm_dhe_compute_key(
    4538              :             m_libspdm_use_dhe_algo, dhe_context,
    4539            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    4540              :             sizeof(spdm_key_exchange_request_t),
    4541              :             dhe_key_size, final_key, &final_key_size);
    4542            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4543            1 :         ptr += dhe_key_size;
    4544              :         /* libspdm_zero_mem (ptr, hash_size);
    4545              :          * ptr += hash_size;*/
    4546            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4547            1 :         ptr += sizeof(uint16_t);
    4548            1 :         libspdm_build_opaque_data_version_selection_data(
    4549              :             spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4550              :                 &opaque_key_exchange_rsp_size, ptr);
    4551            1 :         ptr += opaque_key_exchange_rsp_size;
    4552            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4553              :                                                         m_libspdm_use_asym_algo, &data,
    4554              :                                                         &data_size, NULL, NULL);
    4555            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4556              :                          sizeof(m_libspdm_local_buffer)
    4557            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4558              :                             m_libspdm_local_buffer),
    4559            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4560            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4561            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    4562              :                        m_libspdm_local_buffer_size));
    4563            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4564            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4565            1 :         cert_buffer =  (uint8_t *)data;
    4566            1 :         cert_buffer_size = data_size;
    4567            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4568              :                          cert_buffer_hash);
    4569              :         /* transcript.message_a size is 0*/
    4570            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4571            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4572              :                                       m_libspdm_local_buffer_size);
    4573            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4574              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4575            1 :         free(data);
    4576            1 :         libspdm_responder_data_sign(
    4577              :             spdm_context,
    4578            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4579              :                 SPDM_KEY_EXCHANGE_RSP,
    4580              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4581            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    4582              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    4583              :                 &signature_size);
    4584            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4585              :                          sizeof(m_libspdm_local_buffer)
    4586            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4587              :                             m_libspdm_local_buffer),
    4588              :                          ptr, signature_size);
    4589            1 :         m_libspdm_local_buffer_size += signature_size;
    4590            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4591            1 :         ptr += signature_size;
    4592            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4593              :                          libspdm_get_managed_buffer_size(&th_curr),
    4594              :                          th_curr_hash_data);
    4595            1 :         bin_str0_size = sizeof(bin_str0);
    4596            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4597              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4598            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    4599              :                            &bin_str0_size);
    4600            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4601              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4602            1 :         bin_str2_size = sizeof(bin_str2);
    4603            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4604              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4605            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4606              :                            bin_str2, &bin_str2_size);
    4607            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4608              :                             bin_str2, bin_str2_size,
    4609              :                             response_handshake_secret, hash_size);
    4610            1 :         bin_str7_size = sizeof(bin_str7);
    4611            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4612              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4613            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4614              :                            &bin_str7_size);
    4615            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4616              :                             hash_size, bin_str7, bin_str7_size,
    4617              :                             response_finished_key, hash_size);
    4618            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4619              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4620            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4621              :                          response_finished_key, hash_size, ptr);
    4622            1 :         ptr += hmac_size;
    4623              : 
    4624            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4625              :                                               false, spdm_response_size,
    4626              :                                               spdm_response, response_size,
    4627              :                                               response);
    4628              :     }
    4629            1 :         return LIBSPDM_STATUS_SUCCESS;
    4630              : 
    4631            0 :     default:
    4632            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
    4633              :     }
    4634              : }
    4635              : 
    4636            1 : static void libspdm_test_requester_key_exchange_err_case1(void **state)
    4637              : {
    4638              :     libspdm_return_t status;
    4639              :     libspdm_test_context_t *spdm_test_context;
    4640              :     libspdm_context_t *spdm_context;
    4641              :     uint32_t session_id;
    4642              :     uint8_t heartbeat_period;
    4643              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    4644              :     uint8_t slot_id_param;
    4645              :     void *data;
    4646              :     size_t data_size;
    4647              :     void *hash;
    4648              :     size_t hash_size;
    4649              : 
    4650            1 :     spdm_test_context = *state;
    4651            1 :     spdm_context = spdm_test_context->spdm_context;
    4652            1 :     spdm_test_context->case_id = 0x1;
    4653            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    4654              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    4655            1 :     spdm_context->connection_info.connection_state =
    4656              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    4657            1 :     spdm_context->connection_info.capability.flags |=
    4658              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    4659              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    4660            1 :     spdm_context->local_context.capability.flags |=
    4661              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    4662              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    4663            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4664              :                                                          m_libspdm_use_asym_algo, &data,
    4665              :                                                          &data_size, &hash, &hash_size)) {
    4666            0 :         assert(false);
    4667              :     }
    4668            1 :     libspdm_reset_message_a(spdm_context);
    4669            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    4670              :         m_libspdm_use_hash_algo;
    4671            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    4672              :         m_libspdm_use_asym_algo;
    4673            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    4674              :         m_libspdm_use_dhe_algo;
    4675            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    4676              :         m_libspdm_use_aead_algo;
    4677              : 
    4678              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    4679              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    4680              :         data_size;
    4681              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    4682              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    4683              :                      data, data_size);
    4684              : #else
    4685            1 :     libspdm_hash_all(
    4686              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4687              :         data, data_size,
    4688            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    4689            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    4690            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    4691            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    4692              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4693              :         spdm_context->connection_info.algorithm.base_asym_algo,
    4694              :         data, data_size,
    4695              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    4696              : #endif
    4697              : 
    4698            1 :     heartbeat_period = 0;
    4699            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    4700            1 :     status = libspdm_send_receive_key_exchange(
    4701              :         spdm_context,
    4702              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    4703              :         &session_id, &heartbeat_period, &slot_id_param,
    4704              :         measurement_hash);
    4705            1 :     assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
    4706            1 :     free(data);
    4707            1 : }
    4708              : 
    4709            1 : static void libspdm_test_requester_key_exchange_err_case2(void **state)
    4710              : {
    4711              :     libspdm_return_t status;
    4712              :     libspdm_test_context_t *spdm_test_context;
    4713              :     libspdm_context_t *spdm_context;
    4714              :     uint32_t session_id;
    4715              :     uint8_t heartbeat_period;
    4716              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    4717              :     uint8_t slot_id_param;
    4718              :     void *data;
    4719              :     size_t data_size;
    4720              :     void *hash;
    4721              :     size_t hash_size;
    4722              : 
    4723            1 :     spdm_test_context = *state;
    4724            1 :     spdm_context = spdm_test_context->spdm_context;
    4725            1 :     spdm_test_context->case_id = 0x2;
    4726            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    4727              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    4728            1 :     spdm_context->connection_info.connection_state =
    4729              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    4730            1 :     spdm_context->connection_info.capability.flags |=
    4731              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    4732              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    4733            1 :     spdm_context->local_context.capability.flags |=
    4734              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    4735              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    4736            1 :     spdm_context->local_context.secured_message_version.spdm_version_count = 1;
    4737            1 :     spdm_context->local_context.secured_message_version.spdm_version[0] =
    4738              :         SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    4739            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4740              :                                                          m_libspdm_use_asym_algo, &data,
    4741              :                                                          &data_size, &hash, &hash_size)) {
    4742            0 :         assert(false);
    4743              :     }
    4744            1 :     libspdm_reset_message_a(spdm_context);
    4745            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    4746              :         m_libspdm_use_hash_algo;
    4747            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    4748              :         m_libspdm_use_asym_algo;
    4749            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    4750              :         m_libspdm_use_dhe_algo;
    4751            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    4752              :         m_libspdm_use_aead_algo;
    4753              : 
    4754              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    4755              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    4756              :         data_size;
    4757              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    4758              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    4759              :                      data, data_size);
    4760              : #else
    4761            1 :     libspdm_hash_all(
    4762              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4763              :         data, data_size,
    4764            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    4765            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    4766            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    4767            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    4768              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4769              :         spdm_context->connection_info.algorithm.base_asym_algo,
    4770              :         data, data_size,
    4771              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    4772              : #endif
    4773              : 
    4774            1 :     heartbeat_period = 0;
    4775            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    4776            1 :     status = libspdm_send_receive_key_exchange(
    4777              :         spdm_context,
    4778              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    4779              :         &session_id, &heartbeat_period, &slot_id_param,
    4780              :         measurement_hash);
    4781            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    4782            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    4783            1 :     assert_int_equal(
    4784              :         libspdm_secured_message_get_session_state(
    4785              :             spdm_context->session_info[0].secured_message_context),
    4786              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    4787            1 :     free(data);
    4788            1 : }
    4789              : 
    4790            1 : static void libspdm_test_requester_key_exchange_err_case3(void **state)
    4791              : {
    4792              :     libspdm_return_t status;
    4793              :     libspdm_test_context_t *spdm_test_context;
    4794              :     libspdm_context_t *spdm_context;
    4795              :     uint32_t session_id;
    4796              :     uint8_t heartbeat_period;
    4797              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    4798              :     uint8_t slot_id_param;
    4799              :     void *data;
    4800              :     size_t data_size;
    4801              :     void *hash;
    4802              :     size_t hash_size;
    4803              : 
    4804            1 :     spdm_test_context = *state;
    4805            1 :     spdm_context = spdm_test_context->spdm_context;
    4806            1 :     spdm_test_context->case_id = 0x3;
    4807            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    4808              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    4809            1 :     spdm_context->connection_info.connection_state =
    4810              :         LIBSPDM_CONNECTION_STATE_NOT_STARTED;
    4811            1 :     spdm_context->connection_info.capability.flags |=
    4812              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    4813              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    4814            1 :     spdm_context->local_context.capability.flags |=
    4815              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    4816              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    4817            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4818              :                                                          m_libspdm_use_asym_algo, &data,
    4819              :                                                          &data_size, &hash, &hash_size)) {
    4820            0 :         assert(false);
    4821              :     }
    4822            1 :     libspdm_reset_message_a(spdm_context);
    4823            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    4824              :         m_libspdm_use_hash_algo;
    4825            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    4826              :         m_libspdm_use_asym_algo;
    4827            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    4828              :         m_libspdm_use_dhe_algo;
    4829            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    4830              :         m_libspdm_use_aead_algo;
    4831              : 
    4832              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    4833              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    4834              :         data_size;
    4835              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    4836              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    4837              :                      data, data_size);
    4838              : #else
    4839            1 :     libspdm_hash_all(
    4840              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4841              :         data, data_size,
    4842            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    4843            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    4844            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    4845            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    4846              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4847              :         spdm_context->connection_info.algorithm.base_asym_algo,
    4848              :         data, data_size,
    4849              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    4850              : #endif
    4851              : 
    4852            1 :     heartbeat_period = 0;
    4853            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    4854            1 :     status = libspdm_send_receive_key_exchange(
    4855              :         spdm_context,
    4856              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    4857              :         &session_id, &heartbeat_period, &slot_id_param,
    4858              :         measurement_hash);
    4859            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    4860            1 :     free(data);
    4861            1 : }
    4862              : 
    4863            1 : static void libspdm_test_requester_key_exchange_err_case4(void **state)
    4864              : {
    4865              :     libspdm_return_t status;
    4866              :     libspdm_test_context_t *spdm_test_context;
    4867              :     libspdm_context_t *spdm_context;
    4868              :     uint32_t session_id;
    4869              :     uint8_t heartbeat_period;
    4870              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    4871              :     uint8_t slot_id_param;
    4872              :     void *data;
    4873              :     size_t data_size;
    4874              :     void *hash;
    4875              :     size_t hash_size;
    4876              : 
    4877            1 :     spdm_test_context = *state;
    4878            1 :     spdm_context = spdm_test_context->spdm_context;
    4879            1 :     spdm_test_context->case_id = 0x4;
    4880            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    4881              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    4882            1 :     spdm_context->connection_info.connection_state =
    4883              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    4884            1 :     spdm_context->connection_info.capability.flags |=
    4885              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    4886              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    4887            1 :     spdm_context->local_context.capability.flags |=
    4888              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    4889              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    4890            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4891              :                                                          m_libspdm_use_asym_algo, &data,
    4892              :                                                          &data_size, &hash, &hash_size)) {
    4893            0 :         assert(false);
    4894              :     }
    4895            1 :     libspdm_reset_message_a(spdm_context);
    4896            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    4897              :         m_libspdm_use_hash_algo;
    4898            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    4899              :         m_libspdm_use_asym_algo;
    4900            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    4901              :         m_libspdm_use_dhe_algo;
    4902            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    4903              :         m_libspdm_use_aead_algo;
    4904              : 
    4905              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    4906              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    4907              :         data_size;
    4908              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    4909              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    4910              :                      data, data_size);
    4911              : #else
    4912            1 :     libspdm_hash_all(
    4913              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4914              :         data, data_size,
    4915            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    4916            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    4917            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    4918            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    4919              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4920              :         spdm_context->connection_info.algorithm.base_asym_algo,
    4921              :         data, data_size,
    4922              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    4923              : #endif
    4924              : 
    4925            1 :     heartbeat_period = 0;
    4926            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    4927            1 :     status = libspdm_send_receive_key_exchange(
    4928              :         spdm_context,
    4929              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    4930              :         &session_id, &heartbeat_period, &slot_id_param,
    4931              :         measurement_hash);
    4932            1 :     assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
    4933            1 :     free(data);
    4934            1 : }
    4935              : 
    4936            1 : static void libspdm_test_requester_key_exchange_err_case5(void **state)
    4937              : {
    4938              :     libspdm_return_t status;
    4939              :     libspdm_test_context_t *spdm_test_context;
    4940              :     libspdm_context_t *spdm_context;
    4941              :     uint32_t session_id;
    4942              :     uint8_t heartbeat_period;
    4943              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    4944              :     uint8_t slot_id_param;
    4945              :     void *data;
    4946              :     size_t data_size;
    4947              :     void *hash;
    4948              :     size_t hash_size;
    4949              : 
    4950            1 :     spdm_test_context = *state;
    4951            1 :     spdm_context = spdm_test_context->spdm_context;
    4952            1 :     spdm_test_context->case_id = 0x5;
    4953            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    4954              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    4955            1 :     spdm_context->connection_info.connection_state =
    4956              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    4957            1 :     spdm_context->connection_info.capability.flags |=
    4958              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    4959              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    4960            1 :     spdm_context->local_context.capability.flags |=
    4961              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    4962              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    4963            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4964              :                                                          m_libspdm_use_asym_algo, &data,
    4965              :                                                          &data_size, &hash, &hash_size)) {
    4966            0 :         assert(false);
    4967              :     }
    4968            1 :     libspdm_reset_message_a(spdm_context);
    4969            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    4970              :         m_libspdm_use_hash_algo;
    4971            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    4972              :         m_libspdm_use_asym_algo;
    4973            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    4974              :         m_libspdm_use_dhe_algo;
    4975            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    4976              :         m_libspdm_use_aead_algo;
    4977              : 
    4978              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    4979              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    4980              :         data_size;
    4981              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    4982              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    4983              :                      data, data_size);
    4984              : #else
    4985            1 :     libspdm_hash_all(
    4986              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4987              :         data, data_size,
    4988            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    4989            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    4990            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    4991            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    4992              :         spdm_context->connection_info.algorithm.base_hash_algo,
    4993              :         spdm_context->connection_info.algorithm.base_asym_algo,
    4994              :         data, data_size,
    4995              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    4996              : #endif
    4997              : 
    4998            1 :     heartbeat_period = 0;
    4999            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5000            1 :     status = libspdm_send_receive_key_exchange(
    5001              :         spdm_context,
    5002              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5003              :         &session_id, &heartbeat_period, &slot_id_param,
    5004              :         measurement_hash);
    5005            1 :     assert_int_equal(status, LIBSPDM_STATUS_BUSY_PEER);
    5006            1 :     free(data);
    5007            1 : }
    5008              : 
    5009              : /**
    5010              :  * Test 6: Requester / Responder do not support key exchange.
    5011              :  * Expected Behavior: returns with error LIBSPDM_STATUS_UNSUPPORTED_CAP.
    5012              :  **/
    5013            1 : static void libspdm_test_requester_key_exchange_err_case6(void **state)
    5014              : {
    5015              :     libspdm_return_t status;
    5016              :     libspdm_test_context_t *spdm_test_context;
    5017              :     libspdm_context_t *spdm_context;
    5018              :     uint32_t session_id;
    5019              :     uint8_t slot_id_param;
    5020              : 
    5021            1 :     spdm_test_context = *state;
    5022            1 :     spdm_context = spdm_test_context->spdm_context;
    5023            1 :     spdm_test_context->case_id = 0x6;
    5024            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5025              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5026            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5027            1 :     spdm_context->connection_info.capability.flags = 0;
    5028            1 :     spdm_context->local_context.capability.flags = 0;
    5029              : 
    5030            1 :     status = libspdm_send_receive_key_exchange(
    5031              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5032              :         &session_id, NULL, &slot_id_param, NULL);
    5033            1 :     assert_int_equal(status, LIBSPDM_STATUS_UNSUPPORTED_CAP);
    5034            1 : }
    5035              : 
    5036            1 : static void libspdm_test_requester_key_exchange_err_case7(void **state)
    5037              : {
    5038              :     libspdm_return_t status;
    5039              :     libspdm_test_context_t *spdm_test_context;
    5040              :     libspdm_context_t *spdm_context;
    5041              :     uint32_t session_id;
    5042              :     uint8_t heartbeat_period;
    5043              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5044              :     uint8_t slot_id_param;
    5045              :     void *data;
    5046              :     size_t data_size;
    5047              :     void *hash;
    5048              :     size_t hash_size;
    5049              : 
    5050            1 :     spdm_test_context = *state;
    5051            1 :     spdm_context = spdm_test_context->spdm_context;
    5052            1 :     spdm_test_context->case_id = 0x7;
    5053            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5054              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5055            1 :     spdm_context->connection_info.connection_state =
    5056              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5057            1 :     spdm_context->connection_info.capability.flags |=
    5058              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5059              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5060            1 :     spdm_context->local_context.capability.flags |=
    5061              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5062              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5063            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5064              :                                                          m_libspdm_use_asym_algo, &data,
    5065              :                                                          &data_size, &hash, &hash_size)) {
    5066            0 :         assert(false);
    5067              :     }
    5068            1 :     libspdm_reset_message_a(spdm_context);
    5069            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5070              :         m_libspdm_use_hash_algo;
    5071            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5072              :         m_libspdm_use_asym_algo;
    5073            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5074              :         m_libspdm_use_dhe_algo;
    5075            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5076              :         m_libspdm_use_aead_algo;
    5077              : 
    5078              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5079              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5080              :         data_size;
    5081              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5082              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5083              :                      data, data_size);
    5084              : #else
    5085            1 :     libspdm_hash_all(
    5086              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5087              :         data, data_size,
    5088            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5089            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5090            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5091            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5092              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5093              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5094              :         data, data_size,
    5095              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5096              : #endif
    5097              : 
    5098            1 :     heartbeat_period = 0;
    5099            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5100            1 :     status = libspdm_send_receive_key_exchange(
    5101              :         spdm_context,
    5102              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5103              :         &session_id, &heartbeat_period, &slot_id_param,
    5104              :         measurement_hash);
    5105            1 :     assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
    5106            1 :     assert_int_equal(spdm_context->connection_info.connection_state,
    5107              :                      LIBSPDM_CONNECTION_STATE_NOT_STARTED);
    5108            1 :     free(data);
    5109            1 : }
    5110              : 
    5111            1 : static void libspdm_test_requester_key_exchange_err_case8(void **state)
    5112              : {
    5113              :     libspdm_return_t status;
    5114              :     libspdm_test_context_t *spdm_test_context;
    5115              :     libspdm_context_t *spdm_context;
    5116              :     uint32_t session_id;
    5117              :     uint8_t heartbeat_period;
    5118              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5119              :     uint8_t slot_id_param;
    5120              :     void *data;
    5121              :     size_t data_size;
    5122              :     void *hash;
    5123              :     size_t hash_size;
    5124              : 
    5125            1 :     spdm_test_context = *state;
    5126            1 :     spdm_context = spdm_test_context->spdm_context;
    5127            1 :     spdm_test_context->case_id = 0x8;
    5128            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5129              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5130            1 :     spdm_context->connection_info.connection_state =
    5131              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5132            1 :     spdm_context->connection_info.capability.flags |=
    5133              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5134              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5135            1 :     spdm_context->local_context.capability.flags |=
    5136              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5137              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5138            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5139              :                                                          m_libspdm_use_asym_algo, &data,
    5140              :                                                          &data_size, &hash, &hash_size)) {
    5141            0 :         assert(false);
    5142              :     }
    5143            1 :     libspdm_reset_message_a(spdm_context);
    5144            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5145              :         m_libspdm_use_hash_algo;
    5146            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5147              :         m_libspdm_use_asym_algo;
    5148            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5149              :         m_libspdm_use_dhe_algo;
    5150            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5151              :         m_libspdm_use_aead_algo;
    5152              : 
    5153              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5154              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5155              :         data_size;
    5156              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5157              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5158              :                      data, data_size);
    5159              : #else
    5160            1 :     libspdm_hash_all(
    5161              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5162              :         data, data_size,
    5163            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5164            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5165            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5166            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5167              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5168              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5169              :         data, data_size,
    5170              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5171              : #endif
    5172              : 
    5173            1 :     heartbeat_period = 0;
    5174            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5175            1 :     status = libspdm_send_receive_key_exchange(
    5176              :         spdm_context,
    5177              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5178              :         &session_id, &heartbeat_period, &slot_id_param,
    5179              :         measurement_hash);
    5180            1 :     assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    5181            1 :     free(data);
    5182            1 : }
    5183              : 
    5184              : /**
    5185              :  * Test 9: Unable to acquire the sender buffer.
    5186              :  * Expected Behavior: returns with error LIBSPDM_STATUS_ACQUIRE_FAIL.
    5187              :  **/
    5188            1 : static void libspdm_test_requester_key_exchange_err_case9(void **state)
    5189              : {
    5190              :     libspdm_return_t status;
    5191              :     libspdm_test_context_t *spdm_test_context;
    5192              :     libspdm_context_t *spdm_context;
    5193              :     uint32_t session_id;
    5194              :     uint8_t slot_id_param;
    5195              : 
    5196            1 :     spdm_test_context = *state;
    5197            1 :     spdm_context = spdm_test_context->spdm_context;
    5198            1 :     spdm_test_context->case_id = 0x9;
    5199            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5200              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5201            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5202            1 :     spdm_context->connection_info.capability.flags |=
    5203              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5204              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5205            1 :     spdm_context->local_context.capability.flags |=
    5206              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5207              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5208              : 
    5209            1 :     libspdm_force_error (LIBSPDM_ERR_ACQUIRE_SENDER_BUFFER);
    5210            1 :     status = libspdm_send_receive_key_exchange(
    5211              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5212              :         &session_id, NULL, &slot_id_param, NULL);
    5213            1 :     libspdm_release_error (LIBSPDM_ERR_ACQUIRE_SENDER_BUFFER);
    5214              : 
    5215            1 :     assert_int_equal(status, LIBSPDM_STATUS_ACQUIRE_FAIL);
    5216            1 : }
    5217              : 
    5218            1 : static void libspdm_test_requester_key_exchange_err_case10(void **state) {
    5219              :     libspdm_return_t status;
    5220              :     libspdm_test_context_t    *spdm_test_context;
    5221              :     libspdm_context_t  *spdm_context;
    5222              :     uint32_t session_id;
    5223              :     uint8_t heartbeat_period;
    5224              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5225              :     uint8_t slot_id_param;
    5226              :     void                 *data;
    5227              :     size_t data_size;
    5228              :     void                 *hash;
    5229              :     size_t hash_size;
    5230              :     uint16_t error_code;
    5231              : 
    5232            1 :     spdm_test_context = *state;
    5233            1 :     spdm_context = spdm_test_context->spdm_context;
    5234            1 :     spdm_test_context->case_id = 0xA;
    5235            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5236              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5237            1 :     spdm_context->connection_info.capability.flags |=
    5238              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    5239            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    5240            1 :     libspdm_read_responder_public_certificate_chain (m_libspdm_use_hash_algo,
    5241              :                                                      m_libspdm_use_asym_algo,
    5242              :                                                      &data, &data_size,
    5243              :                                                      &hash, &hash_size);
    5244            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    5245            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    5246            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    5247            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    5248              : 
    5249              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5250              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5251              :         data_size;
    5252              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5253              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5254              :                      data, data_size);
    5255              : #else
    5256            1 :     libspdm_hash_all(
    5257              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5258              :         data, data_size,
    5259            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5260            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5261            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5262            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5263              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5264              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5265              :         data, data_size,
    5266              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5267              : #endif
    5268              : 
    5269            1 :     error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
    5270           19 :     while(error_code <= 0xff) {
    5271           18 :         spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5272           18 :         libspdm_reset_message_a(spdm_context);
    5273              : 
    5274           18 :         heartbeat_period = 0;
    5275           18 :         libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5276           18 :         status = libspdm_send_receive_key_exchange (spdm_context,
    5277              :                                                     SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH,
    5278              :                                                     0, 0, &session_id, &heartbeat_period,
    5279              :                                                     &slot_id_param, measurement_hash);
    5280           18 :         LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_ERROR_PEER, error_code);
    5281              : 
    5282           18 :         error_code++;
    5283           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
    5284            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
    5285              :         }
    5286           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
    5287            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
    5288              :         }
    5289           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
    5290            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
    5291              :         }
    5292              :     }
    5293              : 
    5294            1 :     free(data);
    5295            1 : }
    5296              : 
    5297              : /**
    5298              :  * Test 11: Unable to acquire the receiver buffer.
    5299              :  * Expected Behavior: returns with error LIBSPDM_STATUS_ACQUIRE_FAIL.
    5300              :  **/
    5301            1 : static void libspdm_test_requester_key_exchange_err_case11(void **state)
    5302              : {
    5303              :     libspdm_return_t status;
    5304              :     libspdm_test_context_t *spdm_test_context;
    5305              :     libspdm_context_t *spdm_context;
    5306              :     uint32_t session_id;
    5307              :     uint8_t slot_id_param;
    5308              : 
    5309            1 :     spdm_test_context = *state;
    5310            1 :     spdm_context = spdm_test_context->spdm_context;
    5311            1 :     spdm_test_context->case_id = 0xb;
    5312            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5313              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5314            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5315            1 :     spdm_context->connection_info.capability.flags |=
    5316              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5317              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5318            1 :     spdm_context->local_context.capability.flags |=
    5319              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5320              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5321              : 
    5322            1 :     libspdm_force_error (LIBSPDM_ERR_ACQUIRE_RECEIVER_BUFFER);
    5323            1 :     status = libspdm_send_receive_key_exchange(
    5324              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5325              :         &session_id, NULL, &slot_id_param, NULL);
    5326            1 :     libspdm_release_error (LIBSPDM_ERR_ACQUIRE_RECEIVER_BUFFER);
    5327              : 
    5328            1 :     assert_int_equal(status, LIBSPDM_STATUS_ACQUIRE_FAIL);
    5329            1 : }
    5330              : 
    5331              : /**
    5332              :  * Test 12: Version in response is incorrect.
    5333              :  * Expected Behavior: returns with error LIBSPDM_STATUS_INVALID_MSG_FIELD.
    5334              :  **/
    5335            1 : static void libspdm_test_requester_key_exchange_err_case12(void **state)
    5336              : {
    5337              :     libspdm_return_t status;
    5338              :     libspdm_test_context_t *spdm_test_context;
    5339              :     libspdm_context_t *spdm_context;
    5340              :     uint32_t session_id;
    5341              :     uint8_t slot_id_param;
    5342              : 
    5343            1 :     spdm_test_context = *state;
    5344            1 :     spdm_context = spdm_test_context->spdm_context;
    5345            1 :     spdm_test_context->case_id = 0xc;
    5346            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5347              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5348            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5349            1 :     spdm_context->connection_info.capability.flags |=
    5350              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5351              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5352            1 :     spdm_context->local_context.capability.flags |=
    5353              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5354              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5355              : 
    5356            1 :     status = libspdm_send_receive_key_exchange(
    5357              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5358              :         &session_id, NULL, &slot_id_param, NULL);
    5359              : 
    5360            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    5361            1 : }
    5362              : 
    5363              : /**
    5364              :  * Test 13: Response code is incorrect.
    5365              :  * Expected Behavior: returns with error LIBSPDM_STATUS_INVALID_MSG_FIELD.
    5366              :  **/
    5367            1 : static void libspdm_test_requester_key_exchange_err_case13(void **state)
    5368              : {
    5369              :     libspdm_return_t status;
    5370              :     libspdm_test_context_t *spdm_test_context;
    5371              :     libspdm_context_t *spdm_context;
    5372              :     uint32_t session_id;
    5373              :     uint8_t slot_id_param;
    5374              : 
    5375            1 :     spdm_test_context = *state;
    5376            1 :     spdm_context = spdm_test_context->spdm_context;
    5377            1 :     spdm_test_context->case_id = 0xd;
    5378            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5379              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5380            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5381            1 :     spdm_context->connection_info.capability.flags |=
    5382              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5383              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5384            1 :     spdm_context->local_context.capability.flags |=
    5385              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5386              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5387              : 
    5388            1 :     status = libspdm_send_receive_key_exchange(
    5389              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5390              :         &session_id, NULL, &slot_id_param, NULL);
    5391              : 
    5392            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    5393            1 : }
    5394              : 
    5395              : /**
    5396              :  * Test 14: Responder requests mutual authentication when it is not supported.
    5397              :  * Expected Behavior: returns with error LIBSPDM_STATUS_INVALID_MSG_FIELD.
    5398              :  **/
    5399            1 : static void libspdm_test_requester_key_exchange_err_case14(void **state)
    5400              : {
    5401              :     libspdm_return_t status;
    5402              :     libspdm_test_context_t *spdm_test_context;
    5403              :     libspdm_context_t *spdm_context;
    5404              :     uint32_t session_id;
    5405              :     uint8_t slot_id_param;
    5406              : 
    5407            1 :     spdm_test_context = *state;
    5408            1 :     spdm_context = spdm_test_context->spdm_context;
    5409            1 :     spdm_test_context->case_id = 0xe;
    5410            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5411              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5412            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5413            1 :     spdm_context->connection_info.capability.flags |=
    5414              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5415              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5416            1 :     spdm_context->local_context.capability.flags |=
    5417              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5418              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5419              : 
    5420            1 :     status = libspdm_send_receive_key_exchange(
    5421              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5422              :         &session_id, NULL, &slot_id_param, NULL);
    5423              : 
    5424            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    5425            1 : }
    5426              : 
    5427            1 : static void libspdm_test_requester_key_exchange_err_case15(void **state)
    5428              : {
    5429              :     libspdm_return_t status;
    5430              :     libspdm_test_context_t *spdm_test_context;
    5431              :     libspdm_context_t *spdm_context;
    5432              :     uint32_t session_id;
    5433              :     uint8_t heartbeat_period;
    5434              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5435              :     uint8_t slot_id_param;
    5436              :     void *data;
    5437              :     size_t data_size;
    5438              :     void *hash;
    5439              :     size_t hash_size;
    5440              : 
    5441            1 :     spdm_test_context = *state;
    5442            1 :     spdm_context = spdm_test_context->spdm_context;
    5443              : 
    5444            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    5445            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    5446              :     }
    5447              : 
    5448            1 :     spdm_test_context->case_id = 0xF;
    5449            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5450              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5451            1 :     spdm_context->connection_info.connection_state =
    5452              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5453            1 :     spdm_context->connection_info.capability.flags |=
    5454              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    5455            1 :     spdm_context->connection_info.capability.flags |=
    5456              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    5457            1 :     spdm_context->local_context.capability.flags |=
    5458              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    5459              : 
    5460            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5461              :                                                          m_libspdm_use_asym_algo, &data,
    5462              :                                                          &data_size, &hash, &hash_size)) {
    5463            0 :         assert(false);
    5464              :     }
    5465            1 :     libspdm_reset_message_a(spdm_context);
    5466            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5467              :         m_libspdm_use_hash_algo;
    5468            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5469              :         m_libspdm_use_asym_algo;
    5470            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5471              :         m_libspdm_use_dhe_algo;
    5472            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5473              :         m_libspdm_use_aead_algo;
    5474            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    5475              :         m_libspdm_use_measurement_hash_algo;
    5476              : 
    5477              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5478              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5479              :         data_size;
    5480              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5481              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5482              :                      data, data_size);
    5483              : #else
    5484            1 :     libspdm_hash_all(
    5485              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5486              :         data, data_size,
    5487            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5488            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5489            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5490            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5491              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5492              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5493              :         data, data_size,
    5494              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5495              : #endif
    5496              : 
    5497            1 :     heartbeat_period = 0;
    5498            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5499            1 :     status = libspdm_send_receive_key_exchange(
    5500              :         spdm_context,
    5501              :         SPDM_KEY_EXCHANGE_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH, 0, 0,
    5502              :         &session_id, &heartbeat_period, &slot_id_param,
    5503              :         measurement_hash);
    5504            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
    5505            1 :     free(data);
    5506            1 : }
    5507              : 
    5508            1 : static void libspdm_test_requester_key_exchange_err_case16(void **state)
    5509              : {
    5510              :     libspdm_return_t status;
    5511              :     libspdm_test_context_t *spdm_test_context;
    5512              :     libspdm_context_t *spdm_context;
    5513              :     uint32_t session_id;
    5514              :     uint8_t heartbeat_period;
    5515              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5516              :     uint8_t slot_id_param;
    5517              :     void *data;
    5518              :     size_t data_size;
    5519              :     void *hash;
    5520              :     size_t hash_size;
    5521              : 
    5522            1 :     spdm_test_context = *state;
    5523            1 :     spdm_context = spdm_test_context->spdm_context;
    5524              : 
    5525            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    5526            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    5527              :     }
    5528              : 
    5529            1 :     spdm_test_context->case_id = 0x10;
    5530            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5531              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5532            1 :     spdm_context->connection_info.connection_state =
    5533              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5534            1 :     spdm_context->connection_info.capability.flags |=
    5535              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    5536            1 :     spdm_context->connection_info.capability.flags |=
    5537              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    5538            1 :     spdm_context->local_context.capability.flags |=
    5539              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    5540              : 
    5541            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5542              :                                                          m_libspdm_use_asym_algo, &data,
    5543              :                                                          &data_size, &hash, &hash_size)) {
    5544            0 :         assert(false);
    5545              :     }
    5546            1 :     libspdm_reset_message_a(spdm_context);
    5547            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5548              :         m_libspdm_use_hash_algo;
    5549            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5550              :         m_libspdm_use_asym_algo;
    5551            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5552              :         m_libspdm_use_dhe_algo;
    5553            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5554              :         m_libspdm_use_aead_algo;
    5555            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    5556              :         m_libspdm_use_measurement_hash_algo;
    5557              : 
    5558              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5559              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5560              :         data_size;
    5561              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5562              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5563              :                      data, data_size);
    5564              : #else
    5565            1 :     libspdm_hash_all(
    5566              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5567              :         data, data_size,
    5568            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5569            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5570            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5571            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5572              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5573              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5574              :         data, data_size,
    5575              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5576              : #endif
    5577              : 
    5578            1 :     heartbeat_period = 0;
    5579            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5580            1 :     status = libspdm_send_receive_key_exchange(
    5581              :         spdm_context,
    5582              :         SPDM_KEY_EXCHANGE_REQUEST_ALL_MEASUREMENTS_HASH, 0, 0,
    5583              :         &session_id, &heartbeat_period, &slot_id_param,
    5584              :         measurement_hash);
    5585            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
    5586            1 :     free(data);
    5587            1 : }
    5588              : 
    5589            1 : static void libspdm_test_requester_key_exchange_err_case17(void **state)
    5590              : {
    5591              :     libspdm_return_t status;
    5592              :     libspdm_test_context_t *spdm_test_context;
    5593              :     libspdm_context_t *spdm_context;
    5594              :     uint32_t session_id;
    5595              :     uint8_t heartbeat_period;
    5596              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5597              :     uint8_t slot_id_param;
    5598              :     void *data;
    5599              :     size_t data_size;
    5600              :     void *hash;
    5601              :     size_t hash_size;
    5602              : 
    5603            1 :     spdm_test_context = *state;
    5604            1 :     spdm_context = spdm_test_context->spdm_context;
    5605              : 
    5606            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    5607            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    5608              :     }
    5609              : 
    5610            1 :     spdm_test_context->case_id = 0x11;
    5611            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5612              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5613            1 :     spdm_context->connection_info.connection_state =
    5614              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5615            1 :     spdm_context->connection_info.capability.flags |=
    5616              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    5617            1 :     spdm_context->connection_info.capability.flags |=
    5618              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    5619            1 :     spdm_context->local_context.capability.flags |=
    5620              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    5621              : 
    5622            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5623              :                                                          m_libspdm_use_asym_algo, &data,
    5624              :                                                          &data_size, &hash, &hash_size)) {
    5625            0 :         assert(false);
    5626              :     }
    5627            1 :     libspdm_reset_message_a(spdm_context);
    5628            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5629              :         m_libspdm_use_hash_algo;
    5630            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5631              :         m_libspdm_use_asym_algo;
    5632            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5633              :         m_libspdm_use_dhe_algo;
    5634            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5635              :         m_libspdm_use_aead_algo;
    5636            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    5637              :         m_libspdm_use_measurement_hash_algo;
    5638              : 
    5639              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5640              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5641              :         data_size;
    5642              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5643              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5644              :                      data, data_size);
    5645              : #else
    5646            1 :     libspdm_hash_all(
    5647              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5648              :         data, data_size,
    5649            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5650            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5651            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5652            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5653              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5654              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5655              :         data, data_size,
    5656              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5657              : #endif
    5658              : 
    5659            1 :     heartbeat_period = 0;
    5660            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5661            1 :     status = libspdm_send_receive_key_exchange(
    5662              :         spdm_context,
    5663              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5664              :         &session_id, &heartbeat_period, &slot_id_param,
    5665              :         measurement_hash);
    5666            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
    5667            1 :     free(data);
    5668            1 : }
    5669              : 
    5670            1 : static void libspdm_test_requester_key_exchange_err_case18(void **state)
    5671              : {
    5672              :     libspdm_return_t status;
    5673              :     libspdm_test_context_t *spdm_test_context;
    5674              :     libspdm_context_t *spdm_context;
    5675              :     uint32_t session_id;
    5676              :     uint8_t heartbeat_period;
    5677              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5678              :     uint8_t slot_id_param;
    5679              :     void *data;
    5680              :     size_t data_size;
    5681              :     void *hash;
    5682              :     size_t hash_size;
    5683              : 
    5684            1 :     spdm_test_context = *state;
    5685            1 :     spdm_context = spdm_test_context->spdm_context;
    5686              : 
    5687            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    5688            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    5689              :     }
    5690              : 
    5691            1 :     spdm_test_context->case_id = 0x12;
    5692            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5693              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5694            1 :     spdm_context->connection_info.connection_state =
    5695              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5696            1 :     spdm_context->connection_info.capability.flags |=
    5697              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    5698            1 :     spdm_context->connection_info.capability.flags |=
    5699              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    5700            1 :     spdm_context->local_context.capability.flags |=
    5701              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    5702              : 
    5703            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5704              :                                                          m_libspdm_use_asym_algo, &data,
    5705              :                                                          &data_size, &hash, &hash_size)) {
    5706            0 :         assert(false);
    5707              :     }
    5708            1 :     libspdm_reset_message_a(spdm_context);
    5709            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5710              :         m_libspdm_use_hash_algo;
    5711            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5712              :         m_libspdm_use_asym_algo;
    5713            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5714              :         m_libspdm_use_dhe_algo;
    5715            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5716              :         m_libspdm_use_aead_algo;
    5717            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    5718              :         m_libspdm_use_measurement_hash_algo;
    5719              : 
    5720              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5721              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5722              :         data_size;
    5723              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5724              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5725              :                      data, data_size);
    5726              : #else
    5727            1 :     libspdm_hash_all(
    5728              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5729              :         data, data_size,
    5730            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5731            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5732            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5733            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5734              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5735              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5736              :         data, data_size,
    5737              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5738              : #endif
    5739              : 
    5740            1 :     heartbeat_period = 0;
    5741            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5742            1 :     status = libspdm_send_receive_key_exchange(
    5743              :         spdm_context,
    5744              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5745              :         &session_id, &heartbeat_period, &slot_id_param,
    5746              :         measurement_hash);
    5747            1 :     assert_int_equal(status, LIBSPDM_STATUS_VERIF_FAIL);
    5748            1 :     free(data);
    5749            1 : }
    5750              : 
    5751              : /**
    5752              :  * Test 19:
    5753              :  * Expected Behavior:
    5754              :  **/
    5755            1 : static void libspdm_test_requester_key_exchange_err_case19(void **state)
    5756              : {
    5757            1 : }
    5758              : 
    5759            1 : static void libspdm_test_requester_key_exchange_err_case20(void **state)
    5760              : {
    5761              :     libspdm_return_t status;
    5762              :     libspdm_test_context_t *spdm_test_context;
    5763              :     libspdm_context_t *spdm_context;
    5764              :     uint32_t session_id;
    5765              :     uint8_t heartbeat_period;
    5766              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5767              :     uint8_t slot_id_param;
    5768              :     void *data;
    5769              :     size_t data_size;
    5770              :     void *hash;
    5771              :     size_t hash_size;
    5772              : 
    5773            1 :     spdm_test_context = *state;
    5774            1 :     spdm_context = spdm_test_context->spdm_context;
    5775              : 
    5776            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    5777            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    5778              :     }
    5779              : 
    5780            1 :     spdm_test_context->case_id = 0x14;
    5781            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5782              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5783            1 :     spdm_context->connection_info.connection_state =
    5784              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5785            1 :     spdm_context->connection_info.capability.flags |=
    5786              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    5787            1 :     spdm_context->connection_info.capability.flags |=
    5788              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    5789            1 :     spdm_context->local_context.capability.flags |=
    5790              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    5791              : 
    5792              : 
    5793            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5794              :                                                          m_libspdm_use_asym_algo, &data,
    5795              :                                                          &data_size, &hash, &hash_size)) {
    5796            0 :         assert(false);
    5797              :     }
    5798            1 :     libspdm_reset_message_a(spdm_context);
    5799            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5800              :         m_libspdm_use_hash_algo;
    5801            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5802              :         m_libspdm_use_asym_algo;
    5803            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5804              :         m_libspdm_use_dhe_algo;
    5805            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5806              :         m_libspdm_use_aead_algo;
    5807            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    5808              :         m_libspdm_use_measurement_hash_algo;
    5809              : 
    5810              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5811              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5812              :         data_size;
    5813              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5814              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5815              :                      data, data_size);
    5816              : #else
    5817            1 :     libspdm_hash_all(
    5818              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5819              :         data, data_size,
    5820            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5821            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5822            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5823            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5824              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5825              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5826              :         data, data_size,
    5827              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5828              : #endif
    5829              : 
    5830            1 :     heartbeat_period = 0;
    5831            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5832            1 :     status = libspdm_send_receive_key_exchange(
    5833              :         spdm_context,
    5834              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5835              :         &session_id, &heartbeat_period, &slot_id_param,
    5836              :         measurement_hash);
    5837            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    5838            1 :     free(data);
    5839            1 : }
    5840              : 
    5841              : /**
    5842              :  * Test 21: OpaqueLength is larger than the recommended value.
    5843              :  * Expected Behavior: Returns with LIBSPDM_STATUS_INVALID_MSG_FIELD.
    5844              :  **/
    5845            1 : static void libspdm_test_requester_key_exchange_err_case21(void **state)
    5846              : {
    5847              :     libspdm_return_t status;
    5848              :     libspdm_test_context_t *spdm_test_context;
    5849              :     libspdm_context_t *spdm_context;
    5850              :     uint32_t session_id;
    5851              :     uint8_t slot_id_param;
    5852              : 
    5853            1 :     spdm_test_context = *state;
    5854            1 :     spdm_context = spdm_test_context->spdm_context;
    5855            1 :     spdm_test_context->case_id = 0x15;
    5856            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5857              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5858            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5859            1 :     spdm_context->connection_info.capability.flags |=
    5860              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5861              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5862            1 :     spdm_context->local_context.capability.flags |=
    5863              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5864              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5865              : 
    5866            1 :     status = libspdm_send_receive_key_exchange(
    5867              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5868              :         &session_id, NULL, &slot_id_param, NULL);
    5869              : 
    5870            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    5871            1 : }
    5872              : 
    5873              : /**
    5874              :  * Test 22: Requester's PUB_KEY_ID_CAP is 1 but Responder sets MutAuthRequested to 2 (encapsulated
    5875              :  *          flow).
    5876              :  * Expected Behavior: Returns with LIBSPDM_STATUS_INVALID_MSG_FIELD.
    5877              :  **/
    5878            1 : static void libspdm_test_requester_key_exchange_err_case22(void **state)
    5879              : {
    5880              :     libspdm_return_t status;
    5881              :     libspdm_test_context_t *spdm_test_context;
    5882              :     libspdm_context_t *spdm_context;
    5883              :     uint32_t session_id;
    5884              :     uint8_t slot_id_param;
    5885              : 
    5886            1 :     spdm_test_context = *state;
    5887            1 :     spdm_context = spdm_test_context->spdm_context;
    5888            1 :     spdm_test_context->case_id = 0x16;
    5889            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5890              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5891            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5892            1 :     spdm_context->connection_info.capability.flags |=
    5893              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5894              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    5895            1 :     spdm_context->local_context.capability.flags |=
    5896              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5897              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP |
    5898              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
    5899              : 
    5900            1 :     status = libspdm_send_receive_key_exchange(
    5901              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5902              :         &session_id, NULL, &slot_id_param, NULL);
    5903              : 
    5904            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    5905            1 : }
    5906              : 
    5907              : /**
    5908              :  * Test 23: Responder attempts to initiate a mutual authentication encapsulated flow but both
    5909              :  *          endpoints do not support ENCAP_CAP.
    5910              :  * Expected Behavior: Returns with LIBSPDM_STATUS_INVALID_MSG_FIELD.
    5911              :  **/
    5912            1 : static void libspdm_test_requester_key_exchange_err_case23(void **state)
    5913              : {
    5914              :     libspdm_return_t status;
    5915              :     libspdm_test_context_t *spdm_test_context;
    5916              :     libspdm_context_t *spdm_context;
    5917              :     uint32_t session_id;
    5918              :     uint8_t slot_id_param;
    5919              : 
    5920            1 :     spdm_test_context = *state;
    5921            1 :     spdm_context = spdm_test_context->spdm_context;
    5922            1 :     spdm_test_context->case_id = 0x17;
    5923            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5924              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5925            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5926            1 :     spdm_context->connection_info.capability.flags =
    5927              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5928              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
    5929              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    5930            1 :     spdm_context->local_context.capability.flags =
    5931              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5932              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP |
    5933              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP |
    5934              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
    5935              : 
    5936            1 :     status = libspdm_send_receive_key_exchange(
    5937              :         spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5938              :         &session_id, NULL, &slot_id_param, NULL);
    5939              : 
    5940            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    5941            1 : }
    5942              : 
    5943            1 : static void libspdm_test_requester_key_exchange_err_case24(void **state)
    5944              : {
    5945            1 : }
    5946              : 
    5947            1 : static void libspdm_test_requester_key_exchange_err_case25(void **state)
    5948              : {
    5949            1 : }
    5950              : 
    5951            1 : static void libspdm_test_requester_key_exchange_err_case26(void **state)
    5952              : {
    5953              :     libspdm_return_t status;
    5954              :     libspdm_test_context_t *spdm_test_context;
    5955              :     libspdm_context_t *spdm_context;
    5956              :     uint32_t session_id;
    5957              :     uint8_t heartbeat_period;
    5958              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5959              :     uint8_t slot_id_param;
    5960              :     void *data;
    5961              :     size_t data_size;
    5962              :     void *hash;
    5963              :     size_t hash_size;
    5964              : 
    5965            1 :     spdm_test_context = *state;
    5966            1 :     spdm_context = spdm_test_context->spdm_context;
    5967              : 
    5968            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    5969            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    5970              :     }
    5971              : 
    5972            1 :     spdm_test_context->case_id = 0x1A;
    5973            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5974              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5975            1 :     spdm_context->connection_info.connection_state =
    5976              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5977            1 :     spdm_context->connection_info.capability.flags |=
    5978              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    5979            1 :     spdm_context->connection_info.capability.flags |=
    5980              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    5981            1 :     spdm_context->local_context.capability.flags |=
    5982              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    5983              : 
    5984            1 :     spdm_context->connection_info.capability.flags |=
    5985              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    5986            1 :     spdm_context->local_context.capability.flags |=
    5987              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    5988              : 
    5989              : 
    5990            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5991              :                                                          m_libspdm_use_asym_algo, &data,
    5992              :                                                          &data_size, &hash, &hash_size)) {
    5993            0 :         assert(false);
    5994              :     }
    5995            1 :     libspdm_reset_message_a(spdm_context);
    5996            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5997              :         m_libspdm_use_hash_algo;
    5998            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5999              :         m_libspdm_use_asym_algo;
    6000            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6001              :         m_libspdm_use_dhe_algo;
    6002            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6003              :         m_libspdm_use_aead_algo;
    6004            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6005              :         m_libspdm_use_measurement_hash_algo;
    6006              : 
    6007              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6008              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6009              :         data_size;
    6010              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6011              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6012              :                      data, data_size);
    6013              : #else
    6014            1 :     libspdm_hash_all(
    6015              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6016              :         data, data_size,
    6017            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6018            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6019            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6020            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6021              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6022              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6023              :         data, data_size,
    6024              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6025              : #endif
    6026              : 
    6027            1 :     heartbeat_period = 0;
    6028            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6029            1 :     status = libspdm_send_receive_key_exchange(
    6030              :         spdm_context,
    6031              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6032              :         &session_id, &heartbeat_period, &slot_id_param,
    6033              :         measurement_hash);
    6034              :     /* Clear Mut_auth flags */
    6035            1 :     spdm_context->connection_info.capability.flags &=
    6036              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    6037            1 :     spdm_context->local_context.capability.flags &=
    6038              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    6039            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    6040            1 :     free(data);
    6041            1 : }
    6042              : 
    6043            1 : static void libspdm_test_requester_key_exchange_err_case27(void **state)
    6044              : {
    6045              :     libspdm_return_t status;
    6046              :     libspdm_test_context_t *spdm_test_context;
    6047              :     libspdm_context_t *spdm_context;
    6048              :     uint32_t session_id;
    6049              :     uint8_t heartbeat_period;
    6050              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6051              :     uint8_t slot_id_param;
    6052              :     void *data;
    6053              :     size_t data_size;
    6054              :     void *hash;
    6055              :     size_t hash_size;
    6056              : 
    6057            1 :     spdm_test_context = *state;
    6058            1 :     spdm_context = spdm_test_context->spdm_context;
    6059              : 
    6060            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6061            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6062              :     }
    6063              : 
    6064            1 :     spdm_test_context->case_id = 0x1B;
    6065            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6066              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6067            1 :     spdm_context->connection_info.connection_state =
    6068              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6069            1 :     spdm_context->connection_info.capability.flags |=
    6070              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6071            1 :     spdm_context->connection_info.capability.flags |=
    6072              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6073            1 :     spdm_context->local_context.capability.flags |=
    6074              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6075              : 
    6076            1 :     spdm_context->connection_info.capability.flags |=
    6077              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    6078            1 :     spdm_context->local_context.capability.flags |=
    6079              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    6080              : 
    6081              : 
    6082            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6083              :                                                          m_libspdm_use_asym_algo, &data,
    6084              :                                                          &data_size, &hash, &hash_size)) {
    6085            0 :         assert(false);
    6086              :     }
    6087            1 :     libspdm_reset_message_a(spdm_context);
    6088            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6089              :         m_libspdm_use_hash_algo;
    6090            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6091              :         m_libspdm_use_asym_algo;
    6092            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6093              :         m_libspdm_use_dhe_algo;
    6094            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6095              :         m_libspdm_use_aead_algo;
    6096            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6097              :         m_libspdm_use_measurement_hash_algo;
    6098              : 
    6099              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6100              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6101              :         data_size;
    6102              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6103              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6104              :                      data, data_size);
    6105              : #else
    6106            1 :     libspdm_hash_all(
    6107              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6108              :         data, data_size,
    6109            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6110            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6111            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6112            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6113              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6114              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6115              :         data, data_size,
    6116              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6117              : #endif
    6118              : 
    6119            1 :     heartbeat_period = 0;
    6120            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6121            1 :     status = libspdm_send_receive_key_exchange(
    6122              :         spdm_context,
    6123              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6124              :         &session_id, &heartbeat_period, &slot_id_param,
    6125              :         measurement_hash);
    6126              :     /*Clear Mut_auth flags*/
    6127            1 :     spdm_context->connection_info.capability.flags &=
    6128              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    6129            1 :     spdm_context->local_context.capability.flags &=
    6130              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    6131            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    6132            1 :     free(data);
    6133            1 : }
    6134              : 
    6135            1 : static void libspdm_test_requester_key_exchange_err_case28(void **state)
    6136              : {
    6137              :     libspdm_return_t status;
    6138              :     libspdm_test_context_t *spdm_test_context;
    6139              :     libspdm_context_t *spdm_context;
    6140              :     uint32_t session_id;
    6141              :     uint8_t heartbeat_period;
    6142              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6143              :     uint8_t slot_id_param;
    6144              :     void *data;
    6145              :     size_t data_size;
    6146              :     void *hash;
    6147              :     size_t hash_size;
    6148              : 
    6149            1 :     spdm_test_context = *state;
    6150            1 :     spdm_context = spdm_test_context->spdm_context;
    6151              : 
    6152            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6153            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6154              :     }
    6155              : 
    6156            1 :     spdm_test_context->case_id = 0x1C;
    6157            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6158              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6159            1 :     spdm_context->connection_info.connection_state =
    6160              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6161            1 :     spdm_context->connection_info.capability.flags |=
    6162              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6163            1 :     spdm_context->connection_info.capability.flags |=
    6164              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6165            1 :     spdm_context->local_context.capability.flags |=
    6166              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6167              : 
    6168            1 :     spdm_context->connection_info.capability.flags |=
    6169              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    6170            1 :     spdm_context->local_context.capability.flags |=
    6171              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    6172              : 
    6173              : 
    6174            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6175              :                                                          m_libspdm_use_asym_algo, &data,
    6176              :                                                          &data_size, &hash, &hash_size)) {
    6177            0 :         assert(false);
    6178              :     }
    6179            1 :     libspdm_reset_message_a(spdm_context);
    6180            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6181              :         m_libspdm_use_hash_algo;
    6182            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6183              :         m_libspdm_use_asym_algo;
    6184            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6185              :         m_libspdm_use_dhe_algo;
    6186            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6187              :         m_libspdm_use_aead_algo;
    6188            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6189              :         m_libspdm_use_measurement_hash_algo;
    6190              : 
    6191              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6192              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6193              :         data_size;
    6194              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6195              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6196              :                      data, data_size);
    6197              : #else
    6198            1 :     libspdm_hash_all(
    6199              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6200              :         data, data_size,
    6201            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6202            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6203            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6204            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6205              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6206              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6207              :         data, data_size,
    6208              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6209              : #endif
    6210              : 
    6211            1 :     heartbeat_period = 0;
    6212            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6213            1 :     status = libspdm_send_receive_key_exchange(
    6214              :         spdm_context,
    6215              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6216              :         &session_id, &heartbeat_period, &slot_id_param,
    6217              :         measurement_hash);
    6218              :     /* Clear Mut_auth flags */
    6219            1 :     spdm_context->connection_info.capability.flags &=
    6220              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    6221            1 :     spdm_context->local_context.capability.flags &=
    6222              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    6223            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    6224            1 :     free(data);
    6225            1 : }
    6226              : 
    6227            1 : static void libspdm_test_requester_key_exchange_err_case29(void **state)
    6228              : {
    6229            1 : }
    6230              : 
    6231            1 : static void libspdm_test_requester_key_exchange_err_case30(void **state)
    6232              : {
    6233            1 : }
    6234              : 
    6235              : /**
    6236              :  * Test 31: Fail case, MutAuthRequested bit 0 set, and SlotID is 9 in KEY_EXCHANGE_RSP response
    6237              :  * message but SlotID should be between 0 and 7 inclusive.
    6238              :  * Expected Behavior: returns a status of INVALID_MSG_FIELD.
    6239              :  **/
    6240            1 : static void libspdm_test_requester_key_exchange_err_case31(void **state)
    6241              : {
    6242              :     libspdm_return_t status;
    6243              :     libspdm_test_context_t *spdm_test_context;
    6244              :     libspdm_context_t *spdm_context;
    6245              :     uint32_t session_id;
    6246              :     uint8_t heartbeat_period;
    6247              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6248              :     uint8_t slot_id_param;
    6249              :     void *data;
    6250              :     size_t data_size;
    6251              :     void *hash;
    6252              :     size_t hash_size;
    6253              : 
    6254            1 :     spdm_test_context = *state;
    6255            1 :     spdm_context = spdm_test_context->spdm_context;
    6256              : 
    6257            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6258            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6259              :     }
    6260              : 
    6261            1 :     spdm_test_context->case_id = 0x1F;
    6262            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6263              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6264            1 :     spdm_context->connection_info.connection_state =
    6265              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6266            1 :     spdm_context->connection_info.capability.flags |=
    6267              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6268            1 :     spdm_context->connection_info.capability.flags |=
    6269              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6270            1 :     spdm_context->local_context.capability.flags |=
    6271              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6272              : 
    6273            1 :     spdm_context->connection_info.capability.flags |=
    6274              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    6275            1 :     spdm_context->local_context.capability.flags |=
    6276              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    6277            1 :     spdm_context->local_context.capability.flags |=
    6278              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
    6279              : 
    6280            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6281              :                                                          m_libspdm_use_asym_algo, &data,
    6282              :                                                          &data_size, &hash, &hash_size)) {
    6283            0 :         assert(false);
    6284              :     }
    6285            1 :     libspdm_reset_message_a(spdm_context);
    6286            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6287              :         m_libspdm_use_hash_algo;
    6288            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6289              :         m_libspdm_use_asym_algo;
    6290            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6291              :         m_libspdm_use_dhe_algo;
    6292            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6293              :         m_libspdm_use_aead_algo;
    6294            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6295              :         m_libspdm_use_measurement_hash_algo;
    6296              : 
    6297              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6298              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6299              :         data_size;
    6300              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6301              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6302              :                      data, data_size);
    6303              : #else
    6304            1 :     libspdm_hash_all(
    6305              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6306              :         data, data_size,
    6307            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6308            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6309            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6310            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6311              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6312              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6313              :         data, data_size,
    6314              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6315              : #endif
    6316              : 
    6317            1 :     heartbeat_period = 0;
    6318            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6319            1 :     status = libspdm_send_receive_key_exchange(
    6320              :         spdm_context,
    6321              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6322              :         &session_id, &heartbeat_period, &slot_id_param,
    6323              :         measurement_hash);
    6324              :     /* Clear Mut_auth flags */
    6325            1 :     spdm_context->connection_info.capability.flags &=
    6326              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    6327            1 :     spdm_context->local_context.capability.flags &=
    6328              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    6329            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    6330            1 :     free(data);
    6331            1 : }
    6332              : 
    6333            1 : int libspdm_req_key_exchange_error_test(void)
    6334              : {
    6335            1 :     const struct CMUnitTest test_cases[] = {
    6336              :         /* SendRequest failed*/
    6337              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case1),
    6338              :         /* Successful response*/
    6339              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case2),
    6340              :         /* connection_state check failed*/
    6341              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case3),
    6342              :         /* Error response: SPDM_ERROR_CODE_INVALID_REQUEST*/
    6343              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case4),
    6344              :         /* Always SPDM_ERROR_CODE_BUSY*/
    6345              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case5),
    6346              :         /* SPDM_ERROR_CODE_BUSY + Successful response*/
    6347              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case6),
    6348              :         /* Error response: SPDM_ERROR_CODE_REQUEST_RESYNCH*/
    6349              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case7),
    6350              :         /* Always SPDM_ERROR_CODE_RESPONSE_NOT_READY*/
    6351              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case8),
    6352              :         /* SPDM_ERROR_CODE_RESPONSE_NOT_READY + Successful response*/
    6353              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case9),
    6354              :         /* Unexpected errors*/
    6355              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case10),
    6356              :         /* Buffer reset*/
    6357              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case11),
    6358              :         /* Measurement hash 1, returns a measurement hash*/
    6359              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case12),
    6360              :         /* Measurement hash 1, returns a 0x00 array (no TCB components)*/
    6361              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case13),
    6362              :         /* Measurement hash FF, returns a measurement_hash*/
    6363              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case14),
    6364              :         /* Measurement hash 1, returns no measurement_hash*/
    6365              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case15),
    6366              :         /* Measurement hash FF, returns no measurement_hash*/
    6367              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case16),
    6368              :         /* Measurement hash not requested, returns a measurement_hash*/
    6369              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case17),
    6370              :         /* Wrong signature*/
    6371              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case18),
    6372              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case19),
    6373              :         /* Heartbeat not supported, heartbeat period different from 0 sent*/
    6374              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case20),
    6375              :         /* Heartbeat supported, heartbeat period different from 0 sent*/
    6376              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case21),
    6377              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case22),
    6378              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case23),
    6379              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case24),
    6380              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case25),
    6381              :         /* Muth Auth requested with Encapsulated request and bit 0 set*/
    6382              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case26),
    6383              :         /* Muth Auth requested with implicit get digest and bit 0 set*/
    6384              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case27),
    6385              :         /* Muth Auth requested with Encapsulated request and Muth Auth requested with implicit get digest simultaneously*/
    6386              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case28),
    6387              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case29),
    6388              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case30),
    6389              :         /* Muth Auth requested bit 0 set, but Invalid SlotID in KEY_EXCHANGE_RSP response message*/
    6390              :         cmocka_unit_test(libspdm_test_requester_key_exchange_err_case31),
    6391              :     };
    6392              : 
    6393            1 :     libspdm_test_context_t test_context = {
    6394              :         LIBSPDM_TEST_CONTEXT_VERSION,
    6395              :         true,
    6396              :         send_message,
    6397              :         receive_message,
    6398              :     };
    6399              : 
    6400            1 :     libspdm_setup_test_context(&test_context);
    6401              : 
    6402            1 :     return cmocka_run_group_tests(test_cases,
    6403              :                                   libspdm_unit_test_group_setup,
    6404              :                                   libspdm_unit_test_group_teardown);
    6405              : }
    6406              : 
    6407              : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
        

Generated by: LCOV version 2.0-1