Path: blob/master/Utilities/cmliblzma/liblzma/check/check.h
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file check.h5/// \brief Internal API to different integrity check functions6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#ifndef LZMA_CHECK_H12#define LZMA_CHECK_H1314#include "common.h"1516// If the function for external SHA-256 is missing, use the internal SHA-25617// code. Due to how configure works, these defines can only get defined when18// both a usable header and a type have already been found.19#if !(defined(HAVE_CC_SHA256_INIT) \20|| defined(HAVE_SHA256_INIT) \21|| defined(HAVE_SHA256INIT))22# define HAVE_INTERNAL_SHA256 123#endif2425#if defined(HAVE_INTERNAL_SHA256)26// Nothing27#elif defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)28# include <CommonCrypto/CommonDigest.h>29#elif defined(HAVE_SHA256_H)30# include <sys/types.h>31# include <sha256.h>32#elif defined(HAVE_SHA2_H)33# include <sys/types.h>34# include <sha2.h>35#endif3637#if defined(HAVE_INTERNAL_SHA256)38/// State for the internal SHA-256 implementation39typedef struct {40/// Internal state41uint32_t state[8];4243/// Size of the message excluding padding44uint64_t size;45} lzma_sha256_state;46#elif defined(HAVE_CC_SHA256_CTX)47typedef CC_SHA256_CTX lzma_sha256_state;48#elif defined(HAVE_SHA256_CTX)49typedef SHA256_CTX lzma_sha256_state;50#elif defined(HAVE_SHA2_CTX)51typedef SHA2_CTX lzma_sha256_state;52#endif5354#if defined(HAVE_INTERNAL_SHA256)55// Nothing56#elif defined(HAVE_CC_SHA256_INIT)57# define LZMA_SHA256FUNC(x) CC_SHA256_ ## x58#elif defined(HAVE_SHA256_INIT)59# define LZMA_SHA256FUNC(x) SHA256_ ## x60#elif defined(HAVE_SHA256INIT)61# define LZMA_SHA256FUNC(x) SHA256 ## x62#endif6364// Index hashing needs the best possible hash function (preferably65// a cryptographic hash) for maximum reliability.66#if defined(HAVE_CHECK_SHA256)67# define LZMA_CHECK_BEST LZMA_CHECK_SHA25668#elif defined(HAVE_CHECK_CRC64)69# define LZMA_CHECK_BEST LZMA_CHECK_CRC6470#else71# define LZMA_CHECK_BEST LZMA_CHECK_CRC3272#endif737475/// \brief Structure to hold internal state of the check being calculated76///77/// \note This is not in the public API because this structure may78/// change in future if new integrity check algorithms are added.79typedef struct {80/// Buffer to hold the final result and a temporary buffer for SHA256.81union {82uint8_t u8[64];83uint32_t u32[16];84uint64_t u64[8];85} buffer;8687/// Check-specific data88union {89uint32_t crc32;90uint64_t crc64;91lzma_sha256_state sha256;92} state;9394} lzma_check_state;959697/// lzma_crc32_table[0] is needed by LZ encoder so we need to keep98/// the array two-dimensional.99#ifdef HAVE_SMALL100lzma_attr_visibility_hidden101extern uint32_t lzma_crc32_table[1][256];102103extern void lzma_crc32_init(void);104105#else106107lzma_attr_visibility_hidden108extern const uint32_t lzma_crc32_table[8][256];109110lzma_attr_visibility_hidden111extern const uint64_t lzma_crc64_table[4][256];112#endif113114115/// \brief Initialize *check depending on type116extern void lzma_check_init(lzma_check_state *check, lzma_check type);117118/// Update the check state119extern void lzma_check_update(lzma_check_state *check, lzma_check type,120const uint8_t *buf, size_t size);121122/// Finish the check calculation and store the result to check->buffer.u8.123extern void lzma_check_finish(lzma_check_state *check, lzma_check type);124125126#ifndef LZMA_SHA256FUNC127128/// Prepare SHA-256 state for new input.129extern void lzma_sha256_init(lzma_check_state *check);130131/// Update the SHA-256 hash state132extern void lzma_sha256_update(133const uint8_t *buf, size_t size, lzma_check_state *check);134135/// Finish the SHA-256 calculation and store the result to check->buffer.u8.136extern void lzma_sha256_finish(lzma_check_state *check);137138139#else140141static inline void142lzma_sha256_init(lzma_check_state *check)143{144LZMA_SHA256FUNC(Init)(&check->state.sha256);145}146147148static inline void149lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)150{151#if defined(HAVE_CC_SHA256_INIT) && SIZE_MAX > UINT32_MAX152// Darwin's CC_SHA256_Update takes uint32_t as the buffer size,153// so use a loop to support size_t.154while (size > UINT32_MAX) {155LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, UINT32_MAX);156buf += UINT32_MAX;157size -= UINT32_MAX;158}159#endif160161LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, size);162}163164165static inline void166lzma_sha256_finish(lzma_check_state *check)167{168LZMA_SHA256FUNC(Final)(check->buffer.u8, &check->state.sha256);169}170171#endif172173#endif174175176