check.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. lzma_attr_visibility_hidden
  89. extern uint32_t lzma_crc32_table[1][256];
  90. extern void lzma_crc32_init(void);
  91. #else
  92. lzma_attr_visibility_hidden
  93. extern const uint32_t lzma_crc32_table[8][256];
  94. lzma_attr_visibility_hidden
  95. extern const uint64_t lzma_crc64_table[4][256];
  96. #endif
  97. /// \brief Initialize *check depending on type
  98. extern void lzma_check_init(lzma_check_state *check, lzma_check type);
  99. /// Update the check state
  100. extern void lzma_check_update(lzma_check_state *check, lzma_check type,
  101. const uint8_t *buf, size_t size);
  102. /// Finish the check calculation and store the result to check->buffer.u8.
  103. extern void lzma_check_finish(lzma_check_state *check, lzma_check type);
  104. #ifndef LZMA_SHA256FUNC
  105. /// Prepare SHA-256 state for new input.
  106. extern void lzma_sha256_init(lzma_check_state *check);
  107. /// Update the SHA-256 hash state
  108. extern void lzma_sha256_update(
  109. const uint8_t *buf, size_t size, lzma_check_state *check);
  110. /// Finish the SHA-256 calculation and store the result to check->buffer.u8.
  111. extern void lzma_sha256_finish(lzma_check_state *check);
  112. #else
  113. static inline void
  114. lzma_sha256_init(lzma_check_state *check)
  115. {
  116. LZMA_SHA256FUNC(Init)(&check->state.sha256);
  117. }
  118. static inline void
  119. lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
  120. {
  121. #if defined(HAVE_CC_SHA256_INIT) && SIZE_MAX > UINT32_MAX
  122. // Darwin's CC_SHA256_Update takes uint32_t as the buffer size,
  123. // so use a loop to support size_t.
  124. while (size > UINT32_MAX) {
  125. LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, UINT32_MAX);
  126. buf += UINT32_MAX;
  127. size -= UINT32_MAX;
  128. }
  129. #endif
  130. LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, size);
  131. }
  132. static inline void
  133. lzma_sha256_finish(lzma_check_state *check)
  134. {
  135. LZMA_SHA256FUNC(Final)(check->buffer.u8, &check->state.sha256);
  136. }
  137. #endif
  138. #endif