1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       check.h
4 /// \brief      Internal API to different integrity check functions
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #ifndef LZMA_CHECK_H
14 #define LZMA_CHECK_H
15 
16 #include "common.h"
17 
18 #ifdef NETBSD_NATIVE_SHA256
19 #include <sha2.h>
20 #endif
21 
22 // Index hashing needs the best possible hash function (preferably
23 // a cryptographic hash) for maximum reliability.
24 #if defined(HAVE_CHECK_SHA256)
25 #	define LZMA_CHECK_BEST LZMA_CHECK_SHA256
26 #elif defined(HAVE_CHECK_CRC64)
27 #	define LZMA_CHECK_BEST LZMA_CHECK_CRC64
28 #else
29 #	define LZMA_CHECK_BEST LZMA_CHECK_CRC32
30 #endif
31 
32 
33 /// \brief      Structure to hold internal state of the check being calculated
34 ///
35 /// \note       This is not in the public API because this structure may
36 ///             change in future if new integrity check algorithms are added.
37 typedef struct {
38 	/// Buffer to hold the final result and a temporary buffer for SHA256.
39 	union {
40 		uint8_t u8[64];
41 		uint32_t u32[16];
42 		uint64_t u64[8];
43 	} buffer;
44 
45 	/// Check-specific data
46 	union {
47 		uint32_t crc32;
48 		uint64_t crc64;
49 #ifdef NETBSD_NATIVE_SHA256
50 		SHA256_CTX sha256;
51 #else
52 		struct {
53 			/// Internal state
54 			uint32_t state[8];
55 
56 			/// Size of the message excluding padding
57 			uint64_t size;
58 		} sha256;
59 #endif
60 	} state;
61 
62 } lzma_check_state;
63 
64 
65 /// lzma_crc32_table[0] is needed by LZ encoder so we need to keep
66 /// the array two-dimensional.
67 #ifdef HAVE_SMALL
68 extern uint32_t lzma_crc32_table[1][256];
69 extern void lzma_crc32_init(void);
70 #else
71 extern const uint32_t lzma_crc32_table[8][256];
72 extern const uint64_t lzma_crc64_table[4][256];
73 #endif
74 
75 
76 /// \brief      Initialize *check depending on type
77 ///
78 /// \return     LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
79 ///             supported by the current version or build of liblzma.
80 ///             LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
81 extern void lzma_check_init(lzma_check_state *check, lzma_check type);
82 
83 /// Update the check state
84 extern void lzma_check_update(lzma_check_state *check, lzma_check type,
85 		const uint8_t *buf, size_t size);
86 
87 /// Finish the check calculation and store the result to check->buffer.u8.
88 extern void lzma_check_finish(lzma_check_state *check, lzma_check type);
89 
90 #ifdef NETBSD_NATIVE_SHA256
91 #define lzma_sha256_init(check)	\
92 	SHA256_Init(&(check)->state.sha256)
93 #define lzma_sha256_update(buf,size,check) \
94 	SHA256_Update(&(check)->state.sha256, buf, size)
95 #define lzma_sha256_finish(check) \
96 	SHA256_Final((check)->buffer.u8, &(check)->state.sha256)
97 #else
98 /// Prepare SHA-256 state for new input.
99 extern void lzma_sha256_init(lzma_check_state *check);
100 
101 /// Update the SHA-256 hash state
102 extern void lzma_sha256_update(
103 		const uint8_t *buf, size_t size, lzma_check_state *check);
104 
105 /// Finish the SHA-256 calculation and store the result to check->buffer.u8.
106 extern void lzma_sha256_finish(lzma_check_state *check);
107 #endif
108 
109 #endif
110