1 /* TEA based crypt(), version 0.0 <ndf@linux.mit.edu>
2 * It looks like there are problems with key bits carrying through
3 * to the encryted data, and I want to get rid of that libc call..
4 * This is just so rob could see it ;) */
8 * Compared the TEA implementation to a reference source - OK
9 * Reduced the cycles count from 64 to 32 (the suggested value)
10 * Changed the types of 'n' and 'i' for better code with bcc.
11 * Removed a possible overrun of rkey by looping the values, it's now
12 * possible to _choose_ every bit of the 128bit PW with a 32 character word.
13 * Corrected the output transformation, it lost bits between words.
14 * Cleaned out all trace of the uncrypted PW from rkey.
22 crypt(const char * key
, const char * salt
)
24 /* n is the number of cycles (2 rounds/cycle),
25 delta is a golden # derivative,
26 k is the key, v is the data to be encrypted. */
28 unsigned long v
[2], sum
=0, delta
=0x9e3779b9, k
[4];
32 /* Our constant string will be a string of zeros .. */
33 v
[0]=v
[1]=k
[0]=k
[1]=k
[2]=k
[3]=0;
34 for(i
=0;i
<16;i
++) rkey
[i
]=0;
38 for (j
=2,i
=0;key
[i
];i
++,j
=((j
+1)&15))
39 rkey
[j
]=(rkey
[j
]<<4)+(rkey
[j
]>>4)+ key
[i
];
41 memcpy(k
, rkey
, 4*sizeof(long));
45 v
[0] += (v
[1]<<4)+k
[0] ^ v
[1]+sum
^ (v
[1]>>5)+k
[1];
46 v
[1] += (v
[0]<<4)+k
[2] ^ v
[0]+sum
^ (v
[0]>>5)+k
[3];
49 /* Remove any trace of key */
50 for(i
=0;i
<16;i
++) rkey
[i
]=0;
51 *rkey
=*salt
; rkey
[1]=salt
[1];
53 /* Now we need to unpack the bits and map it to "A-Za-z0-9./" for printing
58 /* This unpacks the 6 bit data, each cluster into its own byte */
61 if(i
==0+2) sum
|= (v
[1]<<26);
62 if(i
==5+2) sum
|= (v
[1]>>4);
64 /* Now we map to the proper chars */
65 if (rkey
[i
]>=0 && rkey
[i
]<12) rkey
[i
]+=46;
66 else if (rkey
[i
]>11 && rkey
[i
]<38) rkey
[i
]+=53;
67 else if (rkey
[i
]>37 && rkey
[i
]<64) rkey
[i
]+=59;