diff options
Diffstat (limited to 'src/lib/libc/crypt/crypt.c')
| -rw-r--r-- | src/lib/libc/crypt/crypt.c | 84 |
1 files changed, 57 insertions, 27 deletions
diff --git a/src/lib/libc/crypt/crypt.c b/src/lib/libc/crypt/crypt.c index 17b50b7f52..7f07f4e7ee 100644 --- a/src/lib/libc/crypt/crypt.c +++ b/src/lib/libc/crypt/crypt.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: crypt.c,v 1.16 2002/04/29 06:26:50 pvalchev Exp $ */ | 1 | /* $OpenBSD: crypt.c,v 1.17 2003/08/07 00:32:12 deraadt Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * FreeSec: libcrypt | 4 | * FreeSec: libcrypt |
| @@ -44,20 +44,16 @@ | |||
| 44 | * pbox, and final permutations are inverted (this has been brought to the | 44 | * pbox, and final permutations are inverted (this has been brought to the |
| 45 | * attention of the author). A list of errata for this book has been | 45 | * attention of the author). A list of errata for this book has been |
| 46 | * posted to the sci.crypt newsgroup by the author and is available for FTP. | 46 | * posted to the sci.crypt newsgroup by the author and is available for FTP. |
| 47 | * | ||
| 48 | * NOTE: | ||
| 49 | * This file has a static version of des_setkey() so that crypt.o exports | ||
| 50 | * only the crypt() interface. This is required to make binaries linked | ||
| 51 | * against crypt.o exportable or re-exportable from the USA. | ||
| 52 | */ | 47 | */ |
| 53 | 48 | ||
| 54 | #if defined(LIBC_SCCS) && !defined(lint) | 49 | #if defined(LIBC_SCCS) && !defined(lint) |
| 55 | static char rcsid[] = "$OpenBSD: crypt.c,v 1.16 2002/04/29 06:26:50 pvalchev Exp $"; | 50 | static char rcsid[] = "$OpenBSD: crypt.c,v 1.17 2003/08/07 00:32:12 deraadt Exp $"; |
| 56 | #endif /* LIBC_SCCS and not lint */ | 51 | #endif /* LIBC_SCCS and not lint */ |
| 57 | 52 | ||
| 58 | #include <sys/types.h> | 53 | #include <sys/types.h> |
| 59 | #include <sys/param.h> | 54 | #include <sys/param.h> |
| 60 | #include <pwd.h> | 55 | #include <pwd.h> |
| 56 | #include <unistd.h> | ||
| 61 | #include <string.h> | 57 | #include <string.h> |
| 62 | 58 | ||
| 63 | #ifdef DEBUG | 59 | #ifdef DEBUG |
| @@ -189,8 +185,7 @@ static u_char ascii64[] = | |||
| 189 | /* 0123456789012345678901234567890123456789012345678901234567890123 */ | 185 | /* 0123456789012345678901234567890123456789012345678901234567890123 */ |
| 190 | 186 | ||
| 191 | static __inline int | 187 | static __inline int |
| 192 | ascii_to_bin(ch) | 188 | ascii_to_bin(char ch) |
| 193 | char ch; | ||
| 194 | { | 189 | { |
| 195 | if (ch > 'z') | 190 | if (ch > 'z') |
| 196 | return(0); | 191 | return(0); |
| @@ -208,7 +203,7 @@ ascii_to_bin(ch) | |||
| 208 | } | 203 | } |
| 209 | 204 | ||
| 210 | static void | 205 | static void |
| 211 | des_init() | 206 | des_init(void) |
| 212 | { | 207 | { |
| 213 | int i, j, b, k, inbit, obit; | 208 | int i, j, b, k, inbit, obit; |
| 214 | u_int32_t *p, *il, *ir, *fl, *fr; | 209 | u_int32_t *p, *il, *ir, *fl, *fr; |
| @@ -338,8 +333,7 @@ des_init() | |||
| 338 | } | 333 | } |
| 339 | 334 | ||
| 340 | static void | 335 | static void |
| 341 | setup_salt(salt) | 336 | setup_salt(int32_t salt) |
| 342 | int32_t salt; | ||
| 343 | { | 337 | { |
| 344 | u_int32_t obit, saltbit; | 338 | u_int32_t obit, saltbit; |
| 345 | int i; | 339 | int i; |
| @@ -359,9 +353,8 @@ setup_salt(salt) | |||
| 359 | } | 353 | } |
| 360 | } | 354 | } |
| 361 | 355 | ||
| 362 | static int | 356 | int |
| 363 | des_setkey(key) | 357 | des_setkey(const char *key) |
| 364 | const char *key; | ||
| 365 | { | 358 | { |
| 366 | u_int32_t k0, k1, rawkey0, rawkey1; | 359 | u_int32_t k0, k1, rawkey0, rawkey1; |
| 367 | int shifts, round; | 360 | int shifts, round; |
| @@ -441,9 +434,8 @@ des_setkey(key) | |||
| 441 | } | 434 | } |
| 442 | 435 | ||
| 443 | static int | 436 | static int |
| 444 | do_des(l_in, r_in, l_out, r_out, count) | 437 | do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, |
| 445 | u_int32_t l_in, r_in, *l_out, *r_out; | 438 | int count) |
| 446 | int count; | ||
| 447 | { | 439 | { |
| 448 | /* | 440 | /* |
| 449 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. | 441 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. |
| @@ -558,12 +550,8 @@ do_des(l_in, r_in, l_out, r_out, count) | |||
| 558 | return(0); | 550 | return(0); |
| 559 | } | 551 | } |
| 560 | 552 | ||
| 561 | static int | 553 | int |
| 562 | des_cipher(in, out, salt, count) | 554 | des_cipher(const char *in, char *out, int32_t salt, int count) |
| 563 | const char *in; | ||
| 564 | char *out; | ||
| 565 | int32_t salt; | ||
| 566 | int count; | ||
| 567 | { | 555 | { |
| 568 | u_int32_t l_out, r_out, rawl, rawr; | 556 | u_int32_t l_out, r_out, rawl, rawr; |
| 569 | u_int32_t x[2]; | 557 | u_int32_t x[2]; |
| @@ -586,9 +574,7 @@ des_cipher(in, out, salt, count) | |||
| 586 | } | 574 | } |
| 587 | 575 | ||
| 588 | char * | 576 | char * |
| 589 | crypt(key, setting) | 577 | crypt(const char *key, const char *setting) |
| 590 | const char *key; | ||
| 591 | const char *setting; | ||
| 592 | { | 578 | { |
| 593 | int i; | 579 | int i; |
| 594 | u_int32_t count, salt, l, r0, r1, keybuf[2]; | 580 | u_int32_t count, salt, l, r0, r1, keybuf[2]; |
| @@ -683,6 +669,7 @@ crypt(key, setting) | |||
| 683 | p = output + 2; | 669 | p = output + 2; |
| 684 | } | 670 | } |
| 685 | setup_salt(salt); | 671 | setup_salt(salt); |
| 672 | |||
| 686 | /* | 673 | /* |
| 687 | * Do it. | 674 | * Do it. |
| 688 | */ | 675 | */ |
| @@ -711,3 +698,46 @@ crypt(key, setting) | |||
| 711 | 698 | ||
| 712 | return((char *)output); | 699 | return((char *)output); |
| 713 | } | 700 | } |
| 701 | |||
| 702 | int | ||
| 703 | setkey(const char *key) | ||
| 704 | { | ||
| 705 | int i, j; | ||
| 706 | u_int32_t packed_keys[2]; | ||
| 707 | u_char *p; | ||
| 708 | |||
| 709 | p = (u_char *) packed_keys; | ||
| 710 | |||
| 711 | for (i = 0; i < 8; i++) { | ||
| 712 | p[i] = 0; | ||
| 713 | for (j = 0; j < 8; j++) | ||
| 714 | if (*key++ & 1) | ||
| 715 | p[i] |= bits8[j]; | ||
| 716 | } | ||
| 717 | return(des_setkey(p)); | ||
| 718 | } | ||
| 719 | |||
| 720 | int | ||
| 721 | encrypt(char *block, int flag) | ||
| 722 | { | ||
| 723 | u_int32_t io[2]; | ||
| 724 | u_char *p; | ||
| 725 | int i, j, retval; | ||
| 726 | |||
| 727 | if (!des_initialised) | ||
| 728 | des_init(); | ||
| 729 | |||
| 730 | setup_salt(0); | ||
| 731 | p = (u_char *)block; | ||
| 732 | for (i = 0; i < 2; i++) { | ||
| 733 | io[i] = 0L; | ||
| 734 | for (j = 0; j < 32; j++) | ||
| 735 | if (*p++ & 1) | ||
| 736 | io[i] |= bits32[j]; | ||
| 737 | } | ||
| 738 | retval = do_des(io[0], io[1], io, io + 1, flag ? -1 : 1); | ||
| 739 | for (i = 0; i < 2; i++) | ||
| 740 | for (j = 0; j < 32; j++) | ||
| 741 | block[(i << 5) | j] = (io[i] & bits32[j]) ? 1 : 0; | ||
| 742 | return(retval); | ||
| 743 | } | ||
