17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e8031f0aSraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*7257d1b4Sraf /*	Copyright (c) 1988 AT&T	*/
28*7257d1b4Sraf /*	  All Rights Reserved  	*/
29*7257d1b4Sraf 
30*7257d1b4Sraf #pragma weak _des_encrypt1 = des_encrypt1
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate void
des_encrypt1(char * block,char * L,char * IP,char * R,char * preS,char * E,char KS[][48],char S[][64],char * f,char * tempL,char * P,char * FP)35e8031f0aSraf des_encrypt1(char *block, char *L, char *IP, char *R, char *preS, char *E,
36e8031f0aSraf 	char KS[][48], char S[][64], char *f, char *tempL, char *P, char *FP)
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 	int	i;
397c478bd9Sstevel@tonic-gate 	int	t, j, k;
407c478bd9Sstevel@tonic-gate 	char	t2;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	/*
437c478bd9Sstevel@tonic-gate 	 * First, permute the bits in the input
447c478bd9Sstevel@tonic-gate 	 */
457c478bd9Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
467c478bd9Sstevel@tonic-gate 		L[j] = block[IP[j]-1];
477c478bd9Sstevel@tonic-gate 	/*
487c478bd9Sstevel@tonic-gate 	 * Perform an encryption operation 16 times.
497c478bd9Sstevel@tonic-gate 	 */
507c478bd9Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
517c478bd9Sstevel@tonic-gate 		/*
527c478bd9Sstevel@tonic-gate 		 * Save the R array,
537c478bd9Sstevel@tonic-gate 		 * which will be the new L.
547c478bd9Sstevel@tonic-gate 		 */
557c478bd9Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
567c478bd9Sstevel@tonic-gate 			tempL[j] = R[j];
577c478bd9Sstevel@tonic-gate 		/*
587c478bd9Sstevel@tonic-gate 		 * Expand R to 48 bits using the E selector;
597c478bd9Sstevel@tonic-gate 		 * exclusive-or with the current key bits.
607c478bd9Sstevel@tonic-gate 		 */
617c478bd9Sstevel@tonic-gate 		for (j = 0; j < 48; j++)
627c478bd9Sstevel@tonic-gate 			preS[j] = R[E[j]-1] ^ KS[i][j];
637c478bd9Sstevel@tonic-gate 		/*
647c478bd9Sstevel@tonic-gate 		 * The pre-select bits are now considered
657c478bd9Sstevel@tonic-gate 		 * in 8 groups of 6 bits each.
667c478bd9Sstevel@tonic-gate 		 * The 8 selection functions map these
677c478bd9Sstevel@tonic-gate 		 * 6-bit quantities into 4-bit quantities
687c478bd9Sstevel@tonic-gate 		 * and the results permuted
697c478bd9Sstevel@tonic-gate 		 * to make an f(R, K).
707c478bd9Sstevel@tonic-gate 		 * The indexing into the selection functions
717c478bd9Sstevel@tonic-gate 		 * is peculiar; it could be simplified by
727c478bd9Sstevel@tonic-gate 		 * rewriting the tables.
737c478bd9Sstevel@tonic-gate 		 */
747c478bd9Sstevel@tonic-gate 		for (j = 0; j < 8; j++) {
757c478bd9Sstevel@tonic-gate 			t = 6*j;
767c478bd9Sstevel@tonic-gate 			k = S[j][(preS[t+0]<<5)+
77*7257d1b4Sraf 			    (preS[t+1]<<3)+
78*7257d1b4Sraf 			    (preS[t+2]<<2)+
79*7257d1b4Sraf 			    (preS[t+3]<<1)+
80*7257d1b4Sraf 			    (preS[t+4]<<0)+
81*7257d1b4Sraf 			    (preS[t+5]<<4)];
827c478bd9Sstevel@tonic-gate 			t = 4*j;
837c478bd9Sstevel@tonic-gate 			f[t+0] = (k>>3)&01;
847c478bd9Sstevel@tonic-gate 			f[t+1] = (k>>2)&01;
857c478bd9Sstevel@tonic-gate 			f[t+2] = (k>>1)&01;
867c478bd9Sstevel@tonic-gate 			f[t+3] = (k>>0)&01;
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 		/*
897c478bd9Sstevel@tonic-gate 		 * The new R is L ^ f(R, K).
907c478bd9Sstevel@tonic-gate 		 * The f here has to be permuted first, though.
917c478bd9Sstevel@tonic-gate 		 */
927c478bd9Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
937c478bd9Sstevel@tonic-gate 			R[j] = L[j] ^ f[P[j]-1];
947c478bd9Sstevel@tonic-gate 		/*
957c478bd9Sstevel@tonic-gate 		 * Finally, the new L (the original R)
967c478bd9Sstevel@tonic-gate 		 * is copied back.
977c478bd9Sstevel@tonic-gate 		 */
987c478bd9Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
997c478bd9Sstevel@tonic-gate 			L[j] = tempL[j];
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * The output L and R are reversed.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	for (j = 0; j < 32; j++) {
1057c478bd9Sstevel@tonic-gate 		t2 = L[j];
1067c478bd9Sstevel@tonic-gate 		L[j] = R[j];
1077c478bd9Sstevel@tonic-gate 		R[j] = t2;
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * The final output
1117c478bd9Sstevel@tonic-gate 	 * gets the inverse permutation of the very original.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
1147c478bd9Sstevel@tonic-gate 		block[j] = L[FP[j]-1];
1157c478bd9Sstevel@tonic-gate }
116