LCOV - code coverage report
Current view: top level - libs/sha512/tests/src - test_libsha512_0001.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 27 27
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 5 5
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : #include "test_libsha512_utils.h"
       2                 :             : 
       3                 :             : /* Published SHA-512 digests for the reference messages used below. The empty,
       4                 :             :    "abc" and 896-bit multi-block vectors come from FIPS-180-2 Appendix C; the
       5                 :             :    "Hello World" vector is a widely cited non-FIPS check that lets a developer
       6                 :             :    eyeball the library output against any common SHA-512 calculator */
       7                 :             : 
       8                 :             : /* FIPS-180-2 Appendix C.1 (empty input). Anchors the simplest SHA-512
       9                 :             :    contract: zero input bytes must still produce the standard 64-byte digest
      10                 :             :    with correct length-encoded padding */
      11                 :             : static const unsigned char empty_digest[SHA512_DIGEST_LENGTH] = {
      12                 :             :         0xcf,0x83,0xe1,0x35,0x7e,0xef,0xb8,0xbd,
      13                 :             :         0xf1,0x54,0x28,0x50,0xd6,0x6d,0x80,0x07,
      14                 :             :         0xd6,0x20,0xe4,0x05,0x0b,0x57,0x15,0xdc,
      15                 :             :         0x83,0xf4,0xa9,0x21,0xd3,0x6c,0xe9,0xce,
      16                 :             :         0x47,0xd0,0xd1,0x3c,0x5d,0x85,0xf2,0xb0,
      17                 :             :         0xff,0x83,0x18,0xd2,0x87,0x7e,0xec,0x2f,
      18                 :             :         0x63,0xb9,0x31,0xbd,0x47,0x41,0x7a,0x81,
      19                 :             :         0xa5,0x38,0x32,0x7a,0xf9,0x27,0xda,0x3e
      20                 :             : };
      21                 :             : 
      22                 :             : /* FIPS-180-2 Appendix C.1 (24-bit "abc" message). The canonical short
      23                 :             :    single-block vector quoted by virtually every SHA-512 reference, so a
      24                 :             :    failure here points directly at the core compression function */
      25                 :             : static const unsigned char abc_digest[SHA512_DIGEST_LENGTH] = {
      26                 :             :         0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
      27                 :             :         0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
      28                 :             :         0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
      29                 :             :         0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
      30                 :             :         0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
      31                 :             :         0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
      32                 :             :         0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
      33                 :             :         0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
      34                 :             : };
      35                 :             : 
      36                 :             : /* Informal "Hello World" check. Not part of FIPS-180-2, but widely cited;
      37                 :             :    convenient because any general-purpose SHA-512 tool will compute the same
      38                 :             :    digest without project-specific context */
      39                 :             : static const unsigned char hello_world_digest[SHA512_DIGEST_LENGTH] = {
      40                 :             :         0x2c,0x74,0xfd,0x17,0xed,0xaf,0xd8,0x0e,
      41                 :             :         0x84,0x47,0xb0,0xd4,0x67,0x41,0xee,0x24,
      42                 :             :         0x3b,0x7e,0xb7,0x4d,0xd2,0x14,0x9a,0x0a,
      43                 :             :         0xb1,0xb9,0x24,0x6f,0xb3,0x03,0x82,0xf2,
      44                 :             :         0x7e,0x85,0x3d,0x85,0x85,0x71,0x9e,0x0e,
      45                 :             :         0x67,0xcb,0xda,0x0d,0xaa,0x8f,0x51,0x67,
      46                 :             :         0x10,0x64,0x61,0x5d,0x64,0x5a,0xe2,0x7a,
      47                 :             :         0xcb,0x15,0xbf,0xb1,0x44,0x7f,0x45,0x9b
      48                 :             : };
      49                 :             : 
      50                 :             : /* FIPS-180-2 Appendix C.2 (896-bit, 112-byte message). After SHA-512 padding
      51                 :             :    this input straddles the 128-byte block boundary, so the same one-shot API
      52                 :             :    call also exercises the multi-block compression path that the short
      53                 :             :    single-block vectors above cannot reach */
      54                 :             : static const unsigned char fips_multi_block_digest[SHA512_DIGEST_LENGTH] = {
      55                 :             :         0x8e,0x95,0x9b,0x75,0xda,0xe3,0x13,0xda,
      56                 :             :         0x8c,0xf4,0xf7,0x28,0x14,0xfc,0x14,0x3f,
      57                 :             :         0x8f,0x77,0x79,0xc6,0xeb,0x9f,0x7f,0xa1,
      58                 :             :         0x72,0x99,0xae,0xad,0xb6,0x88,0x90,0x18,
      59                 :             :         0x50,0x1d,0x28,0x9e,0x49,0x00,0xf7,0xe4,
      60                 :             :         0x33,0x1b,0x99,0xde,0xc4,0xb5,0x43,0x3a,
      61                 :             :         0xc7,0xd3,0x29,0xee,0xb6,0xdd,0x26,0x54,
      62                 :             :         0x5e,0x96,0xe5,0x5b,0x87,0x4b,0xe9,0x09
      63                 :             : };
      64                 :             : 
      65                 :             : /* Use recognizable messages so the expected digests can be checked against
      66                 :             :    common SHA-512 references without extra project context */
      67                 :             : static const char empty_message[] = "";
      68                 :             : static const char abc_message[] = "abc";
      69                 :             : static const char hello_world_message[] = "Hello World";
      70                 :             : 
      71                 :             : /* FIPS-180-2 Appendix C.2 sequence: 112 ASCII bytes that, together with
      72                 :             :    SHA-512 padding, span two 128-byte compression blocks */
      73                 :             : static const char fips_multi_block_message[] =
      74                 :             :         "abcdefghbcdefghicdefghijdefghijkefghijkl"
      75                 :             :         "fghijklmghijklmnhijklmnoijklmnopjklmnopq"
      76                 :             :         "klmnopqrlmnopqrsmnopqrstnopqrstu";
      77                 :             : 
      78                 :             : /**
      79                 :             :  * @brief Check the published SHA-512 digest for empty input
      80                 :             :  * @details Empty input is a useful user-facing baseline: callers can hash zero
      81                 :             :  * bytes and still expect a stable, standard 64-byte SHA-512 digest
      82                 :             :  *
      83                 :             :  * @return SUCCESS when the empty message matches the FIPS reference digest
      84                 :             :  */
      85                 :           1 : static Return test_libsha512_0001_1(void)
      86                 :             : {
      87                 :           1 :         INITTEST;
      88                 :             : 
      89                 :             :         /* Keep the message and expected digest together so this subtest reads as one
      90                 :             :            small reference example */
      91                 :           1 :         const sha512_digest_vector vector = {
      92                 :             :                 .message_text = empty_message,
      93                 :             :                 .message_size = sizeof(empty_message) - 1U,
      94                 :             :                 .expected_digest = empty_digest
      95                 :             :         };
      96                 :             : 
      97                 :             :         /* Run the vector through the shared helper used by every reference case */
      98                 :           1 :         run(check_sha512_vector(&vector));
      99                 :             : 
     100                 :           1 :         RETURN_STATUS;
     101                 :             : }
     102                 :             : 
     103                 :             : /**
     104                 :             :  * @brief Check the canonical SHA-512 digest for "abc"
     105                 :             :  * @details The "abc" vector is the short SHA-512 example most developers know.
     106                 :             :  * It proves that ordinary single-block text input reaches the expected digest
     107                 :             :  *
     108                 :             :  * @return SUCCESS when "abc" matches the FIPS reference digest
     109                 :             :  */
     110                 :           1 : static Return test_libsha512_0001_2(void)
     111                 :             : {
     112                 :           1 :         INITTEST;
     113                 :             : 
     114                 :             :         /* Keep this compact reference vector local to the subtest that reports it */
     115                 :           1 :         const sha512_digest_vector vector = {
     116                 :             :                 .message_text = abc_message,
     117                 :             :                 .message_size = sizeof(abc_message) - 1U,
     118                 :             :                 .expected_digest = abc_digest
     119                 :             :         };
     120                 :             : 
     121                 :             :         /* The common helper handles libmem setup, hashing, and byte comparison */
     122                 :           1 :         run(check_sha512_vector(&vector));
     123                 :             : 
     124                 :           1 :         RETURN_STATUS;
     125                 :             : }
     126                 :             : 
     127                 :             : /**
     128                 :             :  * @brief Check the widely cited SHA-512 digest for "Hello World"
     129                 :             :  * @details This vector is not a FIPS appendix case, but it is easy to reproduce
     130                 :             :  * with common SHA-512 tools and helps people sanity-check the library quickly
     131                 :             :  *
     132                 :             :  * @return SUCCESS when "Hello World" matches the expected public digest
     133                 :             :  */
     134                 :           1 : static Return test_libsha512_0001_3(void)
     135                 :             : {
     136                 :           1 :         INITTEST;
     137                 :             : 
     138                 :             :         /* Use an informal but recognizable message that is convenient to verify
     139                 :             :            outside the project */
     140                 :           1 :         const sha512_digest_vector vector = {
     141                 :             :                 .message_text = hello_world_message,
     142                 :             :                 .message_size = sizeof(hello_world_message) - 1U,
     143                 :             :                 .expected_digest = hello_world_digest
     144                 :             :         };
     145                 :             : 
     146                 :             :         /* Hash and compare the vector through the same route as the FIPS examples */
     147                 :           1 :         run(check_sha512_vector(&vector));
     148                 :             : 
     149                 :           1 :         RETURN_STATUS;
     150                 :             : }
     151                 :             : 
     152                 :             : /**
     153                 :             :  * @brief Check the FIPS multi-block SHA-512 example
     154                 :             :  * @details The 896-bit FIPS vector forces SHA-512 finalization to process more
     155                 :             :  * than one block, so it protects behavior that short examples cannot reach
     156                 :             :  *
     157                 :             :  * @return SUCCESS when the 896-bit message matches the FIPS reference digest
     158                 :             :  */
     159                 :           1 : static Return test_libsha512_0001_4(void)
     160                 :             : {
     161                 :           1 :         INITTEST;
     162                 :             : 
     163                 :             :         /* Keep the long FIPS vector in one descriptor so a failure names this exact
     164                 :             :            multi-block reference case in the test output */
     165                 :           1 :         const sha512_digest_vector vector = {
     166                 :             :                 .message_text = fips_multi_block_message,
     167                 :             :                 .message_size = sizeof(fips_multi_block_message) - 1U,
     168                 :             :                 .expected_digest = fips_multi_block_digest
     169                 :             :         };
     170                 :             : 
     171                 :             :         /* The shared vector helper keeps this check consistent with the short cases */
     172                 :           1 :         run(check_sha512_vector(&vector));
     173                 :             : 
     174                 :           1 :         RETURN_STATUS;
     175                 :             : }
     176                 :             : 
     177                 :             : /**
     178                 :             :  * @brief Check SHA-512 answers that users can verify from public references
     179                 :             :  * @details Three compact messages (the empty string, "abc" and "Hello World")
     180                 :             :  * confirm that the library produces the standard SHA-512 output for ordinary
     181                 :             :  * single-block callers. A fourth vector is the 896-bit two-block example from
     182                 :             :  * FIPS-180-2 Appendix C.2, which also exercises the multi-block compression
     183                 :             :  * path through the very same one-shot API
     184                 :             :  *
     185                 :             :  * @return SUCCESS when all reference messages produce their expected digests
     186                 :             :  */
     187                 :           1 : Return test_libsha512_0001(void)
     188                 :             : {
     189                 :           1 :         INITTEST;
     190                 :             : 
     191                 :             :         /* Each vector is its own SUTE test so the output names the exact reference
     192                 :             :            message that failed */
     193                 :           1 :         TEST(test_libsha512_0001_1,"Empty input hashes to the FIPS SHA-512 digest");
     194                 :           1 :         TEST(test_libsha512_0001_2,"The canonical abc message hashes to the FIPS SHA-512 digest");
     195                 :           1 :         TEST(test_libsha512_0001_3,"Hello World hashes to the widely cited SHA-512 digest");
     196                 :           1 :         TEST(test_libsha512_0001_4,"The 896-bit FIPS message exercises multi-block SHA-512 hashing");
     197                 :             : 
     198                 :           1 :         RETURN_STATUS;
     199                 :             : }
        

Generated by: LCOV version 2.0-1