1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate /*LINTLIBRARY*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma weak des_crypt = _des_crypt
35*0Sstevel@tonic-gate #pragma weak des_encrypt = _des_encrypt
36*0Sstevel@tonic-gate #pragma weak des_setkey = _des_setkey
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "synonyms.h"
39*0Sstevel@tonic-gate #include "mtlib.h"
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <crypt.h>
42*0Sstevel@tonic-gate #include "des_soft.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include <stdlib.h>
45*0Sstevel@tonic-gate #include <thread.h>
46*0Sstevel@tonic-gate #include <synch.h>
47*0Sstevel@tonic-gate #include <sys/types.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /* EXPORT DELETE START */
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * This program implements the
52*0Sstevel@tonic-gate  * Proposed Federal Information Processing
53*0Sstevel@tonic-gate  *  Data Encryption Standard.
54*0Sstevel@tonic-gate  * See Federal Register, March 17, 1975 (40FR12134)
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * Initial permutation,
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate static char IP[] = {
61*0Sstevel@tonic-gate 	58, 50, 42, 34, 26, 18, 10, 2,
62*0Sstevel@tonic-gate 	60, 52, 44, 36, 28, 20, 12, 4,
63*0Sstevel@tonic-gate 	62, 54, 46, 38, 30, 22, 14, 6,
64*0Sstevel@tonic-gate 	64, 56, 48, 40, 32, 24, 16, 8,
65*0Sstevel@tonic-gate 	57, 49, 41, 33, 25, 17, 9, 1,
66*0Sstevel@tonic-gate 	59, 51, 43, 35, 27, 19, 11, 3,
67*0Sstevel@tonic-gate 	61, 53, 45, 37, 29, 21, 13, 5,
68*0Sstevel@tonic-gate 	63, 55, 47, 39, 31, 23, 15, 7,
69*0Sstevel@tonic-gate };
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * Final permutation, FP = IP^(-1)
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate static char FP[] = {
75*0Sstevel@tonic-gate 	40, 8, 48, 16, 56, 24, 64, 32,
76*0Sstevel@tonic-gate 	39, 7, 47, 15, 55, 23, 63, 31,
77*0Sstevel@tonic-gate 	38, 6, 46, 14, 54, 22, 62, 30,
78*0Sstevel@tonic-gate 	37, 5, 45, 13, 53, 21, 61, 29,
79*0Sstevel@tonic-gate 	36, 4, 44, 12, 52, 20, 60, 28,
80*0Sstevel@tonic-gate 	35, 3, 43, 11, 51, 19, 59, 27,
81*0Sstevel@tonic-gate 	34, 2, 42, 10, 50, 18, 58, 26,
82*0Sstevel@tonic-gate 	33, 1, 41, 9, 49, 17, 57, 25,
83*0Sstevel@tonic-gate };
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /*
86*0Sstevel@tonic-gate  * Permuted-choice 1 from the key bits
87*0Sstevel@tonic-gate  * to yield C and D.
88*0Sstevel@tonic-gate  * Note that bits 8, 16... are left out:
89*0Sstevel@tonic-gate  * They are intended for a parity check.
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate static char PC1_C[] = {
92*0Sstevel@tonic-gate 	57, 49, 41, 33, 25, 17, 9,
93*0Sstevel@tonic-gate 	1, 58, 50, 42, 34, 26, 18,
94*0Sstevel@tonic-gate 	10, 2, 59, 51, 43, 35, 27,
95*0Sstevel@tonic-gate 	19, 11, 3, 60, 52, 44, 36,
96*0Sstevel@tonic-gate };
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate static char PC1_D[] = {
99*0Sstevel@tonic-gate 	63, 55, 47, 39, 31, 23, 15,
100*0Sstevel@tonic-gate 	7, 62, 54, 46, 38, 30, 22,
101*0Sstevel@tonic-gate 	14, 6, 61, 53, 45, 37, 29,
102*0Sstevel@tonic-gate 	21, 13, 5, 28, 20, 12, 4,
103*0Sstevel@tonic-gate };
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate  * Sequence of shifts used for the key schedule.
107*0Sstevel@tonic-gate  */
108*0Sstevel@tonic-gate static char shifts[] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, };
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate  * Permuted-choice 2, to pick out the bits from
112*0Sstevel@tonic-gate  * the CD array that generate the key schedule.
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate static char PC2_C[] = {
115*0Sstevel@tonic-gate 	14, 17, 11, 24, 1, 5,
116*0Sstevel@tonic-gate 	3, 28, 15, 6, 21, 10,
117*0Sstevel@tonic-gate 	23, 19, 12, 4, 26, 8,
118*0Sstevel@tonic-gate 	16, 7, 27, 20, 13, 2,
119*0Sstevel@tonic-gate };
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate static char PC2_D[] = {
122*0Sstevel@tonic-gate 	41, 52, 31, 37, 47, 55,
123*0Sstevel@tonic-gate 	30, 40, 51, 45, 33, 48,
124*0Sstevel@tonic-gate 	44, 49, 39, 56, 34, 53,
125*0Sstevel@tonic-gate 	46, 42, 50, 36, 29, 32,
126*0Sstevel@tonic-gate };
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate /*
129*0Sstevel@tonic-gate  * The C and D arrays used to calculate the key schedule.
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate static char C[28];
133*0Sstevel@tonic-gate static char D[28];
134*0Sstevel@tonic-gate /*
135*0Sstevel@tonic-gate  * The key schedule.
136*0Sstevel@tonic-gate  * Generated from the key.
137*0Sstevel@tonic-gate  */
138*0Sstevel@tonic-gate static char KS[16][48];
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate /*
141*0Sstevel@tonic-gate  * The E bit-selection table.
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate static char E[48];
144*0Sstevel@tonic-gate static char e2[] = {
145*0Sstevel@tonic-gate 	32, 1, 2, 3, 4, 5,
146*0Sstevel@tonic-gate 	4, 5, 6, 7, 8, 9,
147*0Sstevel@tonic-gate 	8, 9, 10, 11, 12, 13,
148*0Sstevel@tonic-gate 	12, 13, 14, 15, 16, 17,
149*0Sstevel@tonic-gate 	16, 17, 18, 19, 20, 21,
150*0Sstevel@tonic-gate 	20, 21, 22, 23, 24, 25,
151*0Sstevel@tonic-gate 	24, 25, 26, 27, 28, 29,
152*0Sstevel@tonic-gate 	28, 29, 30, 31, 32, 1,
153*0Sstevel@tonic-gate };
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate  * Set up the key schedule from the key.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate static mutex_t lock = DEFAULTMUTEX;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate /* EXPORT DELETE END */
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate static void
165*0Sstevel@tonic-gate des_setkey_nolock(const char *key)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate /* EXPORT DELETE START */
168*0Sstevel@tonic-gate 	int i, j, k;
169*0Sstevel@tonic-gate 	char t;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	/*
172*0Sstevel@tonic-gate 	 * First, generate C and D by permuting
173*0Sstevel@tonic-gate 	 * the key.  The low order bit of each
174*0Sstevel@tonic-gate 	 * 8-bit char is not used, so C and D are only 28
175*0Sstevel@tonic-gate 	 * bits apiece.
176*0Sstevel@tonic-gate 	 */
177*0Sstevel@tonic-gate 	for (i = 0; i < 28; i++) {
178*0Sstevel@tonic-gate 		C[i] = key[PC1_C[i]-1];
179*0Sstevel@tonic-gate 		D[i] = key[PC1_D[i]-1];
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 	/*
182*0Sstevel@tonic-gate 	 * To generate Ki, rotate C and D according
183*0Sstevel@tonic-gate 	 * to schedule and pick up a permutation
184*0Sstevel@tonic-gate 	 * using PC2.
185*0Sstevel@tonic-gate 	 */
186*0Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
187*0Sstevel@tonic-gate 		/*
188*0Sstevel@tonic-gate 		 * rotate.
189*0Sstevel@tonic-gate 		 */
190*0Sstevel@tonic-gate 		for (k = 0; k < shifts[i]; k++) {
191*0Sstevel@tonic-gate 			t = C[0];
192*0Sstevel@tonic-gate 			for (j = 0; j < 28-1; j++)
193*0Sstevel@tonic-gate 				C[j] = C[j+1];
194*0Sstevel@tonic-gate 			C[27] = (char)t;
195*0Sstevel@tonic-gate 			t = D[0];
196*0Sstevel@tonic-gate 			for (j = 0; j < 28-1; j++)
197*0Sstevel@tonic-gate 				D[j] = D[j+1];
198*0Sstevel@tonic-gate 			D[27] = (char)t;
199*0Sstevel@tonic-gate 		}
200*0Sstevel@tonic-gate 		/*
201*0Sstevel@tonic-gate 		 * get Ki. Note C and D are concatenated.
202*0Sstevel@tonic-gate 		 */
203*0Sstevel@tonic-gate 		for (j = 0; j < 24; j++) {
204*0Sstevel@tonic-gate 			KS[i][j] = C[PC2_C[j]-1];
205*0Sstevel@tonic-gate 			KS[i][j+24] = D[PC2_D[j]-28-1];
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	for (i = 0; i < 48; i++)
210*0Sstevel@tonic-gate 		E[i] = e2[i];
211*0Sstevel@tonic-gate /* EXPORT DELETE END */
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate void
215*0Sstevel@tonic-gate des_setkey(const char *key)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate /* EXPORT DELETE START */
218*0Sstevel@tonic-gate 	(void) mutex_lock(&lock);
219*0Sstevel@tonic-gate 	des_setkey_nolock(key);
220*0Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
221*0Sstevel@tonic-gate /* EXPORT DELETE END */
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate /* EXPORT DELETE START */
225*0Sstevel@tonic-gate /*
226*0Sstevel@tonic-gate  * The 8 selection functions.
227*0Sstevel@tonic-gate  * For some reason, they give a 0-origin
228*0Sstevel@tonic-gate  * index, unlike everything else.
229*0Sstevel@tonic-gate  */
230*0Sstevel@tonic-gate static char S[8][64] = {
231*0Sstevel@tonic-gate 	14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
232*0Sstevel@tonic-gate 	0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
233*0Sstevel@tonic-gate 	4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
234*0Sstevel@tonic-gate 	15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
237*0Sstevel@tonic-gate 	3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
238*0Sstevel@tonic-gate 	0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
239*0Sstevel@tonic-gate 	13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
242*0Sstevel@tonic-gate 	13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
243*0Sstevel@tonic-gate 	13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
244*0Sstevel@tonic-gate 	1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
247*0Sstevel@tonic-gate 	13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
248*0Sstevel@tonic-gate 	10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
249*0Sstevel@tonic-gate 	3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
252*0Sstevel@tonic-gate 	14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
253*0Sstevel@tonic-gate 	4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
254*0Sstevel@tonic-gate 	11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
257*0Sstevel@tonic-gate 	10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
258*0Sstevel@tonic-gate 	9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
259*0Sstevel@tonic-gate 	4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
262*0Sstevel@tonic-gate 	13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
263*0Sstevel@tonic-gate 	1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
264*0Sstevel@tonic-gate 	6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
267*0Sstevel@tonic-gate 	1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
268*0Sstevel@tonic-gate 	7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
269*0Sstevel@tonic-gate 	2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11,
270*0Sstevel@tonic-gate };
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate /*
273*0Sstevel@tonic-gate  * P is a permutation on the selected combination
274*0Sstevel@tonic-gate  * of the current L and key.
275*0Sstevel@tonic-gate  */
276*0Sstevel@tonic-gate static char P[] = {
277*0Sstevel@tonic-gate 	16, 7, 20, 21,
278*0Sstevel@tonic-gate 	29, 12, 28, 17,
279*0Sstevel@tonic-gate 	1, 15, 23, 26,
280*0Sstevel@tonic-gate 	5, 18, 31, 10,
281*0Sstevel@tonic-gate 	2, 8, 24, 14,
282*0Sstevel@tonic-gate 	32, 27, 3, 9,
283*0Sstevel@tonic-gate 	19, 13, 30, 6,
284*0Sstevel@tonic-gate 	22, 11, 4, 25,
285*0Sstevel@tonic-gate };
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate /*
288*0Sstevel@tonic-gate  * The current block, divided into 2 halves.
289*0Sstevel@tonic-gate  */
290*0Sstevel@tonic-gate static char L[64];
291*0Sstevel@tonic-gate static char tempL[32];
292*0Sstevel@tonic-gate static char f[32];
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate /*
295*0Sstevel@tonic-gate  * The combination of the key and the input, before selection.
296*0Sstevel@tonic-gate  */
297*0Sstevel@tonic-gate static char preS[48];
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate /*
300*0Sstevel@tonic-gate  * The payoff: encrypt a block.
301*0Sstevel@tonic-gate  */
302*0Sstevel@tonic-gate /* EXPORT DELETE END */
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate static void
305*0Sstevel@tonic-gate des_encrypt_nolock(char *block, int edflag)
306*0Sstevel@tonic-gate {
307*0Sstevel@tonic-gate /* EXPORT DELETE START */
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	if (edflag)
310*0Sstevel@tonic-gate 		(void) des_decrypt1(block, L, IP, &L[32],
311*0Sstevel@tonic-gate 		    preS, E, KS, S, f, tempL, P, FP);
312*0Sstevel@tonic-gate 	else
313*0Sstevel@tonic-gate 		(void) des_encrypt1(block, L, IP, &L[32],
314*0Sstevel@tonic-gate 		    preS, E, KS, S, f, tempL, P, FP);
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate /* EXPORT DELETE END */
317*0Sstevel@tonic-gate }
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate void
320*0Sstevel@tonic-gate des_encrypt(char *block, int edflag)
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate /* EXPORT DELETE START */
323*0Sstevel@tonic-gate 	(void) mutex_lock(&lock);
324*0Sstevel@tonic-gate 	des_encrypt_nolock(block, edflag);
325*0Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
326*0Sstevel@tonic-gate /* EXPORT DELETE END */
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate #define	IOBUF_SIZE	16
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate static char *
334*0Sstevel@tonic-gate _get_iobuf(thread_key_t *key, unsigned size)
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 	char *iobuf = NULL;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	if (thr_getspecific(*key, (void **)&iobuf) != 0) {
339*0Sstevel@tonic-gate 		if (_thr_keycreate(key, free) != 0) {
340*0Sstevel@tonic-gate 			return (NULL);
341*0Sstevel@tonic-gate 		}
342*0Sstevel@tonic-gate 	}
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	if (!iobuf) {
345*0Sstevel@tonic-gate 		if (_thr_setspecific(*key, (void *)(iobuf = malloc(size)))
346*0Sstevel@tonic-gate 			!= 0) {
347*0Sstevel@tonic-gate 			if (iobuf)
348*0Sstevel@tonic-gate 				(void) free(iobuf);
349*0Sstevel@tonic-gate 			return (NULL);
350*0Sstevel@tonic-gate 		}
351*0Sstevel@tonic-gate 	}
352*0Sstevel@tonic-gate 	return (iobuf);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate char *
356*0Sstevel@tonic-gate des_crypt(const char *pw, const char *salt)
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate /* EXPORT DELETE START */
359*0Sstevel@tonic-gate 	int	i, j;
360*0Sstevel@tonic-gate 	char	c, temp;
361*0Sstevel@tonic-gate 	static thread_key_t key = 0;
362*0Sstevel@tonic-gate 	char block[66], *iobuf = _get_iobuf(&key, IOBUF_SIZE);
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	(void) mutex_lock(&lock);
365*0Sstevel@tonic-gate 	for (i = 0; i < 66; i++)
366*0Sstevel@tonic-gate 		block[i] = 0;
367*0Sstevel@tonic-gate 	for (i = 0; (c = *pw) && (i < 64); pw++) {
368*0Sstevel@tonic-gate 		for (j = 0; j < 7; j++, i++)
369*0Sstevel@tonic-gate 			block[i] = (c>>(6-j)) & 01;
370*0Sstevel@tonic-gate 		i++;
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	des_setkey_nolock(block);
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	for (i = 0; i < 66; i++)
376*0Sstevel@tonic-gate 		block[i] = 0;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
379*0Sstevel@tonic-gate 		c = *salt++;
380*0Sstevel@tonic-gate 		iobuf[i] = (char)c;
381*0Sstevel@tonic-gate 		if (c > 'Z')
382*0Sstevel@tonic-gate 			c -= 6;
383*0Sstevel@tonic-gate 		if (c > '9')
384*0Sstevel@tonic-gate 			c -= 7;
385*0Sstevel@tonic-gate 		c -= '.';
386*0Sstevel@tonic-gate 		for (j = 0; j < 6; j++) {
387*0Sstevel@tonic-gate 			if ((c>>j) & 01) {
388*0Sstevel@tonic-gate 				temp = E[6*i+j];
389*0Sstevel@tonic-gate 				E[6*i+j] = E[6*i+j+24];
390*0Sstevel@tonic-gate 				E[6*i+j+24] = (char)temp;
391*0Sstevel@tonic-gate 			}
392*0Sstevel@tonic-gate 		}
393*0Sstevel@tonic-gate 	}
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	for (i = 0; i < 25; i++)
396*0Sstevel@tonic-gate 		(void) des_encrypt_nolock(block, 0);
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	for (i = 0; i < 11; i++) {
399*0Sstevel@tonic-gate 		c = 0;
400*0Sstevel@tonic-gate 		for (j = 0; j < 6; j++) {
401*0Sstevel@tonic-gate 			c <<= 1;
402*0Sstevel@tonic-gate 			c |= block[6*i+j];
403*0Sstevel@tonic-gate 		}
404*0Sstevel@tonic-gate 		c += '.';
405*0Sstevel@tonic-gate 		if (c > '9')
406*0Sstevel@tonic-gate 			c += 7;
407*0Sstevel@tonic-gate 		if (c > 'Z')
408*0Sstevel@tonic-gate 			c += 6;
409*0Sstevel@tonic-gate 		iobuf[i+2] = (char)c;
410*0Sstevel@tonic-gate 	}
411*0Sstevel@tonic-gate 	iobuf[i+2] = 0;
412*0Sstevel@tonic-gate 	if (iobuf[1] == 0)
413*0Sstevel@tonic-gate 		iobuf[1] = iobuf[0];
414*0Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
415*0Sstevel@tonic-gate 	return (iobuf);
416*0Sstevel@tonic-gate #if 0
417*0Sstevel@tonic-gate /* EXPORT DELETE END */
418*0Sstevel@tonic-gate 	return (0);
419*0Sstevel@tonic-gate /* EXPORT DELETE START */
420*0Sstevel@tonic-gate #endif
421*0Sstevel@tonic-gate /* EXPORT DELETE END */
422*0Sstevel@tonic-gate }
423