41 #include <freetds/tds.h>
42 #include <freetds/bytes.h>
52 for (
i = 0;
i < 8;
i++) {
55 parity ^= parity >> 4;
56 parity ^= parity >> 2;
57 parity ^= parity >> 1;
59 key[
i] = (
key[
i] & 0xfe) | (parity & 1);
75 static const char ip[] = {
76 58, 50, 42, 34, 26, 18, 10, 2,
77 60, 52, 44, 36, 28, 20, 12, 4,
78 62, 54, 46, 38, 30, 22, 14, 6,
79 64, 56, 48, 40, 32, 24, 16, 8,
80 57, 49, 41, 33, 25, 17, 9, 1,
81 59, 51, 43, 35, 27, 19, 11, 3,
82 61, 53, 45, 37, 29, 21, 13, 5,
83 63, 55, 47, 39, 31, 23, 15, 7
87 static const char fp[] = {
88 40, 8, 48, 16, 56, 24, 64, 32,
89 39, 7, 47, 15, 55, 23, 63, 31,
90 38, 6, 46, 14, 54, 22, 62, 30,
91 37, 5, 45, 13, 53, 21, 61, 29,
92 36, 4, 44, 12, 52, 20, 60, 28,
93 35, 3, 43, 11, 51, 19, 59, 27,
94 34, 2, 42, 10, 50, 18, 58, 26,
95 33, 1, 41, 9, 49, 17, 57, 25
106 8, 9, 10, 11, 12, 13,
107 12, 13, 14, 15, 16, 17,
108 16, 17, 18, 19, 20, 21,
109 20, 21, 22, 23, 24, 25,
110 24, 25, 26, 27, 28, 29,
111 28, 29, 30, 31, 32, 1
116 static const char pc1[] = {
117 57, 49, 41, 33, 25, 17, 9,
118 1, 58, 50, 42, 34, 26, 18,
119 10, 2, 59, 51, 43, 35, 27,
120 19, 11, 3, 60, 52, 44, 36,
122 63, 55, 47, 39, 31, 23, 15,
123 7, 62, 54, 46, 38, 30, 22,
124 14, 6, 61, 53, 45, 37, 29,
125 21, 13, 5, 28, 20, 12, 4
130 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
134 static const char pc2[] = {
135 14, 17, 11, 24, 1, 5,
136 3, 28, 15, 6, 21, 10,
137 23, 19, 12, 4, 26, 8,
138 16, 7, 27, 20, 13, 2,
139 41, 52, 31, 37, 47, 55,
140 30, 40, 51, 45, 33, 48,
141 44, 49, 39, 56, 34, 53,
142 46, 42, 50, 36, 29, 32
146 static const char si[8][64] = {
148 {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
149 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
150 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
151 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13},
154 {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
155 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
156 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
157 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
160 {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
161 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
162 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
163 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
166 {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
167 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
168 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
169 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
172 {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
173 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
174 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
175 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
178 {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
179 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
180 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
181 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
184 {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
185 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
186 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
187 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
190 {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
191 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
192 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
193 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11},
199 static const char p32i[] = {
211 #define P32I_INDEX_ROW(n,i,a,b,c,d) \
212 n==a ? 0+i : n==b ? 1+i : n==c ? 2+i : n==d ? 3+i
213 #define P32I_INDEX(n) \
214 (P32I_INDEX_ROW(n, 0, 16, 7, 20, 21) :\
215 P32I_INDEX_ROW(n, 4, 29, 12, 28, 17) :\
216 P32I_INDEX_ROW(n, 8, 1, 15, 23, 26) :\
217 P32I_INDEX_ROW(n,12, 5, 18, 31, 10) :\
218 P32I_INDEX_ROW(n,16, 2, 8, 24, 14) :\
219 P32I_INDEX_ROW(n,20, 32, 27, 3, 9) :\
220 P32I_INDEX_ROW(n,24, 19, 13, 30, 6) :\
221 P32I_INDEX_ROW(n,28, 22, 11, 4, 25) : 0x7f)
264 0200, 0100, 040, 020, 010, 04, 02, 01
292 register int i, j,
l;
295 memset(dkey,
'\0',
sizeof(
DES_KEY));
301 for (j = 0; j < 56; j++) {
304 pc1m[j] = (user_key[
l >> 3] &
309 for (
i = 0;
i < 16;
i++) {
310 for (j = 0; j < 56; j++)
311 pcr[j] = pc1m[(
l = j +
totrot[
i]) < (j < 28 ? 28 : 56) ?
l :
l - 28];
313 for (j = 0; j < 48; j++) {
315 if (pcr[
pc2[j] - 1]) {
330 register unsigned char *knp;
342 knp = &
key->kn[0][0];
343 left ^=
f(
key, right, knp);
345 right ^=
f(
key, left, knp);
347 left ^=
f(
key, right, knp);
349 right ^=
f(
key, left, knp);
351 left ^=
f(
key, right, knp);
353 right ^=
f(
key, left, knp);
355 left ^=
f(
key, right, knp);
357 right ^=
f(
key, left, knp);
359 left ^=
f(
key, right, knp);
361 right ^=
f(
key, left, knp);
363 left ^=
f(
key, right, knp);
365 right ^=
f(
key, left, knp);
367 left ^=
f(
key, right, knp);
369 right ^=
f(
key, left, knp);
371 left ^=
f(
key, right, knp);
373 right ^=
f(
key, left, knp);
387 _mcrypt_decrypt(
DES_KEY *
key,
unsigned char *block)
390 register unsigned char *knp;
403 knp = &
key->kn[15][0];
404 right ^=
f(
key, left, knp);
406 left ^=
f(
key, right, knp);
408 right ^=
f(
key, left, knp);
410 left ^=
f(
key, right, knp);
412 right ^=
f(
key, left, knp);
414 left ^=
f(
key, right, knp);
416 right ^=
f(
key, left, knp);
418 left ^=
f(
key, right, knp);
420 right ^=
f(
key, left, knp);
422 left ^=
f(
key, right, knp);
424 right ^=
f(
key, left, knp);
426 left ^=
f(
key, right, knp);
428 right ^=
f(
key, left, knp);
430 left ^=
f(
key, right, knp);
432 right ^=
f(
key, left, knp);
434 left ^=
f(
key, right, knp);
446 register unsigned char *ib, *ob;
447 register unsigned char *p, *q;
451 memset(outblock,
'\0', 8);
454 for (j = 0; j < 16; j += 2, ib++) {
456 p =
key->iperm[j][(*ib >> 4) & 0xf];
457 q =
key->iperm[j + 1][*ib & 0xf];
459 *ob++ |= *p++ | *q++;
460 *ob++ |= *p++ | *q++;
461 *ob++ |= *p++ | *q++;
462 *ob++ |= *p++ | *q++;
463 *ob++ |= *p++ | *q++;
464 *ob++ |= *p++ | *q++;
465 *ob++ |= *p++ | *q++;
466 *ob++ |= *p++ | *q++;
474 register unsigned char *ib, *ob;
475 register unsigned char *p, *q;
479 memset(outblock,
'\0', 8);
482 for (j = 0; j < 16; j += 2, ib++) {
484 p =
key->fperm[j][(*ib >> 4) & 0xf];
485 q =
key->fperm[j + 1][*ib & 0xf];
487 *ob++ |= *p++ | *q++;
488 *ob++ |= *p++ | *q++;
489 *ob++ |= *p++ | *q++;
490 *ob++ |= *p++ | *q++;
491 *ob++ |= *p++ | *q++;
492 *ob++ |= *p++ | *q++;
493 *ob++ |= *p++ | *q++;
494 *ob++ |= *p++ | *q++;
508 "f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
520 er = ((
int)
r << 1) | ((
r & 0x80000000) ? 1 : 0);
521 spp = &
key->sp[7][0];
522 rval = spp[(er ^ *
subkey--) & 0x3f];
525 rval |= spp[((
int) rt ^ *
subkey--) & 0x3f];
528 rval |= spp[((
int) rt ^ *
subkey--) & 0x3f];
531 rval |= spp[((
int) rt ^ *
subkey--) & 0x3f];
534 rval |= spp[((
int) rt ^ *
subkey--) & 0x3f];
537 rval |= spp[((
int) rt ^ *
subkey--) & 0x3f];
540 rval |= spp[((
int) rt ^ *
subkey--) & 0x3f];
544 rval |= spp[((
int) rt ^ *
subkey) & 0x3f];
555 register int l, j, k;
559 memset(
key->iperm,
'\0', 16 * 16 * 8);
561 for (
i = 0;
i < 16;
i++)
562 for (j = 0; j < 16; j++)
563 for (k = 0; k < 64; k++) {
577 register int l, j, k;
581 memset(
key->fperm,
'\0', 16 * 16 * 8);
583 for (
i = 0;
i < 16;
i++)
584 for (j = 0; j < 16; j++)
585 for (k = 0; k < 64; k++) {
603 for (s = 0; s < 8; s++) {
604 for (
i = 0;
i < 64;
i++) {
609 rowcol = (
i & 32) | ((
i & 1) ? 16 : 0) | ((
i >> 1) & 0xf);
610 for (j = 0; j < 4; j++) {
611 if (
si[s][rowcol] & (8 >> j)) {
612 val |= 1L << (31 -
pbox[4 * s + j]);
629 const unsigned char *plain = (
const unsigned char *) plaintext;
631 for (j = 0; j <
len / 8; j++) {
632 memcpy(&
output[j * 8], &plain[j * 8], 8);
635 if (j == 0 &&
len != 0)
static const int nibblebit[]
static void perminit_ip(DES_KEY *key)
void tds_des_encrypt(DES_KEY *key, des_cblock block)
static const char si[8][64]
static void perminit_fp(DES_KEY *key)
static int des_init(DES_KEY *key)
void tds_des_set_odd_parity(des_cblock key)
static const int bytebit[]
static const char pbox[32]
static void permute_fp(des_cblock inblock, DES_KEY *key, des_cblock outblock)
static TDS_UINT f(DES_KEY *key, register TDS_UINT r, register unsigned char *subkey)
int tds_des_set_key(DES_KEY *dkey, const des_cblock user_key, size_t len)
int tds_des_ecb_encrypt(const void *plaintext, size_t len, DES_KEY *akey, unsigned char *output)
static void permute_ip(des_cblock inblock, DES_KEY *key, des_cblock outblock)
static const char totrot[]
static void spinit(DES_KEY *key)
#define TDS_GET_A4BE(ptr)
#define TDS_PUT_A4BE(ptr, val)
tds_sysdep_uint32_type TDS_UINT
static SQLCHAR output[256]
unsigned int
A callback function used to compare two keys in a database.
unsigned char des_cblock[8]
const struct ncbi::grid::netcache::search::fields::KEY key
const struct ncbi::grid::netcache::search::fields::SUBKEY subkey
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
static SLJIT_INLINE sljit_ins l(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)