xref: /illumos-gate/usr/src/lib/libcrypt/common/des.c (revision 2bbdd445a21f9d61f4a0ca0faf05d5ceb2bd91f3)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 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 /*
36  * DES encryption library routines
37  */
38 
39 #include <sys/types.h>
40 #include <rpc/des_crypt.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <stropts.h>
45 #ifdef sun
46 #include <sys/ioctl.h>
47 #include <sys/des.h>
48 #ifdef _KERNEL
49 #include <sys/conf.h>
50 #define	getdesfd() (cdevsw[11].d_open(0, 0) ? -1 : 0)
51 #define	ioctl(a, b, c) (cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0)
52 #ifndef CRYPT
53 #define	__des_crypt(a, b, c) 0
54 #endif
55 #else
56 #define	getdesfd()	(open("/dev/des", 0, 0))
57 #endif
58 #else
59 #include <des/des.h>
60 #endif
61 
62 #include "des_soft.h"
63 
64 /*
65  * To see if chip is installed
66  */
67 #define	UNOPENED (-2)
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 static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
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 	int err = 0;
100 	struct desparams dp;
101 
102 	dp.des_mode = CBC;
103 	COPY8(ivec, dp.des_ivec);
104 	err = common_crypt(key, buf, len, mode, &dp);
105 	COPY8(dp.des_ivec, ivec);
106 	return (err);
107 }
108 
109 
110 /*
111  * ECB mode encryption
112  */
113 int
114 ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
115 {
116 	int ret = 0;
117 	struct desparams dp;
118 
119 	dp.des_mode = ECB;
120 	ret = common_crypt(key, buf, len, mode, &dp);
121 	return (ret);
122 }
123 
124 
125 /*
126  * Common code to cbc_crypt() & ecb_crypt()
127  */
128 static int
129 common_crypt(char *key, char *buf, unsigned len,
130     unsigned mode, struct desparams *desp)
131 {
132 	int desdev;
133 	int res;
134 	int g_desfd = UNOPENED;
135 
136 	if ((len % 8) != 0 || len > DES_MAXDATA) {
137 		return (DESERR_BADPARAM);
138 	}
139 	desp->des_dir =
140 	    ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
141 
142 	desdev = mode & DES_DEVMASK;
143 	COPY8(key, desp->des_key);
144 #ifdef sun
145 	if (desdev == DES_HW) {
146 		if (g_desfd < 0) {
147 			if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) {
148 				goto software;	/* no hardware device */
149 			}
150 		}
151 
152 		/*
153 		 * hardware
154 		 */
155 		desp->des_len = len;
156 		if (len <= DES_QUICKLEN) {
157 			DESCOPY(buf, desp->des_data, len);
158 			res = ioctl(g_desfd, (int)DESIOCQUICK, (char *)desp);
159 			DESCOPY(desp->des_data, buf, len);
160 		} else {
161 			desp->des_buf = (uchar_t *)buf;
162 			res = ioctl(g_desfd, (int)DESIOCBLOCK, (char *)desp);
163 		}
164 		return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
165 	}
166 software:
167 #endif
168 	/*
169 	 * software
170 	 */
171 	if (!__des_crypt(buf, len, desp)) {
172 		return (DESERR_HWERROR);
173 	}
174 	return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
175 }
176