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