diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-06-15 08:12:00 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-06-15 08:12:00 +0000 |
| commit | 04087c6bbd247ef5802de3f8bd625fa3643e23e2 (patch) | |
| tree | ced2e67083fca4ea67714988f63f6ad6dd99a07b /libbb | |
| parent | d50dda8c3501af9d593cd11272a15b480864a01c (diff) | |
| download | busybox-w32-04087c6bbd247ef5802de3f8bd625fa3643e23e2.tar.gz busybox-w32-04087c6bbd247ef5802de3f8bd625fa3643e23e2.tar.bz2 busybox-w32-04087c6bbd247ef5802de3f8bd625fa3643e23e2.zip | |
cryptpw: fix "cryptpw -a des -- TEXT" case
libbb/pw_encrypt_des.c: optimize
function old new delta
cryptpw_main 177 157 -20
des_crypt 1682 1512 -170
pw_encrypt 1036 842 -194
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-384) Total: -384 bytes
Run tested.
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/pw_encrypt_des.c | 142 |
1 files changed, 75 insertions, 67 deletions
diff --git a/libbb/pw_encrypt_des.c b/libbb/pw_encrypt_des.c index 66e607955..956a3e679 100644 --- a/libbb/pw_encrypt_des.c +++ b/libbb/pw_encrypt_des.c | |||
| @@ -229,17 +229,20 @@ const_des_init(void) | |||
| 229 | return cctx; | 229 | return cctx; |
| 230 | } | 230 | } |
| 231 | 231 | ||
| 232 | #define WANT_REPETITIVE_SPEEDUP 0 | ||
| 232 | 233 | ||
| 233 | struct des_ctx { | 234 | struct des_ctx { |
| 234 | const struct const_des_ctx *const_ctx; | 235 | const struct const_des_ctx *const_ctx; |
| 235 | uint32_t saltbits; /* referenced 5 times */ | 236 | uint32_t saltbits; /* referenced 5 times */ |
| 237 | #if WANT_REPETITIVE_SPEEDUP | ||
| 236 | uint32_t old_salt; /* 3 times */ | 238 | uint32_t old_salt; /* 3 times */ |
| 237 | uint32_t old_rawkey0, old_rawkey1; /* 3 times each */ | 239 | uint32_t old_rawkey0, old_rawkey1; /* 3 times each */ |
| 240 | #endif | ||
| 238 | uint8_t un_pbox[32]; /* 2 times */ | 241 | uint8_t un_pbox[32]; /* 2 times */ |
| 239 | uint8_t inv_comp_perm[56]; /* 3 times */ | 242 | uint8_t inv_comp_perm[56]; /* 3 times */ |
| 240 | uint8_t inv_key_perm[64]; /* 3 times */ | 243 | uint8_t inv_key_perm[64]; /* 3 times */ |
| 241 | uint32_t en_keysl[16], en_keysr[16]; /* 2 times each */ | 244 | uint32_t en_keysl[16], en_keysr[16]; /* 2 times each */ |
| 242 | uint32_t de_keysl[16], de_keysr[16]; /* 2 times each */ | 245 | // uint32_t de_keysl[16], de_keysr[16]; /* 2 times each */ |
| 243 | uint32_t ip_maskl[8][256], ip_maskr[8][256]; /* 9 times each */ | 246 | uint32_t ip_maskl[8][256], ip_maskr[8][256]; /* 9 times each */ |
| 244 | uint32_t fp_maskl[8][256], fp_maskr[8][256]; /* 9 times each */ | 247 | uint32_t fp_maskl[8][256], fp_maskr[8][256]; /* 9 times each */ |
| 245 | uint32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; /* 9 times */ | 248 | uint32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; /* 9 times */ |
| @@ -257,8 +260,8 @@ struct des_ctx { | |||
| 257 | #define inv_key_perm (D.inv_key_perm ) | 260 | #define inv_key_perm (D.inv_key_perm ) |
| 258 | #define en_keysl (D.en_keysl ) | 261 | #define en_keysl (D.en_keysl ) |
| 259 | #define en_keysr (D.en_keysr ) | 262 | #define en_keysr (D.en_keysr ) |
| 260 | #define de_keysl (D.de_keysl ) | 263 | //#define de_keysl (D.de_keysl ) |
| 261 | #define de_keysr (D.de_keysr ) | 264 | //#define de_keysr (D.de_keysr ) |
| 262 | #define ip_maskl (D.ip_maskl ) | 265 | #define ip_maskl (D.ip_maskl ) |
| 263 | #define ip_maskr (D.ip_maskr ) | 266 | #define ip_maskr (D.ip_maskr ) |
| 264 | #define fp_maskl (D.fp_maskl ) | 267 | #define fp_maskl (D.fp_maskl ) |
| @@ -273,16 +276,19 @@ static struct des_ctx* | |||
| 273 | des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx) | 276 | des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx) |
| 274 | { | 277 | { |
| 275 | int i, j, b, k, inbit, obit; | 278 | int i, j, b, k, inbit, obit; |
| 276 | uint32_t *p, *il, *ir, *fl, *fr; | 279 | uint32_t p; |
| 280 | uint32_t il, ir, fl, fr; | ||
| 277 | const uint32_t *bits28, *bits24; | 281 | const uint32_t *bits28, *bits24; |
| 278 | 282 | ||
| 279 | if (!ctx) | 283 | if (!ctx) |
| 280 | ctx = xmalloc(sizeof(*ctx)); | 284 | ctx = xmalloc(sizeof(*ctx)); |
| 281 | const_ctx = cctx; | 285 | const_ctx = cctx; |
| 282 | 286 | ||
| 283 | old_rawkey0 = old_rawkey1 = 0L; | 287 | #if WANT_REPETITIVE_SPEEDUP |
| 284 | saltbits = 0L; | 288 | old_rawkey0 = old_rawkey1 = 0; |
| 285 | old_salt = 0L; | 289 | old_salt = 0; |
| 290 | #endif | ||
| 291 | saltbits = 0; | ||
| 286 | bits28 = bits32 + 4; | 292 | bits28 = bits32 + 4; |
| 287 | bits24 = bits28 + 4; | 293 | bits24 = bits28 + 4; |
| 288 | 294 | ||
| @@ -315,35 +321,33 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx) | |||
| 315 | */ | 321 | */ |
| 316 | for (k = 0; k < 8; k++) { | 322 | for (k = 0; k < 8; k++) { |
| 317 | for (i = 0; i < 256; i++) { | 323 | for (i = 0; i < 256; i++) { |
| 318 | il = &ip_maskl[k][i]; | 324 | il = 0; |
| 319 | ir = &ip_maskr[k][i]; | 325 | ir = 0; |
| 320 | fl = &fp_maskl[k][i]; | 326 | fl = 0; |
| 321 | fr = &fp_maskr[k][i]; | 327 | fr = 0; |
| 322 | *il = 0; | ||
| 323 | *ir = 0; | ||
| 324 | *fl = 0; | ||
| 325 | *fr = 0; | ||
| 326 | for (j = 0; j < 8; j++) { | 328 | for (j = 0; j < 8; j++) { |
| 327 | inbit = 8 * k + j; | 329 | inbit = 8 * k + j; |
| 328 | if (i & bits8[j]) { | 330 | if (i & bits8[j]) { |
| 329 | obit = init_perm[inbit]; | 331 | obit = init_perm[inbit]; |
| 330 | if (obit < 32) | 332 | if (obit < 32) |
| 331 | *il |= bits32[obit]; | 333 | il |= bits32[obit]; |
| 332 | else | 334 | else |
| 333 | *ir |= bits32[obit - 32]; | 335 | ir |= bits32[obit - 32]; |
| 334 | obit = final_perm[inbit]; | 336 | obit = final_perm[inbit]; |
| 335 | if (obit < 32) | 337 | if (obit < 32) |
| 336 | *fl |= bits32[obit]; | 338 | fl |= bits32[obit]; |
| 337 | else | 339 | else |
| 338 | *fr |= bits32[obit - 32]; | 340 | fr |= bits32[obit - 32]; |
| 339 | } | 341 | } |
| 340 | } | 342 | } |
| 343 | ip_maskl[k][i] = il; | ||
| 344 | ip_maskr[k][i] = ir; | ||
| 345 | fp_maskl[k][i] = fl; | ||
| 346 | fp_maskr[k][i] = fr; | ||
| 341 | } | 347 | } |
| 342 | for (i = 0; i < 128; i++) { | 348 | for (i = 0; i < 128; i++) { |
| 343 | il = &key_perm_maskl[k][i]; | 349 | il = 0; |
| 344 | ir = &key_perm_maskr[k][i]; | 350 | ir = 0; |
| 345 | *il = 0; | ||
| 346 | *ir = 0; | ||
| 347 | for (j = 0; j < 7; j++) { | 351 | for (j = 0; j < 7; j++) { |
| 348 | inbit = 8 * k + j; | 352 | inbit = 8 * k + j; |
| 349 | if (i & bits8[j + 1]) { | 353 | if (i & bits8[j + 1]) { |
| @@ -351,15 +355,15 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx) | |||
| 351 | if (obit == 255) | 355 | if (obit == 255) |
| 352 | continue; | 356 | continue; |
| 353 | if (obit < 28) | 357 | if (obit < 28) |
| 354 | *il |= bits28[obit]; | 358 | il |= bits28[obit]; |
| 355 | else | 359 | else |
| 356 | *ir |= bits28[obit - 28]; | 360 | ir |= bits28[obit - 28]; |
| 357 | } | 361 | } |
| 358 | } | 362 | } |
| 359 | il = &comp_maskl[k][i]; | 363 | key_perm_maskl[k][i] = il; |
| 360 | ir = &comp_maskr[k][i]; | 364 | key_perm_maskr[k][i] = ir; |
| 361 | *il = 0; | 365 | il = 0; |
| 362 | *ir = 0; | 366 | ir = 0; |
| 363 | for (j = 0; j < 7; j++) { | 367 | for (j = 0; j < 7; j++) { |
| 364 | inbit = 7 * k + j; | 368 | inbit = 7 * k + j; |
| 365 | if (i & bits8[j + 1]) { | 369 | if (i & bits8[j + 1]) { |
| @@ -367,11 +371,13 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx) | |||
| 367 | if (obit == 255) | 371 | if (obit == 255) |
| 368 | continue; | 372 | continue; |
| 369 | if (obit < 24) | 373 | if (obit < 24) |
| 370 | *il |= bits24[obit]; | 374 | il |= bits24[obit]; |
| 371 | else | 375 | else |
| 372 | *ir |= bits24[obit - 24]; | 376 | ir |= bits24[obit - 24]; |
| 373 | } | 377 | } |
| 374 | } | 378 | } |
| 379 | comp_maskl[k][i] = il; | ||
| 380 | comp_maskr[k][i] = ir; | ||
| 375 | } | 381 | } |
| 376 | } | 382 | } |
| 377 | 383 | ||
| @@ -384,12 +390,12 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx) | |||
| 384 | 390 | ||
| 385 | for (b = 0; b < 4; b++) { | 391 | for (b = 0; b < 4; b++) { |
| 386 | for (i = 0; i < 256; i++) { | 392 | for (i = 0; i < 256; i++) { |
| 387 | p = &psbox[b][i]; | 393 | p = 0; |
| 388 | *p = 0; | ||
| 389 | for (j = 0; j < 8; j++) { | 394 | for (j = 0; j < 8; j++) { |
| 390 | if (i & bits8[j]) | 395 | if (i & bits8[j]) |
| 391 | *p |= bits32[un_pbox[8 * b + j]]; | 396 | p |= bits32[un_pbox[8 * b + j]]; |
| 392 | } | 397 | } |
| 398 | psbox[b][i] = p; | ||
| 393 | } | 399 | } |
| 394 | } | 400 | } |
| 395 | 401 | ||
| @@ -403,11 +409,13 @@ setup_salt(struct des_ctx *ctx, uint32_t salt) | |||
| 403 | uint32_t obit, saltbit; | 409 | uint32_t obit, saltbit; |
| 404 | int i; | 410 | int i; |
| 405 | 411 | ||
| 412 | #if WANT_REPETITIVE_SPEEDUP | ||
| 406 | if (salt == old_salt) | 413 | if (salt == old_salt) |
| 407 | return; | 414 | return; |
| 408 | old_salt = salt; | 415 | old_salt = salt; |
| 416 | #endif | ||
| 409 | 417 | ||
| 410 | saltbits = 0L; | 418 | saltbits = 0; |
| 411 | saltbit = 1; | 419 | saltbit = 1; |
| 412 | obit = 0x800000; | 420 | obit = 0x800000; |
| 413 | for (i = 0; i < 24; i++) { | 421 | for (i = 0; i < 24; i++) { |
| @@ -427,6 +435,7 @@ des_setkey(struct des_ctx *ctx, const char *key) | |||
| 427 | rawkey0 = ntohl(*(const uint32_t *) key); | 435 | rawkey0 = ntohl(*(const uint32_t *) key); |
| 428 | rawkey1 = ntohl(*(const uint32_t *) (key + 4)); | 436 | rawkey1 = ntohl(*(const uint32_t *) (key + 4)); |
| 429 | 437 | ||
| 438 | #if WANT_REPETITIVE_SPEEDUP | ||
| 430 | if ((rawkey0 | rawkey1) | 439 | if ((rawkey0 | rawkey1) |
| 431 | && rawkey0 == old_rawkey0 | 440 | && rawkey0 == old_rawkey0 |
| 432 | && rawkey1 == old_rawkey1 | 441 | && rawkey1 == old_rawkey1 |
| @@ -441,6 +450,7 @@ des_setkey(struct des_ctx *ctx, const char *key) | |||
| 441 | } | 450 | } |
| 442 | old_rawkey0 = rawkey0; | 451 | old_rawkey0 = rawkey0; |
| 443 | old_rawkey1 = rawkey1; | 452 | old_rawkey1 = rawkey1; |
| 453 | #endif | ||
| 444 | 454 | ||
| 445 | /* | 455 | /* |
| 446 | * Do key permutation and split into two 28-bit subkeys. | 456 | * Do key permutation and split into two 28-bit subkeys. |
| @@ -473,7 +483,7 @@ des_setkey(struct des_ctx *ctx, const char *key) | |||
| 473 | t0 = (k0 << shifts) | (k0 >> (28 - shifts)); | 483 | t0 = (k0 << shifts) | (k0 >> (28 - shifts)); |
| 474 | t1 = (k1 << shifts) | (k1 >> (28 - shifts)); | 484 | t1 = (k1 << shifts) | (k1 >> (28 - shifts)); |
| 475 | 485 | ||
| 476 | de_keysl[15 - round] = | 486 | // de_keysl[15 - round] = |
| 477 | en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] | 487 | en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] |
| 478 | | comp_maskl[1][(t0 >> 14) & 0x7f] | 488 | | comp_maskl[1][(t0 >> 14) & 0x7f] |
| 479 | | comp_maskl[2][(t0 >> 7) & 0x7f] | 489 | | comp_maskl[2][(t0 >> 7) & 0x7f] |
| @@ -483,7 +493,7 @@ des_setkey(struct des_ctx *ctx, const char *key) | |||
| 483 | | comp_maskl[6][(t1 >> 7) & 0x7f] | 493 | | comp_maskl[6][(t1 >> 7) & 0x7f] |
| 484 | | comp_maskl[7][t1 & 0x7f]; | 494 | | comp_maskl[7][t1 & 0x7f]; |
| 485 | 495 | ||
| 486 | de_keysr[15 - round] = | 496 | // de_keysr[15 - round] = |
| 487 | en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] | 497 | en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] |
| 488 | | comp_maskr[1][(t0 >> 14) & 0x7f] | 498 | | comp_maskr[1][(t0 >> 14) & 0x7f] |
| 489 | | comp_maskr[2][(t0 >> 7) & 0x7f] | 499 | | comp_maskr[2][(t0 >> 7) & 0x7f] |
| @@ -497,26 +507,19 @@ des_setkey(struct des_ctx *ctx, const char *key) | |||
| 497 | 507 | ||
| 498 | 508 | ||
| 499 | static void | 509 | static void |
| 500 | do_des(struct des_ctx *ctx, uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint32_t *r_out, int count) | 510 | do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, uint32_t *r_out, int count) |
| 501 | { | 511 | { |
| 502 | const struct const_des_ctx *cctx = const_ctx; | 512 | const struct const_des_ctx *cctx = const_ctx; |
| 503 | /* | 513 | /* |
| 504 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. | 514 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. |
| 505 | */ | 515 | */ |
| 506 | uint32_t l, r, *kl, *kr, *kl1, *kr1; | 516 | uint32_t l, r, *kl, *kr; |
| 507 | uint32_t f = f; /* silence gcc */ | 517 | uint32_t f = f; /* silence gcc */ |
| 508 | uint32_t r48l, r48r; | 518 | uint32_t r48l, r48r; |
| 509 | int round; | 519 | int round; |
| 510 | 520 | ||
| 511 | /* | 521 | /* Do initial permutation (IP). */ |
| 512 | * Encrypting | 522 | #if 0 |
| 513 | */ | ||
| 514 | kl1 = en_keysl; | ||
| 515 | kr1 = en_keysr; | ||
| 516 | |||
| 517 | /* | ||
| 518 | * Do initial permutation (IP). | ||
| 519 | */ | ||
| 520 | l = ip_maskl[0][l_in >> 24] | 523 | l = ip_maskl[0][l_in >> 24] |
| 521 | | ip_maskl[1][(l_in >> 16) & 0xff] | 524 | | ip_maskl[1][(l_in >> 16) & 0xff] |
| 522 | | ip_maskl[2][(l_in >> 8) & 0xff] | 525 | | ip_maskl[2][(l_in >> 8) & 0xff] |
| @@ -533,18 +536,24 @@ do_des(struct des_ctx *ctx, uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint3 | |||
| 533 | | ip_maskr[5][(r_in >> 16) & 0xff] | 536 | | ip_maskr[5][(r_in >> 16) & 0xff] |
| 534 | | ip_maskr[6][(r_in >> 8) & 0xff] | 537 | | ip_maskr[6][(r_in >> 8) & 0xff] |
| 535 | | ip_maskr[7][r_in & 0xff]; | 538 | | ip_maskr[7][r_in & 0xff]; |
| 536 | 539 | #elif 0 /* -65 bytes (using the fact that l_in == r_in == 0) */ | |
| 537 | while (count--) { | 540 | l = r = 0; |
| 538 | /* | 541 | for (round = 0; round < 8; round++) { |
| 539 | * Do each round. | 542 | l |= ip_maskl[round][0]; |
| 540 | */ | 543 | r |= ip_maskr[round][0]; |
| 541 | kl = kl1; | 544 | } |
| 542 | kr = kr1; | 545 | bb_error_msg("l:%x r:%x", l, r); /* reports 0, 0 always! */ |
| 546 | #else /* using the fact that ip_maskX[] is constant (written to by des_init) */ | ||
| 547 | l = r = 0; | ||
| 548 | #endif | ||
| 549 | |||
| 550 | do { | ||
| 551 | /* Do each round. */ | ||
| 552 | kl = en_keysl; | ||
| 553 | kr = en_keysr; | ||
| 543 | round = 16; | 554 | round = 16; |
| 544 | while (round--) { | 555 | do { |
| 545 | /* | 556 | /* Expand R to 48 bits (simulate the E-box). */ |
| 546 | * Expand R to 48 bits (simulate the E-box). | ||
| 547 | */ | ||
| 548 | r48l = ((r & 0x00000001) << 23) | 557 | r48l = ((r & 0x00000001) << 23) |
| 549 | | ((r & 0xf8000000) >> 9) | 558 | | ((r & 0xf8000000) >> 9) |
| 550 | | ((r & 0x1f800000) >> 11) | 559 | | ((r & 0x1f800000) >> 11) |
| @@ -571,16 +580,14 @@ do_des(struct des_ctx *ctx, uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint3 | |||
| 571 | | psbox[1][m_sbox[1][r48l & 0xfff]] | 580 | | psbox[1][m_sbox[1][r48l & 0xfff]] |
| 572 | | psbox[2][m_sbox[2][r48r >> 12]] | 581 | | psbox[2][m_sbox[2][r48r >> 12]] |
| 573 | | psbox[3][m_sbox[3][r48r & 0xfff]]; | 582 | | psbox[3][m_sbox[3][r48r & 0xfff]]; |
| 574 | /* | 583 | /* Now that we've permuted things, complete f(). */ |
| 575 | * Now that we've permuted things, complete f(). | ||
| 576 | */ | ||
| 577 | f ^= l; | 584 | f ^= l; |
| 578 | l = r; | 585 | l = r; |
| 579 | r = f; | 586 | r = f; |
| 580 | } | 587 | } while (--round); |
| 581 | r = l; | 588 | r = l; |
| 582 | l = f; | 589 | l = f; |
| 583 | } | 590 | } while (--count); |
| 584 | /* | 591 | /* |
| 585 | * Do final permutation (inverse of IP). | 592 | * Do final permutation (inverse of IP). |
| 586 | */ | 593 | */ |
| @@ -646,7 +653,7 @@ des_crypt(struct des_ctx *ctx, char output[21], const unsigned char *key, const | |||
| 646 | /* | 653 | /* |
| 647 | * Do it. | 654 | * Do it. |
| 648 | */ | 655 | */ |
| 649 | do_des(ctx, 0L, 0L, &r0, &r1, 25 /* count */); | 656 | do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */); |
| 650 | 657 | ||
| 651 | /* | 658 | /* |
| 652 | * Now encode the result... | 659 | * Now encode the result... |
| @@ -672,6 +679,7 @@ des_crypt(struct des_ctx *ctx, char output[21], const unsigned char *key, const | |||
| 672 | return output; | 679 | return output; |
| 673 | } | 680 | } |
| 674 | 681 | ||
| 682 | #undef WANT_REPETITIVE_SPEEDUP | ||
| 675 | #undef C | 683 | #undef C |
| 676 | #undef init_perm | 684 | #undef init_perm |
| 677 | #undef final_perm | 685 | #undef final_perm |
| @@ -687,8 +695,8 @@ des_crypt(struct des_ctx *ctx, char output[21], const unsigned char *key, const | |||
| 687 | #undef inv_key_perm | 695 | #undef inv_key_perm |
| 688 | #undef en_keysl | 696 | #undef en_keysl |
| 689 | #undef en_keysr | 697 | #undef en_keysr |
| 690 | #undef de_keysl | 698 | //#undef de_keysl |
| 691 | #undef de_keysr | 699 | //#undef de_keysr |
| 692 | #undef ip_maskl | 700 | #undef ip_maskl |
| 693 | #undef ip_maskr | 701 | #undef ip_maskr |
| 694 | #undef fp_maskl | 702 | #undef fp_maskl |
