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

@@ -24,6 +24,7 @@
#include "lru.h"
#include "md5.h"
#include "sha1.h"
#include "sha256.h"
#include "structs.h"
#include "utarray.h"
@@ -212,12 +213,14 @@ typedef struct aaruformatContext
hash_map_t *sectorHashMap; ///< Deduplication hash map (fingerprint->entry mapping).
bool deduplicate; ///< Storage deduplication active (duplicates coalesce).
bool rewinded; ///< True if stream has been rewound after open (write path).
uint64_t last_written_block; ///< Last written block number (write path).
bool calculating_md5; ///< True if whole-image MD5 being calculated on-the-fly.
md5_ctx md5_context; ///< Opaque MD5 context for streaming updates
bool calculating_sha1; ///< True if whole-image SHA-1 being calculated on-the-fly.
sha1_ctx sha1_context; ///< Opaque SHA-1 context for streaming updates
bool rewinded; ///< True if stream has been rewound after open (write path).
uint64_t last_written_block; ///< Last written block number (write path).
bool calculating_md5; ///< True if whole-image MD5 being calculated on-the-fly.
md5_ctx md5_context; ///< Opaque MD5 context for streaming updates
bool calculating_sha1; ///< True if whole-image SHA-1 being calculated on-the-fly.
sha1_ctx sha1_context; ///< Opaque SHA-1 context for streaming updates
bool calculating_sha256; ///< True if whole-image SHA-256 being calculated on-the-fly.
sha256_ctx sha256_context; ///< Opaque SHA-256 context for streaming updates
} aaruformatContext;
/** \struct DumpHardwareEntriesWithData

View File

@@ -22,6 +22,7 @@
#include "crc64.h"
#include "md5.h"
#include "sha1.h"
#include "sha256.h"
#include "simd.h"
#include "spamsum.h"
#ifdef __cplusplus
@@ -169,6 +170,11 @@ AARU_EXPORT void AARU_CALL aaruf_sha1_update(sha1_ctx *ctx, const void *data, un
AARU_EXPORT void AARU_CALL aaruf_sha1_final(sha1_ctx *ctx, unsigned char *result);
AARU_EXPORT void AARU_CALL aaruf_sha1_buffer(const void *data, unsigned long size, unsigned char *result);
AARU_EXPORT void AARU_CALL aaruf_sha256_init(sha256_ctx *ctx);
AARU_EXPORT void AARU_CALL aaruf_sha256_update(sha256_ctx *ctx, const void *data, unsigned long size);
AARU_EXPORT void AARU_CALL aaruf_sha256_final(sha256_ctx *ctx, unsigned char *result);
AARU_EXPORT void AARU_CALL aaruf_sha256_buffer(const void *data, unsigned long size, unsigned char *result);
#if defined(__x86_64__) || defined(__amd64) || defined(_M_AMD64) || defined(_M_X64) || defined(__I386__) || \
defined(__i386__) || defined(__THW_INTEL) || defined(_M_IX86)

52
include/sha256.h Normal file
View File

@@ -0,0 +1,52 @@
/*
* Public domain / MIT SHA-256 implementation for libaaruformat.
*
* This code is released into the public domain. If that is not recognized
* in your jurisdiction, you may instead treat it under the terms of the
* MIT license reproduced below.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef LIBAARUFORMAT_SHA256_H
#define LIBAARUFORMAT_SHA256_H
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
#ifndef SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH 32
#endif
typedef struct
{
uint32_t state[8];
uint64_t bitcount; /* total bits processed */
uint8_t buffer[64];
} sha256_ctx;
#ifdef __cplusplus
}
#endif
#endif /* LIBAARUFORMAT_SHA256_H */