25 */
26
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
29
30 #pragma ident "%Z%%M% %I% %E% SMI"
31
32 #pragma weak _des_crypt = des_crypt
33 #pragma weak _des_encrypt = des_encrypt
34 #pragma weak _des_setkey = des_setkey
35
36 #include <sys/types.h>
37 #include <crypt.h>
38 #include "des_soft.h"
39
40 #include <stdlib.h>
41 #include <thread.h>
42 #include <pthread.h>
43 #include <sys/types.h>
44
45 /* EXPORT DELETE START */
46 /*
47 * This program implements the
48 * Proposed Federal Information Processing
49 * Data Encryption Standard.
50 * See Federal Register, March 17, 1975 (40FR12134)
51 */
52
53 /*
54 * Initial permutation,
55 */
56 static char IP[] = {
57 58, 50, 42, 34, 26, 18, 10, 2,
58 60, 52, 44, 36, 28, 20, 12, 4,
59 62, 54, 46, 38, 30, 22, 14, 6,
60 64, 56, 48, 40, 32, 24, 16, 8,
61 57, 49, 41, 33, 25, 17, 9, 1,
62 59, 51, 43, 35, 27, 19, 11, 3,
63 61, 53, 45, 37, 29, 21, 13, 5,
64 63, 55, 47, 39, 31, 23, 15, 7,
65 };
137 * The E bit-selection table.
138 */
139 static char E[48];
140 static char e2[] = {
141 32, 1, 2, 3, 4, 5,
142 4, 5, 6, 7, 8, 9,
143 8, 9, 10, 11, 12, 13,
144 12, 13, 14, 15, 16, 17,
145 16, 17, 18, 19, 20, 21,
146 20, 21, 22, 23, 24, 25,
147 24, 25, 26, 27, 28, 29,
148 28, 29, 30, 31, 32, 1,
149 };
150
151 /*
152 * Set up the key schedule from the key.
153 */
154
155 static mutex_t lock = DEFAULTMUTEX;
156
157 /* EXPORT DELETE END */
158
159
160 static void
161 des_setkey_nolock(const char *key)
162 {
163 /* EXPORT DELETE START */
164 int i, j, k;
165 char t;
166
167 /*
168 * First, generate C and D by permuting
169 * the key. The low order bit of each
170 * 8-bit char is not used, so C and D are only 28
171 * bits apiece.
172 */
173 for (i = 0; i < 28; i++) {
174 C[i] = key[PC1_C[i]-1];
175 D[i] = key[PC1_D[i]-1];
176 }
177 /*
178 * To generate Ki, rotate C and D according
179 * to schedule and pick up a permutation
180 * using PC2.
181 */
182 for (i = 0; i < 16; i++) {
183 /*
187 t = C[0];
188 for (j = 0; j < 28-1; j++)
189 C[j] = C[j+1];
190 C[27] = (char)t;
191 t = D[0];
192 for (j = 0; j < 28-1; j++)
193 D[j] = D[j+1];
194 D[27] = (char)t;
195 }
196 /*
197 * get Ki. Note C and D are concatenated.
198 */
199 for (j = 0; j < 24; j++) {
200 KS[i][j] = C[PC2_C[j]-1];
201 KS[i][j+24] = D[PC2_D[j]-28-1];
202 }
203 }
204
205 for (i = 0; i < 48; i++)
206 E[i] = e2[i];
207 /* EXPORT DELETE END */
208 }
209
210 void
211 des_setkey(const char *key)
212 {
213 /* EXPORT DELETE START */
214 (void) mutex_lock(&lock);
215 des_setkey_nolock(key);
216 (void) mutex_unlock(&lock);
217 /* EXPORT DELETE END */
218 }
219
220 /* EXPORT DELETE START */
221 /*
222 * The 8 selection functions.
223 * For some reason, they give a 0-origin
224 * index, unlike everything else.
225 */
226 static char S[8][64] = {
227 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
228 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
229 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
230 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
231
232 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
233 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
234 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
235 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
236
237 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
238 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
239 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
240 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
278 32, 27, 3, 9,
279 19, 13, 30, 6,
280 22, 11, 4, 25,
281 };
282
283 /*
284 * The current block, divided into 2 halves.
285 */
286 static char L[64];
287 static char tempL[32];
288 static char f[32];
289
290 /*
291 * The combination of the key and the input, before selection.
292 */
293 static char preS[48];
294
295 /*
296 * The payoff: encrypt a block.
297 */
298 /* EXPORT DELETE END */
299
300 static void
301 des_encrypt_nolock(char *block, int edflag)
302 {
303 /* EXPORT DELETE START */
304
305 if (edflag)
306 (void) _des_decrypt1(block, L, IP, &L[32],
307 preS, E, KS, S, f, tempL, P, FP);
308 else
309 (void) des_encrypt1(block, L, IP, &L[32],
310 preS, E, KS, S, f, tempL, P, FP);
311
312 /* EXPORT DELETE END */
313 }
314
315 void
316 des_encrypt(char *block, int edflag)
317 {
318 /* EXPORT DELETE START */
319 (void) mutex_lock(&lock);
320 des_encrypt_nolock(block, edflag);
321 (void) mutex_unlock(&lock);
322 /* EXPORT DELETE END */
323 }
324
325
326
327 #define IOBUF_SIZE 16
328
329 static char *
330 _get_iobuf(thread_key_t *keyp, unsigned size)
331 {
332 char *iobuf;
333
334 if (thr_keycreate_once(keyp, free) != 0)
335 return (NULL);
336 iobuf = pthread_getspecific(*keyp);
337 if (iobuf == NULL) {
338 if (thr_setspecific(*keyp, (iobuf = malloc(size))) != 0) {
339 if (iobuf)
340 (void) free(iobuf);
341 iobuf = NULL;
342 }
343 }
344 return (iobuf);
345 }
346
347 char *
348 des_crypt(const char *pw, const char *salt)
349 {
350 /* EXPORT DELETE START */
351 int i, j;
352 char c, temp;
353 char block[66];
354 static thread_key_t key = THR_ONCE_KEY;
355 char *iobuf = _get_iobuf(&key, IOBUF_SIZE);
356
357 (void) mutex_lock(&lock);
358 for (i = 0; i < 66; i++)
359 block[i] = 0;
360 for (i = 0; (c = *pw) && (i < 64); pw++) {
361 for (j = 0; j < 7; j++, i++)
362 block[i] = (c>>(6-j)) & 01;
363 i++;
364 }
365
366 des_setkey_nolock(block);
367
368 for (i = 0; i < 66; i++)
369 block[i] = 0;
370
389 (void) des_encrypt_nolock(block, 0);
390
391 for (i = 0; i < 11; i++) {
392 c = 0;
393 for (j = 0; j < 6; j++) {
394 c <<= 1;
395 c |= block[6*i+j];
396 }
397 c += '.';
398 if (c > '9')
399 c += 7;
400 if (c > 'Z')
401 c += 6;
402 iobuf[i+2] = (char)c;
403 }
404 iobuf[i+2] = 0;
405 if (iobuf[1] == 0)
406 iobuf[1] = iobuf[0];
407 (void) mutex_unlock(&lock);
408 return (iobuf);
409 #if 0
410 /* EXPORT DELETE END */
411 return (0);
412 /* EXPORT DELETE START */
413 #endif
414 /* EXPORT DELETE END */
415 }
|
25 */
26
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
29
30 #pragma ident "%Z%%M% %I% %E% SMI"
31
32 #pragma weak _des_crypt = des_crypt
33 #pragma weak _des_encrypt = des_encrypt
34 #pragma weak _des_setkey = des_setkey
35
36 #include <sys/types.h>
37 #include <crypt.h>
38 #include "des_soft.h"
39
40 #include <stdlib.h>
41 #include <thread.h>
42 #include <pthread.h>
43 #include <sys/types.h>
44
45 /*
46 * This program implements the
47 * Proposed Federal Information Processing
48 * Data Encryption Standard.
49 * See Federal Register, March 17, 1975 (40FR12134)
50 */
51
52 /*
53 * Initial permutation,
54 */
55 static char IP[] = {
56 58, 50, 42, 34, 26, 18, 10, 2,
57 60, 52, 44, 36, 28, 20, 12, 4,
58 62, 54, 46, 38, 30, 22, 14, 6,
59 64, 56, 48, 40, 32, 24, 16, 8,
60 57, 49, 41, 33, 25, 17, 9, 1,
61 59, 51, 43, 35, 27, 19, 11, 3,
62 61, 53, 45, 37, 29, 21, 13, 5,
63 63, 55, 47, 39, 31, 23, 15, 7,
64 };
136 * The E bit-selection table.
137 */
138 static char E[48];
139 static char e2[] = {
140 32, 1, 2, 3, 4, 5,
141 4, 5, 6, 7, 8, 9,
142 8, 9, 10, 11, 12, 13,
143 12, 13, 14, 15, 16, 17,
144 16, 17, 18, 19, 20, 21,
145 20, 21, 22, 23, 24, 25,
146 24, 25, 26, 27, 28, 29,
147 28, 29, 30, 31, 32, 1,
148 };
149
150 /*
151 * Set up the key schedule from the key.
152 */
153
154 static mutex_t lock = DEFAULTMUTEX;
155
156 static void
157 des_setkey_nolock(const char *key)
158 {
159 int i, j, k;
160 char t;
161
162 /*
163 * First, generate C and D by permuting
164 * the key. The low order bit of each
165 * 8-bit char is not used, so C and D are only 28
166 * bits apiece.
167 */
168 for (i = 0; i < 28; i++) {
169 C[i] = key[PC1_C[i]-1];
170 D[i] = key[PC1_D[i]-1];
171 }
172 /*
173 * To generate Ki, rotate C and D according
174 * to schedule and pick up a permutation
175 * using PC2.
176 */
177 for (i = 0; i < 16; i++) {
178 /*
182 t = C[0];
183 for (j = 0; j < 28-1; j++)
184 C[j] = C[j+1];
185 C[27] = (char)t;
186 t = D[0];
187 for (j = 0; j < 28-1; j++)
188 D[j] = D[j+1];
189 D[27] = (char)t;
190 }
191 /*
192 * get Ki. Note C and D are concatenated.
193 */
194 for (j = 0; j < 24; j++) {
195 KS[i][j] = C[PC2_C[j]-1];
196 KS[i][j+24] = D[PC2_D[j]-28-1];
197 }
198 }
199
200 for (i = 0; i < 48; i++)
201 E[i] = e2[i];
202 }
203
204 void
205 des_setkey(const char *key)
206 {
207 (void) mutex_lock(&lock);
208 des_setkey_nolock(key);
209 (void) mutex_unlock(&lock);
210 }
211
212 /*
213 * The 8 selection functions.
214 * For some reason, they give a 0-origin
215 * index, unlike everything else.
216 */
217 static char S[8][64] = {
218 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
219 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
220 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
221 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
222
223 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
224 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
225 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
226 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
227
228 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
229 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
230 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
231 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
269 32, 27, 3, 9,
270 19, 13, 30, 6,
271 22, 11, 4, 25,
272 };
273
274 /*
275 * The current block, divided into 2 halves.
276 */
277 static char L[64];
278 static char tempL[32];
279 static char f[32];
280
281 /*
282 * The combination of the key and the input, before selection.
283 */
284 static char preS[48];
285
286 /*
287 * The payoff: encrypt a block.
288 */
289
290 static void
291 des_encrypt_nolock(char *block, int edflag)
292 {
293 if (edflag)
294 (void) _des_decrypt1(block, L, IP, &L[32],
295 preS, E, KS, S, f, tempL, P, FP);
296 else
297 (void) des_encrypt1(block, L, IP, &L[32],
298 preS, E, KS, S, f, tempL, P, FP);
299 }
300
301 void
302 des_encrypt(char *block, int edflag)
303 {
304 (void) mutex_lock(&lock);
305 des_encrypt_nolock(block, edflag);
306 (void) mutex_unlock(&lock);
307 }
308
309
310
311 #define IOBUF_SIZE 16
312
313 static char *
314 _get_iobuf(thread_key_t *keyp, unsigned size)
315 {
316 char *iobuf;
317
318 if (thr_keycreate_once(keyp, free) != 0)
319 return (NULL);
320 iobuf = pthread_getspecific(*keyp);
321 if (iobuf == NULL) {
322 if (thr_setspecific(*keyp, (iobuf = malloc(size))) != 0) {
323 if (iobuf)
324 (void) free(iobuf);
325 iobuf = NULL;
326 }
327 }
328 return (iobuf);
329 }
330
331 char *
332 des_crypt(const char *pw, const char *salt)
333 {
334 int i, j;
335 char c, temp;
336 char block[66];
337 static thread_key_t key = THR_ONCE_KEY;
338 char *iobuf = _get_iobuf(&key, IOBUF_SIZE);
339
340 (void) mutex_lock(&lock);
341 for (i = 0; i < 66; i++)
342 block[i] = 0;
343 for (i = 0; (c = *pw) && (i < 64); pw++) {
344 for (j = 0; j < 7; j++, i++)
345 block[i] = (c>>(6-j)) & 01;
346 i++;
347 }
348
349 des_setkey_nolock(block);
350
351 for (i = 0; i < 66; i++)
352 block[i] = 0;
353
372 (void) des_encrypt_nolock(block, 0);
373
374 for (i = 0; i < 11; i++) {
375 c = 0;
376 for (j = 0; j < 6; j++) {
377 c <<= 1;
378 c |= block[6*i+j];
379 }
380 c += '.';
381 if (c > '9')
382 c += 7;
383 if (c > 'Z')
384 c += 6;
385 iobuf[i+2] = (char)c;
386 }
387 iobuf[i+2] = 0;
388 if (iobuf[1] == 0)
389 iobuf[1] = iobuf[0];
390 (void) mutex_unlock(&lock);
391 return (iobuf);
392 }
|