diff options
-rw-r--r-- | libbb/pw_encrypt_des.c | 29 | ||||
-rwxr-xr-x | testsuite/cryptpw.tests | 14 |
2 files changed, 28 insertions, 15 deletions
diff --git a/libbb/pw_encrypt_des.c b/libbb/pw_encrypt_des.c index dcd3521e2..fe8237cfe 100644 --- a/libbb/pw_encrypt_des.c +++ b/libbb/pw_encrypt_des.c | |||
@@ -363,7 +363,7 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx) | |||
363 | old_rawkey0 = old_rawkey1 = 0; | 363 | old_rawkey0 = old_rawkey1 = 0; |
364 | old_salt = 0; | 364 | old_salt = 0; |
365 | #endif | 365 | #endif |
366 | saltbits = 0; | 366 | //saltbits = 0; /* not needed: we call setup_salt() before do_des() */ |
367 | bits28 = bits32 + 4; | 367 | bits28 = bits32 + 4; |
368 | bits24 = bits28 + 4; | 368 | bits24 = bits28 + 4; |
369 | 369 | ||
@@ -481,12 +481,11 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx) | |||
481 | return ctx; | 481 | return ctx; |
482 | } | 482 | } |
483 | 483 | ||
484 | 484 | /* Accepts 24-bit salt at max */ | |
485 | static void | 485 | static void |
486 | setup_salt(struct des_ctx *ctx, uint32_t salt) | 486 | setup_salt(struct des_ctx *ctx, uint32_t salt) |
487 | { | 487 | { |
488 | uint32_t obit, saltbit; | 488 | uint32_t invbits; |
489 | int i; | ||
490 | 489 | ||
491 | #if USE_REPETITIVE_SPEEDUP | 490 | #if USE_REPETITIVE_SPEEDUP |
492 | if (salt == old_salt) | 491 | if (salt == old_salt) |
@@ -494,15 +493,15 @@ setup_salt(struct des_ctx *ctx, uint32_t salt) | |||
494 | old_salt = salt; | 493 | old_salt = salt; |
495 | #endif | 494 | #endif |
496 | 495 | ||
497 | saltbits = 0; | 496 | invbits = 0; |
498 | saltbit = 1; | 497 | |
499 | obit = 0x800000; | 498 | salt |= (1 << 24); |
500 | for (i = 0; i < 24; i++) { | 499 | do { |
501 | if (salt & saltbit) | 500 | invbits = (invbits << 1) + (salt & 1); |
502 | saltbits |= obit; | 501 | salt >>= 1; |
503 | saltbit <<= 1; | 502 | } while (salt != 1); |
504 | obit >>= 1; | 503 | |
505 | } | 504 | saltbits = invbits; |
506 | } | 505 | } |
507 | 506 | ||
508 | static void | 507 | static void |
@@ -736,14 +735,14 @@ des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE], | |||
736 | des_setkey(ctx, (char *)keybuf); | 735 | des_setkey(ctx, (char *)keybuf); |
737 | 736 | ||
738 | /* | 737 | /* |
739 | * salt_str - 2 bytes of salt | 738 | * salt_str - 2 chars of salt (converted to 12 bits) |
740 | * key - up to 8 characters | 739 | * key - up to 8 characters |
741 | */ | 740 | */ |
742 | output[0] = salt_str[0]; | 741 | output[0] = salt_str[0]; |
743 | output[1] = salt_str[1]; | 742 | output[1] = salt_str[1]; |
744 | salt = (ascii_to_bin(salt_str[1]) << 6) | 743 | salt = (ascii_to_bin(salt_str[1]) << 6) |
745 | | ascii_to_bin(salt_str[0]); | 744 | | ascii_to_bin(salt_str[0]); |
746 | setup_salt(ctx, salt); | 745 | setup_salt(ctx, salt); /* set ctx->saltbits for do_des() */ |
747 | 746 | ||
748 | /* Do it. */ | 747 | /* Do it. */ |
749 | do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */); | 748 | do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */); |
diff --git a/testsuite/cryptpw.tests b/testsuite/cryptpw.tests index 8ec476c9f..0dd91fe15 100755 --- a/testsuite/cryptpw.tests +++ b/testsuite/cryptpw.tests | |||
@@ -7,6 +7,20 @@ | |||
7 | 7 | ||
8 | # testing "description" "command" "result" "infile" "stdin" | 8 | # testing "description" "command" "result" "infile" "stdin" |
9 | 9 | ||
10 | #optional USE_BB_CRYPT | ||
11 | testing "cryptpw des 12" \ | ||
12 | "cryptpw -m des QWErty '123456789012345678901234567890'" \ | ||
13 | '12MnB3PqfVbMA\n' "" "" | ||
14 | |||
15 | testing "cryptpw des 55" \ | ||
16 | "cryptpw -m des QWErty 55" \ | ||
17 | '55tgFLtkT1Y72\n' "" "" | ||
18 | |||
19 | testing "cryptpw des zz" \ | ||
20 | "cryptpw -m des QWErty zz" \ | ||
21 | 'zzIZaaXWOkxVk\n' "" "" | ||
22 | #SKIP= | ||
23 | |||
10 | optional USE_BB_CRYPT_SHA | 24 | optional USE_BB_CRYPT_SHA |
11 | testing "cryptpw sha256" \ | 25 | testing "cryptpw sha256" \ |
12 | "cryptpw -m sha256 QWErty '123456789012345678901234567890'" \ | 26 | "cryptpw -m sha256 QWErty '123456789012345678901234567890'" \ |