FreeRDP
Loading...
Searching...
No Matches
md4.c
1/*
2 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
3 * MD4 Message-Digest Algorithm (RFC 1320).
4 *
5 * Homepage:
6 * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
7 *
8 * Author:
9 * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
10 *
11 * This software was written by Alexander Peslyak in 2001. No copyright is
12 * claimed, and the software is hereby placed in the public domain.
13 * In case this attempt to disclaim copyright and place the software in the
14 * public domain is deemed null and void, then the software is
15 * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
16 * general public under the following terms:
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted.
20 *
21 * There's ABSOLUTELY NO WARRANTY, express or implied.
22 *
23 * (This is a heavily cut-down "BSD license".)
24 *
25 * This differs from Colin Plumb's older public domain implementation in that
26 * no exactly 32-bit integer data type is required (any 32-bit or wider
27 * unsigned integer data type will do), there's no compile-time endianness
28 * configuration, and the function prototypes match OpenSSL's. No code from
29 * Colin Plumb's implementation has been reused; this comment merely compares
30 * the properties of the two independent implementations.
31 *
32 * The primary goals of this implementation are portability and ease of use.
33 * It is meant to be fast, but not as fast as possible. Some known
34 * optimizations are not included to reduce source code size and avoid
35 * compile-time configuration.
36 */
37
38#include <string.h>
39
40#include <winpr/cast.h>
41
42#include "md4.h"
43
44/*
45 * The basic MD4 functions.
46 *
47 * F and G are optimized compared to their RFC 1320 definitions, with the
48 * optimization for F borrowed from Colin Plumb's MD5 implementation.
49 */
50static inline winpr_MD4_u32plus F(winpr_MD4_u32plus x, winpr_MD4_u32plus y, winpr_MD4_u32plus z)
51{
52 return ((z) ^ ((x) & ((y) ^ (z))));
53}
54static inline winpr_MD4_u32plus G(winpr_MD4_u32plus x, winpr_MD4_u32plus y, winpr_MD4_u32plus z)
55{
56 return (((x) & ((y) | (z))) | ((y) & (z)));
57}
58static inline winpr_MD4_u32plus H(winpr_MD4_u32plus x, winpr_MD4_u32plus y, winpr_MD4_u32plus z)
59{
60 return ((x) ^ (y) ^ (z));
61}
62
63/*
64 * The MD4 transformation for all three rounds.
65 */
66#define STEP(f, a, b, c, d, x, s) \
67 (a) += f((b), (c), (d)) + (x); \
68 (a) = (((a) << (s)) | (((a)&0xffffffff) >> (32 - (s))));
69
70/*
71 * SET reads 4 input bytes in little-endian byte order and stores them in a
72 * properly aligned word in host byte order.
73 *
74 * The check for little-endian architectures that tolerate unaligned memory
75 * accesses is just an optimization. Nothing will break if it fails to detect
76 * a suitable architecture.
77 *
78 * Unfortunately, this optimization may be a C strict aliasing rules violation
79 * if the caller's data buffer has effective type that cannot be aliased by
80 * winpr_MD4_u32plus. In practice, this problem may occur if these MD4 routines are
81 * inlined into a calling function, or with future and dangerously advanced
82 * link-time optimizations. For the time being, keeping these MD4 routines in
83 * their own translation unit avoids the problem.
84 */
85#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
86#define SET(n) (*(WINPR_PACKED_ALIGN_CAST(const winpr_MD4_u32plus*, &ptr[4ULL * (n)])))
87#define GET(n) SET(n)
88#else
89#define SET(n) \
90 (ctx->block[(n)] = (winpr_MD4_u32plus)ptr[4ULL * (n)] | \
91 ((winpr_MD4_u32plus)ptr[4ULL * (n) + 1] << 8) | \
92 ((winpr_MD4_u32plus)ptr[4ULL * (n) + 2] << 16) | \
93 ((winpr_MD4_u32plus)ptr[4ULL * (n) + 3] << 24))
94#define GET(n) (ctx->block[(n)])
95#endif
96
97/*
98 * This processes one or more 64-byte data blocks, but does NOT update the bit
99 * counters. There are no alignment requirements.
100 */
101static const void* body(WINPR_MD4_CTX* ctx, const void* data, size_t size)
102{
103 const winpr_MD4_u32plus ac1 = 0x5a827999;
104 const winpr_MD4_u32plus ac2 = 0x6ed9eba1;
105
106 const unsigned char* ptr = (const unsigned char*)data;
107
108 winpr_MD4_u32plus a = ctx->a;
109 winpr_MD4_u32plus b = ctx->b;
110 winpr_MD4_u32plus c = ctx->c;
111 winpr_MD4_u32plus d = ctx->d;
112
113 do
114 {
115 const winpr_MD4_u32plus saved_a = a;
116 const winpr_MD4_u32plus saved_b = b;
117 const winpr_MD4_u32plus saved_c = c;
118 const winpr_MD4_u32plus saved_d = d;
119
120 /* Round 1 */
121 STEP(F, a, b, c, d, SET(0), 3)
122 STEP(F, d, a, b, c, SET(1), 7)
123 STEP(F, c, d, a, b, SET(2), 11)
124 STEP(F, b, c, d, a, SET(3), 19)
125 STEP(F, a, b, c, d, SET(4), 3)
126 STEP(F, d, a, b, c, SET(5), 7)
127 STEP(F, c, d, a, b, SET(6), 11)
128 STEP(F, b, c, d, a, SET(7), 19)
129 STEP(F, a, b, c, d, SET(8), 3)
130 STEP(F, d, a, b, c, SET(9), 7)
131 STEP(F, c, d, a, b, SET(10), 11)
132 STEP(F, b, c, d, a, SET(11), 19)
133 STEP(F, a, b, c, d, SET(12), 3)
134 STEP(F, d, a, b, c, SET(13), 7)
135 STEP(F, c, d, a, b, SET(14), 11)
136 STEP(F, b, c, d, a, SET(15), 19)
137
138 /* Round 2 */
139 STEP(G, a, b, c, d, GET(0) + ac1, 3)
140 STEP(G, d, a, b, c, GET(4) + ac1, 5)
141 STEP(G, c, d, a, b, GET(8) + ac1, 9)
142 STEP(G, b, c, d, a, GET(12) + ac1, 13)
143 STEP(G, a, b, c, d, GET(1) + ac1, 3)
144 STEP(G, d, a, b, c, GET(5) + ac1, 5)
145 STEP(G, c, d, a, b, GET(9) + ac1, 9)
146 STEP(G, b, c, d, a, GET(13) + ac1, 13)
147 STEP(G, a, b, c, d, GET(2) + ac1, 3)
148 STEP(G, d, a, b, c, GET(6) + ac1, 5)
149 STEP(G, c, d, a, b, GET(10) + ac1, 9)
150 STEP(G, b, c, d, a, GET(14) + ac1, 13)
151 STEP(G, a, b, c, d, GET(3) + ac1, 3)
152 STEP(G, d, a, b, c, GET(7) + ac1, 5)
153 STEP(G, c, d, a, b, GET(11) + ac1, 9)
154 STEP(G, b, c, d, a, GET(15) + ac1, 13)
155
156 /* Round 3 */
157 STEP(H, a, b, c, d, GET(0) + ac2, 3)
158 STEP(H, d, a, b, c, GET(8) + ac2, 9)
159 STEP(H, c, d, a, b, GET(4) + ac2, 11)
160 STEP(H, b, c, d, a, GET(12) + ac2, 15)
161 STEP(H, a, b, c, d, GET(2) + ac2, 3)
162 STEP(H, d, a, b, c, GET(10) + ac2, 9)
163 STEP(H, c, d, a, b, GET(6) + ac2, 11)
164 STEP(H, b, c, d, a, GET(14) + ac2, 15)
165 STEP(H, a, b, c, d, GET(1) + ac2, 3)
166 STEP(H, d, a, b, c, GET(9) + ac2, 9)
167 STEP(H, c, d, a, b, GET(5) + ac2, 11)
168 STEP(H, b, c, d, a, GET(13) + ac2, 15)
169 STEP(H, a, b, c, d, GET(3) + ac2, 3)
170 STEP(H, d, a, b, c, GET(11) + ac2, 9)
171 STEP(H, c, d, a, b, GET(7) + ac2, 11)
172 STEP(H, b, c, d, a, GET(15) + ac2, 15)
173
174 a += saved_a;
175 b += saved_b;
176 c += saved_c;
177 d += saved_d;
178
179 ptr += 64;
180 } while (size -= 64);
181
182 ctx->a = a;
183 ctx->b = b;
184 ctx->c = c;
185 ctx->d = d;
186
187 return ptr;
188}
189
190void winpr_MD4_Init(WINPR_MD4_CTX* ctx)
191{
192 ctx->a = 0x67452301;
193 ctx->b = 0xefcdab89;
194 ctx->c = 0x98badcfe;
195 ctx->d = 0x10325476;
196
197 ctx->lo = 0;
198 ctx->hi = 0;
199}
200
201void winpr_MD4_Update(WINPR_MD4_CTX* ctx, const void* data, size_t size)
202{
203 winpr_MD4_u32plus saved_lo = ctx->lo;
204 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
205 ctx->hi++;
206 ctx->hi += (winpr_MD4_u32plus)((size >> 29) & 0xffffffff);
207
208 size_t used = saved_lo & 0x3f;
209
210 if (used)
211 {
212 size_t available = 64 - used;
213
214 if (size < available)
215 {
216 memcpy(&ctx->buffer[used], data, size);
217 return;
218 }
219
220 memcpy(&ctx->buffer[used], data, available);
221 data = (const unsigned char*)data + available;
222 size -= available;
223 body(ctx, ctx->buffer, 64);
224 }
225
226 if (size >= 64)
227 {
228 data = body(ctx, data, size & ~(size_t)0x3f);
229 size &= 0x3f;
230 }
231
232 memcpy(ctx->buffer, data, size);
233}
234
235static inline void mdOUT(unsigned char* dst, winpr_MD4_u32plus src)
236{
237 (dst)[0] = (unsigned char)(src);
238 (dst)[1] = (unsigned char)((src) >> 8);
239 (dst)[2] = (unsigned char)((src) >> 16);
240 (dst)[3] = (unsigned char)((src) >> 24);
241}
242
243void winpr_MD4_Final(unsigned char* result, WINPR_MD4_CTX* ctx)
244{
245 size_t used = ctx->lo & 0x3f;
246
247 ctx->buffer[used++] = 0x80;
248
249 size_t available = 64 - used;
250
251 if (available < 8)
252 {
253 memset(&ctx->buffer[used], 0, available);
254 body(ctx, ctx->buffer, 64);
255 used = 0;
256 available = 64;
257 }
258
259 memset(&ctx->buffer[used], 0, available - 8);
260
261 ctx->lo <<= 3;
262 mdOUT(&ctx->buffer[56], ctx->lo);
263 mdOUT(&ctx->buffer[60], ctx->hi);
264
265 body(ctx, ctx->buffer, 64);
266
267 mdOUT(&result[0], ctx->a);
268 mdOUT(&result[4], ctx->b);
269 mdOUT(&result[8], ctx->c);
270 mdOUT(&result[12], ctx->d);
271
272 memset(ctx, 0, sizeof(*ctx));
273}