xref: /titanic_41/usr/src/lib/libnsl/des/des_crypt.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
30 
31 /*
32  * Portions of this source code were derived from Berkeley 4.3 BSD
33  * under license from the Regents of the University of California.
34  */
35 
36 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 /*
39  * des_crypt.c, DES encryption library routines
40  */
41 
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <rpc/des_crypt.h>
46 /* EXPORT DELETE START */
47 #ifdef sun
48 #include <sys/ioctl.h>
49 #include <sys/des.h>
50 #define	getdesfd()	(open("/dev/des", 0, 0))
51 #else
52 #include <des/des.h>
53 #endif
54 /* EXPORT DELETE END */
55 #include <rpc/rpc.h>
56 /* EXPORT DELETE START */
57 
58 extern int __des_crypt(char *, unsigned, struct desparams *);
59 
60 static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
61 
62 /*
63  * To see if chip is installed
64  */
65 #define	UNOPENED (-2)
66 static int g_desfd = UNOPENED;
67 
68 
69 /*
70  * Copy 8 bytes
71  */
72 #define	COPY8(src, dst) { \
73 	char *a = (char *)dst; \
74 	char *b = (char *)src; \
75 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
76 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
77 }
78 
79 /*
80  * Copy multiple of 8 bytes
81  */
82 #define	DESCOPY(src, dst, len) { \
83 	char *a = (char *)dst; \
84 	char *b = (char *)src; \
85 	int i; \
86 	for (i = (int)len; i > 0; i -= 8) { \
87 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
88 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
89 	} \
90 }
91 /* EXPORT DELETE END */
92 
93 /*
94  * CBC mode encryption
95  */
96 int
97 cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
98 {
99 /* EXPORT DELETE START */
100 	int err;
101 	struct desparams dp;
102 
103 	dp.des_mode = CBC;
104 	COPY8(ivec, dp.des_ivec);
105 	err = common_crypt(key, buf, len, mode, &dp);
106 	COPY8(dp.des_ivec, ivec);
107 	return (err);
108 #if 0
109 /* EXPORT DELETE END */
110 	return (DESERR_HWERROR);
111 /* EXPORT DELETE START */
112 #endif
113 /* EXPORT DELETE END */
114 }
115 
116 
117 /*
118  * ECB mode encryption
119  */
120 int
121 ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
122 {
123 /* EXPORT DELETE START */
124 	struct desparams dp;
125 
126 	dp.des_mode = ECB;
127 	return (common_crypt(key, buf, len, mode, &dp));
128 #if 0
129 /* EXPORT DELETE END */
130 	return (DESERR_HWERROR);
131 /* EXPORT DELETE START */
132 #endif
133 /* EXPORT DELETE END */
134 }
135 
136 
137 /* EXPORT DELETE START */
138 
139 /*
140  * Common code to cbc_crypt() & ecb_crypt()
141  */
142 static int
143 common_crypt(char *key, char *buf, unsigned len, unsigned mode,
144 							struct desparams *desp)
145 {
146 	int desdev;
147 	int res;
148 
149 	if ((len % 8) != 0 || len > DES_MAXDATA)
150 		return (DESERR_BADPARAM);
151 	desp->des_dir =
152 		((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
153 
154 	desdev = mode & DES_DEVMASK;
155 	COPY8(key, desp->des_key);
156 #ifdef sun
157 	if (desdev == DES_HW) {
158 		if (g_desfd < 0) {
159 			if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) {
160 				goto software;	/* no hardware device */
161 			}
162 		}
163 
164 		/*
165 		 * hardware
166 		 */
167 		desp->des_len = len;
168 		if (len <= DES_QUICKLEN) {
169 			DESCOPY(buf, desp->des_data, len);
170 			res = ioctl(g_desfd, DESIOCQUICK, (char *)desp);
171 			DESCOPY(desp->des_data, buf, len);
172 		} else {
173 			desp->des_buf = (uchar_t *)buf;
174 			res = ioctl(g_desfd, DESIOCBLOCK, (char *)desp);
175 		}
176 		return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
177 	}
178 software:
179 #endif
180 	/*
181 	 * software
182 	 */
183 	if (!__des_crypt(buf, len, desp))
184 		return (DESERR_HWERROR);
185 	return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
186 }
187 /* EXPORT DELETE END */
188 
189 /* EXPORT DELETE START */
190 static int
191 desN_crypt(des_block keys[], int keynum, char *buf, unsigned int len,
192 		unsigned int mode, char *ivec)
193 {
194 	unsigned int m = mode & (DES_ENCRYPT | DES_DECRYPT);
195 	unsigned int flags = mode & ~(DES_ENCRYPT | DES_DECRYPT);
196 	des_block svec, dvec;
197 	int i, j, stat;
198 
199 	if (keynum < 1)
200 		return (DESERR_BADPARAM);
201 
202 	(void) memcpy(svec.c, ivec, sizeof (des_block));
203 	for (i = 0; i < keynum; i++) {
204 		j = (mode & DES_DECRYPT) ? keynum - 1 - i : i;
205 		stat = cbc_crypt(keys[j].c, buf, len, m | flags, ivec);
206 		if (mode & DES_DECRYPT && i == 0)
207 			(void) memcpy(dvec.c, ivec, sizeof (des_block));
208 
209 		if (DES_FAILED(stat))
210 			return (stat);
211 
212 		m = (m == DES_ENCRYPT ? DES_DECRYPT : DES_ENCRYPT);
213 
214 		if ((mode & DES_DECRYPT) || i != keynum - 1 || i%2)
215 			(void) memcpy(ivec, svec.c, sizeof (des_block));
216 	}
217 	if (keynum % 2 == 0)
218 		stat = cbc_crypt(keys[0].c, buf, len, mode, ivec);
219 
220 	if (mode & DES_DECRYPT)
221 		(void) memcpy(ivec, dvec.c, sizeof (des_block));
222 
223 	return (stat);
224 }
225 /* EXPORT DELETE END */
226 
227 
228 
229 int
230 __cbc_triple_crypt(des_block keys[], char *buf,  uint_t len,
231 			uint_t mode, char *ivec)
232 {
233 /* EXPORT DELETE START */
234 	return (desN_crypt(keys, 3, buf, len, mode, ivec));
235 #if 0
236 /* EXPORT DELETE END */
237 	return (DESERR_HWERROR);
238 /* EXPORT DELETE START */
239 #endif
240 /* EXPORT DELETE END */
241 }
242