30#define F(X,Y,Z) (((X)&(Y)) | ((~(X))&(Z)))
31#define G(X,Y,Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
32#define H(X,Y,Z) ((X)^(Y)^(Z))
33#define lshift(x,s) (((x)<<(s)) | ((x)>>(32-(s))))
35#define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
36#define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + 0x5A827999U, s)
37#define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1U, s)
40static unsigned char PADDING[64] = {
41 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
46static void rs_mdfour_block(
rs_mdfour_t *md,
void const *p);
60static void rs_mdfour64(
rs_mdfour_t *m,
const void *p)
62 uint32_t AA, BB, CC, DD;
64 const uint32_t *X = (
const uint32_t *)p;
75 ROUND1(A, B, C, D, 0, 3);
76 ROUND1(D, A, B, C, 1, 7);
77 ROUND1(C, D, A, B, 2, 11);
78 ROUND1(B, C, D, A, 3, 19);
79 ROUND1(A, B, C, D, 4, 3);
80 ROUND1(D, A, B, C, 5, 7);
81 ROUND1(C, D, A, B, 6, 11);
82 ROUND1(B, C, D, A, 7, 19);
83 ROUND1(A, B, C, D, 8, 3);
84 ROUND1(D, A, B, C, 9, 7);
85 ROUND1(C, D, A, B, 10, 11);
86 ROUND1(B, C, D, A, 11, 19);
87 ROUND1(A, B, C, D, 12, 3);
88 ROUND1(D, A, B, C, 13, 7);
89 ROUND1(C, D, A, B, 14, 11);
90 ROUND1(B, C, D, A, 15, 19);
92 ROUND2(A, B, C, D, 0, 3);
93 ROUND2(D, A, B, C, 4, 5);
94 ROUND2(C, D, A, B, 8, 9);
95 ROUND2(B, C, D, A, 12, 13);
96 ROUND2(A, B, C, D, 1, 3);
97 ROUND2(D, A, B, C, 5, 5);
98 ROUND2(C, D, A, B, 9, 9);
99 ROUND2(B, C, D, A, 13, 13);
100 ROUND2(A, B, C, D, 2, 3);
101 ROUND2(D, A, B, C, 6, 5);
102 ROUND2(C, D, A, B, 10, 9);
103 ROUND2(B, C, D, A, 14, 13);
104 ROUND2(A, B, C, D, 3, 3);
105 ROUND2(D, A, B, C, 7, 5);
106 ROUND2(C, D, A, B, 11, 9);
107 ROUND2(B, C, D, A, 15, 13);
109 ROUND3(A, B, C, D, 0, 3);
110 ROUND3(D, A, B, C, 8, 9);
111 ROUND3(C, D, A, B, 4, 11);
112 ROUND3(B, C, D, A, 12, 15);
113 ROUND3(A, B, C, D, 2, 3);
114 ROUND3(D, A, B, C, 10, 9);
115 ROUND3(C, D, A, B, 6, 11);
116 ROUND3(B, C, D, A, 14, 15);
117 ROUND3(A, B, C, D, 1, 3);
118 ROUND3(D, A, B, C, 9, 9);
119 ROUND3(C, D, A, B, 5, 11);
120 ROUND3(B, C, D, A, 13, 15);
121 ROUND3(A, B, C, D, 3, 3);
122 ROUND3(D, A, B, C, 11, 9);
123 ROUND3(C, D, A, B, 7, 11);
124 ROUND3(B, C, D, A, 15, 15);
144inline static void copy4(
unsigned char *out, uint32_t
const x)
146 out[0] = (
unsigned char)(x);
147 out[1] = (
unsigned char)(x >> 8);
148 out[2] = (
unsigned char)(x >> 16);
149 out[3] = (
unsigned char)(x >> 24);
155inline static void copy8(
unsigned char *out, uint64_t
const x)
157 out[0] = (
unsigned char)(x);
158 out[1] = (
unsigned char)(x >> 8);
159 out[2] = (
unsigned char)(x >> 16);
160 out[3] = (
unsigned char)(x >> 24);
161 out[4] = (
unsigned char)(x >> 32);
162 out[5] = (
unsigned char)(x >> 40);
163 out[6] = (
unsigned char)(x >> 48);
164 out[7] = (
unsigned char)(x >> 56);
169#ifdef WORDS_BIGENDIAN
170inline static void copy64( uint32_t *M,
unsigned char const *in)
176 (((uint32_t)in[3] << 24) | ((uint32_t)in[2] << 16) |
177 ((uint32_t)in[1] << 8) | (uint32_t)in[0]);
184inline static void rs_mdfour_block(
rs_mdfour_t *md,
void const *p)
197inline static void rs_mdfour_block(
rs_mdfour_t *md,
void const *p)
209inline static void rs_mdfour_block(
rs_mdfour_t *md,
void const *p)
211 if ((uintptr_t)p & 3) {
214 memcpy(M, p, 16 *
sizeof(uint32_t));
217 rs_mdfour64(md, (
const uint32_t *)p);
226 memset(md, 0,
sizeof(*md));
234 md->totalN_hi = md->totalN_lo = 0;
252 unsigned char buf[8];
260 b[0] = m->totalN_lo << 3;
261 b[1] = ((m->totalN_hi << 3) | (m->totalN_lo >> 29));
263 copy4(buf + 4, b[1]);
267 pad_len = (m->tail_len < 56) ? (56 - m->tail_len) : (120 - m->tail_len);
275 unsigned char const *in = (
unsigned char const *)in_void;
281 if ((md->totalN_lo += n) < n)
288 size_t tail_gap = 64 - md->tail_len;
290 memcpy(&md->tail[md->tail_len], in, tail_gap);
291 rs_mdfour_block(md, md->tail);
299 rs_mdfour_block(md, in);
305 memcpy(&md->tail[md->tail_len], in, n);
306 md->tail_len += (int)n;
310void rs_mdfour_result(
rs_mdfour_t *md,
unsigned char *out)
315 copy4(out + 4, md->B);
316 copy4(out + 8, md->C);
317 copy4(out + 12, md->D);
320void rs_mdfour(
unsigned char *out,
void const *in,
size_t n)
324 rs_mdfour_begin(&md);
326 rs_mdfour_result(&md, out);
Public header for librsync.
LIBRSYNC_EXPORT void rs_mdfour_update(rs_mdfour_t *md, void const *in_void, size_t n)
Feed some data into the MD4 accumulator.
MD4 message digest algorithm.
The rs_mdfour state type.