Code coverage report for DMTF/libspdm
Current view: top level - library/spdm_crypt_lib/fips - libspdm_selftest_hmac.c (source / functions) Coverage Total Hit
Test: 7d53225164c4aa249b863da608f94d42383fed1c Lines: 76.0 % 75 57
Test Date: 2026-11-15 08:32:33 Functions: 100.0 % 3 3
Branches: 70.0 % 30 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                 :          12 : void libspdm_fips_selftest_hmac_sha256(void *fips_selftest_context)
      14                 :             : {
      15                 :             : #if LIBSPDM_SHA256_SUPPORT
      16                 :          12 :     bool result = true;
      17                 :             : 
      18                 :          12 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
      19                 :          12 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
      20                 :             : 
      21                 :             :     /* any test fail cause the FIPS fail*/
      22         [ +  + ]:          12 :     if (context->tested_algo != context->self_test_result) {
      23                 :           8 :         return;
      24                 :             :     }
      25                 :             : 
      26                 :             :     /* check if run before.*/
      27         [ +  + ]:          10 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256) != 0) {
      28                 :           6 :         return;
      29                 :             :     }
      30                 :             : 
      31                 :             :     /* Test vector (key, msg and mac) from the NIST CAVP: HMAC.rsp, [L=32], Count = 30.
      32                 :             :      * https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/message-authentication
      33                 :             :      */
      34                 :           4 :     const uint8_t key[] = {
      35                 :             :         0x97, 0x79, 0xd9, 0x12, 0x06, 0x42, 0x79, 0x7f,
      36                 :             :         0x17, 0x47, 0x02, 0x5d, 0x5b, 0x22, 0xb7, 0xac,
      37                 :             :         0x60, 0x7c, 0xab, 0x08, 0xe1, 0x75, 0x8f, 0x2f,
      38                 :             :         0x3a, 0x46, 0xc8, 0xbe, 0x1e, 0x25, 0xc5, 0x3b,
      39                 :             :         0x8c, 0x6a, 0x8f, 0x58, 0xff, 0xef, 0xa1, 0x76
      40                 :             :     };
      41                 :           4 :     const uint8_t msg[] = {
      42                 :             :         0xb1, 0x68, 0x9c, 0x25, 0x91, 0xea, 0xf3, 0xc9,
      43                 :             :         0xe6, 0x60, 0x70, 0xf8, 0xa7, 0x79, 0x54, 0xff,
      44                 :             :         0xb8, 0x17, 0x49, 0xf1, 0xb0, 0x03, 0x46, 0xf9,
      45                 :             :         0xdf, 0xe0, 0xb2, 0xee, 0x90, 0x5d, 0xcc, 0x28,
      46                 :             :         0x8b, 0xaf, 0x4a, 0x92, 0xde, 0x3f, 0x40, 0x01,
      47                 :             :         0xdd, 0x9f, 0x44, 0xc4, 0x68, 0xc3, 0xd0, 0x7d,
      48                 :             :         0x6c, 0x6e, 0xe8, 0x2f, 0xac, 0xea, 0xfc, 0x97,
      49                 :             :         0xc2, 0xfc, 0x0f, 0xc0, 0x60, 0x17, 0x19, 0xd2,
      50                 :             :         0xdc, 0xd0, 0xaa, 0x2a, 0xec, 0x92, 0xd1, 0xb0,
      51                 :             :         0xae, 0x93, 0x3c, 0x65, 0xeb, 0x06, 0xa0, 0x3c,
      52                 :             :         0x9c, 0x93, 0x5c, 0x2b, 0xad, 0x04, 0x59, 0x81,
      53                 :             :         0x02, 0x41, 0x34, 0x7a, 0xb8, 0x7e, 0x9f, 0x11,
      54                 :             :         0xad, 0xb3, 0x04, 0x15, 0x42, 0x4c, 0x6c, 0x7f,
      55                 :             :         0x5f, 0x22, 0xa0, 0x03, 0xb8, 0xab, 0x8d, 0xe5,
      56                 :             :         0x4f, 0x6d, 0xed, 0x0e, 0x3a, 0xb9, 0x24, 0x5f,
      57                 :             :         0xa7, 0x95, 0x68, 0x45, 0x1d, 0xfa, 0x25, 0x8e
      58                 :             :     };
      59                 :             : 
      60                 :             :     uint8_t hmac_256_result[32];
      61                 :           4 :     const uint8_t hmac_sha256_answer[] = {
      62                 :             :         0x76, 0x9f, 0x00, 0xd3, 0xe6, 0xa6, 0xcc, 0x1f,
      63                 :             :         0xb4, 0x26, 0xa1, 0x4a, 0x4f, 0x76, 0xc6, 0x46,
      64                 :             :         0x2e, 0x61, 0x49, 0x72, 0x6e, 0x0d, 0xee, 0x0e,
      65                 :             :         0xc0, 0xcf, 0x97, 0xa1, 0x66, 0x05, 0xac, 0x8b
      66                 :             :     };
      67                 :           4 :     libspdm_zero_mem(hmac_256_result, sizeof(hmac_256_result));
      68                 :           4 :     result = libspdm_hmac_sha256_all(msg, sizeof(msg), key, sizeof(key), hmac_256_result);
      69         [ -  + ]:           4 :     if (!result) {
      70                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha256 failed \n"));
      71                 :           0 :         goto update;
      72                 :             :     }
      73                 :             : 
      74         [ +  - ]:           4 :     if (!libspdm_consttime_is_mem_equal(hmac_256_result, hmac_sha256_answer,
      75                 :             :                                         sizeof(hmac_sha256_answer))) {
      76                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha256 KAT failed \n"));
      77                 :           0 :         result = false;
      78                 :           0 :         goto update;
      79                 :             :     }
      80                 :             : 
      81                 :           4 : update:
      82                 :             :     /* mark it as tested*/
      83                 :           4 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
      84                 :             : 
      85                 :             :     /* record test result*/
      86         [ +  - ]:           4 :     if (result) {
      87                 :           4 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
      88                 :             :     } else {
      89                 :           0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
      90                 :             :     }
      91                 :             : 
      92                 :             : #endif/*LIBSPDM_SHA256_SUPPORT*/
      93                 :             : }
      94                 :             : 
      95                 :          12 : void libspdm_fips_selftest_hmac_sha384(void *fips_selftest_context)
      96                 :             : {
      97                 :             : #if LIBSPDM_SHA384_SUPPORT
      98                 :          12 :     bool result = true;
      99                 :             : 
     100                 :          12 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
     101                 :          12 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
     102                 :             : 
     103                 :             :     /* any test fail cause the FIPS fail*/
     104         [ +  + ]:          12 :     if (context->tested_algo != context->self_test_result) {
     105                 :           8 :         return;
     106                 :             :     }
     107                 :             : 
     108                 :             :     /* check if run before.*/
     109         [ +  + ]:          10 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384) != 0) {
     110                 :           6 :         return;
     111                 :             :     }
     112                 :             : 
     113                 :             :     /* Test vector (key, msg and mac) from the NIST CAVP: HMAC.rsp, [L=48], Count = 45.
     114                 :             :      * https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/message-authentication
     115                 :             :      */
     116                 :           4 :     const uint8_t key[] = {
     117                 :             :         0x5e, 0xab, 0x0d, 0xfa, 0x27, 0x31, 0x12, 0x60,
     118                 :             :         0xd7, 0xbd, 0xdc, 0xf7, 0x71, 0x12, 0xb2, 0x3d,
     119                 :             :         0x8b, 0x42, 0xeb, 0x7a, 0x5d, 0x72, 0xa5, 0xa3,
     120                 :             :         0x18, 0xe1, 0xba, 0x7e, 0x79, 0x27, 0xf0, 0x07,
     121                 :             :         0x9d, 0xbb, 0x70, 0x13, 0x17, 0xb8, 0x7a, 0x33,
     122                 :             :         0x40, 0xe1, 0x56, 0xdb, 0xce, 0xe2, 0x8e, 0xc3,
     123                 :             :         0xa8, 0xd9
     124                 :             :     };
     125                 :           4 :     const uint8_t msg[] = {
     126                 :             :         0xf4, 0x13, 0x80, 0x12, 0x3c, 0xcb, 0xec, 0x4c,
     127                 :             :         0x52, 0x7b, 0x42, 0x56, 0x52, 0x64, 0x11, 0x91,
     128                 :             :         0xe9, 0x0a, 0x17, 0xd4, 0x5e, 0x2f, 0x62, 0x06,
     129                 :             :         0xcf, 0x01, 0xb5, 0xed, 0xbe, 0x93, 0x2d, 0x41,
     130                 :             :         0xcc, 0x8a, 0x24, 0x05, 0xc3, 0x19, 0x56, 0x17,
     131                 :             :         0xda, 0x2f, 0x42, 0x05, 0x35, 0xee, 0xd4, 0x22,
     132                 :             :         0xac, 0x60, 0x40, 0xd9, 0xcd, 0x65, 0x31, 0x42,
     133                 :             :         0x24, 0xf0, 0x23, 0xf3, 0xba, 0x73, 0x0d, 0x19,
     134                 :             :         0xdb, 0x98, 0x44, 0xc7, 0x1c, 0x32, 0x9c, 0x8d,
     135                 :             :         0x9d, 0x73, 0xd0, 0x4d, 0x8c, 0x5f, 0x24, 0x4a,
     136                 :             :         0xea, 0x80, 0x48, 0x82, 0x92, 0xdc, 0x80, 0x3e,
     137                 :             :         0x77, 0x24, 0x02, 0xe7, 0x2d, 0x2e, 0x9f, 0x1b,
     138                 :             :         0xab, 0xa5, 0xa6, 0x00, 0x4f, 0x00, 0x06, 0xd8,
     139                 :             :         0x22, 0xb0, 0xb2, 0xd6, 0x5e, 0x9e, 0x4a, 0x30,
     140                 :             :         0x2d, 0xd4, 0xf7, 0x76, 0xb4, 0x7a, 0x97, 0x22,
     141                 :             :         0x50, 0x05, 0x1a, 0x70, 0x1f, 0xab, 0x2b, 0x70
     142                 :             :     };
     143                 :             : 
     144                 :             :     uint8_t hmac_384_result[48];
     145                 :           4 :     const uint8_t hmac_sha384_answer[] = {
     146                 :             :         0x7c, 0xf5, 0xa0, 0x61, 0x56, 0xad, 0x3d, 0xe5,
     147                 :             :         0x40, 0x5a, 0x5d, 0x26, 0x1d, 0xe9, 0x02, 0x75,
     148                 :             :         0xf9, 0xbb, 0x36, 0xde, 0x45, 0x66, 0x7f, 0x84,
     149                 :             :         0xd0, 0x8f, 0xbc, 0xb3, 0x08, 0xca, 0x8f, 0x53,
     150                 :             :         0xa4, 0x19, 0xb0, 0x7d, 0xea, 0xb3, 0xb5, 0xf8,
     151                 :             :         0xea, 0x23, 0x1c, 0x5b, 0x03, 0x6f, 0x88, 0x75
     152                 :             :     };
     153                 :           4 :     libspdm_zero_mem(hmac_384_result, sizeof(hmac_384_result));
     154                 :           4 :     result = libspdm_hmac_sha384_all(msg, sizeof(msg), key, sizeof(key), hmac_384_result);
     155         [ -  + ]:           4 :     if (!result) {
     156                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha384 failed \n"));
     157                 :           0 :         goto update;
     158                 :             :     }
     159                 :             : 
     160         [ +  - ]:           4 :     if (!libspdm_consttime_is_mem_equal(hmac_384_result, hmac_sha384_answer,
     161                 :             :                                         sizeof(hmac_sha384_answer))) {
     162                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha384 KAT failed \n"));
     163                 :           0 :         result = false;
     164                 :           0 :         goto update;
     165                 :             :     }
     166                 :             : 
     167                 :           4 : update:
     168                 :             :     /* mark it as tested*/
     169                 :           4 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     170                 :             : 
     171                 :             :     /* record test result*/
     172         [ +  - ]:           4 :     if (result) {
     173                 :           4 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     174                 :             :     } else {
     175                 :           0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
     176                 :             :     }
     177                 :             : 
     178                 :             : #endif/*LIBSPDM_SHA384_SUPPORT*/
     179                 :             : }
     180                 :             : 
     181                 :          12 : void libspdm_fips_selftest_hmac_sha512(void *fips_selftest_context)
     182                 :             : {
     183                 :             : #if LIBSPDM_SHA512_SUPPORT
     184                 :          12 :     bool result = true;
     185                 :             : 
     186                 :          12 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
     187                 :          12 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
     188                 :             : 
     189                 :             :     /* any test fail cause the FIPS fail*/
     190         [ +  + ]:          12 :     if (context->tested_algo != context->self_test_result) {
     191                 :           8 :         return;
     192                 :             :     }
     193                 :             : 
     194                 :             :     /* check if run before.*/
     195         [ +  + ]:          10 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512) != 0) {
     196                 :           6 :         return;
     197                 :             :     }
     198                 :             : 
     199                 :             :     /* Test vector (key, msg and mac) from the NIST CAVP: HMAC.rsp, [L=64], Count = 60.
     200                 :             :      * https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/message-authentication
     201                 :             :      */
     202                 :           4 :     const uint8_t key[] = {
     203                 :             :         0x57, 0xc2, 0xeb, 0x67, 0x7b, 0x50, 0x93, 0xb9,
     204                 :             :         0xe8, 0x29, 0xea, 0x4b, 0xab, 0xb5, 0x0b, 0xde,
     205                 :             :         0x55, 0xd0, 0xad, 0x59, 0xfe, 0xc3, 0x4a, 0x61,
     206                 :             :         0x89, 0x73, 0x80, 0x2b, 0x2a, 0xd9, 0xb7, 0x8e,
     207                 :             :         0x26, 0xb2, 0x04, 0x5d, 0xda, 0x78, 0x4d, 0xf3,
     208                 :             :         0xff, 0x90, 0xae, 0x0f, 0x2c, 0xc5, 0x1c, 0xe3,
     209                 :             :         0x9c, 0xf5, 0x48, 0x67, 0x32, 0x0a, 0xc6, 0xf3,
     210                 :             :         0xba, 0x2c, 0x6f, 0x0d, 0x72, 0x36, 0x04, 0x80,
     211                 :             :         0xc9, 0x66, 0x14, 0xae, 0x66, 0x58, 0x1f, 0x26,
     212                 :             :         0x6c, 0x35, 0xfb, 0x79, 0xfd, 0x28, 0x77, 0x4a,
     213                 :             :         0xfd, 0x11, 0x3f, 0xa5, 0x18, 0x7e, 0xff, 0x92,
     214                 :             :         0x06, 0xd7, 0xcb, 0xe9, 0x0d, 0xd8, 0xbf, 0x67,
     215                 :             :         0xc8, 0x44, 0xe2, 0x02
     216                 :             :     };
     217                 :           4 :     const uint8_t msg[] = {
     218                 :             :         0x24, 0x23, 0xdf, 0xf4, 0x8b, 0x31, 0x2b, 0xe8,
     219                 :             :         0x64, 0xcb, 0x34, 0x90, 0x64, 0x1f, 0x79, 0x3d,
     220                 :             :         0x2b, 0x9f, 0xb6, 0x8a, 0x77, 0x63, 0xb8, 0xe2,
     221                 :             :         0x98, 0xc8, 0x6f, 0x42, 0x24, 0x5e, 0x45, 0x40,
     222                 :             :         0xeb, 0x01, 0xae, 0x4d, 0x2d, 0x45, 0x00, 0x37,
     223                 :             :         0x0b, 0x18, 0x86, 0xf2, 0x3c, 0xa2, 0xcf, 0x97,
     224                 :             :         0x01, 0x70, 0x4c, 0xad, 0x5b, 0xd2, 0x1b, 0xa8,
     225                 :             :         0x7b, 0x81, 0x1d, 0xaf, 0x7a, 0x85, 0x4e, 0xa2,
     226                 :             :         0x4a, 0x56, 0x56, 0x5c, 0xed, 0x42, 0x5b, 0x35,
     227                 :             :         0xe4, 0x0e, 0x1a, 0xcb, 0xeb, 0xe0, 0x36, 0x03,
     228                 :             :         0xe3, 0x5d, 0xcf, 0x4a, 0x10, 0x0e, 0x57, 0x21,
     229                 :             :         0x84, 0x08, 0xa1, 0xd8, 0xdb, 0xcc, 0x3b, 0x99,
     230                 :             :         0x29, 0x6c, 0xfe, 0xa9, 0x31, 0xef, 0xe3, 0xeb,
     231                 :             :         0xd8, 0xf7, 0x19, 0xa6, 0xd9, 0xa1, 0x54, 0x87,
     232                 :             :         0xb9, 0xad, 0x67, 0xea, 0xfe, 0xdf, 0x15, 0x55,
     233                 :             :         0x9c, 0xa4, 0x24, 0x45, 0xb0, 0xf9, 0xb4, 0x2e
     234                 :             :     };
     235                 :             : 
     236                 :             :     uint8_t hmac_512_result[64];
     237                 :           4 :     const uint8_t hmac_sha512_answer[] = {
     238                 :             :         0x33, 0xc5, 0x11, 0xe9, 0xbc, 0x23, 0x07, 0xc6,
     239                 :             :         0x27, 0x58, 0xdf, 0x61, 0x12, 0x5a, 0x98, 0x0e,
     240                 :             :         0xe6, 0x4c, 0xef, 0xeb, 0xd9, 0x09, 0x31, 0xcb,
     241                 :             :         0x91, 0xc1, 0x37, 0x42, 0xd4, 0x71, 0x4c, 0x06,
     242                 :             :         0xde, 0x40, 0x03, 0xfa, 0xf3, 0xc4, 0x1c, 0x06,
     243                 :             :         0xae, 0xfc, 0x63, 0x8a, 0xd4, 0x7b, 0x21, 0x90,
     244                 :             :         0x6e, 0x6b, 0x10, 0x48, 0x16, 0xb7, 0x2d, 0xe6,
     245                 :             :         0x26, 0x9e, 0x04, 0x5a, 0x1f, 0x44, 0x29, 0xd4
     246                 :             :     };
     247                 :           4 :     libspdm_zero_mem(hmac_512_result, sizeof(hmac_512_result));
     248                 :           4 :     result = libspdm_hmac_sha512_all(msg, sizeof(msg), key, sizeof(key), hmac_512_result);
     249         [ -  + ]:           4 :     if (!result) {
     250                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha512 failed \n"));
     251                 :           0 :         goto update;
     252                 :             :     }
     253                 :             : 
     254         [ +  - ]:           4 :     if (!libspdm_consttime_is_mem_equal(hmac_512_result, hmac_sha512_answer,
     255                 :             :                                         sizeof(hmac_sha512_answer))) {
     256                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha512 KAT failed \n"));
     257                 :           0 :         result = false;
     258                 :           0 :         goto update;
     259                 :             :     }
     260                 :           4 : update:
     261                 :             :     /* mark it as tested*/
     262                 :           4 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     263                 :             : 
     264                 :             :     /* record test result*/
     265         [ +  - ]:           4 :     if (result) {
     266                 :           4 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     267                 :             :     } else {
     268                 :           0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
     269                 :             :     }
     270                 :             : 
     271                 :             : #endif/*LIBSPDM_SHA512_SUPPORT*/
     272                 :             : }
     273                 :             : 
     274                 :             : #endif/*LIBSPDM_FIPS_MODE*/
        

Generated by: LCOV version 2.0-1