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