xref: /onnv-gate/usr/src/lib/libcrypt/common/des_encrypt.c (revision 6812:febeba71273d)
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
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
22*6812Sraf /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #pragma weak _des_encrypt1 = des_encrypt1
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate 
360Sstevel@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)371219Sraf des_encrypt1(char *block, char *L, char *IP, char *R, char *preS, char *E,
381219Sraf 	char KS[][48], char S[][64], char *f, char *tempL, char *P, char *FP)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate /* EXPORT DELETE START */
410Sstevel@tonic-gate 	int	i;
420Sstevel@tonic-gate 	int	t, j, k;
430Sstevel@tonic-gate 	char	t2;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	/*
460Sstevel@tonic-gate 	 * First, permute the bits in the input
470Sstevel@tonic-gate 	 */
480Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
490Sstevel@tonic-gate 		L[j] = block[IP[j]-1];
500Sstevel@tonic-gate 	/*
510Sstevel@tonic-gate 	 * Perform an encryption operation 16 times.
520Sstevel@tonic-gate 	 */
530Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
540Sstevel@tonic-gate 		/*
550Sstevel@tonic-gate 		 * Save the R array,
560Sstevel@tonic-gate 		 * which will be the new L.
570Sstevel@tonic-gate 		 */
580Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
590Sstevel@tonic-gate 			tempL[j] = R[j];
600Sstevel@tonic-gate 		/*
610Sstevel@tonic-gate 		 * Expand R to 48 bits using the E selector;
620Sstevel@tonic-gate 		 * exclusive-or with the current key bits.
630Sstevel@tonic-gate 		 */
640Sstevel@tonic-gate 		for (j = 0; j < 48; j++)
650Sstevel@tonic-gate 			preS[j] = R[E[j]-1] ^ KS[i][j];
660Sstevel@tonic-gate 		/*
670Sstevel@tonic-gate 		 * The pre-select bits are now considered
680Sstevel@tonic-gate 		 * in 8 groups of 6 bits each.
690Sstevel@tonic-gate 		 * The 8 selection functions map these
700Sstevel@tonic-gate 		 * 6-bit quantities into 4-bit quantities
710Sstevel@tonic-gate 		 * and the results permuted
720Sstevel@tonic-gate 		 * to make an f(R, K).
730Sstevel@tonic-gate 		 * The indexing into the selection functions
740Sstevel@tonic-gate 		 * is peculiar; it could be simplified by
750Sstevel@tonic-gate 		 * rewriting the tables.
760Sstevel@tonic-gate 		 */
770Sstevel@tonic-gate 		for (j = 0; j < 8; j++) {
780Sstevel@tonic-gate 			t = 6*j;
790Sstevel@tonic-gate 			k = S[j][(preS[t+0]<<5)+
80*6812Sraf 			    (preS[t+1]<<3)+
81*6812Sraf 			    (preS[t+2]<<2)+
82*6812Sraf 			    (preS[t+3]<<1)+
83*6812Sraf 			    (preS[t+4]<<0)+
84*6812Sraf 			    (preS[t+5]<<4)];
850Sstevel@tonic-gate 			t = 4*j;
860Sstevel@tonic-gate 			f[t+0] = (k>>3)&01;
870Sstevel@tonic-gate 			f[t+1] = (k>>2)&01;
880Sstevel@tonic-gate 			f[t+2] = (k>>1)&01;
890Sstevel@tonic-gate 			f[t+3] = (k>>0)&01;
900Sstevel@tonic-gate 		}
910Sstevel@tonic-gate 		/*
920Sstevel@tonic-gate 		 * The new R is L ^ f(R, K).
930Sstevel@tonic-gate 		 * The f here has to be permuted first, though.
940Sstevel@tonic-gate 		 */
950Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
960Sstevel@tonic-gate 			R[j] = L[j] ^ f[P[j]-1];
970Sstevel@tonic-gate 		/*
980Sstevel@tonic-gate 		 * Finally, the new L (the original R)
990Sstevel@tonic-gate 		 * is copied back.
1000Sstevel@tonic-gate 		 */
1010Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
1020Sstevel@tonic-gate 			L[j] = tempL[j];
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	/*
1050Sstevel@tonic-gate 	 * The output L and R are reversed.
1060Sstevel@tonic-gate 	 */
1070Sstevel@tonic-gate 	for (j = 0; j < 32; j++) {
1080Sstevel@tonic-gate 		t2 = L[j];
1090Sstevel@tonic-gate 		L[j] = R[j];
1100Sstevel@tonic-gate 		R[j] = t2;
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 	/*
1130Sstevel@tonic-gate 	 * The final output
1140Sstevel@tonic-gate 	 * gets the inverse permutation of the very original.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
1170Sstevel@tonic-gate 		block[j] = L[FP[j]-1];
1180Sstevel@tonic-gate /* EXPORT DELETE END */
1190Sstevel@tonic-gate }
120