LCOV - code coverage report
Current view: top level - os_stub/spdm_device_secret_lib_sample - sign.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 54.2 % 48 26
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2024 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 <stdarg.h>
       8              : #include <stddef.h>
       9              : #include <setjmp.h>
      10              : #include <stdint.h>
      11              : #include <stdlib.h>
      12              : #include <stdio.h>
      13              : #include <assert.h>
      14              : #include <string.h>
      15              : 
      16              : #include <base.h>
      17              : #include "library/memlib.h"
      18              : #include "spdm_device_secret_lib_internal.h"
      19              : #include "internal/libspdm_common_lib.h"
      20              : 
      21              : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
      22           35 : bool libspdm_requester_data_sign(
      23              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
      24              :     void *spdm_context,
      25              : #endif
      26              :     spdm_version_number_t spdm_version, uint8_t op_code,
      27              :     uint16_t req_base_asym_alg,
      28              :     uint32_t base_hash_algo, bool is_data_hash,
      29              :     const uint8_t *message, size_t message_size,
      30              :     uint8_t *signature, size_t *sig_size)
      31              : {
      32              :     void *context;
      33              :     bool result;
      34              : 
      35              : #if !LIBSPDM_PRIVATE_KEY_MODE_RAW_KEY_ONLY
      36           35 :     if (g_private_key_mode) {
      37              :         void *private_pem;
      38              :         size_t private_pem_size;
      39              : 
      40           35 :         result = libspdm_read_requester_private_key(
      41              :             req_base_asym_alg, &private_pem, &private_pem_size);
      42           35 :         if (!result) {
      43            0 :             return false;
      44              :         }
      45              : 
      46           35 :         result = libspdm_req_asym_get_private_key_from_pem(req_base_asym_alg,
      47              :                                                            private_pem,
      48              :                                                            private_pem_size, NULL,
      49              :                                                            &context);
      50           35 :         if (!result) {
      51            0 :             libspdm_zero_mem(private_pem, private_pem_size);
      52            0 :             free(private_pem);
      53            0 :             return false;
      54              :         }
      55              : 
      56           35 :         if (is_data_hash) {
      57           13 :             result = libspdm_req_asym_sign_hash(spdm_version, op_code, req_base_asym_alg,
      58              :                                                 base_hash_algo, context,
      59              :                                                 message, message_size, signature, sig_size);
      60              :         } else {
      61           22 :             result = libspdm_req_asym_sign(spdm_version, op_code, req_base_asym_alg,
      62              :                                            base_hash_algo, context,
      63              :                                            message, message_size,
      64              :                                            signature, sig_size);
      65              :         }
      66           35 :         libspdm_req_asym_free(req_base_asym_alg, context);
      67           35 :         libspdm_zero_mem(private_pem, private_pem_size);
      68           35 :         free(private_pem);
      69              :     } else {
      70              : #endif
      71            0 :     result = libspdm_get_requester_private_key_from_raw_data(req_base_asym_alg, &context);
      72            0 :     if (!result) {
      73            0 :         return false;
      74              :     }
      75              : 
      76            0 :     if (is_data_hash) {
      77            0 :         result = libspdm_req_asym_sign_hash(spdm_version, op_code, req_base_asym_alg,
      78              :                                             base_hash_algo, context,
      79              :                                             message, message_size, signature, sig_size);
      80              :     } else {
      81            0 :         result = libspdm_req_asym_sign(spdm_version, op_code, req_base_asym_alg,
      82              :                                        base_hash_algo, context,
      83              :                                        message, message_size,
      84              :                                        signature, sig_size);
      85              :     }
      86            0 :     libspdm_req_asym_free(req_base_asym_alg, context);
      87              : #if !LIBSPDM_PRIVATE_KEY_MODE_RAW_KEY_ONLY
      88              : }
      89              : #endif
      90              : 
      91              : #if LIBSPDM_SECRET_LIB_SIGN_LITTLE_ENDIAN
      92              :     if ((spdm_version >> SPDM_VERSION_NUMBER_SHIFT_BIT) <= SPDM_MESSAGE_VERSION_11) {
      93              :         if (result) {
      94              :             libspdm_copy_signature_swap_endian(
      95              :                 req_base_asym_alg, signature, *sig_size, signature, *sig_size);
      96              :         }
      97              :     }
      98              : #endif
      99              : 
     100           35 :     return result;
     101              : }
     102              : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
     103              : 
     104          138 : bool libspdm_responder_data_sign(
     105              : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
     106              :     void *spdm_context,
     107              : #endif
     108              :     spdm_version_number_t spdm_version, uint8_t op_code,
     109              :     uint32_t base_asym_algo,
     110              :     uint32_t base_hash_algo, bool is_data_hash,
     111              :     const uint8_t *message, size_t message_size,
     112              :     uint8_t *signature, size_t *sig_size)
     113              : {
     114              :     void *context;
     115              :     bool result;
     116              : #if !LIBSPDM_PRIVATE_KEY_MODE_RAW_KEY_ONLY
     117          138 :     if (g_private_key_mode) {
     118              :         void *private_pem;
     119              :         size_t private_pem_size;
     120              : 
     121          138 :         result = libspdm_read_responder_private_key(
     122              :             base_asym_algo, &private_pem, &private_pem_size);
     123          138 :         if (!result) {
     124            0 :             return false;
     125              :         }
     126              : 
     127          138 :         result = libspdm_asym_get_private_key_from_pem(
     128              :             base_asym_algo, private_pem, private_pem_size, NULL, &context);
     129          138 :         if (!result) {
     130            0 :             libspdm_zero_mem(private_pem, private_pem_size);
     131            0 :             free(private_pem);
     132            0 :             return false;
     133              :         }
     134              : 
     135          138 :         if (is_data_hash) {
     136           40 :             result = libspdm_asym_sign_hash(spdm_version, op_code, base_asym_algo, base_hash_algo,
     137              :                                             context,
     138              :                                             message, message_size, signature, sig_size);
     139              :         } else {
     140           98 :             result = libspdm_asym_sign(spdm_version, op_code, base_asym_algo,
     141              :                                        base_hash_algo, context,
     142              :                                        message, message_size,
     143              :                                        signature, sig_size);
     144              :         }
     145          138 :         libspdm_asym_free(base_asym_algo, context);
     146          138 :         libspdm_zero_mem(private_pem, private_pem_size);
     147          138 :         free(private_pem);
     148              :     } else {
     149              : #endif
     150            0 :     result = libspdm_get_responder_private_key_from_raw_data(base_asym_algo, &context);
     151            0 :     if (!result) {
     152            0 :         return false;
     153              :     }
     154              : 
     155            0 :     if (is_data_hash) {
     156            0 :         result = libspdm_asym_sign_hash(spdm_version, op_code, base_asym_algo, base_hash_algo,
     157              :                                         context,
     158              :                                         message, message_size, signature, sig_size);
     159              :     } else {
     160            0 :         result = libspdm_asym_sign(spdm_version, op_code, base_asym_algo,
     161              :                                    base_hash_algo, context,
     162              :                                    message, message_size,
     163              :                                    signature, sig_size);
     164              :     }
     165            0 :     libspdm_asym_free(base_asym_algo, context);
     166              : #if !LIBSPDM_PRIVATE_KEY_MODE_RAW_KEY_ONLY
     167              : }
     168              : #endif
     169              : 
     170              : #if LIBSPDM_SECRET_LIB_SIGN_LITTLE_ENDIAN
     171              :     if ((spdm_version >> SPDM_VERSION_NUMBER_SHIFT_BIT) <= SPDM_MESSAGE_VERSION_11) {
     172              :         if (result) {
     173              :             libspdm_copy_signature_swap_endian(
     174              :                 base_asym_algo, signature, *sig_size, signature, *sig_size);
     175              :         }
     176              :     }
     177              : #endif
     178              : 
     179          138 :     return result;
     180              : }
        

Generated by: LCOV version 2.0-1