Rizin
unix-like reverse engineering framework and cli tools
check.h File Reference

Internal API to different integrity check functions. More...

#include "common.h"

Go to the source code of this file.

Classes

struct  lzma_sha256_state
 State for the internal SHA-256 implementation. More...
 
struct  lzma_check_state
 Structure to hold internal state of the check being calculated. More...
 

Macros

#define HAVE_INTERNAL_SHA256   1
 
#define LZMA_CHECK_BEST   LZMA_CHECK_CRC32
 

Functions

void lzma_check_init (lzma_check_state *check, lzma_check type)
 Initialize *check depending on type. More...
 
void lzma_check_update (lzma_check_state *check, lzma_check type, const uint8_t *buf, size_t size)
 Update the check state. More...
 
void lzma_check_finish (lzma_check_state *check, lzma_check type)
 Finish the check calculation and store the result to check->buffer.u8. More...
 
void lzma_sha256_init (lzma_check_state *check)
 Prepare SHA-256 state for new input. More...
 
void lzma_sha256_update (const uint8_t *buf, size_t size, lzma_check_state *check)
 Update the SHA-256 hash state. More...
 
void lzma_sha256_finish (lzma_check_state *check)
 Finish the SHA-256 calculation and store the result to check->buffer.u8. More...
 

Variables

const uint32_t lzma_crc32_table [8][256]
 
const uint64_t lzma_crc64_table [4][256]
 

Detailed Description

Internal API to different integrity check functions.

Definition in file check.h.

Macro Definition Documentation

◆ HAVE_INTERNAL_SHA256

#define HAVE_INTERNAL_SHA256   1

Definition at line 24 of file check.h.

◆ LZMA_CHECK_BEST

#define LZMA_CHECK_BEST   LZMA_CHECK_CRC32

Definition at line 73 of file check.h.

Function Documentation

◆ lzma_check_finish()

void lzma_check_finish ( lzma_check_state check,
lzma_check  type 
)

Finish the check calculation and store the result to check->buffer.u8.

Definition at line 148 of file check.c.

149 {
150  switch (type) {
151 #ifdef HAVE_CHECK_CRC32
152  case LZMA_CHECK_CRC32:
153  check->buffer.u32[0] = conv32le(check->state.crc32);
154  break;
155 #endif
156 
157 #ifdef HAVE_CHECK_CRC64
158  case LZMA_CHECK_CRC64:
159  check->buffer.u64[0] = conv64le(check->state.crc64);
160  break;
161 #endif
162 
163 #ifdef HAVE_CHECK_SHA256
164  case LZMA_CHECK_SHA256:
166  break;
167 #endif
168 
169  default:
170  break;
171  }
172 
173  return;
174 }
@ LZMA_CHECK_CRC32
Definition: check.h:35
@ LZMA_CHECK_CRC64
Definition: check.h:42
@ LZMA_CHECK_SHA256
Definition: check.h:49
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
lzma_check check
Definition: container.h:292
int type
Definition: mipsasm.c:17
#define conv64le(num)
#define conv32le(num)

References check, conv32le, conv64le, LZMA_CHECK_CRC32, LZMA_CHECK_CRC64, LZMA_CHECK_SHA256, lzma_sha256_finish(), and type.

Referenced by block_buffer_encode(), block_decode(), and block_encode().

◆ lzma_check_init()

void lzma_check_init ( lzma_check_state check,
lzma_check  type 
)

Initialize *check depending on type.

Returns
LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not supported by the current version or build of liblzma. LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.

Definition at line 84 of file check.c.

85 {
86  switch (type) {
87  case LZMA_CHECK_NONE:
88  break;
89 
90 #ifdef HAVE_CHECK_CRC32
91  case LZMA_CHECK_CRC32:
92  check->state.crc32 = 0;
93  break;
94 #endif
95 
96 #ifdef HAVE_CHECK_CRC64
97  case LZMA_CHECK_CRC64:
98  check->state.crc64 = 0;
99  break;
100 #endif
101 
102 #ifdef HAVE_CHECK_SHA256
103  case LZMA_CHECK_SHA256:
105  break;
106 #endif
107 
108  default:
109  break;
110  }
111 
112  return;
113 }
@ LZMA_CHECK_NONE
Definition: check.h:28
void lzma_sha256_init(lzma_check_state *check)
Prepare SHA-256 state for new input.
Definition: sha256.c:127

References check, LZMA_CHECK_CRC32, LZMA_CHECK_CRC64, LZMA_CHECK_NONE, LZMA_CHECK_SHA256, lzma_sha256_init(), and type.

Referenced by block_buffer_encode(), LZMA_API(), lzma_block_decoder_init(), and lzma_block_encoder_init().

◆ lzma_check_update()

void lzma_check_update ( lzma_check_state check,
lzma_check  type,
const uint8_t buf,
size_t  size 
)

Update the check state.

Definition at line 117 of file check.c.

119 {
120  switch (type) {
121 #ifdef HAVE_CHECK_CRC32
122  case LZMA_CHECK_CRC32:
123  check->state.crc32 = lzma_crc32(buf, size, check->state.crc32);
124  break;
125 #endif
126 
127 #ifdef HAVE_CHECK_CRC64
128  case LZMA_CHECK_CRC64:
129  check->state.crc64 = lzma_crc64(buf, size, check->state.crc64);
130  break;
131 #endif
132 
133 #ifdef HAVE_CHECK_SHA256
134  case LZMA_CHECK_SHA256:
136  break;
137 #endif
138 
139  default:
140  break;
141  }
142 
143  return;
144 }
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
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138

References check, LZMA_CHECK_CRC32, LZMA_CHECK_CRC64, LZMA_CHECK_SHA256, lzma_sha256_update(), and type.

Referenced by block_buffer_encode(), block_decode(), block_encode(), and hash_append().

◆ lzma_sha256_finish()

void lzma_sha256_finish ( lzma_check_state check)

Finish the SHA-256 calculation and store the result to check->buffer.u8.

Definition at line 169 of file sha256.c.

170 {
171  // Add padding as described in RFC 3174 (it describes SHA-1 but
172  // the same padding style is used for SHA-256 too).
173  size_t pos = check->state.sha256.size & 0x3F;
174  check->buffer.u8[pos++] = 0x80;
175 
176  while (pos != 64 - 8) {
177  if (pos == 64) {
178  process(check);
179  pos = 0;
180  }
181 
182  check->buffer.u8[pos++] = 0x00;
183  }
184 
185  // Convert the message size from bytes to bits.
186  check->state.sha256.size *= 8;
187 
188  check->buffer.u64[(64 - 8) / 8] = conv64be(check->state.sha256.size);
189 
190  process(check);
191 
192  for (size_t i = 0; i < 8; ++i)
193  check->buffer.u32[i] = conv32be(check->state.sha256.state[i]);
194 
195  return;
196 }
lzma_index ** i
Definition: index.h:629
static void process(lzma_check_state *check)
Definition: sha256.c:119
int pos
Definition: main.c:11
#define conv32be(num)
#define conv64be(num)

References check, conv32be, conv64be, i, pos, and process().

Referenced by lzma_check_finish().

◆ lzma_sha256_init()

void lzma_sha256_init ( lzma_check_state check)

Prepare SHA-256 state for new input.

Definition at line 127 of file sha256.c.

128 {
129  static const uint32_t s[8] = {
130  0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
131  0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
132  };
133 
134  memcpy(check->state.sha256.state, s, sizeof(s));
135  check->state.sha256.size = 0;
136 
137  return;
138 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static RzSocket * s
Definition: rtr.c:28
unsigned int uint32_t
Definition: sftypes.h:29

References check, memcpy(), and s.

Referenced by lzma_check_init().

◆ lzma_sha256_update()

void lzma_sha256_update ( const uint8_t buf,
size_t  size,
lzma_check_state check 
)

Update the SHA-256 hash state.

Definition at line 142 of file sha256.c.

143 {
144  // Copy the input data into a properly aligned temporary buffer.
145  // This way we can be called with arbitrarily sized buffers
146  // (no need to be multiple of 64 bytes), and the code works also
147  // on architectures that don't allow unaligned memory access.
148  while (size > 0) {
149  const size_t copy_start = check->state.sha256.size & 0x3F;
150  size_t copy_size = 64 - copy_start;
151  if (copy_size > size)
152  copy_size = size;
153 
154  memcpy(check->buffer.u8 + copy_start, buf, copy_size);
155 
156  buf += copy_size;
157  size -= copy_size;
158  check->state.sha256.size += copy_size;
159 
160  if ((check->state.sha256.size & 0x3F) == 0)
161  process(check);
162  }
163 
164  return;
165 }

References check, memcpy(), and process().

Referenced by lzma_check_update().

Variable Documentation

◆ lzma_crc32_table

const uint32_t lzma_crc32_table[8][256]
extern

lzma_crc32_table[0] is needed by LZ encoder so we need to keep the array two-dimensional.

Definition at line 16 of file crc32_small.c.

Referenced by LZMA_API().

◆ lzma_crc64_table

const uint64_t lzma_crc64_table[4][256]
extern

Definition at line 3 of file crc64_table_be.h.

Referenced by LZMA_API().