LCOV - code coverage report
Current view: top level - library/spdm_crypt_lib/fips - libspdm_selftest_ecdsa.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 53.1 % 64 34
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2023-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 "internal/libspdm_crypt_lib.h"
       8              : #include "internal/libspdm_common_lib.h"
       9              : #include "internal/libspdm_fips_lib.h"
      10              : 
      11              : #if LIBSPDM_FIPS_MODE
      12              : 
      13              : /*hardcode random for ecdsa*/
      14            4 : int libspdm_hardcode_random_number_ecdsa(void *rng_state, unsigned char *output, size_t len)
      15              : {
      16              :     /*Random number*/
      17            4 :     const uint8_t random[] = {
      18              :         0x01, 0x5b, 0x92, 0x22, 0xfc, 0x35, 0xc5, 0x2b,
      19              :         0x79, 0x1c, 0xcd, 0x37, 0x07, 0x7d, 0xae, 0x6a,
      20              :         0x88, 0x1f, 0x7d, 0x03, 0x30, 0x92, 0x67, 0x47,
      21              :         0x0b, 0xce, 0x31, 0x7c, 0x46, 0xdd, 0x90, 0xbf
      22              :     };
      23            4 :     libspdm_copy_mem(output, len, random, sizeof(random));
      24              : 
      25            4 :     return 0;
      26              : }
      27              : 
      28              : /**
      29              :  * ECDSA self_test
      30              :  **/
      31            1 : bool libspdm_fips_selftest_ecdsa(void *fips_selftest_context)
      32              : {
      33            1 :     bool result = true;
      34              : 
      35              : #if LIBSPDM_ECDSA_SUPPORT
      36            1 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
      37            1 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
      38              : 
      39              :     /* any test fail cause the FIPS fail*/
      40            1 :     if (context->tested_algo != context->self_test_result) {
      41            0 :         return false;
      42              :     }
      43              : 
      44              :     /* check if run before.*/
      45            1 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_ECDSA) != 0) {
      46            0 :         return true;
      47              :     }
      48              : 
      49              :     uint8_t signature[32 * 2];
      50              :     size_t sig_size;
      51              :     void *ec_context;
      52              : 
      53            1 :     sig_size = sizeof(signature);
      54            1 :     libspdm_zero_mem(signature, sig_size);
      55              : 
      56              :     /* Message hash */
      57            1 :     const uint8_t message_hash[] = {
      58              :         0x9a, 0x1b, 0x6d, 0xe3, 0xff, 0xd0, 0x7e, 0x77,
      59              :         0xe2, 0x54, 0xc6, 0x27, 0x9a, 0xc3, 0x28, 0x07,
      60              :         0x42, 0x43, 0x1a, 0x32, 0xc8, 0xaf, 0x0c, 0x87,
      61              :         0x94, 0xff, 0x40, 0x75, 0x09, 0xfe, 0x40, 0xd8
      62              :     };
      63              : 
      64              :     /* Private Key */
      65            1 :     const uint8_t priv_key[] = {
      66              :         0xd6, 0x84, 0xd1, 0x7c, 0xe3, 0x6b, 0xe7, 0x08,
      67              :         0xbc, 0xd9, 0x89, 0x3f, 0xbb, 0xf4, 0xf2, 0xcf,
      68              :         0x8d, 0x7f, 0xd4, 0x72, 0xbc, 0xfb, 0x54, 0x29,
      69              :         0xd9, 0x86, 0xe2, 0x86, 0xc2, 0x38, 0xe5, 0xed
      70              :     };
      71              : 
      72              :     /* Public Key */
      73            1 :     const uint8_t public_key[] = {
      74              :         0x54, 0xbc, 0x5f, 0x6b, 0x70, 0x9b, 0x29, 0x5c,
      75              :         0xa9, 0x43, 0xd0, 0xb7, 0xf3, 0xa2, 0x4b, 0xf0,
      76              :         0x76, 0xb1, 0xd1, 0x9f, 0x55, 0x6a, 0x4e, 0xa0,
      77              :         0x40, 0x54, 0xd2, 0xb1, 0x2f, 0x0f, 0xc1, 0x6d,
      78              :         0xe7, 0x53, 0xe1, 0x3a, 0xd9, 0xb9, 0x2d, 0xd6,
      79              :         0x3a, 0xda, 0x9d, 0xa9, 0xa9, 0x4e, 0xdd, 0x30,
      80              :         0x60, 0x24, 0x9f, 0x9d, 0xcb, 0xfc, 0x1a, 0x56,
      81              :         0x35, 0x63, 0x64, 0xe2, 0x64, 0xcf, 0x00, 0xed
      82              :     };
      83              : 
      84              :     /* Expected signature*/
      85            1 :     const uint8_t expected_signature[] = {
      86              :         0xe1, 0x6a, 0xe9, 0x76, 0x61, 0x97, 0x8e, 0xe9,
      87              :         0xc7, 0x0f, 0xe0, 0x20, 0xc0, 0x65, 0xe1, 0x6c,
      88              :         0x89, 0x6f, 0x24, 0x6b, 0x4e, 0x88, 0x10, 0xcd,
      89              :         0xb4, 0x9a, 0xcc, 0x20, 0xcf, 0xa5, 0xb0, 0xc9,
      90              :         0xc1, 0x02, 0x4b, 0xa7, 0x41, 0xef, 0x51, 0x8f,
      91              :         0xe3, 0x11, 0xec, 0x95, 0xe2, 0xf4, 0x83, 0x97,
      92              :         0x3d, 0x32, 0x72, 0xf6, 0x4b, 0x34, 0xd3, 0x9f,
      93              :         0x25, 0x6a, 0x12, 0x3b, 0x7c, 0x87, 0xc4, 0x4d
      94              :     };
      95              : 
      96            1 :     ec_context = libspdm_ec_new_by_nid(LIBSPDM_CRYPTO_NID_ECDSA_NIST_P256);
      97            1 :     if (ec_context == NULL) {
      98            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ec_context new failed \n"));
      99            0 :         result = false;
     100            0 :         goto update;
     101              :     }
     102              : 
     103            1 :     result = libspdm_ec_set_pub_key(ec_context, public_key, sizeof(public_key));
     104            1 :     if (!result) {
     105            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ec_context set pub_key failed \n"));
     106            0 :         libspdm_ec_free(ec_context);
     107            0 :         result = false;
     108            0 :         goto update;
     109              :     }
     110              : 
     111            1 :     result = libspdm_ec_set_priv_key(ec_context, priv_key, sizeof(priv_key));
     112            1 :     if (!result) {
     113            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ec_context set priv_key failed \n"));
     114            0 :         libspdm_ec_free(ec_context);
     115            0 :         result = false;
     116            0 :         goto update;
     117              :     }
     118              : 
     119              :     /*ECDSA KAT test*/
     120            1 :     result = libspdm_ecdsa_sign_ex(ec_context, LIBSPDM_CRYPTO_NID_SHA256,
     121              :                                    message_hash, sizeof(message_hash),
     122              :                                    signature, &sig_size,
     123              :                                    libspdm_hardcode_random_number_ecdsa);
     124            1 :     if (!result) {
     125            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDSA sign failed \n"));
     126            0 :         libspdm_ec_free(ec_context);
     127            0 :         result = false;
     128            0 :         goto update;
     129              :     }
     130              : 
     131            1 :     if (sig_size != sizeof(expected_signature)) {
     132            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDSA KAT failed \n"));
     133            0 :         libspdm_ec_free(ec_context);
     134            0 :         result = false;
     135            0 :         goto update;
     136              :     }
     137              : 
     138            1 :     if (!libspdm_consttime_is_mem_equal(signature, expected_signature,
     139              :                                         sizeof(expected_signature))) {
     140            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDSA KAT failed \n"));
     141            0 :         libspdm_ec_free(ec_context);
     142            0 :         result = false;
     143            0 :         goto update;
     144              :     }
     145              : 
     146            1 :     result = libspdm_ecdsa_verify(ec_context, LIBSPDM_CRYPTO_NID_SHA256,
     147              :                                   message_hash, sizeof(message_hash),
     148              :                                   signature, sig_size);
     149            1 :     if (!result) {
     150            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDSA selftest failed \n"));
     151            0 :         libspdm_ec_free(ec_context);
     152            0 :         result = false;
     153            0 :         goto update;
     154              :     }
     155              : 
     156            1 :     libspdm_ec_free(ec_context);
     157              : 
     158            1 : update:
     159              :     /* mark it as tested*/
     160            1 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_ECDSA;
     161              : 
     162              :     /* record test result*/
     163            1 :     if (result) {
     164            1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_ECDSA;
     165              :     } else {
     166            0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_ECDSA;
     167              :     }
     168              : 
     169              : #endif/*LIBSPDM_ECDSA_SUPPORT*/
     170              : 
     171            1 :     return result;
     172              : }
     173              : 
     174              : #endif/*LIBSPDM_FIPS_MODE*/
        

Generated by: LCOV version 2.0-1