home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / nerecenz / programers / CRYPT / XCRYPT / CRYPT.C < prev    next >
C/C++ Source or Header  |  1997-04-01  |  4KB  |  131 lines

  1. /*
  2.  * UFC-crypt: ultra fast crypt(3) implementation
  3.  *
  4.  * Copyright (C) 1991, 1992, Michael Glad, email: glad@daimi.aau.dk
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Library General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Library General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU Library General Public
  17.  * License along with this library; if not, write to the Free
  18.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * @(#)crypt.c    2.14 01/23/92
  21.  *
  22.  * Semiportable C version
  23.  *
  24.  */
  25.  
  26. #include "ufc-crypt.h"
  27.  
  28. extern ufc_long *ufc_dofinalperm(ufc_long, ufc_long, ufc_long, ufc_long);
  29.  
  30. ufc_long *ufc_doit         (ufc_long, ufc_long, ufc_long,
  31.                             ufc_long, ufc_long);
  32.  
  33. /*
  34.  * 32 bit version
  35.  */
  36.  
  37. #ifdef _UFC_32_
  38.  
  39. extern long32 ufc_keytab[16][2];
  40. extern long32 ufc_sb0[], ufc_sb1[], ufc_sb2[], ufc_sb3[];
  41.  
  42. #define SBA(sb, v) (*(long32*)((char*)(sb)+(v)))
  43.  
  44. #ifdef __STDC__
  45. ufc_long *ufc_doit(ufc_long l1, ufc_long l2, ufc_long r1,
  46.                    ufc_long r2,ufc_long itr)
  47. #else
  48. ufc_long *ufc_doit(l1, l2, r1, r2, itr)
  49. ufc_long l1, l2, r1, r2, itr;
  50. #endif
  51. {
  52.     int i;
  53.     long32 s, *k;
  54.  
  55.     while(itr--) {
  56.       k = &ufc_keytab[0][0];
  57.       for(i=8; i--; ) {
  58.     s = *k++ ^ r1;
  59.     l1 ^= SBA(ufc_sb1, s & 0xffff); l2 ^= SBA(ufc_sb1, (s & 0xffff) + 4);  
  60.         l1 ^= SBA(ufc_sb0, s >>= 16);   l2 ^= SBA(ufc_sb0, (s)          + 4); 
  61.         s = *k++ ^ r2; 
  62.         l1 ^= SBA(ufc_sb3, s & 0xffff); l2 ^= SBA(ufc_sb3, (s & 0xffff) + 4);
  63.         l1 ^= SBA(ufc_sb2, s >>= 16);   l2 ^= SBA(ufc_sb2, (s)          + 4);
  64.  
  65.         s = *k++ ^ l1; 
  66.         r1 ^= SBA(ufc_sb1, s & 0xffff); r2 ^= SBA(ufc_sb1, (s & 0xffff) + 4);  
  67.         r1 ^= SBA(ufc_sb0, s >>= 16);   r2 ^= SBA(ufc_sb0, (s)          + 4); 
  68.         s = *k++ ^ l2; 
  69.         r1 ^= SBA(ufc_sb3, s & 0xffff); r2 ^= SBA(ufc_sb3, (s & 0xffff) + 4);  
  70.         r1 ^= SBA(ufc_sb2, s >>= 16);   r2 ^= SBA(ufc_sb2, (s)          + 4);
  71.       } 
  72.       s=l1; l1=r1; r1=s; s=l2; l2=r2; r2=s;
  73.     }
  74.     return ufc_dofinalperm(l1, l2, r1, r2);
  75. }
  76.  
  77. #endif
  78.  
  79. #ifdef _UFC_64_
  80. /*
  81.  * 64 bit version
  82.  */
  83.  
  84. extern long64 ufc_keytab[16];
  85. extern long64 ufc_sb0[], ufc_sb1[], ufc_sb2[], ufc_sb3[];
  86.  
  87. #ifdef cray
  88. #define SBA(sb, v) (*(long64*)((long64)(sb)+(v)))
  89. #else
  90. #define SBA(sb, v) (*(long64*)((char*)(sb)+(v)))
  91. #endif
  92.  
  93. #ifdef __STDC__
  94. ufc_long *
  95. ufc_doit(ufc_long l1, ufc_long l2, ufc_long r1, ufc_long r2, ufc_long itr)
  96. #else
  97. ufc_long *ufc_doit(l1, l2, r1, r2, itr)
  98.   ufc_long l1, l2, r1, r2, itr;
  99. #endif
  100.  {
  101.     int i;
  102.     long64 l, r, s, *k;
  103.  
  104.     l = (((long64)l1) << 32) | ((long64)l2);
  105.     r = (((long64)r1) << 32) | ((long64)r2);
  106.  
  107.     while(itr--) {
  108.       k = &ufc_keytab[0];
  109.       for(i=8; i--; ) {
  110.     s = *k++ ^ r;
  111.     l ^= SBA(ufc_sb3, (s >>  0) & 0xffff);
  112.         l ^= SBA(ufc_sb2, (s >> 16) & 0xffff);
  113.         l ^= SBA(ufc_sb1, (s >> 32) & 0xffff);
  114.         l ^= SBA(ufc_sb0, (s >> 48) & 0xffff);
  115.  
  116.     s = *k++ ^ l;
  117.     r ^= SBA(ufc_sb3, (s >>  0) & 0xffff);
  118.         r ^= SBA(ufc_sb2, (s >> 16) & 0xffff);
  119.         r ^= SBA(ufc_sb1, (s >> 32) & 0xffff);
  120.         r ^= SBA(ufc_sb0, (s >> 48) & 0xffff);
  121.       } 
  122.       s=l; l=r; r=s;
  123.     }
  124.  
  125.     l1 = l >> 32; l2 = l & 0xffffffff;
  126.     r1 = r >> 32; r2 = r & 0xffffffff;
  127.     return ufc_dofinalperm(l1, l2, r1, r2);
  128. }
  129.  
  130. #endif
  131.