diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/crypt/crypt.c | 64 |
1 files changed, 33 insertions, 31 deletions
diff --git a/src/lib/libc/crypt/crypt.c b/src/lib/libc/crypt/crypt.c index c74a6784bb..a4c864dea3 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.23 2014/11/25 03:04:22 tedu Exp $ */ | 1 | /* $OpenBSD: crypt.c,v 1.24 2014/11/25 03:23:22 tedu Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * FreeSec: libcrypt | 4 | * FreeSec: libcrypt |
| @@ -162,8 +162,8 @@ const u_char _des_bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; | |||
| 162 | 162 | ||
| 163 | static const u_int32_t *bits28, *bits24; | 163 | static const u_int32_t *bits28, *bits24; |
| 164 | static u_char init_perm[64], final_perm[64]; | 164 | static u_char init_perm[64], final_perm[64]; |
| 165 | static u_int32_t g_en_keysl[16], g_en_keysr[16]; | 165 | static u_int32_t en_keysl[16], en_keysr[16]; |
| 166 | static u_int32_t g_de_keysl[16], g_de_keysr[16]; | 166 | static u_int32_t de_keysl[16], de_keysr[16]; |
| 167 | int _des_initialised = 0; | 167 | int _des_initialised = 0; |
| 168 | static u_char m_sbox[4][4096]; | 168 | static u_char m_sbox[4][4096]; |
| 169 | static u_int32_t psbox[4][256]; | 169 | static u_int32_t psbox[4][256]; |
| @@ -171,6 +171,7 @@ static u_int32_t ip_maskl[8][256], ip_maskr[8][256]; | |||
| 171 | static u_int32_t fp_maskl[8][256], fp_maskr[8][256]; | 171 | static u_int32_t fp_maskl[8][256], fp_maskr[8][256]; |
| 172 | static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; | 172 | static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; |
| 173 | static u_int32_t comp_maskl[8][128], comp_maskr[8][128]; | 173 | static u_int32_t comp_maskl[8][128], comp_maskr[8][128]; |
| 174 | static u_int32_t old_rawkey0, old_rawkey1; | ||
| 174 | 175 | ||
| 175 | static u_char ascii64[] = | 176 | static u_char ascii64[] = |
| 176 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | 177 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| @@ -201,6 +202,7 @@ _des_init(void) | |||
| 201 | int i, j, b, k, inbit, obit; | 202 | int i, j, b, k, inbit, obit; |
| 202 | u_int32_t *p, *il, *ir, *fl, *fr; | 203 | u_int32_t *p, *il, *ir, *fl, *fr; |
| 203 | 204 | ||
| 205 | old_rawkey0 = old_rawkey1 = 0; | ||
| 204 | bits24 = (bits28 = _des_bits32 + 4) + 4; | 206 | bits24 = (bits28 = _des_bits32 + 4) + 4; |
| 205 | 207 | ||
| 206 | /* | 208 | /* |
| @@ -340,8 +342,8 @@ _des_setup_salt(int32_t salt) | |||
| 340 | return saltbits; | 342 | return saltbits; |
| 341 | } | 343 | } |
| 342 | 344 | ||
| 343 | static int | 345 | int |
| 344 | _des_do_setkey(const char *key, u_int32_t *en_keysl, u_int32_t *en_keysr) | 346 | des_setkey(const char *key) |
| 345 | { | 347 | { |
| 346 | u_int32_t k0, k1, rawkey0, rawkey1; | 348 | u_int32_t k0, k1, rawkey0, rawkey1; |
| 347 | int shifts, round; | 349 | int shifts, round; |
| @@ -352,6 +354,20 @@ _des_do_setkey(const char *key, u_int32_t *en_keysl, u_int32_t *en_keysr) | |||
| 352 | rawkey0 = ntohl(*(u_int32_t *) key); | 354 | rawkey0 = ntohl(*(u_int32_t *) key); |
| 353 | rawkey1 = ntohl(*(u_int32_t *) (key + 4)); | 355 | rawkey1 = ntohl(*(u_int32_t *) (key + 4)); |
| 354 | 356 | ||
| 357 | if ((rawkey0 | rawkey1) | ||
| 358 | && rawkey0 == old_rawkey0 | ||
| 359 | && rawkey1 == old_rawkey1) { | ||
| 360 | /* | ||
| 361 | * Already setup for this key. | ||
| 362 | * This optimisation fails on a zero key (which is weak and | ||
| 363 | * has bad parity anyway) in order to simplify the starting | ||
| 364 | * conditions. | ||
| 365 | */ | ||
| 366 | return(0); | ||
| 367 | } | ||
| 368 | old_rawkey0 = rawkey0; | ||
| 369 | old_rawkey1 = rawkey1; | ||
| 370 | |||
| 355 | /* | 371 | /* |
| 356 | * Do key permutation and split into two 28-bit subkeys. | 372 | * Do key permutation and split into two 28-bit subkeys. |
| 357 | */ | 373 | */ |
| @@ -383,7 +399,7 @@ _des_do_setkey(const char *key, u_int32_t *en_keysl, u_int32_t *en_keysr) | |||
| 383 | t0 = (k0 << shifts) | (k0 >> (28 - shifts)); | 399 | t0 = (k0 << shifts) | (k0 >> (28 - shifts)); |
| 384 | t1 = (k1 << shifts) | (k1 >> (28 - shifts)); | 400 | t1 = (k1 << shifts) | (k1 >> (28 - shifts)); |
| 385 | 401 | ||
| 386 | g_de_keysl[15 - round] = /* XXX global */ | 402 | de_keysl[15 - round] = |
| 387 | en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] | 403 | en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] |
| 388 | | comp_maskl[1][(t0 >> 14) & 0x7f] | 404 | | comp_maskl[1][(t0 >> 14) & 0x7f] |
| 389 | | comp_maskl[2][(t0 >> 7) & 0x7f] | 405 | | comp_maskl[2][(t0 >> 7) & 0x7f] |
| @@ -393,7 +409,7 @@ _des_do_setkey(const char *key, u_int32_t *en_keysl, u_int32_t *en_keysr) | |||
| 393 | | comp_maskl[6][(t1 >> 7) & 0x7f] | 409 | | comp_maskl[6][(t1 >> 7) & 0x7f] |
| 394 | | comp_maskl[7][t1 & 0x7f]; | 410 | | comp_maskl[7][t1 & 0x7f]; |
| 395 | 411 | ||
| 396 | g_de_keysr[15 - round] = /* XXX global */ | 412 | de_keysr[15 - round] = |
| 397 | en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] | 413 | en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] |
| 398 | | comp_maskr[1][(t0 >> 14) & 0x7f] | 414 | | comp_maskr[1][(t0 >> 14) & 0x7f] |
| 399 | | comp_maskr[2][(t0 >> 7) & 0x7f] | 415 | | comp_maskr[2][(t0 >> 7) & 0x7f] |
| @@ -407,14 +423,8 @@ _des_do_setkey(const char *key, u_int32_t *en_keysl, u_int32_t *en_keysr) | |||
| 407 | } | 423 | } |
| 408 | 424 | ||
| 409 | int | 425 | int |
| 410 | des_setkey(const char *key) | ||
| 411 | { | ||
| 412 | return _des_do_setkey(key, g_en_keysl, g_en_keysr); | ||
| 413 | } | ||
| 414 | |||
| 415 | int | ||
| 416 | _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, | 426 | _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, |
| 417 | int count, u_int32_t saltbits, u_int32_t *en_keysl, u_int32_t *en_keysr) | 427 | int count, u_int32_t saltbits) |
| 418 | { | 428 | { |
| 419 | /* | 429 | /* |
| 420 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. | 430 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. |
| @@ -433,11 +443,11 @@ _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, | |||
| 433 | kr1 = en_keysr; | 443 | kr1 = en_keysr; |
| 434 | } else { | 444 | } else { |
| 435 | /* | 445 | /* |
| 436 | * Decrypting XXX global | 446 | * Decrypting |
| 437 | */ | 447 | */ |
| 438 | count = -count; | 448 | count = -count; |
| 439 | kl1 = g_de_keysl; | 449 | kl1 = de_keysl; |
| 440 | kr1 = g_de_keysr; | 450 | kr1 = de_keysr; |
| 441 | } | 451 | } |
| 442 | 452 | ||
| 443 | /* | 453 | /* |
| @@ -530,8 +540,7 @@ _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, | |||
| 530 | } | 540 | } |
| 531 | 541 | ||
| 532 | int | 542 | int |
| 533 | _des_do_cipher(const char *in, char *out, int32_t salt, int count, | 543 | des_cipher(const char *in, char *out, int32_t salt, int count) |
| 534 | u_int32_t *en_keysl, u_int32_t *en_keysr) | ||
| 535 | { | 544 | { |
| 536 | u_int32_t l_out, r_out, rawl, rawr, saltbits; | 545 | u_int32_t l_out, r_out, rawl, rawr, saltbits; |
| 537 | u_int32_t x[2]; | 546 | u_int32_t x[2]; |
| @@ -545,7 +554,7 @@ _des_do_cipher(const char *in, char *out, int32_t salt, int count, | |||
| 545 | memcpy(x, in, sizeof x); | 554 | memcpy(x, in, sizeof x); |
| 546 | rawl = ntohl(x[0]); | 555 | rawl = ntohl(x[0]); |
| 547 | rawr = ntohl(x[1]); | 556 | rawr = ntohl(x[1]); |
| 548 | retval = _des_do_des(rawl, rawr, &l_out, &r_out, count, saltbits, en_keysl, en_keysr); | 557 | retval = _des_do_des(rawl, rawr, &l_out, &r_out, count, saltbits); |
| 549 | 558 | ||
| 550 | x[0] = htonl(l_out); | 559 | x[0] = htonl(l_out); |
| 551 | x[1] = htonl(r_out); | 560 | x[1] = htonl(r_out); |
| @@ -553,16 +562,9 @@ _des_do_cipher(const char *in, char *out, int32_t salt, int count, | |||
| 553 | return(retval); | 562 | return(retval); |
| 554 | } | 563 | } |
| 555 | 564 | ||
| 556 | int | ||
| 557 | des_cipher(const char *in, char *out, int32_t salt, int count) | ||
| 558 | { | ||
| 559 | return _des_do_cipher(in, out, salt, count, g_en_keysl, g_en_keysr); | ||
| 560 | } | ||
| 561 | |||
| 562 | static int | 565 | static int |
| 563 | crypt_hashpass(const char *key, const char *setting, char *output) | 566 | crypt_hashpass(const char *key, const char *setting, char *output) |
| 564 | { | 567 | { |
| 565 | u_int32_t en_keysl[16], en_keysr[16]; | ||
| 566 | int i; | 568 | int i; |
| 567 | u_int32_t count, salt, l, r0, r1, saltbits, keybuf[2]; | 569 | u_int32_t count, salt, l, r0, r1, saltbits, keybuf[2]; |
| 568 | u_char *p, *q; | 570 | u_char *p, *q; |
| @@ -579,7 +581,7 @@ crypt_hashpass(const char *key, const char *setting, char *output) | |||
| 579 | if ((*q++ = *key << 1)) | 581 | if ((*q++ = *key << 1)) |
| 580 | key++; | 582 | key++; |
| 581 | } | 583 | } |
| 582 | if (_des_do_setkey((char *)keybuf, en_keysl, en_keysr)) | 584 | if (des_setkey((char *) keybuf)) |
| 583 | return(-1); | 585 | return(-1); |
| 584 | 586 | ||
| 585 | if (*setting == _PASSWORD_EFMT1) { | 587 | if (*setting == _PASSWORD_EFMT1) { |
| @@ -598,7 +600,7 @@ crypt_hashpass(const char *key, const char *setting, char *output) | |||
| 598 | /* | 600 | /* |
| 599 | * Encrypt the key with itself. | 601 | * Encrypt the key with itself. |
| 600 | */ | 602 | */ |
| 601 | if (_des_do_cipher((char *)keybuf, (char *)keybuf, 0, 1, en_keysl, en_keysr)) | 603 | if (des_cipher((char *)keybuf, (char *)keybuf, 0, 1)) |
| 602 | return(-1); | 604 | return(-1); |
| 603 | /* | 605 | /* |
| 604 | * And XOR with the next 8 characters of the key. | 606 | * And XOR with the next 8 characters of the key. |
| @@ -608,7 +610,7 @@ crypt_hashpass(const char *key, const char *setting, char *output) | |||
| 608 | *key) | 610 | *key) |
| 609 | *q++ ^= *key++ << 1; | 611 | *q++ ^= *key++ << 1; |
| 610 | 612 | ||
| 611 | if (_des_do_setkey((char *)keybuf, en_keysl, en_keysr)) | 613 | if (des_setkey((char *) keybuf)) |
| 612 | return(-1); | 614 | return(-1); |
| 613 | } | 615 | } |
| 614 | strlcpy((char *)output, setting, 10); | 616 | strlcpy((char *)output, setting, 10); |
| @@ -648,7 +650,7 @@ crypt_hashpass(const char *key, const char *setting, char *output) | |||
| 648 | /* | 650 | /* |
| 649 | * Do it. | 651 | * Do it. |
| 650 | */ | 652 | */ |
| 651 | if (_des_do_des(0, 0, &r0, &r1, count, saltbits, en_keysl, en_keysr)) | 653 | if (_des_do_des(0, 0, &r0, &r1, count, saltbits)) |
| 652 | return(-1); | 654 | return(-1); |
| 653 | /* | 655 | /* |
| 654 | * Now encode the result... | 656 | * Now encode the result... |
