LCOV - code coverage report
Current view: top level - library/spdm_crypt_lib/fips - libspdm_selftest_hmac.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 73.1 % 78 57
Test Date: 2026-02-22 08:11:49 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2023-2026 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            2 : bool libspdm_fips_selftest_hmac_sha256(void *fips_selftest_context)
      14              : {
      15            2 :     bool result = true;
      16              : 
      17              : #if LIBSPDM_SHA256_SUPPORT
      18            2 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
      19            2 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
      20              : 
      21              :     /* any test fail cause the FIPS fail*/
      22            2 :     if (context->tested_algo != context->self_test_result) {
      23            0 :         return false;
      24              :     }
      25              : 
      26              :     /* check if run before.*/
      27            2 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256) != 0) {
      28            1 :         return true;
      29              :     }
      30              : 
      31            1 :     const uint8_t key[32] = {0};
      32            1 :     const uint8_t msg[32] = {0};
      33              : 
      34              :     uint8_t hmac_256_result[32];
      35            1 :     const uint8_t hmac_sha256_answer[] = {
      36              :         0x33, 0xad, 0x0a, 0x1c, 0x60, 0x7e, 0xc0, 0x3b,
      37              :         0x09, 0xe6, 0xcd, 0x98, 0x93, 0x68, 0x0c, 0xe2,
      38              :         0x10, 0xad, 0xf3, 0x00, 0xaa, 0x1f, 0x26, 0x60,
      39              :         0xe1, 0xb2, 0x2e, 0x10, 0xf1, 0x70, 0xf9, 0x2a
      40              :     };
      41            1 :     libspdm_zero_mem(hmac_256_result, sizeof(hmac_256_result));
      42            1 :     result = libspdm_hmac_sha256_all(msg, sizeof(msg), key, sizeof(key), hmac_256_result);
      43            1 :     if (!result) {
      44            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha256 failed \n"));
      45            0 :         goto update;
      46              :     }
      47              : 
      48            1 :     if (!libspdm_consttime_is_mem_equal(hmac_256_result, hmac_sha256_answer,
      49              :                                         sizeof(hmac_sha256_answer))) {
      50            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha256 KAT failed \n"));
      51            0 :         result = false;
      52            0 :         goto update;
      53              :     }
      54              : 
      55            1 : update:
      56              :     /* mark it as tested*/
      57            1 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
      58              : 
      59              :     /* record test result*/
      60            1 :     if (result) {
      61            1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
      62              :     } else {
      63            0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
      64              :     }
      65              : 
      66              : #endif/*LIBSPDM_SHA256_SUPPORT*/
      67              : 
      68            1 :     return result;
      69              : }
      70              : 
      71            2 : bool libspdm_fips_selftest_hmac_sha384(void *fips_selftest_context)
      72              : {
      73            2 :     bool result = true;
      74              : 
      75              : #if LIBSPDM_SHA384_SUPPORT
      76            2 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
      77            2 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
      78              : 
      79              :     /* any test fail cause the FIPS fail*/
      80            2 :     if (context->tested_algo != context->self_test_result) {
      81            0 :         return false;
      82              :     }
      83              : 
      84              :     /* check if run before.*/
      85            2 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384) != 0) {
      86            1 :         return true;
      87              :     }
      88              : 
      89            1 :     const uint8_t key[32] = {0};
      90            1 :     const uint8_t msg[32] = {0};
      91              : 
      92              :     uint8_t hmac_384_result[48];
      93            1 :     const uint8_t hmac_sha384_answer[] = {
      94              :         0xe6, 0x65, 0xec, 0x75, 0xdc, 0xa3, 0x23, 0xdf,
      95              :         0x31, 0x80, 0x40, 0x60, 0xe1, 0xb0, 0xd8, 0x28,
      96              :         0xb5, 0x0a, 0x6a, 0x8a, 0x53, 0x9c, 0xfe, 0xdd,
      97              :         0x9a, 0xa0, 0x07, 0x4b, 0x5b, 0x36, 0x44, 0x5d,
      98              :         0xef, 0xbc, 0x47, 0x45, 0x3d, 0xf8, 0xd0, 0xc1,
      99              :         0x4b, 0x7a, 0xd2, 0x06, 0x2e, 0x7b, 0xbd, 0xb1
     100              :     };
     101            1 :     libspdm_zero_mem(hmac_384_result, sizeof(hmac_384_result));
     102            1 :     result = libspdm_hmac_sha384_all(msg, sizeof(msg), key, sizeof(key), hmac_384_result);
     103            1 :     if (!result) {
     104            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha384 failed \n"));
     105            0 :         goto update;
     106              :     }
     107              : 
     108            1 :     if (!libspdm_consttime_is_mem_equal(hmac_384_result, hmac_sha384_answer,
     109              :                                         sizeof(hmac_sha384_answer))) {
     110            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha384 KAT failed \n"));
     111            0 :         result = false;
     112            0 :         goto update;
     113              :     }
     114              : 
     115            1 : update:
     116              :     /* mark it as tested*/
     117            1 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     118              : 
     119              :     /* record test result*/
     120            1 :     if (result) {
     121            1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     122              :     } else {
     123            0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     124              :     }
     125              : 
     126              : #endif/*LIBSPDM_SHA384_SUPPORT*/
     127              : 
     128            1 :     return result;
     129              : }
     130              : 
     131            2 : bool libspdm_fips_selftest_hmac_sha512(void *fips_selftest_context)
     132              : {
     133            2 :     bool result = true;
     134              : 
     135              : #if LIBSPDM_SHA512_SUPPORT
     136            2 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
     137            2 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
     138              : 
     139              :     /* any test fail cause the FIPS fail*/
     140            2 :     if (context->tested_algo != context->self_test_result) {
     141            0 :         return false;
     142              :     }
     143              : 
     144              :     /* check if run before.*/
     145            2 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512) != 0) {
     146            1 :         return true;
     147              :     }
     148              : 
     149            1 :     const uint8_t key[32] = {0};
     150            1 :     const uint8_t msg[32] = {0};
     151              : 
     152              :     uint8_t hmac_512_result[64];
     153            1 :     const uint8_t hmac_sha512_answer[] = {
     154              :         0xba, 0xe4, 0x6c, 0xeb, 0xeb, 0xbb, 0x90, 0x40,
     155              :         0x9a, 0xbc, 0x5a, 0xcf, 0x7a, 0xc2, 0x1f, 0xdb,
     156              :         0x33, 0x9c, 0x01, 0xce, 0x15, 0x19, 0x2c, 0x52,
     157              :         0xfb, 0x9e, 0x8a, 0xa1, 0x1a, 0x8d, 0xe9, 0xa4,
     158              :         0xea, 0x15, 0xa0, 0x45, 0xf2, 0xbe, 0x24, 0x5f,
     159              :         0xbb, 0x98, 0x91, 0x6a, 0x9a, 0xe8, 0x1b, 0x35,
     160              :         0x3e, 0x33, 0xb9, 0xc4, 0x2a, 0x55, 0x38, 0x0c,
     161              :         0x51, 0x58, 0x24, 0x1d, 0xae, 0xb3, 0xc6, 0xdd
     162              :     };
     163            1 :     libspdm_zero_mem(hmac_512_result, sizeof(hmac_512_result));
     164            1 :     result = libspdm_hmac_sha512_all(msg, sizeof(msg), key, sizeof(key), hmac_512_result);
     165            1 :     if (!result) {
     166            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha512 failed \n"));
     167            0 :         goto update;
     168              :     }
     169              : 
     170            1 :     if (!libspdm_consttime_is_mem_equal(hmac_512_result, hmac_sha512_answer,
     171              :                                         sizeof(hmac_sha512_answer))) {
     172            0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha512 KAT failed \n"));
     173            0 :         result = false;
     174            0 :         goto update;
     175              :     }
     176            1 : update:
     177              :     /* mark it as tested*/
     178            1 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     179              : 
     180              :     /* record test result*/
     181            1 :     if (result) {
     182            1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     183              :     } else {
     184            0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     185              :     }
     186              : 
     187              : #endif/*LIBSPDM_SHA512_SUPPORT*/
     188              : 
     189            1 :     return result;
     190              : }
     191              : 
     192              : #endif/*LIBSPDM_FIPS_MODE*/
        

Generated by: LCOV version 2.0-1