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  */
22*1219Sraf 
230Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
240Sstevel@tonic-gate /*	  All Rights Reserved  	*/
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #pragma weak des_crypt = _des_crypt
350Sstevel@tonic-gate #pragma weak des_encrypt = _des_encrypt
360Sstevel@tonic-gate #pragma weak des_setkey = _des_setkey
370Sstevel@tonic-gate 
38*1219Sraf #include "des_synonyms.h"
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <crypt.h>
410Sstevel@tonic-gate #include "des_soft.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <thread.h>
450Sstevel@tonic-gate #include <synch.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /* EXPORT DELETE START */
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * This program implements the
510Sstevel@tonic-gate  * Proposed Federal Information Processing
520Sstevel@tonic-gate  *  Data Encryption Standard.
530Sstevel@tonic-gate  * See Federal Register, March 17, 1975 (40FR12134)
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * Initial permutation,
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate static char IP[] = {
600Sstevel@tonic-gate 	58, 50, 42, 34, 26, 18, 10, 2,
610Sstevel@tonic-gate 	60, 52, 44, 36, 28, 20, 12, 4,
620Sstevel@tonic-gate 	62, 54, 46, 38, 30, 22, 14, 6,
630Sstevel@tonic-gate 	64, 56, 48, 40, 32, 24, 16, 8,
640Sstevel@tonic-gate 	57, 49, 41, 33, 25, 17, 9, 1,
650Sstevel@tonic-gate 	59, 51, 43, 35, 27, 19, 11, 3,
660Sstevel@tonic-gate 	61, 53, 45, 37, 29, 21, 13, 5,
670Sstevel@tonic-gate 	63, 55, 47, 39, 31, 23, 15, 7,
680Sstevel@tonic-gate };
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Final permutation, FP = IP^(-1)
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate static char FP[] = {
740Sstevel@tonic-gate 	40, 8, 48, 16, 56, 24, 64, 32,
750Sstevel@tonic-gate 	39, 7, 47, 15, 55, 23, 63, 31,
760Sstevel@tonic-gate 	38, 6, 46, 14, 54, 22, 62, 30,
770Sstevel@tonic-gate 	37, 5, 45, 13, 53, 21, 61, 29,
780Sstevel@tonic-gate 	36, 4, 44, 12, 52, 20, 60, 28,
790Sstevel@tonic-gate 	35, 3, 43, 11, 51, 19, 59, 27,
800Sstevel@tonic-gate 	34, 2, 42, 10, 50, 18, 58, 26,
810Sstevel@tonic-gate 	33, 1, 41, 9, 49, 17, 57, 25,
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Permuted-choice 1 from the key bits
860Sstevel@tonic-gate  * to yield C and D.
870Sstevel@tonic-gate  * Note that bits 8, 16... are left out:
880Sstevel@tonic-gate  * They are intended for a parity check.
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate static char PC1_C[] = {
910Sstevel@tonic-gate 	57, 49, 41, 33, 25, 17, 9,
920Sstevel@tonic-gate 	1, 58, 50, 42, 34, 26, 18,
930Sstevel@tonic-gate 	10, 2, 59, 51, 43, 35, 27,
940Sstevel@tonic-gate 	19, 11, 3, 60, 52, 44, 36,
950Sstevel@tonic-gate };
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static char PC1_D[] = {
980Sstevel@tonic-gate 	63, 55, 47, 39, 31, 23, 15,
990Sstevel@tonic-gate 	7, 62, 54, 46, 38, 30, 22,
1000Sstevel@tonic-gate 	14, 6, 61, 53, 45, 37, 29,
1010Sstevel@tonic-gate 	21, 13, 5, 28, 20, 12, 4,
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Sequence of shifts used for the key schedule.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate static char shifts[] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, };
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Permuted-choice 2, to pick out the bits from
1110Sstevel@tonic-gate  * the CD array that generate the key schedule.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate static char PC2_C[] = {
1140Sstevel@tonic-gate 	14, 17, 11, 24, 1, 5,
1150Sstevel@tonic-gate 	3, 28, 15, 6, 21, 10,
1160Sstevel@tonic-gate 	23, 19, 12, 4, 26, 8,
1170Sstevel@tonic-gate 	16, 7, 27, 20, 13, 2,
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static char PC2_D[] = {
1210Sstevel@tonic-gate 	41, 52, 31, 37, 47, 55,
1220Sstevel@tonic-gate 	30, 40, 51, 45, 33, 48,
1230Sstevel@tonic-gate 	44, 49, 39, 56, 34, 53,
1240Sstevel@tonic-gate 	46, 42, 50, 36, 29, 32,
1250Sstevel@tonic-gate };
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * The C and D arrays used to calculate the key schedule.
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate static char C[28];
1320Sstevel@tonic-gate static char D[28];
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * The key schedule.
1350Sstevel@tonic-gate  * Generated from the key.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate static char KS[16][48];
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * The E bit-selection table.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate static char E[48];
1430Sstevel@tonic-gate static char e2[] = {
1440Sstevel@tonic-gate 	32, 1, 2, 3, 4, 5,
1450Sstevel@tonic-gate 	4, 5, 6, 7, 8, 9,
1460Sstevel@tonic-gate 	8, 9, 10, 11, 12, 13,
1470Sstevel@tonic-gate 	12, 13, 14, 15, 16, 17,
1480Sstevel@tonic-gate 	16, 17, 18, 19, 20, 21,
1490Sstevel@tonic-gate 	20, 21, 22, 23, 24, 25,
1500Sstevel@tonic-gate 	24, 25, 26, 27, 28, 29,
1510Sstevel@tonic-gate 	28, 29, 30, 31, 32, 1,
1520Sstevel@tonic-gate };
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * Set up the key schedule from the key.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static mutex_t lock = DEFAULTMUTEX;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /* EXPORT DELETE END */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static void
1640Sstevel@tonic-gate des_setkey_nolock(const char *key)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate /* EXPORT DELETE START */
1670Sstevel@tonic-gate 	int i, j, k;
1680Sstevel@tonic-gate 	char t;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * First, generate C and D by permuting
1720Sstevel@tonic-gate 	 * the key.  The low order bit of each
1730Sstevel@tonic-gate 	 * 8-bit char is not used, so C and D are only 28
1740Sstevel@tonic-gate 	 * bits apiece.
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 	for (i = 0; i < 28; i++) {
1770Sstevel@tonic-gate 		C[i] = key[PC1_C[i]-1];
1780Sstevel@tonic-gate 		D[i] = key[PC1_D[i]-1];
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * To generate Ki, rotate C and D according
1820Sstevel@tonic-gate 	 * to schedule and pick up a permutation
1830Sstevel@tonic-gate 	 * using PC2.
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
1860Sstevel@tonic-gate 		/*
1870Sstevel@tonic-gate 		 * rotate.
1880Sstevel@tonic-gate 		 */
1890Sstevel@tonic-gate 		for (k = 0; k < shifts[i]; k++) {
1900Sstevel@tonic-gate 			t = C[0];
1910Sstevel@tonic-gate 			for (j = 0; j < 28-1; j++)
1920Sstevel@tonic-gate 				C[j] = C[j+1];
1930Sstevel@tonic-gate 			C[27] = (char)t;
1940Sstevel@tonic-gate 			t = D[0];
1950Sstevel@tonic-gate 			for (j = 0; j < 28-1; j++)
1960Sstevel@tonic-gate 				D[j] = D[j+1];
1970Sstevel@tonic-gate 			D[27] = (char)t;
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 		/*
2000Sstevel@tonic-gate 		 * get Ki. Note C and D are concatenated.
2010Sstevel@tonic-gate 		 */
2020Sstevel@tonic-gate 		for (j = 0; j < 24; j++) {
2030Sstevel@tonic-gate 			KS[i][j] = C[PC2_C[j]-1];
2040Sstevel@tonic-gate 			KS[i][j+24] = D[PC2_D[j]-28-1];
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	for (i = 0; i < 48; i++)
2090Sstevel@tonic-gate 		E[i] = e2[i];
2100Sstevel@tonic-gate /* EXPORT DELETE END */
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate void
2140Sstevel@tonic-gate des_setkey(const char *key)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate /* EXPORT DELETE START */
2170Sstevel@tonic-gate 	(void) mutex_lock(&lock);
2180Sstevel@tonic-gate 	des_setkey_nolock(key);
2190Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
2200Sstevel@tonic-gate /* EXPORT DELETE END */
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /* EXPORT DELETE START */
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * The 8 selection functions.
2260Sstevel@tonic-gate  * For some reason, they give a 0-origin
2270Sstevel@tonic-gate  * index, unlike everything else.
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate static char S[8][64] = {
2300Sstevel@tonic-gate 	14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
2310Sstevel@tonic-gate 	0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
2320Sstevel@tonic-gate 	4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
2330Sstevel@tonic-gate 	15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
2360Sstevel@tonic-gate 	3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
2370Sstevel@tonic-gate 	0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
2380Sstevel@tonic-gate 	13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
2410Sstevel@tonic-gate 	13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
2420Sstevel@tonic-gate 	13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
2430Sstevel@tonic-gate 	1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
2460Sstevel@tonic-gate 	13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
2470Sstevel@tonic-gate 	10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
2480Sstevel@tonic-gate 	3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
2510Sstevel@tonic-gate 	14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
2520Sstevel@tonic-gate 	4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
2530Sstevel@tonic-gate 	11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
2560Sstevel@tonic-gate 	10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
2570Sstevel@tonic-gate 	9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
2580Sstevel@tonic-gate 	4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
2610Sstevel@tonic-gate 	13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
2620Sstevel@tonic-gate 	1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
2630Sstevel@tonic-gate 	6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
2660Sstevel@tonic-gate 	1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
2670Sstevel@tonic-gate 	7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2680Sstevel@tonic-gate 	2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11,
2690Sstevel@tonic-gate };
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate  * P is a permutation on the selected combination
2730Sstevel@tonic-gate  * of the current L and key.
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate static char P[] = {
2760Sstevel@tonic-gate 	16, 7, 20, 21,
2770Sstevel@tonic-gate 	29, 12, 28, 17,
2780Sstevel@tonic-gate 	1, 15, 23, 26,
2790Sstevel@tonic-gate 	5, 18, 31, 10,
2800Sstevel@tonic-gate 	2, 8, 24, 14,
2810Sstevel@tonic-gate 	32, 27, 3, 9,
2820Sstevel@tonic-gate 	19, 13, 30, 6,
2830Sstevel@tonic-gate 	22, 11, 4, 25,
2840Sstevel@tonic-gate };
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  * The current block, divided into 2 halves.
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate static char L[64];
2900Sstevel@tonic-gate static char tempL[32];
2910Sstevel@tonic-gate static char f[32];
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * The combination of the key and the input, before selection.
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate static char preS[48];
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate  * The payoff: encrypt a block.
3000Sstevel@tonic-gate  */
3010Sstevel@tonic-gate /* EXPORT DELETE END */
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate static void
3040Sstevel@tonic-gate des_encrypt_nolock(char *block, int edflag)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate /* EXPORT DELETE START */
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (edflag)
3090Sstevel@tonic-gate 		(void) des_decrypt1(block, L, IP, &L[32],
3100Sstevel@tonic-gate 		    preS, E, KS, S, f, tempL, P, FP);
3110Sstevel@tonic-gate 	else
3120Sstevel@tonic-gate 		(void) des_encrypt1(block, L, IP, &L[32],
3130Sstevel@tonic-gate 		    preS, E, KS, S, f, tempL, P, FP);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /* EXPORT DELETE END */
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate void
3190Sstevel@tonic-gate des_encrypt(char *block, int edflag)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate /* EXPORT DELETE START */
3220Sstevel@tonic-gate 	(void) mutex_lock(&lock);
3230Sstevel@tonic-gate 	des_encrypt_nolock(block, edflag);
3240Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
3250Sstevel@tonic-gate /* EXPORT DELETE END */
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate #define	IOBUF_SIZE	16
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static char *
3330Sstevel@tonic-gate _get_iobuf(thread_key_t *key, unsigned size)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	char *iobuf = NULL;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (thr_getspecific(*key, (void **)&iobuf) != 0) {
338*1219Sraf 		if (thr_keycreate(key, free) != 0) {
3390Sstevel@tonic-gate 			return (NULL);
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (!iobuf) {
344*1219Sraf 		if (thr_setspecific(*key, (void *)(iobuf = malloc(size)))
3450Sstevel@tonic-gate 			!= 0) {
3460Sstevel@tonic-gate 			if (iobuf)
3470Sstevel@tonic-gate 				(void) free(iobuf);
3480Sstevel@tonic-gate 			return (NULL);
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 	return (iobuf);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate char *
3550Sstevel@tonic-gate des_crypt(const char *pw, const char *salt)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate /* EXPORT DELETE START */
3580Sstevel@tonic-gate 	int	i, j;
3590Sstevel@tonic-gate 	char	c, temp;
3600Sstevel@tonic-gate 	static thread_key_t key = 0;
3610Sstevel@tonic-gate 	char block[66], *iobuf = _get_iobuf(&key, IOBUF_SIZE);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	(void) mutex_lock(&lock);
3640Sstevel@tonic-gate 	for (i = 0; i < 66; i++)
3650Sstevel@tonic-gate 		block[i] = 0;
3660Sstevel@tonic-gate 	for (i = 0; (c = *pw) && (i < 64); pw++) {
3670Sstevel@tonic-gate 		for (j = 0; j < 7; j++, i++)
3680Sstevel@tonic-gate 			block[i] = (c>>(6-j)) & 01;
3690Sstevel@tonic-gate 		i++;
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	des_setkey_nolock(block);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	for (i = 0; i < 66; i++)
3750Sstevel@tonic-gate 		block[i] = 0;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
3780Sstevel@tonic-gate 		c = *salt++;
3790Sstevel@tonic-gate 		iobuf[i] = (char)c;
3800Sstevel@tonic-gate 		if (c > 'Z')
3810Sstevel@tonic-gate 			c -= 6;
3820Sstevel@tonic-gate 		if (c > '9')
3830Sstevel@tonic-gate 			c -= 7;
3840Sstevel@tonic-gate 		c -= '.';
3850Sstevel@tonic-gate 		for (j = 0; j < 6; j++) {
3860Sstevel@tonic-gate 			if ((c>>j) & 01) {
3870Sstevel@tonic-gate 				temp = E[6*i+j];
3880Sstevel@tonic-gate 				E[6*i+j] = E[6*i+j+24];
3890Sstevel@tonic-gate 				E[6*i+j+24] = (char)temp;
3900Sstevel@tonic-gate 			}
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	for (i = 0; i < 25; i++)
3950Sstevel@tonic-gate 		(void) des_encrypt_nolock(block, 0);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	for (i = 0; i < 11; i++) {
3980Sstevel@tonic-gate 		c = 0;
3990Sstevel@tonic-gate 		for (j = 0; j < 6; j++) {
4000Sstevel@tonic-gate 			c <<= 1;
4010Sstevel@tonic-gate 			c |= block[6*i+j];
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 		c += '.';
4040Sstevel@tonic-gate 		if (c > '9')
4050Sstevel@tonic-gate 			c += 7;
4060Sstevel@tonic-gate 		if (c > 'Z')
4070Sstevel@tonic-gate 			c += 6;
4080Sstevel@tonic-gate 		iobuf[i+2] = (char)c;
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	iobuf[i+2] = 0;
4110Sstevel@tonic-gate 	if (iobuf[1] == 0)
4120Sstevel@tonic-gate 		iobuf[1] = iobuf[0];
4130Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
4140Sstevel@tonic-gate 	return (iobuf);
4150Sstevel@tonic-gate #if 0
4160Sstevel@tonic-gate /* EXPORT DELETE END */
4170Sstevel@tonic-gate 	return (0);
4180Sstevel@tonic-gate /* EXPORT DELETE START */
4190Sstevel@tonic-gate #endif
4200Sstevel@tonic-gate /* EXPORT DELETE END */
4210Sstevel@tonic-gate }
422