1 /* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
2 */
3
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD$");
6
7 /*-
8 SPDX-License-Identifier: RSA-MD
9
10 Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
11
12 License to copy and use this software is granted provided that it
13 is identified as the "RSA Data Security, Inc. MD4 Message-Digest
14 Algorithm" in all material mentioning or referencing this software
15 or this function.
16
17 License is also granted to make and use derivative works provided
18 that such works are identified as "derived from the RSA Data
19 Security, Inc. MD4 Message-Digest Algorithm" in all material
20 mentioning or referencing the derived work.
21
22 RSA Data Security, Inc. makes no representations concerning either
23 the merchantability of this software or the suitability of this
24 software for any particular purpose. It is provided "as is"
25 without express or implied warranty of any kind.
26
27 These notices must be retained in any copies of any part of this
28 documentation and/or software.
29 */
30
31 #include <sys/types.h>
32 #include <string.h>
33 #include "md4.h"
34
35 typedef unsigned char *POINTER;
36 typedef const unsigned char *CONST_POINTER;
37 typedef u_int16_t UINT2;
38 typedef u_int32_t UINT4;
39
40 #define PROTO_LIST(list) list
41
42 /* Constants for MD4Transform routine.
43 */
44 #define S11 3
45 #define S12 7
46 #define S13 11
47 #define S14 19
48 #define S21 3
49 #define S22 5
50 #define S23 9
51 #define S24 13
52 #define S31 3
53 #define S32 9
54 #define S33 11
55 #define S34 15
56
57 static void MD4Transform PROTO_LIST ((UINT4 [4], const unsigned char [64]));
58 static void Encode PROTO_LIST
59 ((unsigned char *, UINT4 *, unsigned int));
60 static void Decode PROTO_LIST
61 ((UINT4 *, const unsigned char *, unsigned int));
62
63 static unsigned char PADDING[64] = {
64 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
67 };
68
69 /* F, G and H are basic MD4 functions.
70 */
71 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
72 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
73 #define H(x, y, z) ((x) ^ (y) ^ (z))
74
75 /* ROTATE_LEFT rotates x left n bits.
76 */
77 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
78
79 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
80 /* Rotation is separate from addition to prevent recomputation */
81 #define FF(a, b, c, d, x, s) { \
82 (a) += F ((b), (c), (d)) + (x); \
83 (a) = ROTATE_LEFT ((a), (s)); \
84 }
85 #define GG(a, b, c, d, x, s) { \
86 (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
87 (a) = ROTATE_LEFT ((a), (s)); \
88 }
89 #define HH(a, b, c, d, x, s) { \
90 (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
91 (a) = ROTATE_LEFT ((a), (s)); \
92 }
93
94 /* MD4 initialization. Begins an MD4 operation, writing a new context.
95 */
MD4Init(context)96 void MD4Init (context)
97 MD4_CTX *context; /* context */
98 {
99 context->count[0] = context->count[1] = 0;
100
101 /* Load magic initialization constants.
102 */
103 context->state[0] = 0x67452301;
104 context->state[1] = 0xefcdab89;
105 context->state[2] = 0x98badcfe;
106 context->state[3] = 0x10325476;
107 }
108
109 /* MD4 block update operation. Continues an MD4 message-digest
110 operation, processing another message block, and updating the
111 context.
112 */
MD4Update(context,in,inputLen)113 void MD4Update (context, in, inputLen)
114 MD4_CTX *context; /* context */
115 const void *in; /* input block */
116 unsigned int inputLen; /* length of input block */
117 {
118 unsigned int i, idx, partLen;
119 const unsigned char *input = in;
120
121 /* Compute number of bytes mod 64 */
122 idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
123 /* Update number of bits */
124 if ((context->count[0] += ((UINT4)inputLen << 3))
125 < ((UINT4)inputLen << 3))
126 context->count[1]++;
127 context->count[1] += ((UINT4)inputLen >> 29);
128
129 partLen = 64 - idx;
130 /* Transform as many times as possible.
131 */
132 if (inputLen >= partLen) {
133 memcpy
134 ((POINTER)&context->buffer[idx], (CONST_POINTER)input, partLen);
135 MD4Transform (context->state, context->buffer);
136
137 for (i = partLen; i + 63 < inputLen; i += 64)
138 MD4Transform (context->state, &input[i]);
139
140 idx = 0;
141 }
142 else
143 i = 0;
144
145 /* Buffer remaining input */
146 memcpy
147 ((POINTER)&context->buffer[idx], (CONST_POINTER)&input[i],
148 inputLen-i);
149 }
150
151 /* MD4 padding. */
MD4Pad(context)152 void MD4Pad (context)
153 MD4_CTX *context; /* context */
154 {
155 unsigned char bits[8];
156 unsigned int idx, padLen;
157
158 /* Save number of bits */
159 Encode (bits, context->count, 8);
160
161 /* Pad out to 56 mod 64.
162 */
163 idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
164 padLen = (idx < 56) ? (56 - idx) : (120 - idx);
165 MD4Update (context, PADDING, padLen);
166
167 /* Append length (before padding) */
168 MD4Update (context, bits, 8);
169 }
170
171 /* MD4 finalization. Ends an MD4 message-digest operation, writing the
172 the message digest and zeroizing the context.
173 */
MD4Final(digest,context)174 void MD4Final (digest, context)
175 unsigned char digest[16]; /* message digest */
176 MD4_CTX *context; /* context */
177 {
178 /* Do padding */
179 MD4Pad (context);
180
181 /* Store state in digest */
182 Encode (digest, context->state, 16);
183
184 /* Zeroize sensitive information.
185 */
186 explicit_bzero(context, sizeof(*context));
187 }
188
189 /* MD4 basic transformation. Transforms state based on block.
190 */
MD4Transform(state,block)191 static void MD4Transform (state, block)
192 UINT4 state[4];
193 const unsigned char block[64];
194 {
195 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
196
197 Decode (x, block, 64);
198
199 /* Round 1 */
200 FF (a, b, c, d, x[ 0], S11); /* 1 */
201 FF (d, a, b, c, x[ 1], S12); /* 2 */
202 FF (c, d, a, b, x[ 2], S13); /* 3 */
203 FF (b, c, d, a, x[ 3], S14); /* 4 */
204 FF (a, b, c, d, x[ 4], S11); /* 5 */
205 FF (d, a, b, c, x[ 5], S12); /* 6 */
206 FF (c, d, a, b, x[ 6], S13); /* 7 */
207 FF (b, c, d, a, x[ 7], S14); /* 8 */
208 FF (a, b, c, d, x[ 8], S11); /* 9 */
209 FF (d, a, b, c, x[ 9], S12); /* 10 */
210 FF (c, d, a, b, x[10], S13); /* 11 */
211 FF (b, c, d, a, x[11], S14); /* 12 */
212 FF (a, b, c, d, x[12], S11); /* 13 */
213 FF (d, a, b, c, x[13], S12); /* 14 */
214 FF (c, d, a, b, x[14], S13); /* 15 */
215 FF (b, c, d, a, x[15], S14); /* 16 */
216
217 /* Round 2 */
218 GG (a, b, c, d, x[ 0], S21); /* 17 */
219 GG (d, a, b, c, x[ 4], S22); /* 18 */
220 GG (c, d, a, b, x[ 8], S23); /* 19 */
221 GG (b, c, d, a, x[12], S24); /* 20 */
222 GG (a, b, c, d, x[ 1], S21); /* 21 */
223 GG (d, a, b, c, x[ 5], S22); /* 22 */
224 GG (c, d, a, b, x[ 9], S23); /* 23 */
225 GG (b, c, d, a, x[13], S24); /* 24 */
226 GG (a, b, c, d, x[ 2], S21); /* 25 */
227 GG (d, a, b, c, x[ 6], S22); /* 26 */
228 GG (c, d, a, b, x[10], S23); /* 27 */
229 GG (b, c, d, a, x[14], S24); /* 28 */
230 GG (a, b, c, d, x[ 3], S21); /* 29 */
231 GG (d, a, b, c, x[ 7], S22); /* 30 */
232 GG (c, d, a, b, x[11], S23); /* 31 */
233 GG (b, c, d, a, x[15], S24); /* 32 */
234
235 /* Round 3 */
236 HH (a, b, c, d, x[ 0], S31); /* 33 */
237 HH (d, a, b, c, x[ 8], S32); /* 34 */
238 HH (c, d, a, b, x[ 4], S33); /* 35 */
239 HH (b, c, d, a, x[12], S34); /* 36 */
240 HH (a, b, c, d, x[ 2], S31); /* 37 */
241 HH (d, a, b, c, x[10], S32); /* 38 */
242 HH (c, d, a, b, x[ 6], S33); /* 39 */
243 HH (b, c, d, a, x[14], S34); /* 40 */
244 HH (a, b, c, d, x[ 1], S31); /* 41 */
245 HH (d, a, b, c, x[ 9], S32); /* 42 */
246 HH (c, d, a, b, x[ 5], S33); /* 43 */
247 HH (b, c, d, a, x[13], S34); /* 44 */
248 HH (a, b, c, d, x[ 3], S31); /* 45 */
249 HH (d, a, b, c, x[11], S32); /* 46 */
250 HH (c, d, a, b, x[ 7], S33); /* 47 */
251 HH (b, c, d, a, x[15], S34); /* 48 */
252
253 state[0] += a;
254 state[1] += b;
255 state[2] += c;
256 state[3] += d;
257
258 /* Zeroize sensitive information.
259 */
260 memset ((POINTER)x, 0, sizeof (x));
261 }
262
263 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
264 a multiple of 4.
265 */
Encode(output,input,len)266 static void Encode (output, input, len)
267 unsigned char *output;
268 UINT4 *input;
269 unsigned int len;
270 {
271 unsigned int i, j;
272
273 for (i = 0, j = 0; j < len; i++, j += 4) {
274 output[j] = (unsigned char)(input[i] & 0xff);
275 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
276 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
277 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
278 }
279 }
280
281 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
282 a multiple of 4.
283 */
Decode(output,input,len)284 static void Decode (output, input, len)
285
286 UINT4 *output;
287 const unsigned char *input;
288 unsigned int len;
289 {
290 unsigned int i, j;
291
292 for (i = 0, j = 0; j < len; i++, j += 4)
293 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
294 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
295 }
296
297 #ifdef WEAK_REFS
298 /* When building libmd, provide weak references. Note: this is not
299 activated in the context of compiling these sources for internal
300 use in libcrypt.
301 */
302 #undef MD4Init
303 __weak_reference(_libmd_MD4Init, MD4Init);
304 #undef MD4Update
305 __weak_reference(_libmd_MD4Update, MD4Update);
306 #undef MD4Pad
307 __weak_reference(_libmd_MD4Pad, MD4Pad);
308 #undef MD4Final
309 __weak_reference(_libmd_MD4Final, MD4Final);
310 #endif
311