xref: /illumos-gate/usr/src/lib/libcrypt/common/des.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 /*LINTLIBRARY*/
37 
38 /*
39  * DES encryption library routines
40  */
41 
42 #include <sys/types.h>
43 
44 #include <rpc/des_crypt.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <unistd.h>
48 #include <stropts.h>
49 #ifdef sun
50 #include <sys/ioctl.h>
51 #include <sys/des.h>
52 #ifdef _KERNEL
53 #include <sys/conf.h>
54 #define	getdesfd() (cdevsw[11].d_open(0, 0) ? -1 : 0)
55 #define	ioctl(a, b, c) (cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0)
56 #ifndef CRYPT
57 #define	__des_crypt(a, b, c) 0
58 #endif
59 #else
60 #define	getdesfd()	(open("/dev/des", 0, 0))
61 #endif
62 #else
63 #include <des/des.h>
64 #endif
65 
66 #include "des_soft.h"
67 
68 /*
69  * To see if chip is installed
70  */
71 #define	UNOPENED (-2)
72 
73 /*
74  * Copy 8 bytes
75  */
76 #define	COPY8(src, dst) { \
77 	char *a = (char *) dst; \
78 	char *b = (char *) src; \
79 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
80 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
81 }
82 
83 /*
84  * Copy multiple of 8 bytes
85  */
86 #define	DESCOPY(src, dst, len) { \
87 	char *a = (char *) dst; \
88 	char *b = (char *) src; \
89 	int i; \
90 	for (i = (int) len; i > 0; i -= 8) { \
91 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
92 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
93 	} \
94 }
95 static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
96 
97 /*
98  * CBC mode encryption
99  */
100 int
101 cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
102 {
103 	int err = 0;
104 
105 /* EXPORT DELETE START */
106 	struct desparams dp;
107 
108 	dp.des_mode = CBC;
109 	COPY8(ivec, dp.des_ivec);
110 	err = common_crypt(key, buf, len, mode, &dp);
111 	COPY8(dp.des_ivec, ivec);
112 /* EXPORT DELETE END */
113 	return (err);
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 	int ret = 0;
124 
125 /* EXPORT DELETE START */
126 	struct desparams dp;
127 
128 	dp.des_mode = ECB;
129 	ret = common_crypt(key, buf, len, mode, &dp);
130 /* EXPORT DELETE END */
131 	return (ret);
132 }
133 
134 
135 /* EXPORT DELETE START */
136 /*
137  * Common code to cbc_crypt() & ecb_crypt()
138  */
139 static int
140 common_crypt(char *key, char *buf, unsigned len, unsigned mode, struct desparams *desp)
141 {
142 	int desdev;
143 	int res;
144 	int g_desfd = UNOPENED;
145 
146 	if ((len % 8) != 0 || len > DES_MAXDATA) {
147 		return (DESERR_BADPARAM);
148 	}
149 	desp->des_dir =
150 		((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
151 
152 	desdev = mode & DES_DEVMASK;
153 	COPY8(key, desp->des_key);
154 #ifdef sun
155 	if (desdev == DES_HW) {
156 		if (g_desfd < 0) {
157 			if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) {
158 				goto software;	/* no hardware device */
159 			}
160 		}
161 
162 		/*
163 		 * hardware
164 		 */
165 		desp->des_len = len;
166 		if (len <= DES_QUICKLEN) {
167 			DESCOPY(buf, desp->des_data, len);
168 			res = ioctl(g_desfd, (int)DESIOCQUICK, (char *) desp);
169 			DESCOPY(desp->des_data, buf, len);
170 		} else {
171 			desp->des_buf = (u_char *) buf;
172 			res = ioctl(g_desfd, (int)DESIOCBLOCK, (char *) desp);
173 		}
174 		return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
175 	}
176 software:
177 #endif
178 	/*
179 	 * software
180 	 */
181 	if (!__des_crypt(buf, len, desp)) {
182 		return (DESERR_HWERROR);
183 	}
184 	return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
185 }
186 /* EXPORT DELETE END */
187