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#define SET(n) \
86 (ctx->block[(n)] = (winpr_MD4_u32plus)ptr[4ULL * (n)] | \
87 ((winpr_MD4_u32plus)ptr[4ULL * (n) + 1] << 8) | \
88 ((winpr_MD4_u32plus)ptr[4ULL * (n) + 2] << 16) | \
89 ((winpr_MD4_u32plus)ptr[4ULL * (n) + 3] << 24))
90#define GET(n) (ctx->block[(n)])
91
92/*
93 * This processes one or more 64-byte data blocks, but does NOT update the bit
94 * counters. There are no alignment requirements.
95 */
96static const void* body(WINPR_MD4_CTX* ctx, const void* data, size_t size)
97{
98 const winpr_MD4_u32plus ac1 = 0x5a827999;
99 const winpr_MD4_u32plus ac2 = 0x6ed9eba1;
100
101 const unsigned char* ptr = (const unsigned char*)data;
102
103 winpr_MD4_u32plus a = ctx->a;
104 winpr_MD4_u32plus b = ctx->b;
105 winpr_MD4_u32plus c = ctx->c;
106 winpr_MD4_u32plus d = ctx->d;
107
108 do
109 {
110 const winpr_MD4_u32plus saved_a = a;
111 const winpr_MD4_u32plus saved_b = b;
112 const winpr_MD4_u32plus saved_c = c;
113 const winpr_MD4_u32plus saved_d = d;
114
115 /* Round 1 */
116 STEP(F, a, b, c, d, SET(0), 3)
117 STEP(F, d, a, b, c, SET(1), 7)
118 STEP(F, c, d, a, b, SET(2), 11)
119 STEP(F, b, c, d, a, SET(3), 19)
120 STEP(F, a, b, c, d, SET(4), 3)
121 STEP(F, d, a, b, c, SET(5), 7)
122 STEP(F, c, d, a, b, SET(6), 11)
123 STEP(F, b, c, d, a, SET(7), 19)
124 STEP(F, a, b, c, d, SET(8), 3)
125 STEP(F, d, a, b, c, SET(9), 7)
126 STEP(F, c, d, a, b, SET(10), 11)
127 STEP(F, b, c, d, a, SET(11), 19)
128 STEP(F, a, b, c, d, SET(12), 3)
129 STEP(F, d, a, b, c, SET(13), 7)
130 STEP(F, c, d, a, b, SET(14), 11)
131 STEP(F, b, c, d, a, SET(15), 19)
132
133 /* Round 2 */
134 STEP(G, a, b, c, d, GET(0) + ac1, 3)
135 STEP(G, d, a, b, c, GET(4) + ac1, 5)
136 STEP(G, c, d, a, b, GET(8) + ac1, 9)
137 STEP(G, b, c, d, a, GET(12) + ac1, 13)
138 STEP(G, a, b, c, d, GET(1) + ac1, 3)
139 STEP(G, d, a, b, c, GET(5) + ac1, 5)
140 STEP(G, c, d, a, b, GET(9) + ac1, 9)
141 STEP(G, b, c, d, a, GET(13) + ac1, 13)
142 STEP(G, a, b, c, d, GET(2) + ac1, 3)
143 STEP(G, d, a, b, c, GET(6) + ac1, 5)
144 STEP(G, c, d, a, b, GET(10) + ac1, 9)
145 STEP(G, b, c, d, a, GET(14) + ac1, 13)
146 STEP(G, a, b, c, d, GET(3) + ac1, 3)
147 STEP(G, d, a, b, c, GET(7) + ac1, 5)
148 STEP(G, c, d, a, b, GET(11) + ac1, 9)
149 STEP(G, b, c, d, a, GET(15) + ac1, 13)
150
151 /* Round 3 */
152 STEP(H, a, b, c, d, GET(0) + ac2, 3)
153 STEP(H, d, a, b, c, GET(8) + ac2, 9)
154 STEP(H, c, d, a, b, GET(4) + ac2, 11)
155 STEP(H, b, c, d, a, GET(12) + ac2, 15)
156 STEP(H, a, b, c, d, GET(2) + ac2, 3)
157 STEP(H, d, a, b, c, GET(10) + ac2, 9)
158 STEP(H, c, d, a, b, GET(6) + ac2, 11)
159 STEP(H, b, c, d, a, GET(14) + ac2, 15)
160 STEP(H, a, b, c, d, GET(1) + ac2, 3)
161 STEP(H, d, a, b, c, GET(9) + ac2, 9)
162 STEP(H, c, d, a, b, GET(5) + ac2, 11)
163 STEP(H, b, c, d, a, GET(13) + ac2, 15)
164 STEP(H, a, b, c, d, GET(3) + ac2, 3)
165 STEP(H, d, a, b, c, GET(11) + ac2, 9)
166 STEP(H, c, d, a, b, GET(7) + ac2, 11)
167 STEP(H, b, c, d, a, GET(15) + ac2, 15)
168
169 a += saved_a;
170 b += saved_b;
171 c += saved_c;
172 d += saved_d;
173
174 ptr += 64;
175 } while (size -= 64);
176
177 ctx->a = a;
178 ctx->b = b;
179 ctx->c = c;
180 ctx->d = d;
181
182 return ptr;
183}
184
185void winpr_MD4_Init(WINPR_MD4_CTX* ctx)
186{
187 ctx->a = 0x67452301;
188 ctx->b = 0xefcdab89;
189 ctx->c = 0x98badcfe;
190 ctx->d = 0x10325476;
191
192 ctx->lo = 0;
193 ctx->hi = 0;
194}
195
196void winpr_MD4_Update(WINPR_MD4_CTX* ctx, const void* data, size_t size)
197{
198 winpr_MD4_u32plus saved_lo = ctx->lo;
199 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
200 ctx->hi++;
201 ctx->hi += (winpr_MD4_u32plus)((size >> 29) & 0xffffffff);
202
203 size_t used = saved_lo & 0x3f;
204
205 if (used)
206 {
207 size_t available = 64 - used;
208
209 if (size < available)
210 {
211 memcpy(&ctx->buffer[used], data, size);
212 return;
213 }
214
215 memcpy(&ctx->buffer[used], data, available);
216 data = (const unsigned char*)data + available;
217 size -= available;
218 body(ctx, ctx->buffer, 64);
219 }
220
221 if (size >= 64)
222 {
223 data = body(ctx, data, size & ~(size_t)0x3f);
224 size &= 0x3f;
225 }
226
227 memcpy(ctx->buffer, data, size);
228}
229
230static inline void mdOUT(unsigned char* dst, winpr_MD4_u32plus src)
231{
232 (dst)[0] = (unsigned char)(src);
233 (dst)[1] = (unsigned char)((src) >> 8);
234 (dst)[2] = (unsigned char)((src) >> 16);
235 (dst)[3] = (unsigned char)((src) >> 24);
236}
237
238void winpr_MD4_Final(unsigned char* result, WINPR_MD4_CTX* ctx)
239{
240 size_t used = ctx->lo & 0x3f;
241
242 ctx->buffer[used++] = 0x80;
243
244 size_t available = 64 - used;
245
246 if (available < 8)
247 {
248 memset(&ctx->buffer[used], 0, available);
249 body(ctx, ctx->buffer, 64);
250 used = 0;
251 available = 64;
252 }
253
254 memset(&ctx->buffer[used], 0, available - 8);
255
256 ctx->lo <<= 3;
257 mdOUT(&ctx->buffer[56], ctx->lo);
258 mdOUT(&ctx->buffer[60], ctx->hi);
259
260 body(ctx, ctx->buffer, 64);
261
262 mdOUT(&result[0], ctx->a);
263 mdOUT(&result[4], ctx->b);
264 mdOUT(&result[8], ctx->c);
265 mdOUT(&result[12], ctx->d);
266
267 memset(ctx, 0, sizeof(*ctx));
268}