xref: /onnv-gate/usr/src/lib/libcrypt/common/des_decrypt.c (revision 0:68f95e015346)
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 #include <sys/types.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate void
_des_decrypt1(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)37*0Sstevel@tonic-gate _des_decrypt1(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)
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate /* EXPORT DELETE START */
40*0Sstevel@tonic-gate 	int	i, ii;
41*0Sstevel@tonic-gate 	int	t, j, k;
42*0Sstevel@tonic-gate 	char	t2;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	/*
45*0Sstevel@tonic-gate 	 * First, permute the bits in the input
46*0Sstevel@tonic-gate 	 */
47*0Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
48*0Sstevel@tonic-gate 		L[j] = block[IP[j]-1];
49*0Sstevel@tonic-gate 	/*
50*0Sstevel@tonic-gate 	 * Perform a decryption operation 16 times.
51*0Sstevel@tonic-gate 	 */
52*0Sstevel@tonic-gate 	for (ii = 0; ii < 16; ii++) {
53*0Sstevel@tonic-gate 		i = 15-ii;
54*0Sstevel@tonic-gate 		/*
55*0Sstevel@tonic-gate 		 * Save the R array,
56*0Sstevel@tonic-gate 		 * which will be the new L.
57*0Sstevel@tonic-gate 		 */
58*0Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
59*0Sstevel@tonic-gate 			tempL[j] = R[j];
60*0Sstevel@tonic-gate 		/*
61*0Sstevel@tonic-gate 		 * Expand R to 48 bits using the E selector;
62*0Sstevel@tonic-gate 		 * exclusive-or with the current key bits.
63*0Sstevel@tonic-gate 		 */
64*0Sstevel@tonic-gate 		for (j = 0; j < 48; j++)
65*0Sstevel@tonic-gate 			preS[j] = R[E[j]-1] ^ KS[i][j];
66*0Sstevel@tonic-gate 		/*
67*0Sstevel@tonic-gate 		 * The pre-select bits are now considered
68*0Sstevel@tonic-gate 		 * in 8 groups of 6 bits each.
69*0Sstevel@tonic-gate 		 * The 8 selection functions map these
70*0Sstevel@tonic-gate 		 * 6-bit quantities into 4-bit quantities
71*0Sstevel@tonic-gate 		 * and the results permuted
72*0Sstevel@tonic-gate 		 * to make an f(R, K).
73*0Sstevel@tonic-gate 		 * The indexing into the selection functions
74*0Sstevel@tonic-gate 		 * is peculiar; it could be simplified by
75*0Sstevel@tonic-gate 		 * rewriting the tables.
76*0Sstevel@tonic-gate 		 */
77*0Sstevel@tonic-gate 		for (j = 0; j < 8; j++) {
78*0Sstevel@tonic-gate 			t = 6*j;
79*0Sstevel@tonic-gate 			k = S[j][(preS[t+0]<<5)+
80*0Sstevel@tonic-gate 				(preS[t+1]<<3)+
81*0Sstevel@tonic-gate 				(preS[t+2]<<2)+
82*0Sstevel@tonic-gate 				(preS[t+3]<<1)+
83*0Sstevel@tonic-gate 				(preS[t+4]<<0)+
84*0Sstevel@tonic-gate 				(preS[t+5]<<4)];
85*0Sstevel@tonic-gate 			t = 4*j;
86*0Sstevel@tonic-gate 			f[t+0] = (k>>3)&01;
87*0Sstevel@tonic-gate 			f[t+1] = (k>>2)&01;
88*0Sstevel@tonic-gate 			f[t+2] = (k>>1)&01;
89*0Sstevel@tonic-gate 			f[t+3] = (k>>0)&01;
90*0Sstevel@tonic-gate 		}
91*0Sstevel@tonic-gate 		/*
92*0Sstevel@tonic-gate 		 * The new R is L ^ f(R, K).
93*0Sstevel@tonic-gate 		 * The f here has to be permuted first, though.
94*0Sstevel@tonic-gate 		 */
95*0Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
96*0Sstevel@tonic-gate 			R[j] = L[j] ^ f[P[j]-1];
97*0Sstevel@tonic-gate 		/*
98*0Sstevel@tonic-gate 		 * Finally, the new L (the original R)
99*0Sstevel@tonic-gate 		 * is copied back.
100*0Sstevel@tonic-gate 		 */
101*0Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
102*0Sstevel@tonic-gate 			L[j] = tempL[j];
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 	/*
105*0Sstevel@tonic-gate 	 * The output L and R are reversed.
106*0Sstevel@tonic-gate 	 */
107*0Sstevel@tonic-gate 	for (j = 0; j < 32; j++) {
108*0Sstevel@tonic-gate 		t2 = L[j];
109*0Sstevel@tonic-gate 		L[j] = R[j];
110*0Sstevel@tonic-gate 		R[j] = t2;
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 	/*
113*0Sstevel@tonic-gate 	 * The final output
114*0Sstevel@tonic-gate 	 * gets the inverse permutation of the very original.
115*0Sstevel@tonic-gate 	 */
116*0Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
117*0Sstevel@tonic-gate 		block[j] = L[FP[j]-1];
118*0Sstevel@tonic-gate /* EXPORT DELETE END */
119*0Sstevel@tonic-gate }
120