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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
36 * DES encryption library routines
39 #include <sys/types.h>
40 #include <rpc/des_crypt.h>
46 #include <sys/ioctl.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)
53 #define __des_crypt(a, b, c) 0
56 #define getdesfd() (open("/dev/des", 0, 0))
65 * To see if chip is installed
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++; \
80 * Copy multiple of 8 bytes
82 #define DESCOPY(src, dst, len) { \
83 char *a = (char *)dst; \
84 char *b = (char *)src; \
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++; \
91 static int common_crypt(char *, char *, unsigned, unsigned, struct desparams
*);
97 cbc_crypt(char *key
, char *buf
, size_t len
, unsigned int mode
, char *ivec
)
103 COPY8(ivec
, dp
.des_ivec
);
104 err
= common_crypt(key
, buf
, len
, mode
, &dp
);
105 COPY8(dp
.des_ivec
, ivec
);
111 * ECB mode encryption
114 ecb_crypt(char *key
, char *buf
, size_t len
, unsigned int mode
)
120 ret
= common_crypt(key
, buf
, len
, mode
, &dp
);
126 * Common code to cbc_crypt() & ecb_crypt()
129 common_crypt(char *key
, char *buf
, unsigned len
,
130 unsigned mode
, struct desparams
*desp
)
134 int g_desfd
= UNOPENED
;
136 if ((len
% 8) != 0 || len
> DES_MAXDATA
) {
137 return (DESERR_BADPARAM
);
140 ((mode
& DES_DIRMASK
) == DES_ENCRYPT
) ? ENCRYPT
: DECRYPT
;
142 desdev
= mode
& DES_DEVMASK
;
143 COPY8(key
, desp
->des_key
);
145 if (desdev
== DES_HW
) {
147 if (g_desfd
== -1 || (g_desfd
= getdesfd()) < 0) {
148 goto software
; /* no hardware device */
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
);
161 desp
->des_buf
= (uchar_t
*)buf
;
162 res
= ioctl(g_desfd
, (int)DESIOCBLOCK
, (char *)desp
);
164 return (res
== 0 ? DESERR_NONE
: DESERR_HWERROR
);
171 if (!__des_crypt(buf
, len
, desp
)) {
172 return (DESERR_HWERROR
);
174 return (desdev
== DES_SW
? DESERR_NONE
: DESERR_NOHWDEVICE
);