Rizin
unix-like reverse engineering framework and cli tools
sha1.c File Reference
#include "sha1.h"
#include <rz_types.h>
#include <rz_endian.h>
#include <rz_util.h>

Go to the source code of this file.

Functions

void rz_sha1_init (RzSHA1 *context)
 
static ut32 rotate_left_32 (ut32 value, ut32 rot)
 
static void sha1_digest_block (RzSHA1 *context)
 
bool rz_sha1_update (RzSHA1 *context, const ut8 *data, ut64 length)
 
void sha1_padding (RzSHA1 *context)
 
void rz_sha1_fini (ut8 *hash, RzSHA1 *context)
 

Function Documentation

◆ rotate_left_32()

static ut32 rotate_left_32 ( ut32  value,
ut32  rot 
)
inlinestatic

Definition at line 22 of file sha1.c.

22  {
23  return ((((value) << (rot)) & 0xFFFFFFFF) | ((value) >> (32 - (rot))));
24 }
static int value
Definition: cmd_api.c:93

References value.

Referenced by sha1_digest_block().

◆ rz_sha1_fini()

void rz_sha1_fini ( ut8 hash,
RzSHA1 context 
)

Definition at line 137 of file sha1.c.

137  {
138  rz_return_if_fail(context && hash);
139 
141 
142  for (ut32 t = 0; t < 5; ++t) {
143  rz_write_at_be32(hash, context->digest[t], t * 4);
144  }
145 }
uint32_t ut32
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
static void rz_write_at_be32(void *dest, ut32 val, size_t offset)
Definition: rz_endian.h:103
void sha1_padding(RzSHA1 *context)
Definition: sha1.c:112

References rz_return_if_fail, rz_write_at_be32(), and sha1_padding().

Referenced by plugin_sha1_final(), and plugin_sha1_small_block().

◆ rz_sha1_init()

void rz_sha1_init ( RzSHA1 context)

Definition at line 9 of file sha1.c.

9  {
11 
12  context->digest[0] = 0x67452301;
13  context->digest[1] = 0xEFCDAB89;
14  context->digest[2] = 0x98BADCFE;
15  context->digest[3] = 0x10325476;
16  context->digest[4] = 0xC3D2E1F0;
17  context->index = 0;
18  context->len_high = 0;
19  context->len_low = 0;
20 }

References rz_return_if_fail.

Referenced by plugin_sha1_init(), and plugin_sha1_small_block().

◆ rz_sha1_update()

bool rz_sha1_update ( RzSHA1 context,
const ut8 data,
ut64  length 
)

Definition at line 88 of file sha1.c.

88  {
89  rz_return_val_if_fail(context && data, false);
90  for (ut64 i = 0; i < length; ++i) {
91  context->block[context->index++] = data[i];
92 
93  context->len_low += 8;
94  if (context->len_low > 0xFFFFFFFFull) {
95  context->len_low &= 0xFFFFFFFFull;
96  context->len_high++;
97  // check if digested data overflows UT64
98  if (context->len_high > 0xFFFFFFFFull) {
99  return false;
100  }
101  }
102 
103  // digest only 512 bit blocks
104  if (context->index == RZ_HASH_SHA1_BLOCK_LENGTH) {
106  }
107  }
108 
109  return true;
110 }
lzma_index ** i
Definition: index.h:629
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static void sha1_digest_block(RzSHA1 *context)
Definition: sha1.c:26
#define RZ_HASH_SHA1_BLOCK_LENGTH
Definition: sha1.h:10
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References i, length, RZ_HASH_SHA1_BLOCK_LENGTH, rz_return_val_if_fail, sha1_digest_block(), and ut64().

Referenced by plugin_sha1_small_block(), and plugin_sha1_update().

◆ sha1_digest_block()

static void sha1_digest_block ( RzSHA1 context)
static

Definition at line 26 of file sha1.c.

26  {
27  ut32 tmp;
28  ut32 W[80];
29  ut32 A = context->digest[0];
30  ut32 B = context->digest[1];
31  ut32 C = context->digest[2];
32  ut32 D = context->digest[3];
33  ut32 E = context->digest[4];
34 
35  for (ut32 t = 0; t < 16; ++t) {
36  W[t] = rz_read_at_be32(context->block, t * 4);
37  }
38 
39  for (ut32 t = 16; t < 80; ++t) {
40  W[t] = rotate_left_32(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
41  }
42 
43  for (ut32 t = 0; t < 20; ++t) {
44  tmp = rotate_left_32(A, 5) + ((B & C) | ((~B) & D)) + E + W[t] + 0x5A827999;
45  E = D;
46  D = C;
47  C = rotate_left_32(B, 30);
48  B = A;
49  A = tmp;
50  }
51 
52  for (ut32 t = 20; t < 40; ++t) {
53  tmp = rotate_left_32(A, 5) + (B ^ C ^ D) + E + W[t] + 0x6ED9EBA1;
54  E = D;
55  D = C;
56  C = rotate_left_32(B, 30);
57  B = A;
58  A = tmp;
59  }
60 
61  for (ut32 t = 40; t < 60; ++t) {
62  tmp = rotate_left_32(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[t] + 0x8F1BBCDC;
63  E = D;
64  D = C;
65  C = rotate_left_32(B, 30);
66  B = A;
67  A = tmp;
68  }
69 
70  for (ut32 t = 60; t < 80; ++t) {
71  tmp = rotate_left_32(A, 5) + (B ^ C ^ D) + E + W[t] + 0xCA62C1D6;
72  E = D;
73  D = C;
74  C = rotate_left_32(B, 30);
75  B = A;
76  A = tmp;
77  }
78 
79  context->digest[0] += A;
80  context->digest[1] += B;
81  context->digest[2] += C;
82  context->digest[3] += D;
83  context->digest[4] += E;
84 
85  context->index = 0;
86 }
#define A(x)
Definition: arc.h:165
#define B(x)
Definition: arc.h:166
#define C(x)
Definition: arc.h:167
#define W(x, y, z)
#define D
Definition: block.c:38
static ut32 rz_read_at_be32(const void *src, size_t offset)
Definition: rz_endian.h:93
static ut32 rotate_left_32(ut32 value, ut32 rot)
Definition: sha1.c:22
#define E
Definition: zip_err_str.c:12

References A, B, C, D, E, rotate_left_32(), rz_read_at_be32(), autogen_x86imm::tmp, and W.

Referenced by rz_sha1_update(), and sha1_padding().

◆ sha1_padding()

void sha1_padding ( RzSHA1 context)

Definition at line 112 of file sha1.c.

112  {
113  if (context->index > 55) {
114  context->block[context->index++] = 0x80;
115  for (; context->index < RZ_HASH_SHA1_BLOCK_LENGTH;) {
116  context->block[context->index++] = 0;
117  }
118 
120 
121  for (; context->index < 56;) {
122  context->block[context->index++] = 0;
123  }
124  } else {
125  context->block[context->index++] = 0x80;
126  for (; context->index < 56;) {
127  context->block[context->index++] = 0;
128  }
129  }
130 
131  rz_write_be32(&context->block[56], context->len_high);
132  rz_write_be32(&context->block[60], context->len_low);
133 
135 }
static void rz_write_be32(void *dest, ut32 val)
Definition: rz_endian.h:98

References RZ_HASH_SHA1_BLOCK_LENGTH, rz_write_be32(), and sha1_digest_block().

Referenced by rz_sha1_fini().