1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       check.h
4207753Smm/// \brief      Internal API to different integrity check functions
5207753Smm//
6207753Smm//  Author:     Lasse Collin
7207753Smm//
8207753Smm//  This file has been put into the public domain.
9207753Smm//  You can do whatever you want with this file.
10207753Smm//
11207753Smm///////////////////////////////////////////////////////////////////////////////
12207753Smm
13207753Smm#ifndef LZMA_CHECK_H
14207753Smm#define LZMA_CHECK_H
15207753Smm
16207753Smm#include "common.h"
17207753Smm
18207753Smm
19207753Smm// Index hashing needs the best possible hash function (preferably
20207753Smm// a cryptographic hash) for maximum reliability.
21207753Smm#if defined(HAVE_CHECK_SHA256)
22207753Smm#	define LZMA_CHECK_BEST LZMA_CHECK_SHA256
23207753Smm#elif defined(HAVE_CHECK_CRC64)
24207753Smm#	define LZMA_CHECK_BEST LZMA_CHECK_CRC64
25207753Smm#else
26207753Smm#	define LZMA_CHECK_BEST LZMA_CHECK_CRC32
27207753Smm#endif
28207753Smm
29207753Smm
30207753Smm/// \brief      Structure to hold internal state of the check being calculated
31207753Smm///
32207753Smm/// \note       This is not in the public API because this structure may
33207753Smm///             change in future if new integrity check algorithms are added.
34207753Smmtypedef struct {
35207753Smm	/// Buffer to hold the final result and a temporary buffer for SHA256.
36207753Smm	union {
37207753Smm		uint8_t u8[64];
38207753Smm		uint32_t u32[16];
39207753Smm		uint64_t u64[8];
40207753Smm	} buffer;
41207753Smm
42207753Smm	/// Check-specific data
43207753Smm	union {
44207753Smm		uint32_t crc32;
45207753Smm		uint64_t crc64;
46207753Smm
47207753Smm		struct {
48207753Smm			/// Internal state
49207753Smm			uint32_t state[8];
50207753Smm
51207753Smm			/// Size of the message excluding padding
52207753Smm			uint64_t size;
53207753Smm		} sha256;
54207753Smm	} state;
55207753Smm
56207753Smm} lzma_check_state;
57207753Smm
58207753Smm
59207753Smm/// lzma_crc32_table[0] is needed by LZ encoder so we need to keep
60207753Smm/// the array two-dimensional.
61207753Smm#ifdef HAVE_SMALL
62207753Smmextern uint32_t lzma_crc32_table[1][256];
63207753Smmextern void lzma_crc32_init(void);
64207753Smm#else
65207753Smmextern const uint32_t lzma_crc32_table[8][256];
66207753Smmextern const uint64_t lzma_crc64_table[4][256];
67207753Smm#endif
68207753Smm
69207753Smm
70207753Smm/// \brief      Initialize *check depending on type
71207753Smm///
72207753Smm/// \return     LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
73207753Smm///             supported by the current version or build of liblzma.
74207753Smm///             LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
75207753Smmextern void lzma_check_init(lzma_check_state *check, lzma_check type);
76207753Smm
77207753Smm/// Update the check state
78207753Smmextern void lzma_check_update(lzma_check_state *check, lzma_check type,
79207753Smm		const uint8_t *buf, size_t size);
80207753Smm
81207753Smm/// Finish the check calculation and store the result to check->buffer.u8.
82207753Smmextern void lzma_check_finish(lzma_check_state *check, lzma_check type);
83207753Smm
84207753Smm
85207753Smm/// Prepare SHA-256 state for new input.
86207753Smmextern void lzma_sha256_init(lzma_check_state *check);
87207753Smm
88207753Smm/// Update the SHA-256 hash state
89207753Smmextern void lzma_sha256_update(
90207753Smm		const uint8_t *buf, size_t size, lzma_check_state *check);
91207753Smm
92207753Smm/// Finish the SHA-256 calculation and store the result to check->buffer.u8.
93207753Smmextern void lzma_sha256_finish(lzma_check_state *check);
94207753Smm
95207753Smm#endif
96