1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate /*LINTLIBRARY*/
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * DES encryption library routines
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include <rpc/des_crypt.h>
45*0Sstevel@tonic-gate #include <sys/stat.h>
46*0Sstevel@tonic-gate #include <fcntl.h>
47*0Sstevel@tonic-gate #include <unistd.h>
48*0Sstevel@tonic-gate #include <stropts.h>
49*0Sstevel@tonic-gate #ifdef sun
50*0Sstevel@tonic-gate #include <sys/ioctl.h>
51*0Sstevel@tonic-gate #include <sys/des.h>
52*0Sstevel@tonic-gate #ifdef _KERNEL
53*0Sstevel@tonic-gate #include <sys/conf.h>
54*0Sstevel@tonic-gate #define	getdesfd() (cdevsw[11].d_open(0, 0) ? -1 : 0)
55*0Sstevel@tonic-gate #define	ioctl(a, b, c) (cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0)
56*0Sstevel@tonic-gate #ifndef CRYPT
57*0Sstevel@tonic-gate #define	__des_crypt(a, b, c) 0
58*0Sstevel@tonic-gate #endif
59*0Sstevel@tonic-gate #else
60*0Sstevel@tonic-gate #define	getdesfd()	(open("/dev/des", 0, 0))
61*0Sstevel@tonic-gate #endif
62*0Sstevel@tonic-gate #else
63*0Sstevel@tonic-gate #include <des/des.h>
64*0Sstevel@tonic-gate #endif
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #include "des_soft.h"
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * To see if chip is installed
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate #define	UNOPENED (-2)
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Copy 8 bytes
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate #define	COPY8(src, dst) { \
77*0Sstevel@tonic-gate 	char *a = (char *) dst; \
78*0Sstevel@tonic-gate 	char *b = (char *) src; \
79*0Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
80*0Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate /*
84*0Sstevel@tonic-gate  * Copy multiple of 8 bytes
85*0Sstevel@tonic-gate  */
86*0Sstevel@tonic-gate #define	DESCOPY(src, dst, len) { \
87*0Sstevel@tonic-gate 	char *a = (char *) dst; \
88*0Sstevel@tonic-gate 	char *b = (char *) src; \
89*0Sstevel@tonic-gate 	int i; \
90*0Sstevel@tonic-gate 	for (i = (int) len; i > 0; i -= 8) { \
91*0Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
92*0Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
93*0Sstevel@tonic-gate 	} \
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate  * CBC mode encryption
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate int
101*0Sstevel@tonic-gate cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	int err = 0;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /* EXPORT DELETE START */
106*0Sstevel@tonic-gate 	struct desparams dp;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	dp.des_mode = CBC;
109*0Sstevel@tonic-gate 	COPY8(ivec, dp.des_ivec);
110*0Sstevel@tonic-gate 	err = common_crypt(key, buf, len, mode, &dp);
111*0Sstevel@tonic-gate 	COPY8(dp.des_ivec, ivec);
112*0Sstevel@tonic-gate /* EXPORT DELETE END */
113*0Sstevel@tonic-gate 	return (err);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate /*
118*0Sstevel@tonic-gate  * ECB mode encryption
119*0Sstevel@tonic-gate  */
120*0Sstevel@tonic-gate int
121*0Sstevel@tonic-gate ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	int ret = 0;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /* EXPORT DELETE START */
126*0Sstevel@tonic-gate 	struct desparams dp;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	dp.des_mode = ECB;
129*0Sstevel@tonic-gate 	ret = common_crypt(key, buf, len, mode, &dp);
130*0Sstevel@tonic-gate /* EXPORT DELETE END */
131*0Sstevel@tonic-gate 	return (ret);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate /* EXPORT DELETE START */
136*0Sstevel@tonic-gate /*
137*0Sstevel@tonic-gate  * Common code to cbc_crypt() & ecb_crypt()
138*0Sstevel@tonic-gate  */
139*0Sstevel@tonic-gate static int
140*0Sstevel@tonic-gate common_crypt(char *key, char *buf, unsigned len, unsigned mode, struct desparams *desp)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	int desdev;
143*0Sstevel@tonic-gate 	int res;
144*0Sstevel@tonic-gate 	int g_desfd = UNOPENED;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if ((len % 8) != 0 || len > DES_MAXDATA) {
147*0Sstevel@tonic-gate 		return (DESERR_BADPARAM);
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 	desp->des_dir =
150*0Sstevel@tonic-gate 		((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	desdev = mode & DES_DEVMASK;
153*0Sstevel@tonic-gate 	COPY8(key, desp->des_key);
154*0Sstevel@tonic-gate #ifdef sun
155*0Sstevel@tonic-gate 	if (desdev == DES_HW) {
156*0Sstevel@tonic-gate 		if (g_desfd < 0) {
157*0Sstevel@tonic-gate 			if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) {
158*0Sstevel@tonic-gate 				goto software;	/* no hardware device */
159*0Sstevel@tonic-gate 			}
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		/*
163*0Sstevel@tonic-gate 		 * hardware
164*0Sstevel@tonic-gate 		 */
165*0Sstevel@tonic-gate 		desp->des_len = len;
166*0Sstevel@tonic-gate 		if (len <= DES_QUICKLEN) {
167*0Sstevel@tonic-gate 			DESCOPY(buf, desp->des_data, len);
168*0Sstevel@tonic-gate 			res = ioctl(g_desfd, (int)DESIOCQUICK, (char *) desp);
169*0Sstevel@tonic-gate 			DESCOPY(desp->des_data, buf, len);
170*0Sstevel@tonic-gate 		} else {
171*0Sstevel@tonic-gate 			desp->des_buf = (u_char *) buf;
172*0Sstevel@tonic-gate 			res = ioctl(g_desfd, (int)DESIOCBLOCK, (char *) desp);
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate 		return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate software:
177*0Sstevel@tonic-gate #endif
178*0Sstevel@tonic-gate 	/*
179*0Sstevel@tonic-gate 	 * software
180*0Sstevel@tonic-gate 	 */
181*0Sstevel@tonic-gate 	if (!__des_crypt(buf, len, desp)) {
182*0Sstevel@tonic-gate 		return (DESERR_HWERROR);
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 	return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate /* EXPORT DELETE END */
187