xref: /titanic_52/usr/src/lib/libcrypt/common/des_decrypt.c (revision 694c35faa87b858ecdadfe4fc592615f4eefbb07)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate void
36*7c478bd9Sstevel@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)
37*7c478bd9Sstevel@tonic-gate {
38*7c478bd9Sstevel@tonic-gate 	int	i, ii;
39*7c478bd9Sstevel@tonic-gate 	int	t, j, k;
40*7c478bd9Sstevel@tonic-gate 	char	t2;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 	/*
43*7c478bd9Sstevel@tonic-gate 	 * First, permute the bits in the input
44*7c478bd9Sstevel@tonic-gate 	 */
45*7c478bd9Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
46*7c478bd9Sstevel@tonic-gate 		L[j] = block[IP[j]-1];
47*7c478bd9Sstevel@tonic-gate 	/*
48*7c478bd9Sstevel@tonic-gate 	 * Perform a decryption operation 16 times.
49*7c478bd9Sstevel@tonic-gate 	 */
50*7c478bd9Sstevel@tonic-gate 	for (ii = 0; ii < 16; ii++) {
51*7c478bd9Sstevel@tonic-gate 		i = 15-ii;
52*7c478bd9Sstevel@tonic-gate 		/*
53*7c478bd9Sstevel@tonic-gate 		 * Save the R array,
54*7c478bd9Sstevel@tonic-gate 		 * which will be the new L.
55*7c478bd9Sstevel@tonic-gate 		 */
56*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
57*7c478bd9Sstevel@tonic-gate 			tempL[j] = R[j];
58*7c478bd9Sstevel@tonic-gate 		/*
59*7c478bd9Sstevel@tonic-gate 		 * Expand R to 48 bits using the E selector;
60*7c478bd9Sstevel@tonic-gate 		 * exclusive-or with the current key bits.
61*7c478bd9Sstevel@tonic-gate 		 */
62*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < 48; j++)
63*7c478bd9Sstevel@tonic-gate 			preS[j] = R[E[j]-1] ^ KS[i][j];
64*7c478bd9Sstevel@tonic-gate 		/*
65*7c478bd9Sstevel@tonic-gate 		 * The pre-select bits are now considered
66*7c478bd9Sstevel@tonic-gate 		 * in 8 groups of 6 bits each.
67*7c478bd9Sstevel@tonic-gate 		 * The 8 selection functions map these
68*7c478bd9Sstevel@tonic-gate 		 * 6-bit quantities into 4-bit quantities
69*7c478bd9Sstevel@tonic-gate 		 * and the results permuted
70*7c478bd9Sstevel@tonic-gate 		 * to make an f(R, K).
71*7c478bd9Sstevel@tonic-gate 		 * The indexing into the selection functions
72*7c478bd9Sstevel@tonic-gate 		 * is peculiar; it could be simplified by
73*7c478bd9Sstevel@tonic-gate 		 * rewriting the tables.
74*7c478bd9Sstevel@tonic-gate 		 */
75*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < 8; j++) {
76*7c478bd9Sstevel@tonic-gate 			t = 6*j;
77*7c478bd9Sstevel@tonic-gate 			k = S[j][(preS[t+0]<<5)+
78*7c478bd9Sstevel@tonic-gate 				(preS[t+1]<<3)+
79*7c478bd9Sstevel@tonic-gate 				(preS[t+2]<<2)+
80*7c478bd9Sstevel@tonic-gate 				(preS[t+3]<<1)+
81*7c478bd9Sstevel@tonic-gate 				(preS[t+4]<<0)+
82*7c478bd9Sstevel@tonic-gate 				(preS[t+5]<<4)];
83*7c478bd9Sstevel@tonic-gate 			t = 4*j;
84*7c478bd9Sstevel@tonic-gate 			f[t+0] = (k>>3)&01;
85*7c478bd9Sstevel@tonic-gate 			f[t+1] = (k>>2)&01;
86*7c478bd9Sstevel@tonic-gate 			f[t+2] = (k>>1)&01;
87*7c478bd9Sstevel@tonic-gate 			f[t+3] = (k>>0)&01;
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 		/*
90*7c478bd9Sstevel@tonic-gate 		 * The new R is L ^ f(R, K).
91*7c478bd9Sstevel@tonic-gate 		 * The f here has to be permuted first, though.
92*7c478bd9Sstevel@tonic-gate 		 */
93*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
94*7c478bd9Sstevel@tonic-gate 			R[j] = L[j] ^ f[P[j]-1];
95*7c478bd9Sstevel@tonic-gate 		/*
96*7c478bd9Sstevel@tonic-gate 		 * Finally, the new L (the original R)
97*7c478bd9Sstevel@tonic-gate 		 * is copied back.
98*7c478bd9Sstevel@tonic-gate 		 */
99*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < 32; j++)
100*7c478bd9Sstevel@tonic-gate 			L[j] = tempL[j];
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 	/*
103*7c478bd9Sstevel@tonic-gate 	 * The output L and R are reversed.
104*7c478bd9Sstevel@tonic-gate 	 */
105*7c478bd9Sstevel@tonic-gate 	for (j = 0; j < 32; j++) {
106*7c478bd9Sstevel@tonic-gate 		t2 = L[j];
107*7c478bd9Sstevel@tonic-gate 		L[j] = R[j];
108*7c478bd9Sstevel@tonic-gate 		R[j] = t2;
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	/*
111*7c478bd9Sstevel@tonic-gate 	 * The final output
112*7c478bd9Sstevel@tonic-gate 	 * gets the inverse permutation of the very original.
113*7c478bd9Sstevel@tonic-gate 	 */
114*7c478bd9Sstevel@tonic-gate 	for (j = 0; j < 64; j++)
115*7c478bd9Sstevel@tonic-gate 		block[j] = L[FP[j]-1];
116*7c478bd9Sstevel@tonic-gate }
117