diff options
author | tedu <> | 2014-11-25 03:04:22 +0000 |
---|---|---|
committer | tedu <> | 2014-11-25 03:04:22 +0000 |
commit | 7635c59d42cf0e911e47961db9474ca293af1214 (patch) | |
tree | dbbd419000d500db3c8f015396241ce6cb6b7833 /src/lib/libc/crypt | |
parent | bea5237492c48b30ca9a1fc350d30cadc1260e7d (diff) | |
download | openbsd-7635c59d42cf0e911e47961db9474ca293af1214.tar.gz openbsd-7635c59d42cf0e911e47961db9474ca293af1214.tar.bz2 openbsd-7635c59d42cf0e911e47961db9474ca293af1214.zip |
push some global data down into functions to make this threadlier.
only doing what's needed for crypt_hashpass. sigh.
Diffstat (limited to 'src/lib/libc/crypt')
-rw-r--r-- | src/lib/libc/crypt/crypt.c | 64 |
1 files changed, 31 insertions, 33 deletions
diff --git a/src/lib/libc/crypt/crypt.c b/src/lib/libc/crypt/crypt.c index 157784527b..c74a6784bb 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.22 2014/05/17 13:27:55 tedu Exp $ */ | 1 | /* $OpenBSD: crypt.c,v 1.23 2014/11/25 03:04: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 en_keysl[16], en_keysr[16]; | 165 | static u_int32_t g_en_keysl[16], g_en_keysr[16]; |
166 | static u_int32_t de_keysl[16], de_keysr[16]; | 166 | static u_int32_t g_de_keysl[16], g_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,7 +171,6 @@ 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; | ||
175 | 174 | ||
176 | static u_char ascii64[] = | 175 | static u_char ascii64[] = |
177 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | 176 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
@@ -202,7 +201,6 @@ _des_init(void) | |||
202 | int i, j, b, k, inbit, obit; | 201 | int i, j, b, k, inbit, obit; |
203 | u_int32_t *p, *il, *ir, *fl, *fr; | 202 | u_int32_t *p, *il, *ir, *fl, *fr; |
204 | 203 | ||
205 | old_rawkey0 = old_rawkey1 = 0; | ||
206 | bits24 = (bits28 = _des_bits32 + 4) + 4; | 204 | bits24 = (bits28 = _des_bits32 + 4) + 4; |
207 | 205 | ||
208 | /* | 206 | /* |
@@ -342,8 +340,8 @@ _des_setup_salt(int32_t salt) | |||
342 | return saltbits; | 340 | return saltbits; |
343 | } | 341 | } |
344 | 342 | ||
345 | int | 343 | static int |
346 | des_setkey(const char *key) | 344 | _des_do_setkey(const char *key, u_int32_t *en_keysl, u_int32_t *en_keysr) |
347 | { | 345 | { |
348 | u_int32_t k0, k1, rawkey0, rawkey1; | 346 | u_int32_t k0, k1, rawkey0, rawkey1; |
349 | int shifts, round; | 347 | int shifts, round; |
@@ -354,20 +352,6 @@ des_setkey(const char *key) | |||
354 | rawkey0 = ntohl(*(u_int32_t *) key); | 352 | rawkey0 = ntohl(*(u_int32_t *) key); |
355 | rawkey1 = ntohl(*(u_int32_t *) (key + 4)); | 353 | rawkey1 = ntohl(*(u_int32_t *) (key + 4)); |
356 | 354 | ||
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 | |||
371 | /* | 355 | /* |
372 | * Do key permutation and split into two 28-bit subkeys. | 356 | * Do key permutation and split into two 28-bit subkeys. |
373 | */ | 357 | */ |
@@ -399,7 +383,7 @@ des_setkey(const char *key) | |||
399 | t0 = (k0 << shifts) | (k0 >> (28 - shifts)); | 383 | t0 = (k0 << shifts) | (k0 >> (28 - shifts)); |
400 | t1 = (k1 << shifts) | (k1 >> (28 - shifts)); | 384 | t1 = (k1 << shifts) | (k1 >> (28 - shifts)); |
401 | 385 | ||
402 | de_keysl[15 - round] = | 386 | g_de_keysl[15 - round] = /* XXX global */ |
403 | en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] | 387 | en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] |
404 | | comp_maskl[1][(t0 >> 14) & 0x7f] | 388 | | comp_maskl[1][(t0 >> 14) & 0x7f] |
405 | | comp_maskl[2][(t0 >> 7) & 0x7f] | 389 | | comp_maskl[2][(t0 >> 7) & 0x7f] |
@@ -409,7 +393,7 @@ des_setkey(const char *key) | |||
409 | | comp_maskl[6][(t1 >> 7) & 0x7f] | 393 | | comp_maskl[6][(t1 >> 7) & 0x7f] |
410 | | comp_maskl[7][t1 & 0x7f]; | 394 | | comp_maskl[7][t1 & 0x7f]; |
411 | 395 | ||
412 | de_keysr[15 - round] = | 396 | g_de_keysr[15 - round] = /* XXX global */ |
413 | en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] | 397 | en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] |
414 | | comp_maskr[1][(t0 >> 14) & 0x7f] | 398 | | comp_maskr[1][(t0 >> 14) & 0x7f] |
415 | | comp_maskr[2][(t0 >> 7) & 0x7f] | 399 | | comp_maskr[2][(t0 >> 7) & 0x7f] |
@@ -423,8 +407,14 @@ des_setkey(const char *key) | |||
423 | } | 407 | } |
424 | 408 | ||
425 | int | 409 | int |
410 | des_setkey(const char *key) | ||
411 | { | ||
412 | return _des_do_setkey(key, g_en_keysl, g_en_keysr); | ||
413 | } | ||
414 | |||
415 | int | ||
426 | _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, | 416 | _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, |
427 | int count, u_int32_t saltbits) | 417 | int count, u_int32_t saltbits, u_int32_t *en_keysl, u_int32_t *en_keysr) |
428 | { | 418 | { |
429 | /* | 419 | /* |
430 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. | 420 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. |
@@ -443,11 +433,11 @@ _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, | |||
443 | kr1 = en_keysr; | 433 | kr1 = en_keysr; |
444 | } else { | 434 | } else { |
445 | /* | 435 | /* |
446 | * Decrypting | 436 | * Decrypting XXX global |
447 | */ | 437 | */ |
448 | count = -count; | 438 | count = -count; |
449 | kl1 = de_keysl; | 439 | kl1 = g_de_keysl; |
450 | kr1 = de_keysr; | 440 | kr1 = g_de_keysr; |
451 | } | 441 | } |
452 | 442 | ||
453 | /* | 443 | /* |
@@ -540,7 +530,8 @@ _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, | |||
540 | } | 530 | } |
541 | 531 | ||
542 | int | 532 | int |
543 | des_cipher(const char *in, char *out, int32_t salt, int count) | 533 | _des_do_cipher(const char *in, char *out, int32_t salt, int count, |
534 | u_int32_t *en_keysl, u_int32_t *en_keysr) | ||
544 | { | 535 | { |
545 | u_int32_t l_out, r_out, rawl, rawr, saltbits; | 536 | u_int32_t l_out, r_out, rawl, rawr, saltbits; |
546 | u_int32_t x[2]; | 537 | u_int32_t x[2]; |
@@ -554,7 +545,7 @@ des_cipher(const char *in, char *out, int32_t salt, int count) | |||
554 | memcpy(x, in, sizeof x); | 545 | memcpy(x, in, sizeof x); |
555 | rawl = ntohl(x[0]); | 546 | rawl = ntohl(x[0]); |
556 | rawr = ntohl(x[1]); | 547 | rawr = ntohl(x[1]); |
557 | retval = _des_do_des(rawl, rawr, &l_out, &r_out, count, saltbits); | 548 | retval = _des_do_des(rawl, rawr, &l_out, &r_out, count, saltbits, en_keysl, en_keysr); |
558 | 549 | ||
559 | x[0] = htonl(l_out); | 550 | x[0] = htonl(l_out); |
560 | x[1] = htonl(r_out); | 551 | x[1] = htonl(r_out); |
@@ -562,9 +553,16 @@ des_cipher(const char *in, char *out, int32_t salt, int count) | |||
562 | return(retval); | 553 | return(retval); |
563 | } | 554 | } |
564 | 555 | ||
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 | |||
565 | static int | 562 | static int |
566 | crypt_hashpass(const char *key, const char *setting, char *output) | 563 | crypt_hashpass(const char *key, const char *setting, char *output) |
567 | { | 564 | { |
565 | u_int32_t en_keysl[16], en_keysr[16]; | ||
568 | int i; | 566 | int i; |
569 | u_int32_t count, salt, l, r0, r1, saltbits, keybuf[2]; | 567 | u_int32_t count, salt, l, r0, r1, saltbits, keybuf[2]; |
570 | u_char *p, *q; | 568 | u_char *p, *q; |
@@ -581,7 +579,7 @@ crypt_hashpass(const char *key, const char *setting, char *output) | |||
581 | if ((*q++ = *key << 1)) | 579 | if ((*q++ = *key << 1)) |
582 | key++; | 580 | key++; |
583 | } | 581 | } |
584 | if (des_setkey((char *) keybuf)) | 582 | if (_des_do_setkey((char *)keybuf, en_keysl, en_keysr)) |
585 | return(-1); | 583 | return(-1); |
586 | 584 | ||
587 | if (*setting == _PASSWORD_EFMT1) { | 585 | if (*setting == _PASSWORD_EFMT1) { |
@@ -600,7 +598,7 @@ crypt_hashpass(const char *key, const char *setting, char *output) | |||
600 | /* | 598 | /* |
601 | * Encrypt the key with itself. | 599 | * Encrypt the key with itself. |
602 | */ | 600 | */ |
603 | if (des_cipher((char *)keybuf, (char *)keybuf, 0, 1)) | 601 | if (_des_do_cipher((char *)keybuf, (char *)keybuf, 0, 1, en_keysl, en_keysr)) |
604 | return(-1); | 602 | return(-1); |
605 | /* | 603 | /* |
606 | * And XOR with the next 8 characters of the key. | 604 | * And XOR with the next 8 characters of the key. |
@@ -610,7 +608,7 @@ crypt_hashpass(const char *key, const char *setting, char *output) | |||
610 | *key) | 608 | *key) |
611 | *q++ ^= *key++ << 1; | 609 | *q++ ^= *key++ << 1; |
612 | 610 | ||
613 | if (des_setkey((char *) keybuf)) | 611 | if (_des_do_setkey((char *)keybuf, en_keysl, en_keysr)) |
614 | return(-1); | 612 | return(-1); |
615 | } | 613 | } |
616 | strlcpy((char *)output, setting, 10); | 614 | strlcpy((char *)output, setting, 10); |
@@ -650,7 +648,7 @@ crypt_hashpass(const char *key, const char *setting, char *output) | |||
650 | /* | 648 | /* |
651 | * Do it. | 649 | * Do it. |
652 | */ | 650 | */ |
653 | if (_des_do_des(0, 0, &r0, &r1, count, saltbits)) | 651 | if (_des_do_des(0, 0, &r0, &r1, count, saltbits, en_keysl, en_keysr)) |
654 | return(-1); | 652 | return(-1); |
655 | /* | 653 | /* |
656 | * Now encode the result... | 654 | * Now encode the result... |