LCOV - code coverage report
Current view: top level - spdm_crypt_lib/fips - libspdm_selftest_hmac.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 76.0 % 75 57
Test Date: 2026-10-01 20:56:25 Functions: 100.0 % 3 3
Branches: 58.3 % 36 21

             Branch data     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 : void libspdm_fips_selftest_hmac_sha256(void *fips_selftest_context)
      14                 :             : {
      15                 :             : #if LIBSPDM_SHA256_SUPPORT
      16                 :           2 :     bool result = true;
      17                 :             : 
      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                 :           1 :         return;
      24                 :             :     }
      25                 :             : 
      26                 :             :     /* check if run before.*/
      27         [ +  + ]:           2 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256) != 0) {
      28                 :           1 :         return;
      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                 :             : 
      69                 :           2 : void libspdm_fips_selftest_hmac_sha384(void *fips_selftest_context)
      70                 :             : {
      71                 :             : #if LIBSPDM_SHA384_SUPPORT
      72                 :           2 :     bool result = true;
      73                 :             : 
      74                 :           2 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
      75         [ -  + ]:           2 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
      76                 :             : 
      77                 :             :     /* any test fail cause the FIPS fail*/
      78         [ -  + ]:           2 :     if (context->tested_algo != context->self_test_result) {
      79                 :           1 :         return;
      80                 :             :     }
      81                 :             : 
      82                 :             :     /* check if run before.*/
      83         [ +  + ]:           2 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384) != 0) {
      84                 :           1 :         return;
      85                 :             :     }
      86                 :             : 
      87                 :           1 :     const uint8_t key[32] = {0};
      88                 :           1 :     const uint8_t msg[32] = {0};
      89                 :             : 
      90                 :             :     uint8_t hmac_384_result[48];
      91                 :           1 :     const uint8_t hmac_sha384_answer[] = {
      92                 :             :         0xe6, 0x65, 0xec, 0x75, 0xdc, 0xa3, 0x23, 0xdf,
      93                 :             :         0x31, 0x80, 0x40, 0x60, 0xe1, 0xb0, 0xd8, 0x28,
      94                 :             :         0xb5, 0x0a, 0x6a, 0x8a, 0x53, 0x9c, 0xfe, 0xdd,
      95                 :             :         0x9a, 0xa0, 0x07, 0x4b, 0x5b, 0x36, 0x44, 0x5d,
      96                 :             :         0xef, 0xbc, 0x47, 0x45, 0x3d, 0xf8, 0xd0, 0xc1,
      97                 :             :         0x4b, 0x7a, 0xd2, 0x06, 0x2e, 0x7b, 0xbd, 0xb1
      98                 :             :     };
      99                 :           1 :     libspdm_zero_mem(hmac_384_result, sizeof(hmac_384_result));
     100                 :           1 :     result = libspdm_hmac_sha384_all(msg, sizeof(msg), key, sizeof(key), hmac_384_result);
     101         [ -  + ]:           1 :     if (!result) {
     102                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha384 failed \n"));
     103                 :           0 :         goto update;
     104                 :             :     }
     105                 :             : 
     106         [ +  - ]:           1 :     if (!libspdm_consttime_is_mem_equal(hmac_384_result, hmac_sha384_answer,
     107                 :             :                                         sizeof(hmac_sha384_answer))) {
     108                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha384 KAT failed \n"));
     109                 :           0 :         result = false;
     110                 :           0 :         goto update;
     111                 :             :     }
     112                 :             : 
     113                 :           1 : update:
     114                 :             :     /* mark it as tested*/
     115                 :           1 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     116                 :             : 
     117                 :             :     /* record test result*/
     118         [ +  - ]:           1 :     if (result) {
     119                 :           1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     120                 :             :     } else {
     121                 :           0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     122                 :             :     }
     123                 :             : 
     124                 :             : #endif/*LIBSPDM_SHA384_SUPPORT*/
     125                 :             : }
     126                 :             : 
     127                 :           2 : void libspdm_fips_selftest_hmac_sha512(void *fips_selftest_context)
     128                 :             : {
     129                 :             : #if LIBSPDM_SHA512_SUPPORT
     130                 :           2 :     bool result = true;
     131                 :             : 
     132                 :           2 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
     133         [ -  + ]:           2 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
     134                 :             : 
     135                 :             :     /* any test fail cause the FIPS fail*/
     136         [ -  + ]:           2 :     if (context->tested_algo != context->self_test_result) {
     137                 :           1 :         return;
     138                 :             :     }
     139                 :             : 
     140                 :             :     /* check if run before.*/
     141         [ +  + ]:           2 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512) != 0) {
     142                 :           1 :         return;
     143                 :             :     }
     144                 :             : 
     145                 :           1 :     const uint8_t key[32] = {0};
     146                 :           1 :     const uint8_t msg[32] = {0};
     147                 :             : 
     148                 :             :     uint8_t hmac_512_result[64];
     149                 :           1 :     const uint8_t hmac_sha512_answer[] = {
     150                 :             :         0xba, 0xe4, 0x6c, 0xeb, 0xeb, 0xbb, 0x90, 0x40,
     151                 :             :         0x9a, 0xbc, 0x5a, 0xcf, 0x7a, 0xc2, 0x1f, 0xdb,
     152                 :             :         0x33, 0x9c, 0x01, 0xce, 0x15, 0x19, 0x2c, 0x52,
     153                 :             :         0xfb, 0x9e, 0x8a, 0xa1, 0x1a, 0x8d, 0xe9, 0xa4,
     154                 :             :         0xea, 0x15, 0xa0, 0x45, 0xf2, 0xbe, 0x24, 0x5f,
     155                 :             :         0xbb, 0x98, 0x91, 0x6a, 0x9a, 0xe8, 0x1b, 0x35,
     156                 :             :         0x3e, 0x33, 0xb9, 0xc4, 0x2a, 0x55, 0x38, 0x0c,
     157                 :             :         0x51, 0x58, 0x24, 0x1d, 0xae, 0xb3, 0xc6, 0xdd
     158                 :             :     };
     159                 :           1 :     libspdm_zero_mem(hmac_512_result, sizeof(hmac_512_result));
     160                 :           1 :     result = libspdm_hmac_sha512_all(msg, sizeof(msg), key, sizeof(key), hmac_512_result);
     161         [ -  + ]:           1 :     if (!result) {
     162                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha512 failed \n"));
     163                 :           0 :         goto update;
     164                 :             :     }
     165                 :             : 
     166         [ +  - ]:           1 :     if (!libspdm_consttime_is_mem_equal(hmac_512_result, hmac_sha512_answer,
     167                 :             :                                         sizeof(hmac_sha512_answer))) {
     168                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha512 KAT failed \n"));
     169                 :           0 :         result = false;
     170                 :           0 :         goto update;
     171                 :             :     }
     172                 :           1 : update:
     173                 :             :     /* mark it as tested*/
     174                 :           1 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     175                 :             : 
     176                 :             :     /* record test result*/
     177         [ +  - ]:           1 :     if (result) {
     178                 :           1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     179                 :             :     } else {
     180                 :           0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     181                 :             :     }
     182                 :             : 
     183                 :             : #endif/*LIBSPDM_SHA512_SUPPORT*/
     184                 :             : }
     185                 :             : 
     186                 :             : #endif/*LIBSPDM_FIPS_MODE*/
        

Generated by: LCOV version 2.0-1