Mercurial > hg > early-roguelike
comparison srogue/xcrypt.c @ 121:e6c8652473fe
srogue: more compatibility improvements.
Randomness now uses mdport, and xcrypt.c has been replaced with the
rogue5 version.
Super-Rogue now builds on MinGW.
author | John "Elwin" Edwards |
---|---|
date | Sat, 03 May 2014 10:31:30 -0700 |
parents | 458df24e973d |
children |
comparison
equal
deleted
inserted
replaced
120:d6b7c3fb37ea | 121:e6c8652473fe |
---|---|
1 /* | 1 /* |
2 * FreeSec: libcrypt | 2 * FreeSec: libcrypt |
3 * | 3 * |
4 * Copyright (C) 1994 David Burren | 4 * Copyright (C) 1994 David Burren |
5 * All rights reserved. | 5 * All rights reserved. |
6 * | 6 * |
7 * Redistribution and use in source and binary forms, with or without | 7 * Redistribution and use in source and binary forms, with or without |
8 * modification, are permitted provided that the following conditions | 8 * modification, are permitted provided that the following conditions |
9 * are met: | 9 * are met: |
10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
11 * notice, this list of conditions and the following disclaimer. | 11 * notice, this list of conditions and the following disclaimer. |
12 * 2. Redistributions in binary form must reproduce the above copyright | 12 * 2. Redistributions in binary form must reproduce the above copyright |
13 * notice, this list of conditions and the following disclaimer in the | 13 * notice, this list of conditions and the following disclaimer in the |
14 * documentation and/or other materials provided with the distribution. | 14 * documentation and/or other materials provided with the distribution. |
15 * 3. Neither the name(s) of the author(s) nor the names of other contributors | 15 * 3. Neither the name(s) of the author(s) nor the names of other contributors |
16 * may be used to endorse or promote products derived from this software | 16 * may be used to endorse or promote products derived from this software |
17 * without specific prior written permission. | 17 * without specific prior written permission. |
18 * | 18 * |
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND | 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND |
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE | 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE |
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 * SUCH DAMAGE. | 29 * SUCH DAMAGE. |
30 * | 30 * |
31 * | 31 * |
32 * This is an original implementation of the DES and the crypt(3) interfaces | 32 * This is an original implementation of the DES and the crypt(3) interfaces |
33 * by David Burren <davidb@werj.com.au>. | 33 * by David Burren <davidb@werj.com.au>. |
34 * | 34 * |
48 * only the crypt() interface. This is required to make binaries linked | 48 * only the crypt() interface. This is required to make binaries linked |
49 * against crypt.o exportable or re-exportable from the USA. | 49 * against crypt.o exportable or re-exportable from the USA. |
50 */ | 50 */ |
51 | 51 |
52 #include <sys/types.h> | 52 #include <sys/types.h> |
53 #include <sys/param.h> | |
54 #include <string.h> | 53 #include <string.h> |
55 | 54 |
56 #ifdef DEBUG | 55 static unsigned int md_endian = 0x01020304; |
57 # include <stdio.h> | 56 |
58 #endif | 57 unsigned int |
58 xntohl(unsigned int x) | |
59 { | |
60 if ( *((char *)&md_endian) == 0x01 ) | |
61 return(x); | |
62 else | |
63 return( ((x & 0x000000ffU) << 24) | | |
64 ((x & 0x0000ff00U) << 8) | | |
65 ((x & 0x00ff0000U) >> 8) | | |
66 ((x & 0xff000000U) >> 24) ); | |
67 } | |
68 | |
69 unsigned int | |
70 xhtonl(unsigned int x) | |
71 { | |
72 if ( *((char *)&md_endian) == 0x01 ) | |
73 return(x); | |
74 else | |
75 return( ((x & 0x000000ffU) << 24) | | |
76 ((x & 0x0000ff00U) << 8) | | |
77 ((x & 0x00ff0000U) >> 8) | | |
78 ((x & 0xff000000U) >> 24) ); | |
79 } | |
80 | |
59 #define _PASSWORD_EFMT1 '_' | 81 #define _PASSWORD_EFMT1 '_' |
60 | 82 |
61 static unsigned char IP[64] = { | 83 static unsigned char IP[64] = { |
62 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, | 84 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, |
63 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, | 85 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, |
180 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | 202 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
181 /* 0000000000111111111122222222223333333333444444444455555555556666 */ | 203 /* 0000000000111111111122222222223333333333444444444455555555556666 */ |
182 /* 0123456789012345678901234567890123456789012345678901234567890123 */ | 204 /* 0123456789012345678901234567890123456789012345678901234567890123 */ |
183 | 205 |
184 static __inline int | 206 static __inline int |
185 ascii_to_bin(ch) | 207 ascii_to_bin(int ch) |
186 char ch; | |
187 { | 208 { |
188 if (ch > 'z') | 209 if (ch > 'z') |
189 return(0); | 210 return(0); |
190 if (ch >= 'a') | 211 if (ch >= 'a') |
191 return(ch - 'a' + 38); | 212 return(ch - 'a' + 38); |
199 return(ch - '.'); | 220 return(ch - '.'); |
200 return(0); | 221 return(0); |
201 } | 222 } |
202 | 223 |
203 static void | 224 static void |
204 des_init() | 225 des_init(void) |
205 { | 226 { |
206 int i, j, b, k, inbit, obit; | 227 int i, j, b, k, inbit, obit; |
207 unsigned int *p, *il, *ir, *fl, *fr; | 228 unsigned int *p, *il, *ir, *fl, *fr; |
208 | 229 |
209 old_rawkey0 = old_rawkey1 = 0; | 230 old_rawkey0 = old_rawkey1 = 0; |
234 /* | 255 /* |
235 * Set up the initial & final permutations into a useful form, and | 256 * Set up the initial & final permutations into a useful form, and |
236 * initialise the inverted key permutation. | 257 * initialise the inverted key permutation. |
237 */ | 258 */ |
238 for (i = 0; i < 64; i++) { | 259 for (i = 0; i < 64; i++) { |
239 init_perm[final_perm[i] = IP[i] - 1] = i; | 260 init_perm[final_perm[i] = IP[i] - 1] = (unsigned char) i; |
240 inv_key_perm[i] = 255; | 261 inv_key_perm[i] = 255; |
241 } | 262 } |
242 | 263 |
243 /* | 264 /* |
244 * Invert the key permutation and initialise the inverted key | 265 * Invert the key permutation and initialise the inverted key |
245 * compression permutation. | 266 * compression permutation. |
246 */ | 267 */ |
247 for (i = 0; i < 56; i++) { | 268 for (i = 0; i < 56; i++) { |
248 inv_key_perm[key_perm[i] - 1] = i; | 269 inv_key_perm[key_perm[i] - 1] = (unsigned char) i; |
249 inv_comp_perm[i] = 255; | 270 inv_comp_perm[i] = 255; |
250 } | 271 } |
251 | 272 |
252 /* | 273 /* |
253 * Invert the key compression permutation. | 274 * Invert the key compression permutation. |
254 */ | 275 */ |
255 for (i = 0; i < 48; i++) { | 276 for (i = 0; i < 48; i++) { |
256 inv_comp_perm[comp_perm[i] - 1] = i; | 277 inv_comp_perm[comp_perm[i] - 1] = (unsigned char) i; |
257 } | 278 } |
258 | 279 |
259 /* | 280 /* |
260 * Set up the OR-mask arrays for the initial and final permutations, | 281 * Set up the OR-mask arrays for the initial and final permutations, |
261 * and for the key initial and compression permutations. | 282 * and for the key initial and compression permutations. |
313 /* | 334 /* |
314 * Invert the P-box permutation, and convert into OR-masks for | 335 * Invert the P-box permutation, and convert into OR-masks for |
315 * handling the output of the S-box arrays setup above. | 336 * handling the output of the S-box arrays setup above. |
316 */ | 337 */ |
317 for (i = 0; i < 32; i++) | 338 for (i = 0; i < 32; i++) |
318 un_pbox[pbox[i] - 1] = i; | 339 un_pbox[pbox[i] - 1] = (unsigned char) i; |
319 | 340 |
320 for (b = 0; b < 4; b++) | 341 for (b = 0; b < 4; b++) |
321 for (i = 0; i < 256; i++) { | 342 for (i = 0; i < 256; i++) { |
322 *(p = &psbox[b][i]) = 0; | 343 *(p = &psbox[b][i]) = 0; |
323 for (j = 0; j < 8; j++) { | 344 for (j = 0; j < 8; j++) { |
328 | 349 |
329 des_initialised = 1; | 350 des_initialised = 1; |
330 } | 351 } |
331 | 352 |
332 static void | 353 static void |
333 setup_salt(salt) | 354 setup_salt(int salt) |
334 int salt; | |
335 { | 355 { |
336 unsigned int obit, saltbit; | 356 unsigned int obit, saltbit; |
337 int i; | 357 int i; |
338 | 358 |
339 if (salt == old_salt) | 359 if (salt == old_salt) |
350 obit >>= 1; | 370 obit >>= 1; |
351 } | 371 } |
352 } | 372 } |
353 | 373 |
354 static int | 374 static int |
355 des_setkey(key) | 375 des_setkey(const char *key) |
356 const char *key; | |
357 { | 376 { |
358 unsigned int k0, k1, rawkey0, rawkey1; | 377 unsigned int k0, k1, rawkey0, rawkey1; |
359 int shifts, round; | 378 int shifts, round; |
360 | 379 |
361 if (!des_initialised) | 380 if (!des_initialised) |
362 des_init(); | 381 des_init(); |
363 | 382 |
364 rawkey0 = ntohl(*(unsigned int *) key); | 383 rawkey0 = xntohl(*(unsigned int *) key); |
365 rawkey1 = ntohl(*(unsigned int *) (key + 4)); | 384 rawkey1 = xntohl(*(unsigned int *) (key + 4)); |
366 | 385 |
367 if ((rawkey0 | rawkey1) | 386 if ((rawkey0 | rawkey1) |
368 && rawkey0 == old_rawkey0 | 387 && rawkey0 == old_rawkey0 |
369 && rawkey1 == old_rawkey1) { | 388 && rawkey1 == old_rawkey1) { |
370 /* | 389 /* |
431 } | 450 } |
432 return(0); | 451 return(0); |
433 } | 452 } |
434 | 453 |
435 static int | 454 static int |
436 do_des(l_in, r_in, l_out, r_out, count) | 455 do_des(unsigned int l_in, unsigned int r_in, unsigned int *l_out, |
437 unsigned int l_in, r_in, *l_out, *r_out; | 456 unsigned int *r_out, int count) |
438 int count; | |
439 { | 457 { |
440 /* | 458 /* |
441 * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. | 459 * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. |
442 */ | 460 */ |
443 unsigned int l, r, *kl, *kr, *kl1, *kr1; | 461 unsigned int l, r, *kl, *kr, *kl1, *kr1; |
444 unsigned int f, r48l, r48r; | 462 unsigned int f = 0, r48l, r48r; |
445 int round; | 463 int round; |
446 | 464 |
447 if (count == 0) { | 465 if (count == 0) { |
448 return(1); | 466 return(1); |
449 } else if (count > 0) { | 467 } else if (count > 0) { |
549 | fp_maskr[7][r & 0xff]; | 567 | fp_maskr[7][r & 0xff]; |
550 return(0); | 568 return(0); |
551 } | 569 } |
552 | 570 |
553 static int | 571 static int |
554 des_cipher(in, out, salt, count) | 572 des_cipher(const char *in, char *out, int salt, int count) |
555 const char *in; | |
556 char *out; | |
557 int salt; | |
558 int count; | |
559 { | 573 { |
560 unsigned int l_out, r_out, rawl, rawr; | 574 unsigned int l_out, r_out, rawl, rawr; |
561 unsigned int x[2]; | 575 unsigned int x[2]; |
562 int retval; | 576 int retval; |
563 | 577 |
565 des_init(); | 579 des_init(); |
566 | 580 |
567 setup_salt(salt); | 581 setup_salt(salt); |
568 | 582 |
569 memcpy(x, in, sizeof x); | 583 memcpy(x, in, sizeof x); |
570 rawl = ntohl(x[0]); | 584 rawl = xntohl(x[0]); |
571 rawr = ntohl(x[1]); | 585 rawr = xntohl(x[1]); |
572 retval = do_des(rawl, rawr, &l_out, &r_out, count); | 586 retval = do_des(rawl, rawr, &l_out, &r_out, count); |
573 | 587 |
574 x[0] = htonl(l_out); | 588 x[0] = xhtonl(l_out); |
575 x[1] = htonl(r_out); | 589 x[1] = xhtonl(r_out); |
576 memcpy(out, x, sizeof x); | 590 memcpy(out, x, sizeof x); |
577 return(retval); | 591 return(retval); |
578 } | 592 } |
579 | 593 |
580 char * | 594 char * |
581 xcrypt(key, setting) | 595 xcrypt(const char *key, const char *setting) |
582 const char *key; | |
583 const char *setting; | |
584 { | 596 { |
585 int i; | 597 int i; |
586 unsigned int count, salt, l, r0, r1, keybuf[2]; | 598 unsigned int count, salt, l, r0, r1, keybuf[2]; |
587 unsigned char *p, *q; | 599 unsigned char *p, *q; |
588 static unsigned char output[21]; | 600 static unsigned char output[21]; |
597 q = (unsigned char *) keybuf; | 609 q = (unsigned char *) keybuf; |
598 while ((q - (unsigned char *) keybuf) < sizeof(keybuf)) { | 610 while ((q - (unsigned char *) keybuf) < sizeof(keybuf)) { |
599 if ((*q++ = *key << 1)) | 611 if ((*q++ = *key << 1)) |
600 key++; | 612 key++; |
601 } | 613 } |
602 if (des_setkey((unsigned char *) keybuf)) | 614 if (des_setkey((const char *) keybuf)) |
603 return(NULL); | 615 return(NULL); |
604 | 616 |
605 if (*setting == _PASSWORD_EFMT1) { | 617 if (*setting == _PASSWORD_EFMT1) { |
606 /* | 618 /* |
607 * "new"-style: | 619 * "new"-style: |
616 | 628 |
617 while (*key) { | 629 while (*key) { |
618 /* | 630 /* |
619 * Encrypt the key with itself. | 631 * Encrypt the key with itself. |
620 */ | 632 */ |
621 if (des_cipher((unsigned char*)keybuf, (unsigned char*)keybuf, 0, 1)) | 633 if (des_cipher((const char*)keybuf, (char*)keybuf, 0, 1)) |
622 return(NULL); | 634 return(NULL); |
623 /* | 635 /* |
624 * And XOR with the next 8 characters of the key. | 636 * And XOR with the next 8 characters of the key. |
625 */ | 637 */ |
626 q = (unsigned char *) keybuf; | 638 q = (unsigned char *) keybuf; |
627 while (((q - (unsigned char *) keybuf) < sizeof(keybuf)) && | 639 while (((q - (unsigned char *) keybuf) < sizeof(keybuf)) && |
628 *key) | 640 *key) |
629 *q++ ^= *key++ << 1; | 641 *q++ ^= *key++ << 1; |
630 | 642 |
631 if (des_setkey((unsigned char *) keybuf)) | 643 if (des_setkey((const char *) keybuf)) |
632 return(NULL); | 644 return(NULL); |
633 } | 645 } |
634 strncpy((char *)output, setting, 9); | 646 strncpy((char *)output, setting, 9); |
635 | 647 |
636 /* | 648 /* |