check.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // If the function for external SHA-256 is missing, use the internal SHA-256
  16. // code. Due to how configure works, these defines can only get defined when
  17. // both a usable header and a type have already been found.
  18. #if !(defined(HAVE_CC_SHA256_INIT) \
  19. || defined(HAVE_SHA256_INIT) \
  20. || defined(HAVE_SHA256INIT))
  21. # define HAVE_INTERNAL_SHA256 1
  22. #endif
  23. #if defined(HAVE_INTERNAL_SHA256)
  24. // Nothing
  25. #elif defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
  26. # include <CommonCrypto/CommonDigest.h>
  27. #elif defined(HAVE_SHA256_H)
  28. # include <sys/types.h>
  29. # error #include <sha256.h>
  30. #elif defined(HAVE_SHA2_H)
  31. # include <sys/types.h>
  32. # error #include <sha2.h>
  33. #endif
  34. #if defined(HAVE_INTERNAL_SHA256)
  35. /// State for the internal SHA-256 implementation
  36. typedef struct {
  37. /// Internal state
  38. uint32_t state[8];
  39. /// Size of the message excluding padding
  40. uint64_t size;
  41. } lzma_sha256_state;
  42. #elif defined(HAVE_CC_SHA256_CTX)
  43. typedef CC_SHA256_CTX lzma_sha256_state;
  44. #elif defined(HAVE_SHA256_CTX)
  45. typedef SHA256_CTX lzma_sha256_state;
  46. #elif defined(HAVE_SHA2_CTX)
  47. typedef SHA2_CTX lzma_sha256_state;
  48. #endif
  49. #if defined(HAVE_INTERNAL_SHA256)
  50. // Nothing
  51. #elif defined(HAVE_CC_SHA256_INIT)
  52. # define LZMA_SHA256FUNC(x) CC_SHA256_ ## x
  53. #elif defined(HAVE_SHA256_INIT)
  54. # define LZMA_SHA256FUNC(x) SHA256_ ## x
  55. #elif defined(HAVE_SHA256INIT)
  56. # define LZMA_SHA256FUNC(x) SHA256 ## x
  57. #endif
  58. // Index hashing needs the best possible hash function (preferably
  59. // a cryptographic hash) for maximum reliability.
  60. #if defined(HAVE_CHECK_SHA256)
  61. # define LZMA_CHECK_BEST LZMA_CHECK_SHA256
  62. #elif defined(HAVE_CHECK_CRC64)
  63. # define LZMA_CHECK_BEST LZMA_CHECK_CRC64
  64. #else
  65. # define LZMA_CHECK_BEST LZMA_CHECK_CRC32
  66. #endif
  67. /// \brief Structure to hold internal state of the check being calculated
  68. ///
  69. /// \note This is not in the public API because this structure may
  70. /// change in future if new integrity check algorithms are added.
  71. typedef struct {
  72. /// Buffer to hold the final result and a temporary buffer for SHA256.
  73. union {
  74. uint8_t u8[64];
  75. uint32_t u32[16];
  76. uint64_t u64[8];
  77. } buffer;
  78. /// Check-specific data
  79. union {
  80. uint32_t crc32;
  81. uint64_t crc64;
  82. lzma_sha256_state sha256;
  83. } state;
  84. } lzma_check_state;
  85. /// lzma_crc32_table[0] is needed by LZ encoder so we need to keep
  86. /// the array two-dimensional.
  87. #ifdef HAVE_SMALL
  88. extern uint32_t lzma_crc32_table[1][256];
  89. extern void lzma_crc32_init(void);
  90. #else
  91. extern const uint32_t lzma_crc32_table[8][256];
  92. extern const uint64_t lzma_crc64_table[4][256];
  93. #endif
  94. /// \brief Initialize *check depending on type
  95. ///
  96. /// \return LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
  97. /// supported by the current version or build of liblzma.
  98. /// LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
  99. extern void lzma_check_init(lzma_check_state *check, lzma_check type);
  100. /// Update the check state
  101. extern void lzma_check_update(lzma_check_state *check, lzma_check type,
  102. const uint8_t *buf, size_t size);
  103. /// Finish the check calculation and store the result to check->buffer.u8.
  104. extern void lzma_check_finish(lzma_check_state *check, lzma_check type);
  105. #ifndef LZMA_SHA256FUNC
  106. /// Prepare SHA-256 state for new input.
  107. extern void lzma_sha256_init(lzma_check_state *check);
  108. /// Update the SHA-256 hash state
  109. extern void lzma_sha256_update(
  110. const uint8_t *buf, size_t size, lzma_check_state *check);
  111. /// Finish the SHA-256 calculation and store the result to check->buffer.u8.
  112. extern void lzma_sha256_finish(lzma_check_state *check);
  113. #else
  114. static inline void
  115. lzma_sha256_init(lzma_check_state *check)
  116. {
  117. LZMA_SHA256FUNC(Init)(&check->state.sha256);
  118. }
  119. static inline void
  120. lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
  121. {
  122. #if defined(HAVE_CC_SHA256_INIT) && SIZE_MAX > UINT32_MAX
  123. // Darwin's CC_SHA256_Update takes uint32_t as the buffer size,
  124. // so use a loop to support size_t.
  125. while (size > UINT32_MAX) {
  126. LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, UINT32_MAX);
  127. buf += UINT32_MAX;
  128. size -= UINT32_MAX;
  129. }
  130. #endif
  131. LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, size);
  132. }
  133. static inline void
  134. lzma_sha256_finish(lzma_check_state *check)
  135. {
  136. LZMA_SHA256FUNC(Final)(check->buffer.u8, &check->state.sha256);
  137. }
  138. #endif
  139. #endif