Add SHA-256 checksum calculation support

This commit is contained in:
2025-10-03 02:03:39 +01:00
parent 1e569c68a1
commit 79ac2e380c
10 changed files with 329 additions and 124 deletions

View File

@@ -7,36 +7,15 @@ project(tests)
add_subdirectory(lib)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR} ../include ../3rdparty/uthash/include ../3rdparty/uthash/src)
# Find OpenSSL for tests that use it directly
find_package(OpenSSL QUIET)
find_package(LibreSSL QUIET)
# Copy test data files into build tree
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/random DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/flac.flac DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/audio.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/lzma.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/data.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/random
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/flac.flac
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/audio.bin
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/lzma.bin
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/data.bin
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
# 'Google_Tests_run' is the target name
# 'test1.cpp tests2.cpp' are source files with tests
# Test executable (all unit tests)
add_executable(tests_run crc64.cpp spamsum.cpp crc32.c crc32.h flac.cpp lzma.cpp sha256.cpp md5.cpp sha1.cpp)
# Link libraries including OpenSSL for SHA256 test
# Link libraries
target_link_libraries(tests_run PRIVATE gtest gtest_main aaruformat)
# Link OpenSSL/LibreSSL for tests that use crypto functions directly
if(LIBRESSL_FOUND)
target_link_libraries(tests_run PRIVATE ${LIBRESSL_CRYPTO_LIBRARY})
elseif(OpenSSL_FOUND)
include_directories(${OPENSSL_INCLUDE_DIR})
target_link_libraries(tests_run PRIVATE ${OPENSSL_CRYPTO_LIBRARY})
endif()

View File

@@ -2,88 +2,117 @@
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2025 Natalia Portillo.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
* Internal SHA-256 tests (no OpenSSL dependency).
* Validates implementation against standard FIPS 180-4 test vectors and the bundled random file.
*/
#ifdef AARU_HAS_SHA256
#include <openssl/sha.h>
#include <unistd.h>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "decls.h"
#include "gtest/gtest.h"
#include "sha256.h"
static uint8_t *buffer;
// Test vectors
static const unsigned char sha256_empty[SHA256_DIGEST_LENGTH] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
unsigned char expected_sha256[SHA256_DIGEST_LENGTH] = {0x4d, 0x1a, 0x6b, 0x8a, 0x54, 0x67, 0x00, 0xc4, 0x8e, 0xda, 0x70,
0xd3, 0x39, 0x1c, 0x8f, 0x15, 0x8a, 0x8d, 0x12, 0xb2, 0x38, 0x92,
0x89, 0x29, 0x50, 0x47, 0x8c, 0x41, 0x8e, 0x25, 0xcc, 0x39};
static const unsigned char sha256_abc[SHA256_DIGEST_LENGTH] = {
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad};
class sha256Fixture : public ::testing::Test
// Previously validated expected SHA-256 of tests/data/random (1 MiB file)
static const unsigned char sha256_random[SHA256_DIGEST_LENGTH] = {
0x4d, 0x1a, 0x6b, 0x8a, 0x54, 0x67, 0x00, 0xc4, 0x8e, 0xda, 0x70, 0xd3, 0x39, 0x1c, 0x8f, 0x15,
0x8a, 0x8d, 0x12, 0xb2, 0x38, 0x92, 0x89, 0x29, 0x50, 0x47, 0x8c, 0x41, 0x8e, 0x25, 0xcc, 0x39};
#define EXPECT_ARRAY_EQ(expected, actual, len) \
for(size_t _i = 0; _i < (size_t)(len); ++_i) \
EXPECT_EQ(((const unsigned char *)(expected))[_i], ((const unsigned char *)(actual))[_i])
TEST(SHA256, EmptyOneShot)
{
public:
sha256Fixture() = default;
unsigned char out[SHA256_DIGEST_LENGTH];
aaruf_sha256_buffer("", 0, out);
EXPECT_ARRAY_EQ(sha256_empty, out, SHA256_DIGEST_LENGTH);
}
TEST(SHA256, ABCOneShot)
{
unsigned char out[SHA256_DIGEST_LENGTH];
const char *msg = "abc";
aaruf_sha256_buffer(msg, 3, out);
EXPECT_ARRAY_EQ(sha256_abc, out, SHA256_DIGEST_LENGTH);
}
TEST(SHA256, ABCStreaming)
{
unsigned char out[SHA256_DIGEST_LENGTH];
sha256_ctx ctx;
aaruf_sha256_init(&ctx);
const char *msg = "abc";
for(size_t i = 0; i < 3; i++) aaruf_sha256_update(&ctx, msg + i, 1);
aaruf_sha256_final(&ctx, out);
EXPECT_ARRAY_EQ(sha256_abc, out, SHA256_DIGEST_LENGTH);
}
class sha256RandomFixture : public ::testing::Test
{
protected:
unsigned char *buffer = nullptr;
size_t size = 0;
void SetUp() override
{
char path[PATH_MAX];
char filename[PATH_MAX];
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/random", path);
FILE *file = fopen(filename, "rb");
buffer = static_cast<uint8_t *>(malloc(1048576));
fread(buffer, 1, 1048576, file);
fclose(file);
FILE *f = fopen(filename, "rb");
ASSERT_NE(f, nullptr) << "Failed to open random test file";
fseek(f, 0, SEEK_END);
long sz = ftell(f);
ASSERT_GT(sz, 0);
fseek(f, 0, SEEK_SET);
buffer = (unsigned char *)malloc((size_t)sz);
ASSERT_NE(buffer, nullptr);
size = fread(buffer, 1, (size_t)sz, f);
fclose(f);
ASSERT_EQ(size, (size_t)sz);
}
void TearDown() override { free(buffer); }
~sha256Fixture() override = default;
// shared user data
void TearDown() override
{
free(buffer);
buffer = nullptr;
}
};
#define EXPECT_ARRAY_EQ(reference, actual, element_count) \
{ \
unsigned char *reference_ = static_cast<unsigned char *>(reference); \
unsigned char *actual_ = static_cast<unsigned char *>(actual); \
for(int cmp_i = 0; cmp_i < (element_count); cmp_i++) { EXPECT_EQ(reference_[cmp_i], actual_[cmp_i]); } \
}
TEST_F(sha256Fixture, sha256)
TEST_F(sha256RandomFixture, RandomFileOneShot)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX *ctx = static_cast<SHA256_CTX *>(malloc(sizeof(SHA256_CTX)));
EXPECT_NE(nullptr, ctx);
int ret = SHA256_Init(ctx);
EXPECT_EQ(ret, 1);
ret = SHA256_Update(ctx, buffer, 1048576);
EXPECT_EQ(ret, 1);
ret = SHA256_Final(hash, ctx);
EXPECT_EQ(ret, 1);
EXPECT_ARRAY_EQ(expected_sha256, hash, SHA256_DIGEST_LENGTH);
unsigned char out[SHA256_DIGEST_LENGTH];
aaruf_sha256_buffer(buffer, (unsigned long)size, out);
EXPECT_ARRAY_EQ(sha256_random, out, SHA256_DIGEST_LENGTH);
}
#endif
TEST_F(sha256RandomFixture, RandomFileStreaming_Chunk8K)
{
unsigned char out[SHA256_DIGEST_LENGTH];
sha256_ctx ctx;
aaruf_sha256_init(&ctx);
const size_t chunk = 8192;
size_t pos = 0;
while(pos < size)
{
size_t to_do = (size - pos) < chunk ? (size - pos) : chunk;
aaruf_sha256_update(&ctx, buffer + pos, (unsigned long)to_do);
pos += to_do;
}
aaruf_sha256_final(&ctx, out);
EXPECT_ARRAY_EQ(sha256_random, out, SHA256_DIGEST_LENGTH);
}