xref: /freebsd/contrib/xz/src/liblzma/check/check.h (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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 
19 // Index hashing needs the best possible hash function (preferably
20 // a cryptographic hash) for maximum reliability.
21 #if defined(HAVE_CHECK_SHA256)
22 #	define LZMA_CHECK_BEST LZMA_CHECK_SHA256
23 #elif defined(HAVE_CHECK_CRC64)
24 #	define LZMA_CHECK_BEST LZMA_CHECK_CRC64
25 #else
26 #	define LZMA_CHECK_BEST LZMA_CHECK_CRC32
27 #endif
28 
29 
30 /// \brief      Structure to hold internal state of the check being calculated
31 ///
32 /// \note       This is not in the public API because this structure may
33 ///             change in future if new integrity check algorithms are added.
34 typedef struct {
35 	/// Buffer to hold the final result and a temporary buffer for SHA256.
36 	union {
37 		uint8_t u8[64];
38 		uint32_t u32[16];
39 		uint64_t u64[8];
40 	} buffer;
41 
42 	/// Check-specific data
43 	union {
44 		uint32_t crc32;
45 		uint64_t crc64;
46 
47 		struct {
48 			/// Internal state
49 			uint32_t state[8];
50 
51 			/// Size of the message excluding padding
52 			uint64_t size;
53 		} sha256;
54 	} state;
55 
56 } lzma_check_state;
57 
58 
59 /// lzma_crc32_table[0] is needed by LZ encoder so we need to keep
60 /// the array two-dimensional.
61 #ifdef HAVE_SMALL
62 extern uint32_t lzma_crc32_table[1][256];
63 extern void lzma_crc32_init(void);
64 #else
65 extern const uint32_t lzma_crc32_table[8][256];
66 extern const uint64_t lzma_crc64_table[4][256];
67 #endif
68 
69 
70 /// \brief      Initialize *check depending on type
71 ///
72 /// \return     LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
73 ///             supported by the current version or build of liblzma.
74 ///             LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
75 extern void lzma_check_init(lzma_check_state *check, lzma_check type);
76 
77 /// Update the check state
78 extern void lzma_check_update(lzma_check_state *check, lzma_check type,
79 		const uint8_t *buf, size_t size);
80 
81 /// Finish the check calculation and store the result to check->buffer.u8.
82 extern void lzma_check_finish(lzma_check_state *check, lzma_check type);
83 
84 
85 /// Prepare SHA-256 state for new input.
86 extern void lzma_sha256_init(lzma_check_state *check);
87 
88 /// Update the SHA-256 hash state
89 extern void lzma_sha256_update(
90 		const uint8_t *buf, size_t size, lzma_check_state *check);
91 
92 /// Finish the SHA-256 calculation and store the result to check->buffer.u8.
93 extern void lzma_sha256_finish(lzma_check_state *check);
94 
95 #endif
96