1 /**************************************************************************
2 * Unix-like crypt(3) Algorithm for Password Encryption
5 * Purpose : Provides crypt(3) functionality to ANSI C compilers
6 * without a need for the crypt library.
7 * Author : Michael Dipperstein
8 * Date : November 3, 1998
10 ***************************************************************************
11 * The source in this file is heavily borrowed from the crypt3.c file
12 * found on several ftp sites on the Internet. The original source
13 * claimed to be BSD, but was not distributed with any BSD license or
14 * copyright claims. I am releasing the source that I have provided into
15 * public domain without any restrictions, warranties, or copyright
18 * The code below has been cleaned and compiles correctly under, gcc,
19 * lcc, and Borland's bcc C compilers. A bug involving the left and
20 * right halves of the encrypted data block in the widely published
21 * crypt3.c source has been fixed by this version. All implicit register
22 * declarations have been removed, because they generated suboptimal code.
23 * All constant data has been explicitly declared as const and all
24 * declarations have been given a minimal scope, because I'm paranoid.
26 * Caution: crypt() returns a pointer to static data. I left it this way
27 * to maintain backward compatibility. The downside is that
28 * successive calls will cause previous results to be lost.
29 * This can easily be changed with only minor modifications to
30 * the function crypt().
31 **************************************************************************/
33 /* Initial permutation */
34 static const char IP[] =
36 58, 50, 42, 34, 26, 18, 10, 2,
37 60, 52, 44, 36, 28, 20, 12, 4,
38 62, 54, 46, 38, 30, 22, 14, 6,
39 64, 56, 48, 40, 32, 24, 16, 8,
40 57, 49, 41, 33, 25, 17, 9, 1,
41 59, 51, 43, 35, 27, 19, 11, 3,
42 61, 53, 45, 37, 29, 21, 13, 5,
43 63, 55, 47, 39, 31, 23, 15, 7,
46 /* Final permutation, FP = IP^(-1) */
47 static const char FP[] = {
48 40, 8, 48, 16, 56, 24, 64, 32,
49 39, 7, 47, 15, 55, 23, 63, 31,
50 38, 6, 46, 14, 54, 22, 62, 30,
51 37, 5, 45, 13, 53, 21, 61, 29,
52 36, 4, 44, 12, 52, 20, 60, 28,
53 35, 3, 43, 11, 51, 19, 59, 27,
54 34, 2, 42, 10, 50, 18, 58, 26,
55 33, 1, 41, 9, 49, 17, 57, 25,
58 /**************************************************************************
59 * Permuted-choice 1 from the key bits to yield C and D.
60 * Note that bits 8,16... are left out:
61 * They are intended for a parity check.
62 **************************************************************************/
63 static const char PC1_C[] =
65 57, 49, 41, 33, 25, 17, 9,
66 1, 58, 50, 42, 34, 26, 18,
67 10, 2, 59, 51, 43, 35, 27,
68 19, 11, 3, 60, 52, 44, 36,
71 static const char PC1_D[] =
73 63, 55, 47, 39, 31, 23, 15,
74 7, 62, 54, 46, 38, 30, 22,
75 14, 6, 61, 53, 45, 37, 29,
76 21, 13, 5, 28, 20, 12, 4,
79 /* Sequence of shifts used for the key schedule. */
80 static const char shifts[] =
81 {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
83 /**************************************************************************
84 * Permuted-choice 2, to pick out the bits from the CD array that generate
86 **************************************************************************/
87 static const char PC2_C[] =
95 static const char PC2_D[] =
97 41, 52, 31, 37, 47, 55,
98 30, 40, 51, 45, 33, 48,
99 44, 49, 39, 56, 34, 53,
100 46, 42, 50, 36, 29, 32,
103 /* The C and D arrays used to calculate the key schedule. */
107 /* The key schedule. Generated from the key. */
108 static char KS[16][48];
110 /* The E bit-selection table. */
112 static const char e2[] =
116 8, 9, 10, 11, 12, 13,
117 12, 13, 14, 15, 16, 17,
118 16, 17, 18, 19, 20, 21,
119 20, 21, 22, 23, 24, 25,
120 24, 25, 26, 27, 28, 29,
121 28, 29, 30, 31, 32, 1,
124 /**************************************************************************
127 * Description: Set up the key schedule from the encryption key.
130 * pointer to 64 character array. Each character represents a
134 **************************************************************************/
135 void setkey(char *key)
139 /**********************************************************************
140 * First, generate C and D by permuting the key. The low order bit of
141 * each 8-bit char is not used, so C and D are only 28 bits apiece.
142 **********************************************************************/
143 for(i = 0; i < 28; i++)
145 C[i] = key[PC1_C[i] - 1];
146 D[i] = key[PC1_D[i] - 1];
149 /**********************************************************************
150 * To generate Ki, rotate C and D according to schedule and pick up a
151 * permutation using PC2.
152 **********************************************************************/
153 for(i = 0; i < 16; i++)
156 for(k = 0; k < shifts[i]; k++)
160 for(j = 0; j < 28 - 1; j++)
165 for(j = 0; j < 28 - 1; j++)
171 /* get Ki. Note C and D are concatenated */
172 for(j = 0; j < 24; j++)
174 KS[i][j] = C[PC2_C[j] - 1];
175 KS[i][j + 24] = D[PC2_D[j] - 28 -1];
179 /* load E with the initial E bit selections */
180 for(i=0; i < 48; i++)
184 /**************************************************************************
185 * The 8 selection functions. For some reason, they give a 0-origin
186 * index, unlike everything else.
187 **************************************************************************/
189 static const char S[8][64] =
192 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
193 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
194 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
195 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
199 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
200 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
201 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
202 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
206 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
207 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
208 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
209 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
213 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
214 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
215 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
216 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
220 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
221 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
222 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
223 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
227 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
228 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
229 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
230 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
234 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
235 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
236 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
237 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
241 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
242 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
243 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
244 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
248 /**************************************************************************
249 * P is a permutation on the selected combination of the current L and key.
250 **************************************************************************/
251 static const char P[] =
263 /* The combination of the key and the input, before selection. */
264 static char preS[48];
266 /**************************************************************************
269 * Description: Uses DES to encrypt a 64 bit block of data. Requires
270 * setkey to be invoked with the encryption key before it may
271 * be used. The results of the encryption are stored in block.
273 * Inputs: char *block
274 * pointer to 64 character array. Each character represents a
275 * bit in the data block.
278 **************************************************************************/
279 void encrypt(char *block)
281 int i, ii, temp, j, k;
283 char left[32], right[32]; /* block in two halves */
287 /* First, permute the bits in the input */
288 for(j = 0; j < 32; j++)
289 left[j] = block[IP[j] - 1];
292 right[j - 32] = block[IP[j] - 1];
294 /* Perform an encryption operation 16 times. */
295 for(ii= 0; ii < 16; ii++)
298 /* Save the right array, which will be the new left. */
299 for(j = 0; j < 32; j++)
302 /******************************************************************
303 * Expand right to 48 bits using the E selector and
304 * exclusive-or with the current key bits.
305 ******************************************************************/
306 for(j =0 ; j < 48; j++)
307 preS[j] = right[E[j] - 1] ^ KS[i][j];
309 /******************************************************************
310 * The pre-select bits are now considered in 8 groups of 6 bits ea.
311 * The 8 selection functions map these 6-bit quantities into 4-bit
312 * quantities and the results are permuted to make an f(R, K).
313 * The indexing into the selection functions is peculiar;
314 * it could be simplified by rewriting the tables.
315 ******************************************************************/
316 for(j = 0; j < 8; j++)
319 k = S[j][(preS[temp + 0] << 5) +
320 (preS[temp + 1] << 3) +
321 (preS[temp + 2] << 2) +
322 (preS[temp + 3] << 1) +
323 (preS[temp + 4] << 0) +
324 (preS[temp + 5] << 4)];
328 f[temp + 0] = (k >> 3) & 01;
329 f[temp + 1] = (k >> 2) & 01;
330 f[temp + 2] = (k >> 1) & 01;
331 f[temp + 3] = (k >> 0) & 01;
334 /******************************************************************
335 * The new right is left ^ f(R, K).
336 * The f here has to be permuted first, though.
337 ******************************************************************/
338 for(j = 0; j < 32; j++)
339 right[j] = left[j] ^ f[P[j] - 1];
341 /* Finally, the new left (the original right) is copied back. */
342 for(j = 0; j < 32; j++)
346 /* The output left and right are reversed. */
347 for(j = 0; j < 32; j++)
354 /* The final output gets the inverse permutation of the very original. */
355 for(j = 0; j < 64; j++)
359 block[j] = left[FP[j] - 1];
361 block[j] = right[FP[j] - 33];
365 /**************************************************************************
368 * Description: Clone of Unix crypt(3) function.
371 * pointer to 8 character encryption key (user password)
373 * pointer to 2 character salt used to modify the DES results.
375 * Returns: Pointer to static array containing the salt concatenated
376 * on to the encrypted results. Same as stored in passwd file.
377 **************************************************************************/
378 char *crypt(char *pw, char *salt)
382 block[66]; /* 1st store key, then results */
383 static char iobuf[16]; /* encrypted results */
385 for(i = 0; i < 66; i++)
388 /* break pw into 64 bits */
389 for(i = 0, c = *pw; c && (i < 64); i++)
391 for(j = 0; j < 7; j++, i++)
392 block[i] = (c >> (6 - j)) & 01;
397 /* set key based on pw */
400 for(i = 0; i < 66; i++)
403 for(i = 0; i < 2; i++)
405 /* store salt at beginning of results */
417 /* use salt to effect the E-bit selection */
418 for(j = 0; j < 6; j++)
423 E[6 * i +j] = E[6 * i + j + 24];
424 E[6 * i + j + 24] = temp;
429 /* call DES encryption 25 times using pw as key and initial data = 0 */
430 for(i = 0; i < 25; i++)
433 /* format encrypted block for standard crypt(3) output */
434 for(i=0; i < 11; i++)
437 for(j = 0; j < 6; j++)
440 c |= block[6 * i + j];
455 /* prevent premature NULL terminator */