Print this page
first pass
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libcrypt/common/des_crypt.c
+++ new/usr/src/lib/libcrypt/common/des_crypt.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) 1988 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30 #pragma ident "%Z%%M% %I% %E% SMI"
31 31
32 32 #pragma weak _des_crypt = des_crypt
33 33 #pragma weak _des_encrypt = des_encrypt
34 34 #pragma weak _des_setkey = des_setkey
↓ open down ↓ |
34 lines elided |
↑ open up ↑ |
35 35
36 36 #include <sys/types.h>
37 37 #include <crypt.h>
38 38 #include "des_soft.h"
39 39
40 40 #include <stdlib.h>
41 41 #include <thread.h>
42 42 #include <pthread.h>
43 43 #include <sys/types.h>
44 44
45 -/* EXPORT DELETE START */
46 45 /*
47 46 * This program implements the
48 47 * Proposed Federal Information Processing
49 48 * Data Encryption Standard.
50 49 * See Federal Register, March 17, 1975 (40FR12134)
51 50 */
52 51
53 52 /*
54 53 * Initial permutation,
55 54 */
56 55 static char IP[] = {
57 56 58, 50, 42, 34, 26, 18, 10, 2,
58 57 60, 52, 44, 36, 28, 20, 12, 4,
59 58 62, 54, 46, 38, 30, 22, 14, 6,
60 59 64, 56, 48, 40, 32, 24, 16, 8,
61 60 57, 49, 41, 33, 25, 17, 9, 1,
62 61 59, 51, 43, 35, 27, 19, 11, 3,
63 62 61, 53, 45, 37, 29, 21, 13, 5,
64 63 63, 55, 47, 39, 31, 23, 15, 7,
65 64 };
66 65
67 66 /*
68 67 * Final permutation, FP = IP^(-1)
69 68 */
70 69 static char FP[] = {
71 70 40, 8, 48, 16, 56, 24, 64, 32,
72 71 39, 7, 47, 15, 55, 23, 63, 31,
73 72 38, 6, 46, 14, 54, 22, 62, 30,
74 73 37, 5, 45, 13, 53, 21, 61, 29,
75 74 36, 4, 44, 12, 52, 20, 60, 28,
76 75 35, 3, 43, 11, 51, 19, 59, 27,
77 76 34, 2, 42, 10, 50, 18, 58, 26,
78 77 33, 1, 41, 9, 49, 17, 57, 25,
79 78 };
80 79
81 80 /*
82 81 * Permuted-choice 1 from the key bits
83 82 * to yield C and D.
84 83 * Note that bits 8, 16... are left out:
85 84 * They are intended for a parity check.
86 85 */
87 86 static char PC1_C[] = {
88 87 57, 49, 41, 33, 25, 17, 9,
89 88 1, 58, 50, 42, 34, 26, 18,
90 89 10, 2, 59, 51, 43, 35, 27,
91 90 19, 11, 3, 60, 52, 44, 36,
92 91 };
93 92
94 93 static char PC1_D[] = {
95 94 63, 55, 47, 39, 31, 23, 15,
96 95 7, 62, 54, 46, 38, 30, 22,
97 96 14, 6, 61, 53, 45, 37, 29,
98 97 21, 13, 5, 28, 20, 12, 4,
99 98 };
100 99
101 100 /*
102 101 * Sequence of shifts used for the key schedule.
103 102 */
104 103 static char shifts[] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, };
105 104
106 105 /*
107 106 * Permuted-choice 2, to pick out the bits from
108 107 * the CD array that generate the key schedule.
109 108 */
110 109 static char PC2_C[] = {
111 110 14, 17, 11, 24, 1, 5,
112 111 3, 28, 15, 6, 21, 10,
113 112 23, 19, 12, 4, 26, 8,
114 113 16, 7, 27, 20, 13, 2,
115 114 };
116 115
117 116 static char PC2_D[] = {
118 117 41, 52, 31, 37, 47, 55,
119 118 30, 40, 51, 45, 33, 48,
120 119 44, 49, 39, 56, 34, 53,
121 120 46, 42, 50, 36, 29, 32,
122 121 };
123 122
124 123 /*
125 124 * The C and D arrays used to calculate the key schedule.
126 125 */
127 126
128 127 static char C[28];
129 128 static char D[28];
130 129 /*
131 130 * The key schedule.
132 131 * Generated from the key.
133 132 */
134 133 static char KS[16][48];
135 134
136 135 /*
137 136 * The E bit-selection table.
138 137 */
139 138 static char E[48];
140 139 static char e2[] = {
141 140 32, 1, 2, 3, 4, 5,
142 141 4, 5, 6, 7, 8, 9,
143 142 8, 9, 10, 11, 12, 13,
144 143 12, 13, 14, 15, 16, 17,
145 144 16, 17, 18, 19, 20, 21,
146 145 20, 21, 22, 23, 24, 25,
↓ open down ↓ |
91 lines elided |
↑ open up ↑ |
147 146 24, 25, 26, 27, 28, 29,
148 147 28, 29, 30, 31, 32, 1,
149 148 };
150 149
151 150 /*
152 151 * Set up the key schedule from the key.
153 152 */
154 153
155 154 static mutex_t lock = DEFAULTMUTEX;
156 155
157 -/* EXPORT DELETE END */
158 -
159 -
160 156 static void
161 157 des_setkey_nolock(const char *key)
162 158 {
163 -/* EXPORT DELETE START */
164 159 int i, j, k;
165 160 char t;
166 161
167 162 /*
168 163 * First, generate C and D by permuting
169 164 * the key. The low order bit of each
170 165 * 8-bit char is not used, so C and D are only 28
171 166 * bits apiece.
172 167 */
173 168 for (i = 0; i < 28; i++) {
174 169 C[i] = key[PC1_C[i]-1];
175 170 D[i] = key[PC1_D[i]-1];
176 171 }
177 172 /*
178 173 * To generate Ki, rotate C and D according
179 174 * to schedule and pick up a permutation
180 175 * using PC2.
181 176 */
182 177 for (i = 0; i < 16; i++) {
183 178 /*
184 179 * rotate.
185 180 */
186 181 for (k = 0; k < shifts[i]; k++) {
187 182 t = C[0];
188 183 for (j = 0; j < 28-1; j++)
189 184 C[j] = C[j+1];
190 185 C[27] = (char)t;
191 186 t = D[0];
192 187 for (j = 0; j < 28-1; j++)
193 188 D[j] = D[j+1];
194 189 D[27] = (char)t;
195 190 }
196 191 /*
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
197 192 * get Ki. Note C and D are concatenated.
198 193 */
199 194 for (j = 0; j < 24; j++) {
200 195 KS[i][j] = C[PC2_C[j]-1];
201 196 KS[i][j+24] = D[PC2_D[j]-28-1];
202 197 }
203 198 }
204 199
205 200 for (i = 0; i < 48; i++)
206 201 E[i] = e2[i];
207 -/* EXPORT DELETE END */
208 202 }
209 203
210 204 void
211 205 des_setkey(const char *key)
212 206 {
213 -/* EXPORT DELETE START */
214 207 (void) mutex_lock(&lock);
215 208 des_setkey_nolock(key);
216 209 (void) mutex_unlock(&lock);
217 -/* EXPORT DELETE END */
218 210 }
219 211
220 -/* EXPORT DELETE START */
221 212 /*
222 213 * The 8 selection functions.
223 214 * For some reason, they give a 0-origin
224 215 * index, unlike everything else.
225 216 */
226 217 static char S[8][64] = {
227 218 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
228 219 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
229 220 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
230 221 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
231 222
232 223 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
233 224 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
234 225 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
235 226 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
236 227
237 228 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
238 229 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
239 230 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
240 231 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
241 232
242 233 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
243 234 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
244 235 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
245 236 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
246 237
247 238 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
248 239 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
249 240 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
250 241 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
251 242
252 243 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
253 244 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
254 245 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
255 246 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
256 247
257 248 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
258 249 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
259 250 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
260 251 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
261 252
262 253 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
263 254 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
264 255 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
265 256 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11,
266 257 };
267 258
268 259 /*
269 260 * P is a permutation on the selected combination
270 261 * of the current L and key.
271 262 */
272 263 static char P[] = {
273 264 16, 7, 20, 21,
274 265 29, 12, 28, 17,
275 266 1, 15, 23, 26,
276 267 5, 18, 31, 10,
277 268 2, 8, 24, 14,
278 269 32, 27, 3, 9,
279 270 19, 13, 30, 6,
280 271 22, 11, 4, 25,
281 272 };
282 273
283 274 /*
284 275 * The current block, divided into 2 halves.
285 276 */
286 277 static char L[64];
287 278 static char tempL[32];
↓ open down ↓ |
57 lines elided |
↑ open up ↑ |
288 279 static char f[32];
289 280
290 281 /*
291 282 * The combination of the key and the input, before selection.
292 283 */
293 284 static char preS[48];
294 285
295 286 /*
296 287 * The payoff: encrypt a block.
297 288 */
298 -/* EXPORT DELETE END */
299 289
300 290 static void
301 291 des_encrypt_nolock(char *block, int edflag)
302 292 {
303 -/* EXPORT DELETE START */
304 -
305 293 if (edflag)
306 294 (void) _des_decrypt1(block, L, IP, &L[32],
307 295 preS, E, KS, S, f, tempL, P, FP);
308 296 else
309 297 (void) des_encrypt1(block, L, IP, &L[32],
310 298 preS, E, KS, S, f, tempL, P, FP);
311 -
312 -/* EXPORT DELETE END */
313 299 }
314 300
315 301 void
316 302 des_encrypt(char *block, int edflag)
317 303 {
318 -/* EXPORT DELETE START */
319 304 (void) mutex_lock(&lock);
320 305 des_encrypt_nolock(block, edflag);
321 306 (void) mutex_unlock(&lock);
322 -/* EXPORT DELETE END */
323 307 }
324 308
325 309
326 310
327 311 #define IOBUF_SIZE 16
328 312
329 313 static char *
330 314 _get_iobuf(thread_key_t *keyp, unsigned size)
331 315 {
332 316 char *iobuf;
333 317
334 318 if (thr_keycreate_once(keyp, free) != 0)
335 319 return (NULL);
336 320 iobuf = pthread_getspecific(*keyp);
337 321 if (iobuf == NULL) {
338 322 if (thr_setspecific(*keyp, (iobuf = malloc(size))) != 0) {
339 323 if (iobuf)
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
340 324 (void) free(iobuf);
341 325 iobuf = NULL;
342 326 }
343 327 }
344 328 return (iobuf);
345 329 }
346 330
347 331 char *
348 332 des_crypt(const char *pw, const char *salt)
349 333 {
350 -/* EXPORT DELETE START */
351 334 int i, j;
352 335 char c, temp;
353 336 char block[66];
354 337 static thread_key_t key = THR_ONCE_KEY;
355 338 char *iobuf = _get_iobuf(&key, IOBUF_SIZE);
356 339
357 340 (void) mutex_lock(&lock);
358 341 for (i = 0; i < 66; i++)
359 342 block[i] = 0;
360 343 for (i = 0; (c = *pw) && (i < 64); pw++) {
361 344 for (j = 0; j < 7; j++, i++)
362 345 block[i] = (c>>(6-j)) & 01;
363 346 i++;
364 347 }
365 348
366 349 des_setkey_nolock(block);
367 350
368 351 for (i = 0; i < 66; i++)
369 352 block[i] = 0;
370 353
371 354 for (i = 0; i < 2; i++) {
372 355 c = *salt++;
373 356 iobuf[i] = (char)c;
374 357 if (c > 'Z')
375 358 c -= 6;
376 359 if (c > '9')
377 360 c -= 7;
378 361 c -= '.';
379 362 for (j = 0; j < 6; j++) {
380 363 if ((c>>j) & 01) {
381 364 temp = E[6*i+j];
382 365 E[6*i+j] = E[6*i+j+24];
383 366 E[6*i+j+24] = (char)temp;
384 367 }
385 368 }
386 369 }
387 370
388 371 for (i = 0; i < 25; i++)
389 372 (void) des_encrypt_nolock(block, 0);
390 373
391 374 for (i = 0; i < 11; i++) {
392 375 c = 0;
393 376 for (j = 0; j < 6; j++) {
394 377 c <<= 1;
395 378 c |= block[6*i+j];
396 379 }
397 380 c += '.';
398 381 if (c > '9')
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
399 382 c += 7;
400 383 if (c > 'Z')
401 384 c += 6;
402 385 iobuf[i+2] = (char)c;
403 386 }
404 387 iobuf[i+2] = 0;
405 388 if (iobuf[1] == 0)
406 389 iobuf[1] = iobuf[0];
407 390 (void) mutex_unlock(&lock);
408 391 return (iobuf);
409 -#if 0
410 -/* EXPORT DELETE END */
411 - return (0);
412 -/* EXPORT DELETE START */
413 -#endif
414 -/* EXPORT DELETE END */
415 392 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX