Rizin
unix-like reverse engineering framework and cli tools
sha1.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "sha1.h"
5 #include <rz_types.h>
6 #include <rz_endian.h>
7 #include <rz_util.h>
8 
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 }
21 
22 static inline ut32 rotate_left_32(ut32 value, ut32 rot) {
23  return ((((value) << (rot)) & 0xFFFFFFFF) | ((value) >> (32 - (rot))));
24 }
25 
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 }
87 
88 bool rz_sha1_update(RzSHA1 *context, const ut8 *data, ut64 length) {
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 }
111 
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 }
136 
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 }
lzma_index ** i
Definition: index.h:629
#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 int value
Definition: cmd_api.c:93
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
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static void rz_write_at_be32(void *dest, ut32 val, size_t offset)
Definition: rz_endian.h:103
static ut32 rz_read_at_be32(const void *src, size_t offset)
Definition: rz_endian.h:93
static void rz_write_be32(void *dest, ut32 val)
Definition: rz_endian.h:98
void sha1_padding(RzSHA1 *context)
Definition: sha1.c:112
void rz_sha1_init(RzSHA1 *context)
Definition: sha1.c:9
static void sha1_digest_block(RzSHA1 *context)
Definition: sha1.c:26
void rz_sha1_fini(ut8 *hash, RzSHA1 *context)
Definition: sha1.c:137
static ut32 rotate_left_32(ut32 value, ut32 rot)
Definition: sha1.c:22
bool rz_sha1_update(RzSHA1 *context, const ut8 *data, ut64 length)
Definition: sha1.c:88
#define RZ_HASH_SHA1_BLOCK_LENGTH
Definition: sha1.h:10
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
#define E
Definition: zip_err_str.c:12