home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / UNIX.ZIP / GUESS / INI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-04  |  2.9 KB  |  134 lines

  1. #include    "fdes.h"
  2.  
  3. extern unsb        FP[];
  4. extern unsb        PC1_C[];
  5. extern unsb        PC1_D[];
  6. extern unsb        PC2_C[];
  7. extern unsb        PC2_D[];
  8. extern unsb        E[];
  9. extern unsb        P[];
  10. extern fbpb4R    S[8][64];
  11. extern unsb        shifts[];
  12. extern obpb1    C[28];
  13. extern obpb1    D[28];
  14. extern sbpb24    KSL[16], KSH[16];
  15. extern obpb1    L[32], R[32];
  16. extern sbpb24    S0L[64], S1L[64], S2L[64], S3L[64],
  17.                     S4L[64], S5L[64], S6L[64], S7L[64];
  18. extern sbpb24    S0H[64], S1H[64], S2H[64], S3H[64],
  19.                     S4H[64], S5H[64], S6H[64], S7H[64];
  20.  
  21.  
  22. void        fsetkey(obpb1 *key);
  23. void        init(unsl tableno, sbpb24 *lowptr,sbpb24 *highptr);
  24. fbpb4        lookupS(unsl tableno, sbpb6R t6bits);
  25. void        init_des(void);
  26. ebpb24      sixbitTOtf(sbpb24 sb);
  27. sbpb24      tfTOsixbit(ebpb24 tf);
  28. void        Combiner(void);
  29.  
  30. /* Convert unsl in twenty-four bit contiguous format
  31.  * to six bits per byte format.  Return result.
  32.  */
  33. sbpb24 tfTOsixbit(ebpb24 tf)
  34. {
  35.     sbpb24    res;
  36.  
  37.     res = 0;
  38.     res |= (tf >> 0) & 077;
  39.     res |= ((tf >> 6) & 077) << 8;
  40.     res |= ((tf >> 12) & 077) << 16;
  41.     res |= ((tf >> 18) & 077) << 24;
  42.     return(res);
  43. }
  44.  
  45.  
  46. /* Convert unsl in six bits per byte format
  47.  * to twenty-four bit contiguous format.  Return result.
  48.  */
  49.  /*
  50. ebpb24    sixbitTOtf(sbpb24 sb)
  51. {
  52.     ebpb24    res;
  53.  
  54.     res = 0;
  55.     res |= (sb >> 0) & 077;
  56.     res |= ((sb >> 8) & 077) << 6;
  57.     res |= ((sb >> 16) & 077) << 12;
  58.     res |= ((sb >> 24) & 077) << 18;
  59.     return(res);
  60. }
  61. */
  62.  
  63.  
  64. /*
  65.  * Lookup an S-box entry.
  66.  */
  67. fbpb4    lookupS(unsl tableno, sbpb6R t6bits)
  68. {
  69.     sbpb6    fixed6bits;
  70.     fbpb4R    r;
  71.     fbpb4    fixedr;
  72.  
  73.     fixed6bits = (((t6bits >> 0) &01) << 5)+
  74.                  (((t6bits >> 1) &01) << 3)+
  75.                      (((t6bits >> 2) &01) << 2)+
  76.                      (((t6bits >> 3) &01) << 1)+
  77.                      (((t6bits >> 4) &01) << 0)+
  78.                      (((t6bits >> 5) &01) << 4);
  79.  
  80.     r = S[tableno][fixed6bits];
  81.  
  82.     fixedr = (((r >> 3)&01) << 0)+
  83.              (((r >> 2)&01) << 1)+
  84.          (((r >> 1)&01) << 2)+
  85.          (((r >> 0)&01) << 3);
  86.  
  87.     return(fixedr);
  88. }
  89.  
  90.  
  91.  
  92. void    init_des(void)
  93. {
  94.     init(0, S0L, S0H);
  95.     init(1, S1L, S1H);
  96.     init(2, S2L, S2H);
  97.     init(3, S3L, S3H);
  98.     init(4, S4L, S4H);
  99.     init(5, S5L, S5H);
  100.     init(6, S6L, S6H);
  101.     init(7, S7L, S7H);
  102.     Combiner();
  103. }
  104.  
  105.  
  106.  
  107. void    init(unsl tableno, sbpb24 *lowptr,sbpb24 *highptr)
  108. {
  109.     static    obpb1    tmp32[32];
  110.     static    obpb1    tmpP32[32];
  111.     static    obpb1    tmpE[32];
  112.     int    j, k, i;
  113.  
  114.     for (j = 0 ; j < 64 ; j++)  {
  115.         k = lookupS(tableno, j);
  116.         for (i = 0 ; i < 32 ; i++)
  117.             tmp32[i] = 0;
  118.         for (i = 0 ; i < 4 ; i++)
  119.             tmp32[4 * tableno + i] = (k >> i) & 01;
  120.         for (i = 0 ; i < 32 ; i++)
  121.             tmpP32[i] = tmp32[P[i] - 1];
  122.         for (i = 0 ; i < 48 ; i++)
  123.             tmpE[i] = tmpP32[E[i] - 1];
  124.         lowptr[j] = 0;
  125.         highptr[j] = 0;
  126.         for (i = 0 ; i < 24 ; i++)
  127.             lowptr[j] |= ((unsigned long)tmpE[i]) << i;
  128.         for (k = 0, i = 24 ; i < 48 ; i++, k++)
  129.             highptr[j] |= ((unsigned long)tmpE[i]) << k;
  130.         lowptr[j] = tfTOsixbit(lowptr[j]);
  131.         highptr[j] = tfTOsixbit(highptr[j]);
  132.     }
  133. }
  134.