LCOV - code coverage report
Current view: top level - library/spdm_crypt_lib/fips - libspdm_selftest_sha3.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 65.4 % 78 51
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 3 3

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

Generated by: LCOV version 2.0-1