15a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
25a645f22SBen Gras //
35a645f22SBen Gras /// \file       check.h
45a645f22SBen Gras /// \brief      Internal API to different integrity check functions
55a645f22SBen Gras //
65a645f22SBen Gras //  Author:     Lasse Collin
75a645f22SBen Gras //
85a645f22SBen Gras //  This file has been put into the public domain.
95a645f22SBen Gras //  You can do whatever you want with this file.
105a645f22SBen Gras //
115a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
125a645f22SBen Gras 
135a645f22SBen Gras #ifndef LZMA_CHECK_H
145a645f22SBen Gras #define LZMA_CHECK_H
155a645f22SBen Gras 
165a645f22SBen Gras #include "common.h"
175a645f22SBen Gras 
185a645f22SBen Gras #ifdef NETBSD_NATIVE_SHA256
195a645f22SBen Gras #include <sha2.h>
20*0a6a1f1dSLionel Sambuc #else
21*0a6a1f1dSLionel Sambuc #if defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
22*0a6a1f1dSLionel Sambuc #	include <CommonCrypto/CommonDigest.h>
23*0a6a1f1dSLionel Sambuc #elif defined(HAVE_SHA256_H)
24*0a6a1f1dSLionel Sambuc #	include <sys/types.h>
25*0a6a1f1dSLionel Sambuc #	include <sha256.h>
26*0a6a1f1dSLionel Sambuc #elif defined(HAVE_SHA2_H)
27*0a6a1f1dSLionel Sambuc #	include <sys/types.h>
28*0a6a1f1dSLionel Sambuc #	include <sha2.h>
29*0a6a1f1dSLionel Sambuc #elif defined(HAVE_MINIX_SHA2_H)
30*0a6a1f1dSLionel Sambuc #	include <sys/types.h>
31*0a6a1f1dSLionel Sambuc #	include <minix/sha2.h>
32*0a6a1f1dSLionel Sambuc #endif
33*0a6a1f1dSLionel Sambuc #endif
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc #if defined(HAVE_CC_SHA256_CTX)
36*0a6a1f1dSLionel Sambuc typedef CC_SHA256_CTX lzma_sha256_state;
37*0a6a1f1dSLionel Sambuc #elif defined(HAVE_SHA256_CTX)
38*0a6a1f1dSLionel Sambuc typedef SHA256_CTX lzma_sha256_state;
39*0a6a1f1dSLionel Sambuc #elif defined(HAVE_SHA2_CTX)
40*0a6a1f1dSLionel Sambuc typedef SHA2_CTX lzma_sha256_state;
41*0a6a1f1dSLionel Sambuc #else
42*0a6a1f1dSLionel Sambuc /// State for the internal SHA-256 implementation
43*0a6a1f1dSLionel Sambuc typedef struct {
44*0a6a1f1dSLionel Sambuc 	/// Internal state
45*0a6a1f1dSLionel Sambuc 	uint32_t state[8];
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc 	/// Size of the message excluding padding
48*0a6a1f1dSLionel Sambuc 	uint64_t size;
49*0a6a1f1dSLionel Sambuc } lzma_sha256_state;
50*0a6a1f1dSLionel Sambuc #endif
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc #if defined(HAVE_CC_SHA256_INIT)
53*0a6a1f1dSLionel Sambuc #	define LZMA_SHA256FUNC(x) CC_SHA256_ ## x
54*0a6a1f1dSLionel Sambuc #elif defined(HAVE_SHA256_INIT)
55*0a6a1f1dSLionel Sambuc #	define LZMA_SHA256FUNC(x) SHA256_ ## x
56*0a6a1f1dSLionel Sambuc #elif defined(HAVE_SHA256INIT)
57*0a6a1f1dSLionel Sambuc #	define LZMA_SHA256FUNC(x) SHA256 ## x
585a645f22SBen Gras #endif
595a645f22SBen Gras 
605a645f22SBen Gras // Index hashing needs the best possible hash function (preferably
615a645f22SBen Gras // a cryptographic hash) for maximum reliability.
625a645f22SBen Gras #if defined(HAVE_CHECK_SHA256)
635a645f22SBen Gras #	define LZMA_CHECK_BEST LZMA_CHECK_SHA256
645a645f22SBen Gras #elif defined(HAVE_CHECK_CRC64)
655a645f22SBen Gras #	define LZMA_CHECK_BEST LZMA_CHECK_CRC64
665a645f22SBen Gras #else
675a645f22SBen Gras #	define LZMA_CHECK_BEST LZMA_CHECK_CRC32
685a645f22SBen Gras #endif
695a645f22SBen Gras 
705a645f22SBen Gras 
715a645f22SBen Gras /// \brief      Structure to hold internal state of the check being calculated
725a645f22SBen Gras ///
735a645f22SBen Gras /// \note       This is not in the public API because this structure may
745a645f22SBen Gras ///             change in future if new integrity check algorithms are added.
755a645f22SBen Gras typedef struct {
765a645f22SBen Gras 	/// Buffer to hold the final result and a temporary buffer for SHA256.
775a645f22SBen Gras 	union {
785a645f22SBen Gras 		uint8_t u8[64];
795a645f22SBen Gras 		uint32_t u32[16];
805a645f22SBen Gras 		uint64_t u64[8];
815a645f22SBen Gras 	} buffer;
825a645f22SBen Gras 
835a645f22SBen Gras 	/// Check-specific data
845a645f22SBen Gras 	union {
855a645f22SBen Gras 		uint32_t crc32;
865a645f22SBen Gras 		uint64_t crc64;
875a645f22SBen Gras #ifdef NETBSD_NATIVE_SHA256
885a645f22SBen Gras 		SHA256_CTX sha256;
895a645f22SBen Gras #else
90*0a6a1f1dSLionel Sambuc 		lzma_sha256_state sha256;
915a645f22SBen Gras #endif
925a645f22SBen Gras 	} state;
935a645f22SBen Gras 
945a645f22SBen Gras } lzma_check_state;
95*0a6a1f1dSLionel Sambuc #endif
965a645f22SBen Gras 
975a645f22SBen Gras 
985a645f22SBen Gras /// lzma_crc32_table[0] is needed by LZ encoder so we need to keep
995a645f22SBen Gras /// the array two-dimensional.
1005a645f22SBen Gras #ifdef HAVE_SMALL
1015a645f22SBen Gras extern uint32_t lzma_crc32_table[1][256];
1025a645f22SBen Gras extern void lzma_crc32_init(void);
1035a645f22SBen Gras #else
1045a645f22SBen Gras extern const uint32_t lzma_crc32_table[8][256];
1055a645f22SBen Gras extern const uint64_t lzma_crc64_table[4][256];
1065a645f22SBen Gras #endif
1075a645f22SBen Gras 
1085a645f22SBen Gras 
1095a645f22SBen Gras /// \brief      Initialize *check depending on type
1105a645f22SBen Gras ///
1115a645f22SBen Gras /// \return     LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
1125a645f22SBen Gras ///             supported by the current version or build of liblzma.
1135a645f22SBen Gras ///             LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
1145a645f22SBen Gras extern void lzma_check_init(lzma_check_state *check, lzma_check type);
1155a645f22SBen Gras 
1165a645f22SBen Gras /// Update the check state
1175a645f22SBen Gras extern void lzma_check_update(lzma_check_state *check, lzma_check type,
1185a645f22SBen Gras 		const uint8_t *buf, size_t size);
1195a645f22SBen Gras 
1205a645f22SBen Gras /// Finish the check calculation and store the result to check->buffer.u8.
1215a645f22SBen Gras extern void lzma_check_finish(lzma_check_state *check, lzma_check type);
1225a645f22SBen Gras 
1235a645f22SBen Gras #ifdef NETBSD_NATIVE_SHA256
1245a645f22SBen Gras #define lzma_sha256_init(check)	\
1255a645f22SBen Gras 	SHA256_Init(&(check)->state.sha256)
1265a645f22SBen Gras #define lzma_sha256_update(buf,size,check) \
1275a645f22SBen Gras 	SHA256_Update(&(check)->state.sha256, buf, size)
1285a645f22SBen Gras #define lzma_sha256_finish(check) \
1295a645f22SBen Gras 	SHA256_Final((check)->buffer.u8, &(check)->state.sha256)
1305a645f22SBen Gras #else
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc #ifndef LZMA_SHA256FUNC
133*0a6a1f1dSLionel Sambuc 
1345a645f22SBen Gras /// Prepare SHA-256 state for new input.
1355a645f22SBen Gras extern void lzma_sha256_init(lzma_check_state *check);
1365a645f22SBen Gras 
1375a645f22SBen Gras /// Update the SHA-256 hash state
1385a645f22SBen Gras extern void lzma_sha256_update(
1395a645f22SBen Gras 		const uint8_t *buf, size_t size, lzma_check_state *check);
1405a645f22SBen Gras 
1415a645f22SBen Gras /// Finish the SHA-256 calculation and store the result to check->buffer.u8.
1425a645f22SBen Gras extern void lzma_sha256_finish(lzma_check_state *check);
143*0a6a1f1dSLionel Sambuc 
144*0a6a1f1dSLionel Sambuc 
145*0a6a1f1dSLionel Sambuc #else
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc static inline void
lzma_sha256_init(lzma_check_state * check)148*0a6a1f1dSLionel Sambuc lzma_sha256_init(lzma_check_state *check)
149*0a6a1f1dSLionel Sambuc {
150*0a6a1f1dSLionel Sambuc 	LZMA_SHA256FUNC(Init)(&check->state.sha256);
151*0a6a1f1dSLionel Sambuc }
152*0a6a1f1dSLionel Sambuc 
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc static inline void
lzma_sha256_update(const uint8_t * buf,size_t size,lzma_check_state * check)155*0a6a1f1dSLionel Sambuc lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
156*0a6a1f1dSLionel Sambuc {
157*0a6a1f1dSLionel Sambuc #if defined(HAVE_CC_SHA256_INIT) && SIZE_MAX > UINT32_MAX
158*0a6a1f1dSLionel Sambuc 	// Darwin's CC_SHA256_Update takes uint32_t as the buffer size,
159*0a6a1f1dSLionel Sambuc 	// so use a loop to support size_t.
160*0a6a1f1dSLionel Sambuc 	while (size > UINT32_MAX) {
161*0a6a1f1dSLionel Sambuc 		LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, UINT32_MAX);
162*0a6a1f1dSLionel Sambuc 		buf += UINT32_MAX;
163*0a6a1f1dSLionel Sambuc 		size -= UINT32_MAX;
164*0a6a1f1dSLionel Sambuc 	}
165*0a6a1f1dSLionel Sambuc #endif
166*0a6a1f1dSLionel Sambuc 
167*0a6a1f1dSLionel Sambuc 	LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, size);
168*0a6a1f1dSLionel Sambuc }
169*0a6a1f1dSLionel Sambuc 
170*0a6a1f1dSLionel Sambuc 
171*0a6a1f1dSLionel Sambuc static inline void
lzma_sha256_finish(lzma_check_state * check)172*0a6a1f1dSLionel Sambuc lzma_sha256_finish(lzma_check_state *check)
173*0a6a1f1dSLionel Sambuc {
174*0a6a1f1dSLionel Sambuc 	LZMA_SHA256FUNC(Final)(check->buffer.u8, &check->state.sha256);
175*0a6a1f1dSLionel Sambuc }
176*0a6a1f1dSLionel Sambuc 
1775a645f22SBen Gras #endif
1785a645f22SBen Gras 
1795a645f22SBen Gras #endif
180