FreeRDP
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 "md4.h"
41 
42 /*
43  * The basic MD4 functions.
44  *
45  * F and G are optimized compared to their RFC 1320 definitions, with the
46  * optimization for F borrowed from Colin Plumb's MD5 implementation.
47  */
48 static inline winpr_MD4_u32plus F(winpr_MD4_u32plus x, winpr_MD4_u32plus y, winpr_MD4_u32plus z)
49 {
50  return ((z) ^ ((x) & ((y) ^ (z))));
51 }
52 static inline winpr_MD4_u32plus G(winpr_MD4_u32plus x, winpr_MD4_u32plus y, winpr_MD4_u32plus z)
53 {
54  return (((x) & ((y) | (z))) | ((y) & (z)));
55 }
56 static inline winpr_MD4_u32plus H(winpr_MD4_u32plus x, winpr_MD4_u32plus y, winpr_MD4_u32plus z)
57 {
58  return ((x) ^ (y) ^ (z));
59 }
60 
61 /*
62  * The MD4 transformation for all three rounds.
63  */
64 #define STEP(f, a, b, c, d, x, s) \
65  (a) += f((b), (c), (d)) + (x); \
66  (a) = (((a) << (s)) | (((a)&0xffffffff) >> (32 - (s))));
67 
68 /*
69  * SET reads 4 input bytes in little-endian byte order and stores them in a
70  * properly aligned word in host byte order.
71  *
72  * The check for little-endian architectures that tolerate unaligned memory
73  * accesses is just an optimization. Nothing will break if it fails to detect
74  * a suitable architecture.
75  *
76  * Unfortunately, this optimization may be a C strict aliasing rules violation
77  * if the caller's data buffer has effective type that cannot be aliased by
78  * winpr_MD4_u32plus. In practice, this problem may occur if these MD4 routines are
79  * inlined into a calling function, or with future and dangerously advanced
80  * link-time optimizations. For the time being, keeping these MD4 routines in
81  * their own translation unit avoids the problem.
82  */
83 #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
84 #define SET(n) (*(const winpr_MD4_u32plus*)&ptr[4ULL * (n)])
85 #define GET(n) SET(n)
86 #else
87 #define SET(n) \
88  (ctx->block[(n)] = (winpr_MD4_u32plus)ptr[4ULL * (n)] | \
89  ((winpr_MD4_u32plus)ptr[4ULL * (n) + 1] << 8) | \
90  ((winpr_MD4_u32plus)ptr[4ULL * (n) + 2] << 16) | \
91  ((winpr_MD4_u32plus)ptr[4ULL * (n) + 3] << 24))
92 #define GET(n) (ctx->block[(n)])
93 #endif
94 
95 /*
96  * This processes one or more 64-byte data blocks, but does NOT update the bit
97  * counters. There are no alignment requirements.
98  */
99 static const void* body(WINPR_MD4_CTX* ctx, const void* data, unsigned long size)
100 {
101  const winpr_MD4_u32plus ac1 = 0x5a827999;
102  const winpr_MD4_u32plus ac2 = 0x6ed9eba1;
103 
104  const unsigned char* ptr = (const unsigned char*)data;
105 
106  winpr_MD4_u32plus a = ctx->a;
107  winpr_MD4_u32plus b = ctx->b;
108  winpr_MD4_u32plus c = ctx->c;
109  winpr_MD4_u32plus d = ctx->d;
110 
111  do
112  {
113  const winpr_MD4_u32plus saved_a = a;
114  const winpr_MD4_u32plus saved_b = b;
115  const winpr_MD4_u32plus saved_c = c;
116  const winpr_MD4_u32plus saved_d = d;
117 
118  /* Round 1 */
119  STEP(F, a, b, c, d, SET(0), 3)
120  STEP(F, d, a, b, c, SET(1), 7)
121  STEP(F, c, d, a, b, SET(2), 11)
122  STEP(F, b, c, d, a, SET(3), 19)
123  STEP(F, a, b, c, d, SET(4), 3)
124  STEP(F, d, a, b, c, SET(5), 7)
125  STEP(F, c, d, a, b, SET(6), 11)
126  STEP(F, b, c, d, a, SET(7), 19)
127  STEP(F, a, b, c, d, SET(8), 3)
128  STEP(F, d, a, b, c, SET(9), 7)
129  STEP(F, c, d, a, b, SET(10), 11)
130  STEP(F, b, c, d, a, SET(11), 19)
131  STEP(F, a, b, c, d, SET(12), 3)
132  STEP(F, d, a, b, c, SET(13), 7)
133  STEP(F, c, d, a, b, SET(14), 11)
134  STEP(F, b, c, d, a, SET(15), 19)
135 
136  /* Round 2 */
137  STEP(G, a, b, c, d, GET(0) + ac1, 3)
138  STEP(G, d, a, b, c, GET(4) + ac1, 5)
139  STEP(G, c, d, a, b, GET(8) + ac1, 9)
140  STEP(G, b, c, d, a, GET(12) + ac1, 13)
141  STEP(G, a, b, c, d, GET(1) + ac1, 3)
142  STEP(G, d, a, b, c, GET(5) + ac1, 5)
143  STEP(G, c, d, a, b, GET(9) + ac1, 9)
144  STEP(G, b, c, d, a, GET(13) + ac1, 13)
145  STEP(G, a, b, c, d, GET(2) + ac1, 3)
146  STEP(G, d, a, b, c, GET(6) + ac1, 5)
147  STEP(G, c, d, a, b, GET(10) + ac1, 9)
148  STEP(G, b, c, d, a, GET(14) + ac1, 13)
149  STEP(G, a, b, c, d, GET(3) + ac1, 3)
150  STEP(G, d, a, b, c, GET(7) + ac1, 5)
151  STEP(G, c, d, a, b, GET(11) + ac1, 9)
152  STEP(G, b, c, d, a, GET(15) + ac1, 13)
153 
154  /* Round 3 */
155  STEP(H, a, b, c, d, GET(0) + ac2, 3)
156  STEP(H, d, a, b, c, GET(8) + ac2, 9)
157  STEP(H, c, d, a, b, GET(4) + ac2, 11)
158  STEP(H, b, c, d, a, GET(12) + ac2, 15)
159  STEP(H, a, b, c, d, GET(2) + ac2, 3)
160  STEP(H, d, a, b, c, GET(10) + ac2, 9)
161  STEP(H, c, d, a, b, GET(6) + ac2, 11)
162  STEP(H, b, c, d, a, GET(14) + ac2, 15)
163  STEP(H, a, b, c, d, GET(1) + ac2, 3)
164  STEP(H, d, a, b, c, GET(9) + ac2, 9)
165  STEP(H, c, d, a, b, GET(5) + ac2, 11)
166  STEP(H, b, c, d, a, GET(13) + ac2, 15)
167  STEP(H, a, b, c, d, GET(3) + ac2, 3)
168  STEP(H, d, a, b, c, GET(11) + ac2, 9)
169  STEP(H, c, d, a, b, GET(7) + ac2, 11)
170  STEP(H, b, c, d, a, GET(15) + ac2, 15)
171 
172  a += saved_a;
173  b += saved_b;
174  c += saved_c;
175  d += saved_d;
176 
177  ptr += 64;
178  } while (size -= 64);
179 
180  ctx->a = a;
181  ctx->b = b;
182  ctx->c = c;
183  ctx->d = d;
184 
185  return ptr;
186 }
187 
188 void winpr_MD4_Init(WINPR_MD4_CTX* ctx)
189 {
190  ctx->a = 0x67452301;
191  ctx->b = 0xefcdab89;
192  ctx->c = 0x98badcfe;
193  ctx->d = 0x10325476;
194 
195  ctx->lo = 0;
196  ctx->hi = 0;
197 }
198 
199 void winpr_MD4_Update(WINPR_MD4_CTX* ctx, const void* data, unsigned long size)
200 {
201  winpr_MD4_u32plus saved_lo = ctx->lo;
202  if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
203  ctx->hi++;
204  ctx->hi += size >> 29;
205 
206  unsigned long used = saved_lo & 0x3f;
207 
208  if (used)
209  {
210  unsigned long available = 64 - used;
211 
212  if (size < available)
213  {
214  memcpy(&ctx->buffer[used], data, size);
215  return;
216  }
217 
218  memcpy(&ctx->buffer[used], data, available);
219  data = (const unsigned char*)data + available;
220  size -= available;
221  body(ctx, ctx->buffer, 64);
222  }
223 
224  if (size >= 64)
225  {
226  data = body(ctx, data, size & ~(unsigned long)0x3f);
227  size &= 0x3f;
228  }
229 
230  memcpy(ctx->buffer, data, size);
231 }
232 
233 static inline void mdOUT(unsigned char* dst, winpr_MD4_u32plus src)
234 {
235  (dst)[0] = (unsigned char)(src);
236  (dst)[1] = (unsigned char)((src) >> 8);
237  (dst)[2] = (unsigned char)((src) >> 16);
238  (dst)[3] = (unsigned char)((src) >> 24);
239 }
240 
241 void winpr_MD4_Final(unsigned char* result, WINPR_MD4_CTX* ctx)
242 {
243  unsigned long used = ctx->lo & 0x3f;
244 
245  ctx->buffer[used++] = 0x80;
246 
247  unsigned long available = 64 - used;
248 
249  if (available < 8)
250  {
251  memset(&ctx->buffer[used], 0, available);
252  body(ctx, ctx->buffer, 64);
253  used = 0;
254  available = 64;
255  }
256 
257  memset(&ctx->buffer[used], 0, available - 8);
258 
259  ctx->lo <<= 3;
260  mdOUT(&ctx->buffer[56], ctx->lo);
261  mdOUT(&ctx->buffer[60], ctx->hi);
262 
263  body(ctx, ctx->buffer, 64);
264 
265  mdOUT(&result[0], ctx->a);
266  mdOUT(&result[4], ctx->b);
267  mdOUT(&result[8], ctx->c);
268  mdOUT(&result[12], ctx->d);
269 
270  memset(ctx, 0, sizeof(*ctx));
271 }