check.h 2.6 KB

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