diff --git a/CMakeLists.txt b/CMakeLists.txt index 681e413..f18ae34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ # limitations under the License. cmake_minimum_required(VERSION 3.15) -project(bfc VERSION 1.4.0 LANGUAGES C) +project(bfc VERSION 1.4.1 LANGUAGES C) set(CMAKE_C_STANDARD 17) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/docs/format.md b/docs/format.md index 6c91587..e3f28cf 100644 --- a/docs/format.md +++ b/docs/format.md @@ -148,6 +148,10 @@ struct BfcFooter { ## Integrity Guarantees - **CRC32C** used for content validation (hardware accelerated when available) + - Castagnoli polynomial (reflected `0x82F63B78`), initial value `0`, no final XOR + - This is what the SSE4.2/ARMv8 CRC32C instructions compute directly; it is not + the conventional CRC32C (`~0` init, final XOR). Check value for `"123456789"` + is `0x58E3FA20` - **Atomic commits** via index+footer write with fsync - **Crash recovery** possible by scanning for previous valid footer - **Tamper detection** via checksum validation on read diff --git a/src/cli/cli.h b/src/cli/cli.h index 5cc6ab6..fb85579 100644 --- a/src/cli/cli.h +++ b/src/cli/cli.h @@ -57,6 +57,7 @@ void print_error(const char* format, ...); void print_verbose(const char* format, ...); int parse_global_options(int* argc, char*** argv, bfc_options_t* opts); const char* bfc_error_string(int error_code); +int read_key_from_file(const char* keyfile, uint8_t key[32]); // Global options (extern declaration) extern bfc_options_t g_options; diff --git a/src/cli/cli_util.c b/src/cli/cli_util.c index 00edc53..6c4e16d 100644 --- a/src/cli/cli_util.c +++ b/src/cli/cli_util.c @@ -15,8 +15,10 @@ */ #include "cli.h" +#include #include #include +#include void print_error(const char* format, ...) { va_list args; @@ -40,6 +42,25 @@ void print_verbose(const char* format, ...) { va_end(args); } +// Read a 32-byte encryption key from a file +int read_key_from_file(const char* keyfile, uint8_t key[32]) { + FILE* f = fopen(keyfile, "rb"); + if (!f) { + print_error("Cannot open key file '%s': %s", keyfile, strerror(errno)); + return -1; + } + + size_t bytes_read = fread(key, 1, 32, f); + fclose(f); + + if (bytes_read != 32) { + print_error("Key file '%s' must contain exactly 32 bytes (got %zu)", keyfile, bytes_read); + return -1; + } + + return 0; +} + const char* bfc_error_string(int error_code) { switch (error_code) { case BFC_OK: diff --git a/src/cli/cmd_create.c b/src/cli/cmd_create.c index e407bf0..06c84f2 100644 --- a/src/cli/cmd_create.c +++ b/src/cli/cmd_create.c @@ -26,27 +26,6 @@ #include #include -#ifdef BFC_WITH_SODIUM -// Function to read encryption key from file -static int read_key_from_file(const char* keyfile, uint8_t key[32]) { - FILE* f = fopen(keyfile, "rb"); - if (!f) { - print_error("Cannot open key file '%s': %s", keyfile, strerror(errno)); - return -1; - } - - size_t bytes_read = fread(key, 1, 32, f); - fclose(f); - - if (bytes_read != 32) { - print_error("Key file '%s' must contain exactly 32 bytes (got %zu)", keyfile, bytes_read); - return -1; - } - - return 0; -} -#endif - typedef struct { uint32_t block_size; int force; diff --git a/src/cli/cmd_extract.c b/src/cli/cmd_extract.c index f9d52d5..b9be52f 100644 --- a/src/cli/cmd_extract.c +++ b/src/cli/cmd_extract.c @@ -28,26 +28,6 @@ #include #include -#ifdef BFC_WITH_SODIUM -static int read_key_from_file(const char* filename, uint8_t key[32]) { - int fd = open(filename, O_RDONLY); - if (fd < 0) { - print_error("Cannot open key file '%s': %s", filename, strerror(errno)); - return -1; - } - - ssize_t bytes_read = read(fd, key, 32); - close(fd); - - if (bytes_read != 32) { - print_error("Key file '%s' must be exactly 32 bytes, got %zd bytes", filename, bytes_read); - return -1; - } - - return 0; -} -#endif - typedef struct { int force; int preserve_paths; diff --git a/src/cli/cmd_verify.c b/src/cli/cmd_verify.c index c263c09..eefc47f 100644 --- a/src/cli/cmd_verify.c +++ b/src/cli/cmd_verify.c @@ -25,6 +25,8 @@ typedef struct { const char* container_file; int deep_verify; int show_progress; + const char* encryption_password; + const char* encryption_keyfile; } verify_options_t; static void print_verify_help(void) { @@ -33,6 +35,8 @@ static void print_verify_help(void) { printf("Options:\n"); printf(" --deep Perform deep verification (read and check all file contents)\n"); printf(" -p, --progress Show progress during verification\n"); + printf(" --password PASS Password for encrypted container (needed for --deep)\n"); + printf(" -K, --keyfile FILE Key file for encrypted container (32 bytes)\n"); printf(" -h, --help Show this help message\n\n"); printf("Arguments:\n"); printf(" container.bfc BFC container to verify\n\n"); @@ -40,6 +44,7 @@ static void print_verify_help(void) { printf(" bfc verify archive.bfc # Quick structural verification\n"); printf(" bfc verify --deep archive.bfc # Full content verification\n"); printf(" bfc verify -p --deep archive.bfc # Deep verification with progress\n"); + printf(" bfc verify --deep --password secret archive.bfc # Deep verify encrypted container\n"); } static int parse_verify_options(int argc, char* argv[], verify_options_t* opts) { @@ -47,6 +52,8 @@ static int parse_verify_options(int argc, char* argv[], verify_options_t* opts) opts->container_file = NULL; opts->deep_verify = 0; opts->show_progress = 0; + opts->encryption_password = NULL; + opts->encryption_keyfile = NULL; int i; for (i = 1; i < argc; i++) { @@ -57,6 +64,18 @@ static int parse_verify_options(int argc, char* argv[], verify_options_t* opts) opts->deep_verify = 1; } else if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--progress") == 0) { opts->show_progress = 1; + } else if (strcmp(argv[i], "--password") == 0) { + if (i + 1 >= argc) { + print_error("--password requires an argument"); + return -1; + } + opts->encryption_password = argv[++i]; + } else if (strcmp(argv[i], "-K") == 0 || strcmp(argv[i], "--keyfile") == 0) { + if (i + 1 >= argc) { + print_error("--keyfile requires an argument"); + return -1; + } + opts->encryption_keyfile = argv[++i]; } else if (argv[i][0] == '-') { // Handle combined short options like -p const char* opt = argv[i] + 1; @@ -127,6 +146,42 @@ int cmd_verify(int argc, char* argv[]) { return 1; } + // Configure encryption if needed +#ifdef BFC_WITH_SODIUM + if (opts.encryption_password) { + result = bfc_reader_set_encryption_password(reader, opts.encryption_password, + strlen(opts.encryption_password)); + if (result != BFC_OK) { + print_error("Failed to set encryption password: %s", bfc_error_string(result)); + bfc_close_read(reader); + return 1; + } + } else if (opts.encryption_keyfile) { + uint8_t key[32]; + if (read_key_from_file(opts.encryption_keyfile, key) != 0) { + bfc_close_read(reader); + return 1; + } + + result = bfc_reader_set_encryption_key(reader, key); + + // Clear key from memory + memset(key, 0, sizeof(key)); + + if (result != BFC_OK) { + print_error("Failed to set encryption key: %s", bfc_error_string(result)); + bfc_close_read(reader); + return 1; + } + } +#else + if (opts.encryption_password || opts.encryption_keyfile) { + print_error("Encryption support not available. Please build with BFC_WITH_SODIUM=ON"); + bfc_close_read(reader); + return 1; + } +#endif + if (!g_options.quiet) { printf("Verifying container: %s\n", opts.container_file); if (opts.deep_verify) { @@ -185,7 +240,11 @@ int cmd_verify(int argc, char* argv[]) { print_error("The file is not a valid BFC container or is corrupted"); break; case BFC_E_CRC: - print_error("CRC32C checksum mismatch detected - data corruption"); + if (opts.encryption_password || opts.encryption_keyfile) { + print_error("Checksum mismatch - wrong password/key or data corruption"); + } else { + print_error("CRC32C checksum mismatch detected - data corruption"); + } break; case BFC_E_IO: print_error("I/O error occurred while reading the container"); @@ -193,6 +252,9 @@ int cmd_verify(int argc, char* argv[]) { case BFC_E_INVAL: print_error("Invalid container structure or format"); break; + case BFC_E_PERM: + print_error("Container content is encrypted; pass --password or --keyfile to verify it"); + break; default: break; } diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index c164966..50ef7a9 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -35,14 +35,6 @@ set(BFC_LIB_HEADERS add_library(bfc STATIC ${BFC_LIB_SOURCES}) add_library(bfc_shared SHARED ${BFC_LIB_SOURCES}) -# Enable SSE4.2 and CRC32 for hardware support on x86_64 -if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|amd64") - if(NOT MSVC) - target_compile_options(bfc PRIVATE -msse4.2 -mcrc32) - target_compile_options(bfc_shared PRIVATE -msse4.2 -mcrc32) - endif() -endif() - # Set properties for both libraries foreach(target bfc bfc_shared) target_include_directories(${target} diff --git a/src/lib/bfc_crc32c.c b/src/lib/bfc_crc32c.c index 93e395f..9e2594b 100644 --- a/src/lib/bfc_crc32c.c +++ b/src/lib/bfc_crc32c.c @@ -26,6 +26,11 @@ #endif #include #define HAS_X86_64 1 +#ifdef _MSC_VER +#define BFC_TARGET_SSE42 +#else +#define BFC_TARGET_SSE42 __attribute__((target("sse4.2"))) +#endif #elif defined(__aarch64__) || defined(_M_ARM64) #include #define HAS_ARM64 1 @@ -68,7 +73,9 @@ static int detect_sse42_support(void) { #endif } -static uint32_t crc32c_hw_x86(uint32_t crc, const void* data, size_t len) { +// Only this function is compiled for SSE4.2, so the rest of the library stays +// runnable on CPUs without it; detect_sse42_support() gates the call. +static BFC_TARGET_SSE42 uint32_t crc32c_hw_x86(uint32_t crc, const void* data, size_t len) { const uint8_t* ptr = (const uint8_t*) data; // Process aligned 8-byte chunks @@ -164,14 +171,16 @@ static uint32_t crc32c_hw_arm(uint32_t crc, const void* data, size_t len) { } #endif +// Must match the hardware paths bit for bit: the CRC32 instructions apply no +// pre/post inversion, so neither does this. Containers written on one machine +// have to verify on any other. static uint32_t crc32c_sw(uint32_t crc, const void* data, size_t len) { const uint8_t* ptr = (const uint8_t*) data; - crc = ~crc; for (size_t i = 0; i < len; i++) { crc = crc32c_table[(crc ^ ptr[i]) & 0xFF] ^ (crc >> 8); } - return ~crc; + return crc; } void bfc_crc32c_init(void) { diff --git a/tests/unit/test_crc32c.c b/tests/unit/test_crc32c.c index 4609943..cafca3d 100644 --- a/tests/unit/test_crc32c.c +++ b/tests/unit/test_crc32c.c @@ -48,6 +48,17 @@ static int test_known_values(void) { return 0; } +static int test_format_check_value(void) { + // Pins the on-disk checksum definition: Castagnoli polynomial, initial value 0, + // no final XOR. Hardware and software paths must both produce this value, + // otherwise containers stop verifying across machines. (The standard CRC32C + // check value, with ~0 init and final XOR, would be 0xE3069283.) + const char* check = "123456789"; + assert(bfc_crc32c_compute(check, strlen(check)) == 0x58E3FA20U); + + return 0; +} + static int test_streaming_crc(void) { const char* data = "hello world"; size_t len = strlen(data); @@ -261,6 +272,8 @@ int test_crc32c(void) { return 1; if (test_known_values() != 0) return 1; + if (test_format_check_value() != 0) + return 1; if (test_streaming_crc() != 0) return 1; if (test_incremental_crc() != 0)