LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester - key_exchange.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.2 % 3623 3521
Test Date: 2025-08-24 08:11:14 Functions: 100.0 % 38 38

            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           51 : 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           51 :     spdm_request = buffer;
      29           51 :     message_size = sizeof(spdm_message_header_t);
      30           51 :     if (buffer_size < message_size) {
      31            0 :         return buffer_size;
      32              :     }
      33              : 
      34           51 :     if (spdm_request->header.request_response_code != SPDM_KEY_EXCHANGE) {
      35            1 :         return buffer_size;
      36              :     }
      37              : 
      38           50 :     message_size = sizeof(spdm_key_exchange_request_t);
      39           50 :     if (buffer_size < message_size) {
      40            0 :         return buffer_size;
      41              :     }
      42              : 
      43           50 :     dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
      44           50 :     message_size += dhe_key_size + sizeof(uint16_t);
      45           50 :     if (buffer_size < message_size) {
      46            0 :         return buffer_size;
      47              :     }
      48              : 
      49           50 :     opaque_length =
      50           50 :         *(uint16_t *)((size_t)buffer +
      51           50 :                       sizeof(spdm_key_exchange_request_t) + dhe_key_size);
      52           50 :     message_size += opaque_length;
      53           50 :     if (buffer_size < message_size) {
      54            0 :         return buffer_size;
      55              :     }
      56              : 
      57              :     /* Good message, return actual size*/
      58           50 :     return message_size;
      59              : }
      60              : 
      61           53 : 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           53 :     spdm_test_context = libspdm_get_test_context();
      70           53 :     header_size = sizeof(libspdm_test_message_header_t);
      71           53 :     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            2 :     case 0x6:
     111            2 :         m_libspdm_local_buffer_size = 0;
     112            2 :         message_size = libspdm_test_get_key_exchange_request_size(
     113              :             spdm_context, (const uint8_t *)request + header_size,
     114              :             request_size - header_size);
     115            2 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     116              :                          (const uint8_t *)request + header_size, message_size);
     117            2 :         m_libspdm_local_buffer_size += message_size;
     118            2 :         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            2 :     case 0x9: {
     138              :         static size_t sub_index = 0;
     139            2 :         if (sub_index == 0) {
     140            1 :             m_libspdm_local_buffer_size = 0;
     141            1 :             message_size = libspdm_test_get_key_exchange_request_size(
     142              :                 spdm_context, (const uint8_t *)request + header_size,
     143              :                 request_size - header_size);
     144            1 :             libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     145              :                              (const uint8_t *)request + header_size, message_size);
     146            1 :             m_libspdm_local_buffer_size += message_size;
     147            1 :             sub_index++;
     148              :         }
     149              :     }
     150            2 :         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            1 :     case 0x13:
     233            1 :         m_libspdm_local_buffer_size = 0;
     234            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     235              :             spdm_context, (const uint8_t *)request + header_size,
     236              :             request_size - header_size);
     237            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     238              :                          (const uint8_t *)request + header_size, message_size);
     239            1 :         m_libspdm_local_buffer_size += message_size;
     240            1 :         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            1 :     case 0x18:
     278            1 :         m_libspdm_local_buffer_size = 0;
     279            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     280              :             spdm_context, (const uint8_t *)request + header_size,
     281              :             request_size - header_size);
     282            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     283              :                          (const uint8_t *)request + header_size, message_size);
     284            1 :         m_libspdm_local_buffer_size += message_size;
     285            1 :         return LIBSPDM_STATUS_SUCCESS;
     286            1 :     case 0x19:
     287            1 :         m_libspdm_local_buffer_size = 0;
     288            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     289              :             spdm_context, (const uint8_t *)request + header_size,
     290              :             request_size - header_size);
     291            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     292              :                          (const uint8_t *)request + header_size, message_size);
     293            1 :         m_libspdm_local_buffer_size += message_size;
     294            1 :         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            1 :     case 0x1D:
     323            1 :         m_libspdm_local_buffer_size = 0;
     324            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     325              :             spdm_context, (const uint8_t *)request + header_size,
     326              :             request_size - header_size);
     327            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     328              :                          (const uint8_t *)request + header_size,  message_size);
     329            1 :         m_libspdm_local_buffer_size += message_size;
     330            1 :         return LIBSPDM_STATUS_SUCCESS;
     331            1 :     case 0x1E:
     332            1 :         m_libspdm_local_buffer_size = 0;
     333            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     334              :             spdm_context, (const uint8_t *)request + header_size,
     335              :             request_size - header_size);
     336            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     337              :                          (const uint8_t *)request + header_size, message_size);
     338            1 :         m_libspdm_local_buffer_size += message_size;
     339            1 :         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            1 :     case 0x20:
     350            1 :         m_libspdm_local_buffer_size = 0;
     351            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     352              :             spdm_context, (const uint8_t *)request + header_size,
     353              :             request_size - header_size);
     354            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     355              :                          (const uint8_t *)request + header_size, message_size);
     356            1 :         m_libspdm_local_buffer_size += message_size;
     357            1 :         return LIBSPDM_STATUS_SUCCESS;
     358            1 :     case 0x21:
     359            1 :         m_libspdm_local_buffer_size = 0;
     360            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     361              :             spdm_context, (const uint8_t *)request + header_size,
     362              :             request_size - header_size);
     363            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     364              :                          (const uint8_t *)request + header_size, message_size);
     365            1 :         m_libspdm_local_buffer_size += message_size;
     366            1 :         return LIBSPDM_STATUS_SUCCESS;
     367            1 :     case 0x22:
     368            1 :         m_libspdm_local_buffer_size = 0;
     369            1 :         message_size = libspdm_test_get_key_exchange_request_size(
     370              :             spdm_context, (const uint8_t *)request + header_size,
     371              :             request_size - header_size);
     372            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
     373              :                          (const uint8_t *)request + header_size, message_size);
     374            1 :         m_libspdm_local_buffer_size += message_size;
     375            1 :         return LIBSPDM_STATUS_SUCCESS;
     376            0 :     default:
     377            0 :         return LIBSPDM_STATUS_SEND_FAIL;
     378              :     }
     379              : }
     380              : 
     381           52 : static libspdm_return_t libspdm_requester_key_exchange_test_receive_message(
     382              :     void *spdm_context, size_t *response_size,
     383              :     void **response, uint64_t timeout)
     384              : {
     385              :     libspdm_test_context_t *spdm_test_context;
     386              : 
     387           52 :     spdm_test_context = libspdm_get_test_context();
     388           52 :     switch (spdm_test_context->case_id) {
     389            0 :     case 0x1:
     390            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     391              : 
     392            1 :     case 0x2: {
     393              :         spdm_key_exchange_response_t *spdm_response;
     394              :         size_t dhe_key_size;
     395              :         uint32_t hash_size;
     396              :         size_t signature_size;
     397              :         uint32_t hmac_size;
     398              :         uint8_t *ptr;
     399              :         void *dhe_context;
     400              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
     401              :         size_t final_key_size;
     402              :         size_t opaque_key_exchange_rsp_size;
     403              :         void *data;
     404              :         size_t data_size;
     405              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     406              :         uint8_t *cert_buffer;
     407              :         size_t cert_buffer_size;
     408              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     409              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
     410              :         uint8_t bin_str0[128];
     411              :         size_t bin_str0_size;
     412              :         uint8_t bin_str2[128];
     413              :         size_t bin_str2_size;
     414              :         uint8_t bin_str7[128];
     415              :         size_t bin_str7_size;
     416              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     417              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     418              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     419              :         size_t spdm_response_size;
     420              :         size_t transport_header_size;
     421              : 
     422            1 :         ((libspdm_context_t *)spdm_context)->connection_info.secured_message_version =
     423              :             SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
     424              :         ((libspdm_context_t *)spdm_context)
     425            1 :         ->connection_info.algorithm.base_asym_algo =
     426              :             m_libspdm_use_asym_algo;
     427              :         ((libspdm_context_t *)spdm_context)
     428            1 :         ->connection_info.algorithm.base_hash_algo =
     429              :             m_libspdm_use_hash_algo;
     430              :         ((libspdm_context_t *)spdm_context)
     431            1 :         ->connection_info.algorithm.dhe_named_group =
     432              :             m_libspdm_use_dhe_algo;
     433              :         ((libspdm_context_t *)spdm_context)
     434            1 :         ->connection_info.algorithm.measurement_hash_algo =
     435              :             m_libspdm_use_measurement_hash_algo;
     436            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
     437            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     438            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     439            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
     440            1 :         opaque_key_exchange_rsp_size =
     441            1 :             libspdm_get_opaque_data_version_selection_data_size(
     442              :                 spdm_context);
     443            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
     444            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
     445            1 :                              opaque_key_exchange_rsp_size + signature_size +
     446              :                              hmac_size;
     447            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     448            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     449              : 
     450            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     451            1 :         spdm_response->header.request_response_code =
     452              :             SPDM_KEY_EXCHANGE_RSP;
     453            1 :         spdm_response->header.param1 = 0;
     454            1 :         spdm_response->rsp_session_id =
     455            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
     456            1 :         spdm_response->mut_auth_requested = 0;
     457            1 :         spdm_response->req_slot_id_param = 0;
     458            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
     459            1 :                                   spdm_response->random_data);
     460            1 :         ptr = (void *)(spdm_response + 1);
     461            1 :         dhe_context = libspdm_dhe_new(
     462            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     463              :                 m_libspdm_use_dhe_algo,
     464              :                 true);
     465            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
     466              :                                  &dhe_key_size);
     467            1 :         final_key_size = sizeof(final_key);
     468            1 :         libspdm_dhe_compute_key(
     469              :             m_libspdm_use_dhe_algo, dhe_context,
     470            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
     471              :             sizeof(spdm_key_exchange_request_t),
     472              :             dhe_key_size, final_key, &final_key_size);
     473            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
     474            1 :         ptr += dhe_key_size;
     475              :         /* libspdm_zero_mem (ptr, hash_size);
     476              :          * ptr += hash_size;*/
     477            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
     478            1 :         ptr += sizeof(uint16_t);
     479            1 :         libspdm_build_opaque_data_version_selection_data(
     480              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
     481            1 :         ptr += opaque_key_exchange_rsp_size;
     482            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     483              :                                                         m_libspdm_use_asym_algo, &data,
     484              :                                                         &data_size, NULL, NULL);
     485            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     486              :                          sizeof(m_libspdm_local_buffer)
     487            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     488              :                             m_libspdm_local_buffer),
     489            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
     490            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
     491            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
     492              :                        m_libspdm_local_buffer_size));
     493            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
     494            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     495            1 :         cert_buffer = (uint8_t *)data;
     496            1 :         cert_buffer_size = data_size;
     497            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     498              :                          cert_buffer_hash);
     499              :         /* transcript.message_a size is 0*/
     500            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     501            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     502              :                                       m_libspdm_local_buffer_size);
     503            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     504              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     505            1 :         free(data);
     506            1 :         libspdm_responder_data_sign(
     507              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
     508              :             spdm_context,
     509              : #endif
     510            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     511              :                 SPDM_KEY_EXCHANGE_RSP,
     512              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
     513            1 :                 false, libspdm_get_managed_buffer(&th_curr),
     514              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
     515              :                 &signature_size);
     516            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     517              :                          sizeof(m_libspdm_local_buffer)
     518            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     519              :                             m_libspdm_local_buffer),
     520              :                          ptr, signature_size);
     521            1 :         m_libspdm_local_buffer_size += signature_size;
     522            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
     523            1 :         ptr += signature_size;
     524            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     525              :                          libspdm_get_managed_buffer_size(&th_curr),
     526              :                          th_curr_hash_data);
     527            1 :         bin_str0_size = sizeof(bin_str0);
     528            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     529              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
     530            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
     531              :                            &bin_str0_size);
     532            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
     533              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
     534            1 :         bin_str2_size = sizeof(bin_str2);
     535            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     536              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
     537            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
     538              :                            bin_str2, &bin_str2_size);
     539            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
     540              :                             bin_str2, bin_str2_size,
     541              :                             response_handshake_secret, hash_size);
     542            1 :         bin_str7_size = sizeof(bin_str7);
     543            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     544              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
     545            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
     546              :                            &bin_str7_size);
     547            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
     548              :                             hash_size, bin_str7, bin_str7_size,
     549              :                             response_finished_key, hash_size);
     550            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     551              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     552            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     553              :                          response_finished_key, hash_size, ptr);
     554            1 :         ptr += hmac_size;
     555              : 
     556            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     557              :                                               false, spdm_response_size,
     558              :                                               spdm_response, response_size,
     559              :                                               response);
     560              :     }
     561            1 :         return LIBSPDM_STATUS_SUCCESS;
     562              : 
     563            0 :     case 0x3: {
     564              :         spdm_key_exchange_response_t *spdm_response;
     565              :         size_t dhe_key_size;
     566              :         uint32_t hash_size;
     567              :         size_t signature_size;
     568              :         uint32_t hmac_size;
     569              :         uint8_t *ptr;
     570              :         void *dhe_context;
     571              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
     572              :         size_t final_key_size;
     573              :         size_t opaque_key_exchange_rsp_size;
     574              :         void *data;
     575              :         size_t data_size;
     576              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     577              :         uint8_t *cert_buffer;
     578              :         size_t cert_buffer_size;
     579              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     580              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
     581              :         uint8_t bin_str0[128];
     582              :         size_t bin_str0_size;
     583              :         uint8_t bin_str2[128];
     584              :         size_t bin_str2_size;
     585              :         uint8_t bin_str7[128];
     586              :         size_t bin_str7_size;
     587              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     588              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     589              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     590              :         size_t spdm_response_size;
     591              :         size_t transport_header_size;
     592              : 
     593              :         ((libspdm_context_t *)spdm_context)
     594            0 :         ->connection_info.algorithm.base_asym_algo =
     595              :             m_libspdm_use_asym_algo;
     596              :         ((libspdm_context_t *)spdm_context)
     597            0 :         ->connection_info.algorithm.base_hash_algo =
     598              :             m_libspdm_use_hash_algo;
     599              :         ((libspdm_context_t *)spdm_context)
     600            0 :         ->connection_info.algorithm.dhe_named_group =
     601              :             m_libspdm_use_dhe_algo;
     602              :         ((libspdm_context_t *)spdm_context)
     603            0 :         ->connection_info.algorithm.measurement_hash_algo =
     604              :             m_libspdm_use_measurement_hash_algo;
     605            0 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
     606            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     607            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     608            0 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
     609            0 :         opaque_key_exchange_rsp_size =
     610            0 :             libspdm_get_opaque_data_version_selection_data_size(
     611              :                 spdm_context);
     612            0 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
     613            0 :                              dhe_key_size + 0 + sizeof(uint16_t) +
     614            0 :                              opaque_key_exchange_rsp_size + signature_size +
     615              :                              hmac_size;
     616            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     617            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     618              : 
     619            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     620            0 :         spdm_response->header.request_response_code =
     621              :             SPDM_KEY_EXCHANGE_RSP;
     622            0 :         spdm_response->header.param1 = 0;
     623            0 :         spdm_response->rsp_session_id =
     624            0 :             libspdm_allocate_rsp_session_id(spdm_context, false);
     625            0 :         spdm_response->mut_auth_requested = 0;
     626            0 :         spdm_response->req_slot_id_param = 0;
     627            0 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
     628            0 :                                   spdm_response->random_data);
     629            0 :         ptr = (void *)(spdm_response + 1);
     630            0 :         dhe_context = libspdm_dhe_new(
     631            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     632              :                 m_libspdm_use_dhe_algo,
     633              :                 true);
     634            0 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
     635              :                                  &dhe_key_size);
     636            0 :         final_key_size = sizeof(final_key);
     637            0 :         libspdm_dhe_compute_key(
     638              :             m_libspdm_use_dhe_algo, dhe_context,
     639            0 :             (uint8_t *)&m_libspdm_local_buffer[0] +
     640              :             sizeof(spdm_key_exchange_request_t),
     641              :             dhe_key_size, final_key, &final_key_size);
     642            0 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
     643            0 :         ptr += dhe_key_size;
     644              :         /* libspdm_zero_mem (ptr, hash_size);
     645              :          * ptr += hash_size;*/
     646            0 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
     647            0 :         ptr += sizeof(uint16_t);
     648            0 :         libspdm_build_opaque_data_version_selection_data(
     649              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
     650            0 :         ptr += opaque_key_exchange_rsp_size;
     651            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     652              :                                                         m_libspdm_use_asym_algo, &data,
     653              :                                                         &data_size, NULL, NULL);
     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            0 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
     659            0 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
     660            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
     661              :                        m_libspdm_local_buffer_size));
     662            0 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
     663            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     664            0 :         cert_buffer = (uint8_t *)data;
     665            0 :         cert_buffer_size = data_size;
     666            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     667              :                          cert_buffer_hash);
     668              :         /* transcript.message_a size is 0*/
     669            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     670            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     671              :                                       m_libspdm_local_buffer_size);
     672            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     673              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     674            0 :         free(data);
     675            0 :         libspdm_responder_data_sign(
     676              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
     677              :             spdm_context,
     678              : #endif
     679            0 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     680              :                 SPDM_KEY_EXCHANGE_RSP,
     681              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
     682            0 :                 false, libspdm_get_managed_buffer(&th_curr),
     683              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
     684              :                 &signature_size);
     685            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     686              :                          sizeof(m_libspdm_local_buffer)
     687            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     688              :                             m_libspdm_local_buffer),
     689              :                          ptr, signature_size);
     690            0 :         m_libspdm_local_buffer_size += signature_size;
     691            0 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
     692            0 :         ptr += signature_size;
     693            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     694              :                          libspdm_get_managed_buffer_size(&th_curr),
     695              :                          th_curr_hash_data);
     696            0 :         bin_str0_size = sizeof(bin_str0);
     697            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     698              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
     699            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
     700              :                            &bin_str0_size);
     701            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
     702              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
     703            0 :         bin_str2_size = sizeof(bin_str2);
     704            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     705              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
     706            0 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
     707              :                            bin_str2, &bin_str2_size);
     708            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
     709              :                             bin_str2, bin_str2_size,
     710              :                             response_handshake_secret, hash_size);
     711            0 :         bin_str7_size = sizeof(bin_str7);
     712            0 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     713              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
     714            0 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
     715              :                            &bin_str7_size);
     716            0 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
     717              :                             hash_size, bin_str7, bin_str7_size,
     718              :                             response_finished_key, hash_size);
     719            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     720              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     721            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     722              :                          response_finished_key, hash_size, ptr);
     723            0 :         ptr += hmac_size;
     724              : 
     725            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     726              :                                               false, spdm_response_size,
     727              :                                               spdm_response, response_size,
     728              :                                               response);
     729              :     }
     730            0 :         return LIBSPDM_STATUS_SUCCESS;
     731              : 
     732            1 :     case 0x4: {
     733              :         spdm_error_response_t *spdm_response;
     734              :         size_t spdm_response_size;
     735              :         size_t transport_header_size;
     736              : 
     737            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     738            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     739            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     740              : 
     741            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     742            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     743            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_INVALID_REQUEST;
     744            1 :         spdm_response->header.param2 = 0;
     745              : 
     746            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     747              :                                               false, spdm_response_size,
     748              :                                               spdm_response,
     749              :                                               response_size, response);
     750              :     }
     751            1 :         return LIBSPDM_STATUS_SUCCESS;
     752              : 
     753            1 :     case 0x5: {
     754              :         spdm_error_response_t *spdm_response;
     755              :         size_t spdm_response_size;
     756              :         size_t transport_header_size;
     757              : 
     758            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     759            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     760            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     761              : 
     762            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     763            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     764            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     765            1 :         spdm_response->header.param2 = 0;
     766              : 
     767            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     768              :                                               false, spdm_response_size,
     769              :                                               spdm_response,
     770              :                                               response_size, response);
     771              :     }
     772            1 :         return LIBSPDM_STATUS_SUCCESS;
     773              : 
     774            2 :     case 0x6: {
     775              :         static size_t sub_index1 = 0;
     776            2 :         if (sub_index1 == 0) {
     777              :             spdm_error_response_t *spdm_response;
     778              :             size_t spdm_response_size;
     779              :             size_t transport_header_size;
     780              : 
     781            1 :             spdm_response_size = sizeof(spdm_error_response_t);
     782            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     783            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     784              : 
     785            1 :             spdm_response->header.spdm_version =
     786              :                 SPDM_MESSAGE_VERSION_11;
     787            1 :             spdm_response->header.request_response_code = SPDM_ERROR;
     788            1 :             spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     789            1 :             spdm_response->header.param2 = 0;
     790              : 
     791            1 :             libspdm_transport_test_encode_message(
     792              :                 spdm_context, NULL, false, false,
     793              :                 spdm_response_size, spdm_response,
     794              :                 response_size, response);
     795            1 :             sub_index1++;
     796            1 :         } else if (sub_index1 == 1) {
     797              :             spdm_key_exchange_response_t *spdm_response;
     798              :             size_t dhe_key_size;
     799              :             uint32_t hash_size;
     800              :             size_t signature_size;
     801              :             uint32_t hmac_size;
     802              :             uint8_t *ptr;
     803              :             void *dhe_context;
     804              :             uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
     805              :             size_t final_key_size;
     806              :             size_t opaque_key_exchange_rsp_size;
     807              :             void *data;
     808              :             size_t data_size;
     809              :             uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     810              :             uint8_t *cert_buffer;
     811              :             size_t cert_buffer_size;
     812              :             uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     813              :             uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
     814              :             uint8_t bin_str0[128];
     815              :             size_t bin_str0_size;
     816              :             uint8_t bin_str2[128];
     817              :             size_t bin_str2_size;
     818              :             uint8_t bin_str7[128];
     819              :             size_t bin_str7_size;
     820              :             uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     821              :             uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
     822              :             uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     823              :             size_t spdm_response_size;
     824              :             size_t transport_header_size;
     825              : 
     826              :             ((libspdm_context_t *)spdm_context)
     827            1 :             ->connection_info.algorithm.base_asym_algo =
     828              :                 m_libspdm_use_asym_algo;
     829              :             ((libspdm_context_t *)spdm_context)
     830            1 :             ->connection_info.algorithm.base_hash_algo =
     831              :                 m_libspdm_use_hash_algo;
     832              :             ((libspdm_context_t *)spdm_context)
     833            1 :             ->connection_info.algorithm.dhe_named_group =
     834              :                 m_libspdm_use_dhe_algo;
     835              :             ((libspdm_context_t *)spdm_context)
     836              :             ->connection_info.algorithm
     837            1 :             .measurement_hash_algo =
     838              :                 m_libspdm_use_measurement_hash_algo;
     839            1 :             signature_size =
     840            1 :                 libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
     841            1 :             hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     842            1 :             hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     843            1 :             dhe_key_size =
     844            1 :                 libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
     845            1 :             opaque_key_exchange_rsp_size =
     846            1 :                 libspdm_get_opaque_data_version_selection_data_size(
     847              :                     spdm_context);
     848            1 :             spdm_response_size = sizeof(spdm_key_exchange_response_t) +
     849            1 :                                  dhe_key_size + 0 + sizeof(uint16_t) +
     850            1 :                                  opaque_key_exchange_rsp_size +
     851            1 :                                  signature_size + hmac_size;
     852            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     853            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     854              : 
     855            1 :             spdm_response->header.spdm_version =
     856              :                 SPDM_MESSAGE_VERSION_11;
     857            1 :             spdm_response->header.request_response_code =
     858              :                 SPDM_KEY_EXCHANGE_RSP;
     859            1 :             spdm_response->header.param1 = 0;
     860            1 :             spdm_response->rsp_session_id =
     861            1 :                 libspdm_allocate_rsp_session_id(spdm_context, false);
     862            1 :             spdm_response->mut_auth_requested = 0;
     863            1 :             spdm_response->req_slot_id_param = 0;
     864            1 :             libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
     865            1 :                                       spdm_response->random_data);
     866            1 :             ptr = (void *)(spdm_response + 1);
     867            1 :             dhe_context = libspdm_dhe_new(
     868            1 :                 spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     869              :                     m_libspdm_use_dhe_algo, true);
     870            1 :             libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
     871              :                                      &dhe_key_size);
     872            1 :             final_key_size = sizeof(final_key);
     873            1 :             libspdm_dhe_compute_key(
     874              :                 m_libspdm_use_dhe_algo, dhe_context,
     875            1 :                 (uint8_t *)&m_libspdm_local_buffer[0] +
     876              :                 sizeof(spdm_key_exchange_request_t),
     877              :                 dhe_key_size, final_key, &final_key_size);
     878            1 :             libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
     879            1 :             ptr += dhe_key_size;
     880              :             /* libspdm_zero_mem (ptr, hash_size);
     881              :              * ptr += hash_size;*/
     882            1 :             *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
     883            1 :             ptr += sizeof(uint16_t);
     884            1 :             libspdm_build_opaque_data_version_selection_data(
     885              :                 spdm_context, &opaque_key_exchange_rsp_size,
     886              :                 ptr);
     887            1 :             ptr += opaque_key_exchange_rsp_size;
     888            1 :             libspdm_read_responder_public_certificate_chain(
     889              :                 m_libspdm_use_hash_algo, m_libspdm_use_asym_algo, &data,
     890              :                 &data_size, NULL, NULL);
     891            1 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     892              :                              sizeof(m_libspdm_local_buffer)
     893            1 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     894              :                                 m_libspdm_local_buffer),
     895            1 :                              spdm_response, (size_t)ptr - (size_t)spdm_response);
     896            1 :             m_libspdm_local_buffer_size +=
     897            1 :                 ((size_t)ptr - (size_t)spdm_response);
     898            1 :             LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
     899              :                            m_libspdm_local_buffer_size));
     900            1 :             libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
     901            1 :             libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     902            1 :             cert_buffer = (uint8_t *)data;
     903            1 :             cert_buffer_size = data_size;
     904            1 :             libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer,
     905              :                              cert_buffer_size, cert_buffer_hash);
     906              :             /* transcript.message_a size is 0*/
     907            1 :             libspdm_append_managed_buffer(&th_curr, cert_buffer_hash,
     908              :                                           hash_size);
     909            1 :             libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     910              :                                           m_libspdm_local_buffer_size);
     911            2 :             libspdm_hash_all(m_libspdm_use_hash_algo,
     912            1 :                              libspdm_get_managed_buffer(&th_curr),
     913              :                              libspdm_get_managed_buffer_size(&th_curr),
     914              :                              hash_data);
     915            1 :             free(data);
     916            1 :             libspdm_responder_data_sign(
     917              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
     918              :                 spdm_context,
     919              : #endif
     920            1 :                 spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
     921              :                     SPDM_KEY_EXCHANGE_RSP,
     922              :                     m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
     923            1 :                     false, libspdm_get_managed_buffer(&th_curr),
     924              :                     libspdm_get_managed_buffer_size(&th_curr), ptr,
     925              :                     &signature_size);
     926            1 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     927              :                              sizeof(m_libspdm_local_buffer)
     928            1 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     929              :                                 m_libspdm_local_buffer),
     930              :                              ptr, signature_size);
     931            1 :             m_libspdm_local_buffer_size += signature_size;
     932            1 :             libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
     933            1 :             ptr += signature_size;
     934            2 :             libspdm_hash_all(m_libspdm_use_hash_algo,
     935            1 :                              libspdm_get_managed_buffer(&th_curr),
     936              :                              libspdm_get_managed_buffer_size(&th_curr),
     937              :                              th_curr_hash_data);
     938            1 :             bin_str0_size = sizeof(bin_str0);
     939            1 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     940              :                                SPDM_BIN_STR_0_LABEL,
     941              :                                sizeof(SPDM_BIN_STR_0_LABEL) - 1, NULL,
     942            1 :                                (uint16_t)hash_size, hash_size, bin_str0,
     943              :                                &bin_str0_size);
     944            1 :             libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
     945              :                              m_libspdm_zero_filled_buffer, hash_size, handshake_secret);
     946            1 :             bin_str2_size = sizeof(bin_str2);
     947            1 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     948              :                                SPDM_BIN_STR_2_LABEL,
     949              :                                sizeof(SPDM_BIN_STR_2_LABEL) - 1,
     950            1 :                                th_curr_hash_data, (uint16_t)hash_size,
     951              :                                hash_size, bin_str2, &bin_str2_size);
     952            1 :             libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret,
     953              :                                 hash_size, bin_str2, bin_str2_size,
     954              :                                 response_handshake_secret, hash_size);
     955            1 :             bin_str7_size = sizeof(bin_str7);
     956            1 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
     957              :                                SPDM_BIN_STR_7_LABEL,
     958              :                                sizeof(SPDM_BIN_STR_7_LABEL) - 1, NULL,
     959            1 :                                (uint16_t)hash_size, hash_size, bin_str7,
     960              :                                &bin_str7_size);
     961            1 :             libspdm_hkdf_expand(m_libspdm_use_hash_algo,
     962              :                                 response_handshake_secret, hash_size,
     963              :                                 bin_str7, bin_str7_size,
     964              :                                 response_finished_key, hash_size);
     965            1 :             libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     966              :                              libspdm_get_managed_buffer_size(&th_curr), hash_data);
     967            1 :             libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     968              :                              response_finished_key, hash_size, ptr);
     969            1 :             ptr += hmac_size;
     970              : 
     971            1 :             libspdm_transport_test_encode_message(
     972              :                 spdm_context, NULL, false, false, spdm_response_size,
     973              :                 spdm_response, response_size, response);
     974              :         }
     975              :     }
     976            2 :         return LIBSPDM_STATUS_SUCCESS;
     977              : 
     978            1 :     case 0x7: {
     979              :         spdm_error_response_t *spdm_response;
     980              :         size_t spdm_response_size;
     981              :         size_t transport_header_size;
     982              : 
     983            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     984            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     985            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     986              : 
     987            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
     988            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     989            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
     990            1 :         spdm_response->header.param2 = 0;
     991              : 
     992            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     993              :                                               false, spdm_response_size,
     994              :                                               spdm_response,
     995              :                                               response_size, response);
     996              :     }
     997            1 :         return LIBSPDM_STATUS_SUCCESS;
     998              : 
     999            2 :     case 0x8: {
    1000              :         spdm_error_response_data_response_not_ready_t *spdm_response;
    1001              :         size_t spdm_response_size;
    1002              :         size_t transport_header_size;
    1003              : 
    1004            2 :         spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
    1005            2 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1006            2 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1007              : 
    1008            2 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1009            2 :         spdm_response->header.request_response_code = SPDM_ERROR;
    1010            2 :         spdm_response->header.param1 =
    1011              :             SPDM_ERROR_CODE_RESPONSE_NOT_READY;
    1012            2 :         spdm_response->header.param2 = 0;
    1013            2 :         spdm_response->extend_error_data.rd_exponent = 1;
    1014            2 :         spdm_response->extend_error_data.rd_tm = 2;
    1015            2 :         spdm_response->extend_error_data.request_code =
    1016              :             SPDM_KEY_EXCHANGE;
    1017            2 :         spdm_response->extend_error_data.token = 0;
    1018              : 
    1019            2 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1020              :                                               false, spdm_response_size,
    1021              :                                               spdm_response,
    1022              :                                               response_size, response);
    1023              :     }
    1024            2 :         return LIBSPDM_STATUS_SUCCESS;
    1025              : 
    1026            2 :     case 0x9: {
    1027              :         static size_t sub_index2 = 0;
    1028            2 :         if (sub_index2 == 0) {
    1029              :             spdm_error_response_data_response_not_ready_t
    1030              :             *spdm_response;
    1031              :             size_t spdm_response_size;
    1032              :             size_t transport_header_size;
    1033              : 
    1034            1 :             spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
    1035            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1036            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1037              : 
    1038            1 :             spdm_response->header.spdm_version =
    1039              :                 SPDM_MESSAGE_VERSION_11;
    1040            1 :             spdm_response->header.request_response_code = SPDM_ERROR;
    1041            1 :             spdm_response->header.param1 =
    1042              :                 SPDM_ERROR_CODE_RESPONSE_NOT_READY;
    1043            1 :             spdm_response->header.param2 = 0;
    1044            1 :             spdm_response->extend_error_data.rd_exponent = 1;
    1045            1 :             spdm_response->extend_error_data.rd_tm = 2;
    1046            1 :             spdm_response->extend_error_data.request_code =
    1047              :                 SPDM_KEY_EXCHANGE;
    1048            1 :             spdm_response->extend_error_data.token = 1;
    1049              : 
    1050            1 :             libspdm_transport_test_encode_message(
    1051              :                 spdm_context, NULL, false, false,
    1052              :                 spdm_response_size, spdm_response,
    1053              :                 response_size, response);
    1054            1 :             sub_index2++;
    1055            1 :         } else if (sub_index2 == 1) {
    1056              :             spdm_key_exchange_response_t *spdm_response;
    1057              :             size_t dhe_key_size;
    1058              :             uint32_t hash_size;
    1059              :             size_t signature_size;
    1060              :             uint32_t hmac_size;
    1061              :             uint8_t *ptr;
    1062              :             void *dhe_context;
    1063              :             uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1064              :             size_t final_key_size;
    1065              :             size_t opaque_key_exchange_rsp_size;
    1066              :             void *data;
    1067              :             size_t data_size;
    1068              :             uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1069              :             uint8_t *cert_buffer;
    1070              :             size_t cert_buffer_size;
    1071              :             uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1072              :             uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1073              :             uint8_t bin_str0[128];
    1074              :             size_t bin_str0_size;
    1075              :             uint8_t bin_str2[128];
    1076              :             size_t bin_str2_size;
    1077              :             uint8_t bin_str7[128];
    1078              :             size_t bin_str7_size;
    1079              :             uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1080              :             uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1081              :             uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1082              :             size_t spdm_response_size;
    1083              :             size_t transport_header_size;
    1084              : 
    1085              :             ((libspdm_context_t *)spdm_context)
    1086            1 :             ->connection_info.algorithm.base_asym_algo =
    1087              :                 m_libspdm_use_asym_algo;
    1088              :             ((libspdm_context_t *)spdm_context)
    1089            1 :             ->connection_info.algorithm.base_hash_algo =
    1090              :                 m_libspdm_use_hash_algo;
    1091              :             ((libspdm_context_t *)spdm_context)
    1092            1 :             ->connection_info.algorithm.dhe_named_group =
    1093              :                 m_libspdm_use_dhe_algo;
    1094              :             ((libspdm_context_t *)spdm_context)
    1095              :             ->connection_info.algorithm
    1096            1 :             .measurement_hash_algo =
    1097              :                 m_libspdm_use_measurement_hash_algo;
    1098            1 :             signature_size =
    1099            1 :                 libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1100            1 :             hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1101            1 :             hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1102            1 :             dhe_key_size =
    1103            1 :                 libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1104            1 :             opaque_key_exchange_rsp_size =
    1105            1 :                 libspdm_get_opaque_data_version_selection_data_size(
    1106              :                     spdm_context);
    1107            1 :             spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1108            1 :                                  dhe_key_size + 0 + sizeof(uint16_t) +
    1109            1 :                                  opaque_key_exchange_rsp_size +
    1110            1 :                                  signature_size + hmac_size;
    1111            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1112            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1113              : 
    1114            1 :             spdm_response->header.spdm_version =
    1115              :                 SPDM_MESSAGE_VERSION_11;
    1116            1 :             spdm_response->header.request_response_code =
    1117              :                 SPDM_KEY_EXCHANGE_RSP;
    1118            1 :             spdm_response->header.param1 = 0;
    1119            1 :             spdm_response->rsp_session_id =
    1120            1 :                 libspdm_allocate_rsp_session_id(spdm_context, false);
    1121            1 :             spdm_response->mut_auth_requested = 0;
    1122            1 :             spdm_response->req_slot_id_param = 0;
    1123            1 :             libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1124            1 :                                       spdm_response->random_data);
    1125            1 :             ptr = (void *)(spdm_response + 1);
    1126            1 :             dhe_context = libspdm_dhe_new(
    1127            1 :                 spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1128              :                     m_libspdm_use_dhe_algo, true);
    1129            1 :             libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1130              :                                      &dhe_key_size);
    1131            1 :             final_key_size = sizeof(final_key);
    1132            1 :             libspdm_dhe_compute_key(
    1133              :                 m_libspdm_use_dhe_algo, dhe_context,
    1134            1 :                 (uint8_t *)&m_libspdm_local_buffer[0] +
    1135              :                 sizeof(spdm_key_exchange_request_t),
    1136              :                 dhe_key_size, final_key, &final_key_size);
    1137            1 :             libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1138            1 :             ptr += dhe_key_size;
    1139              :             /* libspdm_zero_mem (ptr, hash_size);
    1140              :              * ptr += hash_size;*/
    1141            1 :             *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1142            1 :             ptr += sizeof(uint16_t);
    1143            1 :             libspdm_build_opaque_data_version_selection_data(
    1144              :                 spdm_context, &opaque_key_exchange_rsp_size,
    1145              :                 ptr);
    1146            1 :             ptr += opaque_key_exchange_rsp_size;
    1147            1 :             libspdm_read_responder_public_certificate_chain(
    1148              :                 m_libspdm_use_hash_algo, m_libspdm_use_asym_algo, &data,
    1149              :                 &data_size, NULL, NULL);
    1150            1 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1151              :                              sizeof(m_libspdm_local_buffer)
    1152            1 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1153              :                                 m_libspdm_local_buffer),
    1154            1 :                              spdm_response, (size_t)ptr - (size_t)spdm_response);
    1155            1 :             m_libspdm_local_buffer_size +=
    1156            1 :                 ((size_t)ptr - (size_t)spdm_response);
    1157            1 :             LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1158              :                            m_libspdm_local_buffer_size));
    1159            1 :             libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1160            1 :             libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1161            1 :             cert_buffer = (uint8_t *)data;
    1162            1 :             cert_buffer_size = data_size;
    1163            1 :             libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer,
    1164              :                              cert_buffer_size, cert_buffer_hash);
    1165              :             /* transcript.message_a size is 0*/
    1166            1 :             libspdm_append_managed_buffer(&th_curr, cert_buffer_hash,
    1167              :                                           hash_size);
    1168            1 :             libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1169              :                                           m_libspdm_local_buffer_size);
    1170            2 :             libspdm_hash_all(m_libspdm_use_hash_algo,
    1171            1 :                              libspdm_get_managed_buffer(&th_curr),
    1172              :                              libspdm_get_managed_buffer_size(&th_curr),
    1173              :                              hash_data);
    1174            1 :             free(data);
    1175            1 :             libspdm_responder_data_sign(
    1176              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    1177              :                 spdm_context,
    1178              : #endif
    1179            1 :                 spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1180              :                     SPDM_KEY_EXCHANGE_RSP,
    1181              :                     m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1182            1 :                     false, libspdm_get_managed_buffer(&th_curr),
    1183              :                     libspdm_get_managed_buffer_size(&th_curr), ptr,
    1184              :                     &signature_size);
    1185            1 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1186              :                              sizeof(m_libspdm_local_buffer)
    1187            1 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1188              :                                 m_libspdm_local_buffer),
    1189              :                              ptr, signature_size);
    1190            1 :             m_libspdm_local_buffer_size += signature_size;
    1191            1 :             libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1192            1 :             ptr += signature_size;
    1193            2 :             libspdm_hash_all(m_libspdm_use_hash_algo,
    1194            1 :                              libspdm_get_managed_buffer(&th_curr),
    1195              :                              libspdm_get_managed_buffer_size(&th_curr),
    1196              :                              th_curr_hash_data);
    1197            1 :             bin_str0_size = sizeof(bin_str0);
    1198            1 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1199              :                                SPDM_BIN_STR_0_LABEL,
    1200              :                                sizeof(SPDM_BIN_STR_0_LABEL) - 1, NULL,
    1201            1 :                                (uint16_t)hash_size, hash_size, bin_str0,
    1202              :                                &bin_str0_size);
    1203            1 :             libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1204              :                              m_libspdm_zero_filled_buffer, hash_size, handshake_secret);
    1205            1 :             bin_str2_size = sizeof(bin_str2);
    1206            1 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1207              :                                SPDM_BIN_STR_2_LABEL,
    1208              :                                sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1209            1 :                                th_curr_hash_data, (uint16_t)hash_size,
    1210              :                                hash_size, bin_str2, &bin_str2_size);
    1211            1 :             libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret,
    1212              :                                 hash_size, bin_str2, bin_str2_size,
    1213              :                                 response_handshake_secret, hash_size);
    1214            1 :             bin_str7_size = sizeof(bin_str7);
    1215            1 :             libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1216              :                                SPDM_BIN_STR_7_LABEL,
    1217              :                                sizeof(SPDM_BIN_STR_7_LABEL) - 1, NULL,
    1218            1 :                                (uint16_t)hash_size, hash_size, bin_str7,
    1219              :                                &bin_str7_size);
    1220            1 :             libspdm_hkdf_expand(m_libspdm_use_hash_algo,
    1221              :                                 response_handshake_secret, hash_size,
    1222              :                                 bin_str7, bin_str7_size,
    1223              :                                 response_finished_key, hash_size);
    1224            1 :             libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1225              :                              libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1226            1 :             libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1227              :                              response_finished_key, hash_size, ptr);
    1228            1 :             ptr += hmac_size;
    1229              : 
    1230            1 :             libspdm_transport_test_encode_message(
    1231              :                 spdm_context, NULL, false, false, spdm_response_size,
    1232              :                 spdm_response, response_size, response);
    1233              :         }
    1234              :     }
    1235            2 :         return LIBSPDM_STATUS_SUCCESS;
    1236              : 
    1237           18 :     case 0xA:
    1238              :     {
    1239              :         static uint16_t error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
    1240              : 
    1241              :         spdm_error_response_t *spdm_response;
    1242              :         size_t spdm_response_size;
    1243              :         size_t transport_header_size;
    1244              : 
    1245           18 :         spdm_response_size = sizeof(spdm_error_response_t);
    1246           18 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1247           18 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1248              : 
    1249           18 :         if(error_code <= 0xff) {
    1250           18 :             libspdm_zero_mem (spdm_response, spdm_response_size);
    1251           18 :             spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1252           18 :             spdm_response->header.request_response_code = SPDM_ERROR;
    1253           18 :             spdm_response->header.param1 = (uint8_t) error_code;
    1254           18 :             spdm_response->header.param2 = 0;
    1255              : 
    1256           18 :             libspdm_transport_test_encode_message (spdm_context, NULL, false, false,
    1257              :                                                    spdm_response_size, spdm_response,
    1258              :                                                    response_size, response);
    1259              :         }
    1260              : 
    1261           18 :         error_code++;
    1262           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
    1263            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
    1264              :         }
    1265           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
    1266            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
    1267              :         }
    1268           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
    1269            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
    1270              :         }
    1271              :     }
    1272           18 :         return LIBSPDM_STATUS_SUCCESS;
    1273            1 :     case 0xB: {
    1274              :         spdm_key_exchange_response_t *spdm_response;
    1275              :         size_t dhe_key_size;
    1276              :         uint32_t hash_size;
    1277              :         size_t signature_size;
    1278              :         uint32_t hmac_size;
    1279              :         uint8_t *ptr;
    1280              :         void *dhe_context;
    1281              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1282              :         size_t final_key_size;
    1283              :         size_t opaque_key_exchange_rsp_size;
    1284              :         void *data;
    1285              :         size_t data_size;
    1286              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1287              :         uint8_t *cert_buffer;
    1288              :         size_t cert_buffer_size;
    1289              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1290              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1291              :         uint8_t bin_str0[128];
    1292              :         size_t bin_str0_size;
    1293              :         uint8_t bin_str2[128];
    1294              :         size_t bin_str2_size;
    1295              :         uint8_t bin_str7[128];
    1296              :         size_t bin_str7_size;
    1297              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1298              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1299              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1300              :         size_t spdm_response_size;
    1301              :         size_t transport_header_size;
    1302              : 
    1303              :         ((libspdm_context_t *)spdm_context)
    1304            1 :         ->connection_info.algorithm.base_asym_algo =
    1305              :             m_libspdm_use_asym_algo;
    1306              :         ((libspdm_context_t *)spdm_context)
    1307            1 :         ->connection_info.algorithm.base_hash_algo =
    1308              :             m_libspdm_use_hash_algo;
    1309              :         ((libspdm_context_t *)spdm_context)
    1310            1 :         ->connection_info.algorithm.dhe_named_group =
    1311              :             m_libspdm_use_dhe_algo;
    1312              :         ((libspdm_context_t *)spdm_context)
    1313            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1314              :             m_libspdm_use_measurement_hash_algo;
    1315            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1316            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1317            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1318            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1319            1 :         opaque_key_exchange_rsp_size =
    1320            1 :             libspdm_get_opaque_data_version_selection_data_size(
    1321              :                 spdm_context);
    1322            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1323            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    1324            1 :                              opaque_key_exchange_rsp_size + signature_size +
    1325              :                              hmac_size;
    1326            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1327            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1328              : 
    1329            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1330            1 :         spdm_response->header.request_response_code =
    1331              :             SPDM_KEY_EXCHANGE_RSP;
    1332            1 :         spdm_response->header.param1 = 0;
    1333            1 :         spdm_response->rsp_session_id =
    1334            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    1335            1 :         spdm_response->mut_auth_requested = 0;
    1336            1 :         spdm_response->req_slot_id_param = 0;
    1337            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1338            1 :                                   spdm_response->random_data);
    1339            1 :         ptr = (void *)(spdm_response + 1);
    1340            1 :         dhe_context = libspdm_dhe_new(
    1341            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1342              :                 m_libspdm_use_dhe_algo,
    1343              :                 true);
    1344            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1345              :                                  &dhe_key_size);
    1346            1 :         final_key_size = sizeof(final_key);
    1347            1 :         libspdm_dhe_compute_key(
    1348              :             m_libspdm_use_dhe_algo, dhe_context,
    1349            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    1350              :             sizeof(spdm_key_exchange_request_t),
    1351              :             dhe_key_size, final_key, &final_key_size);
    1352            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1353            1 :         ptr += dhe_key_size;
    1354              :         /* libspdm_zero_mem (ptr, hash_size);
    1355              :          * ptr += hash_size;*/
    1356            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1357            1 :         ptr += sizeof(uint16_t);
    1358            1 :         libspdm_build_opaque_data_version_selection_data(
    1359              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    1360            1 :         ptr += opaque_key_exchange_rsp_size;
    1361            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1362              :                                                         m_libspdm_use_asym_algo, &data,
    1363              :                                                         &data_size, NULL, NULL);
    1364            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1365              :                          sizeof(m_libspdm_local_buffer)
    1366            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1367              :                             m_libspdm_local_buffer),
    1368            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    1369            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    1370            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1371              :                        m_libspdm_local_buffer_size));
    1372            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1373            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1374            1 :         cert_buffer = (uint8_t *)data;
    1375            1 :         cert_buffer_size = data_size;
    1376            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1377              :                          cert_buffer_hash);
    1378              :         /* transcript.message_a size is 0*/
    1379            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1380            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1381              :                                       m_libspdm_local_buffer_size);
    1382            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1383              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1384            1 :         free(data);
    1385            1 :         libspdm_responder_data_sign(
    1386              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    1387              :             spdm_context,
    1388              : #endif
    1389            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1390              :                 SPDM_KEY_EXCHANGE_RSP,
    1391              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1392            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    1393              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    1394              :                 &signature_size);
    1395            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1396              :                          sizeof(m_libspdm_local_buffer)
    1397            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1398              :                             m_libspdm_local_buffer),
    1399              :                          ptr, signature_size);
    1400            1 :         m_libspdm_local_buffer_size += signature_size;
    1401            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1402            1 :         ptr += signature_size;
    1403            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1404              :                          libspdm_get_managed_buffer_size(&th_curr),
    1405              :                          th_curr_hash_data);
    1406            1 :         bin_str0_size = sizeof(bin_str0);
    1407            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1408              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    1409            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    1410              :                            &bin_str0_size);
    1411            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1412              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    1413            1 :         bin_str2_size = sizeof(bin_str2);
    1414            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1415              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1416            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    1417              :                            bin_str2, &bin_str2_size);
    1418            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    1419              :                             bin_str2, bin_str2_size,
    1420              :                             response_handshake_secret, hash_size);
    1421            1 :         bin_str7_size = sizeof(bin_str7);
    1422            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1423              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    1424            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    1425              :                            &bin_str7_size);
    1426            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    1427              :                             hash_size, bin_str7, bin_str7_size,
    1428              :                             response_finished_key, hash_size);
    1429            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1430              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1431            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1432              :                          response_finished_key, hash_size, ptr);
    1433            1 :         ptr += hmac_size;
    1434              : 
    1435            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1436              :                                               false, spdm_response_size,
    1437              :                                               spdm_response, response_size,
    1438              :                                               response);
    1439              :     }
    1440            1 :         return LIBSPDM_STATUS_SUCCESS;
    1441            1 :     case 0xC: {
    1442              :         spdm_key_exchange_response_t *spdm_response;
    1443              :         size_t dhe_key_size;
    1444              :         uint32_t hash_size;
    1445              :         uint32_t measurement_hash_size;
    1446              :         size_t signature_size;
    1447              :         uint32_t hmac_size;
    1448              :         uint8_t *ptr;
    1449              :         void *dhe_context;
    1450              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1451              :         size_t final_key_size;
    1452              :         size_t opaque_key_exchange_rsp_size;
    1453              :         void *data;
    1454              :         size_t data_size;
    1455              :         uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1456              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1457              :         uint8_t *cert_buffer;
    1458              :         size_t cert_buffer_size;
    1459              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1460              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1461              :         uint8_t bin_str0[128];
    1462              :         size_t bin_str0_size;
    1463              :         uint8_t bin_str2[128];
    1464              :         size_t bin_str2_size;
    1465              :         uint8_t bin_str7[128];
    1466              :         size_t bin_str7_size;
    1467              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1468              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1469              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1470              :         size_t spdm_response_size;
    1471              :         size_t transport_header_size;
    1472              : 
    1473              :         ((libspdm_context_t *)spdm_context)
    1474            1 :         ->connection_info.algorithm.base_asym_algo =
    1475              :             m_libspdm_use_asym_algo;
    1476              :         ((libspdm_context_t *)spdm_context)
    1477            1 :         ->connection_info.algorithm.base_hash_algo =
    1478              :             m_libspdm_use_hash_algo;
    1479              :         ((libspdm_context_t *)spdm_context)
    1480            1 :         ->connection_info.algorithm.dhe_named_group =
    1481              :             m_libspdm_use_dhe_algo;
    1482              :         ((libspdm_context_t *)spdm_context)
    1483            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1484              :             m_libspdm_use_measurement_hash_algo;
    1485            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1486            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1487            1 :         measurement_hash_size = libspdm_get_hash_size(
    1488              :             m_libspdm_use_hash_algo);
    1489            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1490            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1491            1 :         opaque_key_exchange_rsp_size =
    1492            1 :             libspdm_get_opaque_data_version_selection_data_size(
    1493              :                 spdm_context);
    1494            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1495            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    1496            1 :                              opaque_key_exchange_rsp_size + signature_size +
    1497              :                              hmac_size;
    1498            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1499            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1500            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    1501              : 
    1502            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1503            1 :         spdm_response->header.request_response_code =
    1504              :             SPDM_KEY_EXCHANGE_RSP;
    1505            1 :         spdm_response->header.param1 = 0;
    1506            1 :         spdm_response->rsp_session_id =
    1507            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    1508            1 :         spdm_response->mut_auth_requested = 0;
    1509            1 :         spdm_response->req_slot_id_param = 0;
    1510            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1511            1 :                                   spdm_response->random_data);
    1512            1 :         ptr = (void *)(spdm_response + 1);
    1513            1 :         dhe_context = libspdm_dhe_new(
    1514            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1515              :                 m_libspdm_use_dhe_algo,
    1516              :                 true);
    1517            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1518              :                                  &dhe_key_size);
    1519            1 :         final_key_size = sizeof(final_key);
    1520            1 :         libspdm_dhe_compute_key(
    1521              :             m_libspdm_use_dhe_algo, dhe_context,
    1522            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    1523              :             sizeof(spdm_key_exchange_request_t),
    1524              :             dhe_key_size, final_key, &final_key_size);
    1525            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1526            1 :         ptr += dhe_key_size;
    1527              :         /*Mock measurement hash as TCB*/
    1528            1 :         libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
    1529              :                          m_libspdm_use_tcb_hash_value, measurement_hash_size);
    1530            1 :         libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
    1531              :                          measurement_hash_data, measurement_hash_size);
    1532              :         /*libspdm_zero_mem (ptr, measurement_hash_size);*/
    1533            1 :         ptr += measurement_hash_size;
    1534            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1535            1 :         ptr += sizeof(uint16_t);
    1536            1 :         libspdm_build_opaque_data_version_selection_data(
    1537              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    1538            1 :         ptr += opaque_key_exchange_rsp_size;
    1539            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1540              :                                                         m_libspdm_use_asym_algo, &data,
    1541              :                                                         &data_size, NULL, NULL);
    1542            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1543              :                          sizeof(m_libspdm_local_buffer)
    1544            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1545              :                             m_libspdm_local_buffer),
    1546            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    1547            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    1548            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1549              :                        m_libspdm_local_buffer_size));
    1550            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1551            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1552            1 :         cert_buffer =  (uint8_t *)data;
    1553            1 :         cert_buffer_size = data_size;
    1554            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1555              :                          cert_buffer_hash);
    1556              :         /* transcript.message_a size is 0*/
    1557            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1558            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1559              :                                       m_libspdm_local_buffer_size);
    1560            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1561              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1562            1 :         free(data);
    1563            1 :         libspdm_responder_data_sign(
    1564              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    1565              :             spdm_context,
    1566              : #endif
    1567            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1568              :                 SPDM_KEY_EXCHANGE_RSP,
    1569              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1570            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    1571              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    1572              :                 &signature_size);
    1573            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1574              :                          sizeof(m_libspdm_local_buffer)
    1575            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1576              :                             m_libspdm_local_buffer),
    1577              :                          ptr, signature_size);
    1578            1 :         m_libspdm_local_buffer_size += signature_size;
    1579            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1580            1 :         ptr += signature_size;
    1581            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1582              :                          libspdm_get_managed_buffer_size(&th_curr),
    1583              :                          th_curr_hash_data);
    1584            1 :         bin_str0_size = sizeof(bin_str0);
    1585            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1586              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    1587            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    1588              :                            &bin_str0_size);
    1589            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1590              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    1591            1 :         bin_str2_size = sizeof(bin_str2);
    1592            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1593              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1594            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    1595              :                            bin_str2, &bin_str2_size);
    1596            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    1597              :                             bin_str2, bin_str2_size,
    1598              :                             response_handshake_secret, hash_size);
    1599            1 :         bin_str7_size = sizeof(bin_str7);
    1600            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1601              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    1602            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    1603              :                            &bin_str7_size);
    1604            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    1605              :                             hash_size, bin_str7, bin_str7_size,
    1606              :                             response_finished_key, hash_size);
    1607            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1608              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1609            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1610              :                          response_finished_key, hash_size, ptr);
    1611            1 :         ptr += hmac_size;
    1612              : 
    1613            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1614              :                                               false, spdm_response_size,
    1615              :                                               spdm_response, response_size,
    1616              :                                               response);
    1617              :     }
    1618            1 :         return LIBSPDM_STATUS_SUCCESS;
    1619              : 
    1620            1 :     case 0xD: {
    1621              :         spdm_key_exchange_response_t *spdm_response;
    1622              :         size_t dhe_key_size;
    1623              :         uint32_t hash_size;
    1624              :         uint32_t measurement_hash_size;
    1625              :         size_t signature_size;
    1626              :         uint32_t hmac_size;
    1627              :         uint8_t *ptr;
    1628              :         void *dhe_context;
    1629              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1630              :         size_t final_key_size;
    1631              :         size_t opaque_key_exchange_rsp_size;
    1632              :         void *data;
    1633              :         size_t data_size;
    1634              :         uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1635              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1636              :         uint8_t *cert_buffer;
    1637              :         size_t cert_buffer_size;
    1638              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1639              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1640              :         uint8_t bin_str0[128];
    1641              :         size_t bin_str0_size;
    1642              :         uint8_t bin_str2[128];
    1643              :         size_t bin_str2_size;
    1644              :         uint8_t bin_str7[128];
    1645              :         size_t bin_str7_size;
    1646              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1647              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1648              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1649              :         size_t spdm_response_size;
    1650              :         size_t transport_header_size;
    1651              : 
    1652              :         ((libspdm_context_t *)spdm_context)
    1653            1 :         ->connection_info.algorithm.base_asym_algo =
    1654              :             m_libspdm_use_asym_algo;
    1655              :         ((libspdm_context_t *)spdm_context)
    1656            1 :         ->connection_info.algorithm.base_hash_algo =
    1657              :             m_libspdm_use_hash_algo;
    1658              :         ((libspdm_context_t *)spdm_context)
    1659            1 :         ->connection_info.algorithm.dhe_named_group =
    1660              :             m_libspdm_use_dhe_algo;
    1661              :         ((libspdm_context_t *)spdm_context)
    1662            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1663              :             m_libspdm_use_measurement_hash_algo;
    1664            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1665            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1666            1 :         measurement_hash_size = libspdm_get_hash_size(
    1667              :             m_libspdm_use_hash_algo);
    1668            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1669            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1670            1 :         opaque_key_exchange_rsp_size =
    1671            1 :             libspdm_get_opaque_data_version_selection_data_size(
    1672              :                 spdm_context);
    1673            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1674            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    1675            1 :                              opaque_key_exchange_rsp_size + signature_size +
    1676              :                              hmac_size;
    1677            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1678            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1679            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    1680              : 
    1681            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1682            1 :         spdm_response->header.request_response_code =
    1683              :             SPDM_KEY_EXCHANGE_RSP;
    1684            1 :         spdm_response->header.param1 = 0;
    1685            1 :         spdm_response->rsp_session_id =
    1686            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    1687            1 :         spdm_response->mut_auth_requested = 0;
    1688            1 :         spdm_response->req_slot_id_param = 0;
    1689            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1690            1 :                                   spdm_response->random_data);
    1691            1 :         ptr = (void *)(spdm_response + 1);
    1692            1 :         dhe_context = libspdm_dhe_new(
    1693            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1694              :                 m_libspdm_use_dhe_algo,
    1695              :                 true);
    1696            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1697              :                                  &dhe_key_size);
    1698            1 :         final_key_size = sizeof(final_key);
    1699            1 :         libspdm_dhe_compute_key(
    1700              :             m_libspdm_use_dhe_algo, dhe_context,
    1701            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    1702              :             sizeof(spdm_key_exchange_request_t),
    1703              :             dhe_key_size, final_key, &final_key_size);
    1704            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1705            1 :         ptr += dhe_key_size;
    1706              :         /*Mock measurement hash as 0x00 array*/
    1707            1 :         libspdm_zero_mem(measurement_hash_data, measurement_hash_size);
    1708            1 :         libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
    1709              :                          measurement_hash_data, measurement_hash_size);
    1710              :         /*libspdm_zero_mem (ptr, measurement_hash_size);*/
    1711            1 :         ptr += measurement_hash_size;
    1712            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1713            1 :         ptr += sizeof(uint16_t);
    1714            1 :         libspdm_build_opaque_data_version_selection_data(
    1715              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    1716            1 :         ptr += opaque_key_exchange_rsp_size;
    1717            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1718              :                                                         m_libspdm_use_asym_algo, &data,
    1719              :                                                         &data_size, NULL, NULL);
    1720            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1721              :                          sizeof(m_libspdm_local_buffer)
    1722            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1723              :                             m_libspdm_local_buffer),
    1724            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    1725            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    1726            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1727              :                        m_libspdm_local_buffer_size));
    1728            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1729            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1730            1 :         cert_buffer = (uint8_t *)data;
    1731            1 :         cert_buffer_size =  data_size;
    1732            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1733              :                          cert_buffer_hash);
    1734              :         /* transcript.message_a size is 0*/
    1735            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1736            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1737              :                                       m_libspdm_local_buffer_size);
    1738            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1739              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1740            1 :         free(data);
    1741            1 :         libspdm_responder_data_sign(
    1742              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    1743              :             spdm_context,
    1744              : #endif
    1745            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1746              :                 SPDM_KEY_EXCHANGE_RSP,
    1747              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1748            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    1749              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    1750              :                 &signature_size);
    1751            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1752              :                          sizeof(m_libspdm_local_buffer)
    1753            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1754              :                             m_libspdm_local_buffer),
    1755              :                          ptr, signature_size);
    1756            1 :         m_libspdm_local_buffer_size += signature_size;
    1757            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1758            1 :         ptr += signature_size;
    1759            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1760              :                          libspdm_get_managed_buffer_size(&th_curr),
    1761              :                          th_curr_hash_data);
    1762            1 :         bin_str0_size = sizeof(bin_str0);
    1763            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1764              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    1765            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    1766              :                            &bin_str0_size);
    1767            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1768              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    1769            1 :         bin_str2_size = sizeof(bin_str2);
    1770            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1771              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1772            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    1773              :                            bin_str2, &bin_str2_size);
    1774            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    1775              :                             bin_str2, bin_str2_size,
    1776              :                             response_handshake_secret, hash_size);
    1777            1 :         bin_str7_size = sizeof(bin_str7);
    1778            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1779              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    1780            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    1781              :                            &bin_str7_size);
    1782            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    1783              :                             hash_size, bin_str7, bin_str7_size,
    1784              :                             response_finished_key, hash_size);
    1785            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1786              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1787            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1788              :                          response_finished_key, hash_size, ptr);
    1789            1 :         ptr += hmac_size;
    1790              : 
    1791            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1792              :                                               false, spdm_response_size,
    1793              :                                               spdm_response, response_size,
    1794              :                                               response);
    1795              :     }
    1796            1 :         return LIBSPDM_STATUS_SUCCESS;
    1797              : 
    1798            1 :     case 0xE: {
    1799              :         spdm_key_exchange_response_t *spdm_response;
    1800              :         size_t dhe_key_size;
    1801              :         uint32_t hash_size;
    1802              :         uint32_t measurement_hash_size;
    1803              :         size_t signature_size;
    1804              :         uint32_t hmac_size;
    1805              :         uint8_t *ptr;
    1806              :         void *dhe_context;
    1807              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1808              :         size_t final_key_size;
    1809              :         size_t opaque_key_exchange_rsp_size;
    1810              :         void *data;
    1811              :         size_t data_size;
    1812              :         uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1813              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1814              :         uint8_t *cert_buffer;
    1815              :         size_t cert_buffer_size;
    1816              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1817              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1818              :         uint8_t bin_str0[128];
    1819              :         size_t bin_str0_size;
    1820              :         uint8_t bin_str2[128];
    1821              :         size_t bin_str2_size;
    1822              :         uint8_t bin_str7[128];
    1823              :         size_t bin_str7_size;
    1824              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1825              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    1826              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1827              :         size_t spdm_response_size;
    1828              :         size_t transport_header_size;
    1829              : 
    1830              :         ((libspdm_context_t *)spdm_context)
    1831            1 :         ->connection_info.algorithm.base_asym_algo =
    1832              :             m_libspdm_use_asym_algo;
    1833              :         ((libspdm_context_t *)spdm_context)
    1834            1 :         ->connection_info.algorithm.base_hash_algo =
    1835              :             m_libspdm_use_hash_algo;
    1836              :         ((libspdm_context_t *)spdm_context)
    1837            1 :         ->connection_info.algorithm.dhe_named_group =
    1838              :             m_libspdm_use_dhe_algo;
    1839              :         ((libspdm_context_t *)spdm_context)
    1840            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1841              :             m_libspdm_use_measurement_hash_algo;
    1842            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    1843            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1844            1 :         measurement_hash_size = libspdm_get_hash_size(
    1845              :             m_libspdm_use_hash_algo);
    1846            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1847            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    1848            1 :         opaque_key_exchange_rsp_size =
    1849            1 :             libspdm_get_opaque_data_version_selection_data_size(
    1850              :                 spdm_context);
    1851            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    1852            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    1853            1 :                              opaque_key_exchange_rsp_size + signature_size +
    1854              :                              hmac_size;
    1855            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1856            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1857            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    1858              : 
    1859            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1860            1 :         spdm_response->header.request_response_code =
    1861              :             SPDM_KEY_EXCHANGE_RSP;
    1862            1 :         spdm_response->header.param1 = 0;
    1863            1 :         spdm_response->rsp_session_id =
    1864            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    1865            1 :         spdm_response->mut_auth_requested = 0;
    1866            1 :         spdm_response->req_slot_id_param = 0;
    1867            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    1868            1 :                                   spdm_response->random_data);
    1869            1 :         ptr = (void *)(spdm_response + 1);
    1870            1 :         dhe_context = libspdm_dhe_new(
    1871            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1872              :                 m_libspdm_use_dhe_algo,
    1873              :                 true);
    1874            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    1875              :                                  &dhe_key_size);
    1876            1 :         final_key_size = sizeof(final_key);
    1877            1 :         libspdm_dhe_compute_key(
    1878              :             m_libspdm_use_dhe_algo, dhe_context,
    1879            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    1880              :             sizeof(spdm_key_exchange_request_t),
    1881              :             dhe_key_size, final_key, &final_key_size);
    1882            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    1883            1 :         ptr += dhe_key_size;
    1884              :         /*Mock measurement hash*/
    1885            1 :         libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
    1886              :                          m_libspdm_use_tcb_hash_value, measurement_hash_size);
    1887            1 :         libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
    1888              :                          measurement_hash_data, measurement_hash_size);
    1889              :         /*libspdm_zero_mem (ptr, measurement_hash_size);*/
    1890            1 :         ptr += measurement_hash_size;
    1891            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    1892            1 :         ptr += sizeof(uint16_t);
    1893            1 :         libspdm_build_opaque_data_version_selection_data(
    1894              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    1895            1 :         ptr += opaque_key_exchange_rsp_size;
    1896            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1897              :                                                         m_libspdm_use_asym_algo, &data,
    1898              :                                                         &data_size, NULL, NULL);
    1899            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1900              :                          sizeof(m_libspdm_local_buffer)
    1901            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1902              :                             m_libspdm_local_buffer),
    1903            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    1904            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    1905            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    1906              :                        m_libspdm_local_buffer_size));
    1907            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    1908            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1909            1 :         cert_buffer =  (uint8_t *)data;
    1910            1 :         cert_buffer_size = data_size;
    1911            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1912              :                          cert_buffer_hash);
    1913              :         /* transcript.message_a size is 0*/
    1914            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1915            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1916              :                                       m_libspdm_local_buffer_size);
    1917            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1918              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1919            1 :         free(data);
    1920            1 :         libspdm_responder_data_sign(
    1921              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    1922              :             spdm_context,
    1923              : #endif
    1924            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    1925              :                 SPDM_KEY_EXCHANGE_RSP,
    1926              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    1927            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    1928              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    1929              :                 &signature_size);
    1930            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1931              :                          sizeof(m_libspdm_local_buffer)
    1932            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1933              :                             m_libspdm_local_buffer),
    1934              :                          ptr, signature_size);
    1935            1 :         m_libspdm_local_buffer_size += signature_size;
    1936            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    1937            1 :         ptr += signature_size;
    1938            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1939              :                          libspdm_get_managed_buffer_size(&th_curr),
    1940              :                          th_curr_hash_data);
    1941            1 :         bin_str0_size = sizeof(bin_str0);
    1942            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1943              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    1944            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    1945              :                            &bin_str0_size);
    1946            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    1947              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    1948            1 :         bin_str2_size = sizeof(bin_str2);
    1949            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1950              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    1951            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    1952              :                            bin_str2, &bin_str2_size);
    1953            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    1954              :                             bin_str2, bin_str2_size,
    1955              :                             response_handshake_secret, hash_size);
    1956            1 :         bin_str7_size = sizeof(bin_str7);
    1957            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    1958              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    1959            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    1960              :                            &bin_str7_size);
    1961            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    1962              :                             hash_size, bin_str7, bin_str7_size,
    1963              :                             response_finished_key, hash_size);
    1964            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1965              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1966            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1967              :                          response_finished_key, hash_size, ptr);
    1968            1 :         ptr += hmac_size;
    1969              : 
    1970            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1971              :                                               false, spdm_response_size,
    1972              :                                               spdm_response, response_size,
    1973              :                                               response);
    1974              :     }
    1975            1 :         return LIBSPDM_STATUS_SUCCESS;
    1976              : 
    1977            1 :     case 0xF: {
    1978              :         spdm_key_exchange_response_t *spdm_response;
    1979              :         size_t dhe_key_size;
    1980              :         uint32_t hash_size;
    1981              :         size_t signature_size;
    1982              :         uint32_t hmac_size;
    1983              :         uint8_t *ptr;
    1984              :         void *dhe_context;
    1985              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    1986              :         size_t final_key_size;
    1987              :         size_t opaque_key_exchange_rsp_size;
    1988              :         void *data;
    1989              :         size_t data_size;
    1990              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1991              :         uint8_t *cert_buffer;
    1992              :         size_t cert_buffer_size;
    1993              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1994              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    1995              :         uint8_t bin_str0[128];
    1996              :         size_t bin_str0_size;
    1997              :         uint8_t bin_str2[128];
    1998              :         size_t bin_str2_size;
    1999              :         uint8_t bin_str7[128];
    2000              :         size_t bin_str7_size;
    2001              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2002              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2003              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2004              :         size_t spdm_response_size;
    2005              :         size_t transport_header_size;
    2006              : 
    2007              :         ((libspdm_context_t *)spdm_context)
    2008            1 :         ->connection_info.algorithm.base_asym_algo =
    2009              :             m_libspdm_use_asym_algo;
    2010              :         ((libspdm_context_t *)spdm_context)
    2011            1 :         ->connection_info.algorithm.base_hash_algo =
    2012              :             m_libspdm_use_hash_algo;
    2013              :         ((libspdm_context_t *)spdm_context)
    2014            1 :         ->connection_info.algorithm.dhe_named_group =
    2015              :             m_libspdm_use_dhe_algo;
    2016              :         ((libspdm_context_t *)spdm_context)
    2017            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2018              :             m_libspdm_use_measurement_hash_algo;
    2019            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2020            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2021            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2022            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2023            1 :         opaque_key_exchange_rsp_size =
    2024            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2025              :                 spdm_context);
    2026            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2027            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    2028            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2029              :                              hmac_size;
    2030            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2031            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2032            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2033              : 
    2034            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2035            1 :         spdm_response->header.request_response_code =
    2036              :             SPDM_KEY_EXCHANGE_RSP;
    2037            1 :         spdm_response->header.param1 = 0;
    2038            1 :         spdm_response->rsp_session_id =
    2039            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2040            1 :         spdm_response->mut_auth_requested = 0;
    2041            1 :         spdm_response->req_slot_id_param = 0;
    2042            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2043            1 :                                   spdm_response->random_data);
    2044            1 :         ptr = (void *)(spdm_response + 1);
    2045            1 :         dhe_context = libspdm_dhe_new(
    2046            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2047              :                 m_libspdm_use_dhe_algo,
    2048              :                 true);
    2049            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2050              :                                  &dhe_key_size);
    2051            1 :         final_key_size = sizeof(final_key);
    2052            1 :         libspdm_dhe_compute_key(
    2053              :             m_libspdm_use_dhe_algo, dhe_context,
    2054            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2055              :             sizeof(spdm_key_exchange_request_t),
    2056              :             dhe_key_size, final_key, &final_key_size);
    2057            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2058            1 :         ptr += dhe_key_size;
    2059              :         /* libspdm_zero_mem (ptr, hash_size);
    2060              :          * ptr += hash_size;*/
    2061            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2062            1 :         ptr += sizeof(uint16_t);
    2063            1 :         libspdm_build_opaque_data_version_selection_data(
    2064              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    2065            1 :         ptr += opaque_key_exchange_rsp_size;
    2066            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2067              :                                                         m_libspdm_use_asym_algo, &data,
    2068              :                                                         &data_size, NULL, NULL);
    2069            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2070              :                          sizeof(m_libspdm_local_buffer)
    2071            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2072              :                             m_libspdm_local_buffer),
    2073            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2074            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2075            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2076              :                        m_libspdm_local_buffer_size));
    2077            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2078            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2079            1 :         cert_buffer =  (uint8_t *)data;
    2080            1 :         cert_buffer_size = data_size;
    2081            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2082              :                          cert_buffer_hash);
    2083              :         /* transcript.message_a size is 0*/
    2084            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2085            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2086              :                                       m_libspdm_local_buffer_size);
    2087            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2088              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2089            1 :         free(data);
    2090            1 :         libspdm_responder_data_sign(
    2091              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    2092              :             spdm_context,
    2093              : #endif
    2094            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2095              :                 SPDM_KEY_EXCHANGE_RSP,
    2096              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2097            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2098              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2099              :                 &signature_size);
    2100            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2101              :                          sizeof(m_libspdm_local_buffer)
    2102            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2103              :                             m_libspdm_local_buffer),
    2104              :                          ptr, signature_size);
    2105            1 :         m_libspdm_local_buffer_size += signature_size;
    2106            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2107            1 :         ptr += signature_size;
    2108            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2109              :                          libspdm_get_managed_buffer_size(&th_curr),
    2110              :                          th_curr_hash_data);
    2111            1 :         bin_str0_size = sizeof(bin_str0);
    2112            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2113              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2114            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2115              :                            &bin_str0_size);
    2116            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2117              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2118            1 :         bin_str2_size = sizeof(bin_str2);
    2119            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2120              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2121            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2122              :                            bin_str2, &bin_str2_size);
    2123            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2124              :                             bin_str2, bin_str2_size,
    2125              :                             response_handshake_secret, hash_size);
    2126            1 :         bin_str7_size = sizeof(bin_str7);
    2127            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2128              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2129            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2130              :                            &bin_str7_size);
    2131            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2132              :                             hash_size, bin_str7, bin_str7_size,
    2133              :                             response_finished_key, hash_size);
    2134            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2135              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2136            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2137              :                          response_finished_key, hash_size, ptr);
    2138            1 :         ptr += hmac_size;
    2139              : 
    2140            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2141              :                                               false, spdm_response_size,
    2142              :                                               spdm_response, response_size,
    2143              :                                               response);
    2144              :     }
    2145            1 :         return LIBSPDM_STATUS_SUCCESS;
    2146              : 
    2147            1 :     case 0x10: {
    2148              :         spdm_key_exchange_response_t *spdm_response;
    2149              :         size_t dhe_key_size;
    2150              :         uint32_t hash_size;
    2151              :         size_t signature_size;
    2152              :         uint32_t hmac_size;
    2153              :         uint8_t *ptr;
    2154              :         void *dhe_context;
    2155              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2156              :         size_t final_key_size;
    2157              :         size_t opaque_key_exchange_rsp_size;
    2158              :         void *data;
    2159              :         size_t data_size;
    2160              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2161              :         uint8_t *cert_buffer;
    2162              :         size_t cert_buffer_size;
    2163              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2164              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2165              :         uint8_t bin_str0[128];
    2166              :         size_t bin_str0_size;
    2167              :         uint8_t bin_str2[128];
    2168              :         size_t bin_str2_size;
    2169              :         uint8_t bin_str7[128];
    2170              :         size_t bin_str7_size;
    2171              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2172              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2173              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2174              :         size_t spdm_response_size;
    2175              :         size_t transport_header_size;
    2176              : 
    2177              :         ((libspdm_context_t *)spdm_context)
    2178            1 :         ->connection_info.algorithm.base_asym_algo =
    2179              :             m_libspdm_use_asym_algo;
    2180              :         ((libspdm_context_t *)spdm_context)
    2181            1 :         ->connection_info.algorithm.base_hash_algo =
    2182              :             m_libspdm_use_hash_algo;
    2183              :         ((libspdm_context_t *)spdm_context)
    2184            1 :         ->connection_info.algorithm.dhe_named_group =
    2185              :             m_libspdm_use_dhe_algo;
    2186              :         ((libspdm_context_t *)spdm_context)
    2187            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2188              :             m_libspdm_use_measurement_hash_algo;
    2189            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2190            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2191            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2192            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2193            1 :         opaque_key_exchange_rsp_size =
    2194            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2195              :                 spdm_context);
    2196            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2197            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    2198            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2199              :                              hmac_size;
    2200            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2201            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2202            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2203              : 
    2204            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2205            1 :         spdm_response->header.request_response_code =
    2206              :             SPDM_KEY_EXCHANGE_RSP;
    2207            1 :         spdm_response->header.param1 = 0;
    2208            1 :         spdm_response->rsp_session_id =
    2209            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2210            1 :         spdm_response->mut_auth_requested = 0;
    2211            1 :         spdm_response->req_slot_id_param = 0;
    2212            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2213            1 :                                   spdm_response->random_data);
    2214            1 :         ptr = (void *)(spdm_response + 1);
    2215            1 :         dhe_context = libspdm_dhe_new(
    2216            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2217              :                 m_libspdm_use_dhe_algo,
    2218              :                 true);
    2219            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2220              :                                  &dhe_key_size);
    2221            1 :         final_key_size = sizeof(final_key);
    2222            1 :         libspdm_dhe_compute_key(
    2223              :             m_libspdm_use_dhe_algo, dhe_context,
    2224            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2225              :             sizeof(spdm_key_exchange_request_t),
    2226              :             dhe_key_size, final_key, &final_key_size);
    2227            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2228            1 :         ptr += dhe_key_size;
    2229              :         /* libspdm_zero_mem (ptr, hash_size);
    2230              :          * ptr += hash_size;*/
    2231            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2232            1 :         ptr += sizeof(uint16_t);
    2233            1 :         libspdm_build_opaque_data_version_selection_data(
    2234              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    2235            1 :         ptr += opaque_key_exchange_rsp_size;
    2236            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2237              :                                                         m_libspdm_use_asym_algo, &data,
    2238              :                                                         &data_size, NULL, NULL);
    2239            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2240              :                          sizeof(m_libspdm_local_buffer)
    2241            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2242              :                             m_libspdm_local_buffer),
    2243            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2244            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2245            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2246              :                        m_libspdm_local_buffer_size));
    2247            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2248            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2249            1 :         cert_buffer =  (uint8_t *)data;
    2250            1 :         cert_buffer_size = data_size;
    2251            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2252              :                          cert_buffer_hash);
    2253              :         /* transcript.message_a size is 0*/
    2254            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2255            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2256              :                                       m_libspdm_local_buffer_size);
    2257            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2258              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2259            1 :         free(data);
    2260            1 :         libspdm_responder_data_sign(
    2261              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    2262              :             spdm_context,
    2263              : #endif
    2264            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2265              :                 SPDM_KEY_EXCHANGE_RSP,
    2266              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2267            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2268              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2269              :                 &signature_size);
    2270            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2271              :                          sizeof(m_libspdm_local_buffer)
    2272            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2273              :                             m_libspdm_local_buffer),
    2274              :                          ptr, signature_size);
    2275            1 :         m_libspdm_local_buffer_size += signature_size;
    2276            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2277            1 :         ptr += signature_size;
    2278            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2279              :                          libspdm_get_managed_buffer_size(&th_curr),
    2280              :                          th_curr_hash_data);
    2281            1 :         bin_str0_size = sizeof(bin_str0);
    2282            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2283              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2284            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2285              :                            &bin_str0_size);
    2286            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2287              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2288            1 :         bin_str2_size = sizeof(bin_str2);
    2289            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2290              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2291            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2292              :                            bin_str2, &bin_str2_size);
    2293            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2294              :                             bin_str2, bin_str2_size,
    2295              :                             response_handshake_secret, hash_size);
    2296            1 :         bin_str7_size = sizeof(bin_str7);
    2297            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2298              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2299            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2300              :                            &bin_str7_size);
    2301            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2302              :                             hash_size, bin_str7, bin_str7_size,
    2303              :                             response_finished_key, hash_size);
    2304            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2305              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2306            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2307              :                          response_finished_key, hash_size, ptr);
    2308            1 :         ptr += hmac_size;
    2309              : 
    2310            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2311              :                                               false, spdm_response_size,
    2312              :                                               spdm_response, response_size,
    2313              :                                               response);
    2314              :     }
    2315            1 :         return LIBSPDM_STATUS_SUCCESS;
    2316              : 
    2317            1 :     case 0x11: {
    2318              :         spdm_key_exchange_response_t *spdm_response;
    2319              :         size_t dhe_key_size;
    2320              :         uint32_t hash_size;
    2321              :         uint32_t measurement_hash_size;
    2322              :         size_t signature_size;
    2323              :         uint32_t hmac_size;
    2324              :         uint8_t *ptr;
    2325              :         void *dhe_context;
    2326              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2327              :         size_t final_key_size;
    2328              :         size_t opaque_key_exchange_rsp_size;
    2329              :         void *data;
    2330              :         size_t data_size;
    2331              :         uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2332              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2333              :         uint8_t *cert_buffer;
    2334              :         size_t cert_buffer_size;
    2335              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2336              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2337              :         uint8_t bin_str0[128];
    2338              :         size_t bin_str0_size;
    2339              :         uint8_t bin_str2[128];
    2340              :         size_t bin_str2_size;
    2341              :         uint8_t bin_str7[128];
    2342              :         size_t bin_str7_size;
    2343              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2344              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2345              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2346              :         size_t spdm_response_size;
    2347              :         size_t transport_header_size;
    2348              : 
    2349              :         ((libspdm_context_t *)spdm_context)
    2350            1 :         ->connection_info.algorithm.base_asym_algo =
    2351              :             m_libspdm_use_asym_algo;
    2352              :         ((libspdm_context_t *)spdm_context)
    2353            1 :         ->connection_info.algorithm.base_hash_algo =
    2354              :             m_libspdm_use_hash_algo;
    2355              :         ((libspdm_context_t *)spdm_context)
    2356            1 :         ->connection_info.algorithm.dhe_named_group =
    2357              :             m_libspdm_use_dhe_algo;
    2358              :         ((libspdm_context_t *)spdm_context)
    2359            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2360              :             m_libspdm_use_measurement_hash_algo;
    2361            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2362            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2363            1 :         measurement_hash_size = libspdm_get_hash_size(
    2364              :             m_libspdm_use_hash_algo);
    2365            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2366            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2367            1 :         opaque_key_exchange_rsp_size =
    2368            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2369              :                 spdm_context);
    2370            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2371            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    2372            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2373              :                              hmac_size;
    2374            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2375            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2376            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2377              : 
    2378            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2379            1 :         spdm_response->header.request_response_code =
    2380              :             SPDM_KEY_EXCHANGE_RSP;
    2381            1 :         spdm_response->header.param1 = 0;
    2382            1 :         spdm_response->rsp_session_id =
    2383            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2384            1 :         spdm_response->mut_auth_requested = 0;
    2385            1 :         spdm_response->req_slot_id_param = 0;
    2386            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2387            1 :                                   spdm_response->random_data);
    2388            1 :         ptr = (void *)(spdm_response + 1);
    2389            1 :         dhe_context = libspdm_dhe_new(
    2390            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2391              :                 m_libspdm_use_dhe_algo,
    2392              :                 true);
    2393            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2394              :                                  &dhe_key_size);
    2395            1 :         final_key_size = sizeof(final_key);
    2396            1 :         libspdm_dhe_compute_key(
    2397              :             m_libspdm_use_dhe_algo, dhe_context,
    2398            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2399              :             sizeof(spdm_key_exchange_request_t),
    2400              :             dhe_key_size, final_key, &final_key_size);
    2401            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2402            1 :         ptr += dhe_key_size;
    2403              :         /*Mock measurement hash as TCB*/
    2404            1 :         libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
    2405              :                          m_libspdm_use_tcb_hash_value, measurement_hash_size);
    2406            1 :         libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
    2407              :                          measurement_hash_data, measurement_hash_size);
    2408              :         /*libspdm_zero_mem (ptr, measurement_hash_size);*/
    2409            1 :         ptr += measurement_hash_size;
    2410            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2411            1 :         ptr += sizeof(uint16_t);
    2412            1 :         libspdm_build_opaque_data_version_selection_data(
    2413              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    2414            1 :         ptr += opaque_key_exchange_rsp_size;
    2415            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2416              :                                                         m_libspdm_use_asym_algo, &data,
    2417              :                                                         &data_size, NULL, NULL);
    2418            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2419              :                          sizeof(m_libspdm_local_buffer)
    2420            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2421              :                             m_libspdm_local_buffer),
    2422            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2423            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2424            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2425              :                        m_libspdm_local_buffer_size));
    2426            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2427            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2428            1 :         cert_buffer =  (uint8_t *)data;
    2429            1 :         cert_buffer_size = data_size;
    2430            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2431              :                          cert_buffer_hash);
    2432              :         /* transcript.message_a size is 0*/
    2433            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2434            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2435              :                                       m_libspdm_local_buffer_size);
    2436            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2437              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2438            1 :         free(data);
    2439            1 :         libspdm_responder_data_sign(
    2440              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    2441              :             spdm_context,
    2442              : #endif
    2443            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2444              :                 SPDM_KEY_EXCHANGE_RSP,
    2445              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2446            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2447              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2448              :                 &signature_size);
    2449            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2450              :                          sizeof(m_libspdm_local_buffer)
    2451            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2452              :                             m_libspdm_local_buffer),
    2453              :                          ptr, signature_size);
    2454            1 :         m_libspdm_local_buffer_size += signature_size;
    2455            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2456            1 :         ptr += signature_size;
    2457            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2458              :                          libspdm_get_managed_buffer_size(&th_curr),
    2459              :                          th_curr_hash_data);
    2460            1 :         bin_str0_size = sizeof(bin_str0);
    2461            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2462              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2463            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2464              :                            &bin_str0_size);
    2465            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2466              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2467            1 :         bin_str2_size = sizeof(bin_str2);
    2468            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2469              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2470            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2471              :                            bin_str2, &bin_str2_size);
    2472            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2473              :                             bin_str2, bin_str2_size,
    2474              :                             response_handshake_secret, hash_size);
    2475            1 :         bin_str7_size = sizeof(bin_str7);
    2476            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2477              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2478            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2479              :                            &bin_str7_size);
    2480            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2481              :                             hash_size, bin_str7, bin_str7_size,
    2482              :                             response_finished_key, hash_size);
    2483            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2484              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2485            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2486              :                          response_finished_key, hash_size, ptr);
    2487            1 :         ptr += hmac_size;
    2488              : 
    2489            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2490              :                                               false, spdm_response_size,
    2491              :                                               spdm_response, response_size,
    2492              :                                               response);
    2493              :     }
    2494            1 :         return LIBSPDM_STATUS_SUCCESS;
    2495              : 
    2496            1 :     case 0x12: {
    2497              :         spdm_key_exchange_response_t *spdm_response;
    2498              :         size_t dhe_key_size;
    2499              :         uint32_t hash_size;
    2500              :         uint32_t measurement_hash_size;
    2501              :         size_t signature_size;
    2502              :         uint32_t hmac_size;
    2503              :         uint8_t *ptr;
    2504              :         void *dhe_context;
    2505              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2506              :         size_t final_key_size;
    2507              :         size_t opaque_key_exchange_rsp_size;
    2508              :         void *data;
    2509              :         size_t data_size;
    2510              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2511              :         uint8_t *cert_buffer;
    2512              :         size_t cert_buffer_size;
    2513              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2514              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2515              :         uint8_t bin_str0[128];
    2516              :         size_t bin_str0_size;
    2517              :         uint8_t bin_str2[128];
    2518              :         size_t bin_str2_size;
    2519              :         uint8_t bin_str7[128];
    2520              :         size_t bin_str7_size;
    2521              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2522              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2523              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2524              :         size_t spdm_response_size;
    2525              :         size_t transport_header_size;
    2526              : 
    2527              :         ((libspdm_context_t *)spdm_context)
    2528            1 :         ->connection_info.algorithm.base_asym_algo =
    2529              :             m_libspdm_use_asym_algo;
    2530              :         ((libspdm_context_t *)spdm_context)
    2531            1 :         ->connection_info.algorithm.base_hash_algo =
    2532              :             m_libspdm_use_hash_algo;
    2533              :         ((libspdm_context_t *)spdm_context)
    2534            1 :         ->connection_info.algorithm.dhe_named_group =
    2535              :             m_libspdm_use_dhe_algo;
    2536              :         ((libspdm_context_t *)spdm_context)
    2537            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2538              :             m_libspdm_use_measurement_hash_algo;
    2539            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2540            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2541            1 :         measurement_hash_size = 0;
    2542            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2543            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2544            1 :         opaque_key_exchange_rsp_size =
    2545            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2546              :                 spdm_context);
    2547            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2548            1 :                              dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
    2549            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2550              :                              hmac_size;
    2551            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2552            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2553            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2554              : 
    2555            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2556            1 :         spdm_response->header.request_response_code =
    2557              :             SPDM_KEY_EXCHANGE_RSP;
    2558            1 :         spdm_response->header.param1 = 0;
    2559            1 :         spdm_response->rsp_session_id =
    2560            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2561            1 :         spdm_response->mut_auth_requested = 0;
    2562            1 :         spdm_response->req_slot_id_param = 0;
    2563            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2564            1 :                                   spdm_response->random_data);
    2565            1 :         ptr = (void *)(spdm_response + 1);
    2566            1 :         dhe_context = libspdm_dhe_new(
    2567            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2568              :                 m_libspdm_use_dhe_algo,
    2569              :                 true);
    2570            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2571              :                                  &dhe_key_size);
    2572            1 :         final_key_size = sizeof(final_key);
    2573            1 :         libspdm_dhe_compute_key(
    2574              :             m_libspdm_use_dhe_algo, dhe_context,
    2575            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2576              :             sizeof(spdm_key_exchange_request_t),
    2577              :             dhe_key_size, final_key, &final_key_size);
    2578            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2579            1 :         ptr += dhe_key_size;
    2580              : 
    2581            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2582            1 :         ptr += sizeof(uint16_t);
    2583            1 :         libspdm_build_opaque_data_version_selection_data(
    2584              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    2585            1 :         ptr += opaque_key_exchange_rsp_size;
    2586            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2587              :                                                         m_libspdm_use_asym_algo, &data,
    2588              :                                                         &data_size, NULL, NULL);
    2589            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2590              :                          sizeof(m_libspdm_local_buffer)
    2591            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2592              :                             m_libspdm_local_buffer),
    2593            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2594            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2595            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2596              :                        m_libspdm_local_buffer_size));
    2597            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2598            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2599            1 :         cert_buffer =  (uint8_t *)data;
    2600            1 :         cert_buffer_size = data_size;
    2601            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2602              :                          cert_buffer_hash);
    2603              :         /* transcript.message_a size is 0*/
    2604            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2605            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2606              :                                       m_libspdm_local_buffer_size);
    2607            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2608              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2609            1 :         free(data);
    2610            1 :         libspdm_responder_data_sign(
    2611              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    2612              :             spdm_context,
    2613              : #endif
    2614            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2615              :                 SPDM_KEY_EXCHANGE_RSP,
    2616              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2617            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2618              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2619              :                 &signature_size);
    2620            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2621              :                          sizeof(m_libspdm_local_buffer)
    2622            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2623              :                             m_libspdm_local_buffer),
    2624              :                          ptr, signature_size);
    2625            1 :         m_libspdm_local_buffer_size += signature_size;
    2626            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2627            1 :         ptr += signature_size-1;
    2628            1 :         *ptr ^= 0xFF;
    2629            1 :         ptr++;
    2630            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2631              :                          libspdm_get_managed_buffer_size(&th_curr),
    2632              :                          th_curr_hash_data);
    2633            1 :         bin_str0_size = sizeof(bin_str0);
    2634            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2635              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2636            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2637              :                            &bin_str0_size);
    2638            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2639              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2640            1 :         bin_str2_size = sizeof(bin_str2);
    2641            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2642              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2643            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2644              :                            bin_str2, &bin_str2_size);
    2645            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2646              :                             bin_str2, bin_str2_size,
    2647              :                             response_handshake_secret, hash_size);
    2648            1 :         bin_str7_size = sizeof(bin_str7);
    2649            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2650              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2651            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2652              :                            &bin_str7_size);
    2653            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2654              :                             hash_size, bin_str7, bin_str7_size,
    2655              :                             response_finished_key, hash_size);
    2656            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2657              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2658            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2659              :                          response_finished_key, hash_size, ptr);
    2660            1 :         ptr += hmac_size;
    2661              : 
    2662            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2663              :                                               false, spdm_response_size,
    2664              :                                               spdm_response, response_size,
    2665              :                                               response);
    2666              :     }
    2667            1 :         return LIBSPDM_STATUS_SUCCESS;
    2668              : 
    2669            1 :     case 0x13: {
    2670              :         spdm_key_exchange_response_t *spdm_response;
    2671              :         size_t dhe_key_size;
    2672              :         uint32_t hash_size;
    2673              :         size_t signature_size;
    2674              :         uint32_t hmac_size;
    2675              :         uint8_t *ptr;
    2676              :         void *dhe_context;
    2677              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2678              :         size_t final_key_size;
    2679              :         size_t opaque_key_exchange_rsp_size;
    2680              :         void *data;
    2681              :         size_t data_size;
    2682              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2683              :         uint8_t *cert_buffer;
    2684              :         size_t cert_buffer_size;
    2685              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2686              :         size_t spdm_response_size;
    2687              :         size_t transport_header_size;
    2688              : 
    2689              :         ((libspdm_context_t *)spdm_context)
    2690            1 :         ->connection_info.algorithm.base_asym_algo =
    2691              :             m_libspdm_use_asym_algo;
    2692              :         ((libspdm_context_t *)spdm_context)
    2693            1 :         ->connection_info.algorithm.base_hash_algo =
    2694              :             m_libspdm_use_hash_algo;
    2695              :         ((libspdm_context_t *)spdm_context)
    2696            1 :         ->connection_info.algorithm.dhe_named_group =
    2697              :             m_libspdm_use_dhe_algo;
    2698              :         ((libspdm_context_t *)spdm_context)
    2699            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2700              :             m_libspdm_use_measurement_hash_algo;
    2701            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2702            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2703            1 :         hmac_size = 0;
    2704            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2705            1 :         opaque_key_exchange_rsp_size =
    2706            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2707              :                 spdm_context);
    2708            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2709            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    2710            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2711              :                              hmac_size;
    2712            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2713            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2714            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2715              : 
    2716            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2717            1 :         spdm_response->header.request_response_code =
    2718              :             SPDM_KEY_EXCHANGE_RSP;
    2719            1 :         spdm_response->header.param1 = 0;
    2720            1 :         spdm_response->rsp_session_id =
    2721            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2722            1 :         spdm_response->mut_auth_requested = 0;
    2723            1 :         spdm_response->req_slot_id_param = 0;
    2724            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2725            1 :                                   spdm_response->random_data);
    2726            1 :         ptr = (void *)(spdm_response + 1);
    2727            1 :         dhe_context = libspdm_dhe_new(
    2728            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2729              :                 m_libspdm_use_dhe_algo,
    2730              :                 true);
    2731            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2732              :                                  &dhe_key_size);
    2733            1 :         final_key_size = sizeof(final_key);
    2734            1 :         libspdm_dhe_compute_key(
    2735              :             m_libspdm_use_dhe_algo, dhe_context,
    2736            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2737              :             sizeof(spdm_key_exchange_request_t),
    2738              :             dhe_key_size, final_key, &final_key_size);
    2739            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2740            1 :         ptr += dhe_key_size;
    2741              :         /* libspdm_zero_mem (ptr, hash_size);
    2742              :          * ptr += hash_size;*/
    2743            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2744            1 :         ptr += sizeof(uint16_t);
    2745            1 :         libspdm_build_opaque_data_version_selection_data(
    2746              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    2747            1 :         ptr += opaque_key_exchange_rsp_size;
    2748            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2749              :                                                         m_libspdm_use_asym_algo, &data,
    2750              :                                                         &data_size, NULL, NULL);
    2751            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2752              :                          sizeof(m_libspdm_local_buffer)
    2753            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2754              :                             m_libspdm_local_buffer),
    2755            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2756            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2757            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2758              :                        m_libspdm_local_buffer_size));
    2759            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2760            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2761            1 :         cert_buffer =  (uint8_t *)data;
    2762            1 :         cert_buffer_size = data_size;
    2763            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2764              :                          cert_buffer_hash);
    2765              :         /* transcript.message_a size is 0*/
    2766            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2767            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2768              :                                       m_libspdm_local_buffer_size);
    2769            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2770              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2771            1 :         free(data);
    2772            1 :         libspdm_responder_data_sign(
    2773              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    2774              :             spdm_context,
    2775              : #endif
    2776            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2777              :                 SPDM_KEY_EXCHANGE_RSP,
    2778              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2779            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2780              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2781              :                 &signature_size);
    2782            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2783              :                          sizeof(m_libspdm_local_buffer)
    2784            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2785              :                             m_libspdm_local_buffer),
    2786              :                          ptr, signature_size);
    2787            1 :         m_libspdm_local_buffer_size += signature_size;
    2788            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2789            1 :         ptr += signature_size;
    2790              : 
    2791            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2792              :                                               false, spdm_response_size,
    2793              :                                               spdm_response, response_size,
    2794              :                                               response);
    2795              :     }
    2796            1 :         return LIBSPDM_STATUS_SUCCESS;
    2797              : 
    2798            1 :     case 0x14: {
    2799              :         spdm_key_exchange_response_t *spdm_response;
    2800              :         size_t dhe_key_size;
    2801              :         uint32_t hash_size;
    2802              :         size_t signature_size;
    2803              :         uint32_t hmac_size;
    2804              :         uint8_t *ptr;
    2805              :         void *dhe_context;
    2806              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2807              :         size_t final_key_size;
    2808              :         size_t opaque_key_exchange_rsp_size;
    2809              :         void *data;
    2810              :         size_t data_size;
    2811              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2812              :         uint8_t *cert_buffer;
    2813              :         size_t cert_buffer_size;
    2814              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2815              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2816              :         uint8_t bin_str0[128];
    2817              :         size_t bin_str0_size;
    2818              :         uint8_t bin_str2[128];
    2819              :         size_t bin_str2_size;
    2820              :         uint8_t bin_str7[128];
    2821              :         size_t bin_str7_size;
    2822              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2823              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2824              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2825              :         size_t spdm_response_size;
    2826              :         size_t transport_header_size;
    2827              : 
    2828              :         ((libspdm_context_t *)spdm_context)
    2829            1 :         ->connection_info.algorithm.base_asym_algo =
    2830              :             m_libspdm_use_asym_algo;
    2831              :         ((libspdm_context_t *)spdm_context)
    2832            1 :         ->connection_info.algorithm.base_hash_algo =
    2833              :             m_libspdm_use_hash_algo;
    2834              :         ((libspdm_context_t *)spdm_context)
    2835            1 :         ->connection_info.algorithm.dhe_named_group =
    2836              :             m_libspdm_use_dhe_algo;
    2837              :         ((libspdm_context_t *)spdm_context)
    2838            1 :         ->connection_info.algorithm.measurement_hash_algo =
    2839              :             m_libspdm_use_measurement_hash_algo;
    2840            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    2841            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2842            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2843            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    2844            1 :         opaque_key_exchange_rsp_size =
    2845            1 :             libspdm_get_opaque_data_version_selection_data_size(
    2846              :                 spdm_context);
    2847            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    2848            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    2849            1 :                              opaque_key_exchange_rsp_size + signature_size +
    2850              :                              hmac_size;
    2851            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    2852            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    2853            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    2854              : 
    2855            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    2856            1 :         spdm_response->header.request_response_code =
    2857              :             SPDM_KEY_EXCHANGE_RSP;
    2858            1 :         spdm_response->header.param1 = 5;
    2859            1 :         spdm_response->rsp_session_id =
    2860            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    2861            1 :         spdm_response->mut_auth_requested = 0;
    2862            1 :         spdm_response->req_slot_id_param = 0;
    2863            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    2864            1 :                                   spdm_response->random_data);
    2865            1 :         ptr = (void *)(spdm_response + 1);
    2866            1 :         dhe_context = libspdm_dhe_new(
    2867            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2868              :                 m_libspdm_use_dhe_algo,
    2869              :                 true);
    2870            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    2871              :                                  &dhe_key_size);
    2872            1 :         final_key_size = sizeof(final_key);
    2873            1 :         libspdm_dhe_compute_key(
    2874              :             m_libspdm_use_dhe_algo, dhe_context,
    2875            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    2876              :             sizeof(spdm_key_exchange_request_t),
    2877              :             dhe_key_size, final_key, &final_key_size);
    2878            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    2879            1 :         ptr += dhe_key_size;
    2880              :         /* libspdm_zero_mem (ptr, hash_size);
    2881              :          * ptr += hash_size;*/
    2882            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    2883            1 :         ptr += sizeof(uint16_t);
    2884            1 :         libspdm_build_opaque_data_version_selection_data(
    2885              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    2886            1 :         ptr += opaque_key_exchange_rsp_size;
    2887            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2888              :                                                         m_libspdm_use_asym_algo, &data,
    2889              :                                                         &data_size, NULL, NULL);
    2890            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2891              :                          sizeof(m_libspdm_local_buffer)
    2892            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2893              :                             m_libspdm_local_buffer),
    2894            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    2895            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    2896            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    2897              :                        m_libspdm_local_buffer_size));
    2898            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2899            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    2900            1 :         cert_buffer =  (uint8_t *)data;
    2901            1 :         cert_buffer_size = data_size;
    2902            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    2903              :                          cert_buffer_hash);
    2904              :         /* transcript.message_a size is 0*/
    2905            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    2906            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    2907              :                                       m_libspdm_local_buffer_size);
    2908            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2909              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2910            1 :         free(data);
    2911            1 :         libspdm_responder_data_sign(
    2912              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    2913              :             spdm_context,
    2914              : #endif
    2915            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    2916              :                 SPDM_KEY_EXCHANGE_RSP,
    2917              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    2918            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    2919              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    2920              :                 &signature_size);
    2921            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    2922              :                          sizeof(m_libspdm_local_buffer)
    2923            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    2924              :                             m_libspdm_local_buffer),
    2925              :                          ptr, signature_size);
    2926            1 :         m_libspdm_local_buffer_size += signature_size;
    2927            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    2928            1 :         ptr += signature_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),
    2931              :                          th_curr_hash_data);
    2932            1 :         bin_str0_size = sizeof(bin_str0);
    2933            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2934              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    2935            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    2936              :                            &bin_str0_size);
    2937            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    2938              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    2939            1 :         bin_str2_size = sizeof(bin_str2);
    2940            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2941              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    2942            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    2943              :                            bin_str2, &bin_str2_size);
    2944            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    2945              :                             bin_str2, bin_str2_size,
    2946              :                             response_handshake_secret, hash_size);
    2947            1 :         bin_str7_size = sizeof(bin_str7);
    2948            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    2949              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    2950            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    2951              :                            &bin_str7_size);
    2952            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    2953              :                             hash_size, bin_str7, bin_str7_size,
    2954              :                             response_finished_key, hash_size);
    2955            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    2956              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    2957            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    2958              :                          response_finished_key, hash_size, ptr);
    2959            1 :         ptr += hmac_size;
    2960              : 
    2961            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    2962              :                                               false, spdm_response_size,
    2963              :                                               spdm_response, response_size,
    2964              :                                               response);
    2965              :     }
    2966            1 :         return LIBSPDM_STATUS_SUCCESS;
    2967              : 
    2968            1 :     case 0x15: {
    2969              :         spdm_key_exchange_response_t *spdm_response;
    2970              :         size_t dhe_key_size;
    2971              :         uint32_t hash_size;
    2972              :         size_t signature_size;
    2973              :         uint32_t hmac_size;
    2974              :         uint8_t *ptr;
    2975              :         void *dhe_context;
    2976              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    2977              :         size_t final_key_size;
    2978              :         size_t opaque_key_exchange_rsp_size;
    2979              :         void *data;
    2980              :         size_t data_size;
    2981              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    2982              :         uint8_t *cert_buffer;
    2983              :         size_t cert_buffer_size;
    2984              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    2985              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    2986              :         uint8_t bin_str0[128];
    2987              :         size_t bin_str0_size;
    2988              :         uint8_t bin_str2[128];
    2989              :         size_t bin_str2_size;
    2990              :         uint8_t bin_str7[128];
    2991              :         size_t bin_str7_size;
    2992              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2993              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    2994              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    2995              :         size_t spdm_response_size;
    2996              :         size_t transport_header_size;
    2997              : 
    2998              :         ((libspdm_context_t *)spdm_context)
    2999            1 :         ->connection_info.algorithm.base_asym_algo =
    3000              :             m_libspdm_use_asym_algo;
    3001              :         ((libspdm_context_t *)spdm_context)
    3002            1 :         ->connection_info.algorithm.base_hash_algo =
    3003              :             m_libspdm_use_hash_algo;
    3004              :         ((libspdm_context_t *)spdm_context)
    3005            1 :         ->connection_info.algorithm.dhe_named_group =
    3006              :             m_libspdm_use_dhe_algo;
    3007              :         ((libspdm_context_t *)spdm_context)
    3008            1 :         ->connection_info.algorithm.measurement_hash_algo =
    3009              :             m_libspdm_use_measurement_hash_algo;
    3010            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3011            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3012            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3013            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3014            1 :         opaque_key_exchange_rsp_size =
    3015            1 :             libspdm_get_opaque_data_version_selection_data_size(
    3016              :                 spdm_context);
    3017            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3018            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3019            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3020              :                              hmac_size;
    3021            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3022            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3023            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3024              : 
    3025            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3026            1 :         spdm_response->header.request_response_code =
    3027              :             SPDM_KEY_EXCHANGE_RSP;
    3028            1 :         spdm_response->header.param1 = 5;
    3029            1 :         spdm_response->rsp_session_id =
    3030            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3031            1 :         spdm_response->mut_auth_requested = 0;
    3032            1 :         spdm_response->req_slot_id_param = 0;
    3033            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3034            1 :                                   spdm_response->random_data);
    3035            1 :         ptr = (void *)(spdm_response + 1);
    3036            1 :         dhe_context = libspdm_dhe_new(
    3037            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3038              :                 m_libspdm_use_dhe_algo,
    3039              :                 true);
    3040            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3041              :                                  &dhe_key_size);
    3042            1 :         final_key_size = sizeof(final_key);
    3043            1 :         libspdm_dhe_compute_key(
    3044              :             m_libspdm_use_dhe_algo, dhe_context,
    3045            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3046              :             sizeof(spdm_key_exchange_request_t),
    3047              :             dhe_key_size, final_key, &final_key_size);
    3048            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3049            1 :         ptr += dhe_key_size;
    3050              :         /* libspdm_zero_mem (ptr, hash_size);
    3051              :          * ptr += hash_size;*/
    3052            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3053            1 :         ptr += sizeof(uint16_t);
    3054            1 :         libspdm_build_opaque_data_version_selection_data(
    3055              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    3056            1 :         ptr += opaque_key_exchange_rsp_size;
    3057            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3058              :                                                         m_libspdm_use_asym_algo, &data,
    3059              :                                                         &data_size, NULL, NULL);
    3060            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3061              :                          sizeof(m_libspdm_local_buffer)
    3062            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3063              :                             m_libspdm_local_buffer),
    3064            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3065            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3066            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3067              :                        m_libspdm_local_buffer_size));
    3068            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3069            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3070            1 :         cert_buffer = (uint8_t *)data;
    3071            1 :         cert_buffer_size = data_size;
    3072            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3073              :                          cert_buffer_hash);
    3074              :         /* transcript.message_a size is 0*/
    3075            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3076            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3077              :                                       m_libspdm_local_buffer_size);
    3078            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3079              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3080            1 :         free(data);
    3081            1 :         libspdm_responder_data_sign(
    3082              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    3083              :             spdm_context,
    3084              : #endif
    3085            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3086              :                 SPDM_KEY_EXCHANGE_RSP,
    3087              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3088            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3089              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3090              :                 &signature_size);
    3091            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3092              :                          sizeof(m_libspdm_local_buffer)
    3093            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3094              :                             m_libspdm_local_buffer),
    3095              :                          ptr, signature_size);
    3096            1 :         m_libspdm_local_buffer_size += signature_size;
    3097            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3098            1 :         ptr += signature_size;
    3099            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3100              :                          libspdm_get_managed_buffer_size(&th_curr),
    3101              :                          th_curr_hash_data);
    3102            1 :         bin_str0_size = sizeof(bin_str0);
    3103            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3104              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3105            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3106              :                            &bin_str0_size);
    3107            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3108              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3109            1 :         bin_str2_size = sizeof(bin_str2);
    3110            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3111              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3112            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3113              :                            bin_str2, &bin_str2_size);
    3114            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3115              :                             bin_str2, bin_str2_size,
    3116              :                             response_handshake_secret, hash_size);
    3117            1 :         bin_str7_size = sizeof(bin_str7);
    3118            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3119              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3120            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3121              :                            &bin_str7_size);
    3122            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3123              :                             hash_size, bin_str7, bin_str7_size,
    3124              :                             response_finished_key, hash_size);
    3125            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3126              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3127            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3128              :                          response_finished_key, hash_size, ptr);
    3129            1 :         ptr += hmac_size;
    3130              : 
    3131            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3132              :                                               false, spdm_response_size,
    3133              :                                               spdm_response, response_size,
    3134              :                                               response);
    3135              :     }
    3136            1 :         return LIBSPDM_STATUS_SUCCESS;
    3137              : 
    3138            1 :     case 0x16: {
    3139              :         spdm_key_exchange_response_t *spdm_response;
    3140              :         size_t dhe_key_size;
    3141              :         uint32_t hash_size;
    3142              :         size_t signature_size;
    3143              :         uint32_t hmac_size;
    3144              :         uint8_t *ptr;
    3145              :         void *dhe_context;
    3146              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3147              :         size_t final_key_size;
    3148              :         size_t opaque_key_exchange_rsp_size;
    3149              :         void *data;
    3150              :         size_t data_size;
    3151              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3152              :         uint8_t *cert_buffer;
    3153              :         size_t cert_buffer_size;
    3154              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3155              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3156              :         uint8_t bin_str0[128];
    3157              :         size_t bin_str0_size;
    3158              :         uint8_t bin_str2[128];
    3159              :         size_t bin_str2_size;
    3160              :         uint8_t bin_str7[128];
    3161              :         size_t bin_str7_size;
    3162              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3163              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3164              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3165              :         size_t spdm_response_size;
    3166              :         size_t transport_header_size;
    3167              : 
    3168              :         ((libspdm_context_t *)spdm_context)
    3169            1 :         ->connection_info.algorithm.base_asym_algo =
    3170              :             m_libspdm_use_asym_algo;
    3171              :         ((libspdm_context_t *)spdm_context)
    3172            1 :         ->connection_info.algorithm.base_hash_algo =
    3173              :             m_libspdm_use_hash_algo;
    3174              :         ((libspdm_context_t *)spdm_context)
    3175            1 :         ->connection_info.algorithm.dhe_named_group =
    3176              :             m_libspdm_use_dhe_algo;
    3177              :         ((libspdm_context_t *)spdm_context)
    3178            1 :         ->connection_info.algorithm.measurement_hash_algo =
    3179              :             m_libspdm_use_measurement_hash_algo;
    3180            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3181            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3182            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3183            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3184            1 :         opaque_key_exchange_rsp_size =
    3185            1 :             libspdm_get_opaque_data_version_selection_data_size(
    3186              :                 spdm_context);
    3187            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3188            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3189            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3190              :                              hmac_size;
    3191            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3192            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3193            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3194              : 
    3195            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3196            1 :         spdm_response->header.request_response_code =
    3197              :             SPDM_KEY_EXCHANGE_RSP;
    3198            1 :         spdm_response->header.param1 = 0;
    3199            1 :         spdm_response->rsp_session_id =
    3200            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3201            1 :         spdm_response->mut_auth_requested = 0;
    3202            1 :         spdm_response->req_slot_id_param = 0;
    3203            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3204            1 :                                   spdm_response->random_data);
    3205            1 :         ptr = (void *)(spdm_response + 1);
    3206            1 :         dhe_context = libspdm_dhe_new(
    3207            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3208              :                 m_libspdm_use_dhe_algo,
    3209              :                 true);
    3210            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3211              :                                  &dhe_key_size);
    3212            1 :         final_key_size = sizeof(final_key);
    3213            1 :         libspdm_dhe_compute_key(
    3214              :             m_libspdm_use_dhe_algo, dhe_context,
    3215            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3216              :             sizeof(spdm_key_exchange_request_t),
    3217              :             dhe_key_size, final_key, &final_key_size);
    3218            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3219            1 :         ptr += dhe_key_size;
    3220              :         /* libspdm_zero_mem (ptr, hash_size);
    3221              :          * ptr += hash_size;*/
    3222            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3223            1 :         ptr += sizeof(uint16_t);
    3224            1 :         libspdm_build_opaque_data_version_selection_data(
    3225              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    3226            1 :         ptr += opaque_key_exchange_rsp_size;
    3227            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3228              :                                                         m_libspdm_use_asym_algo, &data,
    3229              :                                                         &data_size, NULL, NULL);
    3230            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3231              :                          sizeof(m_libspdm_local_buffer)
    3232            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3233              :                             m_libspdm_local_buffer),
    3234            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3235            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3236            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3237              :                        m_libspdm_local_buffer_size));
    3238            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3239            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3240            1 :         cert_buffer =  (uint8_t *)data;
    3241            1 :         cert_buffer_size = data_size;
    3242            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3243              :                          cert_buffer_hash);
    3244              :         /* transcript.message_a size is 0*/
    3245            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3246            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3247              :                                       m_libspdm_local_buffer_size);
    3248            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3249              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3250            1 :         free(data);
    3251            1 :         libspdm_responder_data_sign(
    3252              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    3253              :             spdm_context,
    3254              : #endif
    3255            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3256              :                 SPDM_KEY_EXCHANGE_RSP,
    3257              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3258            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3259              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3260              :                 &signature_size);
    3261            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3262              :                          sizeof(m_libspdm_local_buffer)
    3263            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3264              :                             m_libspdm_local_buffer),
    3265              :                          ptr, signature_size);
    3266            1 :         m_libspdm_local_buffer_size += signature_size;
    3267            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3268            1 :         ptr += signature_size;
    3269            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3270              :                          libspdm_get_managed_buffer_size(&th_curr),
    3271              :                          th_curr_hash_data);
    3272            1 :         bin_str0_size = sizeof(bin_str0);
    3273            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3274              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3275            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3276              :                            &bin_str0_size);
    3277            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3278              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3279            1 :         bin_str2_size = sizeof(bin_str2);
    3280            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3281              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3282            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3283              :                            bin_str2, &bin_str2_size);
    3284            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3285              :                             bin_str2, bin_str2_size,
    3286              :                             response_handshake_secret, hash_size);
    3287            1 :         bin_str7_size = sizeof(bin_str7);
    3288            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3289              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3290            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3291              :                            &bin_str7_size);
    3292            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3293              :                             hash_size, bin_str7, bin_str7_size,
    3294              :                             response_finished_key, hash_size);
    3295            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3296              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3297            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3298              :                          response_finished_key, hash_size, ptr);
    3299            1 :         ptr += hmac_size;
    3300              : 
    3301            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3302              :                                               false, spdm_response_size,
    3303              :                                               spdm_response, response_size,
    3304              :                                               response);
    3305              :     }
    3306            1 :         return LIBSPDM_STATUS_SUCCESS;
    3307              : 
    3308            1 :     case 0x17: {
    3309              :         spdm_key_exchange_response_t *spdm_response;
    3310              :         size_t dhe_key_size;
    3311              :         uint32_t hash_size;
    3312              :         size_t signature_size;
    3313              :         uint32_t hmac_size;
    3314              :         uint8_t *ptr;
    3315              :         void *dhe_context;
    3316              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3317              :         size_t final_key_size;
    3318              :         size_t opaque_key_exchange_rsp_size;
    3319              :         void *data;
    3320              :         size_t data_size;
    3321              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3322              :         uint8_t *cert_buffer;
    3323              :         size_t cert_buffer_size;
    3324              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3325              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3326              :         uint8_t bin_str0[128];
    3327              :         size_t bin_str0_size;
    3328              :         uint8_t bin_str2[128];
    3329              :         size_t bin_str2_size;
    3330              :         uint8_t bin_str7[128];
    3331              :         size_t bin_str7_size;
    3332              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3333              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3334              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3335              :         size_t spdm_response_size;
    3336              :         size_t transport_header_size;
    3337              : 
    3338              :         ((libspdm_context_t *)spdm_context)
    3339            1 :         ->connection_info.algorithm.base_asym_algo =
    3340              :             m_libspdm_use_asym_algo;
    3341              :         ((libspdm_context_t *)spdm_context)
    3342            1 :         ->connection_info.algorithm.base_hash_algo =
    3343              :             m_libspdm_use_hash_algo;
    3344              :         ((libspdm_context_t *)spdm_context)
    3345            1 :         ->connection_info.algorithm.dhe_named_group =
    3346              :             m_libspdm_use_dhe_algo;
    3347              :         ((libspdm_context_t *)spdm_context)
    3348            1 :         ->connection_info.algorithm.measurement_hash_algo =
    3349              :             m_libspdm_use_measurement_hash_algo;
    3350            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3351            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3352            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3353            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3354            1 :         opaque_key_exchange_rsp_size =
    3355            1 :             libspdm_get_opaque_data_version_selection_data_size(
    3356              :                 spdm_context);
    3357            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3358            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3359            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3360              :                              hmac_size;
    3361            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3362            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3363            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3364              : 
    3365            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3366            1 :         spdm_response->header.request_response_code =
    3367              :             SPDM_KEY_EXCHANGE_RSP;
    3368            1 :         spdm_response->header.param1 = 0;
    3369            1 :         spdm_response->rsp_session_id =
    3370            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3371            1 :         spdm_response->mut_auth_requested =
    3372              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED;
    3373            1 :         spdm_response->req_slot_id_param = 0xF;
    3374            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3375            1 :                                   spdm_response->random_data);
    3376            1 :         ptr = (void *)(spdm_response + 1);
    3377            1 :         dhe_context = libspdm_dhe_new(
    3378            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3379              :                 m_libspdm_use_dhe_algo,
    3380              :                 true);
    3381            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3382              :                                  &dhe_key_size);
    3383            1 :         final_key_size = sizeof(final_key);
    3384            1 :         libspdm_dhe_compute_key(
    3385              :             m_libspdm_use_dhe_algo, dhe_context,
    3386            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3387              :             sizeof(spdm_key_exchange_request_t),
    3388              :             dhe_key_size, final_key, &final_key_size);
    3389            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3390            1 :         ptr += dhe_key_size;
    3391              :         /* libspdm_zero_mem (ptr, hash_size);
    3392              :          * ptr += hash_size;*/
    3393            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3394            1 :         ptr += sizeof(uint16_t);
    3395            1 :         libspdm_build_opaque_data_version_selection_data(
    3396              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    3397            1 :         ptr += opaque_key_exchange_rsp_size;
    3398            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3399              :                                                         m_libspdm_use_asym_algo, &data,
    3400              :                                                         &data_size, NULL, NULL);
    3401            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3402              :                          sizeof(m_libspdm_local_buffer)
    3403            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3404              :                             m_libspdm_local_buffer),
    3405            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3406            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3407            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3408              :                        m_libspdm_local_buffer_size));
    3409            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3410            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3411            1 :         cert_buffer =  (uint8_t *)data;
    3412            1 :         cert_buffer_size = data_size;
    3413            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3414              :                          cert_buffer_hash);
    3415              :         /* transcript.message_a size is 0*/
    3416            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3417            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3418              :                                       m_libspdm_local_buffer_size);
    3419            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3420              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3421            1 :         free(data);
    3422            1 :         libspdm_responder_data_sign(
    3423              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    3424              :             spdm_context,
    3425              : #endif
    3426            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3427              :                 SPDM_KEY_EXCHANGE_RSP,
    3428              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3429            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3430              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3431              :                 &signature_size);
    3432            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3433              :                          sizeof(m_libspdm_local_buffer)
    3434            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3435              :                             m_libspdm_local_buffer),
    3436              :                          ptr, signature_size);
    3437            1 :         m_libspdm_local_buffer_size += signature_size;
    3438            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3439            1 :         ptr += signature_size;
    3440            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3441              :                          libspdm_get_managed_buffer_size(&th_curr),
    3442              :                          th_curr_hash_data);
    3443            1 :         bin_str0_size = sizeof(bin_str0);
    3444            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3445              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3446            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3447              :                            &bin_str0_size);
    3448            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3449              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3450            1 :         bin_str2_size = sizeof(bin_str2);
    3451            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3452              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3453            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3454              :                            bin_str2, &bin_str2_size);
    3455            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3456              :                             bin_str2, bin_str2_size,
    3457              :                             response_handshake_secret, hash_size);
    3458            1 :         bin_str7_size = sizeof(bin_str7);
    3459            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3460              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3461            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3462              :                            &bin_str7_size);
    3463            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3464              :                             hash_size, bin_str7, bin_str7_size,
    3465              :                             response_finished_key, hash_size);
    3466            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3467              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3468            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3469              :                          response_finished_key, hash_size, ptr);
    3470            1 :         ptr += hmac_size;
    3471              : 
    3472            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3473              :                                               false, spdm_response_size,
    3474              :                                               spdm_response, response_size,
    3475              :                                               response);
    3476              :     }
    3477            1 :         return LIBSPDM_STATUS_SUCCESS;
    3478              : 
    3479            1 :     case 0x18: {
    3480              :         spdm_key_exchange_response_t *spdm_response;
    3481              :         size_t dhe_key_size;
    3482              :         uint32_t hash_size;
    3483              :         size_t signature_size;
    3484              :         uint32_t hmac_size;
    3485              :         uint8_t *ptr;
    3486              :         void *dhe_context;
    3487              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3488              :         size_t final_key_size;
    3489              :         size_t opaque_key_exchange_rsp_size;
    3490              :         void *data;
    3491              :         size_t data_size;
    3492              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3493              :         uint8_t *cert_buffer;
    3494              :         size_t cert_buffer_size;
    3495              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3496              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3497              :         uint8_t bin_str0[128];
    3498              :         size_t bin_str0_size;
    3499              :         uint8_t bin_str2[128];
    3500              :         size_t bin_str2_size;
    3501              :         uint8_t bin_str7[128];
    3502              :         size_t bin_str7_size;
    3503              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3504              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3505              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3506              :         size_t spdm_response_size;
    3507              :         size_t transport_header_size;
    3508              : 
    3509              :         ((libspdm_context_t *)spdm_context)
    3510            1 :         ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    3511              :         ((libspdm_context_t *)spdm_context)
    3512            1 :         ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    3513              :         ((libspdm_context_t *)spdm_context)
    3514            1 :         ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    3515              :         ((libspdm_context_t *)spdm_context)
    3516            1 :         ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
    3517            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3518            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3519            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3520            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3521            1 :         opaque_key_exchange_rsp_size =
    3522            1 :             libspdm_get_opaque_data_version_selection_data_size(spdm_context);
    3523            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3524            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3525            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3526              :                              hmac_size;
    3527            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3528            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3529            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3530              : 
    3531            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3532            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    3533            1 :         spdm_response->header.param1 = 0x0;
    3534            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    3535            1 :         spdm_response->mut_auth_requested =
    3536              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
    3537            1 :         spdm_response->req_slot_id_param = 0x0;
    3538            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
    3539            1 :         ptr = (void *)(spdm_response + 1);
    3540            1 :         dhe_context = libspdm_dhe_new(
    3541            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3542              :                 m_libspdm_use_dhe_algo, true);
    3543            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    3544            1 :         final_key_size = sizeof(final_key);
    3545            1 :         libspdm_dhe_compute_key(
    3546              :             m_libspdm_use_dhe_algo, dhe_context,
    3547            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3548              :             sizeof(spdm_key_exchange_request_t),
    3549              :             dhe_key_size, final_key, &final_key_size);
    3550            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3551            1 :         ptr += dhe_key_size;
    3552              :         /* libspdm_zero_mem (ptr, hash_size);
    3553              :          * ptr += hash_size;*/
    3554            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3555            1 :         ptr += sizeof(uint16_t);
    3556            1 :         libspdm_build_opaque_data_version_selection_data(
    3557              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    3558            1 :         ptr += opaque_key_exchange_rsp_size;
    3559            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3560              :                                                         m_libspdm_use_asym_algo, &data,
    3561              :                                                         &data_size, NULL, NULL);
    3562            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3563              :                          sizeof(m_libspdm_local_buffer)
    3564            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3565              :                             m_libspdm_local_buffer),
    3566            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3567            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3568            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3569              :                        m_libspdm_local_buffer_size));
    3570            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3571            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3572            1 :         cert_buffer =  (uint8_t *)data;
    3573            1 :         cert_buffer_size = data_size;
    3574            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3575              :                          cert_buffer_hash);
    3576              :         /* transcript.message_a size is 0*/
    3577            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3578            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3579              :                                       m_libspdm_local_buffer_size);
    3580            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3581              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3582            1 :         free(data);
    3583            1 :         libspdm_responder_data_sign(
    3584              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    3585              :             spdm_context,
    3586              : #endif
    3587            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3588              :                 SPDM_KEY_EXCHANGE_RSP,
    3589              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3590            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3591              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3592              :                 &signature_size);
    3593            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3594              :                          sizeof(m_libspdm_local_buffer)
    3595            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3596              :                             m_libspdm_local_buffer),
    3597              :                          ptr, signature_size);
    3598            1 :         m_libspdm_local_buffer_size += signature_size;
    3599            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3600            1 :         ptr += signature_size;
    3601            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3602              :                          libspdm_get_managed_buffer_size(&th_curr),
    3603              :                          th_curr_hash_data);
    3604            1 :         bin_str0_size = sizeof(bin_str0);
    3605            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3606              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3607            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3608              :                            &bin_str0_size);
    3609            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3610              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3611            1 :         bin_str2_size = sizeof(bin_str2);
    3612            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3613              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3614            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3615              :                            bin_str2, &bin_str2_size);
    3616            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3617              :                             bin_str2, bin_str2_size,
    3618              :                             response_handshake_secret, hash_size);
    3619            1 :         bin_str7_size = sizeof(bin_str7);
    3620            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3621              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3622            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3623              :                            &bin_str7_size);
    3624            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3625              :                             hash_size, bin_str7, bin_str7_size,
    3626              :                             response_finished_key, hash_size);
    3627            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3628              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3629            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3630              :                          response_finished_key, hash_size, ptr);
    3631            1 :         ptr += hmac_size;
    3632              : 
    3633            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3634              :                                               false, spdm_response_size,
    3635              :                                               spdm_response, response_size,
    3636              :                                               response);
    3637              :     }
    3638            1 :         return LIBSPDM_STATUS_SUCCESS;
    3639              : 
    3640            1 :     case 0x19: {
    3641              :         spdm_key_exchange_response_t *spdm_response;
    3642              :         size_t dhe_key_size;
    3643              :         uint32_t hash_size;
    3644              :         size_t signature_size;
    3645              :         uint32_t hmac_size;
    3646              :         uint8_t *ptr;
    3647              :         void *dhe_context;
    3648              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3649              :         size_t final_key_size;
    3650              :         size_t opaque_key_exchange_rsp_size;
    3651              :         void *data;
    3652              :         size_t data_size;
    3653              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3654              :         uint8_t *cert_buffer;
    3655              :         size_t cert_buffer_size;
    3656              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3657              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3658              :         uint8_t bin_str0[128];
    3659              :         size_t bin_str0_size;
    3660              :         uint8_t bin_str2[128];
    3661              :         size_t bin_str2_size;
    3662              :         uint8_t bin_str7[128];
    3663              :         size_t bin_str7_size;
    3664              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3665              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3666              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3667              :         size_t spdm_response_size;
    3668              :         size_t transport_header_size;
    3669              : 
    3670              :         ((libspdm_context_t *)spdm_context)
    3671            1 :         ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    3672              :         ((libspdm_context_t *)spdm_context)
    3673            1 :         ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    3674              :         ((libspdm_context_t *)spdm_context)
    3675            1 :         ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    3676              :         ((libspdm_context_t *)spdm_context)
    3677            1 :         ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
    3678            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3679            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3680            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3681            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3682            1 :         opaque_key_exchange_rsp_size =
    3683            1 :             libspdm_get_opaque_data_version_selection_data_size(spdm_context);
    3684            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3685            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3686            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3687              :                              hmac_size;
    3688            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3689            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3690            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3691              : 
    3692            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3693            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    3694            1 :         spdm_response->header.param1 = 0;
    3695            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    3696            1 :         spdm_response->mut_auth_requested =
    3697              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
    3698            1 :         spdm_response->req_slot_id_param = 0;
    3699            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3700            1 :                                   spdm_response->random_data);
    3701            1 :         ptr = (void *)(spdm_response + 1);
    3702            1 :         dhe_context = libspdm_dhe_new(
    3703            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3704              :                 m_libspdm_use_dhe_algo, true);
    3705            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    3706            1 :         final_key_size = sizeof(final_key);
    3707            1 :         libspdm_dhe_compute_key(
    3708              :             m_libspdm_use_dhe_algo, dhe_context,
    3709            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3710              :             sizeof(spdm_key_exchange_request_t),
    3711              :             dhe_key_size, final_key, &final_key_size);
    3712            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3713            1 :         ptr += dhe_key_size;
    3714              :         /* libspdm_zero_mem (ptr, hash_size);
    3715              :          * ptr += hash_size;*/
    3716            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3717            1 :         ptr += sizeof(uint16_t);
    3718            1 :         libspdm_build_opaque_data_version_selection_data(
    3719              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    3720            1 :         ptr += opaque_key_exchange_rsp_size;
    3721            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3722              :                                                         m_libspdm_use_asym_algo, &data,
    3723              :                                                         &data_size, NULL, NULL);
    3724            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3725              :                          sizeof(m_libspdm_local_buffer)
    3726            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3727              :                             m_libspdm_local_buffer),
    3728            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3729            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3730            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3731              :                        m_libspdm_local_buffer_size));
    3732            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3733            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3734            1 :         cert_buffer =  (uint8_t *)data;
    3735            1 :         cert_buffer_size = data_size;
    3736            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3737              :                          cert_buffer_hash);
    3738              :         /* transcript.message_a size is 0*/
    3739            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3740            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3741              :                                       m_libspdm_local_buffer_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), hash_data);
    3744            1 :         free(data);
    3745            1 :         libspdm_responder_data_sign(
    3746              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    3747              :             spdm_context,
    3748              : #endif
    3749            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3750              :                 SPDM_KEY_EXCHANGE_RSP,
    3751              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3752            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3753              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3754              :                 &signature_size);
    3755            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3756              :                          sizeof(m_libspdm_local_buffer)
    3757            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3758              :                             m_libspdm_local_buffer),
    3759              :                          ptr, signature_size);
    3760            1 :         m_libspdm_local_buffer_size += signature_size;
    3761            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3762            1 :         ptr += signature_size;
    3763            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3764              :                          libspdm_get_managed_buffer_size(&th_curr),
    3765              :                          th_curr_hash_data);
    3766            1 :         bin_str0_size = sizeof(bin_str0);
    3767            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3768              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3769            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3770              :                            &bin_str0_size);
    3771            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3772              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3773            1 :         bin_str2_size = sizeof(bin_str2);
    3774            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3775              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3776            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3777              :                            bin_str2, &bin_str2_size);
    3778            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3779              :                             bin_str2, bin_str2_size,
    3780              :                             response_handshake_secret, hash_size);
    3781            1 :         bin_str7_size = sizeof(bin_str7);
    3782            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3783              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3784            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3785              :                            &bin_str7_size);
    3786            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3787              :                             hash_size, bin_str7, bin_str7_size,
    3788              :                             response_finished_key, hash_size);
    3789            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3790              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3791            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3792              :                          response_finished_key, hash_size, ptr);
    3793            1 :         ptr += hmac_size;
    3794              : 
    3795            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3796              :                                               false, spdm_response_size,
    3797              :                                               spdm_response, response_size,
    3798              :                                               response);
    3799              :     }
    3800            1 :         return LIBSPDM_STATUS_SUCCESS;
    3801              : 
    3802            1 :     case 0x1A: {
    3803              :         spdm_key_exchange_response_t *spdm_response;
    3804              :         size_t dhe_key_size;
    3805              :         uint32_t hash_size;
    3806              :         size_t signature_size;
    3807              :         uint32_t hmac_size;
    3808              :         uint8_t *ptr;
    3809              :         void *dhe_context;
    3810              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3811              :         size_t final_key_size;
    3812              :         size_t opaque_key_exchange_rsp_size;
    3813              :         void *data;
    3814              :         size_t data_size;
    3815              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3816              :         uint8_t *cert_buffer;
    3817              :         size_t cert_buffer_size;
    3818              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3819              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3820              :         uint8_t bin_str0[128];
    3821              :         size_t bin_str0_size;
    3822              :         uint8_t bin_str2[128];
    3823              :         size_t bin_str2_size;
    3824              :         uint8_t bin_str7[128];
    3825              :         size_t bin_str7_size;
    3826              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3827              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3828              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    3829              :         size_t spdm_response_size;
    3830              :         size_t transport_header_size;
    3831              : 
    3832              :         ((libspdm_context_t *)spdm_context)
    3833            1 :         ->connection_info.algorithm.base_asym_algo =
    3834              :             m_libspdm_use_asym_algo;
    3835              :         ((libspdm_context_t *)spdm_context)
    3836            1 :         ->connection_info.algorithm.base_hash_algo =
    3837              :             m_libspdm_use_hash_algo;
    3838              :         ((libspdm_context_t *)spdm_context)
    3839            1 :         ->connection_info.algorithm.dhe_named_group =
    3840              :             m_libspdm_use_dhe_algo;
    3841              :         ((libspdm_context_t *)spdm_context)
    3842            1 :         ->connection_info.algorithm.measurement_hash_algo =
    3843              :             m_libspdm_use_measurement_hash_algo;
    3844            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    3845            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3846            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3847            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    3848            1 :         opaque_key_exchange_rsp_size =
    3849            1 :             libspdm_get_opaque_data_version_selection_data_size(
    3850              :                 spdm_context);
    3851            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    3852            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    3853            1 :                              opaque_key_exchange_rsp_size + signature_size +
    3854              :                              hmac_size;
    3855            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    3856            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    3857            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    3858              : 
    3859            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    3860            1 :         spdm_response->header.request_response_code =
    3861              :             SPDM_KEY_EXCHANGE_RSP;
    3862            1 :         spdm_response->header.param1 = 0;
    3863            1 :         spdm_response->rsp_session_id =
    3864            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    3865            1 :         spdm_response->mut_auth_requested =
    3866              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED |
    3867              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
    3868            1 :         spdm_response->req_slot_id_param = 0xF;
    3869            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    3870            1 :                                   spdm_response->random_data);
    3871            1 :         ptr = (void *)(spdm_response + 1);
    3872            1 :         dhe_context = libspdm_dhe_new(
    3873            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3874              :                 m_libspdm_use_dhe_algo,
    3875              :                 true);
    3876            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    3877              :                                  &dhe_key_size);
    3878            1 :         final_key_size = sizeof(final_key);
    3879            1 :         libspdm_dhe_compute_key(
    3880              :             m_libspdm_use_dhe_algo, dhe_context,
    3881            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    3882              :             sizeof(spdm_key_exchange_request_t),
    3883              :             dhe_key_size, final_key, &final_key_size);
    3884            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    3885            1 :         ptr += dhe_key_size;
    3886              :         /* libspdm_zero_mem (ptr, hash_size);
    3887              :          * ptr += hash_size;*/
    3888            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    3889            1 :         ptr += sizeof(uint16_t);
    3890            1 :         libspdm_build_opaque_data_version_selection_data(
    3891              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    3892            1 :         ptr += opaque_key_exchange_rsp_size;
    3893            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3894              :                                                         m_libspdm_use_asym_algo, &data,
    3895              :                                                         &data_size, NULL, NULL);
    3896            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3897              :                          sizeof(m_libspdm_local_buffer)
    3898            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3899              :                             m_libspdm_local_buffer),
    3900            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    3901            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    3902            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    3903              :                        m_libspdm_local_buffer_size));
    3904            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3905            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    3906            1 :         cert_buffer =  (uint8_t *)data;
    3907            1 :         cert_buffer_size = data_size;
    3908            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    3909              :                          cert_buffer_hash);
    3910              :         /* transcript.message_a size is 0*/
    3911            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    3912            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    3913              :                                       m_libspdm_local_buffer_size);
    3914            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3915              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3916            1 :         free(data);
    3917            1 :         libspdm_responder_data_sign(
    3918              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    3919              :             spdm_context,
    3920              : #endif
    3921            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    3922              :                 SPDM_KEY_EXCHANGE_RSP,
    3923              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    3924            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    3925              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    3926              :                 &signature_size);
    3927            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    3928              :                          sizeof(m_libspdm_local_buffer)
    3929            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    3930              :                             m_libspdm_local_buffer),
    3931              :                          ptr, signature_size);
    3932            1 :         m_libspdm_local_buffer_size += signature_size;
    3933            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    3934            1 :         ptr += signature_size;
    3935            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3936              :                          libspdm_get_managed_buffer_size(&th_curr),
    3937              :                          th_curr_hash_data);
    3938            1 :         bin_str0_size = sizeof(bin_str0);
    3939            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3940              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    3941            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    3942              :                            &bin_str0_size);
    3943            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    3944              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    3945            1 :         bin_str2_size = sizeof(bin_str2);
    3946            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3947              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    3948            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    3949              :                            bin_str2, &bin_str2_size);
    3950            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    3951              :                             bin_str2, bin_str2_size,
    3952              :                             response_handshake_secret, hash_size);
    3953            1 :         bin_str7_size = sizeof(bin_str7);
    3954            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    3955              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    3956            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    3957              :                            &bin_str7_size);
    3958            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    3959              :                             hash_size, bin_str7, bin_str7_size,
    3960              :                             response_finished_key, hash_size);
    3961            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    3962              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    3963            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    3964              :                          response_finished_key, hash_size, ptr);
    3965            1 :         ptr += hmac_size;
    3966              : 
    3967            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    3968              :                                               false, spdm_response_size,
    3969              :                                               spdm_response, response_size,
    3970              :                                               response);
    3971              :     }
    3972            1 :         return LIBSPDM_STATUS_SUCCESS;
    3973              : 
    3974            1 :     case 0x1B: {
    3975              :         spdm_key_exchange_response_t *spdm_response;
    3976              :         size_t dhe_key_size;
    3977              :         uint32_t hash_size;
    3978              :         size_t signature_size;
    3979              :         uint32_t hmac_size;
    3980              :         uint8_t *ptr;
    3981              :         void *dhe_context;
    3982              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    3983              :         size_t final_key_size;
    3984              :         size_t opaque_key_exchange_rsp_size;
    3985              :         void *data;
    3986              :         size_t data_size;
    3987              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    3988              :         uint8_t *cert_buffer;
    3989              :         size_t cert_buffer_size;
    3990              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    3991              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    3992              :         uint8_t bin_str0[128];
    3993              :         size_t bin_str0_size;
    3994              :         uint8_t bin_str2[128];
    3995              :         size_t bin_str2_size;
    3996              :         uint8_t bin_str7[128];
    3997              :         size_t bin_str7_size;
    3998              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    3999              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4000              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4001              :         size_t spdm_response_size;
    4002              :         size_t transport_header_size;
    4003              : 
    4004              :         ((libspdm_context_t *)spdm_context)
    4005            1 :         ->connection_info.algorithm.base_asym_algo =
    4006              :             m_libspdm_use_asym_algo;
    4007              :         ((libspdm_context_t *)spdm_context)
    4008            1 :         ->connection_info.algorithm.base_hash_algo =
    4009              :             m_libspdm_use_hash_algo;
    4010              :         ((libspdm_context_t *)spdm_context)
    4011            1 :         ->connection_info.algorithm.dhe_named_group =
    4012              :             m_libspdm_use_dhe_algo;
    4013              :         ((libspdm_context_t *)spdm_context)
    4014            1 :         ->connection_info.algorithm.measurement_hash_algo =
    4015              :             m_libspdm_use_measurement_hash_algo;
    4016            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4017            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4018            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4019            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4020            1 :         opaque_key_exchange_rsp_size =
    4021            1 :             libspdm_get_opaque_data_version_selection_data_size(
    4022              :                 spdm_context);
    4023            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4024            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4025            1 :                              opaque_key_exchange_rsp_size + signature_size +
    4026              :                              hmac_size;
    4027            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4028            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4029            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    4030              : 
    4031            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    4032            1 :         spdm_response->header.request_response_code =
    4033              :             SPDM_KEY_EXCHANGE_RSP;
    4034            1 :         spdm_response->header.param1 = 0;
    4035            1 :         spdm_response->rsp_session_id =
    4036            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    4037            1 :         spdm_response->mut_auth_requested =
    4038              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED |
    4039              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
    4040            1 :         spdm_response->req_slot_id_param = 0xF;
    4041            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    4042            1 :                                   spdm_response->random_data);
    4043            1 :         ptr = (void *)(spdm_response + 1);
    4044            1 :         dhe_context = libspdm_dhe_new(
    4045            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4046              :                 m_libspdm_use_dhe_algo,
    4047              :                 true);
    4048            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    4049              :                                  &dhe_key_size);
    4050            1 :         final_key_size = sizeof(final_key);
    4051            1 :         libspdm_dhe_compute_key(
    4052              :             m_libspdm_use_dhe_algo, dhe_context,
    4053            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    4054              :             sizeof(spdm_key_exchange_request_t),
    4055              :             dhe_key_size, final_key, &final_key_size);
    4056            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4057            1 :         ptr += dhe_key_size;
    4058              :         /* libspdm_zero_mem (ptr, hash_size);
    4059              :          * ptr += hash_size;*/
    4060            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4061            1 :         ptr += sizeof(uint16_t);
    4062            1 :         libspdm_build_opaque_data_version_selection_data(
    4063              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    4064            1 :         ptr += opaque_key_exchange_rsp_size;
    4065            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4066              :                                                         m_libspdm_use_asym_algo, &data,
    4067              :                                                         &data_size, NULL, NULL);
    4068            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4069              :                          sizeof(m_libspdm_local_buffer)
    4070            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4071              :                             m_libspdm_local_buffer),
    4072            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4073            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4074            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    4075              :                        m_libspdm_local_buffer_size));
    4076            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4077            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4078            1 :         cert_buffer =  (uint8_t *)data;
    4079            1 :         cert_buffer_size = data_size;
    4080            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4081              :                          cert_buffer_hash);
    4082              :         /* transcript.message_a size is 0*/
    4083            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4084            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4085              :                                       m_libspdm_local_buffer_size);
    4086            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4087              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4088            1 :         free(data);
    4089            1 :         libspdm_responder_data_sign(
    4090              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    4091              :             spdm_context,
    4092              : #endif
    4093            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4094              :                 SPDM_KEY_EXCHANGE_RSP,
    4095              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4096            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    4097              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    4098              :                 &signature_size);
    4099            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4100              :                          sizeof(m_libspdm_local_buffer)
    4101            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4102              :                             m_libspdm_local_buffer),
    4103              :                          ptr, signature_size);
    4104            1 :         m_libspdm_local_buffer_size += signature_size;
    4105            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4106            1 :         ptr += signature_size;
    4107            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4108              :                          libspdm_get_managed_buffer_size(&th_curr),
    4109              :                          th_curr_hash_data);
    4110            1 :         bin_str0_size = sizeof(bin_str0);
    4111            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4112              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4113            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    4114              :                            &bin_str0_size);
    4115            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4116              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4117            1 :         bin_str2_size = sizeof(bin_str2);
    4118            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4119              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4120            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4121              :                            bin_str2, &bin_str2_size);
    4122            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4123              :                             bin_str2, bin_str2_size,
    4124              :                             response_handshake_secret, hash_size);
    4125            1 :         bin_str7_size = sizeof(bin_str7);
    4126            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4127              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4128            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4129              :                            &bin_str7_size);
    4130            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4131              :                             hash_size, bin_str7, bin_str7_size,
    4132              :                             response_finished_key, hash_size);
    4133            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4134              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4135            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4136              :                          response_finished_key, hash_size, ptr);
    4137            1 :         ptr += hmac_size;
    4138              : 
    4139            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4140              :                                               false, spdm_response_size,
    4141              :                                               spdm_response, response_size,
    4142              :                                               response);
    4143              :     }
    4144            1 :         return LIBSPDM_STATUS_SUCCESS;
    4145              : 
    4146            1 :     case 0x1C: {
    4147              :         spdm_key_exchange_response_t *spdm_response;
    4148              :         size_t dhe_key_size;
    4149              :         uint32_t hash_size;
    4150              :         size_t signature_size;
    4151              :         uint32_t hmac_size;
    4152              :         uint8_t *ptr;
    4153              :         void *dhe_context;
    4154              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4155              :         size_t final_key_size;
    4156              :         size_t opaque_key_exchange_rsp_size;
    4157              :         void *data;
    4158              :         size_t data_size;
    4159              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4160              :         uint8_t *cert_buffer;
    4161              :         size_t cert_buffer_size;
    4162              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4163              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4164              :         uint8_t bin_str0[128];
    4165              :         size_t bin_str0_size;
    4166              :         uint8_t bin_str2[128];
    4167              :         size_t bin_str2_size;
    4168              :         uint8_t bin_str7[128];
    4169              :         size_t bin_str7_size;
    4170              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4171              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4172              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4173              :         size_t spdm_response_size;
    4174              :         size_t transport_header_size;
    4175              : 
    4176              :         ((libspdm_context_t *)spdm_context)
    4177            1 :         ->connection_info.algorithm.base_asym_algo =
    4178              :             m_libspdm_use_asym_algo;
    4179              :         ((libspdm_context_t *)spdm_context)
    4180            1 :         ->connection_info.algorithm.base_hash_algo =
    4181              :             m_libspdm_use_hash_algo;
    4182              :         ((libspdm_context_t *)spdm_context)
    4183            1 :         ->connection_info.algorithm.dhe_named_group =
    4184              :             m_libspdm_use_dhe_algo;
    4185              :         ((libspdm_context_t *)spdm_context)
    4186            1 :         ->connection_info.algorithm.measurement_hash_algo =
    4187              :             m_libspdm_use_measurement_hash_algo;
    4188            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4189            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4190            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4191            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4192            1 :         opaque_key_exchange_rsp_size =
    4193            1 :             libspdm_get_opaque_data_version_selection_data_size(
    4194              :                 spdm_context);
    4195            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4196            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4197            1 :                              opaque_key_exchange_rsp_size + signature_size +
    4198              :                              hmac_size;
    4199            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4200            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4201            1 :         libspdm_zero_mem(spdm_response,spdm_response_size);
    4202              : 
    4203            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    4204            1 :         spdm_response->header.request_response_code =
    4205              :             SPDM_KEY_EXCHANGE_RSP;
    4206            1 :         spdm_response->header.param1 = 0;
    4207            1 :         spdm_response->rsp_session_id =
    4208            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    4209            1 :         spdm_response->mut_auth_requested =
    4210              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST |
    4211              :             SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
    4212            1 :         spdm_response->req_slot_id_param = 0xF;
    4213            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    4214            1 :                                   spdm_response->random_data);
    4215            1 :         ptr = (void *)(spdm_response + 1);
    4216            1 :         dhe_context = libspdm_dhe_new(
    4217            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4218              :                 m_libspdm_use_dhe_algo,
    4219              :                 true);
    4220            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    4221              :                                  &dhe_key_size);
    4222            1 :         final_key_size = sizeof(final_key);
    4223            1 :         libspdm_dhe_compute_key(
    4224              :             m_libspdm_use_dhe_algo, dhe_context,
    4225            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    4226              :             sizeof(spdm_key_exchange_request_t),
    4227              :             dhe_key_size, final_key, &final_key_size);
    4228            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4229            1 :         ptr += dhe_key_size;
    4230              :         /* libspdm_zero_mem (ptr, hash_size);
    4231              :          * ptr += hash_size;*/
    4232            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4233            1 :         ptr += sizeof(uint16_t);
    4234            1 :         libspdm_build_opaque_data_version_selection_data(
    4235              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    4236            1 :         ptr += opaque_key_exchange_rsp_size;
    4237            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4238              :                                                         m_libspdm_use_asym_algo, &data,
    4239              :                                                         &data_size, NULL, NULL);
    4240            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4241              :                          sizeof(m_libspdm_local_buffer)
    4242            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4243              :                             m_libspdm_local_buffer),
    4244            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4245            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4246            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    4247              :                        m_libspdm_local_buffer_size));
    4248            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4249            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4250            1 :         cert_buffer =  (uint8_t *)data;
    4251            1 :         cert_buffer_size = data_size;
    4252            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4253              :                          cert_buffer_hash);
    4254              :         /* transcript.message_a size is 0*/
    4255            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4256            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4257              :                                       m_libspdm_local_buffer_size);
    4258            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4259              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4260            1 :         free(data);
    4261            1 :         libspdm_responder_data_sign(
    4262              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    4263              :             spdm_context,
    4264              : #endif
    4265            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4266              :                 SPDM_KEY_EXCHANGE_RSP,
    4267              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4268            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    4269              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    4270              :                 &signature_size);
    4271            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4272              :                          sizeof(m_libspdm_local_buffer)
    4273            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4274              :                             m_libspdm_local_buffer),
    4275              :                          ptr, signature_size);
    4276            1 :         m_libspdm_local_buffer_size += signature_size;
    4277            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4278            1 :         ptr += signature_size;
    4279            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4280              :                          libspdm_get_managed_buffer_size(&th_curr),
    4281              :                          th_curr_hash_data);
    4282            1 :         bin_str0_size = sizeof(bin_str0);
    4283            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4284              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4285            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    4286              :                            &bin_str0_size);
    4287            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4288              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4289            1 :         bin_str2_size = sizeof(bin_str2);
    4290            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4291              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4292            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4293              :                            bin_str2, &bin_str2_size);
    4294            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4295              :                             bin_str2, bin_str2_size,
    4296              :                             response_handshake_secret, hash_size);
    4297            1 :         bin_str7_size = sizeof(bin_str7);
    4298            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4299              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4300            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4301              :                            &bin_str7_size);
    4302            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4303              :                             hash_size, bin_str7, bin_str7_size,
    4304              :                             response_finished_key, hash_size);
    4305            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4306              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4307            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4308              :                          response_finished_key, hash_size, ptr);
    4309            1 :         ptr += hmac_size;
    4310              : 
    4311            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4312              :                                               false, spdm_response_size,
    4313              :                                               spdm_response, response_size,
    4314              :                                               response);
    4315              :     }
    4316            1 :         return LIBSPDM_STATUS_SUCCESS;
    4317              : 
    4318            1 :     case 0x1D: {
    4319              :         spdm_key_exchange_response_t *spdm_response;
    4320              :         size_t dhe_key_size;
    4321              :         uint32_t hash_size;
    4322              :         size_t signature_size;
    4323              :         uint32_t hmac_size;
    4324              :         uint8_t *ptr;
    4325              :         void *dhe_context;
    4326              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4327              :         size_t final_key_size;
    4328              :         size_t opaque_key_exchange_rsp_size;
    4329              :         void *data;
    4330              :         size_t data_size;
    4331              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4332              :         uint8_t *cert_buffer;
    4333              :         size_t cert_buffer_size;
    4334              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4335              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4336              :         uint8_t bin_str0[128];
    4337              :         size_t bin_str0_size;
    4338              :         uint8_t bin_str2[128];
    4339              :         size_t bin_str2_size;
    4340              :         uint8_t bin_str7[128];
    4341              :         size_t bin_str7_size;
    4342              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4343              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4344              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4345              :         size_t spdm_response_size;
    4346              :         size_t transport_header_size;
    4347              : 
    4348            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_asym_algo =
    4349              :             m_libspdm_use_asym_algo;
    4350            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_hash_algo =
    4351              :             m_libspdm_use_hash_algo;
    4352            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.dhe_named_group =
    4353              :             m_libspdm_use_dhe_algo;
    4354            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.measurement_hash_algo =
    4355              :             m_libspdm_use_measurement_hash_algo;
    4356            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4357            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4358            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4359            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4360            1 :         opaque_key_exchange_rsp_size =
    4361            1 :             libspdm_get_opaque_data_version_selection_data_size(spdm_context);
    4362            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4363            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4364            1 :                              opaque_key_exchange_rsp_size + signature_size +
    4365              :                              hmac_size;
    4366            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4367            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4368              : 
    4369            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    4370            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    4371            1 :         spdm_response->header.param1 = 0;
    4372            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    4373            1 :         spdm_response->mut_auth_requested = 0;
    4374            1 :         spdm_response->req_slot_id_param = 0;
    4375            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
    4376            1 :         ptr = (void *)(spdm_response + 1);
    4377            1 :         dhe_context = libspdm_dhe_new(
    4378            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4379              :                 m_libspdm_use_dhe_algo, true);
    4380            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    4381            1 :         final_key_size = sizeof(final_key);
    4382            1 :         libspdm_dhe_compute_key(
    4383              :             m_libspdm_use_dhe_algo, dhe_context,
    4384            1 :             (uint8_t *)&m_libspdm_local_buffer[0] + sizeof(spdm_key_exchange_request_t),
    4385              :             dhe_key_size, final_key, &final_key_size);
    4386            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4387            1 :         ptr += dhe_key_size;
    4388              :         /* libspdm_zero_mem (ptr, hash_size);
    4389              :          * ptr += hash_size;*/
    4390            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4391            1 :         ptr += sizeof(uint16_t);
    4392            1 :         libspdm_build_opaque_data_version_selection_data(
    4393              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    4394            1 :         ptr += opaque_key_exchange_rsp_size;
    4395            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4396              :                                                         m_libspdm_use_asym_algo, &data,
    4397              :                                                         &data_size, NULL, NULL);
    4398            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4399              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
    4400            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4401            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4402            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer (0x%zx):\n",
    4403              :                        m_libspdm_local_buffer_size));
    4404            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4405            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4406            1 :         cert_buffer = (uint8_t *)data;
    4407            1 :         cert_buffer_size = data_size;
    4408            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4409              :                          cert_buffer_hash);
    4410              :         /* transcript.message_a size is 0*/
    4411            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4412            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4413              :                                       m_libspdm_local_buffer_size);
    4414            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4415              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4416            1 :         free(data);
    4417            1 :         libspdm_responder_data_sign(
    4418              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    4419              :             spdm_context,
    4420              : #endif
    4421            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4422              :                 SPDM_KEY_EXCHANGE_RSP,
    4423              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4424            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    4425              :                 libspdm_get_managed_buffer_size(&th_curr), ptr, &signature_size);
    4426            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4427              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
    4428              :                          ptr, signature_size);
    4429            1 :         m_libspdm_local_buffer_size += signature_size;
    4430            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4431            1 :         ptr += signature_size;
    4432            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4433              :                          libspdm_get_managed_buffer_size(&th_curr), th_curr_hash_data);
    4434            1 :         bin_str0_size = sizeof(bin_str0);
    4435            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4436              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4437            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0, &bin_str0_size);
    4438            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4439              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4440            1 :         bin_str2_size = sizeof(bin_str2);
    4441            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4442              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4443            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4444              :                            bin_str2, &bin_str2_size);
    4445            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4446              :                             bin_str2, bin_str2_size,
    4447              :                             response_handshake_secret, hash_size);
    4448            1 :         bin_str7_size = sizeof(bin_str7);
    4449            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4450              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4451            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4452              :                            &bin_str7_size);
    4453            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4454              :                             hash_size, bin_str7, bin_str7_size,
    4455              :                             response_finished_key, hash_size);
    4456            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4457              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4458            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4459              :                          response_finished_key, hash_size, ptr);
    4460            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4461              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
    4462              :                          ptr, hmac_size);
    4463            1 :         m_libspdm_local_buffer_size += hmac_size;
    4464            1 :         ptr += hmac_size;
    4465              : 
    4466            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4467              :                                               false, spdm_response_size,
    4468              :                                               spdm_response, response_size,
    4469              :                                               response);
    4470              :     }
    4471            1 :         return LIBSPDM_STATUS_SUCCESS;
    4472            1 :     case 0x1E: {
    4473              :         spdm_key_exchange_response_t *spdm_response;
    4474              :         size_t dhe_key_size;
    4475              :         uint32_t hash_size;
    4476              :         size_t signature_size;
    4477              :         uint32_t hmac_size;
    4478              :         uint8_t *ptr;
    4479              :         void *dhe_context;
    4480              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4481              :         size_t final_key_size;
    4482              :         size_t opaque_key_exchange_rsp_size;
    4483              :         void *data;
    4484              :         size_t data_size;
    4485              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4486              :         uint8_t *cert_buffer;
    4487              :         size_t cert_buffer_size;
    4488              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4489              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4490              :         uint8_t bin_str0[128];
    4491              :         size_t bin_str0_size;
    4492              :         uint8_t bin_str2[128];
    4493              :         size_t bin_str2_size;
    4494              :         uint8_t bin_str7[128];
    4495              :         size_t bin_str7_size;
    4496              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4497              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4498              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4499              :         size_t spdm_response_size;
    4500              :         size_t transport_header_size;
    4501              : 
    4502            1 :         ((libspdm_context_t *)spdm_context)->connection_info.secured_message_version =
    4503              :             SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    4504              :         ((libspdm_context_t *)spdm_context)
    4505            1 :         ->connection_info.algorithm.base_asym_algo =
    4506              :             m_libspdm_use_asym_algo;
    4507              :         ((libspdm_context_t *)spdm_context)
    4508            1 :         ->connection_info.algorithm.base_hash_algo =
    4509              :             m_libspdm_use_hash_algo;
    4510              :         ((libspdm_context_t *)spdm_context)
    4511            1 :         ->connection_info.algorithm.dhe_named_group =
    4512              :             m_libspdm_use_dhe_algo;
    4513              :         ((libspdm_context_t *)spdm_context)
    4514            1 :         ->connection_info.algorithm.measurement_hash_algo =
    4515              :             m_libspdm_use_measurement_hash_algo;
    4516            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4517            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4518            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4519            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4520            1 :         opaque_key_exchange_rsp_size =
    4521            1 :             libspdm_get_opaque_data_version_selection_data_size(
    4522              :                 spdm_context);
    4523            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4524            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4525            1 :                              opaque_key_exchange_rsp_size + signature_size +
    4526              :                              hmac_size;
    4527            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4528            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4529              : 
    4530            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
    4531            1 :         spdm_response->header.request_response_code =
    4532              :             SPDM_KEY_EXCHANGE_RSP;
    4533            1 :         spdm_response->header.param1 = 0;
    4534            1 :         spdm_response->rsp_session_id =
    4535            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    4536            1 :         spdm_response->mut_auth_requested = 0;
    4537            1 :         spdm_response->req_slot_id_param = 0;
    4538            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    4539            1 :                                   spdm_response->random_data);
    4540            1 :         ptr = (void *)(spdm_response + 1);
    4541            1 :         dhe_context = libspdm_dhe_new(
    4542            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4543              :                 m_libspdm_use_dhe_algo,
    4544              :                 true);
    4545            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    4546              :                                  &dhe_key_size);
    4547            1 :         final_key_size = sizeof(final_key);
    4548            1 :         libspdm_dhe_compute_key(
    4549              :             m_libspdm_use_dhe_algo, dhe_context,
    4550            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    4551              :             sizeof(spdm_key_exchange_request_t),
    4552              :             dhe_key_size, final_key, &final_key_size);
    4553            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4554            1 :         ptr += dhe_key_size;
    4555              :         /* libspdm_zero_mem (ptr, hash_size);
    4556              :          * ptr += hash_size;*/
    4557            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4558            1 :         ptr += sizeof(uint16_t);
    4559            1 :         libspdm_build_opaque_data_version_selection_data(
    4560              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    4561            1 :         ptr += opaque_key_exchange_rsp_size;
    4562            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4563              :                                                         m_libspdm_use_asym_algo, &data,
    4564              :                                                         &data_size, NULL, NULL);
    4565            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4566              :                          sizeof(m_libspdm_local_buffer)
    4567            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4568              :                             m_libspdm_local_buffer),
    4569            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4570            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4571            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    4572              :                        m_libspdm_local_buffer_size));
    4573            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4574            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4575            1 :         cert_buffer = (uint8_t *)data;
    4576            1 :         cert_buffer_size = data_size;
    4577            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4578              :                          cert_buffer_hash);
    4579              :         /* transcript.message_a size is 0*/
    4580            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4581            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4582              :                                       m_libspdm_local_buffer_size);
    4583            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4584              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4585            1 :         free(data);
    4586            1 :         libspdm_responder_data_sign(
    4587              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    4588              :             spdm_context,
    4589              : #endif
    4590            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4591              :                 SPDM_KEY_EXCHANGE_RSP,
    4592              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4593            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    4594              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    4595              :                 &signature_size);
    4596            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4597              :                          sizeof(m_libspdm_local_buffer)
    4598            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4599              :                             m_libspdm_local_buffer),
    4600              :                          ptr, signature_size);
    4601            1 :         m_libspdm_local_buffer_size += signature_size;
    4602            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4603            1 :         ptr += signature_size;
    4604            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4605              :                          libspdm_get_managed_buffer_size(&th_curr),
    4606              :                          th_curr_hash_data);
    4607            1 :         bin_str0_size = sizeof(bin_str0);
    4608            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4609              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4610            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    4611              :                            &bin_str0_size);
    4612            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4613              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4614            1 :         bin_str2_size = sizeof(bin_str2);
    4615            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4616              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4617            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4618              :                            bin_str2, &bin_str2_size);
    4619            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4620              :                             bin_str2, bin_str2_size,
    4621              :                             response_handshake_secret, hash_size);
    4622            1 :         bin_str7_size = sizeof(bin_str7);
    4623            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4624              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4625            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4626              :                            &bin_str7_size);
    4627            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4628              :                             hash_size, bin_str7, bin_str7_size,
    4629              :                             response_finished_key, hash_size);
    4630            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4631              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4632            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4633              :                          response_finished_key, hash_size, ptr);
    4634            1 :         ptr += hmac_size;
    4635              : 
    4636            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4637              :                                               false, spdm_response_size,
    4638              :                                               spdm_response, response_size,
    4639              :                                               response);
    4640              :     }
    4641            1 :         return LIBSPDM_STATUS_SUCCESS;
    4642              : 
    4643            1 :     case 0x1F: {
    4644              :         spdm_key_exchange_response_t *spdm_response;
    4645              :         size_t dhe_key_size;
    4646              :         uint32_t hash_size;
    4647              :         size_t signature_size;
    4648              :         uint32_t hmac_size;
    4649              :         uint8_t *ptr;
    4650              :         void *dhe_context;
    4651              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4652              :         size_t final_key_size;
    4653              :         size_t opaque_key_exchange_rsp_size;
    4654              :         void *data;
    4655              :         size_t data_size;
    4656              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4657              :         uint8_t *cert_buffer;
    4658              :         size_t cert_buffer_size;
    4659              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4660              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4661              :         uint8_t bin_str0[128];
    4662              :         size_t bin_str0_size;
    4663              :         uint8_t bin_str2[128];
    4664              :         size_t bin_str2_size;
    4665              :         uint8_t bin_str7[128];
    4666              :         size_t bin_str7_size;
    4667              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4668              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4669              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4670              :         size_t spdm_response_size;
    4671              :         size_t transport_header_size;
    4672              : 
    4673            1 :         ((libspdm_context_t *)spdm_context)->connection_info.secured_message_version =
    4674              :             SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    4675              :         ((libspdm_context_t *)spdm_context)
    4676            1 :         ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    4677              :         ((libspdm_context_t *)spdm_context)
    4678            1 :         ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    4679              :         ((libspdm_context_t *)spdm_context)
    4680            1 :         ->connection_info.algorithm.dhe_named_group =  m_libspdm_use_dhe_algo;
    4681              :         ((libspdm_context_t *)spdm_context)
    4682            1 :         ->connection_info.algorithm.measurement_hash_algo =
    4683              :             m_libspdm_use_measurement_hash_algo;
    4684            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4685            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4686            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4687            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4688            1 :         opaque_key_exchange_rsp_size =
    4689            1 :             libspdm_get_opaque_data_version_selection_data_size(spdm_context);
    4690            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4691            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4692            1 :                              opaque_key_exchange_rsp_size + signature_size +
    4693              :                              hmac_size;
    4694            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4695            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4696              : 
    4697            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    4698            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    4699            1 :         spdm_response->header.param1 = 0;
    4700            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    4701            1 :         spdm_response->mut_auth_requested = 0;
    4702            1 :         spdm_response->req_slot_id_param = 0;
    4703            1 :         memset(spdm_response->random_data, 0x5c, SPDM_RANDOM_DATA_SIZE);
    4704            1 :         ptr = (void *)(spdm_response + 1);
    4705            1 :         dhe_context = libspdm_dhe_new(
    4706            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4707              :                 m_libspdm_use_dhe_algo, true);
    4708            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    4709            1 :         final_key_size = sizeof(final_key);
    4710            1 :         libspdm_dhe_compute_key(
    4711              :             m_libspdm_use_dhe_algo, dhe_context,
    4712            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    4713              :             sizeof(spdm_key_exchange_request_t),
    4714              :             dhe_key_size, final_key, &final_key_size);
    4715            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4716            1 :         ptr += dhe_key_size;
    4717              :         /* libspdm_zero_mem (ptr, hash_size);
    4718              :          * ptr += hash_size;*/
    4719            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4720            1 :         ptr += sizeof(uint16_t);
    4721            1 :         libspdm_build_opaque_data_version_selection_data(
    4722              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    4723            1 :         ptr += opaque_key_exchange_rsp_size;
    4724            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    4725              :                                                         m_libspdm_use_asym_algo, &data,
    4726              :                                                         &data_size, NULL, NULL);
    4727            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4728              :                          sizeof(m_libspdm_local_buffer)
    4729            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4730              :                             m_libspdm_local_buffer),
    4731            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4732            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4733            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    4734              :                        m_libspdm_local_buffer_size));
    4735            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4736            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4737            1 :         cert_buffer = (uint8_t *)data;
    4738            1 :         cert_buffer_size = data_size;
    4739            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
    4740              :         /* transcript.message_a size is 0*/
    4741            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4742            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4743              :                                       m_libspdm_local_buffer_size);
    4744            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4745              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4746            1 :         free(data);
    4747            1 :         libspdm_responder_data_sign(
    4748              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    4749              :             spdm_context,
    4750              : #endif
    4751            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4752              :                 SPDM_KEY_EXCHANGE_RSP,
    4753              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4754            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    4755              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    4756              :                 &signature_size);
    4757            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4758              :                          sizeof(m_libspdm_local_buffer)
    4759            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4760              :                             m_libspdm_local_buffer),
    4761              :                          ptr, signature_size);
    4762            1 :         m_libspdm_local_buffer_size += signature_size;
    4763            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4764            1 :         ptr += signature_size;
    4765            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4766              :                          libspdm_get_managed_buffer_size(&th_curr),
    4767              :                          th_curr_hash_data);
    4768            1 :         bin_str0_size = sizeof(bin_str0);
    4769            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4770              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4771            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    4772              :                            &bin_str0_size);
    4773            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4774              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4775            1 :         bin_str2_size = sizeof(bin_str2);
    4776            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4777              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4778            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4779              :                            bin_str2, &bin_str2_size);
    4780            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4781              :                             bin_str2, bin_str2_size,
    4782              :                             response_handshake_secret, hash_size);
    4783            1 :         bin_str7_size = sizeof(bin_str7);
    4784            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4785              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4786            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4787              :                            &bin_str7_size);
    4788            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4789              :                             hash_size, bin_str7, bin_str7_size,
    4790              :                             response_finished_key, hash_size);
    4791            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4792              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4793            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4794              :                          response_finished_key, hash_size, ptr);
    4795            1 :         ptr += hmac_size;
    4796              : 
    4797            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4798              :                                               false, spdm_response_size,
    4799              :                                               spdm_response, response_size,
    4800              :                                               response);
    4801              :     }
    4802            1 :         return LIBSPDM_STATUS_SUCCESS;
    4803              : 
    4804            1 :     case 0x20: {
    4805              :         spdm_key_exchange_response_t *spdm_response;
    4806              :         size_t dhe_key_size;
    4807              :         uint32_t hash_size;
    4808              :         size_t signature_size;
    4809              :         uint32_t hmac_size;
    4810              :         uint8_t *ptr;
    4811              :         void *dhe_context;
    4812              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4813              :         size_t final_key_size;
    4814              :         size_t opaque_key_exchange_rsp_size;
    4815              :         void *data;
    4816              :         size_t data_size;
    4817              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4818              :         uint8_t *cert_buffer;
    4819              :         size_t cert_buffer_size;
    4820              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4821              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4822              :         uint8_t bin_str0[128];
    4823              :         size_t bin_str0_size;
    4824              :         uint8_t bin_str2[128];
    4825              :         size_t bin_str2_size;
    4826              :         uint8_t bin_str7[128];
    4827              :         size_t bin_str7_size;
    4828              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4829              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4830              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4831              :         size_t spdm_response_size;
    4832              :         size_t transport_header_size;
    4833              : 
    4834            1 :         ((libspdm_context_t *)spdm_context)->connection_info.secured_message_version =
    4835              :             SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    4836              :         ((libspdm_context_t *)spdm_context)
    4837            1 :         ->connection_info.algorithm.base_asym_algo =
    4838              :             m_libspdm_use_asym_algo;
    4839              :         ((libspdm_context_t *)spdm_context)
    4840            1 :         ->connection_info.algorithm.base_hash_algo =
    4841              :             m_libspdm_use_hash_algo;
    4842              :         ((libspdm_context_t *)spdm_context)
    4843            1 :         ->connection_info.algorithm.dhe_named_group =
    4844              :             m_libspdm_use_dhe_algo;
    4845              :         ((libspdm_context_t *)spdm_context)
    4846            1 :         ->connection_info.algorithm.measurement_hash_algo =
    4847              :             m_libspdm_use_measurement_hash_algo;
    4848            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    4849            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4850            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4851            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    4852            1 :         opaque_key_exchange_rsp_size =
    4853            1 :             libspdm_get_opaque_data_version_selection_data_size(
    4854              :                 spdm_context);
    4855            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    4856            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    4857            1 :                              opaque_key_exchange_rsp_size + signature_size +
    4858              :                              hmac_size;
    4859            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    4860            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    4861              : 
    4862            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
    4863            1 :         spdm_response->header.request_response_code =
    4864              :             SPDM_KEY_EXCHANGE_RSP;
    4865            1 :         spdm_response->header.param1 = 0;
    4866            1 :         spdm_response->rsp_session_id =
    4867            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    4868            1 :         spdm_response->mut_auth_requested = 0;
    4869            1 :         spdm_response->req_slot_id_param = 0;
    4870            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    4871            1 :                                   spdm_response->random_data);
    4872            1 :         ptr = (void *)(spdm_response + 1);
    4873            1 :         dhe_context = libspdm_dhe_new(
    4874            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4875              :                 m_libspdm_use_dhe_algo,
    4876              :                 true);
    4877            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    4878              :                                  &dhe_key_size);
    4879            1 :         final_key_size = sizeof(final_key);
    4880            1 :         libspdm_dhe_compute_key(
    4881              :             m_libspdm_use_dhe_algo, dhe_context,
    4882            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    4883              :             sizeof(spdm_key_exchange_request_t),
    4884              :             dhe_key_size, final_key, &final_key_size);
    4885            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    4886            1 :         ptr += dhe_key_size;
    4887              :         /* libspdm_zero_mem (ptr, hash_size);
    4888              :          * ptr += hash_size;*/
    4889            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    4890            1 :         ptr += sizeof(uint16_t);
    4891            1 :         libspdm_build_opaque_data_version_selection_data(
    4892              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    4893            1 :         ptr += opaque_key_exchange_rsp_size;
    4894            1 :         libspdm_read_responder_public_key(m_libspdm_use_asym_algo, &data, &data_size);
    4895            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4896              :                          sizeof(m_libspdm_local_buffer)
    4897            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4898              :                             m_libspdm_local_buffer),
    4899            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    4900            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    4901            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    4902              :                        m_libspdm_local_buffer_size));
    4903            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    4904            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    4905            1 :         cert_buffer = (uint8_t *)data;
    4906            1 :         cert_buffer_size = data_size;
    4907            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    4908              :                          cert_buffer_hash);
    4909              :         /* transcript.message_a size is 0*/
    4910            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    4911            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    4912              :                                       m_libspdm_local_buffer_size);
    4913            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4914              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4915            1 :         free(data);
    4916            1 :         libspdm_responder_data_sign(
    4917              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    4918              :             spdm_context,
    4919              : #endif
    4920            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    4921              :                 SPDM_KEY_EXCHANGE_RSP,
    4922              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    4923            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    4924              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    4925              :                 &signature_size);
    4926            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    4927              :                          sizeof(m_libspdm_local_buffer)
    4928            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    4929              :                             m_libspdm_local_buffer),
    4930              :                          ptr, signature_size);
    4931            1 :         m_libspdm_local_buffer_size += signature_size;
    4932            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    4933            1 :         ptr += signature_size;
    4934            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4935              :                          libspdm_get_managed_buffer_size(&th_curr),
    4936              :                          th_curr_hash_data);
    4937            1 :         bin_str0_size = sizeof(bin_str0);
    4938            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4939              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    4940            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    4941              :                            &bin_str0_size);
    4942            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    4943              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    4944            1 :         bin_str2_size = sizeof(bin_str2);
    4945            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4946              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    4947            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    4948              :                            bin_str2, &bin_str2_size);
    4949            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    4950              :                             bin_str2, bin_str2_size,
    4951              :                             response_handshake_secret, hash_size);
    4952            1 :         bin_str7_size = sizeof(bin_str7);
    4953            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    4954              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    4955            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    4956              :                            &bin_str7_size);
    4957            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    4958              :                             hash_size, bin_str7, bin_str7_size,
    4959              :                             response_finished_key, hash_size);
    4960            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    4961              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    4962            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    4963              :                          response_finished_key, hash_size, ptr);
    4964            1 :         ptr += hmac_size;
    4965              : 
    4966            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    4967              :                                               false, spdm_response_size,
    4968              :                                               spdm_response, response_size,
    4969              :                                               response);
    4970              :     }
    4971            1 :         return LIBSPDM_STATUS_SUCCESS;
    4972            1 :     case 0x21: {
    4973              :         spdm_key_exchange_response_t *spdm_response;
    4974              :         size_t dhe_key_size;
    4975              :         uint32_t hash_size;
    4976              :         size_t signature_size;
    4977              :         uint32_t hmac_size;
    4978              :         uint8_t *ptr;
    4979              :         void *dhe_context;
    4980              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    4981              :         size_t final_key_size;
    4982              :         size_t opaque_key_exchange_rsp_size;
    4983              :         void *data;
    4984              :         size_t data_size;
    4985              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    4986              :         uint8_t *cert_buffer;
    4987              :         size_t cert_buffer_size;
    4988              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    4989              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    4990              :         uint8_t bin_str0[128];
    4991              :         size_t bin_str0_size;
    4992              :         uint8_t bin_str2[128];
    4993              :         size_t bin_str2_size;
    4994              :         uint8_t bin_str7[128];
    4995              :         size_t bin_str7_size;
    4996              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4997              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    4998              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    4999              :         size_t spdm_response_size;
    5000              :         size_t transport_header_size;
    5001              : 
    5002            1 :         ((libspdm_context_t *)spdm_context)->connection_info.secured_message_version =
    5003              :             SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    5004              :         ((libspdm_context_t *)spdm_context)
    5005            1 :         ->connection_info.algorithm.base_asym_algo =
    5006              :             m_libspdm_use_asym_algo;
    5007              :         ((libspdm_context_t *)spdm_context)
    5008            1 :         ->connection_info.algorithm.base_hash_algo =
    5009              :             m_libspdm_use_hash_algo;
    5010              :         ((libspdm_context_t *)spdm_context)
    5011            1 :         ->connection_info.algorithm.dhe_named_group =
    5012              :             m_libspdm_use_dhe_algo;
    5013              :         ((libspdm_context_t *)spdm_context)
    5014            1 :         ->connection_info.algorithm.measurement_hash_algo =
    5015              :             m_libspdm_use_measurement_hash_algo;
    5016            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    5017            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    5018            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    5019            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    5020            1 :         opaque_key_exchange_rsp_size =
    5021            1 :             libspdm_get_opaque_data_version_selection_data_size(
    5022              :                 spdm_context);
    5023            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    5024            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    5025            1 :                              opaque_key_exchange_rsp_size + signature_size +
    5026              :                              hmac_size;
    5027            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    5028            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    5029              : 
    5030            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
    5031            1 :         spdm_response->header.request_response_code =
    5032              :             SPDM_KEY_EXCHANGE_RSP;
    5033            1 :         spdm_response->header.param1 = 0;
    5034            1 :         spdm_response->rsp_session_id =
    5035            1 :             libspdm_allocate_rsp_session_id(spdm_context, false);
    5036            1 :         spdm_response->mut_auth_requested = 0;
    5037            1 :         spdm_response->req_slot_id_param = 0;
    5038            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
    5039            1 :                                   spdm_response->random_data);
    5040            1 :         ptr = (void *)(spdm_response + 1);
    5041            1 :         dhe_context = libspdm_dhe_new(
    5042            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    5043              :                 m_libspdm_use_dhe_algo,
    5044              :                 true);
    5045            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr,
    5046              :                                  &dhe_key_size);
    5047            1 :         final_key_size = sizeof(final_key);
    5048            1 :         libspdm_dhe_compute_key(
    5049              :             m_libspdm_use_dhe_algo, dhe_context,
    5050            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    5051              :             sizeof(spdm_key_exchange_request_t),
    5052              :             dhe_key_size, final_key, &final_key_size);
    5053            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    5054            1 :         ptr += dhe_key_size;
    5055              :         /* libspdm_zero_mem (ptr, hash_size);
    5056              :          * ptr += hash_size;*/
    5057            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    5058            1 :         ptr += sizeof(uint16_t);
    5059            1 :         libspdm_build_opaque_data_version_selection_data(
    5060              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    5061            1 :         ptr += opaque_key_exchange_rsp_size;
    5062            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5063              :                                                         m_libspdm_use_asym_algo, &data,
    5064              :                                                         &data_size, NULL, NULL);
    5065            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    5066              :                          sizeof(m_libspdm_local_buffer)
    5067            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    5068              :                             m_libspdm_local_buffer),
    5069            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    5070            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    5071            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    5072              :                        m_libspdm_local_buffer_size));
    5073            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    5074            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    5075            1 :         cert_buffer = (uint8_t *)data;
    5076            1 :         cert_buffer_size = data_size;
    5077            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    5078              :                          cert_buffer_hash);
    5079              :         /* transcript.message_a size is 0*/
    5080            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    5081            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    5082              :                                       m_libspdm_local_buffer_size);
    5083            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    5084              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    5085            1 :         free(data);
    5086            1 :         libspdm_responder_data_sign(
    5087              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    5088              :             spdm_context,
    5089              : #endif
    5090            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    5091              :                 SPDM_KEY_EXCHANGE_RSP,
    5092              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    5093            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    5094              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    5095              :                 &signature_size);
    5096            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    5097              :                          sizeof(m_libspdm_local_buffer)
    5098            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    5099              :                             m_libspdm_local_buffer),
    5100              :                          ptr, signature_size);
    5101            1 :         m_libspdm_local_buffer_size += signature_size;
    5102            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    5103            1 :         ptr += signature_size;
    5104            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    5105              :                          libspdm_get_managed_buffer_size(&th_curr),
    5106              :                          th_curr_hash_data);
    5107            1 :         bin_str0_size = sizeof(bin_str0);
    5108            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    5109              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    5110            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    5111              :                            &bin_str0_size);
    5112            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    5113              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    5114            1 :         bin_str2_size = sizeof(bin_str2);
    5115            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    5116              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    5117            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    5118              :                            bin_str2, &bin_str2_size);
    5119            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    5120              :                             bin_str2, bin_str2_size,
    5121              :                             response_handshake_secret, hash_size);
    5122            1 :         bin_str7_size = sizeof(bin_str7);
    5123            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    5124              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    5125            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    5126              :                            &bin_str7_size);
    5127            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    5128              :                             hash_size, bin_str7, bin_str7_size,
    5129              :                             response_finished_key, hash_size);
    5130            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    5131              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    5132            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    5133              :                          response_finished_key, hash_size, ptr);
    5134            1 :         ptr += hmac_size;
    5135              : 
    5136            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    5137              :                                               false, spdm_response_size,
    5138              :                                               spdm_response, response_size,
    5139              :                                               response);
    5140              :     }
    5141            1 :         return LIBSPDM_STATUS_SUCCESS;
    5142            1 :     case 0x22: {
    5143              :         spdm_key_exchange_response_t *spdm_response;
    5144              :         size_t dhe_key_size;
    5145              :         uint32_t hash_size;
    5146              :         size_t signature_size;
    5147              :         uint32_t hmac_size;
    5148              :         uint8_t *ptr;
    5149              :         void *dhe_context;
    5150              :         uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
    5151              :         size_t final_key_size;
    5152              :         size_t opaque_key_exchange_rsp_size;
    5153              :         void *data;
    5154              :         size_t data_size;
    5155              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    5156              :         uint8_t *cert_buffer;
    5157              :         size_t cert_buffer_size;
    5158              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    5159              :         uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
    5160              :         uint8_t bin_str0[128];
    5161              :         size_t bin_str0_size;
    5162              :         uint8_t bin_str2[128];
    5163              :         size_t bin_str2_size;
    5164              :         uint8_t bin_str7[128];
    5165              :         size_t bin_str7_size;
    5166              :         uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    5167              :         uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
    5168              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    5169              :         size_t spdm_response_size;
    5170              :         size_t transport_header_size;
    5171              : 
    5172            1 :         ((libspdm_context_t *)spdm_context)->connection_info.secured_message_version =
    5173              :             SECURED_SPDM_VERSION_12 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    5174              :         ((libspdm_context_t *)spdm_context)
    5175            1 :         ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    5176              :         ((libspdm_context_t *)spdm_context)
    5177            1 :         ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    5178              :         ((libspdm_context_t *)spdm_context)
    5179            1 :         ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    5180              :         ((libspdm_context_t *)spdm_context)
    5181            1 :         ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
    5182            1 :         signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
    5183            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    5184            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    5185            1 :         dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
    5186            1 :         opaque_key_exchange_rsp_size =
    5187            1 :             libspdm_get_opaque_data_version_selection_data_size(spdm_context);
    5188            1 :         spdm_response_size = sizeof(spdm_key_exchange_response_t) +
    5189            1 :                              dhe_key_size + 0 + sizeof(uint16_t) +
    5190            1 :                              opaque_key_exchange_rsp_size + signature_size +
    5191              :                              hmac_size;
    5192            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    5193            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    5194              : 
    5195            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    5196            1 :         spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
    5197            1 :         spdm_response->header.param1 = 0;
    5198            1 :         spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
    5199            1 :         spdm_response->mut_auth_requested = 0;
    5200            1 :         spdm_response->req_slot_id_param = 0;
    5201            1 :         libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
    5202            1 :         ptr = (void *)(spdm_response + 1);
    5203            1 :         dhe_context = libspdm_dhe_new(
    5204            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    5205              :                 m_libspdm_use_dhe_algo, true);
    5206            1 :         libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
    5207            1 :         final_key_size = sizeof(final_key);
    5208            1 :         libspdm_dhe_compute_key(
    5209              :             m_libspdm_use_dhe_algo, dhe_context,
    5210            1 :             (uint8_t *)&m_libspdm_local_buffer[0] +
    5211              :             sizeof(spdm_key_exchange_request_t),
    5212              :             dhe_key_size, final_key, &final_key_size);
    5213            1 :         libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
    5214            1 :         ptr += dhe_key_size;
    5215              :         /* libspdm_zero_mem (ptr, hash_size);
    5216              :          * ptr += hash_size;*/
    5217            1 :         *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_rsp_size;
    5218            1 :         ptr += sizeof(uint16_t);
    5219            1 :         libspdm_build_opaque_data_version_selection_data(
    5220              :             spdm_context, &opaque_key_exchange_rsp_size, ptr);
    5221            1 :         ptr += opaque_key_exchange_rsp_size;
    5222            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5223              :                                                         m_libspdm_use_asym_algo, &data,
    5224              :                                                         &data_size, NULL, NULL);
    5225            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    5226              :                          sizeof(m_libspdm_local_buffer)
    5227            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    5228              :                             m_libspdm_local_buffer),
    5229            1 :                          spdm_response, (size_t)ptr - (size_t)spdm_response);
    5230            1 :         m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
    5231            1 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
    5232              :                        m_libspdm_local_buffer_size));
    5233            1 :         libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    5234            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    5235            1 :         cert_buffer = (uint8_t *)data;
    5236            1 :         cert_buffer_size = data_size;
    5237            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    5238              :                          cert_buffer_hash);
    5239              :         /* transcript.message_a size is 0*/
    5240            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    5241            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    5242              :                                       m_libspdm_local_buffer_size);
    5243            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    5244              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    5245            1 :         free(data);
    5246            1 :         libspdm_responder_data_sign(
    5247              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
    5248              :             spdm_context,
    5249              : #endif
    5250            1 :             spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
    5251              :                 SPDM_KEY_EXCHANGE_RSP,
    5252              :                 m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
    5253            1 :                 false, libspdm_get_managed_buffer(&th_curr),
    5254              :                 libspdm_get_managed_buffer_size(&th_curr), ptr,
    5255              :                 &signature_size);
    5256            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    5257              :                          sizeof(m_libspdm_local_buffer)
    5258            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    5259              :                             m_libspdm_local_buffer),
    5260              :                          ptr, signature_size);
    5261            1 :         m_libspdm_local_buffer_size += signature_size;
    5262            1 :         libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
    5263            1 :         ptr += signature_size;
    5264            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    5265              :                          libspdm_get_managed_buffer_size(&th_curr),
    5266              :                          th_curr_hash_data);
    5267            1 :         bin_str0_size = sizeof(bin_str0);
    5268            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    5269              :                            SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
    5270            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str0,
    5271              :                            &bin_str0_size);
    5272            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
    5273              :                          m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
    5274            1 :         bin_str2_size = sizeof(bin_str2);
    5275            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    5276              :                            SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
    5277            1 :                            th_curr_hash_data, (uint16_t)hash_size, hash_size,
    5278              :                            bin_str2, &bin_str2_size);
    5279            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
    5280              :                             bin_str2, bin_str2_size,
    5281              :                             response_handshake_secret, hash_size);
    5282            1 :         bin_str7_size = sizeof(bin_str7);
    5283            1 :         libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
    5284              :                            SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
    5285            1 :                            NULL, (uint16_t)hash_size, hash_size, bin_str7,
    5286              :                            &bin_str7_size);
    5287            1 :         libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
    5288              :                             hash_size, bin_str7, bin_str7_size,
    5289              :                             response_finished_key, hash_size);
    5290            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    5291              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    5292            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    5293              :                          response_finished_key, hash_size, ptr);
    5294            1 :         ptr += hmac_size;
    5295              : 
    5296            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    5297              :                                               false, spdm_response_size,
    5298              :                                               spdm_response, response_size,
    5299              :                                               response);
    5300              :     }
    5301            1 :         return LIBSPDM_STATUS_SUCCESS;
    5302              : 
    5303            0 :     default:
    5304            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
    5305              :     }
    5306              : }
    5307              : 
    5308            1 : static void libspdm_test_requester_key_exchange_case1(void **state)
    5309              : {
    5310              :     libspdm_return_t status;
    5311              :     libspdm_test_context_t *spdm_test_context;
    5312              :     libspdm_context_t *spdm_context;
    5313              :     uint32_t session_id;
    5314              :     uint8_t heartbeat_period;
    5315              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5316              :     uint8_t slot_id_param;
    5317              :     void *data;
    5318              :     size_t data_size;
    5319              :     void *hash;
    5320              :     size_t hash_size;
    5321              : 
    5322            1 :     spdm_test_context = *state;
    5323            1 :     spdm_context = spdm_test_context->spdm_context;
    5324            1 :     spdm_test_context->case_id = 0x1;
    5325            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5326              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5327            1 :     spdm_context->connection_info.connection_state =
    5328              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5329            1 :     spdm_context->connection_info.capability.flags |=
    5330              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5331              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5332            1 :     spdm_context->local_context.capability.flags |=
    5333              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5334              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5335            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5336              :                                                     m_libspdm_use_asym_algo, &data,
    5337              :                                                     &data_size, &hash, &hash_size);
    5338            1 :     libspdm_reset_message_a(spdm_context);
    5339            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5340              :         m_libspdm_use_hash_algo;
    5341            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5342              :         m_libspdm_use_asym_algo;
    5343            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5344              :         m_libspdm_use_dhe_algo;
    5345            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5346              :         m_libspdm_use_aead_algo;
    5347              : 
    5348              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5349              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5350              :         data_size;
    5351              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5352              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5353              :                      data, data_size);
    5354              : #else
    5355            1 :     libspdm_hash_all(
    5356              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5357              :         data, data_size,
    5358            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5359            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5360            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5361            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5362              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5363              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5364              :         data, data_size,
    5365              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5366              : #endif
    5367              : 
    5368            1 :     heartbeat_period = 0;
    5369            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5370            1 :     status = libspdm_send_receive_key_exchange(
    5371              :         spdm_context,
    5372              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5373              :         &session_id, &heartbeat_period, &slot_id_param,
    5374              :         measurement_hash);
    5375            1 :     assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
    5376            1 :     free(data);
    5377            1 : }
    5378              : 
    5379            1 : static void libspdm_test_requester_key_exchange_case2(void **state)
    5380              : {
    5381              :     libspdm_return_t status;
    5382              :     libspdm_test_context_t *spdm_test_context;
    5383              :     libspdm_context_t *spdm_context;
    5384              :     uint32_t session_id;
    5385              :     uint8_t heartbeat_period;
    5386              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5387              :     uint8_t slot_id_param;
    5388              :     void *data;
    5389              :     size_t data_size;
    5390              :     void *hash;
    5391              :     size_t hash_size;
    5392              : 
    5393            1 :     spdm_test_context = *state;
    5394            1 :     spdm_context = spdm_test_context->spdm_context;
    5395            1 :     spdm_test_context->case_id = 0x2;
    5396            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5397              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5398            1 :     spdm_context->connection_info.connection_state =
    5399              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5400            1 :     spdm_context->connection_info.capability.flags |=
    5401              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5402              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5403            1 :     spdm_context->local_context.capability.flags |=
    5404              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5405              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5406            1 :     spdm_context->local_context.secured_message_version.spdm_version_count = 1;
    5407            1 :     spdm_context->local_context.secured_message_version.spdm_version[0] =
    5408              :         SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    5409            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5410              :                                                     m_libspdm_use_asym_algo, &data,
    5411              :                                                     &data_size, &hash, &hash_size);
    5412            1 :     libspdm_reset_message_a(spdm_context);
    5413            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5414              :         m_libspdm_use_hash_algo;
    5415            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5416              :         m_libspdm_use_asym_algo;
    5417            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5418              :         m_libspdm_use_dhe_algo;
    5419            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5420              :         m_libspdm_use_aead_algo;
    5421              : 
    5422              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5423              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5424              :         data_size;
    5425              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5426              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5427              :                      data, data_size);
    5428              : #else
    5429            1 :     libspdm_hash_all(
    5430              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5431              :         data, data_size,
    5432            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5433            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5434            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5435            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5436              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5437              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5438              :         data, data_size,
    5439              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5440              : #endif
    5441              : 
    5442            1 :     heartbeat_period = 0;
    5443            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5444            1 :     status = libspdm_send_receive_key_exchange(
    5445              :         spdm_context,
    5446              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5447              :         &session_id, &heartbeat_period, &slot_id_param,
    5448              :         measurement_hash);
    5449            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    5450            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    5451            1 :     assert_int_equal(
    5452              :         libspdm_secured_message_get_session_state(
    5453              :             spdm_context->session_info[0].secured_message_context),
    5454              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    5455            1 :     free(data);
    5456            1 : }
    5457              : 
    5458            1 : static void libspdm_test_requester_key_exchange_case3(void **state)
    5459              : {
    5460              :     libspdm_return_t status;
    5461              :     libspdm_test_context_t *spdm_test_context;
    5462              :     libspdm_context_t *spdm_context;
    5463              :     uint32_t session_id;
    5464              :     uint8_t heartbeat_period;
    5465              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5466              :     uint8_t slot_id_param;
    5467              :     void *data;
    5468              :     size_t data_size;
    5469              :     void *hash;
    5470              :     size_t hash_size;
    5471              : 
    5472            1 :     spdm_test_context = *state;
    5473            1 :     spdm_context = spdm_test_context->spdm_context;
    5474            1 :     spdm_test_context->case_id = 0x3;
    5475            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5476              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5477            1 :     spdm_context->connection_info.connection_state =
    5478              :         LIBSPDM_CONNECTION_STATE_NOT_STARTED;
    5479            1 :     spdm_context->connection_info.capability.flags |=
    5480              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5481              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5482            1 :     spdm_context->local_context.capability.flags |=
    5483              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5484              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5485            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5486              :                                                     m_libspdm_use_asym_algo, &data,
    5487              :                                                     &data_size, &hash, &hash_size);
    5488            1 :     libspdm_reset_message_a(spdm_context);
    5489            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5490              :         m_libspdm_use_hash_algo;
    5491            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5492              :         m_libspdm_use_asym_algo;
    5493            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5494              :         m_libspdm_use_dhe_algo;
    5495            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5496              :         m_libspdm_use_aead_algo;
    5497              : 
    5498              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5499              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5500              :         data_size;
    5501              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5502              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5503              :                      data, data_size);
    5504              : #else
    5505            1 :     libspdm_hash_all(
    5506              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5507              :         data, data_size,
    5508            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5509            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5510            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5511            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5512              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5513              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5514              :         data, data_size,
    5515              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5516              : #endif
    5517              : 
    5518            1 :     heartbeat_period = 0;
    5519            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5520            1 :     status = libspdm_send_receive_key_exchange(
    5521              :         spdm_context,
    5522              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5523              :         &session_id, &heartbeat_period, &slot_id_param,
    5524              :         measurement_hash);
    5525            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    5526            1 :     free(data);
    5527            1 : }
    5528              : 
    5529            1 : static void libspdm_test_requester_key_exchange_case4(void **state)
    5530              : {
    5531              :     libspdm_return_t status;
    5532              :     libspdm_test_context_t *spdm_test_context;
    5533              :     libspdm_context_t *spdm_context;
    5534              :     uint32_t session_id;
    5535              :     uint8_t heartbeat_period;
    5536              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5537              :     uint8_t slot_id_param;
    5538              :     void *data;
    5539              :     size_t data_size;
    5540              :     void *hash;
    5541              :     size_t hash_size;
    5542              : 
    5543            1 :     spdm_test_context = *state;
    5544            1 :     spdm_context = spdm_test_context->spdm_context;
    5545            1 :     spdm_test_context->case_id = 0x4;
    5546            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5547              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5548            1 :     spdm_context->connection_info.connection_state =
    5549              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5550            1 :     spdm_context->connection_info.capability.flags |=
    5551              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5552              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5553            1 :     spdm_context->local_context.capability.flags |=
    5554              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5555              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5556            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5557              :                                                     m_libspdm_use_asym_algo, &data,
    5558              :                                                     &data_size, &hash, &hash_size);
    5559            1 :     libspdm_reset_message_a(spdm_context);
    5560            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5561              :         m_libspdm_use_hash_algo;
    5562            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5563              :         m_libspdm_use_asym_algo;
    5564            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5565              :         m_libspdm_use_dhe_algo;
    5566            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5567              :         m_libspdm_use_aead_algo;
    5568              : 
    5569              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5570              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5571              :         data_size;
    5572              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5573              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5574              :                      data, data_size);
    5575              : #else
    5576            1 :     libspdm_hash_all(
    5577              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5578              :         data, data_size,
    5579            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5580            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5581            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5582            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5583              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5584              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5585              :         data, data_size,
    5586              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5587              : #endif
    5588              : 
    5589            1 :     heartbeat_period = 0;
    5590            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5591            1 :     status = libspdm_send_receive_key_exchange(
    5592              :         spdm_context,
    5593              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5594              :         &session_id, &heartbeat_period, &slot_id_param,
    5595              :         measurement_hash);
    5596            1 :     assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
    5597            1 :     free(data);
    5598            1 : }
    5599              : 
    5600            1 : static void libspdm_test_requester_key_exchange_case5(void **state)
    5601              : {
    5602              :     libspdm_return_t status;
    5603              :     libspdm_test_context_t *spdm_test_context;
    5604              :     libspdm_context_t *spdm_context;
    5605              :     uint32_t session_id;
    5606              :     uint8_t heartbeat_period;
    5607              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5608              :     uint8_t slot_id_param;
    5609              :     void *data;
    5610              :     size_t data_size;
    5611              :     void *hash;
    5612              :     size_t hash_size;
    5613              : 
    5614            1 :     spdm_test_context = *state;
    5615            1 :     spdm_context = spdm_test_context->spdm_context;
    5616            1 :     spdm_test_context->case_id = 0x5;
    5617            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5618              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5619            1 :     spdm_context->connection_info.connection_state =
    5620              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5621            1 :     spdm_context->connection_info.capability.flags |=
    5622              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5623              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5624            1 :     spdm_context->local_context.capability.flags |=
    5625              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5626              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5627            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5628              :                                                     m_libspdm_use_asym_algo, &data,
    5629              :                                                     &data_size, &hash, &hash_size);
    5630            1 :     libspdm_reset_message_a(spdm_context);
    5631            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5632              :         m_libspdm_use_hash_algo;
    5633            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5634              :         m_libspdm_use_asym_algo;
    5635            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5636              :         m_libspdm_use_dhe_algo;
    5637            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5638              :         m_libspdm_use_aead_algo;
    5639              : 
    5640              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5641              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5642              :         data_size;
    5643              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5644              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5645              :                      data, data_size);
    5646              : #else
    5647            1 :     libspdm_hash_all(
    5648              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5649              :         data, data_size,
    5650            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5651            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5652            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5653            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5654              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5655              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5656              :         data, data_size,
    5657              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5658              : #endif
    5659              : 
    5660            1 :     heartbeat_period = 0;
    5661            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5662            1 :     status = libspdm_send_receive_key_exchange(
    5663              :         spdm_context,
    5664              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5665              :         &session_id, &heartbeat_period, &slot_id_param,
    5666              :         measurement_hash);
    5667            1 :     assert_int_equal(status, LIBSPDM_STATUS_BUSY_PEER);
    5668            1 :     free(data);
    5669            1 : }
    5670              : 
    5671            1 : static void libspdm_test_requester_key_exchange_case6(void **state)
    5672              : {
    5673              :     libspdm_return_t status;
    5674              :     libspdm_test_context_t *spdm_test_context;
    5675              :     libspdm_context_t *spdm_context;
    5676              :     uint32_t session_id;
    5677              :     uint8_t heartbeat_period;
    5678              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5679              :     uint8_t slot_id_param;
    5680              :     void *data;
    5681              :     size_t data_size;
    5682              :     void *hash;
    5683              :     size_t hash_size;
    5684              : 
    5685            1 :     spdm_test_context = *state;
    5686            1 :     spdm_context = spdm_test_context->spdm_context;
    5687            1 :     spdm_test_context->case_id = 0x6;
    5688            1 :     spdm_context->retry_times = 3;
    5689            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5690              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5691            1 :     spdm_context->connection_info.connection_state =
    5692              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5693            1 :     spdm_context->connection_info.capability.flags |=
    5694              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5695              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5696            1 :     spdm_context->local_context.capability.flags |=
    5697              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5698              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5699            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5700              :                                                     m_libspdm_use_asym_algo, &data,
    5701              :                                                     &data_size, &hash, &hash_size);
    5702            1 :     libspdm_reset_message_a(spdm_context);
    5703            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5704              :         m_libspdm_use_hash_algo;
    5705            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5706              :         m_libspdm_use_asym_algo;
    5707            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5708              :         m_libspdm_use_dhe_algo;
    5709            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5710              :         m_libspdm_use_aead_algo;
    5711              : 
    5712              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5713              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5714              :         data_size;
    5715              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5716              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5717              :                      data, data_size);
    5718              : #else
    5719            1 :     libspdm_hash_all(
    5720              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5721              :         data, data_size,
    5722            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5723            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5724            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5725            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5726              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5727              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5728              :         data, data_size,
    5729              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5730              : #endif
    5731              : 
    5732            1 :     heartbeat_period = 0;
    5733            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5734            1 :     status = libspdm_send_receive_key_exchange(
    5735              :         spdm_context,
    5736              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5737              :         &session_id, &heartbeat_period, &slot_id_param,
    5738              :         measurement_hash);
    5739            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    5740            1 :     assert_int_equal(session_id, 0xFFFEFFFE);
    5741            1 :     assert_int_equal(
    5742              :         libspdm_secured_message_get_session_state(
    5743              :             spdm_context->session_info[0].secured_message_context),
    5744              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    5745            1 :     free(data);
    5746            1 : }
    5747              : 
    5748            1 : static void libspdm_test_requester_key_exchange_case7(void **state)
    5749              : {
    5750              :     libspdm_return_t status;
    5751              :     libspdm_test_context_t *spdm_test_context;
    5752              :     libspdm_context_t *spdm_context;
    5753              :     uint32_t session_id;
    5754              :     uint8_t heartbeat_period;
    5755              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5756              :     uint8_t slot_id_param;
    5757              :     void *data;
    5758              :     size_t data_size;
    5759              :     void *hash;
    5760              :     size_t hash_size;
    5761              : 
    5762            1 :     spdm_test_context = *state;
    5763            1 :     spdm_context = spdm_test_context->spdm_context;
    5764            1 :     spdm_test_context->case_id = 0x7;
    5765            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5766              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5767            1 :     spdm_context->connection_info.connection_state =
    5768              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5769            1 :     spdm_context->connection_info.capability.flags |=
    5770              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5771              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5772            1 :     spdm_context->local_context.capability.flags |=
    5773              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5774              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5775            1 :     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            1 :     libspdm_reset_message_a(spdm_context);
    5779            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5780              :         m_libspdm_use_hash_algo;
    5781            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5782              :         m_libspdm_use_asym_algo;
    5783            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5784              :         m_libspdm_use_dhe_algo;
    5785            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5786              :         m_libspdm_use_aead_algo;
    5787              : 
    5788              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5789              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5790              :         data_size;
    5791              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5792              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5793              :                      data, data_size);
    5794              : #else
    5795            1 :     libspdm_hash_all(
    5796              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5797              :         data, data_size,
    5798            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5799            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5800            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5801            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5802              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5803              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5804              :         data, data_size,
    5805              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5806              : #endif
    5807              : 
    5808            1 :     heartbeat_period = 0;
    5809            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5810            1 :     status = libspdm_send_receive_key_exchange(
    5811              :         spdm_context,
    5812              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5813              :         &session_id, &heartbeat_period, &slot_id_param,
    5814              :         measurement_hash);
    5815            1 :     assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
    5816            1 :     assert_int_equal(spdm_context->connection_info.connection_state,
    5817              :                      LIBSPDM_CONNECTION_STATE_NOT_STARTED);
    5818            1 :     free(data);
    5819            1 : }
    5820              : 
    5821            1 : static void libspdm_test_requester_key_exchange_case8(void **state)
    5822              : {
    5823              :     libspdm_return_t status;
    5824              :     libspdm_test_context_t *spdm_test_context;
    5825              :     libspdm_context_t *spdm_context;
    5826              :     uint32_t session_id;
    5827              :     uint8_t heartbeat_period;
    5828              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5829              :     uint8_t slot_id_param;
    5830              :     void *data;
    5831              :     size_t data_size;
    5832              :     void *hash;
    5833              :     size_t hash_size;
    5834              : 
    5835            1 :     spdm_test_context = *state;
    5836            1 :     spdm_context = spdm_test_context->spdm_context;
    5837            1 :     spdm_test_context->case_id = 0x8;
    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 =
    5841              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5842            1 :     spdm_context->connection_info.capability.flags |=
    5843              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5844              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5845            1 :     spdm_context->local_context.capability.flags |=
    5846              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5847              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5848            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5849              :                                                     m_libspdm_use_asym_algo, &data,
    5850              :                                                     &data_size, &hash, &hash_size);
    5851            1 :     libspdm_reset_message_a(spdm_context);
    5852            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5853              :         m_libspdm_use_hash_algo;
    5854            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5855              :         m_libspdm_use_asym_algo;
    5856            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5857              :         m_libspdm_use_dhe_algo;
    5858            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5859              :         m_libspdm_use_aead_algo;
    5860              : 
    5861              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5862              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5863              :         data_size;
    5864              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5865              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5866              :                      data, data_size);
    5867              : #else
    5868            1 :     libspdm_hash_all(
    5869              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5870              :         data, data_size,
    5871            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5872            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5873            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5874            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5875              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5876              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5877              :         data, data_size,
    5878              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5879              : #endif
    5880              : 
    5881            1 :     heartbeat_period = 0;
    5882            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5883            1 :     status = libspdm_send_receive_key_exchange(
    5884              :         spdm_context,
    5885              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5886              :         &session_id, &heartbeat_period, &slot_id_param,
    5887              :         measurement_hash);
    5888            1 :     assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    5889            1 :     free(data);
    5890            1 : }
    5891              : 
    5892            1 : static void libspdm_test_requester_key_exchange_case9(void **state)
    5893              : {
    5894              :     libspdm_return_t status;
    5895              :     libspdm_test_context_t *spdm_test_context;
    5896              :     libspdm_context_t *spdm_context;
    5897              :     uint32_t session_id;
    5898              :     uint8_t heartbeat_period;
    5899              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5900              :     uint8_t slot_id_param;
    5901              :     void *data;
    5902              :     size_t data_size;
    5903              :     void *hash;
    5904              :     size_t hash_size;
    5905              : 
    5906            1 :     spdm_test_context = *state;
    5907            1 :     spdm_context = spdm_test_context->spdm_context;
    5908            1 :     spdm_test_context->case_id = 0x9;
    5909            1 :     spdm_context->retry_times = 3;
    5910            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5911              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5912            1 :     spdm_context->connection_info.connection_state =
    5913              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    5914            1 :     spdm_context->connection_info.capability.flags |=
    5915              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    5916              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    5917            1 :     spdm_context->local_context.capability.flags |=
    5918              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    5919              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    5920            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    5921              :                                                     m_libspdm_use_asym_algo, &data,
    5922              :                                                     &data_size, &hash, &hash_size);
    5923            1 :     libspdm_reset_message_a(spdm_context);
    5924            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    5925              :         m_libspdm_use_hash_algo;
    5926            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    5927              :         m_libspdm_use_asym_algo;
    5928            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    5929              :         m_libspdm_use_dhe_algo;
    5930            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    5931              :         m_libspdm_use_aead_algo;
    5932              : 
    5933              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    5934              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    5935              :         data_size;
    5936              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    5937              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    5938              :                      data, data_size);
    5939              : #else
    5940            1 :     libspdm_hash_all(
    5941              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5942              :         data, data_size,
    5943            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    5944            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    5945            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    5946            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    5947              :         spdm_context->connection_info.algorithm.base_hash_algo,
    5948              :         spdm_context->connection_info.algorithm.base_asym_algo,
    5949              :         data, data_size,
    5950              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    5951              : #endif
    5952              : 
    5953            1 :     heartbeat_period = 0;
    5954            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    5955            1 :     status = libspdm_send_receive_key_exchange(
    5956              :         spdm_context,
    5957              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    5958              :         &session_id, &heartbeat_period, &slot_id_param,
    5959              :         measurement_hash);
    5960              :     if (LIBSPDM_RESPOND_IF_READY_SUPPORT) {
    5961            1 :         assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    5962            1 :         assert_int_equal(session_id, 0xFFFDFFFD);
    5963            1 :         assert_int_equal(
    5964              :             libspdm_secured_message_get_session_state(
    5965              :                 spdm_context->session_info[0].secured_message_context),
    5966              :             LIBSPDM_SESSION_STATE_HANDSHAKING);
    5967              :     } else {
    5968              :         assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    5969              :     }
    5970            1 :     free(data);
    5971            1 : }
    5972              : 
    5973            1 : static void libspdm_test_requester_key_exchange_case10(void **state) {
    5974              :     libspdm_return_t status;
    5975              :     libspdm_test_context_t    *spdm_test_context;
    5976              :     libspdm_context_t  *spdm_context;
    5977              :     uint32_t session_id;
    5978              :     uint8_t heartbeat_period;
    5979              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    5980              :     uint8_t slot_id_param;
    5981              :     void                 *data;
    5982              :     size_t data_size;
    5983              :     void                 *hash;
    5984              :     size_t hash_size;
    5985              :     uint16_t error_code;
    5986              : 
    5987            1 :     spdm_test_context = *state;
    5988            1 :     spdm_context = spdm_test_context->spdm_context;
    5989            1 :     spdm_test_context->case_id = 0xA;
    5990            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    5991              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    5992            1 :     spdm_context->connection_info.capability.flags |=
    5993              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    5994            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    5995            1 :     libspdm_read_responder_public_certificate_chain (m_libspdm_use_hash_algo,
    5996              :                                                      m_libspdm_use_asym_algo,
    5997              :                                                      &data, &data_size,
    5998              :                                                      &hash, &hash_size);
    5999            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    6000            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    6001            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    6002            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    6003              : 
    6004              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6005              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6006              :         data_size;
    6007              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6008              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6009              :                      data, data_size);
    6010              : #else
    6011            1 :     libspdm_hash_all(
    6012              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6013              :         data, data_size,
    6014            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6015            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6016            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6017            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6018              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6019              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6020              :         data, data_size,
    6021              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6022              : #endif
    6023              : 
    6024            1 :     error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
    6025           19 :     while(error_code <= 0xff) {
    6026           18 :         spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6027           18 :         libspdm_reset_message_a(spdm_context);
    6028              : 
    6029           18 :         heartbeat_period = 0;
    6030           18 :         libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6031           18 :         status = libspdm_send_receive_key_exchange (spdm_context,
    6032              :                                                     SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH,
    6033              :                                                     0, 0, &session_id, &heartbeat_period,
    6034              :                                                     &slot_id_param, measurement_hash);
    6035           18 :         LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_ERROR_PEER, error_code);
    6036              : 
    6037           18 :         error_code++;
    6038           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
    6039            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
    6040              :         }
    6041           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
    6042            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
    6043              :         }
    6044           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
    6045            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
    6046              :         }
    6047              :     }
    6048              : 
    6049            1 :     free(data);
    6050            1 : }
    6051              : 
    6052            1 : static void libspdm_test_requester_key_exchange_case11(void **state)
    6053              : {
    6054              :     libspdm_return_t status;
    6055              :     libspdm_test_context_t *spdm_test_context;
    6056              :     libspdm_context_t *spdm_context;
    6057              :     uint32_t session_id;
    6058              :     uint8_t heartbeat_period;
    6059              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6060              :     uint8_t slot_id_param;
    6061              :     void *data;
    6062              :     size_t data_size;
    6063              :     void *hash;
    6064              :     size_t hash_size;
    6065              : 
    6066            1 :     spdm_test_context = *state;
    6067            1 :     spdm_context = spdm_test_context->spdm_context;
    6068            1 :     spdm_test_context->case_id = 0xB;
    6069            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6070              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6071            1 :     spdm_context->connection_info.connection_state =
    6072              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6073            1 :     spdm_context->connection_info.capability.flags |=
    6074              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    6075              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    6076            1 :     spdm_context->local_context.capability.flags |=
    6077              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    6078              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    6079            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6080              :                                                     m_libspdm_use_asym_algo, &data,
    6081              :                                                     &data_size, &hash, &hash_size);
    6082            1 :     libspdm_reset_message_a(spdm_context);
    6083            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6084              :         m_libspdm_use_hash_algo;
    6085            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6086              :         m_libspdm_use_asym_algo;
    6087            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6088              :         m_libspdm_use_dhe_algo;
    6089            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6090              :         m_libspdm_use_aead_algo;
    6091              : 
    6092              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6093              :     spdm_context->transcript.message_m.buffer_size =
    6094              :         spdm_context->transcript.message_m.max_buffer_size;
    6095              :     spdm_context->transcript.message_b.buffer_size =
    6096              :         spdm_context->transcript.message_b.max_buffer_size;
    6097              :     spdm_context->transcript.message_c.buffer_size =
    6098              :         spdm_context->transcript.message_c.max_buffer_size;
    6099              :     spdm_context->transcript.message_mut_b.buffer_size =
    6100              :         spdm_context->transcript.message_mut_b.max_buffer_size;
    6101              :     spdm_context->transcript.message_mut_c.buffer_size =
    6102              :         spdm_context->transcript.message_mut_c.max_buffer_size;
    6103              : #endif
    6104              : 
    6105              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6106              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6107              :         data_size;
    6108              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6109              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6110              :                      data, data_size);
    6111              : #else
    6112            1 :     libspdm_hash_all(
    6113              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6114              :         data, data_size,
    6115            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6116            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6117            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6118            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6119              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6120              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6121              :         data, data_size,
    6122              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6123              : #endif
    6124              : 
    6125            1 :     heartbeat_period = 0;
    6126            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6127            1 :     status = libspdm_send_receive_key_exchange(
    6128              :         spdm_context,
    6129              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6130              :         &session_id, &heartbeat_period, &slot_id_param,
    6131              :         measurement_hash);
    6132            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    6133            1 :     assert_int_equal(
    6134              :         libspdm_secured_message_get_session_state(
    6135              :             spdm_context->session_info[0].secured_message_context),
    6136              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    6137              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6138              :     assert_int_equal(spdm_context->transcript.message_m.buffer_size, 0);
    6139              :     assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
    6140              :     assert_int_equal(spdm_context->transcript.message_c.buffer_size, 0);
    6141              :     assert_int_equal(spdm_context->transcript.message_mut_b.buffer_size, 0);
    6142              :     assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
    6143              : #endif
    6144            1 :     free(data);
    6145            1 : }
    6146              : 
    6147            1 : static void libspdm_test_requester_key_exchange_case12(void **state)
    6148              : {
    6149              :     libspdm_return_t status;
    6150              :     libspdm_test_context_t *spdm_test_context;
    6151              :     libspdm_context_t *spdm_context;
    6152              :     uint32_t session_id;
    6153              :     uint8_t heartbeat_period;
    6154              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6155              :     uint8_t slot_id_param;
    6156              :     void *data;
    6157              :     size_t data_size;
    6158              :     void *hash;
    6159              :     size_t hash_size;
    6160              : 
    6161            1 :     spdm_test_context = *state;
    6162            1 :     spdm_context = spdm_test_context->spdm_context;
    6163              : 
    6164            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6165            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6166              :     }
    6167              : 
    6168            1 :     spdm_test_context->case_id = 0xC;
    6169            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6170              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6171            1 :     spdm_context->connection_info.connection_state =
    6172              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6173            1 :     spdm_context->connection_info.capability.flags |=
    6174              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6175            1 :     spdm_context->connection_info.capability.flags |=
    6176              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6177            1 :     spdm_context->local_context.capability.flags |=
    6178              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6179              : 
    6180            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6181              :                                                     m_libspdm_use_asym_algo, &data,
    6182              :                                                     &data_size, &hash, &hash_size);
    6183            1 :     libspdm_reset_message_a(spdm_context);
    6184            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6185              :         m_libspdm_use_hash_algo;
    6186            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6187              :         m_libspdm_use_asym_algo;
    6188            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6189              :         m_libspdm_use_dhe_algo;
    6190            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6191              :         m_libspdm_use_aead_algo;
    6192            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6193              :         m_libspdm_use_measurement_hash_algo;
    6194              : 
    6195              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6196              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6197              :         data_size;
    6198              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6199              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6200              :                      data, data_size);
    6201              : #else
    6202            1 :     libspdm_hash_all(
    6203              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6204              :         data, data_size,
    6205            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6206            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6207            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6208            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6209              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6210              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6211              :         data, data_size,
    6212              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6213              : #endif
    6214              : 
    6215            1 :     heartbeat_period = 0;
    6216            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6217            1 :     status = libspdm_send_receive_key_exchange(
    6218              :         spdm_context,
    6219              :         SPDM_KEY_EXCHANGE_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH, 0, 0,
    6220              :         &session_id, &heartbeat_period, &slot_id_param,
    6221              :         measurement_hash);
    6222            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    6223            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    6224            1 :     assert_memory_equal(
    6225              :         measurement_hash,
    6226              :         m_libspdm_use_tcb_hash_value,
    6227              :         libspdm_get_hash_size(m_libspdm_use_hash_algo));
    6228            1 :     assert_int_equal(
    6229              :         libspdm_secured_message_get_session_state(
    6230              :             spdm_context->session_info[0].secured_message_context),
    6231              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    6232            1 :     free(data);
    6233            1 : }
    6234              : 
    6235            1 : static void libspdm_test_requester_key_exchange_case13(void **state)
    6236              : {
    6237              :     libspdm_return_t status;
    6238              :     libspdm_test_context_t *spdm_test_context;
    6239              :     libspdm_context_t *spdm_context;
    6240              :     uint32_t session_id;
    6241              :     uint8_t heartbeat_period;
    6242              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6243              :     uint8_t slot_id_param;
    6244              :     void *data;
    6245              :     size_t data_size;
    6246              :     void *hash;
    6247              :     size_t hash_size;
    6248              : 
    6249            1 :     spdm_test_context = *state;
    6250            1 :     spdm_context = spdm_test_context->spdm_context;
    6251              : 
    6252            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6253            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6254              :     }
    6255              : 
    6256            1 :     spdm_test_context->case_id = 0xD;
    6257            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6258              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6259            1 :     spdm_context->connection_info.connection_state =
    6260              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6261            1 :     spdm_context->connection_info.capability.flags |=
    6262              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6263            1 :     spdm_context->connection_info.capability.flags |=
    6264              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6265            1 :     spdm_context->local_context.capability.flags |=
    6266              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6267              : 
    6268            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6269              :                                                     m_libspdm_use_asym_algo, &data,
    6270              :                                                     &data_size, &hash, &hash_size);
    6271            1 :     libspdm_reset_message_a(spdm_context);
    6272            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6273              :         m_libspdm_use_hash_algo;
    6274            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6275              :         m_libspdm_use_asym_algo;
    6276            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6277              :         m_libspdm_use_dhe_algo;
    6278            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6279              :         m_libspdm_use_aead_algo;
    6280            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6281              :         m_libspdm_use_measurement_hash_algo;
    6282              : 
    6283              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6284              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6285              :         data_size;
    6286              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6287              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6288              :                      data, data_size);
    6289              : #else
    6290            1 :     libspdm_hash_all(
    6291              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6292              :         data, data_size,
    6293            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6294            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6295            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6296            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6297              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6298              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6299              :         data, data_size,
    6300              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6301              : #endif
    6302              : 
    6303            1 :     heartbeat_period = 0;
    6304            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6305            1 :     status = libspdm_send_receive_key_exchange(
    6306              :         spdm_context,
    6307              :         SPDM_KEY_EXCHANGE_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH, 0, 0,
    6308              :         &session_id, &heartbeat_period, &slot_id_param,
    6309              :         measurement_hash);
    6310            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    6311            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    6312            1 :     assert_memory_equal(
    6313              :         measurement_hash,
    6314              :         m_libspdm_zero_filled_buffer,
    6315              :         libspdm_get_hash_size(m_libspdm_use_hash_algo));
    6316            1 :     assert_int_equal(
    6317              :         libspdm_secured_message_get_session_state(
    6318              :             spdm_context->session_info[0].secured_message_context),
    6319              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    6320            1 :     free(data);
    6321            1 : }
    6322              : 
    6323            1 : static void libspdm_test_requester_key_exchange_case14(void **state)
    6324              : {
    6325              :     libspdm_return_t status;
    6326              :     libspdm_test_context_t *spdm_test_context;
    6327              :     libspdm_context_t *spdm_context;
    6328              :     uint32_t session_id;
    6329              :     uint8_t heartbeat_period;
    6330              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6331              :     uint8_t slot_id_param;
    6332              :     void *data;
    6333              :     size_t data_size;
    6334              :     void *hash;
    6335              :     size_t hash_size;
    6336              : 
    6337            1 :     spdm_test_context = *state;
    6338            1 :     spdm_context = spdm_test_context->spdm_context;
    6339              : 
    6340            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6341            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6342              :     }
    6343              : 
    6344            1 :     spdm_test_context->case_id = 0xE;
    6345            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6346              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6347            1 :     spdm_context->connection_info.connection_state =
    6348              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6349            1 :     spdm_context->connection_info.capability.flags |=
    6350              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6351            1 :     spdm_context->connection_info.capability.flags |=
    6352              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6353            1 :     spdm_context->local_context.capability.flags |=
    6354              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6355              : 
    6356            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6357              :                                                     m_libspdm_use_asym_algo, &data,
    6358              :                                                     &data_size, &hash, &hash_size);
    6359            1 :     libspdm_reset_message_a(spdm_context);
    6360            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6361              :         m_libspdm_use_hash_algo;
    6362            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6363              :         m_libspdm_use_asym_algo;
    6364            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6365              :         m_libspdm_use_dhe_algo;
    6366            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6367              :         m_libspdm_use_aead_algo;
    6368            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6369              :         m_libspdm_use_measurement_hash_algo;
    6370              : 
    6371              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6372              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6373              :         data_size;
    6374              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6375              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6376              :                      data, data_size);
    6377              : #else
    6378            1 :     libspdm_hash_all(
    6379              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6380              :         data, data_size,
    6381            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6382            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6383            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6384            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6385              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6386              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6387              :         data, data_size,
    6388              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6389              : #endif
    6390              : 
    6391            1 :     heartbeat_period = 0;
    6392            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6393            1 :     status = libspdm_send_receive_key_exchange(
    6394              :         spdm_context,
    6395              :         SPDM_KEY_EXCHANGE_REQUEST_ALL_MEASUREMENTS_HASH, 0, 0,
    6396              :         &session_id, &heartbeat_period, &slot_id_param,
    6397              :         measurement_hash);
    6398            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    6399            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    6400            1 :     assert_memory_equal(
    6401              :         measurement_hash,
    6402              :         m_libspdm_use_tcb_hash_value,
    6403              :         libspdm_get_hash_size(m_libspdm_use_hash_algo));
    6404            1 :     assert_int_equal(
    6405              :         libspdm_secured_message_get_session_state(
    6406              :             spdm_context->session_info[0].secured_message_context),
    6407              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    6408            1 :     free(data);
    6409            1 : }
    6410              : 
    6411            1 : static void libspdm_test_requester_key_exchange_case15(void **state)
    6412              : {
    6413              :     libspdm_return_t status;
    6414              :     libspdm_test_context_t *spdm_test_context;
    6415              :     libspdm_context_t *spdm_context;
    6416              :     uint32_t session_id;
    6417              :     uint8_t heartbeat_period;
    6418              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6419              :     uint8_t slot_id_param;
    6420              :     void *data;
    6421              :     size_t data_size;
    6422              :     void *hash;
    6423              :     size_t hash_size;
    6424              : 
    6425            1 :     spdm_test_context = *state;
    6426            1 :     spdm_context = spdm_test_context->spdm_context;
    6427              : 
    6428            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6429            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6430              :     }
    6431              : 
    6432            1 :     spdm_test_context->case_id = 0xF;
    6433            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6434              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6435            1 :     spdm_context->connection_info.connection_state =
    6436              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6437            1 :     spdm_context->connection_info.capability.flags |=
    6438              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6439            1 :     spdm_context->connection_info.capability.flags |=
    6440              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6441            1 :     spdm_context->local_context.capability.flags |=
    6442              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6443              : 
    6444            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6445              :                                                     m_libspdm_use_asym_algo, &data,
    6446              :                                                     &data_size, &hash, &hash_size);
    6447            1 :     libspdm_reset_message_a(spdm_context);
    6448            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6449              :         m_libspdm_use_hash_algo;
    6450            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6451              :         m_libspdm_use_asym_algo;
    6452            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6453              :         m_libspdm_use_dhe_algo;
    6454            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6455              :         m_libspdm_use_aead_algo;
    6456            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6457              :         m_libspdm_use_measurement_hash_algo;
    6458              : 
    6459              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6460              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6461              :         data_size;
    6462              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6463              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6464              :                      data, data_size);
    6465              : #else
    6466            1 :     libspdm_hash_all(
    6467              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6468              :         data, data_size,
    6469            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6470            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6471            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6472            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6473              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6474              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6475              :         data, data_size,
    6476              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6477              : #endif
    6478              : 
    6479            1 :     heartbeat_period = 0;
    6480            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6481            1 :     status = libspdm_send_receive_key_exchange(
    6482              :         spdm_context,
    6483              :         SPDM_KEY_EXCHANGE_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH, 0, 0,
    6484              :         &session_id, &heartbeat_period, &slot_id_param,
    6485              :         measurement_hash);
    6486            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
    6487            1 :     free(data);
    6488            1 : }
    6489              : 
    6490            1 : static void libspdm_test_requester_key_exchange_case16(void **state)
    6491              : {
    6492              :     libspdm_return_t status;
    6493              :     libspdm_test_context_t *spdm_test_context;
    6494              :     libspdm_context_t *spdm_context;
    6495              :     uint32_t session_id;
    6496              :     uint8_t heartbeat_period;
    6497              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6498              :     uint8_t slot_id_param;
    6499              :     void *data;
    6500              :     size_t data_size;
    6501              :     void *hash;
    6502              :     size_t hash_size;
    6503              : 
    6504            1 :     spdm_test_context = *state;
    6505            1 :     spdm_context = spdm_test_context->spdm_context;
    6506              : 
    6507            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6508            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6509              :     }
    6510              : 
    6511            1 :     spdm_test_context->case_id = 0x10;
    6512            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6513              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6514            1 :     spdm_context->connection_info.connection_state =
    6515              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6516            1 :     spdm_context->connection_info.capability.flags |=
    6517              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6518            1 :     spdm_context->connection_info.capability.flags |=
    6519              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6520            1 :     spdm_context->local_context.capability.flags |=
    6521              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6522              : 
    6523            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6524              :                                                     m_libspdm_use_asym_algo, &data,
    6525              :                                                     &data_size, &hash, &hash_size);
    6526            1 :     libspdm_reset_message_a(spdm_context);
    6527            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6528              :         m_libspdm_use_hash_algo;
    6529            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6530              :         m_libspdm_use_asym_algo;
    6531            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6532              :         m_libspdm_use_dhe_algo;
    6533            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6534              :         m_libspdm_use_aead_algo;
    6535            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6536              :         m_libspdm_use_measurement_hash_algo;
    6537              : 
    6538              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6539              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6540              :         data_size;
    6541              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6542              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6543              :                      data, data_size);
    6544              : #else
    6545            1 :     libspdm_hash_all(
    6546              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6547              :         data, data_size,
    6548            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6549            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6550            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6551            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6552              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6553              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6554              :         data, data_size,
    6555              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6556              : #endif
    6557              : 
    6558            1 :     heartbeat_period = 0;
    6559            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6560            1 :     status = libspdm_send_receive_key_exchange(
    6561              :         spdm_context,
    6562              :         SPDM_KEY_EXCHANGE_REQUEST_ALL_MEASUREMENTS_HASH, 0, 0,
    6563              :         &session_id, &heartbeat_period, &slot_id_param,
    6564              :         measurement_hash);
    6565            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
    6566            1 :     free(data);
    6567            1 : }
    6568              : 
    6569            1 : static void libspdm_test_requester_key_exchange_case17(void **state)
    6570              : {
    6571              :     libspdm_return_t status;
    6572              :     libspdm_test_context_t *spdm_test_context;
    6573              :     libspdm_context_t *spdm_context;
    6574              :     uint32_t session_id;
    6575              :     uint8_t heartbeat_period;
    6576              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6577              :     uint8_t slot_id_param;
    6578              :     void *data;
    6579              :     size_t data_size;
    6580              :     void *hash;
    6581              :     size_t hash_size;
    6582              : 
    6583            1 :     spdm_test_context = *state;
    6584            1 :     spdm_context = spdm_test_context->spdm_context;
    6585              : 
    6586            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6587            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6588              :     }
    6589              : 
    6590            1 :     spdm_test_context->case_id = 0x11;
    6591            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6592              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6593            1 :     spdm_context->connection_info.connection_state =
    6594              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6595            1 :     spdm_context->connection_info.capability.flags |=
    6596              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6597            1 :     spdm_context->connection_info.capability.flags |=
    6598              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6599            1 :     spdm_context->local_context.capability.flags |=
    6600              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6601              : 
    6602            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6603              :                                                     m_libspdm_use_asym_algo, &data,
    6604              :                                                     &data_size, &hash, &hash_size);
    6605            1 :     libspdm_reset_message_a(spdm_context);
    6606            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6607              :         m_libspdm_use_hash_algo;
    6608            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6609              :         m_libspdm_use_asym_algo;
    6610            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6611              :         m_libspdm_use_dhe_algo;
    6612            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6613              :         m_libspdm_use_aead_algo;
    6614            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6615              :         m_libspdm_use_measurement_hash_algo;
    6616              : 
    6617              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6618              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6619              :         data_size;
    6620              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6621              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6622              :                      data, data_size);
    6623              : #else
    6624            1 :     libspdm_hash_all(
    6625              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6626              :         data, data_size,
    6627            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6628            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6629            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6630            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6631              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6632              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6633              :         data, data_size,
    6634              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6635              : #endif
    6636              : 
    6637            1 :     heartbeat_period = 0;
    6638            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6639            1 :     status = libspdm_send_receive_key_exchange(
    6640              :         spdm_context,
    6641              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6642              :         &session_id, &heartbeat_period, &slot_id_param,
    6643              :         measurement_hash);
    6644            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
    6645            1 :     free(data);
    6646            1 : }
    6647              : 
    6648            1 : static void libspdm_test_requester_key_exchange_case18(void **state)
    6649              : {
    6650              :     libspdm_return_t status;
    6651              :     libspdm_test_context_t *spdm_test_context;
    6652              :     libspdm_context_t *spdm_context;
    6653              :     uint32_t session_id;
    6654              :     uint8_t heartbeat_period;
    6655              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6656              :     uint8_t slot_id_param;
    6657              :     void *data;
    6658              :     size_t data_size;
    6659              :     void *hash;
    6660              :     size_t hash_size;
    6661              : 
    6662            1 :     spdm_test_context = *state;
    6663            1 :     spdm_context = spdm_test_context->spdm_context;
    6664              : 
    6665            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6666            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6667              :     }
    6668              : 
    6669            1 :     spdm_test_context->case_id = 0x12;
    6670            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6671              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6672            1 :     spdm_context->connection_info.connection_state =
    6673              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6674            1 :     spdm_context->connection_info.capability.flags |=
    6675              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6676            1 :     spdm_context->connection_info.capability.flags |=
    6677              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6678            1 :     spdm_context->local_context.capability.flags |=
    6679              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6680              : 
    6681            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6682              :                                                     m_libspdm_use_asym_algo, &data,
    6683              :                                                     &data_size, &hash, &hash_size);
    6684            1 :     libspdm_reset_message_a(spdm_context);
    6685            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6686              :         m_libspdm_use_hash_algo;
    6687            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6688              :         m_libspdm_use_asym_algo;
    6689            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6690              :         m_libspdm_use_dhe_algo;
    6691            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6692              :         m_libspdm_use_aead_algo;
    6693            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6694              :         m_libspdm_use_measurement_hash_algo;
    6695              : 
    6696              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6697              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6698              :         data_size;
    6699              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6700              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6701              :                      data, data_size);
    6702              : #else
    6703            1 :     libspdm_hash_all(
    6704              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6705              :         data, data_size,
    6706            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6707            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6708            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6709            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6710              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6711              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6712              :         data, data_size,
    6713              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6714              : #endif
    6715              : 
    6716            1 :     heartbeat_period = 0;
    6717            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6718            1 :     status = libspdm_send_receive_key_exchange(
    6719              :         spdm_context,
    6720              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6721              :         &session_id, &heartbeat_period, &slot_id_param,
    6722              :         measurement_hash);
    6723            1 :     assert_int_equal(status, LIBSPDM_STATUS_VERIF_FAIL);
    6724            1 :     free(data);
    6725            1 : }
    6726              : 
    6727            1 : static void libspdm_test_requester_key_exchange_case19(void **state)
    6728              : {
    6729              :     libspdm_return_t status;
    6730              :     libspdm_test_context_t *spdm_test_context;
    6731              :     libspdm_context_t *spdm_context;
    6732              :     uint32_t session_id;
    6733              :     uint8_t heartbeat_period;
    6734              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6735              :     uint8_t slot_id_param;
    6736              :     void *data;
    6737              :     size_t data_size;
    6738              :     void *hash;
    6739              :     size_t hash_size;
    6740              : 
    6741            1 :     spdm_test_context = *state;
    6742            1 :     spdm_context = spdm_test_context->spdm_context;
    6743            1 :     spdm_test_context->case_id = 0x13;
    6744            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6745              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6746            1 :     spdm_context->connection_info.connection_state =
    6747              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6748            1 :     spdm_context->connection_info.capability.flags |=
    6749              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6750            1 :     spdm_context->connection_info.capability.flags |=
    6751              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6752            1 :     spdm_context->local_context.capability.flags |=
    6753              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6754              : 
    6755            1 :     spdm_context->connection_info.capability.flags |=
    6756              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    6757            1 :     spdm_context->local_context.capability.flags |=
    6758              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    6759              : 
    6760            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6761              :                                                     m_libspdm_use_asym_algo, &data,
    6762              :                                                     &data_size, &hash, &hash_size);
    6763            1 :     libspdm_reset_message_a(spdm_context);
    6764            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6765              :         m_libspdm_use_hash_algo;
    6766            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6767              :         m_libspdm_use_asym_algo;
    6768            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6769              :         m_libspdm_use_dhe_algo;
    6770            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6771              :         m_libspdm_use_aead_algo;
    6772            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6773              :         m_libspdm_use_measurement_hash_algo;
    6774              : 
    6775              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6776              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6777              :         data_size;
    6778              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6779              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6780              :                      data, data_size);
    6781              : #else
    6782            1 :     libspdm_hash_all(
    6783              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6784              :         data, data_size,
    6785            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6786            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6787            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6788            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6789              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6790              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6791              :         data, data_size,
    6792              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6793              : #endif
    6794              : 
    6795            1 :     heartbeat_period = 0;
    6796            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6797            1 :     status = libspdm_send_receive_key_exchange(
    6798              :         spdm_context,
    6799              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6800              :         &session_id, &heartbeat_period, &slot_id_param,
    6801              :         measurement_hash);
    6802              :     /* Clear Handshake in the clear flags */
    6803            1 :     spdm_context->connection_info.capability.flags &=
    6804              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    6805            1 :     spdm_context->local_context.capability.flags &=
    6806              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    6807            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    6808            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    6809            1 :     assert_int_equal(
    6810              :         libspdm_secured_message_get_session_state(
    6811              :             spdm_context->session_info[0].secured_message_context),
    6812              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    6813            1 :     free(data);
    6814            1 : }
    6815              : 
    6816            1 : static void libspdm_test_requester_key_exchange_case20(void **state)
    6817              : {
    6818              :     libspdm_return_t status;
    6819              :     libspdm_test_context_t *spdm_test_context;
    6820              :     libspdm_context_t *spdm_context;
    6821              :     uint32_t session_id;
    6822              :     uint8_t heartbeat_period;
    6823              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6824              :     uint8_t slot_id_param;
    6825              :     void *data;
    6826              :     size_t data_size;
    6827              :     void *hash;
    6828              :     size_t hash_size;
    6829              : 
    6830            1 :     spdm_test_context = *state;
    6831            1 :     spdm_context = spdm_test_context->spdm_context;
    6832              : 
    6833            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6834            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6835              :     }
    6836              : 
    6837            1 :     spdm_test_context->case_id = 0x14;
    6838            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6839              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6840            1 :     spdm_context->connection_info.connection_state =
    6841              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6842            1 :     spdm_context->connection_info.capability.flags |=
    6843              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6844            1 :     spdm_context->connection_info.capability.flags |=
    6845              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6846            1 :     spdm_context->local_context.capability.flags |=
    6847              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6848              : 
    6849              : 
    6850            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6851              :                                                     m_libspdm_use_asym_algo, &data,
    6852              :                                                     &data_size, &hash, &hash_size);
    6853            1 :     libspdm_reset_message_a(spdm_context);
    6854            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6855              :         m_libspdm_use_hash_algo;
    6856            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6857              :         m_libspdm_use_asym_algo;
    6858            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6859              :         m_libspdm_use_dhe_algo;
    6860            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6861              :         m_libspdm_use_aead_algo;
    6862            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6863              :         m_libspdm_use_measurement_hash_algo;
    6864              : 
    6865              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6866              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6867              :         data_size;
    6868              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6869              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6870              :                      data, data_size);
    6871              : #else
    6872            1 :     libspdm_hash_all(
    6873              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6874              :         data, data_size,
    6875            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6876            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6877            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6878            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6879              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6880              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6881              :         data, data_size,
    6882              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6883              : #endif
    6884              : 
    6885            1 :     heartbeat_period = 0;
    6886            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6887            1 :     status = libspdm_send_receive_key_exchange(
    6888              :         spdm_context,
    6889              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6890              :         &session_id, &heartbeat_period, &slot_id_param,
    6891              :         measurement_hash);
    6892            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    6893            1 :     free(data);
    6894            1 : }
    6895              : 
    6896            1 : static void libspdm_test_requester_key_exchange_case21(void **state)
    6897              : {
    6898              :     libspdm_return_t status;
    6899              :     libspdm_test_context_t *spdm_test_context;
    6900              :     libspdm_context_t *spdm_context;
    6901              :     uint32_t session_id;
    6902              :     uint8_t heartbeat_period;
    6903              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    6904              :     uint8_t slot_id_param;
    6905              :     void *data;
    6906              :     size_t data_size;
    6907              :     void *hash;
    6908              :     size_t hash_size;
    6909              : 
    6910            1 :     spdm_test_context = *state;
    6911            1 :     spdm_context = spdm_test_context->spdm_context;
    6912              : 
    6913            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    6914            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    6915              :     }
    6916              : 
    6917            1 :     spdm_test_context->case_id = 0x15;
    6918            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    6919              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    6920            1 :     spdm_context->connection_info.connection_state =
    6921              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    6922            1 :     spdm_context->connection_info.capability.flags |=
    6923              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    6924            1 :     spdm_context->connection_info.capability.flags |=
    6925              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    6926            1 :     spdm_context->local_context.capability.flags |=
    6927              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    6928              : 
    6929            1 :     spdm_context->connection_info.capability.flags |=
    6930              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    6931            1 :     spdm_context->local_context.capability.flags |=
    6932              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    6933              : 
    6934              : 
    6935            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    6936              :                                                     m_libspdm_use_asym_algo, &data,
    6937              :                                                     &data_size, &hash, &hash_size);
    6938            1 :     libspdm_reset_message_a(spdm_context);
    6939            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    6940              :         m_libspdm_use_hash_algo;
    6941            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    6942              :         m_libspdm_use_asym_algo;
    6943            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    6944              :         m_libspdm_use_dhe_algo;
    6945            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    6946              :         m_libspdm_use_aead_algo;
    6947            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    6948              :         m_libspdm_use_measurement_hash_algo;
    6949              : 
    6950              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    6951              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    6952              :         data_size;
    6953              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    6954              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    6955              :                      data, data_size);
    6956              : #else
    6957            1 :     libspdm_hash_all(
    6958              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6959              :         data, data_size,
    6960            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    6961            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    6962            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    6963            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    6964              :         spdm_context->connection_info.algorithm.base_hash_algo,
    6965              :         spdm_context->connection_info.algorithm.base_asym_algo,
    6966              :         data, data_size,
    6967              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    6968              : #endif
    6969              : 
    6970            1 :     heartbeat_period = 0;
    6971            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    6972            1 :     status = libspdm_send_receive_key_exchange(
    6973              :         spdm_context,
    6974              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    6975              :         &session_id, &heartbeat_period, &slot_id_param,
    6976              :         measurement_hash);
    6977              :     /* clear Heartbeat flags */
    6978            1 :     spdm_context->connection_info.capability.flags &=
    6979              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    6980            1 :     spdm_context->local_context.capability.flags &=
    6981              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    6982            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    6983            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    6984            1 :     assert_int_equal(
    6985              :         libspdm_secured_message_get_session_state(
    6986              :             spdm_context->session_info[0].secured_message_context),
    6987              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    6988            1 :     assert_int_equal(heartbeat_period,5);
    6989            1 :     free(data);
    6990            1 : }
    6991              : 
    6992            1 : static void libspdm_test_requester_key_exchange_case22(void **state)
    6993              : {
    6994              :     libspdm_return_t status;
    6995              :     libspdm_test_context_t *spdm_test_context;
    6996              :     libspdm_context_t *spdm_context;
    6997              :     uint32_t session_id;
    6998              :     uint8_t heartbeat_period;
    6999              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7000              :     uint8_t slot_id_param;
    7001              :     void *data;
    7002              :     size_t data_size;
    7003              :     void *hash;
    7004              :     size_t hash_size;
    7005              : 
    7006            1 :     spdm_test_context = *state;
    7007            1 :     spdm_context = spdm_test_context->spdm_context;
    7008              : 
    7009            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7010            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7011              :     }
    7012              : 
    7013            1 :     spdm_test_context->case_id = 0x16;
    7014            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7015              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7016            1 :     spdm_context->connection_info.connection_state =
    7017              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7018            1 :     spdm_context->connection_info.capability.flags |=
    7019              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    7020            1 :     spdm_context->connection_info.capability.flags |=
    7021              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    7022            1 :     spdm_context->local_context.capability.flags |=
    7023              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    7024              : 
    7025            1 :     spdm_context->connection_info.capability.flags |=
    7026              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    7027            1 :     spdm_context->local_context.capability.flags |=
    7028              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    7029              : 
    7030              : 
    7031            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7032              :                                                     m_libspdm_use_asym_algo, &data,
    7033              :                                                     &data_size, &hash, &hash_size);
    7034            1 :     libspdm_reset_message_a(spdm_context);
    7035            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    7036              :         m_libspdm_use_hash_algo;
    7037            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    7038              :         m_libspdm_use_asym_algo;
    7039            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    7040              :         m_libspdm_use_dhe_algo;
    7041            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    7042              :         m_libspdm_use_aead_algo;
    7043            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    7044              :         m_libspdm_use_measurement_hash_algo;
    7045              : 
    7046              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7047              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7048              :         data_size;
    7049              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7050              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7051              :                      data, data_size);
    7052              : #else
    7053            1 :     libspdm_hash_all(
    7054              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7055              :         data, data_size,
    7056            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7057            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7058            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7059            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7060              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7061              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7062              :         data, data_size,
    7063              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7064              : #endif
    7065              : 
    7066            1 :     heartbeat_period = 0;
    7067            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7068            1 :     status = libspdm_send_receive_key_exchange(
    7069              :         spdm_context,
    7070              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7071              :         &session_id, &heartbeat_period, &slot_id_param,
    7072              :         measurement_hash);
    7073              : 
    7074              :     /*clear Heartbeat flags*/
    7075            1 :     spdm_context->connection_info.capability.flags &=
    7076              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    7077            1 :     spdm_context->local_context.capability.flags &=
    7078              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    7079              : 
    7080            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    7081            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    7082            1 :     assert_int_equal(
    7083              :         libspdm_secured_message_get_session_state(
    7084              :             spdm_context->session_info[0].secured_message_context),
    7085              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    7086            1 :     assert_int_equal(heartbeat_period,0);
    7087            1 :     free(data);
    7088            1 : }
    7089              : 
    7090            1 : static void libspdm_test_requester_key_exchange_case23(void **state)
    7091              : {
    7092              :     libspdm_return_t status;
    7093              :     libspdm_test_context_t *spdm_test_context;
    7094              :     libspdm_context_t *spdm_context;
    7095              :     uint32_t session_id;
    7096              :     uint8_t heartbeat_period;
    7097              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7098              :     uint8_t slot_id_param;
    7099              :     void *data;
    7100              :     size_t data_size;
    7101              :     void *hash;
    7102              :     size_t hash_size;
    7103              : 
    7104            1 :     spdm_test_context = *state;
    7105            1 :     spdm_context = spdm_test_context->spdm_context;
    7106              : 
    7107            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7108            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7109              :     }
    7110              : 
    7111            1 :     spdm_test_context->case_id = 0x17;
    7112            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7113              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7114            1 :     spdm_context->connection_info.connection_state =
    7115              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7116            1 :     spdm_context->connection_info.capability.flags |=
    7117              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    7118            1 :     spdm_context->connection_info.capability.flags |=
    7119              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    7120            1 :     spdm_context->local_context.capability.flags |=
    7121              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    7122              : 
    7123            1 :     spdm_context->connection_info.capability.flags |=
    7124              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7125            1 :     spdm_context->local_context.capability.flags |=
    7126              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7127              : 
    7128              : 
    7129            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7130              :                                                     m_libspdm_use_asym_algo, &data,
    7131              :                                                     &data_size, &hash, &hash_size);
    7132            1 :     libspdm_reset_message_a(spdm_context);
    7133            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    7134              :         m_libspdm_use_hash_algo;
    7135            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    7136              :         m_libspdm_use_asym_algo;
    7137            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    7138              :         m_libspdm_use_dhe_algo;
    7139            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    7140              :         m_libspdm_use_aead_algo;
    7141            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    7142              :         m_libspdm_use_measurement_hash_algo;
    7143              : 
    7144              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7145              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7146              :         data_size;
    7147              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7148              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7149              :                      data, data_size);
    7150              : #else
    7151            1 :     libspdm_hash_all(
    7152              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7153              :         data, data_size,
    7154            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7155            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7156            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7157            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7158              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7159              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7160              :         data, data_size,
    7161              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7162              : #endif
    7163              : 
    7164            1 :     heartbeat_period = 0;
    7165            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7166            1 :     status = libspdm_send_receive_key_exchange(
    7167              :         spdm_context,
    7168              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7169              :         &session_id, &heartbeat_period, &slot_id_param,
    7170              :         measurement_hash);
    7171              :     /* Clear Mut_auth flags */
    7172            1 :     spdm_context->connection_info.capability.flags &=
    7173              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7174            1 :     spdm_context->local_context.capability.flags &=
    7175              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7176            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    7177            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    7178            1 :     assert_int_equal(
    7179              :         libspdm_secured_message_get_session_state(
    7180              :             spdm_context->session_info[0].secured_message_context),
    7181              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    7182            1 :     assert_int_equal(
    7183              :         spdm_context->session_info[0].mut_auth_requested,
    7184              :         SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED);
    7185            1 :     free(data);
    7186            1 : }
    7187              : 
    7188            1 : static void libspdm_test_requester_key_exchange_case24(void **state)
    7189              : {
    7190              :     libspdm_return_t status;
    7191              :     libspdm_test_context_t *spdm_test_context;
    7192              :     libspdm_context_t *spdm_context;
    7193              :     uint32_t session_id;
    7194              :     uint8_t heartbeat_period;
    7195              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7196              :     uint8_t slot_id_param;
    7197              :     void *data;
    7198              :     size_t data_size;
    7199              :     void *hash;
    7200              :     size_t hash_size;
    7201              : 
    7202            1 :     spdm_test_context = *state;
    7203            1 :     spdm_context = spdm_test_context->spdm_context;
    7204              : 
    7205            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7206            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7207              :     }
    7208              : 
    7209            1 :     spdm_test_context->case_id = 0x18;
    7210            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7211              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7212            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7213            1 :     spdm_context->connection_info.capability.flags |=
    7214              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    7215            1 :     spdm_context->connection_info.capability.flags |=
    7216              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    7217            1 :     spdm_context->local_context.capability.flags |=
    7218              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    7219              : 
    7220            1 :     spdm_context->connection_info.capability.flags |=
    7221              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7222            1 :     spdm_context->local_context.capability.flags |=
    7223              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7224              : 
    7225            1 :     spdm_context->connection_info.capability.flags |=
    7226              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
    7227            1 :     spdm_context->local_context.capability.flags |=
    7228              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
    7229              : 
    7230            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7231              :                                                     m_libspdm_use_asym_algo, &data,
    7232              :                                                     &data_size, &hash, &hash_size);
    7233            1 :     libspdm_reset_message_a(spdm_context);
    7234            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    7235            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    7236            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    7237            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    7238            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    7239              :         m_libspdm_use_measurement_hash_algo;
    7240              : 
    7241              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7242              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7243              :         data_size;
    7244              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7245              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7246              :                      data, data_size);
    7247              : #else
    7248            1 :     libspdm_hash_all(
    7249              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7250              :         data, data_size,
    7251            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7252            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7253            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7254            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7255              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7256              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7257              :         data, data_size,
    7258              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7259              : #endif
    7260              : 
    7261            1 :     heartbeat_period = 0;
    7262            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7263            1 :     status = libspdm_send_receive_key_exchange(
    7264              :         spdm_context,
    7265              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7266              :         &session_id, &heartbeat_period, &slot_id_param,
    7267              :         measurement_hash);
    7268              :     /* Clear Mut_auth flags */
    7269            1 :     spdm_context->connection_info.capability.flags &=
    7270              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7271            1 :     spdm_context->local_context.capability.flags &=
    7272              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7273            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    7274            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    7275            1 :     assert_int_equal(
    7276              :         libspdm_secured_message_get_session_state(
    7277              :             spdm_context->session_info[0].secured_message_context),
    7278              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    7279            1 :     assert_int_equal(
    7280              :         spdm_context->session_info[0].mut_auth_requested,
    7281              :         SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST);
    7282            1 :     free(data);
    7283            1 : }
    7284              : 
    7285            1 : static void libspdm_test_requester_key_exchange_case25(void **state)
    7286              : {
    7287              :     libspdm_return_t status;
    7288              :     libspdm_test_context_t *spdm_test_context;
    7289              :     libspdm_context_t *spdm_context;
    7290              :     uint32_t session_id;
    7291              :     uint8_t heartbeat_period;
    7292              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7293              :     uint8_t slot_id_param;
    7294              :     void *data;
    7295              :     size_t data_size;
    7296              :     void *hash;
    7297              :     size_t hash_size;
    7298              : 
    7299            1 :     spdm_test_context = *state;
    7300            1 :     spdm_context = spdm_test_context->spdm_context;
    7301              : 
    7302            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7303            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7304              :     }
    7305              : 
    7306            1 :     spdm_test_context->case_id = 0x19;
    7307            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7308              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7309            1 :     spdm_context->connection_info.connection_state =
    7310              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7311            1 :     spdm_context->connection_info.capability.flags |=
    7312              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    7313            1 :     spdm_context->connection_info.capability.flags |=
    7314              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    7315            1 :     spdm_context->local_context.capability.flags |=
    7316              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    7317              : 
    7318            1 :     spdm_context->connection_info.capability.flags |=
    7319              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7320            1 :     spdm_context->local_context.capability.flags |=
    7321              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7322              : 
    7323            1 :     spdm_context->connection_info.capability.flags |=
    7324              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
    7325            1 :     spdm_context->local_context.capability.flags |=
    7326              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
    7327              : 
    7328            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7329              :                                                     m_libspdm_use_asym_algo, &data,
    7330              :                                                     &data_size, &hash, &hash_size);
    7331            1 :     libspdm_reset_message_a(spdm_context);
    7332            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    7333              :         m_libspdm_use_hash_algo;
    7334            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    7335              :         m_libspdm_use_asym_algo;
    7336            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    7337              :         m_libspdm_use_dhe_algo;
    7338            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    7339              :         m_libspdm_use_aead_algo;
    7340            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    7341              :         m_libspdm_use_measurement_hash_algo;
    7342              : 
    7343              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7344              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7345              :         data_size;
    7346              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7347              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7348              :                      data, data_size);
    7349              : #else
    7350            1 :     libspdm_hash_all(
    7351              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7352              :         data, data_size,
    7353            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7354            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7355            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7356            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7357              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7358              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7359              :         data, data_size,
    7360              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7361              : #endif
    7362              : 
    7363            1 :     heartbeat_period = 0;
    7364            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7365            1 :     status = libspdm_send_receive_key_exchange(
    7366              :         spdm_context,
    7367              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7368              :         &session_id, &heartbeat_period, &slot_id_param,
    7369              :         measurement_hash);
    7370              :     /* Clear Mut_auth flags */
    7371            1 :     spdm_context->connection_info.capability.flags &=
    7372              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7373            1 :     spdm_context->local_context.capability.flags &=
    7374              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7375            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    7376            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    7377            1 :     assert_int_equal(
    7378              :         libspdm_secured_message_get_session_state(
    7379              :             spdm_context->session_info[0].secured_message_context),
    7380              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    7381            1 :     assert_int_equal(
    7382              :         spdm_context->session_info[0].mut_auth_requested,
    7383              :         SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS);
    7384            1 :     free(data);
    7385            1 : }
    7386              : 
    7387            1 : static void libspdm_test_requester_key_exchange_case26(void **state)
    7388              : {
    7389              :     libspdm_return_t status;
    7390              :     libspdm_test_context_t *spdm_test_context;
    7391              :     libspdm_context_t *spdm_context;
    7392              :     uint32_t session_id;
    7393              :     uint8_t heartbeat_period;
    7394              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7395              :     uint8_t slot_id_param;
    7396              :     void *data;
    7397              :     size_t data_size;
    7398              :     void *hash;
    7399              :     size_t hash_size;
    7400              : 
    7401            1 :     spdm_test_context = *state;
    7402            1 :     spdm_context = spdm_test_context->spdm_context;
    7403              : 
    7404            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7405            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7406              :     }
    7407              : 
    7408            1 :     spdm_test_context->case_id = 0x1A;
    7409            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7410              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7411            1 :     spdm_context->connection_info.connection_state =
    7412              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7413            1 :     spdm_context->connection_info.capability.flags |=
    7414              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    7415            1 :     spdm_context->connection_info.capability.flags |=
    7416              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    7417            1 :     spdm_context->local_context.capability.flags |=
    7418              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    7419              : 
    7420            1 :     spdm_context->connection_info.capability.flags |=
    7421              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7422            1 :     spdm_context->local_context.capability.flags |=
    7423              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7424              : 
    7425              : 
    7426            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7427              :                                                     m_libspdm_use_asym_algo, &data,
    7428              :                                                     &data_size, &hash, &hash_size);
    7429            1 :     libspdm_reset_message_a(spdm_context);
    7430            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    7431              :         m_libspdm_use_hash_algo;
    7432            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    7433              :         m_libspdm_use_asym_algo;
    7434            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    7435              :         m_libspdm_use_dhe_algo;
    7436            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    7437              :         m_libspdm_use_aead_algo;
    7438            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    7439              :         m_libspdm_use_measurement_hash_algo;
    7440              : 
    7441              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7442              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7443              :         data_size;
    7444              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7445              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7446              :                      data, data_size);
    7447              : #else
    7448            1 :     libspdm_hash_all(
    7449              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7450              :         data, data_size,
    7451            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7452            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7453            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7454            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7455              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7456              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7457              :         data, data_size,
    7458              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7459              : #endif
    7460              : 
    7461            1 :     heartbeat_period = 0;
    7462            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7463            1 :     status = libspdm_send_receive_key_exchange(
    7464              :         spdm_context,
    7465              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7466              :         &session_id, &heartbeat_period, &slot_id_param,
    7467              :         measurement_hash);
    7468              :     /* Clear Mut_auth flags */
    7469            1 :     spdm_context->connection_info.capability.flags &=
    7470              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7471            1 :     spdm_context->local_context.capability.flags &=
    7472              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7473            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    7474            1 :     free(data);
    7475            1 : }
    7476              : 
    7477            1 : static void libspdm_test_requester_key_exchange_case27(void **state)
    7478              : {
    7479              :     libspdm_return_t status;
    7480              :     libspdm_test_context_t *spdm_test_context;
    7481              :     libspdm_context_t *spdm_context;
    7482              :     uint32_t session_id;
    7483              :     uint8_t heartbeat_period;
    7484              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7485              :     uint8_t slot_id_param;
    7486              :     void *data;
    7487              :     size_t data_size;
    7488              :     void *hash;
    7489              :     size_t hash_size;
    7490              : 
    7491            1 :     spdm_test_context = *state;
    7492            1 :     spdm_context = spdm_test_context->spdm_context;
    7493              : 
    7494            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7495            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7496              :     }
    7497              : 
    7498            1 :     spdm_test_context->case_id = 0x1B;
    7499            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7500              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7501            1 :     spdm_context->connection_info.connection_state =
    7502              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7503            1 :     spdm_context->connection_info.capability.flags |=
    7504              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    7505            1 :     spdm_context->connection_info.capability.flags |=
    7506              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    7507            1 :     spdm_context->local_context.capability.flags |=
    7508              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    7509              : 
    7510            1 :     spdm_context->connection_info.capability.flags |=
    7511              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7512            1 :     spdm_context->local_context.capability.flags |=
    7513              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7514              : 
    7515              : 
    7516            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7517              :                                                     m_libspdm_use_asym_algo, &data,
    7518              :                                                     &data_size, &hash, &hash_size);
    7519            1 :     libspdm_reset_message_a(spdm_context);
    7520            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    7521              :         m_libspdm_use_hash_algo;
    7522            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    7523              :         m_libspdm_use_asym_algo;
    7524            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    7525              :         m_libspdm_use_dhe_algo;
    7526            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    7527              :         m_libspdm_use_aead_algo;
    7528            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    7529              :         m_libspdm_use_measurement_hash_algo;
    7530              : 
    7531              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7532              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7533              :         data_size;
    7534              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7535              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7536              :                      data, data_size);
    7537              : #else
    7538            1 :     libspdm_hash_all(
    7539              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7540              :         data, data_size,
    7541            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7542            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7543            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7544            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7545              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7546              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7547              :         data, data_size,
    7548              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7549              : #endif
    7550              : 
    7551            1 :     heartbeat_period = 0;
    7552            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7553            1 :     status = libspdm_send_receive_key_exchange(
    7554              :         spdm_context,
    7555              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7556              :         &session_id, &heartbeat_period, &slot_id_param,
    7557              :         measurement_hash);
    7558              :     /*Clear Mut_auth flags*/
    7559            1 :     spdm_context->connection_info.capability.flags &=
    7560              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7561            1 :     spdm_context->local_context.capability.flags &=
    7562              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7563            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    7564            1 :     free(data);
    7565            1 : }
    7566              : 
    7567            1 : static void libspdm_test_requester_key_exchange_case28(void **state)
    7568              : {
    7569              :     libspdm_return_t status;
    7570              :     libspdm_test_context_t *spdm_test_context;
    7571              :     libspdm_context_t *spdm_context;
    7572              :     uint32_t session_id;
    7573              :     uint8_t heartbeat_period;
    7574              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7575              :     uint8_t slot_id_param;
    7576              :     void *data;
    7577              :     size_t data_size;
    7578              :     void *hash;
    7579              :     size_t hash_size;
    7580              : 
    7581            1 :     spdm_test_context = *state;
    7582            1 :     spdm_context = spdm_test_context->spdm_context;
    7583              : 
    7584            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7585            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7586              :     }
    7587              : 
    7588            1 :     spdm_test_context->case_id = 0x1C;
    7589            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7590              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7591            1 :     spdm_context->connection_info.connection_state =
    7592              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7593            1 :     spdm_context->connection_info.capability.flags |=
    7594              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    7595            1 :     spdm_context->connection_info.capability.flags |=
    7596              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
    7597            1 :     spdm_context->local_context.capability.flags |=
    7598              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    7599              : 
    7600            1 :     spdm_context->connection_info.capability.flags |=
    7601              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7602            1 :     spdm_context->local_context.capability.flags |=
    7603              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7604              : 
    7605              : 
    7606            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7607              :                                                     m_libspdm_use_asym_algo, &data,
    7608              :                                                     &data_size, &hash, &hash_size);
    7609            1 :     libspdm_reset_message_a(spdm_context);
    7610            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    7611              :         m_libspdm_use_hash_algo;
    7612            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    7613              :         m_libspdm_use_asym_algo;
    7614            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    7615              :         m_libspdm_use_dhe_algo;
    7616            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    7617              :         m_libspdm_use_aead_algo;
    7618            1 :     spdm_context->connection_info.algorithm.measurement_hash_algo =
    7619              :         m_libspdm_use_measurement_hash_algo;
    7620              : 
    7621              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7622              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7623              :         data_size;
    7624              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7625              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7626              :                      data, data_size);
    7627              : #else
    7628            1 :     libspdm_hash_all(
    7629              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7630              :         data, data_size,
    7631            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7632            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7633            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7634            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7635              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7636              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7637              :         data, data_size,
    7638              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7639              : #endif
    7640              : 
    7641            1 :     heartbeat_period = 0;
    7642            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7643            1 :     status = libspdm_send_receive_key_exchange(
    7644              :         spdm_context,
    7645              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7646              :         &session_id, &heartbeat_period, &slot_id_param,
    7647              :         measurement_hash);
    7648              :     /* Clear Mut_auth flags */
    7649            1 :     spdm_context->connection_info.capability.flags &=
    7650              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
    7651            1 :     spdm_context->local_context.capability.flags &=
    7652              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
    7653            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    7654            1 :     free(data);
    7655            1 : }
    7656              : 
    7657            1 : static void libspdm_test_requester_key_exchange_case29(void **state)
    7658              : {
    7659              :     libspdm_return_t status;
    7660              :     libspdm_test_context_t *spdm_test_context;
    7661              :     libspdm_context_t *spdm_context;
    7662              :     uint32_t session_id;
    7663              :     uint8_t heartbeat_period;
    7664              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7665              :     uint8_t slot_id_param;
    7666              :     void *data;
    7667              :     size_t data_size;
    7668              :     void *hash;
    7669              :     size_t hash_size;
    7670              : 
    7671            1 :     spdm_test_context = *state;
    7672            1 :     spdm_context = spdm_test_context->spdm_context;
    7673              : 
    7674            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7675            0 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7676              :     }
    7677              : 
    7678            1 :     spdm_test_context->case_id = 0x1D;
    7679            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7680              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7681            1 :     spdm_context->connection_info.connection_state =
    7682              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7683            1 :     spdm_context->connection_info.capability.flags |=
    7684              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    7685              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    7686            1 :     spdm_context->local_context.capability.flags |=
    7687              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    7688              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    7689            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7690              :                                                     m_libspdm_use_asym_algo, &data,
    7691              :                                                     &data_size, &hash, &hash_size);
    7692            1 :     libspdm_reset_message_a(spdm_context);
    7693            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    7694            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    7695            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    7696            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    7697              : 
    7698              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7699              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7700              :         data_size;
    7701              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7702              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7703              :                      data, data_size);
    7704              : #else
    7705            1 :     libspdm_hash_all(
    7706              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7707              :         data, data_size,
    7708            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7709            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7710            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7711            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7712              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7713              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7714              :         data, data_size,
    7715              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7716              : #endif
    7717              : 
    7718            1 :     heartbeat_period = 0;
    7719            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7720            1 :     status = libspdm_send_receive_key_exchange(
    7721              :         spdm_context,
    7722              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7723              :         &session_id, &heartbeat_period, &slot_id_param,
    7724              :         measurement_hash);
    7725            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    7726            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    7727            1 :     assert_int_equal(
    7728              :         libspdm_secured_message_get_session_state(
    7729              :             spdm_context->session_info[0].secured_message_context),
    7730              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    7731              : 
    7732              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7733              :     assert_int_equal(spdm_context->session_info[0].session_transcript.message_k.buffer_size,
    7734              :                      m_libspdm_local_buffer_size);
    7735              :     LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer (0x%x):\n",
    7736              :                    m_libspdm_local_buffer_size));
    7737              :     libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    7738              :     assert_memory_equal(spdm_context->session_info[0].session_transcript.message_k.buffer,
    7739              :                         m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    7740              : #endif
    7741              : 
    7742            1 :     free(data);
    7743            1 : }
    7744              : 
    7745            1 : static void libspdm_test_requester_key_exchange_case30(void **state)
    7746              : {
    7747              :     libspdm_return_t status;
    7748              :     libspdm_test_context_t *spdm_test_context;
    7749              :     libspdm_context_t *spdm_context;
    7750              :     uint32_t session_id;
    7751              :     uint8_t heartbeat_period;
    7752              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7753              :     uint8_t slot_id_param;
    7754              :     void *data;
    7755              :     size_t data_size;
    7756              :     void *hash;
    7757              :     size_t hash_size;
    7758              : 
    7759            1 :     spdm_test_context = *state;
    7760            1 :     spdm_context = spdm_test_context->spdm_context;
    7761            1 :     spdm_test_context->case_id = 0x1e;
    7762            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
    7763              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7764            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7765            1 :     spdm_context->connection_info.capability.flags |=
    7766              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    7767              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    7768            1 :     spdm_context->local_context.capability.flags |=
    7769              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    7770              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    7771            1 :     spdm_context->local_context.secured_message_version.spdm_version_count = 1;
    7772            1 :     spdm_context->local_context.secured_message_version.spdm_version[0] =
    7773              :         SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    7774            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7775              :                                                     m_libspdm_use_asym_algo, &data,
    7776              :                                                     &data_size, &hash, &hash_size);
    7777            1 :     libspdm_reset_message_a(spdm_context);
    7778            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    7779            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    7780            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    7781            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    7782            1 :     spdm_context->connection_info.algorithm.other_params_support =
    7783              :         SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
    7784            1 :     libspdm_session_info_init(spdm_context,
    7785            1 :                               spdm_context->session_info,
    7786              :                               INVALID_SESSION_ID, false);
    7787              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7788              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7789              :         data_size;
    7790              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7791              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7792              :                      data, data_size);
    7793              : #else
    7794            1 :     libspdm_hash_all(
    7795              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7796              :         data, data_size,
    7797            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7798            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7799            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7800            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7801              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7802              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7803              :         data, data_size,
    7804              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7805              : #endif
    7806              : 
    7807            1 :     heartbeat_period = 0;
    7808            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7809            1 :     status = libspdm_send_receive_key_exchange(
    7810              :         spdm_context,
    7811              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0,
    7812              :         SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_TERMINATION_POLICY_RUNTIME_UPDATE,
    7813              :         &session_id, &heartbeat_period, &slot_id_param,
    7814              :         measurement_hash);
    7815            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    7816            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    7817            1 :     assert_int_equal(
    7818              :         libspdm_secured_message_get_session_state(
    7819              :             spdm_context->session_info[0].secured_message_context),
    7820              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    7821            1 :     assert_int_equal(spdm_context->session_info[0].session_policy,
    7822              :                      SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_TERMINATION_POLICY_RUNTIME_UPDATE);
    7823            1 :     free(data);
    7824            1 : }
    7825              : 
    7826              : /**
    7827              :  * Test 31: Exercise the libspdm_send_receive_key_exchange_ex path
    7828              :  * Expected Behavior: requester_random_in is sent to Responder and correct responder_random is
    7829              :  *                    returned to Requester.
    7830              :  **/
    7831            1 : static void libspdm_test_requester_key_exchange_case31(void **state)
    7832              : {
    7833              :     libspdm_return_t status;
    7834              :     libspdm_test_context_t *spdm_test_context;
    7835              :     libspdm_context_t *spdm_context;
    7836              :     uint32_t session_id;
    7837              :     uint8_t heartbeat_period;
    7838              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7839              :     uint8_t slot_id_param;
    7840              :     void *data;
    7841              :     size_t data_size;
    7842              :     void *hash;
    7843              :     size_t hash_size;
    7844              :     uint8_t requester_random_in[SPDM_RANDOM_DATA_SIZE];
    7845              :     uint8_t requester_random[SPDM_RANDOM_DATA_SIZE];
    7846              :     uint8_t responder_random[SPDM_RANDOM_DATA_SIZE];
    7847              :     uint8_t responder_opaque_data[SPDM_MAX_OPAQUE_DATA_SIZE];
    7848              :     size_t responder_opaque_data_size;
    7849              :     uint8_t requester_opaque_data[SPDM_MAX_OPAQUE_DATA_SIZE];
    7850              :     size_t requester_opaque_data_size;
    7851              : 
    7852            1 :     spdm_test_context = *state;
    7853            1 :     spdm_context = spdm_test_context->spdm_context;
    7854            1 :     spdm_test_context->case_id = 0x1f;
    7855            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    7856              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7857            1 :     spdm_context->connection_info.connection_state =
    7858              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7859            1 :     spdm_context->connection_info.capability.flags |=
    7860              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    7861              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    7862            1 :     spdm_context->local_context.capability.flags |=
    7863              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    7864              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    7865            1 :     spdm_context->local_context.secured_message_version.spdm_version_count = 1;
    7866            1 :     spdm_context->local_context.secured_message_version.spdm_version[0] =
    7867              :         SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    7868            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    7869              :                                                     m_libspdm_use_asym_algo, &data,
    7870              :                                                     &data_size, &hash, &hash_size);
    7871            1 :     libspdm_reset_message_a(spdm_context);
    7872            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    7873              :         m_libspdm_use_hash_algo;
    7874            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    7875              :         m_libspdm_use_asym_algo;
    7876            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    7877              :         m_libspdm_use_dhe_algo;
    7878            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    7879              :         m_libspdm_use_aead_algo;
    7880              : 
    7881            1 :     if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    7882            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    7883              :     }
    7884              : 
    7885              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    7886              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    7887              :         data_size;
    7888              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    7889              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    7890              :                      data, data_size);
    7891              : #else
    7892            1 :     libspdm_hash_all(
    7893              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7894              :         data, data_size,
    7895            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    7896            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    7897            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    7898            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    7899              :         spdm_context->connection_info.algorithm.base_hash_algo,
    7900              :         spdm_context->connection_info.algorithm.base_asym_algo,
    7901              :         data, data_size,
    7902              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    7903              : #endif
    7904              : 
    7905           33 :     for (int index = 0; index < SPDM_RANDOM_DATA_SIZE; index++) {
    7906           32 :         requester_random_in[index] = 0x12;
    7907              :     }
    7908              : 
    7909            1 :     heartbeat_period = 0;
    7910            1 :     responder_opaque_data_size = sizeof(responder_opaque_data);
    7911            1 :     requester_opaque_data_size = sizeof(requester_opaque_data);
    7912            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7913            1 :     status = libspdm_send_receive_key_exchange_ex(
    7914              :         spdm_context,
    7915              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    7916              :         &session_id, &heartbeat_period, &slot_id_param,
    7917              :         measurement_hash, requester_random_in, requester_random, responder_random,
    7918              :         requester_opaque_data, requester_opaque_data_size,
    7919              :         responder_opaque_data, &responder_opaque_data_size);
    7920            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    7921            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    7922            1 :     assert_int_equal(
    7923              :         libspdm_secured_message_get_session_state(
    7924              :             spdm_context->session_info[0].secured_message_context),
    7925              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    7926              : 
    7927           33 :     for (int index = 0; index < SPDM_RANDOM_DATA_SIZE; index++) {
    7928           32 :         assert_int_equal(requester_random[index], requester_random_in[index]);
    7929           32 :         assert_int_equal(requester_random[index], 0x12);
    7930           32 :         assert_int_equal(responder_random[index], 0x5c);
    7931              :     }
    7932              : 
    7933            1 :     free(data);
    7934            1 : }
    7935              : 
    7936            1 : void libspdm_test_requester_key_exchange_case32(void **state)
    7937              : {
    7938              :     libspdm_return_t status;
    7939              :     libspdm_test_context_t *spdm_test_context;
    7940              :     libspdm_context_t *spdm_context;
    7941              :     uint32_t session_id;
    7942              :     uint8_t heartbeat_period;
    7943              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    7944              :     uint8_t slot_id_param;
    7945              :     void *data;
    7946              :     size_t data_size;
    7947              : 
    7948            1 :     spdm_test_context = *state;
    7949            1 :     spdm_context = spdm_test_context->spdm_context;
    7950            1 :     spdm_test_context->case_id = 0x20;
    7951            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
    7952              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    7953            1 :     spdm_context->connection_info.connection_state =
    7954              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    7955            1 :     spdm_context->connection_info.capability.flags |=
    7956              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    7957              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    7958            1 :     spdm_context->local_context.capability.flags |=
    7959              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    7960              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    7961            1 :     spdm_context->connection_info.capability.flags |=
    7962              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PUB_KEY_ID_CAP;
    7963            1 :     spdm_context->local_context.capability.flags |=
    7964              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
    7965            1 :     spdm_context->local_context.secured_message_version.spdm_version_count = 1;
    7966            1 :     spdm_context->local_context.secured_message_version.spdm_version[0] =
    7967              :         SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    7968            1 :     libspdm_read_responder_public_key(m_libspdm_use_asym_algo, &data, &data_size);
    7969            1 :     spdm_context->local_context.peer_public_key_provision = data;
    7970            1 :     spdm_context->local_context.peer_public_key_provision_size = data_size;
    7971              : 
    7972            1 :     libspdm_reset_message_a(spdm_context);
    7973            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    7974            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    7975            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    7976            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    7977            1 :     spdm_context->connection_info.algorithm.other_params_support =
    7978              :         SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
    7979            1 :     libspdm_session_info_init(spdm_context,
    7980            1 :                               spdm_context->session_info,
    7981              :                               INVALID_SESSION_ID, false);
    7982              : 
    7983            1 :     heartbeat_period = 0;
    7984            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    7985            1 :     status = libspdm_send_receive_key_exchange(
    7986              :         spdm_context,
    7987              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0xFF,
    7988              :         SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_TERMINATION_POLICY_RUNTIME_UPDATE,
    7989              :         &session_id, &heartbeat_period, &slot_id_param,
    7990              :         measurement_hash);
    7991            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    7992            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    7993            1 :     assert_int_equal(
    7994              :         libspdm_secured_message_get_session_state(
    7995              :             spdm_context->session_info[0].secured_message_context),
    7996              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    7997            1 :     assert_int_equal(spdm_context->session_info[0].session_policy,
    7998              :                      SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_TERMINATION_POLICY_RUNTIME_UPDATE);
    7999            1 :     free(data);
    8000            1 : }
    8001              : 
    8002            1 : static void libspdm_test_requester_key_exchange_case33(void **state)
    8003              : {
    8004              :     libspdm_return_t status;
    8005              :     libspdm_test_context_t *spdm_test_context;
    8006              :     libspdm_context_t *spdm_context;
    8007              :     uint32_t session_id;
    8008              :     uint8_t heartbeat_period;
    8009              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    8010              :     uint8_t slot_id_param;
    8011              :     void *data;
    8012              :     size_t data_size;
    8013              :     void *hash;
    8014              :     size_t hash_size;
    8015              : 
    8016            1 :     spdm_test_context = *state;
    8017            1 :     spdm_context = spdm_test_context->spdm_context;
    8018            1 :     spdm_test_context->case_id = 0x21;
    8019            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
    8020              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    8021            1 :     spdm_context->connection_info.connection_state =
    8022              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    8023            1 :     spdm_context->connection_info.capability.flags |=
    8024              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    8025              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    8026            1 :     spdm_context->local_context.capability.flags |=
    8027              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    8028              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    8029            1 :     spdm_context->local_context.secured_message_version.spdm_version_count = 1;
    8030            1 :     spdm_context->local_context.secured_message_version.spdm_version[0] =
    8031              :         SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    8032            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    8033              :                                                     m_libspdm_use_asym_algo, &data,
    8034              :                                                     &data_size, &hash, &hash_size);
    8035            1 :     libspdm_reset_message_a(spdm_context);
    8036            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    8037              :         m_libspdm_use_hash_algo;
    8038            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    8039              :         m_libspdm_use_asym_algo;
    8040            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    8041              :         m_libspdm_use_dhe_algo;
    8042            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    8043              :         m_libspdm_use_aead_algo;
    8044            1 :     spdm_context->connection_info.algorithm.other_params_support =
    8045              :         SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
    8046            1 :     libspdm_session_info_init(spdm_context,
    8047            1 :                               spdm_context->session_info,
    8048              :                               INVALID_SESSION_ID, false);
    8049              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    8050              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    8051              :         data_size;
    8052              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    8053              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    8054              :                      data, data_size);
    8055              : #else
    8056            1 :     libspdm_hash_all(
    8057              :         spdm_context->connection_info.algorithm.base_hash_algo,
    8058              :         data, data_size,
    8059            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    8060            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    8061            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    8062            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    8063              :         spdm_context->connection_info.algorithm.base_hash_algo,
    8064              :         spdm_context->connection_info.algorithm.base_asym_algo,
    8065              :         data, data_size,
    8066              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    8067              : #endif
    8068              : 
    8069            1 :     heartbeat_period = 0;
    8070            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    8071            1 :     status = libspdm_send_receive_key_exchange(
    8072              :         spdm_context,
    8073              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0,
    8074              :         SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_TERMINATION_POLICY_RUNTIME_UPDATE,
    8075              :         &session_id, &heartbeat_period, &slot_id_param,
    8076              :         measurement_hash);
    8077            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    8078            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    8079            1 :     assert_int_equal(
    8080              :         libspdm_secured_message_get_session_state(
    8081              :             spdm_context->session_info[0].secured_message_context),
    8082              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    8083            1 :     assert_int_equal(spdm_context->session_info[0].session_policy,
    8084              :                      SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_TERMINATION_POLICY_RUNTIME_UPDATE);
    8085            1 :     free(data);
    8086            1 : }
    8087              : 
    8088              : /**
    8089              :  * Test 34: The secured message version is 1.2 and the Integrator has set the sequence number
    8090              :  *          endianness to big-endian. After key exchange the session's endianness will be
    8091              :  *          little-endian as 277 1.2 only supports little-endian.
    8092              :  * Expected Behavior: A successful key exchange with the session's endianness set to little-endian.
    8093              :  **/
    8094            1 : static void libspdm_test_requester_key_exchange_case34(void **state)
    8095              : {
    8096              :     libspdm_return_t status;
    8097              :     libspdm_test_context_t *spdm_test_context;
    8098              :     libspdm_context_t *spdm_context;
    8099              :     uint32_t session_id;
    8100              :     uint8_t heartbeat_period;
    8101              :     uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
    8102              :     uint8_t slot_id_param;
    8103              :     void *data;
    8104              :     size_t data_size;
    8105              :     void *hash;
    8106              :     size_t hash_size;
    8107              : 
    8108            1 :     spdm_test_context = *state;
    8109            1 :     spdm_context = spdm_test_context->spdm_context;
    8110              : 
    8111            1 :     if (spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
    8112            1 :         libspdm_free_session_id(spdm_context,0xFFFFFFFF);
    8113              :     }
    8114              : 
    8115            1 :     spdm_test_context->case_id = 0x22;
    8116            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    8117              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    8118            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    8119            1 :     spdm_context->connection_info.capability.flags |=
    8120              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
    8121              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    8122            1 :     spdm_context->local_context.capability.flags |=
    8123              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
    8124              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    8125            1 :     spdm_context->local_context.secured_message_version.spdm_version_count = 1;
    8126            1 :     spdm_context->local_context.secured_message_version.spdm_version[0] =
    8127              :         SECURED_SPDM_VERSION_12 << SPDM_VERSION_NUMBER_SHIFT_BIT;
    8128              : 
    8129              :     /* Set 277 sequence number endianness to big-endian. */
    8130            1 :     spdm_context->sequence_number_endian = LIBSPDM_DATA_SESSION_SEQ_NUM_ENC_BIG_DEC_BIG;
    8131              : 
    8132            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    8133              :                                                     m_libspdm_use_asym_algo, &data,
    8134              :                                                     &data_size, &hash, &hash_size);
    8135            1 :     libspdm_reset_message_a(spdm_context);
    8136            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    8137            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    8138            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    8139            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    8140              : 
    8141              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    8142              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
    8143              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    8144              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    8145              :                      data, data_size);
    8146              : #else
    8147            1 :     libspdm_hash_all(
    8148              :         spdm_context->connection_info.algorithm.base_hash_algo,
    8149              :         data, data_size,
    8150            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    8151            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    8152            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    8153            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    8154              :         spdm_context->connection_info.algorithm.base_hash_algo,
    8155              :         spdm_context->connection_info.algorithm.base_asym_algo,
    8156              :         data, data_size,
    8157              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    8158              : #endif
    8159              : 
    8160            1 :     heartbeat_period = 0;
    8161            1 :     libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
    8162            1 :     status = libspdm_send_receive_key_exchange(
    8163              :         spdm_context,
    8164              :         SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
    8165              :         &session_id, &heartbeat_period, &slot_id_param,
    8166              :         measurement_hash);
    8167            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    8168            1 :     assert_int_equal(session_id, 0xFFFFFFFF);
    8169            1 :     assert_int_equal(
    8170              :         libspdm_secured_message_get_session_state(
    8171              :             spdm_context->session_info[0].secured_message_context),
    8172              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    8173            1 :     assert_int_equal(((libspdm_secured_message_context_t *)spdm_context->session_info->
    8174              :                       secured_message_context)->sequence_number_endian,
    8175              :                      LIBSPDM_DATA_SESSION_SEQ_NUM_ENC_LITTLE_DEC_LITTLE);
    8176            1 :     free(data);
    8177            1 : }
    8178              : 
    8179            1 : int libspdm_requester_key_exchange_test_main(void)
    8180              : {
    8181            1 :     const struct CMUnitTest spdm_requester_key_exchange_tests[] = {
    8182              :         /* SendRequest failed*/
    8183              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case1),
    8184              :         /* Successful response*/
    8185              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case2),
    8186              :         /* connection_state check failed*/
    8187              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case3),
    8188              :         /* Error response: SPDM_ERROR_CODE_INVALID_REQUEST*/
    8189              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case4),
    8190              :         /* Always SPDM_ERROR_CODE_BUSY*/
    8191              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case5),
    8192              :         /* SPDM_ERROR_CODE_BUSY + Successful response*/
    8193              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case6),
    8194              :         /* Error response: SPDM_ERROR_CODE_REQUEST_RESYNCH*/
    8195              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case7),
    8196              :         /* Always SPDM_ERROR_CODE_RESPONSE_NOT_READY*/
    8197              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case8),
    8198              :         /* SPDM_ERROR_CODE_RESPONSE_NOT_READY + Successful response*/
    8199              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case9),
    8200              :         /* Unexpected errors*/
    8201              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case10),
    8202              :         /* Buffer reset*/
    8203              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case11),
    8204              :         /* Measurement hash 1, returns a measurement hash*/
    8205              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case12),
    8206              :         /* Measurement hash 1, returns a 0x00 array (no TCB components)*/
    8207              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case13),
    8208              :         /* Measurement hash FF, returns a measurement_hash*/
    8209              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case14),
    8210              :         /* Measurement hash 1, returns no measurement_hash*/
    8211              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case15),
    8212              :         /* Measurement hash FF, returns no measurement_hash*/
    8213              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case16),
    8214              :         /* Measurement hash not requested, returns a measurement_hash*/
    8215              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case17),
    8216              :         /* Wrong signature*/
    8217              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case18),
    8218              :         /* Requester and Responder Handshake in the clear set, no ResponderVerifyData*/
    8219              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case19),
    8220              :         /* Heartbeat not supported, heartbeat period different from 0 sent*/
    8221              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case20),
    8222              :         /* Heartbeat supported, heartbeat period different from 0 sent*/
    8223              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case21),
    8224              :         /* Heartbeat supported, heartbeat period 0 sent NOTE: This should disable heartbeat*/
    8225              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case22),
    8226              :         /* Muth Auth requested*/
    8227              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case23),
    8228              :         /* Muth Auth requested with Encapsulated request*/
    8229              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case24),
    8230              :         /* Muth Auth requested with implicit get digest*/
    8231              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case25),
    8232              :         /* Muth Auth requested with Encapsulated request and bit 0 set*/
    8233              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case26),
    8234              :         /* Muth Auth requested with implicit get digest and bit 0 set*/
    8235              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case27),
    8236              :         /* Muth Auth requested with Encapsulated request and Muth Auth requested with implicit get digest simultaneously*/
    8237              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case28),
    8238              :         /* Buffer verification*/
    8239              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case29),
    8240              :         /* Successful response V1.2*/
    8241              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case30),
    8242              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case31),
    8243              :         /* Successful response using provisioned public key (slot_id 0xFF) */
    8244              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case32),
    8245              :         /* OpaqueData only supports OpaqueDataFmt1, Success Case */
    8246              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case33),
    8247              :         cmocka_unit_test(libspdm_test_requester_key_exchange_case34),
    8248              :     };
    8249              : 
    8250            1 :     libspdm_test_context_t test_context = {
    8251              :         LIBSPDM_TEST_CONTEXT_VERSION,
    8252              :         true,
    8253              :         libspdm_requester_key_exchange_test_send_message,
    8254              :         libspdm_requester_key_exchange_test_receive_message,
    8255              :     };
    8256              : 
    8257            1 :     libspdm_setup_test_context(&test_context);
    8258              : 
    8259            1 :     return cmocka_run_group_tests(spdm_requester_key_exchange_tests,
    8260              :                                   libspdm_unit_test_group_setup,
    8261              :                                   libspdm_unit_test_group_teardown);
    8262              : }
    8263              : 
    8264              : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
        

Generated by: LCOV version 2.0-1