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