LCOV - code coverage report
Current view: top level - library/spdm_secured_message_lib - libspdm_secmes_key_exchange.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.1 % 17 16
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2021-2022 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 "internal/libspdm_secured_message_lib.h"
       8              : 
       9              : /**
      10              :  * Allocates and Initializes one Diffie-Hellman Ephemeral (DHE) context for subsequent use,
      11              :  * based upon negotiated DHE algorithm.
      12              :  *
      13              :  * @param  dhe_named_group                SPDM dhe_named_group
      14              :  * @param  is_initiator                   if the caller is initiator.
      15              :  *                                       true: initiator
      16              :  *                                       false: not an initiator
      17              :  *
      18              :  * @return  Pointer to the Diffie-Hellman context that has been initialized.
      19              :  **/
      20          102 : void *libspdm_secured_message_dhe_new(spdm_version_number_t spdm_version,
      21              :                                       uint16_t dhe_named_group, bool is_initiator)
      22              : {
      23          102 :     return libspdm_dhe_new(spdm_version, dhe_named_group, is_initiator);
      24              : }
      25              : 
      26              : /**
      27              :  * Release the specified DHE context,
      28              :  * based upon negotiated DHE algorithm.
      29              :  *
      30              :  * @param  dhe_named_group                SPDM dhe_named_group
      31              :  * @param  dhe_context                   Pointer to the DHE context to be released.
      32              :  **/
      33          102 : void libspdm_secured_message_dhe_free(uint16_t dhe_named_group, void *dhe_context)
      34              : {
      35          102 :     libspdm_dhe_free(dhe_named_group, dhe_context);
      36          102 : }
      37              : 
      38              : /**
      39              :  * Generates DHE public key,
      40              :  * based upon negotiated DHE algorithm.
      41              :  *
      42              :  * This function generates random secret exponent, and computes the public key, which is
      43              :  * returned via parameter public_key and public_key_size. DH context is updated accordingly.
      44              :  * If the public_key buffer is too small to hold the public key, false is returned and
      45              :  * public_key_size is set to the required buffer size to obtain the public key.
      46              :  *
      47              :  * @param  dhe_named_group                SPDM dhe_named_group
      48              :  * @param  dhe_context                   Pointer to the DHE context.
      49              :  * @param  public_key                    Pointer to the buffer to receive generated public key.
      50              :  * @param  public_key_size                On input, the size of public_key buffer in bytes.
      51              :  *                                     On output, the size of data returned in public_key buffer in bytes.
      52              :  *
      53              :  * @retval true   DHE public key generation succeeded.
      54              :  * @retval false  DHE public key generation failed.
      55              :  * @retval false  public_key_size is not large enough.
      56              :  **/
      57          102 : bool libspdm_secured_message_dhe_generate_key(uint16_t dhe_named_group,
      58              :                                               void *dhe_context,
      59              :                                               uint8_t *public_key,
      60              :                                               size_t *public_key_size)
      61              : {
      62          102 :     return libspdm_dhe_generate_key(dhe_named_group, dhe_context, public_key, public_key_size);
      63              : }
      64              : 
      65              : /**
      66              :  * Computes exchanged common key,
      67              :  * based upon negotiated DHE algorithm.
      68              :  *
      69              :  * Given peer's public key, this function computes the exchanged common key, based on its own
      70              :  * context including value of prime modulus and random secret exponent.
      71              :  *
      72              :  * @param  dhe_named_group                SPDM dhe_named_group
      73              :  * @param  dhe_context                   Pointer to the DHE context.
      74              :  * @param  peer_public_key                Pointer to the peer's public key.
      75              :  * @param  peer_public_key_size            size of peer's public key in bytes.
      76              :  * @param  key                          Pointer to the buffer to receive generated key.
      77              :  * @param  spdm_secured_message_context    A pointer to the SPDM secured message context.
      78              :  *
      79              :  * @retval true   DHE exchanged key generation succeeded.
      80              :  * @retval false  DHE exchanged key generation failed.
      81              :  * @retval false  key_size is not large enough.
      82              :  **/
      83           31 : bool libspdm_secured_message_dhe_compute_key(
      84              :     uint16_t dhe_named_group, void *dhe_context,
      85              :     const uint8_t *peer_public, size_t peer_public_size,
      86              :     void *spdm_secured_message_context)
      87              : {
      88              :     libspdm_secured_message_context_t *secured_message_context;
      89              :     uint8_t final_key[LIBSPDM_MAX_DHE_KEY_SIZE];
      90              :     size_t final_key_size;
      91              :     bool ret;
      92              : 
      93           31 :     secured_message_context = spdm_secured_message_context;
      94              : 
      95           31 :     final_key_size = sizeof(final_key);
      96           31 :     ret = libspdm_dhe_compute_key(dhe_named_group, dhe_context, peer_public,
      97              :                                   peer_public_size, final_key,
      98              :                                   &final_key_size);
      99           31 :     if (!ret) {
     100            0 :         return ret;
     101              :     }
     102           31 :     libspdm_copy_mem(secured_message_context->master_secret.dhe_secret,
     103              :                      sizeof(secured_message_context->master_secret.dhe_secret),
     104              :                      final_key, final_key_size);
     105           31 :     libspdm_zero_mem(final_key, final_key_size);
     106           31 :     secured_message_context->dhe_key_size = final_key_size;
     107           31 :     return true;
     108              : }
        

Generated by: LCOV version 2.0-1