Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : a implementation of MD4 designed for use in the SMB authentication protocol
4 : Copyright (C) Andrew Tridgell 1997-1998.
5 :
6 : This program is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation; either version 3 of the License, or
9 : (at your option) any later version.
10 :
11 : This program is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "replace.h"
21 : #include "../lib/crypto/md4.h"
22 :
23 : /* NOTE: This code makes no attempt to be fast!
24 :
25 : It assumes that a int is at least 32 bits long
26 : */
27 :
28 : struct mdfour_state {
29 : uint32_t A, B, C, D;
30 : };
31 :
32 2048997 : static uint32_t F(uint32_t X, uint32_t Y, uint32_t Z)
33 : {
34 2048997 : return (X&Y) | ((~X)&Z);
35 : }
36 :
37 2048997 : static uint32_t G(uint32_t X, uint32_t Y, uint32_t Z)
38 : {
39 2048997 : return (X&Y) | (X&Z) | (Y&Z);
40 : }
41 :
42 2048997 : static uint32_t H(uint32_t X, uint32_t Y, uint32_t Z)
43 : {
44 2048997 : return X^Y^Z;
45 : }
46 :
47 6143941 : static uint32_t lshift(uint32_t x, int s)
48 : {
49 6143941 : x &= 0xFFFFFFFF;
50 6143941 : return ((x<<s)&0xFFFFFFFF) | (x>>(32-s));
51 : }
52 :
53 : #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
54 : #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32_t)0x5A827999,s)
55 : #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32_t)0x6ED9EBA1,s)
56 :
57 : /* this applies md4 to 64 byte chunks */
58 129492 : static void mdfour64(struct mdfour_state *s, uint32_t *M)
59 : {
60 1525 : int j;
61 1525 : uint32_t AA, BB, CC, DD;
62 1525 : uint32_t X[16];
63 :
64 2201364 : for (j=0;j<16;j++)
65 2071872 : X[j] = M[j];
66 :
67 129492 : AA = s->A; BB = s->B; CC = s->C; DD = s->D;
68 :
69 129492 : ROUND1(s->A,s->B,s->C,s->D, 0, 3); ROUND1(s->D,s->A,s->B,s->C, 1, 7);
70 129492 : ROUND1(s->C,s->D,s->A,s->B, 2, 11); ROUND1(s->B,s->C,s->D,s->A, 3, 19);
71 129492 : ROUND1(s->A,s->B,s->C,s->D, 4, 3); ROUND1(s->D,s->A,s->B,s->C, 5, 7);
72 129492 : ROUND1(s->C,s->D,s->A,s->B, 6, 11); ROUND1(s->B,s->C,s->D,s->A, 7, 19);
73 129492 : ROUND1(s->A,s->B,s->C,s->D, 8, 3); ROUND1(s->D,s->A,s->B,s->C, 9, 7);
74 129492 : ROUND1(s->C,s->D,s->A,s->B, 10, 11); ROUND1(s->B,s->C,s->D,s->A, 11, 19);
75 129492 : ROUND1(s->A,s->B,s->C,s->D, 12, 3); ROUND1(s->D,s->A,s->B,s->C, 13, 7);
76 129492 : ROUND1(s->C,s->D,s->A,s->B, 14, 11); ROUND1(s->B,s->C,s->D,s->A, 15, 19);
77 :
78 129492 : ROUND2(s->A,s->B,s->C,s->D, 0, 3); ROUND2(s->D,s->A,s->B,s->C, 4, 5);
79 129492 : ROUND2(s->C,s->D,s->A,s->B, 8, 9); ROUND2(s->B,s->C,s->D,s->A, 12, 13);
80 129492 : ROUND2(s->A,s->B,s->C,s->D, 1, 3); ROUND2(s->D,s->A,s->B,s->C, 5, 5);
81 129492 : ROUND2(s->C,s->D,s->A,s->B, 9, 9); ROUND2(s->B,s->C,s->D,s->A, 13, 13);
82 129492 : ROUND2(s->A,s->B,s->C,s->D, 2, 3); ROUND2(s->D,s->A,s->B,s->C, 6, 5);
83 129492 : ROUND2(s->C,s->D,s->A,s->B, 10, 9); ROUND2(s->B,s->C,s->D,s->A, 14, 13);
84 129492 : ROUND2(s->A,s->B,s->C,s->D, 3, 3); ROUND2(s->D,s->A,s->B,s->C, 7, 5);
85 129492 : ROUND2(s->C,s->D,s->A,s->B, 11, 9); ROUND2(s->B,s->C,s->D,s->A, 15, 13);
86 :
87 129492 : ROUND3(s->A,s->B,s->C,s->D, 0, 3); ROUND3(s->D,s->A,s->B,s->C, 8, 9);
88 129492 : ROUND3(s->C,s->D,s->A,s->B, 4, 11); ROUND3(s->B,s->C,s->D,s->A, 12, 15);
89 129492 : ROUND3(s->A,s->B,s->C,s->D, 2, 3); ROUND3(s->D,s->A,s->B,s->C, 10, 9);
90 129492 : ROUND3(s->C,s->D,s->A,s->B, 6, 11); ROUND3(s->B,s->C,s->D,s->A, 14, 15);
91 129492 : ROUND3(s->A,s->B,s->C,s->D, 1, 3); ROUND3(s->D,s->A,s->B,s->C, 9, 9);
92 129492 : ROUND3(s->C,s->D,s->A,s->B, 5, 11); ROUND3(s->B,s->C,s->D,s->A, 13, 15);
93 129492 : ROUND3(s->A,s->B,s->C,s->D, 3, 3); ROUND3(s->D,s->A,s->B,s->C, 11, 9);
94 129492 : ROUND3(s->C,s->D,s->A,s->B, 7, 11); ROUND3(s->B,s->C,s->D,s->A, 15, 15);
95 :
96 129492 : s->A += AA;
97 129492 : s->B += BB;
98 129492 : s->C += CC;
99 129492 : s->D += DD;
100 :
101 129492 : s->A &= 0xFFFFFFFF;
102 129492 : s->B &= 0xFFFFFFFF;
103 129492 : s->C &= 0xFFFFFFFF;
104 129492 : s->D &= 0xFFFFFFFF;
105 :
106 2176964 : for (j=0;j<16;j++)
107 2047472 : X[j] = 0;
108 129492 : }
109 :
110 206425 : static void copy64(uint32_t *M, const uint8_t *in)
111 : {
112 1935 : int i;
113 :
114 3509225 : for (i=0;i<16;i++)
115 3302800 : M[i] = ((uint32_t)in[i*4+3] << 24) |
116 3302800 : ((uint32_t)in[i*4+2] << 16) |
117 3302800 : ((uint32_t)in[i*4+1] << 8) |
118 3302800 : ((uint32_t)in[i*4+0] << 0);
119 206425 : }
120 :
121 383435 : static void copy4(uint8_t *out, uint32_t x)
122 : {
123 383435 : out[0] = x&0xFF;
124 383435 : out[1] = (x>>8)&0xFF;
125 383435 : out[2] = (x>>16)&0xFF;
126 383435 : out[3] = (x>>24)&0xFF;
127 382615 : }
128 :
129 : /**
130 : * produce a md4 message digest from data of length n bytes
131 : */
132 76933 : _PUBLIC_ void mdfour(uint8_t *out, const uint8_t *in, int n)
133 : {
134 410 : uint8_t buf[128];
135 410 : uint32_t M[16];
136 76933 : uint32_t b = n * 8;
137 410 : int i;
138 410 : struct mdfour_state state;
139 :
140 76933 : state.A = 0x67452301;
141 76933 : state.B = 0xefcdab89;
142 76933 : state.C = 0x98badcfe;
143 76933 : state.D = 0x10325476;
144 :
145 124570 : while (n > 64) {
146 47637 : copy64(M, in);
147 47637 : mdfour64(&state, M);
148 47637 : in += 64;
149 47637 : n -= 64;
150 : }
151 :
152 9924357 : for (i=0;i<128;i++)
153 9847424 : buf[i] = 0;
154 76933 : memcpy(buf, in, n);
155 76933 : buf[n] = 0x80;
156 :
157 76933 : if (n <= 55) {
158 72011 : copy4(buf+56, b);
159 72011 : copy64(M, buf);
160 72011 : mdfour64(&state, M);
161 : } else {
162 4922 : copy4(buf+120, b);
163 4922 : copy64(M, buf);
164 4922 : mdfour64(&state, M);
165 4922 : copy64(M, buf+64);
166 4922 : mdfour64(&state, M);
167 : }
168 :
169 9924357 : for (i=0;i<128;i++)
170 9847424 : buf[i] = 0;
171 76933 : copy64(M, buf);
172 :
173 76933 : copy4(out, state.A);
174 76933 : copy4(out+4, state.B);
175 76933 : copy4(out+8, state.C);
176 76933 : copy4(out+12, state.D);
177 76933 : }
178 :
179 :
|