check.h 4.4 KB

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