home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xntp3.zip / authstuff / makePC1.c < prev    next >
C/C++ Source or Header  |  1989-07-25  |  5KB  |  279 lines

  1. /*
  2.  * makePC1 - build custom permutted choice 1 tables
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7.  
  8. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  9.  
  10. char *progname;
  11. int debug;
  12.  
  13. /*
  14.  * main - parse arguments and handle options
  15.  */
  16. main(argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     int c;
  21.     int errflg = 0;
  22.     extern int optind;
  23.     extern char *optarg;
  24.  
  25.     progname = argv[0];
  26.     while ((c = getopt(argc, argv, "d")) != EOF)
  27.         switch (c) {
  28.         case 'd':
  29.             ++debug;
  30.             break;
  31.         default:
  32.             errflg++;
  33.             break;
  34.         }
  35.     if (errflg) {
  36.         (void) fprintf(stderr, "usage: %s [-d]\n", progname);
  37.         exit(2);
  38.     }
  39.     doit();
  40.     exit(0);
  41. }
  42.  
  43. /*
  44.  * Permuted choice 1 table, to produce the initial C.  This table
  45.  * has had 1 subtracted from it to give it a zero base.
  46.  */
  47. static u_char PC1_C[28] = {
  48.     56, 48, 40, 32, 24, 16,  8,
  49.      0, 57, 49, 41, 33, 25, 17,
  50.      9,  1, 58, 50, 42, 34, 26,
  51.     18, 10,  2, 59, 51, 43, 35
  52. };
  53.  
  54. /*
  55.  * Permuted choice 1 table, to produce the initial D.  Again, 1 has
  56.  * been subtracted to match C language zero base arrays.
  57.  */
  58. static u_char PC1_D[28] = {
  59.     62, 54, 46, 38, 30, 22, 14,
  60.      6, 61, 53, 45, 37, 29, 21,
  61.     13,  5, 60, 52, 44, 36, 28,
  62.     20, 12,  4, 27, 19, 11,  3
  63. };
  64.  
  65. /*
  66.  * permute - produce c and d for the given bits
  67.  */
  68. permute(bits, cp, dp)
  69.     u_char *bits;
  70.     u_long *cp;
  71.     u_long *dp;
  72. {
  73.     register int i;
  74.     register u_long mask;
  75.     u_char c[28];
  76.     u_char d[28];
  77.  
  78.     bzero((char *)c, sizeof c);
  79.     bzero((char *)d, sizeof d);
  80.  
  81.     for (i = 0; i < 28; i++) {
  82.         c[i] = bits[PC1_C[i]];
  83.         d[i] = bits[PC1_D[i]];
  84.     }
  85.  
  86.     mask = 0x10000000;
  87.     *cp = *dp = 0;
  88.     for (i = 0; i < 28; i++) {
  89.         mask >>= 1;
  90.         if (c[i])
  91.             *cp |= mask;
  92.         if (d[i])
  93.             *dp |= mask;
  94.     }
  95. }
  96.  
  97.  
  98. /*
  99.  * bits from the left part of the key used to form the C subkey
  100.  */
  101. int lc3[4] = { 0, 8, 16, 24 };
  102.  
  103. /*
  104.  * bits from the left part of the key used to form the D subkey
  105.  */
  106. int ld4[4] = { 3, 11, 19, 27 };
  107.  
  108. /*
  109.  * bits from the right part of the key used to form the C subkey
  110.  */
  111. int rc4[4] = { 32, 40, 48, 56 };
  112.  
  113. /*
  114.  * bits from the right part of the key used to form the D subkey
  115.  */
  116. int rd3[4] = { 36, 44, 52, 60 };
  117.  
  118. u_long PC_CL[8];
  119. u_long PC_DL[16];
  120. u_long PC_CR[16];
  121. u_long PC_DR[8];
  122.  
  123.  
  124. /*
  125.  * doit - compute and print the four PC1 tables
  126.  */
  127. doit()
  128. {
  129.     int i;
  130.     int comb;
  131.     u_long c;
  132.     u_long d;
  133.     u_char bits[64];
  134.  
  135.     bzero((char *)bits, sizeof bits);
  136.  
  137.     printf("static u_long PC1_CL[8] = {");
  138.     for (i = 0; i < 4; i++) {
  139.         for (comb = 0; comb < 8; comb++) {
  140.             if (comb & 0x4)
  141.                 bits[lc3[i]] = 1;
  142.             if (comb & 0x2)
  143.                 bits[lc3[i]+1] = 1;
  144.             if (comb & 0x1)
  145.                 bits[lc3[i]+2] = 1;
  146.             permute(bits, &c, &d);
  147.             bits[lc3[i]] = 0;
  148.             bits[lc3[i]+1] = 0;
  149.             bits[lc3[i]+2] = 0;
  150.             if (d != 0) {
  151.                 (void) fprintf(stderr,
  152.                     "Error PC_CL i %d comb %d\n", i, comb);
  153.             }
  154.             if (i == 0) {
  155.                 PC_CL[comb] = c;
  156.                 if ((comb & 0x3) == 0)
  157.                     printf("\n\t0x%08x,", c);
  158.                 else if (comb == 7)
  159.                     printf(" 0x%08x\n};\n\n", c);
  160.                 else
  161.                     printf(" 0x%08x,", c);
  162.             } else {
  163.                 if (c != PC_CL[comb] << i)
  164.                     (void) fprintf(stderr,
  165.                         "Error PC_CL 0x%08x c 0x%08x\n",
  166.                         PC_CL[comb], c);
  167.             }
  168.         }
  169.     }
  170.  
  171.     printf("static u_long PC1_DL[16] = {");
  172.     for (i = 0; i < 4; i++) {
  173.         for (comb = 0; comb < 16; comb++) {
  174.             if (comb & 0x8)
  175.                 bits[ld4[i]] = 1;
  176.             if (comb & 0x4)
  177.                 bits[ld4[i]+1] = 1;
  178.             if (comb & 0x2)
  179.                 bits[ld4[i]+2] = 1;
  180.             if (comb & 0x1)
  181.                 bits[ld4[i]+3] = 1;
  182.             permute(bits, &c, &d);
  183.             bits[ld4[i]] = 0;
  184.             bits[ld4[i]+1] = 0;
  185.             bits[ld4[i]+2] = 0;
  186.             bits[ld4[i]+3] = 0;
  187.             if (c != 0) {
  188.                 (void) fprintf(stderr,
  189.                     "Error PC_DL i %d comb %d\n", i, comb);
  190.             }
  191.             if (i == 0) {
  192.                 PC_DL[comb] = d;
  193.                 if ((comb & 0x3) == 0)
  194.                     printf("\n\t0x%08x,", d);
  195.                 else if (comb == 15)
  196.                     printf(" 0x%08x\n};\n\n", d);
  197.                 else
  198.                     printf(" 0x%08x,", d);
  199.             } else {
  200.                 if (d != PC_DL[comb] << i)
  201.                     (void) fprintf(stderr,
  202.                         "Error PC_DL 0x%08x c 0x%08x\n",
  203.                         PC_DL[comb], d);
  204.             }
  205.         }
  206.     }
  207.  
  208.     printf("static u_long PC1_CR[16] = {");
  209.     for (i = 0; i < 4; i++) {
  210.         for (comb = 0; comb < 16; comb++) {
  211.             if (comb & 0x8)
  212.                 bits[rc4[i]] = 1;
  213.             if (comb & 0x4)
  214.                 bits[rc4[i]+1] = 1;
  215.             if (comb & 0x2)
  216.                 bits[rc4[i]+2] = 1;
  217.             if (comb & 0x1)
  218.                 bits[rc4[i]+3] = 1;
  219.             permute(bits, &c, &d);
  220.             bits[rc4[i]] = 0;
  221.             bits[rc4[i]+1] = 0;
  222.             bits[rc4[i]+2] = 0;
  223.             bits[rc4[i]+3] = 0;
  224.             if (d != 0) {
  225.                 (void) fprintf(stderr,
  226.                     "Error PC_CR i %d comb %d\n", i, comb);
  227.             }
  228.             if (i == 0) {
  229.                 PC_CR[comb] = c;
  230.                 if ((comb & 0x3) == 0)
  231.                     printf("\n\t0x%08x,", c);
  232.                 else if (comb == 15)
  233.                     printf(" 0x%08x\n};\n\n", c);
  234.                 else
  235.                     printf(" 0x%08x,", c);
  236.             } else {
  237.                 if (c != PC_CR[comb] << i)
  238.                     (void) fprintf(stderr,
  239.                         "Error PC_CR 0x%08x c 0x%08x\n",
  240.                         PC_CR[comb], c);
  241.             }
  242.         }
  243.     }
  244.  
  245.     printf("static u_long PC1_DR[8] = {");
  246.     for (i = 0; i < 4; i++) {
  247.         for (comb = 0; comb < 8; comb++) {
  248.             if (comb & 0x4)
  249.                 bits[rd3[i]] = 1;
  250.             if (comb & 0x2)
  251.                 bits[rd3[i]+1] = 1;
  252.             if (comb & 0x1)
  253.                 bits[rd3[i]+2] = 1;
  254.             permute(bits, &c, &d);
  255.             bits[rd3[i]] = 0;
  256.             bits[rd3[i]+1] = 0;
  257.             bits[rd3[i]+2] = 0;
  258.             if (c != 0) {
  259.                 (void) fprintf(stderr,
  260.                     "Error PC_DR i %d comb %d\n", i, comb);
  261.             }
  262.             if (i == 0) {
  263.                 PC_DR[comb] = d;
  264.                 if ((comb & 0x3) == 0)
  265.                     printf("\n\t0x%08x,", d);
  266.                 else if (comb == 7)
  267.                     printf(" 0x%08x\n};\n\n", d);
  268.                 else
  269.                     printf(" 0x%08x,", d);
  270.             } else {
  271.                 if (d != PC_DR[comb] << i)
  272.                     (void) fprintf(stderr,
  273.                         "Error PC_DR 0x%08x c 0x%08x\n",
  274.                         PC_DR[comb], d);
  275.             }
  276.         }
  277.     }
  278. }
  279.