3882 Remove xmod & friends
[illumos-gate.git] / usr / src / lib / libcrypt / common / des.c
blob4ef8e5c332555579d6fd40d5177f04192f802a88
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
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
62 #include "des_soft.h"
65 * To see if chip is installed
67 #define UNOPENED (-2)
70 * Copy 8 bytes
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; \
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 } \
91 static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
94 * CBC mode encryption
96 int
97 cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
99 int err = 0;
100 struct desparams dp;
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);
111 * ECB mode encryption
114 ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
116 int ret = 0;
117 struct desparams dp;
119 dp.des_mode = ECB;
120 ret = common_crypt(key, buf, len, mode, &dp);
121 return (ret);
126 * Common code to cbc_crypt() & ecb_crypt()
128 static int
129 common_crypt(char *key, char *buf, unsigned len,
130 unsigned mode, struct desparams *desp)
132 int desdev;
133 int res;
134 int g_desfd = UNOPENED;
136 if ((len % 8) != 0 || len > DES_MAXDATA) {
137 return (DESERR_BADPARAM);
139 desp->des_dir =
140 ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
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 */
153 * hardware
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);
164 return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
166 software:
167 #endif
169 * software
171 if (!__des_crypt(buf, len, desp)) {
172 return (DESERR_HWERROR);
174 return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);