kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / netproto / ncp / ncp_crypt.c
1 /* 
2  * $FreeBSD: src/sys/netncp/ncp_crypt.c,v 1.3.2.1 2001/02/22 08:57:58 bp Exp $
3  * $DragonFly: src/sys/netproto/ncp/ncp_crypt.c,v 1.3 2003/08/07 21:17:38 dillon Exp $
4  */
5 #include <sys/param.h>
6 #include <sys/systm.h>
7 #include "ncp.h"
8 #include "ncp_subr.h"
9
10 /*
11  *  Routines in this file based on work of Volker Lendecke
12  */
13 /*$*********************************************************
14    $*
15    $* This code has been taken from DDJ 11/93, from an 
16    $* article by Pawel Szczerbina.
17    $*
18    $* Password encryption routines follow.
19    $* Converted to C from Barry Nance's Pascal
20    $* prog published in the March -93 issue of Byte.
21    $*
22    $* Adapted to be useable for ncpfs by 
23    $* Volker Lendecke <lendecke@namu01.gwdg.de> in 
24    $* October 1995. 
25    $*
26    $********************************************************* */
27
28
29
30 typedef unsigned char buf32[32];
31
32 static unsigned char encrypttable[256] = {
33 0x7, 0x8, 0x0, 0x8, 0x6, 0x4, 0xE, 0x4, 0x5, 0xC, 0x1, 0x7, 0xB, 0xF, 0xA, 0x8,
34 0xF, 0x8, 0xC, 0xC, 0x9, 0x4, 0x1, 0xE, 0x4, 0x6, 0x2, 0x4, 0x0, 0xA, 0xB, 0x9,
35 0x2, 0xF, 0xB, 0x1, 0xD, 0x2, 0x1, 0x9, 0x5, 0xE, 0x7, 0x0, 0x0, 0x2, 0x6, 0x6,
36 0x0, 0x7, 0x3, 0x8, 0x2, 0x9, 0x3, 0xF, 0x7, 0xF, 0xC, 0xF, 0x6, 0x4, 0xA, 0x0,
37 0x2, 0x3, 0xA, 0xB, 0xD, 0x8, 0x3, 0xA, 0x1, 0x7, 0xC, 0xF, 0x1, 0x8, 0x9, 0xD,
38 0x9, 0x1, 0x9, 0x4, 0xE, 0x4, 0xC, 0x5, 0x5, 0xC, 0x8, 0xB, 0x2, 0x3, 0x9, 0xE,
39 0x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56 };
57
58 /*
59  * Create table-based 16-bytes hash from a 32-bytes array
60  */
61 static void
62 nw_hash(buf32 temp, unsigned char *target) {
63         short sum;
64         unsigned char b3;
65         int s, b2, i;
66
67         sum = 0;
68
69         for (b2 = 0; b2 <= 1; ++b2) {
70                 for (s = 0; s <= 31; ++s) {
71                         b3 = (temp[s] + sum) ^ (temp[(s + sum) & 31] - encryptkeys[s]);
72                         sum += b3;
73                         temp[s] = b3;
74                 }
75         }
76
77         for (i = 0; i <= 15; ++i) {
78                 target[i] = encrypttable[temp[2 * i]]
79                     | (encrypttable[temp[2 * i + 1]] << 4);
80         }
81 }
82
83
84 /*
85  * Create a 16-bytes pattern from given buffer based on a four bytes key
86  */
87 void
88 nw_keyhash(const u_char *key, const u_char *buf, int buflen, u_char *target) {
89         int b2, d, s;
90         buf32 temp;
91
92         while (buflen > 0 && buf[buflen - 1] == 0)
93                 buflen--;
94
95         bzero(temp, sizeof(temp));
96
97         d = 0;
98         while (buflen >= 32) {
99                 for (s = 0; s <= 31; ++s)
100                         temp[s] ^= buf[d++];
101                 buflen -= 32;
102         }
103         b2 = d;
104         if (buflen > 0) {
105                 for (s = 0; s <= 31; ++s) {
106                         if (d + buflen == b2) {
107                                 temp[s] ^= encryptkeys[s];
108                                 b2 = d;
109                         } else
110                                 temp[s] ^= buf[b2++];
111                 }
112         }
113         for (s = 0; s <= 31; ++s)
114                 temp[s] ^= key[s & 3];
115
116         nw_hash(temp, target);
117 }
118
119 /*
120  * Create an 8-bytes pattern from an 8-bytes key and 16-bytes of data
121  */
122 void
123 nw_encrypt(const u_char *fra, const u_char *buf, u_char *target) {
124         buf32 k;
125         int s;
126
127         nw_keyhash(fra, buf, 16, k);
128         nw_keyhash(fra + 4, buf, 16, k + 16);
129
130         for (s = 0; s < 16; s++)
131                 k[s] ^= k[31 - s];
132
133         for (s = 0; s < 8; s++)
134                 *target++ = k[s] ^ k[15 - s];
135 }
136
137 #ifdef _KERNEL
138 /*
139  * MD4 routine taken from libmd sources
140  */
141 typedef u_int32_t       UINT4;
142 typedef unsigned char *POINTER;
143
144 #define PROTO_LIST(list) list
145
146 static void Decode PROTO_LIST
147   ((UINT4 *, const unsigned char *, unsigned int));
148
149 /* Constants for MD4Transform routine.
150  */
151 #define S11 3
152 #define S12 7
153 #define S13 11
154 #define S14 19
155 #define S21 3
156 #define S22 5
157 #define S23 9
158 #define S24 13
159 #define S31 3
160 #define S32 9
161 #define S33 11
162 #define S34 15
163
164 /* F, G and H are basic MD4 functions.
165  */
166 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
167 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
168 #define H(x, y, z) ((x) ^ (y) ^ (z))
169
170 /* ROTATE_LEFT rotates x left n bits.
171  */
172 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
173
174 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
175 /* Rotation is separate from addition to prevent recomputation */
176 #define FF(a, b, c, d, x, s) { \
177     (a) += F ((b), (c), (d)) + (x); \
178     (a) = ROTATE_LEFT ((a), (s)); \
179   }
180 #define GG(a, b, c, d, x, s) { \
181     (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
182     (a) = ROTATE_LEFT ((a), (s)); \
183   }
184 #define HH(a, b, c, d, x, s) { \
185     (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
186     (a) = ROTATE_LEFT ((a), (s)); \
187   }
188
189 void
190 ncp_sign(const u_int32_t *state, const char *block, u_int32_t *ostate) {
191         UINT4 a, b, c, d, x[16];
192
193         Decode (x, block, 64);
194         a = state[0];
195         b = state[1];
196         c = state[2];
197         d = state[3];
198         /* Round 1 */
199         FF (a, b, c, d, x[ 0], S11); /* 1 */
200         FF (d, a, b, c, x[ 1], S12); /* 2 */
201         FF (c, d, a, b, x[ 2], S13); /* 3 */
202         FF (b, c, d, a, x[ 3], S14); /* 4 */
203         FF (a, b, c, d, x[ 4], S11); /* 5 */
204         FF (d, a, b, c, x[ 5], S12); /* 6 */
205         FF (c, d, a, b, x[ 6], S13); /* 7 */
206         FF (b, c, d, a, x[ 7], S14); /* 8 */
207         FF (a, b, c, d, x[ 8], S11); /* 9 */
208         FF (d, a, b, c, x[ 9], S12); /* 10 */
209         FF (c, d, a, b, x[10], S13); /* 11 */
210         FF (b, c, d, a, x[11], S14); /* 12 */
211         FF (a, b, c, d, x[12], S11); /* 13 */
212         FF (d, a, b, c, x[13], S12); /* 14 */
213         FF (c, d, a, b, x[14], S13); /* 15 */
214         FF (b, c, d, a, x[15], S14); /* 16 */
215
216         /* Round 2 */
217         GG (a, b, c, d, x[ 0], S21); /* 17 */
218         GG (d, a, b, c, x[ 4], S22); /* 18 */
219         GG (c, d, a, b, x[ 8], S23); /* 19 */
220         GG (b, c, d, a, x[12], S24); /* 20 */
221         GG (a, b, c, d, x[ 1], S21); /* 21 */
222         GG (d, a, b, c, x[ 5], S22); /* 22 */
223         GG (c, d, a, b, x[ 9], S23); /* 23 */
224         GG (b, c, d, a, x[13], S24); /* 24 */
225         GG (a, b, c, d, x[ 2], S21); /* 25 */
226         GG (d, a, b, c, x[ 6], S22); /* 26 */
227         GG (c, d, a, b, x[10], S23); /* 27 */
228         GG (b, c, d, a, x[14], S24); /* 28 */
229         GG (a, b, c, d, x[ 3], S21); /* 29 */
230         GG (d, a, b, c, x[ 7], S22); /* 30 */
231         GG (c, d, a, b, x[11], S23); /* 31 */
232         GG (b, c, d, a, x[15], S24); /* 32 */
233
234         /* Round 3 */
235         HH (a, b, c, d, x[ 0], S31); /* 33 */
236         HH (d, a, b, c, x[ 8], S32); /* 34 */
237         HH (c, d, a, b, x[ 4], S33); /* 35 */
238         HH (b, c, d, a, x[12], S34); /* 36 */
239         HH (a, b, c, d, x[ 2], S31); /* 37 */
240         HH (d, a, b, c, x[10], S32); /* 38 */
241         HH (c, d, a, b, x[ 6], S33); /* 39 */
242         HH (b, c, d, a, x[14], S34); /* 40 */
243         HH (a, b, c, d, x[ 1], S31); /* 41 */
244         HH (d, a, b, c, x[ 9], S32); /* 42 */
245         HH (c, d, a, b, x[ 5], S33); /* 43 */
246         HH (b, c, d, a, x[13], S34); /* 44 */
247         HH (a, b, c, d, x[ 3], S31); /* 45 */
248         HH (d, a, b, c, x[11], S32); /* 46 */
249         HH (c, d, a, b, x[ 7], S33); /* 47 */
250         HH (b, c, d, a, x[15], S34); /* 48 */
251
252         ostate[0] = state[0] + a;
253         ostate[1] = state[1] + b;
254         ostate[2] = state[2] + c;
255         ostate[3] = state[3] + d;
256 }
257
258 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
259      a multiple of 4.
260  */
261 static void Decode (output, input, len)
262
263 UINT4 *output;
264 const unsigned char *input;
265 unsigned int len;
266 {
267   unsigned int i, j;
268
269   for (i = 0, j = 0; j < len; i++, j += 4)
270     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
271       (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
272 }
273
274 #endif /* _KERNEL */