]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/smblib/md4.c
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * Unix SMB/Netbios implementation.
12 * a implementation of MD4 designed for use in the SMB authentication protocol
13 * Copyright (C) Andrew Tridgell 1997
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "smblib/md4.h"
36 /* NOTE: This code makes no attempt to be fast!
38 * It assumes that a int is at least 32 bits long
41 typedef unsigned int uint32
;
43 static uint32 A
, B
, C
, D
;
46 F(uint32 X
, uint32 Y
, uint32 Z
)
48 return (X
& Y
) | ((~X
) & Z
);
52 G(uint32 X
, uint32 Y
, uint32 Z
)
54 return (X
& Y
) | (X
& Z
) | (Y
& Z
);
58 H(uint32 X
, uint32 Y
, uint32 Z
)
64 lshift(uint32 x
, int s
)
67 return ((x
<< s
) & 0xFFFFFFFF) | (x
>> (32 - s
));
70 #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
71 #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32)0x5A827999,s)
72 #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32)0x6ED9EBA1,s)
74 /* this applies md4 to 64 byte chunks */
79 uint32 AA
, BB
, CC
, DD
;
82 for (j
= 0; j
< 16; j
++)
90 ROUND1(A
, B
, C
, D
, 0, 3);
91 ROUND1(D
, A
, B
, C
, 1, 7);
92 ROUND1(C
, D
, A
, B
, 2, 11);
93 ROUND1(B
, C
, D
, A
, 3, 19);
94 ROUND1(A
, B
, C
, D
, 4, 3);
95 ROUND1(D
, A
, B
, C
, 5, 7);
96 ROUND1(C
, D
, A
, B
, 6, 11);
97 ROUND1(B
, C
, D
, A
, 7, 19);
98 ROUND1(A
, B
, C
, D
, 8, 3);
99 ROUND1(D
, A
, B
, C
, 9, 7);
100 ROUND1(C
, D
, A
, B
, 10, 11);
101 ROUND1(B
, C
, D
, A
, 11, 19);
102 ROUND1(A
, B
, C
, D
, 12, 3);
103 ROUND1(D
, A
, B
, C
, 13, 7);
104 ROUND1(C
, D
, A
, B
, 14, 11);
105 ROUND1(B
, C
, D
, A
, 15, 19);
107 ROUND2(A
, B
, C
, D
, 0, 3);
108 ROUND2(D
, A
, B
, C
, 4, 5);
109 ROUND2(C
, D
, A
, B
, 8, 9);
110 ROUND2(B
, C
, D
, A
, 12, 13);
111 ROUND2(A
, B
, C
, D
, 1, 3);
112 ROUND2(D
, A
, B
, C
, 5, 5);
113 ROUND2(C
, D
, A
, B
, 9, 9);
114 ROUND2(B
, C
, D
, A
, 13, 13);
115 ROUND2(A
, B
, C
, D
, 2, 3);
116 ROUND2(D
, A
, B
, C
, 6, 5);
117 ROUND2(C
, D
, A
, B
, 10, 9);
118 ROUND2(B
, C
, D
, A
, 14, 13);
119 ROUND2(A
, B
, C
, D
, 3, 3);
120 ROUND2(D
, A
, B
, C
, 7, 5);
121 ROUND2(C
, D
, A
, B
, 11, 9);
122 ROUND2(B
, C
, D
, A
, 15, 13);
124 ROUND3(A
, B
, C
, D
, 0, 3);
125 ROUND3(D
, A
, B
, C
, 8, 9);
126 ROUND3(C
, D
, A
, B
, 4, 11);
127 ROUND3(B
, C
, D
, A
, 12, 15);
128 ROUND3(A
, B
, C
, D
, 2, 3);
129 ROUND3(D
, A
, B
, C
, 10, 9);
130 ROUND3(C
, D
, A
, B
, 6, 11);
131 ROUND3(B
, C
, D
, A
, 14, 15);
132 ROUND3(A
, B
, C
, D
, 1, 3);
133 ROUND3(D
, A
, B
, C
, 9, 9);
134 ROUND3(C
, D
, A
, B
, 5, 11);
135 ROUND3(B
, C
, D
, A
, 13, 15);
136 ROUND3(A
, B
, C
, D
, 3, 3);
137 ROUND3(D
, A
, B
, C
, 11, 9);
138 ROUND3(C
, D
, A
, B
, 7, 11);
139 ROUND3(B
, C
, D
, A
, 15, 15);
151 for (j
= 0; j
< 16; j
++)
156 copy64(uint32
* M
, unsigned char *in
)
160 for (i
= 0; i
< 16; i
++)
161 M
[i
] = (in
[i
* 4 + 3] << 24) | (in
[i
* 4 + 2] << 16) |
162 (in
[i
* 4 + 1] << 8) | (in
[i
* 4 + 0] << 0);
166 copy4(unsigned char *out
, uint32 x
)
169 out
[1] = (x
>> 8) & 0xFF;
170 out
[2] = (x
>> 16) & 0xFF;
171 out
[3] = (x
>> 24) & 0xFF;
174 /* produce a md4 message digest from data of length n bytes */
176 mdfour(unsigned char *out
, unsigned char *in
, int n
)
178 unsigned char buf
[128];
195 for (i
= 0; i
< 128; i
++)
212 for (i
= 0; i
< 128; i
++)