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

  1. /*
  2.  * makeIPFP - make fast DES IP and FP tables
  3.  *
  4.  * This is an older version which generated tables half the size of
  5.  * the current version, but which took about double the CPU time to
  6.  * compute permutations from these tables.  Since the CPU spent on the
  7.  * permutations is small compared to the CPU spent in the cipher code,
  8.  * I may go back to the smaller tables to save the space some day.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13.  
  14. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  15.  
  16. u_long IPL[8][16];
  17. u_long FPL[8][16];
  18.  
  19. char *progname;
  20. int debug;
  21.  
  22. /*
  23.  * main - parse arguments and handle options
  24.  */
  25. main(argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29.     int c;
  30.     int errflg = 0;
  31.     extern int optind;
  32.     extern char *optarg;
  33.  
  34.     progname = argv[0];
  35.     while ((c = getopt(argc, argv, "d")) != EOF)
  36.         switch (c) {
  37.         case 'd':
  38.             ++debug;
  39.             break;
  40.         default:
  41.             errflg++;
  42.             break;
  43.         }
  44.     if (errflg) {
  45.         (void) fprintf(stderr, "usage: %s [-d]\n", progname);
  46.         exit(2);
  47.     }
  48.     doit();
  49.     exit(0);
  50. }
  51.  
  52.  
  53. /*
  54.  * Initial permutation table
  55.  */
  56. u_char IP[64] = {
  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
  65. };
  66.  
  67. /*
  68.  * Inverse initial permutation table
  69.  */
  70. u_char FP[64] = {
  71.     40,  8, 48, 16, 56, 24, 64, 32,
  72.     39,  7, 47, 15, 55, 23, 63, 31,
  73.     38,  6, 46, 14, 54, 22, 62, 30,
  74.     37,  5, 45, 13, 53, 21, 61, 29,
  75.     36,  4, 44, 12, 52, 20, 60, 28,
  76.     35,  3, 43, 11, 51, 19, 59, 27,
  77.     34,  2, 42, 10, 50, 18, 58, 26,
  78.     33,  1, 41,  9, 49, 17, 57, 25
  79. };
  80.  
  81.  
  82. /*
  83.  * Bit order after the operation
  84.  *
  85.  * ((left & 0x55555555) << 1) | (right & 0x55555555)
  86.  */
  87. u_char IPLbits[32] = {
  88.      2, 34,  4, 36,  6, 38,  8, 40,
  89.     10, 42, 12, 44, 14, 46, 16, 48,
  90.     18, 50, 20, 52, 22, 54, 24, 56,
  91.     26, 58, 28, 60, 30, 62, 32, 64
  92. };
  93.  
  94.  
  95. /*
  96.  * Bit order after the operation
  97.  *
  98.  * (left & 0xaaaaaaaa) | ((right & 0xaaaaaaaa) >> 1)
  99.  */
  100. u_char IPRbits[32] = {
  101.      1, 33,  3, 35,  5, 37,  7, 39,
  102.      9, 41, 11, 43, 13, 45, 15, 47,
  103.     17, 49, 19, 51, 21, 53, 23, 55,
  104.     25, 57, 27, 59, 29, 61, 31, 63
  105. };
  106.  
  107.  
  108. /*
  109.  * Bit order after the operation
  110.  *
  111.  * ((left & 0x0f0f0f0f) << 4) | (right & 0x0f0f0f0f)
  112.  */
  113. u_char FPLbits[32] = {
  114.      5,  6,  7,  8, 37, 38, 39, 40,
  115.     13, 14, 15, 16, 45, 46, 47, 48,
  116.     21, 22, 23, 24, 53, 54, 55, 56,
  117.     29, 30, 31, 32, 61, 62, 63, 64
  118. };
  119.  
  120.  
  121. /*
  122.  * Bit order after the operation
  123.  *
  124.  * (left & 0xf0f0f0f0) | ((right & 0xf0f0f0f0) >> 4)
  125.  */
  126. u_char FPRbits[32] = {
  127.      1,  2,  3,  4, 33, 34, 35, 36,
  128.      9, 10, 11, 12, 41, 42, 43, 44,
  129.     17, 18, 19, 20, 49, 50, 51, 52,
  130.     25, 26, 27, 28, 57, 58, 59, 60
  131. };
  132.  
  133.  
  134. /*
  135.  * perm - do a permutation with the given table
  136.  */
  137. perm(databits, permtab, leftp, rightp)
  138.     u_char *databits;
  139.     u_char *permtab;
  140.     u_long *leftp;
  141.     u_long *rightp;
  142. {
  143.     register u_long left;
  144.     register u_long right;
  145.     register u_char *P;
  146.     register u_char *bits;
  147.     register int i;
  148.  
  149.     left = right = 0;
  150.     P = permtab;
  151.     bits = databits;
  152.  
  153.     for (i = 0; i < 32; i++) {
  154.         left <<= 1;
  155.         if (bits[P[i]-1])
  156.             left |= 1;
  157.     }
  158.  
  159.     for (i = 32; i < 64; i++) {
  160.         right <<= 1;
  161.         if (bits[P[i]-1])
  162.             right |= 1;
  163.     }
  164.  
  165.     *leftp = left;
  166.     *rightp = right;
  167. }
  168.  
  169.  
  170. /*
  171.  * doit - make up the tables
  172.  */
  173. doit()
  174. {
  175.     u_char bits[64];
  176.     u_long left;
  177.     u_long right;
  178.     int tabno;
  179.     int i;
  180.     int ind0, ind1, ind2, ind3;
  181.     int quadbits;
  182.  
  183.     bzero((char *)bits, sizeof bits);
  184.  
  185.     /*
  186.      * Do the rounds for the IPL table.  We save the results of
  187.      * this as well as printing them.  Note that this is the
  188.      * left-half table.
  189.      */
  190.     printf("static u_long IP[8][16] = {");
  191.     for (tabno = 0; tabno < 8; tabno++) {
  192.         i = tabno * 4;
  193.         ind3 = IPLbits[i] - 1;
  194.         ind2 = IPLbits[i+1] - 1;
  195.         ind1 = IPLbits[i+2] - 1;
  196.         ind0 = IPLbits[i+3] - 1;
  197.         for (quadbits = 0; quadbits < 16; quadbits++) {
  198.             if (quadbits & (1 << 3))
  199.                 bits[ind3] = 1;
  200.             if (quadbits & (1 << 2))
  201.                 bits[ind2] = 1;
  202.             if (quadbits & (1 << 1))
  203.                 bits[ind1] = 1;
  204.             if (quadbits & 1)
  205.                 bits[ind0] = 1;
  206.             perm(bits, IP, &left, &right);
  207.             bits[ind3] = 0;
  208.             bits[ind2] = 0;
  209.             bits[ind1] = 0;
  210.             bits[ind0] = 0;
  211.             if (right != 0) {
  212.                 fprintf(stderr,
  213.                     "IPL tabno %d quad %d right not zero\n",
  214.                     tabno, quadbits);
  215.                 exit(1);
  216.             }
  217.             IPL[tabno][quadbits] = left;
  218.             if (quadbits == 15 && tabno == 7) {
  219.                 printf(" 0x%08x", left);
  220.             } else if (quadbits & 0x3) {
  221.                 printf(" 0x%08x,", left);
  222.             } else {
  223.                 printf("\n\t0x%08x,", left);
  224.             }
  225.         }
  226.         if (tabno == 7)
  227.             printf("\n};\n");
  228.         printf("\n");
  229.     }
  230.  
  231.     /*
  232.      * Compute the right half of the same table.  I noticed this table
  233.      * was the same as the previous one, just by luck, so we don't
  234.      * actually have to do this.  Do it anyway just for a check.
  235.      */
  236.     for (tabno = 0; tabno < 8; tabno++) {
  237.         i = tabno * 4;
  238.         ind3 = IPRbits[i] - 1;
  239.         ind2 = IPRbits[i+1] - 1;
  240.         ind1 = IPRbits[i+2] - 1;
  241.         ind0 = IPRbits[i+3] - 1;
  242.         for (quadbits = 0; quadbits < 16; quadbits++) {
  243.             if (quadbits & (1 << 3))
  244.                 bits[ind3] = 1;
  245.             if (quadbits & (1 << 2))
  246.                 bits[ind2] = 1;
  247.             if (quadbits & (1 << 1))
  248.                 bits[ind1] = 1;
  249.             if (quadbits & 1)
  250.                 bits[ind0] = 1;
  251.             perm(bits, IP, &left, &right);
  252.             bits[ind3] = 0;
  253.             bits[ind2] = 0;
  254.             bits[ind1] = 0;
  255.             bits[ind0] = 0;
  256.             if (left != 0) {
  257.                 fprintf(stderr,
  258.                     "IPR tabno %d quad %d left not zero\n",
  259.                     tabno, quadbits);
  260.                 exit(1);
  261.             }
  262.             if (right != IPL[tabno][quadbits]) {
  263.                 fprintf(stderr,
  264.             "IPR tabno %d quad %d: 0x%08x not same as 0x%08x\n",
  265.                    tabno, quadbits, right,IPL[tabno][quadbits]);
  266.                 exit(1);
  267.             }
  268.         }
  269.     }
  270.  
  271.     /*
  272.      * Next are the FP tables
  273.      */
  274.     printf("static u_long FP[8][16] = {");
  275.     for (tabno = 0; tabno < 8; tabno++) {
  276.         i = tabno * 4;
  277.         ind3 = FPLbits[i] - 1;
  278.         ind2 = FPLbits[i+1] - 1;
  279.         ind1 = FPLbits[i+2] - 1;
  280.         ind0 = FPLbits[i+3] - 1;
  281.         for (quadbits = 0; quadbits < 16; quadbits++) {
  282.             if (quadbits & (1 << 3))
  283.                 bits[ind3] = 1;
  284.             if (quadbits & (1 << 2))
  285.                 bits[ind2] = 1;
  286.             if (quadbits & (1 << 1))
  287.                 bits[ind1] = 1;
  288.             if (quadbits & 1)
  289.                 bits[ind0] = 1;
  290.             perm(bits, FP, &left, &right);
  291.             bits[ind3] = 0;
  292.             bits[ind2] = 0;
  293.             bits[ind1] = 0;
  294.             bits[ind0] = 0;
  295.             if (right != 0) {
  296.                 fprintf(stderr,
  297.                     "FPL tabno %d quad %d right not zero\n",
  298.                     tabno, quadbits);
  299.                 exit(1);
  300.             }
  301.             FPL[tabno][quadbits] = left;
  302.             if (quadbits == 15 && tabno == 7) {
  303.                 printf(" 0x%08x", left);
  304.             } else if (quadbits & 0x3) {
  305.                 printf(" 0x%08x,", left);
  306.             } else {
  307.                 printf("\n\t0x%08x,", left);
  308.             }
  309.         }
  310.         if (tabno == 7)
  311.             printf("\n};");
  312.         printf("\n");
  313.     }
  314.  
  315.     /*
  316.      * Right half of same set of tables.  This was symmetric too.
  317.      * Amazing!
  318.      */
  319.     for (tabno = 0; tabno < 8; tabno++) {
  320.         i = tabno * 4;
  321.         ind3 = FPRbits[i] - 1;
  322.         ind2 = FPRbits[i+1] - 1;
  323.         ind1 = FPRbits[i+2] - 1;
  324.         ind0 = FPRbits[i+3] - 1;
  325.         for (quadbits = 0; quadbits < 16; quadbits++) {
  326.             if (quadbits & (1 << 3))
  327.                 bits[ind3] = 1;
  328.             if (quadbits & (1 << 2))
  329.                 bits[ind2] = 1;
  330.             if (quadbits & (1 << 1))
  331.                 bits[ind1] = 1;
  332.             if (quadbits & 1)
  333.                 bits[ind0] = 1;
  334.             perm(bits, FP, &left, &right);
  335.             bits[ind3] = 0;
  336.             bits[ind2] = 0;
  337.             bits[ind1] = 0;
  338.             bits[ind0] = 0;
  339.             if (left != 0) {
  340.                 fprintf(stderr,
  341.                     "FPR tabno %d quad %d left not zero\n",
  342.                     tabno, quadbits);
  343.                 exit(1);
  344.             }
  345.             if (right != FPL[tabno][quadbits]) {
  346.                 fprintf(stderr,
  347.             "FPR tabno %d quad %d: 0x%08x not same as 0x%08x\n",
  348.                    tabno, quadbits, right,FPL[tabno][quadbits]);
  349.                 exit(1);
  350.             }
  351.         }
  352.     }
  353. }
  354.