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

Generated by: LCOV version 2.0-1