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