NCBI C++ ToolKit
des.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2  * Copyright (C) 1998,1999,2000,2001 Nikos Mavroyanopoulos
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Library General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 /* Sofware DES functions
21  * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
22  * the 1977 public-domain program by Jim Gillogly
23  * Modified for additional speed - 6 December 1988 Phil Karn
24  * Modified for parameterized key schedules - Jan 1991 Phil Karn
25  * Callers now allocate a key schedule as follows:
26  * kn = (char (*)[8])malloc(sizeof(char) * 8 * 16);
27  * or
28  * char kn[16][8];
29  */
30 
31 /* modified in order to use the libmcrypt API by Nikos Mavroyanopoulos
32  * All modifications are placed under the license of libmcrypt.
33  */
34 
35 #include <config.h>
36 
37 #if HAVE_STRING_H
38 #include <string.h>
39 #endif /* HAVE_STRING_H */
40 
41 #include <freetds/tds.h>
42 #include <freetds/bytes.h>
43 #include "des.h"
44 
45 void
47 {
48 
49  int i;
50  unsigned char parity;
51 
52  for (i = 0; i < 8; i++) {
53  parity = key[i];
54 
55  parity ^= parity >> 4;
56  parity ^= parity >> 2;
57  parity ^= parity >> 1;
58 
59  key[i] = (key[i] & 0xfe) | (parity & 1);
60  }
61 }
62 
63 #ifndef HAVE_NETTLE
64 
65 static void permute_ip(des_cblock inblock, DES_KEY * key, des_cblock outblock);
66 static void permute_fp(des_cblock inblock, DES_KEY * key, des_cblock outblock);
67 static void perminit_ip(DES_KEY * key);
68 static void spinit(DES_KEY * key);
69 static void perminit_fp(DES_KEY * key);
70 static TDS_UINT f(DES_KEY * key, register TDS_UINT r, register unsigned char *subkey);
71 
72 /* Tables defined in the Data Encryption Standard documents */
73 
74 /* initial permutation IP */
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
84 };
85 
86 /* final permutation IP^-1 */
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
96 };
97 
98 /* expansion operation matrix
99  * This is for reference only; it is unused in the code
100  * as the f() function performs it implicitly for speed
101  */
102 #ifdef notdef
103 static char ei[] = {
104  32, 1, 2, 3, 4, 5,
105  4, 5, 6, 7, 8, 9,
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
112 };
113 #endif
114 
115 /* permuted choice table (key) */
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,
121 
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
126 };
127 
128 /* number left rotations of pc1 */
129 static const char totrot[] = {
130  1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
131 };
132 
133 /* permuted choice key (table) */
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
143 };
144 
145 /* The (in)famous S-boxes */
146 static const char si[8][64] = {
147  /* S1 */
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},
152 
153  /* S2 */
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},
158 
159  /* S3 */
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},
164 
165  /* S4 */
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},
170 
171  /* S5 */
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},
176 
177  /* S6 */
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},
182 
183  /* S7 */
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},
188 
189  /* S8 */
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},
194 
195 };
196 
197 #ifdef notdef
198 /* 32-bit permutation function P used on the output of the S-boxes */
199 static const char p32i[] = {
200  16, 7, 20, 21,
201  29, 12, 28, 17,
202  1, 15, 23, 26,
203  5, 18, 31, 10,
204  2, 8, 24, 14,
205  32, 27, 3, 9,
206  19, 13, 30, 6,
207  22, 11, 4, 25
208 };
209 #endif
210 
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)
222 
223 static const char pbox[32] = {
224  P32I_INDEX( 1),
225  P32I_INDEX( 2),
226  P32I_INDEX( 3),
227  P32I_INDEX( 4),
228  P32I_INDEX( 5),
229  P32I_INDEX( 6),
230  P32I_INDEX( 7),
231  P32I_INDEX( 8),
232  P32I_INDEX( 9),
233  P32I_INDEX(10),
234  P32I_INDEX(11),
235  P32I_INDEX(12),
236  P32I_INDEX(13),
237  P32I_INDEX(14),
238  P32I_INDEX(15),
239  P32I_INDEX(16),
240  P32I_INDEX(17),
241  P32I_INDEX(18),
242  P32I_INDEX(19),
243  P32I_INDEX(20),
244  P32I_INDEX(21),
245  P32I_INDEX(22),
246  P32I_INDEX(23),
247  P32I_INDEX(24),
248  P32I_INDEX(25),
249  P32I_INDEX(26),
250  P32I_INDEX(27),
251  P32I_INDEX(28),
252  P32I_INDEX(29),
253  P32I_INDEX(30),
254  P32I_INDEX(31),
255  P32I_INDEX(32),
256 };
257 
258 /* End of DES-defined tables */
259 
260 /* Lookup tables initialized once only at startup by des_init() */
261 
262 /* bit 0 is left-most in byte */
263 static const int bytebit[] = {
264  0200, 0100, 040, 020, 010, 04, 02, 01
265 };
266 
267 static const int nibblebit[] = {
268  010, 04, 02, 01
269 };
270 
271 /* Allocate space and initialize DES lookup arrays
272  * mode == 0: standard Data Encryption Algorithm
273  */
274 static int
276 {
277 
278  spinit(key);
279  perminit_ip(key);
280  perminit_fp(key);
281 
282  return 0;
283 }
284 
285 
286 /* Set key (initialize key schedule array) */
287 int
288 tds_des_set_key(DES_KEY * dkey, const des_cblock user_key, size_t len)
289 {
290  char pc1m[56]; /* place to modify pc1 into */
291  char pcr[56]; /* place to rotate pc1 into */
292  register int i, j, l;
293  int m;
294 
295  memset(dkey, '\0', sizeof(DES_KEY));
296  des_init(dkey);
297 
298  /* Clear key schedule */
299 
300 
301  for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */
302  l = pc1[j] - 1; /* integer bit location */
303  m = l & 07; /* find bit */
304  pc1m[j] = (user_key[l >> 3] & /* find which key byte l is in */
305  bytebit[m]) /* and which bit of that byte */
306  ? 1 : 0; /* and store 1-bit result */
307 
308  }
309  for (i = 0; i < 16; i++) { /* key chunk for each iteration */
310  for (j = 0; j < 56; j++) /* rotate pc1 the right amount */
311  pcr[j] = pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l - 28];
312  /* rotate left and right halves independently */
313  for (j = 0; j < 48; j++) { /* select bits individually */
314  /* check bit that goes to kn[j] */
315  if (pcr[pc2[j] - 1]) {
316  /* mask it in if it's there */
317  l = j % 6;
318  dkey->kn[i][j / 6] |= bytebit[l] >> 2;
319  }
320  }
321  }
322  return 0;
323 }
324 
325 /* In-place encryption of 64-bit block */
326 void
328 {
329  register TDS_UINT left, right;
330  register unsigned char *knp;
331  TDS_UINT work[2]; /* Working data storage */
332 
333  permute_ip(block, key, (unsigned char *) work); /* Initial Permutation */
334  left = TDS_GET_A4BE(&work[0]);
335  right = TDS_GET_A4BE(&work[1]);
336 
337  /* Do the 16 rounds.
338  * The rounds are numbered from 0 to 15. On even rounds
339  * the right half is fed to f() and the result exclusive-ORs
340  * the left half; on odd rounds the reverse is done.
341  */
342  knp = &key->kn[0][0];
343  left ^= f(key, right, knp);
344  knp += 8;
345  right ^= f(key, left, knp);
346  knp += 8;
347  left ^= f(key, right, knp);
348  knp += 8;
349  right ^= f(key, left, knp);
350  knp += 8;
351  left ^= f(key, right, knp);
352  knp += 8;
353  right ^= f(key, left, knp);
354  knp += 8;
355  left ^= f(key, right, knp);
356  knp += 8;
357  right ^= f(key, left, knp);
358  knp += 8;
359  left ^= f(key, right, knp);
360  knp += 8;
361  right ^= f(key, left, knp);
362  knp += 8;
363  left ^= f(key, right, knp);
364  knp += 8;
365  right ^= f(key, left, knp);
366  knp += 8;
367  left ^= f(key, right, knp);
368  knp += 8;
369  right ^= f(key, left, knp);
370  knp += 8;
371  left ^= f(key, right, knp);
372  knp += 8;
373  right ^= f(key, left, knp);
374 
375  /* Left/right half swap, plus byte swap if little-endian */
376  TDS_PUT_A4BE(&work[0], right);
377  TDS_PUT_A4BE(&work[1], left);
378  permute_fp((unsigned char *) work, key, block); /* Inverse initial permutation */
379 }
380 
381 /* In-place decryption of 64-bit block. This function is the mirror
382  * image of encryption; exactly the same steps are taken, but in
383  * reverse order
384  */
385 #if 0
386 void
387 _mcrypt_decrypt(DES_KEY * key, unsigned char *block)
388 {
389  register TDS_UINT left, right;
390  register unsigned char *knp;
391  TDS_UINT work[2]; /* Working data storage */
392 
393  permute_ip(block, key, (unsigned char *) work); /* Initial permutation */
394 
395  /* Left/right half swap, plus byte swap if little-endian */
396  right = TDS_GET_A4BE(&work[0]);
397  left = TDS_GET_A4BE(&work[1]);
398  /* Do the 16 rounds in reverse order.
399  * The rounds are numbered from 15 to 0. On even rounds
400  * the right half is fed to f() and the result exclusive-ORs
401  * the left half; on odd rounds the reverse is done.
402  */
403  knp = &key->kn[15][0];
404  right ^= f(key, left, knp);
405  knp -= 8;
406  left ^= f(key, right, knp);
407  knp -= 8;
408  right ^= f(key, left, knp);
409  knp -= 8;
410  left ^= f(key, right, knp);
411  knp -= 8;
412  right ^= f(key, left, knp);
413  knp -= 8;
414  left ^= f(key, right, knp);
415  knp -= 8;
416  right ^= f(key, left, knp);
417  knp -= 8;
418  left ^= f(key, right, knp);
419  knp -= 8;
420  right ^= f(key, left, knp);
421  knp -= 8;
422  left ^= f(key, right, knp);
423  knp -= 8;
424  right ^= f(key, left, knp);
425  knp -= 8;
426  left ^= f(key, right, knp);
427  knp -= 8;
428  right ^= f(key, left, knp);
429  knp -= 8;
430  left ^= f(key, right, knp);
431  knp -= 8;
432  right ^= f(key, left, knp);
433  knp -= 8;
434  left ^= f(key, right, knp);
435 
436  TDS_PUT_A4BE(&work[0], left);
437  TDS_PUT_A4BE(&work[1], right);
438  permute_fp((unsigned char *) work, key, block); /* Inverse initial permutation */
439 }
440 #endif
441 
442 /* Permute inblock with perm */
443 static void
445 {
446  register unsigned char *ib, *ob; /* ptr to input or output block */
447  register unsigned char *p, *q;
448  register int j;
449 
450  /* Clear output block */
451  memset(outblock, '\0', 8);
452 
453  ib = inblock;
454  for (j = 0; j < 16; j += 2, ib++) { /* for each input nibble */
455  ob = outblock;
456  p = key->iperm[j][(*ib >> 4) & 0xf];
457  q = key->iperm[j + 1][*ib & 0xf];
458  /* and each output byte, OR the masks together */
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++;
467  }
468 }
469 
470 /* Permute inblock with perm */
471 static void
473 {
474  register unsigned char *ib, *ob; /* ptr to input or output block */
475  register unsigned char *p, *q;
476  register int j;
477 
478  /* Clear output block */
479  memset(outblock, '\0', 8);
480 
481  ib = inblock;
482  for (j = 0; j < 16; j += 2, ib++) { /* for each input nibble */
483  ob = outblock;
484  p = key->fperm[j][(*ib >> 4) & 0xf];
485  q = key->fperm[j + 1][*ib & 0xf];
486  /* and each output byte, OR the masks together */
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++;
495  }
496 }
497 
498 /* The nonlinear function f(r,k), the heart of DES */
499 static TDS_UINT
500 f(DES_KEY * key, register TDS_UINT r, register unsigned char *subkey)
501 {
502  register const TDS_UINT *spp;
503  register TDS_UINT rval, rt;
504  register int er;
505 
506 #ifdef TRACE
508  "f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
509  r, subkey[0], subkey[1], subkey[2], subkey[3], subkey[4],
510  subkey[5], subkey[6], subkey[7]);
511 #endif
512  /* Run E(R) ^ K through the combined S & P boxes.
513  * This code takes advantage of a convenient regularity in
514  * E, namely that each group of 6 bits in E(R) feeding
515  * a single S-box is a contiguous segment of R.
516  */
517  subkey += 7;
518 
519  /* Compute E(R) for each block of 6 bits, and run thru boxes */
520  er = ((int) r << 1) | ((r & 0x80000000) ? 1 : 0);
521  spp = &key->sp[7][0];
522  rval = spp[(er ^ *subkey--) & 0x3f];
523  spp -= 64;
524  rt = (TDS_UINT) r >> 3;
525  rval |= spp[((int) rt ^ *subkey--) & 0x3f];
526  spp -= 64;
527  rt >>= 4;
528  rval |= spp[((int) rt ^ *subkey--) & 0x3f];
529  spp -= 64;
530  rt >>= 4;
531  rval |= spp[((int) rt ^ *subkey--) & 0x3f];
532  spp -= 64;
533  rt >>= 4;
534  rval |= spp[((int) rt ^ *subkey--) & 0x3f];
535  spp -= 64;
536  rt >>= 4;
537  rval |= spp[((int) rt ^ *subkey--) & 0x3f];
538  spp -= 64;
539  rt >>= 4;
540  rval |= spp[((int) rt ^ *subkey--) & 0x3f];
541  spp -= 64;
542  rt >>= 4;
543  rt |= (r & 1) << 5;
544  rval |= spp[((int) rt ^ *subkey) & 0x3f];
545 #ifdef TRACE
546  tdsdump_log(TDS_DBG_FUNC, " %08lx\n", rval);
547 #endif
548  return rval;
549 }
550 
551 /* initialize a perm array */
552 static void
554 {
555  register int l, j, k;
556  int i, m;
557 
558  /* Clear the permutation array */
559  memset(key->iperm, '\0', 16 * 16 * 8);
560 
561  for (i = 0; i < 16; i++) /* each input nibble position */
562  for (j = 0; j < 16; j++) /* each possible input nibble */
563  for (k = 0; k < 64; k++) { /* each output bit position */
564  l = ip[k] - 1; /* where does this bit come from */
565  if ((l >> 2) != i) /* does it come from input posn? */
566  continue; /* if not, bit k is 0 */
567  if (!(j & nibblebit[l & 3]))
568  continue; /* any such bit in input? */
569  m = k & 07; /* which bit is this in the byte */
570  key->iperm[i][j][k >> 3] |= bytebit[m];
571  }
572 }
573 
574 static void
576 {
577  register int l, j, k;
578  int i, m;
579 
580  /* Clear the permutation array */
581  memset(key->fperm, '\0', 16 * 16 * 8);
582 
583  for (i = 0; i < 16; i++) /* each input nibble position */
584  for (j = 0; j < 16; j++) /* each possible input nibble */
585  for (k = 0; k < 64; k++) { /* each output bit position */
586  l = fp[k] - 1; /* where does this bit come from */
587  if ((l >> 2) != i) /* does it come from input posn? */
588  continue; /* if not, bit k is 0 */
589  if (!(j & nibblebit[l & 3]))
590  continue; /* any such bit in input? */
591  m = k & 07; /* which bit is this in the byte */
592  key->fperm[i][j][k >> 3] |= bytebit[m];
593  }
594 }
595 
596 /* Initialize the lookup table for the combined S and P boxes */
597 static void
599 {
600  int i, s, j, rowcol;
601  TDS_UINT val;
602 
603  for (s = 0; s < 8; s++) { /* For each S-box */
604  for (i = 0; i < 64; i++) { /* For each possible input */
605  val = 0;
606  /* The row number is formed from the first and last
607  * bits; the column number is from the middle 4
608  */
609  rowcol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf);
610  for (j = 0; j < 4; j++) { /* For each output bit */
611  if (si[s][rowcol] & (8 >> j)) {
612  val |= 1L << (31 - pbox[4 * s + j]);
613  }
614  }
615  key->sp[s][i] = val;
616  }
617  }
618 }
619 
620 #endif
621 
622 /* ECB MODE */
623 
624 int
625 tds_des_ecb_encrypt(const void *plaintext, size_t len, DES_KEY * akey,
626  unsigned char *output)
627 {
628  size_t j;
629  const unsigned char *plain = (const unsigned char *) plaintext;
630 
631  for (j = 0; j < len / 8; j++) {
632  memcpy(&output[j * 8], &plain[j * 8], 8);
633  tds_des_encrypt(akey, &output[j * 8]);
634  }
635  if (j == 0 && len != 0)
636  return -1; /* no blocks were encrypted */
637  return 0;
638 }
#define TDS_GET_A4BE(ptr)
Definition: bytes.h:76
#define TDS_PUT_A4BE(ptr, val)
Definition: bytes.h:85
static const int nibblebit[]
Definition: des.c:267
static void perminit_ip(DES_KEY *key)
Definition: des.c:553
static const char fp[]
Definition: des.c:87
void tds_des_encrypt(DES_KEY *key, des_cblock block)
Definition: des.c:327
static const char si[8][64]
Definition: des.c:146
static void perminit_fp(DES_KEY *key)
Definition: des.c:575
#define P32I_INDEX(n)
Definition: des.c:213
static int des_init(DES_KEY *key)
Definition: des.c:275
void tds_des_set_odd_parity(des_cblock key)
Definition: des.c:46
static const int bytebit[]
Definition: des.c:263
static const char ip[]
Definition: des.c:75
static const char pc1[]
Definition: des.c:116
static const char pbox[32]
Definition: des.c:223
static void permute_fp(des_cblock inblock, DES_KEY *key, des_cblock outblock)
Definition: des.c:472
static const char pc2[]
Definition: des.c:134
static TDS_UINT f(DES_KEY *key, register TDS_UINT r, register unsigned char *subkey)
Definition: des.c:500
int tds_des_set_key(DES_KEY *dkey, const des_cblock user_key, size_t len)
Definition: des.c:288
int tds_des_ecb_encrypt(const void *plaintext, size_t len, DES_KEY *akey, unsigned char *output)
Definition: des.c:625
static void permute_ip(des_cblock inblock, DES_KEY *key, des_cblock outblock)
Definition: des.c:444
static const char totrot[]
Definition: des.c:129
static void spinit(DES_KEY *key)
Definition: des.c:598
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
unsigned char des_cblock[8]
Definition: des.h:12
int i
int len
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 SQLCHAR output[256]
Definition: print.c:5
Definition: des.h:16
unsigned char kn[16][8]
Definition: des.h:17
Main include file for libtds.
#define tdsdump_log
Definition: tds.h:1561
tds_sysdep_uint32_type TDS_UINT
Definition: tds.h:150
#define TDS_DBG_FUNC
Definition: tds.h:898
Modified on Wed Mar 27 11:21:45 2024 by modify_doxy.py rev. 669887