Rizin
unix-like reverse engineering framework and cli tools
md4.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 "md4.h"
5 #include <rz_types.h>
6 #include <rz_endian.h>
7 #include <rz_util.h>
8 
9 #define XAX(x, y, z) (z ^ (x & (y ^ z)))
10 #define AAA(x, y, z) ((x & y) | (x & z) | (y & z))
11 #define XXX(x, y, z) (x ^ y ^ z)
12 
13 #define md4_round(m, w, x, y, z, v, s) \
14  w += m(x, y, z) + v; \
15  w = rotate_left_32(w, s)
16 
19 
20  context->digest[0] = 0x67452301;
21  context->digest[1] = 0xEFCDAB89;
22  context->digest[2] = 0x98BADCFE;
23  context->digest[3] = 0x10325476;
24  context->index = 0;
25  context->len_high = 0;
26  context->len_low = 0;
27 }
28 
29 static inline ut32 rotate_left_32(ut32 value, ut32 rot) {
30  return ((((value) << (rot)) & 0xFFFFFFFF) | ((value) >> (32 - (rot))));
31 }
32 
34  // printb(context->block, 64);
35 
36  ut32 W[16];
37  ut32 A = context->digest[0];
38  ut32 B = context->digest[1];
39  ut32 C = context->digest[2];
40  ut32 D = context->digest[3];
41 
42  for (ut32 t = 0; t < 16; ++t) {
43  W[t] = rz_read_at_le32(context->block, t * 4);
44  }
45 
46  md4_round(XAX, A, B, C, D, W[/* */ 0], 3);
47  md4_round(XAX, D, A, B, C, W[/* */ 1], 7);
48  md4_round(XAX, C, D, A, B, W[/* */ 2], 11);
49  md4_round(XAX, B, C, D, A, W[/* */ 3], 19);
50  md4_round(XAX, A, B, C, D, W[/* */ 4], 3);
51  md4_round(XAX, D, A, B, C, W[/* */ 5], 7);
52  md4_round(XAX, C, D, A, B, W[/* */ 6], 11);
53  md4_round(XAX, B, C, D, A, W[/* */ 7], 19);
54  md4_round(XAX, A, B, C, D, W[/* */ 8], 3);
55  md4_round(XAX, D, A, B, C, W[/* */ 9], 7);
56  md4_round(XAX, C, D, A, B, W[ 10], 11);
57  md4_round(XAX, B, C, D, A, W[ 11], 19);
58  md4_round(XAX, A, B, C, D, W[ 12], 3);
59  md4_round(XAX, D, A, B, C, W[ 13], 7);
60  md4_round(XAX, C, D, A, B, W[ 14], 11);
61  md4_round(XAX, B, C, D, A, W[ 15], 19);
62 
63  md4_round(AAA, A, B, C, D, W[/* */ 0] + 0x5A827999, 3);
64  md4_round(AAA, D, A, B, C, W[/* */ 4] + 0x5A827999, 5);
65  md4_round(AAA, C, D, A, B, W[/* */ 8] + 0x5A827999, 9);
66  md4_round(AAA, B, C, D, A, W[ 12] + 0x5A827999, 13);
67  md4_round(AAA, A, B, C, D, W[/* */ 1] + 0x5A827999, 3);
68  md4_round(AAA, D, A, B, C, W[/* */ 5] + 0x5A827999, 5);
69  md4_round(AAA, C, D, A, B, W[/* */ 9] + 0x5A827999, 9);
70  md4_round(AAA, B, C, D, A, W[ 13] + 0x5A827999, 13);
71  md4_round(AAA, A, B, C, D, W[/* */ 2] + 0x5A827999, 3);
72  md4_round(AAA, D, A, B, C, W[/* */ 6] + 0x5A827999, 5);
73  md4_round(AAA, C, D, A, B, W[ 10] + 0x5A827999, 9);
74  md4_round(AAA, B, C, D, A, W[ 14] + 0x5A827999, 13);
75  md4_round(AAA, A, B, C, D, W[/* */ 3] + 0x5A827999, 3);
76  md4_round(AAA, D, A, B, C, W[/* */ 7] + 0x5A827999, 5);
77  md4_round(AAA, C, D, A, B, W[ 11] + 0x5A827999, 9);
78  md4_round(AAA, B, C, D, A, W[ 15] + 0x5A827999, 13);
79 
80  md4_round(XXX, A, B, C, D, W[/* */ 0] + 0x6ED9EBA1, 3);
81  md4_round(XXX, D, A, B, C, W[/* */ 8] + 0x6ED9EBA1, 9);
82  md4_round(XXX, C, D, A, B, W[/* */ 4] + 0x6ED9EBA1, 11);
83  md4_round(XXX, B, C, D, A, W[ 12] + 0x6ED9EBA1, 15);
84  md4_round(XXX, A, B, C, D, W[/* */ 2] + 0x6ED9EBA1, 3);
85  md4_round(XXX, D, A, B, C, W[ 10] + 0x6ED9EBA1, 9);
86  md4_round(XXX, C, D, A, B, W[/* */ 6] + 0x6ED9EBA1, 11);
87  md4_round(XXX, B, C, D, A, W[ 14] + 0x6ED9EBA1, 15);
88  md4_round(XXX, A, B, C, D, W[/* */ 1] + 0x6ED9EBA1, 3);
89  md4_round(XXX, D, A, B, C, W[/* */ 9] + 0x6ED9EBA1, 9);
90  md4_round(XXX, C, D, A, B, W[/* */ 5] + 0x6ED9EBA1, 11);
91  md4_round(XXX, B, C, D, A, W[ 13] + 0x6ED9EBA1, 15);
92  md4_round(XXX, A, B, C, D, W[/* */ 3] + 0x6ED9EBA1, 3);
93  md4_round(XXX, D, A, B, C, W[ 11] + 0x6ED9EBA1, 9);
94  md4_round(XXX, C, D, A, B, W[/* */ 7] + 0x6ED9EBA1, 11);
95  md4_round(XXX, B, C, D, A, W[ 15] + 0x6ED9EBA1, 15);
96 
97  context->digest[0] += A;
98  context->digest[1] += B;
99  context->digest[2] += C;
100  context->digest[3] += D;
101 
102  context->index = 0;
103 }
104 
105 bool rz_md4_update(RzMD4 *context, const ut8 *data, ut64 length) {
106  rz_return_val_if_fail(context && data, false);
107  for (ut64 i = 0; i < length; ++i) {
108  context->block[context->index++] = data[i];
109 
110  context->len_low += 8;
111  if (context->len_low > 0xFFFFFFFFull) {
112  context->len_low &= 0xFFFFFFFFull;
113  context->len_high++;
114  // check if digested data overflows UT64
115  if (context->len_high > 0xFFFFFFFFull) {
116  return false;
117  }
118  }
119 
120  // digest only 512 bit blocks
121  if (context->index == RZ_HASH_MD4_BLOCK_LENGTH) {
123  }
124  }
125 
126  return true;
127 }
128 
130  if (context->index > 55) {
131  context->block[context->index++] = 0x80;
132  for (; context->index < RZ_HASH_MD4_BLOCK_LENGTH;) {
133  context->block[context->index++] = 0;
134  }
135 
137 
138  for (; context->index < 56;) {
139  context->block[context->index++] = 0;
140  }
141  } else {
142  context->block[context->index++] = 0x80;
143  for (; context->index < 56;) {
144  context->block[context->index++] = 0;
145  }
146  }
147 
148  rz_write_le32(&context->block[56], context->len_low);
149  rz_write_le32(&context->block[60], context->len_high);
150 
152 }
153 
154 void rz_md4_fini(ut8 *hash, RzMD4 *context) {
155  rz_return_if_fail(context && hash);
156 
158 
159  for (ut32 t = 0; t < 4; ++t) {
160  rz_write_at_le32(hash, context->digest[t], t * 4);
161  }
162 }
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
void md4_padding(RzMD4 *context)
Definition: md4.c:129
#define md4_round(m, w, x, y, z, v, s)
Definition: md4.c:13
static ut32 rotate_left_32(ut32 value, ut32 rot)
Definition: md4.c:29
bool rz_md4_update(RzMD4 *context, const ut8 *data, ut64 length)
Definition: md4.c:105
void rz_md4_init(RzMD4 *context)
Definition: md4.c:17
#define XXX(x, y, z)
Definition: md4.c:11
#define XAX(x, y, z)
Definition: md4.c:9
#define AAA(x, y, z)
Definition: md4.c:10
void rz_md4_fini(ut8 *hash, RzMD4 *context)
Definition: md4.c:154
static void md4_digest_block(RzMD4 *context)
Definition: md4.c:33
#define RZ_HASH_MD4_BLOCK_LENGTH
Definition: md4.h:10
#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_le32(void *dest, ut32 val)
Definition: rz_endian.h:256
static ut32 rz_read_at_le32(const void *src, size_t offset)
Definition: rz_endian.h:248
static void rz_write_at_le32(void *dest, ut32 val, size_t offset)
Definition: rz_endian.h:261
Definition: md4.h:11
ut64(WINAPI *w32_GetEnabledXStateFeatures)()