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 / makePC2.c < prev    next >
C/C++ Source or Header  |  1989-07-25  |  4KB  |  229 lines

  1. /*
  2.  * makePC2 - build custom permutted choice 2 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 2 table.  This actually produces the low order 24
  45.  * bits of the subkey Ki from the 28 bit value of Ci.  This has had
  46.  * 1 subtracted from it to give a zero base.
  47.  */
  48. static u_char PC2_C[24] = {
  49.     13, 16, 10, 23,  0,  4,
  50.      2, 27, 14,  5, 20,  9,
  51.     22, 18, 11,  3, 25,  7,
  52.     15,  6, 26, 19, 12,  1
  53. };
  54.  
  55. /*
  56.  * Permuted choice 2 table, operating on the 28 Di bits to produce the
  57.  * high order 24 bits of subkey Ki.  This has had 29 subtracted from
  58.  * it to give it a zero base into our D bit array.
  59.  */
  60. static u_char PC2_D[24] = {
  61.     12, 23,  2,  8, 18, 26,
  62.      1, 11, 22, 16,  4, 19,
  63.     15, 20, 10, 27,  5, 24,
  64.     17, 13, 21,  7,  0,  3
  65. };
  66.  
  67. u_long masks[4] = { 0x40000000, 0x400000, 0x4000, 0x40 };
  68.  
  69.  
  70. /*
  71.  * permc - permute C, producing a four byte result
  72.  */
  73. permc(bits, resp)
  74.     u_char *bits;
  75.     u_long *resp;
  76. {
  77.     register int part;
  78.     register int i;
  79.     register u_long mask;
  80.     u_char res[24];
  81.  
  82.     bzero((char *)res, sizeof res);
  83.  
  84.     for (i = 0; i < 24; i++) {
  85.         res[i] = bits[PC2_C[i]];
  86.     }
  87.  
  88.     *resp = 0;
  89.     for (part = 0; part < 4; part++) {
  90.         mask = masks[part];
  91.         for (i = part*6; i < (part+1)*6; i++) {
  92.             mask >>= 1;
  93.             if (res[i])
  94.                 *resp |= mask;
  95.         }
  96.     }
  97. }
  98.  
  99. /*
  100.  * permd - permute D, producing a four byte result
  101.  */
  102. permd(bits, resp)
  103.     u_char *bits;
  104.     u_long *resp;
  105. {
  106.     register int part;
  107.     register int i;
  108.     register u_long mask;
  109.     u_char res[24];
  110.  
  111.     bzero((char *)res, sizeof res);
  112.  
  113.     for (i = 0; i < 24; i++) {
  114.         res[i] = bits[PC2_D[i]];
  115.     }
  116.  
  117.     *resp = 0;
  118.     for (part = 0; part < 4; part++) {
  119.         mask = masks[part];
  120.         for (i = part*6; i < (part+1)*6; i++) {
  121.             mask >>= 1;
  122.             if (res[i])
  123.                 *resp |= mask;
  124.         }
  125.     }
  126. }
  127.  
  128.  
  129. /*
  130.  * bits used for each round in C
  131.  */
  132. int cbits[4][6] = {
  133.     0, 1, 2, 3, 4, 5,
  134.     6, 7, 9, 10, 11, 12,
  135.     13, 14, 15, 16, 22, 23,
  136.     18, 19, 20, 25, 26, 27
  137. };
  138.  
  139.  
  140. /*
  141.  * bits used for each round in D
  142.  */
  143. int dbits[4][6] = {
  144.     0, 1, 2, 3, 4, 5,
  145.     7, 8, 10, 11, 12, 13,
  146.     15, 16, 17, 18, 19, 20,
  147.     21, 22, 23, 24, 26, 27
  148. };
  149.  
  150.  
  151. /*
  152.  * doit - compute and print the four PC1 tables
  153.  */
  154. doit()
  155. {
  156.     int i;
  157.     int comb;
  158.     u_long res;
  159.     u_char bits[28];
  160.  
  161.     bzero((char *)bits, sizeof bits);
  162.  
  163.     printf("static u_long PC2_C[4][64] = {");
  164.     for (i = 0; i < 4; i++) {
  165.         for (comb = 0; comb < 64; comb++) {
  166.             if (comb & 0x20)
  167.                 bits[cbits[i][0]] = 1;
  168.             if (comb & 0x10)
  169.                 bits[cbits[i][1]] = 1;
  170.             if (comb & 0x8)
  171.                 bits[cbits[i][2]] = 1;
  172.             if (comb & 0x4)
  173.                 bits[cbits[i][3]] = 1;
  174.             if (comb & 0x2)
  175.                 bits[cbits[i][4]] = 1;
  176.             if (comb & 0x1)
  177.                 bits[cbits[i][5]] = 1;
  178.             permc(bits, &res);
  179.             bits[cbits[i][0]] = 0;
  180.             bits[cbits[i][1]] = 0;
  181.             bits[cbits[i][2]] = 0;
  182.             bits[cbits[i][3]] = 0;
  183.             bits[cbits[i][4]] = 0;
  184.             bits[cbits[i][5]] = 0;
  185.             if ((comb & 0x3) == 0)
  186.                 printf("\n\t0x%08x,", res);
  187.             else if (comb == 63 && i == 3)
  188.                 printf(" 0x%08x\n};\n\n", res);
  189.             else if (comb == 63)
  190.                 printf(" 0x%08x,\n", res);
  191.             else
  192.                 printf(" 0x%08x,", res);
  193.         }
  194.     }
  195.  
  196.     printf("static u_long PC2_D[4][64] = {");
  197.     for (i = 0; i < 4; i++) {
  198.         for (comb = 0; comb < 64; comb++) {
  199.             if (comb & 0x20)
  200.                 bits[dbits[i][0]] = 1;
  201.             if (comb & 0x10)
  202.                 bits[dbits[i][1]] = 1;
  203.             if (comb & 0x8)
  204.                 bits[dbits[i][2]] = 1;
  205.             if (comb & 0x4)
  206.                 bits[dbits[i][3]] = 1;
  207.             if (comb & 0x2)
  208.                 bits[dbits[i][4]] = 1;
  209.             if (comb & 0x1)
  210.                 bits[dbits[i][5]] = 1;
  211.             permd(bits, &res);
  212.             bits[dbits[i][0]] = 0;
  213.             bits[dbits[i][1]] = 0;
  214.             bits[dbits[i][2]] = 0;
  215.             bits[dbits[i][3]] = 0;
  216.             bits[dbits[i][4]] = 0;
  217.             bits[dbits[i][5]] = 0;
  218.             if ((comb & 0x3) == 0)
  219.                 printf("\n\t0x%08x,", res);
  220.             else if (comb == 63 && i == 3)
  221.                 printf(" 0x%08x\n};\n\n", res);
  222.             else if (comb == 63)
  223.                 printf(" 0x%08x,\n", res);
  224.             else
  225.                 printf(" 0x%08x,", res);
  226.         }
  227.     }
  228. }
  229.