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_encrypt1 = _des_encrypt1
350Sstevel@tonic-gate 
36*1219Sraf #include "des_synonyms.h"
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate void
40*1219Sraf des_encrypt1(char *block, char *L, char *IP, char *R, char *preS, char *E,
41*1219Sraf 	char KS[][48], char S[][64], char *f, char *tempL, char *P, char *FP)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate /* EXPORT DELETE START */
440Sstevel@tonic-gate 	int	i;
450Sstevel@tonic-gate 	int	t, j, k;
460Sstevel@tonic-gate 	char	t2;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	/*
490Sstevel@tonic-gate 	 * First, permute the bits in the input
500Sstevel@tonic-gate 	 */
510Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
520Sstevel@tonic-gate 		L[j] = block[IP[j]-1];
530Sstevel@tonic-gate 	/*
540Sstevel@tonic-gate 	 * Perform an encryption operation 16 times.
550Sstevel@tonic-gate 	 */
560Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
570Sstevel@tonic-gate 		/*
580Sstevel@tonic-gate 		 * Save the R array,
590Sstevel@tonic-gate 		 * which will be the new L.
600Sstevel@tonic-gate 		 */
610Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
620Sstevel@tonic-gate 			tempL[j] = R[j];
630Sstevel@tonic-gate 		/*
640Sstevel@tonic-gate 		 * Expand R to 48 bits using the E selector;
650Sstevel@tonic-gate 		 * exclusive-or with the current key bits.
660Sstevel@tonic-gate 		 */
670Sstevel@tonic-gate 		for (j = 0; j < 48; j++)
680Sstevel@tonic-gate 			preS[j] = R[E[j]-1] ^ KS[i][j];
690Sstevel@tonic-gate 		/*
700Sstevel@tonic-gate 		 * The pre-select bits are now considered
710Sstevel@tonic-gate 		 * in 8 groups of 6 bits each.
720Sstevel@tonic-gate 		 * The 8 selection functions map these
730Sstevel@tonic-gate 		 * 6-bit quantities into 4-bit quantities
740Sstevel@tonic-gate 		 * and the results permuted
750Sstevel@tonic-gate 		 * to make an f(R, K).
760Sstevel@tonic-gate 		 * The indexing into the selection functions
770Sstevel@tonic-gate 		 * is peculiar; it could be simplified by
780Sstevel@tonic-gate 		 * rewriting the tables.
790Sstevel@tonic-gate 		 */
800Sstevel@tonic-gate 		for (j = 0; j < 8; j++) {
810Sstevel@tonic-gate 			t = 6*j;
820Sstevel@tonic-gate 			k = S[j][(preS[t+0]<<5)+
830Sstevel@tonic-gate 				(preS[t+1]<<3)+
840Sstevel@tonic-gate 				(preS[t+2]<<2)+
850Sstevel@tonic-gate 				(preS[t+3]<<1)+
860Sstevel@tonic-gate 				(preS[t+4]<<0)+
870Sstevel@tonic-gate 				(preS[t+5]<<4)];
880Sstevel@tonic-gate 			t = 4*j;
890Sstevel@tonic-gate 			f[t+0] = (k>>3)&01;
900Sstevel@tonic-gate 			f[t+1] = (k>>2)&01;
910Sstevel@tonic-gate 			f[t+2] = (k>>1)&01;
920Sstevel@tonic-gate 			f[t+3] = (k>>0)&01;
930Sstevel@tonic-gate 		}
940Sstevel@tonic-gate 		/*
950Sstevel@tonic-gate 		 * The new R is L ^ f(R, K).
960Sstevel@tonic-gate 		 * The f here has to be permuted first, though.
970Sstevel@tonic-gate 		 */
980Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
990Sstevel@tonic-gate 			R[j] = L[j] ^ f[P[j]-1];
1000Sstevel@tonic-gate 		/*
1010Sstevel@tonic-gate 		 * Finally, the new L (the original R)
1020Sstevel@tonic-gate 		 * is copied back.
1030Sstevel@tonic-gate 		 */
1040Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
1050Sstevel@tonic-gate 			L[j] = tempL[j];
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * The output L and R are reversed.
1090Sstevel@tonic-gate 	 */
1100Sstevel@tonic-gate 	for (j = 0; j < 32; j++) {
1110Sstevel@tonic-gate 		t2 = L[j];
1120Sstevel@tonic-gate 		L[j] = R[j];
1130Sstevel@tonic-gate 		R[j] = t2;
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 	/*
1160Sstevel@tonic-gate 	 * The final output
1170Sstevel@tonic-gate 	 * gets the inverse permutation of the very original.
1180Sstevel@tonic-gate 	 */
1190Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
1200Sstevel@tonic-gate 		block[j] = L[FP[j]-1];
1210Sstevel@tonic-gate /* EXPORT DELETE END */
1220Sstevel@tonic-gate }
123