43 #include <QtCore/qendian.h>
45 static void permute_ip (
unsigned char *inblock,
DES_KEY * key,
unsigned char *outblock);
46 static void permute_fp (
unsigned char *inblock,
DES_KEY * key,
unsigned char *outblock);
56 static const char ip[] = {
57 58, 50, 42, 34, 26, 18, 10, 2,
58 60, 52, 44, 36, 28, 20, 12, 4,
59 62, 54, 46, 38, 30, 22, 14, 6,
60 64, 56, 48, 40, 32, 24, 16, 8,
61 57, 49, 41, 33, 25, 17, 9, 1,
62 59, 51, 43, 35, 27, 19, 11, 3,
63 61, 53, 45, 37, 29, 21, 13, 5,
64 63, 55, 47, 39, 31, 23, 15, 7
68 static const char fp[] = {
69 40, 8, 48, 16, 56, 24, 64, 32,
70 39, 7, 47, 15, 55, 23, 63, 31,
71 38, 6, 46, 14, 54, 22, 62, 30,
72 37, 5, 45, 13, 53, 21, 61, 29,
73 36, 4, 44, 12, 52, 20, 60, 28,
74 35, 3, 43, 11, 51, 19, 59, 27,
75 34, 2, 42, 10, 50, 18, 58, 26,
76 33, 1, 41, 9, 49, 17, 57, 25
84 static const char ei[] = {
88 12, 13, 14, 15, 16, 17,
89 16, 17, 18, 19, 20, 21,
90 20, 21, 22, 23, 24, 25,
91 24, 25, 26, 27, 28, 29,
97 static const char pc1[] = {
98 57, 49, 41, 33, 25, 17, 9,
99 1, 58, 50, 42, 34, 26, 18,
100 10, 2, 59, 51, 43, 35, 27,
101 19, 11, 3, 60, 52, 44, 36,
103 63, 55, 47, 39, 31, 23, 15,
104 7, 62, 54, 46, 38, 30, 22,
105 14, 6, 61, 53, 45, 37, 29,
106 21, 13, 5, 28, 20, 12, 4
111 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
115 static const char pc2[] = {
116 14, 17, 11, 24, 1, 5,
117 3, 28, 15, 6, 21, 10,
118 23, 19, 12, 4, 26, 8,
119 16, 7, 27, 20, 13, 2,
120 41, 52, 31, 37, 47, 55,
121 30, 40, 51, 45, 33, 48,
122 44, 49, 39, 56, 34, 53,
123 46, 42, 50, 36, 29, 32
127 static const char si[8][64] = {
129 {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
130 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
131 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
132 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13},
135 {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
136 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
137 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
138 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
141 {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
142 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
143 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
144 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
147 {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
148 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
149 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
150 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
153 {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
154 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
155 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
156 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
159 {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
160 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
161 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
162 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
165 {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
166 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
167 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
168 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
171 {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
172 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
173 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
174 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11},
196 0200, 0100, 040, 020, 010, 04, 02, 01
227 memset(dkey, 0,
sizeof (
DES_KEY));
233 for (j = 0; j < 56; ++j)
237 pc1m[j] = (user_key[l >> 3] &
242 for (i = 0; i < 16; ++i)
244 for (j = 0; j < 56; ++j)
245 pcr[j] = pc1m[(l = j +
totrot[i]) < (j < 28 ? 28 : 56) ? l : l - 28];
247 for (j = 0; j < 48; ++j)
269 permute_ip (block, key, (
unsigned char *) work);
270 left = qFromBigEndian(work[0]);
271 right = qFromBigEndian(work[1]);
278 knp = &key->
kn[0][0];
279 left ^=
f (key, right, knp);
281 right ^=
f (key, left, knp);
283 left ^=
f (key, right, knp);
285 right ^=
f (key, left, knp);
287 left ^=
f (key, right, knp);
289 right ^=
f (key, left, knp);
291 left ^=
f (key, right, knp);
293 right ^=
f (key, left, knp);
295 left ^=
f (key, right, knp);
297 right ^=
f (key, left, knp);
299 left ^=
f (key, right, knp);
301 right ^=
f (key, left, knp);
303 left ^=
f (key, right, knp);
305 right ^=
f (key, left, knp);
307 left ^=
f (key, right, knp);
309 right ^=
f (key, left, knp);
312 work[1] = qToBigEndian( left );
313 work[0] = qToBigEndian( right );
315 permute_fp ((
unsigned char *) work, key, block);
322 unsigned char *ib, *ob;
327 memset(outblock, 0, 8);
330 for (j = 0; j < 16; j += 2, ++ib)
333 p = key->
iperm[j][(*ib >> 4) & 0xf];
334 q = key->
iperm[j + 1][*ib & 0xf];
336 *ob++ |= *p++ | *q++;
337 *ob++ |= *p++ | *q++;
338 *ob++ |= *p++ | *q++;
339 *ob++ |= *p++ | *q++;
340 *ob++ |= *p++ | *q++;
341 *ob++ |= *p++ | *q++;
342 *ob++ |= *p++ | *q++;
343 *ob++ |= *p++ | *q++;
351 unsigned char *ib, *ob;
356 memset(outblock, 0, 8);
359 for (j = 0; j < 16; j += 2, ++ib)
362 p = key->
fperm[j][(*ib >> 4) & 0xf];
363 q = key->
fperm[j + 1][*ib & 0xf];
365 *ob++ |= *p++ | *q++;
366 *ob++ |= *p++ | *q++;
367 *ob++ |= *p++ | *q++;
368 *ob++ |= *p++ | *q++;
369 *ob++ |= *p++ | *q++;
370 *ob++ |= *p++ | *q++;
371 *ob++ |= *p++ | *q++;
372 *ob++ |= *p++ | *q++;
385 printf (
"f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
387 subkey[0], subkey[1], subkey[2],
388 subkey[3], subkey[4], subkey[5], subkey[6], subkey[7]);
398 er = ((int) r << 1) | ((r & 0x80000000) ? 1 : 0);
399 spp = &key->
sp[7][0];
400 rval = spp[(er ^ *subkey--) & 0x3f];
403 rval |= spp[((int) rt ^ *subkey--) & 0x3f];
406 rval |= spp[((int) rt ^ *subkey--) & 0x3f];
409 rval |= spp[((int) rt ^ *subkey--) & 0x3f];
412 rval |= spp[((int) rt ^ *subkey--) & 0x3f];
415 rval |= spp[((int) rt ^ *subkey--) & 0x3f];
418 rval |= spp[((int) rt ^ *subkey--) & 0x3f];
422 rval |= spp[((int) rt ^ *subkey) & 0x3f];
424 printf (
" %08lx\n", rval);
437 memset(key->
iperm, 0, 16 * 16 * 8);
439 for (i = 0; i < 16; ++i)
440 for (j = 0; j < 16; ++j)
441 for (k = 0; k < 64; ++k)
460 memset(key->
fperm, 0, 16 * 16 * 8);
462 for (i = 0; i < 16; ++i)
463 for (j = 0; j < 16; ++j)
464 for (k = 0; k < 64; ++k)
481 int p, i, s, j, rowcol;
487 for (p = 0; p < 32; ++p)
489 for (i = 0; i < 32; ++i)
491 if (
p32i[i] - 1 == p)
498 for (s = 0; s < 8; ++s)
500 for (i = 0; i < 64; ++i)
506 rowcol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf);
507 for (j = 0; j < 4; j++)
509 if (
si[s][rowcol] & (8 >> j))
511 val |= 1L << (31 - pbox[4 * s + j]);
521 unsigned char output[8])
524 const unsigned char *plain = (
const unsigned char *) plaintext;
526 for (j = 0; j < len / 8; ++j)
528 memcpy (&output[j * 8], &plain[j * 8], 8);
532 if (j == 0 && len != 0)
static void permute_ip(unsigned char *inblock, DES_KEY *key, unsigned char *outblock)
static quint32 f(DES_KEY *key, quint32 r, char *subkey)
static void perminit_fp(DES_KEY *key)
static void permute_fp(unsigned char *inblock, DES_KEY *key, unsigned char *outblock)
static void ntlm_des_encrypt(DES_KEY *key, unsigned char *block)
static const int bytebit[]
int ntlm_des_set_key(DES_KEY *dkey, char *user_key, int)
static const char si[8][64]
static void perminit_ip(DES_KEY *key)
static void spinit(DES_KEY *key)
static int desinit(DES_KEY *key)
static const char totrot[]
static const int nibblebit[]
int ntlm_des_ecb_encrypt(const void *plaintext, int len, DES_KEY *akey, unsigned char output[8])