Rizin
unix-like reverse engineering framework and cli tools
sha256.c File Reference

SHA-256. More...

#include "check.h"

Go to the source code of this file.

Macros

#define blk0(i)   (W[i] = conv32be(data[i]))
 
#define blk2(i)
 
#define Ch(x, y, z)   (z ^ (x & (y ^ z)))
 
#define Maj(x, y, z)   ((x & (y ^ z)) + (y & z))
 
#define a(i)   T[(0 - i) & 7]
 
#define b(i)   T[(1 - i) & 7]
 
#define c(i)   T[(2 - i) & 7]
 
#define d(i)   T[(3 - i) & 7]
 
#define e(i)   T[(4 - i) & 7]
 
#define f(i)   T[(5 - i) & 7]
 
#define g(i)   T[(6 - i) & 7]
 
#define h(i)   T[(7 - i) & 7]
 
#define R(i, j, blk)
 
#define R0(i)   R(i, 0, blk0(i))
 
#define R2(i)   R(i, j, blk2(i))
 
#define S0(x)   rotr_32(x ^ rotr_32(x ^ rotr_32(x, 9), 11), 2)
 
#define S1(x)   rotr_32(x ^ rotr_32(x ^ rotr_32(x, 14), 5), 6)
 
#define s0(x)   (rotr_32(x ^ rotr_32(x, 11), 7) ^ (x >> 3))
 
#define s1(x)   (rotr_32(x ^ rotr_32(x, 2), 17) ^ (x >> 10))
 

Functions

static uint32_t rotr_32 (uint32_t num, unsigned amount)
 
static void transform (uint32_t state[8], const uint32_t data[16])
 
static void process (lzma_check_state *check)
 
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

static const uint32_t SHA256_K [64]
 

Detailed Description

SHA-256.

Todo:
Crypto++ has x86 ASM optimizations. They use SSE so if they are imported to liblzma, SSE instructions need to be used conditionally to keep the code working on older boxes.

Definition in file sha256.c.

Macro Definition Documentation

◆ a

#define a (   i)    T[(0 - i) & 7]

Definition at line 41 of file sha256.c.

◆ b

#define b (   i)    T[(1 - i) & 7]

Definition at line 42 of file sha256.c.

◆ blk0

#define blk0 (   i)    (W[i] = conv32be(data[i]))

Definition at line 34 of file sha256.c.

◆ blk2

#define blk2 (   i)
Value:
(W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \
+ s0(W[(i - 15) & 15]))
lzma_index ** i
Definition: index.h:629
#define W(x, y, z)
#define s0(x)
Definition: sha256.c:59
#define s1(x)
Definition: sha256.c:60

Definition at line 35 of file sha256.c.

◆ c

#define c (   i)    T[(2 - i) & 7]

Definition at line 43 of file sha256.c.

◆ Ch

#define Ch (   x,
  y,
 
)    (z ^ (x & (y ^ z)))

Definition at line 38 of file sha256.c.

◆ d

#define d (   i)    T[(3 - i) & 7]

Definition at line 44 of file sha256.c.

◆ e

#define e (   i)    T[(4 - i) & 7]

Definition at line 45 of file sha256.c.

◆ f

#define f (   i)    T[(5 - i) & 7]

Definition at line 46 of file sha256.c.

◆ g

#define g (   i)    T[(6 - i) & 7]

Definition at line 47 of file sha256.c.

◆ h

#define h (   i)    T[(7 - i) & 7]

Definition at line 48 of file sha256.c.

◆ Maj

#define Maj (   x,
  y,
 
)    ((x & (y ^ z)) + (y & z))

Definition at line 39 of file sha256.c.

◆ R

#define R (   i,
  j,
  blk 
)
Value:
h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + SHA256_K[i + j] + blk; \
d(i) += h(i); \
h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
#define b(i)
Definition: sha256.c:42
#define f(i)
Definition: sha256.c:46
#define Maj(x, y, z)
Definition: sha256.c:39
#define S0(x)
Definition: sha256.c:57
#define S1(x)
Definition: sha256.c:58
#define c(i)
Definition: sha256.c:43
#define g(i)
Definition: sha256.c:47
#define a(i)
Definition: sha256.c:41
#define Ch(x, y, z)
Definition: sha256.c:38
static const uint32_t SHA256_K[64]
Definition: sha256.c:63
#define h(i)
Definition: sha256.c:48
#define e(i)
Definition: sha256.c:45

Definition at line 50 of file sha256.c.

◆ R0

#define R0 (   i)    R(i, 0, blk0(i))

Definition at line 54 of file sha256.c.

◆ R2

#define R2 (   i)    R(i, j, blk2(i))

Definition at line 55 of file sha256.c.

◆ S0

#define S0 (   x)    rotr_32(x ^ rotr_32(x ^ rotr_32(x, 9), 11), 2)

Definition at line 57 of file sha256.c.

◆ s0

#define s0 (   x)    (rotr_32(x ^ rotr_32(x, 11), 7) ^ (x >> 3))

Definition at line 59 of file sha256.c.

◆ S1

#define S1 (   x)    rotr_32(x ^ rotr_32(x ^ rotr_32(x, 14), 5), 6)

Definition at line 58 of file sha256.c.

◆ s1

#define s1 (   x)    (rotr_32(x ^ rotr_32(x, 2), 17) ^ (x >> 10))

Definition at line 60 of file sha256.c.

Function Documentation

◆ 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_check check
Definition: container.h:292
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 }
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138

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

Referenced by lzma_check_update().

◆ process()

static void process ( lzma_check_state check)
static

Definition at line 119 of file sha256.c.

120 {
121  transform(check->state.sha256.state, check->buffer.u32);
122  return;
123 }
static void transform(uint32_t state[8], const uint32_t data[16])
Definition: sha256.c:84

References check, and transform().

Referenced by lzma_sha256_finish(), and lzma_sha256_update().

◆ rotr_32()

static uint32_t rotr_32 ( uint32_t  num,
unsigned  amount 
)
inlinestatic

Definition at line 29 of file sha256.c.

30 {
31  return (num >> amount) | (num << (32 - amount));
32 }
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126

References num.

◆ transform()

static void transform ( uint32_t  state[8],
const uint32_t  data[16] 
)
static

Definition at line 84 of file sha256.c.

85 {
86  uint32_t W[16];
87  uint32_t T[8];
88 
89  // Copy state[] to working vars.
90  memcpy(T, state, sizeof(T));
91 
92  // The first 16 operations unrolled
93  R0( 0); R0( 1); R0( 2); R0( 3);
94  R0( 4); R0( 5); R0( 6); R0( 7);
95  R0( 8); R0( 9); R0(10); R0(11);
96  R0(12); R0(13); R0(14); R0(15);
97 
98  // The remaining 48 operations partially unrolled
99  for (unsigned int j = 16; j < 64; j += 16) {
100  R2( 0); R2( 1); R2( 2); R2( 3);
101  R2( 4); R2( 5); R2( 6); R2( 7);
102  R2( 8); R2( 9); R2(10); R2(11);
103  R2(12); R2(13); R2(14); R2(15);
104  }
105 
106  // Add the working vars back into state[].
107  state[0] += a(0);
108  state[1] += b(0);
109  state[2] += c(0);
110  state[3] += d(0);
111  state[4] += e(0);
112  state[5] += f(0);
113  state[6] += g(0);
114  state[7] += h(0);
115 }
#define T(op)
#define d(i)
Definition: sha256.c:44
#define R0(i)
Definition: sha256.c:54
#define R2(i)
Definition: sha256.c:55
Definition: dis.h:43

References a, b, c, d, e, f, g, h, memcpy(), R0, R2, T, and W.

Referenced by process().

Variable Documentation

◆ SHA256_K

const uint32_t SHA256_K[64]
static
Initial value:
= {
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
}

Definition at line 63 of file sha256.c.