00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #ifdef _MSC_VER
00009 #include "des.h"
00010 #else
00011 #include "libdes/des.h"
00012 #endif
00013
00014 #include <string.h>
00015
00016 #define bcopy(b1,b2,len) memcpy(b2, b1, (size_t)(len))
00017 #define bzero(b,len) memset(b, 0, (size_t)(len))
00018 #define bcmp(b1,b2,len) memcmp(b1, b2, (size_t)(len))
00019 #define index(s1,char) strchr(s1,char)
00020
00021 #if !defined(_LIBC) || defined(NOCONST)
00022 #define const
00023 #endif
00024
00025 #ifdef __STDC__
00026 #define PROTO
00027 #endif
00028
00029 #ifdef RAND
00030 #define random() rand()
00031 #define srandom(s) srand(s)
00032 #endif
00033
00034 #define ITERATIONS 16
00035 #define HALF_ITERATIONS 8
00036
00037 #define MAXWRITE (1024*16)
00038 #define BSIZE (MAXWRITE+4)
00039
00040 #define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
00041 l|=((unsigned long)(*((c)++)))<< 8, \
00042 l|=((unsigned long)(*((c)++)))<<16, \
00043 l|=((unsigned long)(*((c)++)))<<24)
00044
00045 #define c2ln(c,l1,l2,n) { \
00046 c+=n; \
00047 l1=l2=0; \
00048 switch (n) { \
00049 case 8: l2 =((unsigned long)(*(--(c))))<<24; \
00050 case 7: l2|=((unsigned long)(*(--(c))))<<16; \
00051 case 6: l2|=((unsigned long)(*(--(c))))<< 8; \
00052 case 5: l2|=((unsigned long)(*(--(c)))); \
00053 case 4: l1 =((unsigned long)(*(--(c))))<<24; \
00054 case 3: l1|=((unsigned long)(*(--(c))))<<16; \
00055 case 2: l1|=((unsigned long)(*(--(c))))<< 8; \
00056 case 1: l1|=((unsigned long)(*(--(c)))); \
00057 } \
00058 }
00059
00060 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
00061 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
00062 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
00063 *((c)++)=(unsigned char)(((l)>>24)&0xff))
00064
00065 #define HDRSIZE 4
00066
00067 #define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24, \
00068 l|=((unsigned long)(*((c)++)))<<16, \
00069 l|=((unsigned long)(*((c)++)))<< 8, \
00070 l|=((unsigned long)(*((c)++))))
00071
00072 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
00073 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
00074 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
00075 *((c)++)=(unsigned char)(((l) )&0xff))
00076
00077 #define l2cn(l1,l2,c,n) { \
00078 c+=n; \
00079 switch (n) { \
00080 case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
00081 case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
00082 case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
00083 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
00084 case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
00085 case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
00086 case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
00087 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
00088 } \
00089 }
00090
00091 #ifdef ALT_ECB
00092 #define D_ENCRYPT(L,R,S) \
00093 u=((R^s[S ])<<2); \
00094 t= R^s[S+1]; \
00095 t=((t>>2)+(t<<30)); \
00096 L^= \
00097 *(unsigned long *)(des_SP+0x0100+((t )&0xfc))+ \
00098 *(unsigned long *)(des_SP+0x0300+((t>> 8)&0xfc))+ \
00099 *(unsigned long *)(des_SP+0x0500+((t>>16)&0xfc))+ \
00100 *(unsigned long *)(des_SP+0x0700+((t>>24)&0xfc))+ \
00101 *(unsigned long *)(des_SP+ ((u )&0xfc))+ \
00102 *(unsigned long *)(des_SP+0x0200+((u>> 8)&0xfc))+ \
00103 *(unsigned long *)(des_SP+0x0400+((u>>16)&0xfc))+ \
00104 *(unsigned long *)(des_SP+0x0600+((u>>24)&0xfc));
00105 #else
00106 #ifdef MSDOS
00107 #define D_ENCRYPT(L,R,S) \
00108 U.l=R^s[S+1]; \
00109 T.s[0]=((U.s[0]>>4)|(U.s[1]<<12))&0x3f3f; \
00110 T.s[1]=((U.s[1]>>4)|(U.s[0]<<12))&0x3f3f; \
00111 U.l=(R^s[S ])&0x3f3f3f3f; \
00112 L^= des_SPtrans[1][(T.c[0])]| \
00113 des_SPtrans[3][(T.c[1])]| \
00114 des_SPtrans[5][(T.c[2])]| \
00115 des_SPtrans[7][(T.c[3])]| \
00116 des_SPtrans[0][(U.c[0])]| \
00117 des_SPtrans[2][(U.c[1])]| \
00118 des_SPtrans[4][(U.c[2])]| \
00119 des_SPtrans[6][(U.c[3])];
00120 #else
00121 #define D_ENCRYPT(L,R,S) \
00122 u=(R^s[S ]); \
00123 t=R^s[S+1]; \
00124 t=((t>>4)+(t<<28)); \
00125 L^= des_SPtrans[1][(t )&0x3f]| \
00126 des_SPtrans[3][(t>> 8)&0x3f]| \
00127 des_SPtrans[5][(t>>16)&0x3f]| \
00128 des_SPtrans[7][(t>>24)&0x3f]| \
00129 des_SPtrans[0][(u )&0x3f]| \
00130 des_SPtrans[2][(u>> 8)&0x3f]| \
00131 des_SPtrans[4][(u>>16)&0x3f]| \
00132 des_SPtrans[6][(u>>24)&0x3f];
00133 #endif
00134 #endif
00135
00136
00137 #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
00138 (b)^=(t),\
00139 (a)^=((t)<<(n)))
00140
00141 #define IP(l,r,t) \
00142 PERM_OP(r,l,t, 4,0x0f0f0f0fL); \
00143 PERM_OP(l,r,t,16,0x0000ffffL); \
00144 PERM_OP(r,l,t, 2,0x33333333L); \
00145 PERM_OP(l,r,t, 8,0x00ff00ffL); \
00146 PERM_OP(r,l,t, 1,0x55555555L);
00147
00148 #define FP(l,r,t) \
00149 PERM_OP(l,r,t, 1,0x55555555L); \
00150 PERM_OP(r,l,t, 8,0x00ff00ffL); \
00151 PERM_OP(l,r,t, 2,0x33333333L); \
00152 PERM_OP(r,l,t,16,0x0000ffffL); \
00153 PERM_OP(l,r,t, 4,0x0f0f0f0fL);
00154