xref: /dragonfly/contrib/xz/src/liblzma/check/check.h (revision 46a2189d)
12940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
22940b44dSPeter Avalos //
32940b44dSPeter Avalos /// \file       check.h
42940b44dSPeter Avalos /// \brief      Internal API to different integrity check functions
52940b44dSPeter Avalos //
62940b44dSPeter Avalos //  Author:     Lasse Collin
72940b44dSPeter Avalos //
82940b44dSPeter Avalos //  This file has been put into the public domain.
92940b44dSPeter Avalos //  You can do whatever you want with this file.
102940b44dSPeter Avalos //
112940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
122940b44dSPeter Avalos 
132940b44dSPeter Avalos #ifndef LZMA_CHECK_H
142940b44dSPeter Avalos #define LZMA_CHECK_H
152940b44dSPeter Avalos 
162940b44dSPeter Avalos #include "common.h"
172940b44dSPeter Avalos 
18*46a2189dSzrj // If the function for external SHA-256 is missing, use the internal SHA-256
19*46a2189dSzrj // code. Due to how configure works, these defines can only get defined when
20*46a2189dSzrj // both a usable header and a type have already been found.
21*46a2189dSzrj #if !(defined(HAVE_CC_SHA256_INIT) \
22*46a2189dSzrj 		|| defined(HAVE_SHA256_INIT) \
23*46a2189dSzrj 		|| defined(HAVE_SHA256INIT))
24*46a2189dSzrj #	define HAVE_INTERNAL_SHA256 1
25*46a2189dSzrj #endif
26*46a2189dSzrj 
27*46a2189dSzrj #if defined(HAVE_INTERNAL_SHA256)
28*46a2189dSzrj // Nothing
29*46a2189dSzrj #elif defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
3015ab8c86SJohn Marino #	include <CommonCrypto/CommonDigest.h>
3115ab8c86SJohn Marino #elif defined(HAVE_SHA256_H)
3215ab8c86SJohn Marino #	include <sys/types.h>
3315ab8c86SJohn Marino #	include <sha256.h>
3415ab8c86SJohn Marino #elif defined(HAVE_SHA2_H)
3515ab8c86SJohn Marino #	include <sys/types.h>
3615ab8c86SJohn Marino #	include <sha2.h>
3715ab8c86SJohn Marino #endif
3815ab8c86SJohn Marino 
39*46a2189dSzrj #if defined(HAVE_INTERNAL_SHA256)
4015ab8c86SJohn Marino /// State for the internal SHA-256 implementation
4115ab8c86SJohn Marino typedef struct {
4215ab8c86SJohn Marino 	/// Internal state
4315ab8c86SJohn Marino 	uint32_t state[8];
4415ab8c86SJohn Marino 
4515ab8c86SJohn Marino 	/// Size of the message excluding padding
4615ab8c86SJohn Marino 	uint64_t size;
4715ab8c86SJohn Marino } lzma_sha256_state;
48*46a2189dSzrj #elif defined(HAVE_CC_SHA256_CTX)
49*46a2189dSzrj typedef CC_SHA256_CTX lzma_sha256_state;
50*46a2189dSzrj #elif defined(HAVE_SHA256_CTX)
51*46a2189dSzrj typedef SHA256_CTX lzma_sha256_state;
52*46a2189dSzrj #elif defined(HAVE_SHA2_CTX)
53*46a2189dSzrj typedef SHA2_CTX lzma_sha256_state;
5415ab8c86SJohn Marino #endif
5515ab8c86SJohn Marino 
56*46a2189dSzrj #if defined(HAVE_INTERNAL_SHA256)
57*46a2189dSzrj // Nothing
58*46a2189dSzrj #elif defined(HAVE_CC_SHA256_INIT)
5915ab8c86SJohn Marino #	define LZMA_SHA256FUNC(x) CC_SHA256_ ## x
6015ab8c86SJohn Marino #elif defined(HAVE_SHA256_INIT)
6115ab8c86SJohn Marino #	define LZMA_SHA256FUNC(x) SHA256_ ## x
6215ab8c86SJohn Marino #elif defined(HAVE_SHA256INIT)
6315ab8c86SJohn Marino #	define LZMA_SHA256FUNC(x) SHA256 ## x
6415ab8c86SJohn Marino #endif
652940b44dSPeter Avalos 
662940b44dSPeter Avalos // Index hashing needs the best possible hash function (preferably
672940b44dSPeter Avalos // a cryptographic hash) for maximum reliability.
682940b44dSPeter Avalos #if defined(HAVE_CHECK_SHA256)
692940b44dSPeter Avalos #	define LZMA_CHECK_BEST LZMA_CHECK_SHA256
702940b44dSPeter Avalos #elif defined(HAVE_CHECK_CRC64)
712940b44dSPeter Avalos #	define LZMA_CHECK_BEST LZMA_CHECK_CRC64
722940b44dSPeter Avalos #else
732940b44dSPeter Avalos #	define LZMA_CHECK_BEST LZMA_CHECK_CRC32
742940b44dSPeter Avalos #endif
752940b44dSPeter Avalos 
762940b44dSPeter Avalos 
772940b44dSPeter Avalos /// \brief      Structure to hold internal state of the check being calculated
782940b44dSPeter Avalos ///
792940b44dSPeter Avalos /// \note       This is not in the public API because this structure may
802940b44dSPeter Avalos ///             change in future if new integrity check algorithms are added.
812940b44dSPeter Avalos typedef struct {
822940b44dSPeter Avalos 	/// Buffer to hold the final result and a temporary buffer for SHA256.
832940b44dSPeter Avalos 	union {
842940b44dSPeter Avalos 		uint8_t u8[64];
852940b44dSPeter Avalos 		uint32_t u32[16];
862940b44dSPeter Avalos 		uint64_t u64[8];
872940b44dSPeter Avalos 	} buffer;
882940b44dSPeter Avalos 
892940b44dSPeter Avalos 	/// Check-specific data
902940b44dSPeter Avalos 	union {
912940b44dSPeter Avalos 		uint32_t crc32;
922940b44dSPeter Avalos 		uint64_t crc64;
9315ab8c86SJohn Marino 		lzma_sha256_state sha256;
942940b44dSPeter Avalos 	} state;
952940b44dSPeter Avalos 
962940b44dSPeter Avalos } lzma_check_state;
972940b44dSPeter Avalos 
982940b44dSPeter Avalos 
992940b44dSPeter Avalos /// lzma_crc32_table[0] is needed by LZ encoder so we need to keep
1002940b44dSPeter Avalos /// the array two-dimensional.
1012940b44dSPeter Avalos #ifdef HAVE_SMALL
1022940b44dSPeter Avalos extern uint32_t lzma_crc32_table[1][256];
1032940b44dSPeter Avalos extern void lzma_crc32_init(void);
1042940b44dSPeter Avalos #else
1052940b44dSPeter Avalos extern const uint32_t lzma_crc32_table[8][256];
1062940b44dSPeter Avalos extern const uint64_t lzma_crc64_table[4][256];
1072940b44dSPeter Avalos #endif
1082940b44dSPeter Avalos 
1092940b44dSPeter Avalos 
1102940b44dSPeter Avalos /// \brief      Initialize *check depending on type
1112940b44dSPeter Avalos ///
1122940b44dSPeter Avalos /// \return     LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
1132940b44dSPeter Avalos ///             supported by the current version or build of liblzma.
1142940b44dSPeter Avalos ///             LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
1152940b44dSPeter Avalos extern void lzma_check_init(lzma_check_state *check, lzma_check type);
1162940b44dSPeter Avalos 
1172940b44dSPeter Avalos /// Update the check state
1182940b44dSPeter Avalos extern void lzma_check_update(lzma_check_state *check, lzma_check type,
1192940b44dSPeter Avalos 		const uint8_t *buf, size_t size);
1202940b44dSPeter Avalos 
1212940b44dSPeter Avalos /// Finish the check calculation and store the result to check->buffer.u8.
1222940b44dSPeter Avalos extern void lzma_check_finish(lzma_check_state *check, lzma_check type);
1232940b44dSPeter Avalos 
1242940b44dSPeter Avalos 
12515ab8c86SJohn Marino #ifndef LZMA_SHA256FUNC
12615ab8c86SJohn Marino 
1272940b44dSPeter Avalos /// Prepare SHA-256 state for new input.
1282940b44dSPeter Avalos extern void lzma_sha256_init(lzma_check_state *check);
1292940b44dSPeter Avalos 
1302940b44dSPeter Avalos /// Update the SHA-256 hash state
1312940b44dSPeter Avalos extern void lzma_sha256_update(
1322940b44dSPeter Avalos 		const uint8_t *buf, size_t size, lzma_check_state *check);
1332940b44dSPeter Avalos 
1342940b44dSPeter Avalos /// Finish the SHA-256 calculation and store the result to check->buffer.u8.
1352940b44dSPeter Avalos extern void lzma_sha256_finish(lzma_check_state *check);
1362940b44dSPeter Avalos 
13715ab8c86SJohn Marino 
13815ab8c86SJohn Marino #else
13915ab8c86SJohn Marino 
14015ab8c86SJohn Marino static inline void
lzma_sha256_init(lzma_check_state * check)14115ab8c86SJohn Marino lzma_sha256_init(lzma_check_state *check)
14215ab8c86SJohn Marino {
14315ab8c86SJohn Marino 	LZMA_SHA256FUNC(Init)(&check->state.sha256);
14415ab8c86SJohn Marino }
14515ab8c86SJohn Marino 
14615ab8c86SJohn Marino 
14715ab8c86SJohn Marino static inline void
lzma_sha256_update(const uint8_t * buf,size_t size,lzma_check_state * check)14815ab8c86SJohn Marino lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
14915ab8c86SJohn Marino {
15015ab8c86SJohn Marino #if defined(HAVE_CC_SHA256_INIT) && SIZE_MAX > UINT32_MAX
15115ab8c86SJohn Marino 	// Darwin's CC_SHA256_Update takes uint32_t as the buffer size,
15215ab8c86SJohn Marino 	// so use a loop to support size_t.
15315ab8c86SJohn Marino 	while (size > UINT32_MAX) {
15415ab8c86SJohn Marino 		LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, UINT32_MAX);
15515ab8c86SJohn Marino 		buf += UINT32_MAX;
15615ab8c86SJohn Marino 		size -= UINT32_MAX;
15715ab8c86SJohn Marino 	}
15815ab8c86SJohn Marino #endif
15915ab8c86SJohn Marino 
16015ab8c86SJohn Marino 	LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, size);
16115ab8c86SJohn Marino }
16215ab8c86SJohn Marino 
16315ab8c86SJohn Marino 
16415ab8c86SJohn Marino static inline void
lzma_sha256_finish(lzma_check_state * check)16515ab8c86SJohn Marino lzma_sha256_finish(lzma_check_state *check)
16615ab8c86SJohn Marino {
16715ab8c86SJohn Marino 	LZMA_SHA256FUNC(Final)(check->buffer.u8, &check->state.sha256);
16815ab8c86SJohn Marino }
16915ab8c86SJohn Marino 
17015ab8c86SJohn Marino #endif
17115ab8c86SJohn Marino 
1722940b44dSPeter Avalos #endif
173