Indent files.
[midnight-commander.git] / src / vfs / smbfs / helpers / lib / md4.c
blob54186eba698a7058fd8cccaab8097a2f07505665
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 a implementation of MD4 designed for use in the SMB authentication protocol
6 Copyright (C) Andrew Tridgell 1997-1998.
8 Copyright (C) 2011
9 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "includes.h"
29 /* NOTE: This code makes no attempt to be fast!
31 It assumes that a int is at least 32 bits long
34 static uint32 A, B, C, D;
36 static uint32
37 F (uint32 X, uint32 Y, uint32 Z)
39 return (X & Y) | ((~X) & Z);
42 static uint32
43 G (uint32 X, uint32 Y, uint32 Z)
45 return (X & Y) | (X & Z) | (Y & Z);
48 static uint32
49 H (uint32 X, uint32 Y, uint32 Z)
51 return X ^ Y ^ Z;
54 static uint32
55 lshift (uint32 x, int s)
57 x &= 0xFFFFFFFF;
58 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
61 #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
62 #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32)0x5A827999,s)
63 #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32)0x6ED9EBA1,s)
65 /* this applies md4 to 64 byte chunks */
66 static void
67 mdfour64 (uint32 * M)
69 int j;
70 uint32 AA, BB, CC, DD;
71 uint32 X[16];
73 for (j = 0; j < 16; j++)
74 X[j] = M[j];
76 AA = A;
77 BB = B;
78 CC = C;
79 DD = D;
81 ROUND1 (A, B, C, D, 0, 3);
82 ROUND1 (D, A, B, C, 1, 7);
83 ROUND1 (C, D, A, B, 2, 11);
84 ROUND1 (B, C, D, A, 3, 19);
85 ROUND1 (A, B, C, D, 4, 3);
86 ROUND1 (D, A, B, C, 5, 7);
87 ROUND1 (C, D, A, B, 6, 11);
88 ROUND1 (B, C, D, A, 7, 19);
89 ROUND1 (A, B, C, D, 8, 3);
90 ROUND1 (D, A, B, C, 9, 7);
91 ROUND1 (C, D, A, B, 10, 11);
92 ROUND1 (B, C, D, A, 11, 19);
93 ROUND1 (A, B, C, D, 12, 3);
94 ROUND1 (D, A, B, C, 13, 7);
95 ROUND1 (C, D, A, B, 14, 11);
96 ROUND1 (B, C, D, A, 15, 19);
98 ROUND2 (A, B, C, D, 0, 3);
99 ROUND2 (D, A, B, C, 4, 5);
100 ROUND2 (C, D, A, B, 8, 9);
101 ROUND2 (B, C, D, A, 12, 13);
102 ROUND2 (A, B, C, D, 1, 3);
103 ROUND2 (D, A, B, C, 5, 5);
104 ROUND2 (C, D, A, B, 9, 9);
105 ROUND2 (B, C, D, A, 13, 13);
106 ROUND2 (A, B, C, D, 2, 3);
107 ROUND2 (D, A, B, C, 6, 5);
108 ROUND2 (C, D, A, B, 10, 9);
109 ROUND2 (B, C, D, A, 14, 13);
110 ROUND2 (A, B, C, D, 3, 3);
111 ROUND2 (D, A, B, C, 7, 5);
112 ROUND2 (C, D, A, B, 11, 9);
113 ROUND2 (B, C, D, A, 15, 13);
115 ROUND3 (A, B, C, D, 0, 3);
116 ROUND3 (D, A, B, C, 8, 9);
117 ROUND3 (C, D, A, B, 4, 11);
118 ROUND3 (B, C, D, A, 12, 15);
119 ROUND3 (A, B, C, D, 2, 3);
120 ROUND3 (D, A, B, C, 10, 9);
121 ROUND3 (C, D, A, B, 6, 11);
122 ROUND3 (B, C, D, A, 14, 15);
123 ROUND3 (A, B, C, D, 1, 3);
124 ROUND3 (D, A, B, C, 9, 9);
125 ROUND3 (C, D, A, B, 5, 11);
126 ROUND3 (B, C, D, A, 13, 15);
127 ROUND3 (A, B, C, D, 3, 3);
128 ROUND3 (D, A, B, C, 11, 9);
129 ROUND3 (C, D, A, B, 7, 11);
130 ROUND3 (B, C, D, A, 15, 15);
132 A += AA;
133 B += BB;
134 C += CC;
135 D += DD;
137 A &= 0xFFFFFFFF;
138 B &= 0xFFFFFFFF;
139 C &= 0xFFFFFFFF;
140 D &= 0xFFFFFFFF;
142 for (j = 0; j < 16; j++)
143 X[j] = 0;
146 static void
147 copy64 (uint32 * M, unsigned char *in)
149 int i;
151 for (i = 0; i < 16; i++)
152 M[i] = (in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) |
153 (in[i * 4 + 1] << 8) | (in[i * 4 + 0] << 0);
156 static void
157 copy4 (unsigned char *out, uint32 x)
159 out[0] = x & 0xFF;
160 out[1] = (x >> 8) & 0xFF;
161 out[2] = (x >> 16) & 0xFF;
162 out[3] = (x >> 24) & 0xFF;
165 /* produce a md4 message digest from data of length n bytes */
166 void
167 mdfour (unsigned char *out, unsigned char *in, int n)
169 unsigned char buf[128];
170 uint32 M[16];
171 uint32 b = n * 8;
172 int i;
174 A = 0x67452301;
175 B = 0xefcdab89;
176 C = 0x98badcfe;
177 D = 0x10325476;
179 while (n > 64)
181 copy64 (M, in);
182 mdfour64 (M);
183 in += 64;
184 n -= 64;
187 for (i = 0; i < 128; i++)
188 buf[i] = 0;
189 memcpy (buf, in, n);
190 buf[n] = 0x80;
192 if (n <= 55)
194 copy4 (buf + 56, b);
195 copy64 (M, buf);
196 mdfour64 (M);
198 else
200 copy4 (buf + 120, b);
201 copy64 (M, buf);
202 mdfour64 (M);
203 copy64 (M, buf + 64);
204 mdfour64 (M);
207 for (i = 0; i < 128; i++)
208 buf[i] = 0;
209 copy64 (M, buf);
211 copy4 (out, A);
212 copy4 (out + 4, B);
213 copy4 (out + 8, C);
214 copy4 (out + 12, D);
216 A = B = C = D = 0;