Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions docs/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/cli/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/cli/cli_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

#include "cli.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

void print_error(const char* format, ...) {
va_list args;
Expand All @@ -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:
Expand Down
21 changes: 0 additions & 21 deletions src/cli/cmd_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,6 @@
#include <string.h>
#include <sys/stat.h>

#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;
Expand Down
20 changes: 0 additions & 20 deletions src/cli/cmd_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,6 @@
#include <sys/stat.h>
#include <time.h>

#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;
Expand Down
64 changes: 63 additions & 1 deletion src/cli/cmd_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -33,20 +35,25 @@ 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");
printf("Examples:\n");
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) {
// Initialize options
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++) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -185,14 +240,21 @@ 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");
break;
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;
}
Expand Down
8 changes: 0 additions & 8 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
15 changes: 12 additions & 3 deletions src/lib/bfc_crc32c.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#endif
#include <immintrin.h>
#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 <arm_acle.h>
#define HAS_ARM64 1
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_crc32c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
Loading