Rizin
unix-like reverse engineering framework and cli tools
check.h
Go to the documentation of this file.
1 //
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 //
12 
13 #ifndef LZMA_CHECK_H
14 #define LZMA_CHECK_H
15 
16 #include "common.h"
17 
18 // If the function for external SHA-256 is missing, use the internal SHA-256
19 // code. Due to how configure works, these defines can only get defined when
20 // both a usable header and a type have already been found.
21 #if !(defined(HAVE_CC_SHA256_INIT) \
22  || defined(HAVE_SHA256_INIT) \
23  || defined(HAVE_SHA256INIT))
24 # define HAVE_INTERNAL_SHA256 1
25 #endif
26 
27 #if defined(HAVE_INTERNAL_SHA256)
28 // Nothing
29 #elif defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
30 # include <CommonCrypto/CommonDigest.h>
31 #elif defined(HAVE_SHA256_H)
32 # include <sys/types.h>
33 # include <sha256.h>
34 #elif defined(HAVE_SHA2_H)
35 # include <sys/types.h>
36 # include <sha2.h>
37 #endif
38 
39 #if defined(HAVE_INTERNAL_SHA256)
41 typedef struct {
44 
48 #elif defined(HAVE_CC_SHA256_CTX)
49 typedef CC_SHA256_CTX lzma_sha256_state;
50 #elif defined(HAVE_SHA256_CTX)
51 typedef SHA256_CTX lzma_sha256_state;
52 #elif defined(HAVE_SHA2_CTX)
53 typedef SHA2_CTX lzma_sha256_state;
54 #endif
55 
56 #if defined(HAVE_INTERNAL_SHA256)
57 // Nothing
58 #elif defined(HAVE_CC_SHA256_INIT)
59 # define LZMA_SHA256FUNC(x) CC_SHA256_ ## x
60 #elif defined(HAVE_SHA256_INIT)
61 # define LZMA_SHA256FUNC(x) SHA256_ ## x
62 #elif defined(HAVE_SHA256INIT)
63 # define LZMA_SHA256FUNC(x) SHA256 ## x
64 #endif
65 
66 // Index hashing needs the best possible hash function (preferably
67 // a cryptographic hash) for maximum reliability.
68 #if defined(HAVE_CHECK_SHA256)
69 # define LZMA_CHECK_BEST LZMA_CHECK_SHA256
70 #elif defined(HAVE_CHECK_CRC64)
71 # define LZMA_CHECK_BEST LZMA_CHECK_CRC64
72 #else
73 # define LZMA_CHECK_BEST LZMA_CHECK_CRC32
74 #endif
75 
76 
81 typedef struct {
83  union {
84  uint8_t u8[64];
85  uint32_t u32[16];
86  uint64_t u64[8];
87  } buffer;
88 
90  union {
94  } state;
95 
97 
98 
101 #ifdef HAVE_SMALL
102 extern uint32_t lzma_crc32_table[1][256];
103 extern void lzma_crc32_init(void);
104 #else
105 extern const uint32_t lzma_crc32_table[8][256];
106 extern const uint64_t lzma_crc64_table[4][256];
107 #endif
108 
109 
116 
119  const uint8_t *buf, size_t size);
120 
123 
124 
125 #ifndef LZMA_SHA256FUNC
126 
129 
131 extern void lzma_sha256_update(
132  const uint8_t *buf, size_t size, lzma_check_state *check);
133 
136 
137 
138 #else
139 
140 static inline void
142 {
143  LZMA_SHA256FUNC(Init)(&check->state.sha256);
144 }
145 
146 
147 static inline void
149 {
150 #if defined(HAVE_CC_SHA256_INIT) && SIZE_MAX > UINT32_MAX
151  // Darwin's CC_SHA256_Update takes uint32_t as the buffer size,
152  // so use a loop to support size_t.
153  while (size > UINT32_MAX) {
154  LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, UINT32_MAX);
155  buf += UINT32_MAX;
156  size -= UINT32_MAX;
157  }
158 #endif
159 
160  LZMA_SHA256FUNC(Update)(&check->state.sha256, buf, size);
161 }
162 
163 
164 static inline void
166 {
167  LZMA_SHA256FUNC(Final)(check->buffer.u8, &check->state.sha256);
168 }
169 
170 #endif
171 
172 #endif
lzma_check
Type of the integrity check (Check ID)
Definition: check.h:27
size_t size
Definition: check.h:120
struct buffer buffer
void lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
Update the SHA-256 hash state.
Definition: sha256.c:142
const uint64_t lzma_crc64_table[4][256]
Definition: crc64_table_be.h:3
void lzma_sha256_init(lzma_check_state *check)
Prepare SHA-256 state for new input.
Definition: sha256.c:127
void lzma_check_finish(lzma_check_state *check, lzma_check type)
Finish the check calculation and store the result to check->buffer.u8.
Definition: check.c:148
const uint32_t lzma_crc32_table[8][256]
Definition: crc32_small.c:16
void lzma_sha256_finish(lzma_check_state *check)
Finish the SHA-256 calculation and store the result to check->buffer.u8.
Definition: sha256.c:169
void lzma_check_update(lzma_check_state *check, lzma_check type, const uint8_t *buf, size_t size)
Update the check state.
Definition: check.c:117
void lzma_check_init(lzma_check_state *check, lzma_check type)
Initialize *check depending on type.
Definition: check.c:84
lzma_check check
Definition: container.h:292
void lzma_crc32_init(void)
Definition: crc32_small.c:41
int Init(const char *driverPath)
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
int type
Definition: mipsasm.c:17
unsigned int uint32_t
Definition: sftypes.h:29
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31
#define UINT32_MAX
Structure to hold internal state of the check being calculated.
Definition: check.h:81
uint64_t crc64
Definition: check.h:92
uint32_t crc32
Definition: check.h:91
lzma_sha256_state sha256
Definition: check.h:93
State for the internal SHA-256 implementation.
Definition: check.h:41
uint64_t size
Size of the message excluding padding.
Definition: check.h:46
Definition: dis.h:43