* added compilers lcc and bcc (linux86)
[mascara-docs.git] / compilers / linux86-0.16.17 / libc / misc / crypt.c
blobbe51948e0c0f1587799d32a8c5597ef5c2f2cced
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 ;) */
6 /*
7 * I've:
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.
16 * RDB.
19 #include <stdlib.h>
21 char *
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];
29 int n=32, i, j;
30 static char rkey[16];
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;
36 rkey[0]=*salt;
37 rkey[1]=salt[1];
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));
43 while (n-->0) {
44 sum += delta;
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
54 in /etc/passwd */
55 sum=v[0];
56 for (i=2;i<13;i++)
58 /* This unpacks the 6 bit data, each cluster into its own byte */
59 rkey[i]=(sum&0x3F);
60 sum>>=6;
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;
68 else return NULL;
71 rkey[13]='\0';
72 return rkey;