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