xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/_crypt.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*      Copyright (c) 1984,1988 AT&T */
280Sstevel@tonic-gate /*        All Rights Reserved   */
290Sstevel@tonic-gate 
30*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * This program implements the
350Sstevel@tonic-gate  * Proposed Federal Information Processing
360Sstevel@tonic-gate  *  Data Encryption Standard.
370Sstevel@tonic-gate  * See Federal Register, March 17, 1975 (40FR12134)
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * Initial permutation,
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate static	char	IP[] = {
440Sstevel@tonic-gate 	58,50,42,34,26,18,10, 2,
450Sstevel@tonic-gate 	60,52,44,36,28,20,12, 4,
460Sstevel@tonic-gate 	62,54,46,38,30,22,14, 6,
470Sstevel@tonic-gate 	64,56,48,40,32,24,16, 8,
480Sstevel@tonic-gate 	57,49,41,33,25,17, 9, 1,
490Sstevel@tonic-gate 	59,51,43,35,27,19,11, 3,
500Sstevel@tonic-gate 	61,53,45,37,29,21,13, 5,
510Sstevel@tonic-gate 	63,55,47,39,31,23,15, 7,
520Sstevel@tonic-gate };
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Final permutation, FP = IP^(-1)
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static	char	FP[] = {
580Sstevel@tonic-gate 	40, 8,48,16,56,24,64,32,
590Sstevel@tonic-gate 	39, 7,47,15,55,23,63,31,
600Sstevel@tonic-gate 	38, 6,46,14,54,22,62,30,
610Sstevel@tonic-gate 	37, 5,45,13,53,21,61,29,
620Sstevel@tonic-gate 	36, 4,44,12,52,20,60,28,
630Sstevel@tonic-gate 	35, 3,43,11,51,19,59,27,
640Sstevel@tonic-gate 	34, 2,42,10,50,18,58,26,
650Sstevel@tonic-gate 	33, 1,41, 9,49,17,57,25,
660Sstevel@tonic-gate };
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * Permuted-choice 1 from the key bits
700Sstevel@tonic-gate  * to yield C and D.
710Sstevel@tonic-gate  * Note that bits 8,16... are left out:
720Sstevel@tonic-gate  * They are intended for a parity check.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate static	char	PC1_C[] = {
750Sstevel@tonic-gate 	57,49,41,33,25,17, 9,
760Sstevel@tonic-gate 	 1,58,50,42,34,26,18,
770Sstevel@tonic-gate 	10, 2,59,51,43,35,27,
780Sstevel@tonic-gate 	19,11, 3,60,52,44,36,
790Sstevel@tonic-gate };
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static	char	PC1_D[] = {
820Sstevel@tonic-gate 	63,55,47,39,31,23,15,
830Sstevel@tonic-gate 	 7,62,54,46,38,30,22,
840Sstevel@tonic-gate 	14, 6,61,53,45,37,29,
850Sstevel@tonic-gate 	21,13, 5,28,20,12, 4,
860Sstevel@tonic-gate };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * Sequence of shifts used for the key schedule.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate static	char	shifts[] = {
920Sstevel@tonic-gate 	1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Permuted-choice 2, to pick out the bits from
970Sstevel@tonic-gate  * the CD array that generate the key schedule.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate static	char	PC2_C[] = {
1000Sstevel@tonic-gate 	14,17,11,24, 1, 5,
1010Sstevel@tonic-gate 	 3,28,15, 6,21,10,
1020Sstevel@tonic-gate 	23,19,12, 4,26, 8,
1030Sstevel@tonic-gate 	16, 7,27,20,13, 2,
1040Sstevel@tonic-gate };
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static	char	PC2_D[] = {
1070Sstevel@tonic-gate 	41,52,31,37,47,55,
1080Sstevel@tonic-gate 	30,40,51,45,33,48,
1090Sstevel@tonic-gate 	44,49,39,56,34,53,
1100Sstevel@tonic-gate 	46,42,50,36,29,32,
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static struct _crypt {
1140Sstevel@tonic-gate 	/*
1150Sstevel@tonic-gate 	 * The C and D arrays used to calculate the key schedule.
1160Sstevel@tonic-gate 	 */
1170Sstevel@tonic-gate 	char	_C[28];
1180Sstevel@tonic-gate 	char	_D[28];
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 * The key schedule.
1210Sstevel@tonic-gate 	 * Generated from the key.
1220Sstevel@tonic-gate 	 */
1230Sstevel@tonic-gate 	char	_KS[16][48];
1240Sstevel@tonic-gate 	/*
1250Sstevel@tonic-gate 	 * The E bit-selection table.
1260Sstevel@tonic-gate 	 */
1270Sstevel@tonic-gate 	char	_E[48];
1280Sstevel@tonic-gate 	/*
1290Sstevel@tonic-gate 	 * The current block, divided into 2 halves.
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	char	_L[32], _R[32];
1320Sstevel@tonic-gate 	char	_tempL[32];
1330Sstevel@tonic-gate 	char	_f[32];
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * The combination of the key and the input, before selection.
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	char	_preS[48];
1380Sstevel@tonic-gate 	/*
1390Sstevel@tonic-gate 	 * Temps for crypt
1400Sstevel@tonic-gate 	 */
1410Sstevel@tonic-gate 	char	_ablock[66], _iobuf[16];
1420Sstevel@tonic-gate } *__crypt;
1430Sstevel@tonic-gate #define	C	(_c->_C)
1440Sstevel@tonic-gate #define	D	(_c->_D)
1450Sstevel@tonic-gate #define	KS	(_c->_KS)
1460Sstevel@tonic-gate #define	E	(_c->_E)
1470Sstevel@tonic-gate #define	L	(_c->_L)
1480Sstevel@tonic-gate #define	R	(_c->_R)
1490Sstevel@tonic-gate #define	tempL	(_c->_tempL)
1500Sstevel@tonic-gate #define	f	(_c->_f)
1510Sstevel@tonic-gate #define	preS	(_c->_preS)
1520Sstevel@tonic-gate #define	ablock	(_c->_ablock)
1530Sstevel@tonic-gate #define	iobuf	(_c->_iobuf)
1540Sstevel@tonic-gate 
155*722Smuffin static void	_cryptinit(void);
156*722Smuffin 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Set up the key schedule from the key.
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate #ifndef CRYPT
1620Sstevel@tonic-gate static
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate void
setkey(char * key)165*722Smuffin setkey(char *key)
1660Sstevel@tonic-gate {
167*722Smuffin 	int i, j, k;
1680Sstevel@tonic-gate 	int t;
169*722Smuffin 	struct _crypt *_c = __crypt;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (!_c) {
1720Sstevel@tonic-gate 		_cryptinit();
1730Sstevel@tonic-gate 		_c = __crypt;
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * First, generate C and D by permuting
1770Sstevel@tonic-gate 	 * the key.  The low order bit of each
1780Sstevel@tonic-gate 	 * 8-bit char is not used, so C and D are only 28
1790Sstevel@tonic-gate 	 * bits apiece.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	for (i=0; i<28; i++) {
1820Sstevel@tonic-gate 		C[i] = key[PC1_C[i]-1];
1830Sstevel@tonic-gate 		D[i] = key[PC1_D[i]-1];
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * To generate Ki, rotate C and D according
1870Sstevel@tonic-gate 	 * to schedule and pick up a permutation
1880Sstevel@tonic-gate 	 * using PC2.
1890Sstevel@tonic-gate 	 */
1900Sstevel@tonic-gate 	for (i=0; i<16; i++) {
1910Sstevel@tonic-gate 		/*
1920Sstevel@tonic-gate 		 * rotate.
1930Sstevel@tonic-gate 		 */
1940Sstevel@tonic-gate 		for (k=0; k<shifts[i]; k++) {
1950Sstevel@tonic-gate 			t = C[0];
1960Sstevel@tonic-gate 			for (j=0; j<28-1; j++)
1970Sstevel@tonic-gate 				C[j] = C[j+1];
1980Sstevel@tonic-gate 			C[27] = t;
1990Sstevel@tonic-gate 			t = D[0];
2000Sstevel@tonic-gate 			for (j=0; j<28-1; j++)
2010Sstevel@tonic-gate 				D[j] = D[j+1];
2020Sstevel@tonic-gate 			D[27] = t;
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 		/*
2050Sstevel@tonic-gate 		 * get Ki. Note C and D are concatenated.
2060Sstevel@tonic-gate 		 */
2070Sstevel@tonic-gate 		for (j=0; j<24; j++) {
2080Sstevel@tonic-gate 			KS[i][j] = C[PC2_C[j]-1];
2090Sstevel@tonic-gate 			KS[i][j+24] = D[PC2_D[j]-28-1];
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * The E bit-selection table.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate static	char	e[] = {
2180Sstevel@tonic-gate 	32, 1, 2, 3, 4, 5,
2190Sstevel@tonic-gate 	 4, 5, 6, 7, 8, 9,
2200Sstevel@tonic-gate 	 8, 9,10,11,12,13,
2210Sstevel@tonic-gate 	12,13,14,15,16,17,
2220Sstevel@tonic-gate 	16,17,18,19,20,21,
2230Sstevel@tonic-gate 	20,21,22,23,24,25,
2240Sstevel@tonic-gate 	24,25,26,27,28,29,
2250Sstevel@tonic-gate 	28,29,30,31,32, 1,
2260Sstevel@tonic-gate };
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * The 8 selection functions.
2300Sstevel@tonic-gate  * For some reason, they give a 0-origin
2310Sstevel@tonic-gate  * index, unlike everything else.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate static	char	S[8][64] = {
2340Sstevel@tonic-gate 	14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
2350Sstevel@tonic-gate 	 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
2360Sstevel@tonic-gate 	 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
2370Sstevel@tonic-gate 	15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
2400Sstevel@tonic-gate 	 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
2410Sstevel@tonic-gate 	 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
2420Sstevel@tonic-gate 	13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
2450Sstevel@tonic-gate 	13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
2460Sstevel@tonic-gate 	13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
2470Sstevel@tonic-gate 	 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	 7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
2500Sstevel@tonic-gate 	13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
2510Sstevel@tonic-gate 	10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
2520Sstevel@tonic-gate 	 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	 2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
2550Sstevel@tonic-gate 	14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
2560Sstevel@tonic-gate 	 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
2570Sstevel@tonic-gate 	11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
2600Sstevel@tonic-gate 	10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
2610Sstevel@tonic-gate 	 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
2620Sstevel@tonic-gate 	 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	 4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
2650Sstevel@tonic-gate 	13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
2660Sstevel@tonic-gate 	 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
2670Sstevel@tonic-gate 	 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
2700Sstevel@tonic-gate 	 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
2710Sstevel@tonic-gate 	 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
2720Sstevel@tonic-gate 	 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11,
2730Sstevel@tonic-gate };
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * P is a permutation on the selected combination
2770Sstevel@tonic-gate  * of the current L and key.
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate static	char	P[] = {
2800Sstevel@tonic-gate 	16, 7,20,21,
2810Sstevel@tonic-gate 	29,12,28,17,
2820Sstevel@tonic-gate 	 1,15,23,26,
2830Sstevel@tonic-gate 	 5,18,31,10,
2840Sstevel@tonic-gate 	 2, 8,24,14,
2850Sstevel@tonic-gate 	32,27, 3, 9,
2860Sstevel@tonic-gate 	19,13,30, 6,
2870Sstevel@tonic-gate 	22,11, 4,25,
2880Sstevel@tonic-gate };
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * The payoff: encrypt a block.
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate void
encrypt(char * block,int edflag)296*722Smuffin encrypt(char *block, int edflag)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	int	i, ii;
299*722Smuffin 	int t, j, k;
300*722Smuffin 	struct _crypt *_c = __crypt;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if (!_c) {
3030Sstevel@tonic-gate 		_cryptinit();
3040Sstevel@tonic-gate 		_c = __crypt;
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * First, permute the bits in the input
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	for (j=0; j<64; j++)
3100Sstevel@tonic-gate 		L[j] = block[IP[j]-1];
3110Sstevel@tonic-gate 	/*
3120Sstevel@tonic-gate 	 * Perform an encryption operation 16 times.
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	for (ii=0; ii<16; ii++) {
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 * Set direction
3170Sstevel@tonic-gate 		 */
3180Sstevel@tonic-gate #ifdef CRYPT
3190Sstevel@tonic-gate 		if (edflag)
3200Sstevel@tonic-gate 			i = 15-ii;
3210Sstevel@tonic-gate 		else
3220Sstevel@tonic-gate #endif
3230Sstevel@tonic-gate 			i = ii;
3240Sstevel@tonic-gate 		/*
3250Sstevel@tonic-gate 		 * Save the R array,
3260Sstevel@tonic-gate 		 * which will be the new L.
3270Sstevel@tonic-gate 		 */
3280Sstevel@tonic-gate 		for (j=0; j<32; j++)
3290Sstevel@tonic-gate 			tempL[j] = R[j];
3300Sstevel@tonic-gate 		/*
3310Sstevel@tonic-gate 		 * Expand R to 48 bits using the E selector;
3320Sstevel@tonic-gate 		 * exclusive-or with the current key bits.
3330Sstevel@tonic-gate 		 */
3340Sstevel@tonic-gate 		for (j=0; j<48; j++)
3350Sstevel@tonic-gate 			preS[j] = R[E[j]-1] ^ KS[i][j];
3360Sstevel@tonic-gate 		/*
3370Sstevel@tonic-gate 		 * The pre-select bits are now considered
3380Sstevel@tonic-gate 		 * in 8 groups of 6 bits each.
3390Sstevel@tonic-gate 		 * The 8 selection functions map these
3400Sstevel@tonic-gate 		 * 6-bit quantities into 4-bit quantities
3410Sstevel@tonic-gate 		 * and the results permuted
3420Sstevel@tonic-gate 		 * to make an f(R, K).
3430Sstevel@tonic-gate 		 * The indexing into the selection functions
3440Sstevel@tonic-gate 		 * is peculiar; it could be simplified by
3450Sstevel@tonic-gate 		 * rewriting the tables.
3460Sstevel@tonic-gate 		 */
3470Sstevel@tonic-gate 		for (j=0; j<8; j++) {
3480Sstevel@tonic-gate 			t = 6*j;
3490Sstevel@tonic-gate 			k = S[j][(preS[t+0]<<5)+
3500Sstevel@tonic-gate 				(preS[t+1]<<3)+
3510Sstevel@tonic-gate 				(preS[t+2]<<2)+
3520Sstevel@tonic-gate 				(preS[t+3]<<1)+
3530Sstevel@tonic-gate 				(preS[t+4]<<0)+
3540Sstevel@tonic-gate 				(preS[t+5]<<4)];
3550Sstevel@tonic-gate 			t = 4*j;
3560Sstevel@tonic-gate 			f[t+0] = (k>>3)&01;
3570Sstevel@tonic-gate 			f[t+1] = (k>>2)&01;
3580Sstevel@tonic-gate 			f[t+2] = (k>>1)&01;
3590Sstevel@tonic-gate 			f[t+3] = (k>>0)&01;
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 		/*
3620Sstevel@tonic-gate 		 * The new R is L ^ f(R, K).
3630Sstevel@tonic-gate 		 * The f here has to be permuted first, though.
3640Sstevel@tonic-gate 		 */
3650Sstevel@tonic-gate 		for (j=0; j<32; j++)
3660Sstevel@tonic-gate 			R[j] = L[j] ^ f[P[j]-1];
3670Sstevel@tonic-gate 		/*
3680Sstevel@tonic-gate 		 * Finally, the new L (the original R)
3690Sstevel@tonic-gate 		 * is copied back.
3700Sstevel@tonic-gate 		 */
3710Sstevel@tonic-gate 		for (j=0; j<32; j++)
3720Sstevel@tonic-gate 			L[j] = tempL[j];
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	/*
3750Sstevel@tonic-gate 	 * The output L and R are reversed.
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	for (j=0; j<32; j++) {
3780Sstevel@tonic-gate 		t = L[j];
3790Sstevel@tonic-gate 		L[j] = R[j];
3800Sstevel@tonic-gate 		R[j] = t;
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * The final output
3840Sstevel@tonic-gate 	 * gets the inverse permutation of the very original.
3850Sstevel@tonic-gate 	 */
3860Sstevel@tonic-gate 	for (j=0; j<64; j++)
3870Sstevel@tonic-gate 		block[j] = L[FP[j]-1];
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate char *
_crypt(char * pw,char * salt)391*722Smuffin _crypt(char *pw, char *salt)
3920Sstevel@tonic-gate {
393*722Smuffin 	int i, j, c;
3940Sstevel@tonic-gate 	int	temp;
395*722Smuffin 	struct _crypt *_c = __crypt;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (!_c) {
3980Sstevel@tonic-gate 		_cryptinit();
3990Sstevel@tonic-gate 		_c = __crypt;
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 	for(i=0; i<66; i++)
4020Sstevel@tonic-gate 		ablock[i] = 0;
4030Sstevel@tonic-gate 	for(i=0; (c= *pw) && i<64; pw++){
4040Sstevel@tonic-gate 		for(j=0; j<7; j++, i++)
4050Sstevel@tonic-gate 			ablock[i] = (c>>(6-j)) & 01;
4060Sstevel@tonic-gate 		i++;
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	setkey(ablock);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	for(i=0; i<66; i++)
4120Sstevel@tonic-gate 		ablock[i] = 0;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	for(i=0;i<48;i++)
4150Sstevel@tonic-gate 		E[i] = e[i];
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	for(i=0;i<2;i++){
4180Sstevel@tonic-gate 		c = *salt++;
4190Sstevel@tonic-gate 		iobuf[i] = c;
4200Sstevel@tonic-gate 		if(c > 'Z')
4210Sstevel@tonic-gate 			c -= 6;
4220Sstevel@tonic-gate 		if(c > '9')
4230Sstevel@tonic-gate 			c -= 7;
4240Sstevel@tonic-gate 		c -= '.';
4250Sstevel@tonic-gate 		for(j=0;j<6;j++){
4260Sstevel@tonic-gate 			if((c>>j) & 01){
4270Sstevel@tonic-gate 				temp = E[6*i+j];
4280Sstevel@tonic-gate 				E[6*i+j] = E[6*i+j+24];
4290Sstevel@tonic-gate 				E[6*i+j+24] = temp;
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	for(i=0; i<25; i++)
4350Sstevel@tonic-gate 		encrypt(ablock,0);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	for(i=0; i < 11; i++) {
4380Sstevel@tonic-gate 		c = 0;
4390Sstevel@tonic-gate 		for(j=0; j<6; j++){
4400Sstevel@tonic-gate 			c <<= 1;
4410Sstevel@tonic-gate 			c |= ablock[6*i+j];
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 		c += '.';
4440Sstevel@tonic-gate 		if(c > '9')
4450Sstevel@tonic-gate 			c += 7;
4460Sstevel@tonic-gate 		if(c > 'Z')
4470Sstevel@tonic-gate 			c += 6;
4480Sstevel@tonic-gate 		iobuf[i+2] = c;
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 	iobuf[i+2] = 0;
4510Sstevel@tonic-gate 	if(iobuf[1]==0)
4520Sstevel@tonic-gate 		iobuf[1] = iobuf[0];
4530Sstevel@tonic-gate 	return(iobuf);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
456*722Smuffin static void
_cryptinit(void)457*722Smuffin _cryptinit(void)
4580Sstevel@tonic-gate {
459*722Smuffin 	struct _crypt *_c = __crypt;
460*722Smuffin 	int i;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	if (_c)
4630Sstevel@tonic-gate 		return;
4640Sstevel@tonic-gate 	_c = __crypt = (struct _crypt *)calloc(1, sizeof (struct _crypt));
4650Sstevel@tonic-gate 	if (_c == 0)
4660Sstevel@tonic-gate 		abort();
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	for(i=0;i<48;i++)
4690Sstevel@tonic-gate 		E[i] = e[i];
4700Sstevel@tonic-gate }
471