LCOV - code coverage report
Current view: top level - spdm_crypt_lib/fips - libspdm_selftest_sha3.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 64.0 % 75 48
Test Date: 2026-10-01 20:56:25 Functions: 100.0 % 3 3
Branches: 50.0 % 36 18

             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                 :           1 : void libspdm_fips_selftest_sha3_256(void *fips_selftest_context)
      14                 :             : {
      15                 :             : #if LIBSPDM_SHA3_256_SUPPORT
      16                 :           1 :     bool result = true;
      17                 :             : 
      18                 :           1 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
      19         [ -  + ]:           1 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
      20                 :             : 
      21                 :             :     /* any test fail cause the FIPS fail*/
      22         [ -  + ]:           1 :     if (context->tested_algo != context->self_test_result) {
      23                 :           0 :         return;
      24                 :             :     }
      25                 :             : 
      26                 :             :     /* check if run before.*/
      27         [ -  + ]:           1 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_SHA3_256) != 0) {
      28                 :           0 :         return;
      29                 :             :     }
      30                 :             : 
      31                 :           1 :     const uint8_t msg[] = {0x7f, 0x94};
      32                 :             :     /*Test Vectors: https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#sha3vsha3vss */
      33                 :             :     uint8_t sha3_256_result[32];
      34                 :           1 :     const uint8_t sha3_256_answer[] = {
      35                 :             :         0xde, 0x01, 0x6a, 0xcf, 0xc1, 0xa2, 0xe2, 0x2e,
      36                 :             :         0x39, 0x52, 0x6c, 0x60, 0x9d, 0x9c, 0x69, 0xd8,
      37                 :             :         0x56, 0xa5, 0x43, 0xfe, 0xbb, 0x3c, 0xb4, 0x26,
      38                 :             :         0xee, 0x1f, 0x13, 0x18, 0xd7, 0x80, 0xea, 0x88
      39                 :             :     };
      40                 :           1 :     libspdm_zero_mem(sha3_256_result, sizeof(sha3_256_result));
      41                 :           1 :     result = libspdm_sha3_256_hash_all(msg, sizeof(msg), sha3_256_result);
      42         [ -  + ]:           1 :     if (!result) {
      43                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen sha3_256 failed \n"));
      44                 :           0 :         result = false;
      45                 :           0 :         goto update;
      46                 :             :     }
      47                 :             : 
      48         [ +  - ]:           1 :     if (!libspdm_consttime_is_mem_equal(sha3_256_result, sha3_256_answer,
      49                 :             :                                         sizeof(sha3_256_answer))) {
      50                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "sha3_256 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_SHA3_256;
      58                 :             : 
      59                 :             :     /* record test result*/
      60         [ +  - ]:           1 :     if (result) {
      61                 :           1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_SHA3_256;
      62                 :             :     } else {
      63                 :           0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_SHA3_256;
      64                 :             :     }
      65                 :             : 
      66                 :             : #endif/* LIBSPDM_SHA3_256_SUPPORT */
      67                 :             : }
      68                 :             : 
      69                 :           1 : void libspdm_fips_selftest_sha3_384(void *fips_selftest_context)
      70                 :             : {
      71                 :             : #if LIBSPDM_SHA3_384_SUPPORT
      72                 :           1 :     bool result = true;
      73                 :             : 
      74                 :           1 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
      75         [ -  + ]:           1 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
      76                 :             : 
      77                 :             :     /* any test fail cause the FIPS fail*/
      78         [ -  + ]:           1 :     if (context->tested_algo != context->self_test_result) {
      79                 :           0 :         return;
      80                 :             :     }
      81                 :             : 
      82                 :             :     /* check if run before.*/
      83         [ -  + ]:           1 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_SHA3_384) != 0) {
      84                 :           0 :         return;
      85                 :             :     }
      86                 :             : 
      87                 :             :     uint8_t sha3_384_result[48];
      88                 :             :     /*Test Vectors: https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#sha3vsha3vss */
      89                 :           1 :     const uint8_t msg[] = {0x89, 0xcc};
      90                 :           1 :     const uint8_t sha3_384_answer[] = {
      91                 :             :         0xcf, 0x9b, 0xe5, 0x91, 0x0e, 0x2c, 0x4f, 0x89,
      92                 :             :         0x5b, 0x9e, 0x92, 0x08, 0x02, 0x26, 0x52, 0xbb,
      93                 :             :         0x4d, 0x6a, 0x7e, 0x85, 0x84, 0x5b, 0x2a, 0x6c,
      94                 :             :         0x22, 0x1c, 0x22, 0x84, 0x1e, 0xc0, 0x74, 0x64,
      95                 :             :         0xae, 0xe9, 0xfb, 0x5f, 0x89, 0x38, 0xb2, 0xda,
      96                 :             :         0xa8, 0x7b, 0xe3, 0x37, 0xf0, 0x38, 0xcb, 0xcf
      97                 :             :     };
      98                 :           1 :     libspdm_zero_mem(sha3_384_result, sizeof(sha3_384_result));
      99                 :           1 :     result = libspdm_sha3_384_hash_all(msg, sizeof(msg), sha3_384_result);
     100         [ -  + ]:           1 :     if (!result) {
     101                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen sha3_384 failed \n"));
     102                 :           0 :         result = false;
     103                 :           0 :         goto update;
     104                 :             :     }
     105                 :             : 
     106         [ +  - ]:           1 :     if (!libspdm_consttime_is_mem_equal(sha3_384_result, sha3_384_answer,
     107                 :             :                                         sizeof(sha3_384_answer))) {
     108                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "sha3_384 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_SHA3_384;
     116                 :             : 
     117                 :             :     /* record test result*/
     118         [ +  - ]:           1 :     if (result) {
     119                 :           1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_SHA3_384;
     120                 :             :     } else {
     121                 :           0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_SHA3_384;
     122                 :             :     }
     123                 :             : 
     124                 :             : #endif/* LIBSPDM_SHA3_384_SUPPORT */
     125                 :             : }
     126                 :             : 
     127                 :           1 : void libspdm_fips_selftest_sha3_512(void *fips_selftest_context)
     128                 :             : {
     129                 :             : #if LIBSPDM_SHA3_512_SUPPORT
     130                 :           1 :     bool result = true;
     131                 :             : 
     132                 :           1 :     libspdm_fips_selftest_context_t *context = fips_selftest_context;
     133         [ -  + ]:           1 :     LIBSPDM_ASSERT(fips_selftest_context != NULL);
     134                 :             : 
     135                 :             :     /* any test fail cause the FIPS fail*/
     136         [ -  + ]:           1 :     if (context->tested_algo != context->self_test_result) {
     137                 :           0 :         return;
     138                 :             :     }
     139                 :             : 
     140                 :             :     /* check if run before.*/
     141         [ -  + ]:           1 :     if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_SHA3_512) != 0) {
     142                 :           0 :         return;
     143                 :             :     }
     144                 :             : 
     145                 :             :     uint8_t sha3_512_result[64];
     146                 :             :     /*Test Vectors: https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#sha3vsha3vss */
     147                 :           1 :     const uint8_t msg[] = {0xb1, 0x39};
     148                 :           1 :     const uint8_t sha3_512_answer[] = {
     149                 :             :         0xd3, 0xe5, 0xc0, 0x26, 0x47, 0x05, 0xe8, 0x1d,
     150                 :             :         0x0c, 0x90, 0xf9, 0x9d, 0xae, 0xff, 0x00, 0x89,
     151                 :             :         0xfa, 0x3e, 0x91, 0x77, 0xd3, 0xd5, 0xbc, 0x74,
     152                 :             :         0x9c, 0xde, 0x10, 0xf0, 0x35, 0xf4, 0x95, 0x65,
     153                 :             :         0x55, 0x44, 0xf8, 0x57, 0x79, 0x91, 0x71, 0x2e,
     154                 :             :         0xb5, 0x18, 0x01, 0x5b, 0xe2, 0x9d, 0x19, 0x5b,
     155                 :             :         0x7e, 0xbf, 0x61, 0xe8, 0xd2, 0x93, 0x90, 0xea,
     156                 :             :         0xf1, 0x47, 0x88, 0x08, 0x2b, 0x11, 0x97, 0x6d
     157                 :             :     };
     158                 :           1 :     libspdm_zero_mem(sha3_512_result, sizeof(sha3_512_result));
     159                 :           1 :     result = libspdm_sha3_512_hash_all(msg, sizeof(msg), sha3_512_result);
     160         [ -  + ]:           1 :     if (!result) {
     161                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen sha3_512 failed \n"));
     162                 :           0 :         result = false;
     163                 :           0 :         goto update;
     164                 :             :     }
     165                 :             : 
     166         [ +  - ]:           1 :     if (!libspdm_consttime_is_mem_equal(sha3_512_result, sha3_512_answer,
     167                 :             :                                         sizeof(sha3_512_answer))) {
     168                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "sha3_512 KAT failed \n"));
     169                 :           0 :         result = false;
     170                 :           0 :         goto update;
     171                 :             :     }
     172                 :             : 
     173                 :           1 : update:
     174                 :             :     /* mark it as tested*/
     175                 :           1 :     context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_SHA3_512;
     176                 :             : 
     177                 :             :     /* record test result*/
     178         [ +  - ]:           1 :     if (result) {
     179                 :           1 :         context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_SHA3_512;
     180                 :             :     } else {
     181                 :           0 :         context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_SHA3_512;
     182                 :             :     }
     183                 :             : 
     184                 :             : #endif/* LIBSPDM_SHA3_512_SUPPORT */
     185                 :             : }
     186                 :             : 
     187                 :             : #endif/*LIBSPDM_FIPS_MODE*/
        

Generated by: LCOV version 2.0-1