OLD | NEW |
1 /* | 1 /* |
2 * des.c | 2 * des.c |
3 * | 3 * |
4 * core source file for DES-150 library | 4 * core source file for DES-150 library |
5 * Make key schedule from DES key. | 5 * Make key schedule from DES key. |
6 * Encrypt/Decrypt one 8-byte block. | 6 * Encrypt/Decrypt one 8-byte block. |
7 * | 7 * |
8 * This Source Code Form is subject to the terms of the Mozilla Public | 8 * This Source Code Form is subject to the terms of the Mozilla Public |
9 * License, v. 2.0. If a copy of the MPL was not distributed with this | 9 * License, v. 2.0. If a copy of the MPL was not distributed with this |
10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
11 | 11 |
12 #include "des.h" | 12 #include "des.h" |
13 #include <stddef.h> /* for ptrdiff_t */ | 13 #include <stddef.h> /* for ptrdiff_t */ |
14 /* #define USE_INDEXING 1 */ | 14 /* #define USE_INDEXING 1 */ |
15 | 15 |
| 16 /* Some processors automatically fix up unaligned memory access, so they can |
| 17 * read or write a HALF (4 bytes) at a time whether the address is 4-byte |
| 18 * aligned or not. */ |
| 19 #if defined(NSS_X86_OR_X64) |
| 20 #define HAVE_UNALIGNED_ACCESS 1 |
| 21 #endif |
| 22 |
16 /* | 23 /* |
17 * The tables below are the 8 sbox functions, with the 6-bit input permutation | 24 * The tables below are the 8 sbox functions, with the 6-bit input permutation |
18 * and the 32-bit output permutation pre-computed. | 25 * and the 32-bit output permutation pre-computed. |
19 * They are shifted circularly to the left 3 bits, which removes 2 shifts | 26 * They are shifted circularly to the left 3 bits, which removes 2 shifts |
20 * and an or from each round by reducing the number of sboxes whose | 27 * and an or from each round by reducing the number of sboxes whose |
21 * indices cross word broundaries from 2 to 1. | 28 * indices cross word broundaries from 2 to 1. |
22 */ | 29 */ |
23 | 30 |
24 static const HALF SP[8][64] = { | 31 static const HALF SP[8][64] = { |
25 /* Box S1 */ { | 32 /* Box S1 */ { |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 | 421 |
415 void | 422 void |
416 DES_MakeSchedule( HALF * ks, const BYTE * key, DESDirection direction) | 423 DES_MakeSchedule( HALF * ks, const BYTE * key, DESDirection direction) |
417 { | 424 { |
418 register HALF left, right; | 425 register HALF left, right; |
419 register HALF c0, d0; | 426 register HALF c0, d0; |
420 register HALF temp; | 427 register HALF temp; |
421 int delta; | 428 int delta; |
422 unsigned int ls; | 429 unsigned int ls; |
423 | 430 |
424 #if defined(NSS_X86_OR_X64) | 431 #if defined(HAVE_UNALIGNED_ACCESS) |
425 left = HALFPTR(key)[0]; | 432 left = HALFPTR(key)[0]; |
426 right = HALFPTR(key)[1]; | 433 right = HALFPTR(key)[1]; |
| 434 #if defined(IS_LITTLE_ENDIAN) |
427 BYTESWAP(left, temp); | 435 BYTESWAP(left, temp); |
428 BYTESWAP(right, temp); | 436 BYTESWAP(right, temp); |
| 437 #endif |
429 #else | 438 #else |
430 if (((ptrdiff_t)key & 0x03) == 0) { | 439 if (((ptrdiff_t)key & 0x03) == 0) { |
431 left = HALFPTR(key)[0]; | 440 left = HALFPTR(key)[0]; |
432 right = HALFPTR(key)[1]; | 441 right = HALFPTR(key)[1]; |
433 #if defined(IS_LITTLE_ENDIAN) | 442 #if defined(IS_LITTLE_ENDIAN) |
434 BYTESWAP(left, temp); | 443 BYTESWAP(left, temp); |
435 BYTESWAP(right, temp); | 444 BYTESWAP(right, temp); |
436 #endif | 445 #endif |
437 } else { | 446 } else { |
438 left = ((HALF)key[0] << 24) | ((HALF)key[1] << 16) | | 447 left = ((HALF)key[0] << 24) | ((HALF)key[1] << 16) | |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 left ^= temp << 16; \ | 574 left ^= temp << 16; \ |
566 right ^= temp = ((left >> 4) ^ right) & 0x0f0f0f0f; \ | 575 right ^= temp = ((left >> 4) ^ right) & 0x0f0f0f0f; \ |
567 left ^= temp << 4; | 576 left ^= temp << 4; |
568 | 577 |
569 void | 578 void |
570 DES_Do1Block(HALF * ks, const BYTE * inbuf, BYTE * outbuf) | 579 DES_Do1Block(HALF * ks, const BYTE * inbuf, BYTE * outbuf) |
571 { | 580 { |
572 register HALF left, right; | 581 register HALF left, right; |
573 register HALF temp; | 582 register HALF temp; |
574 | 583 |
575 #if defined(NSS_X86_OR_X64) | 584 #if defined(HAVE_UNALIGNED_ACCESS) |
576 left = HALFPTR(inbuf)[0]; | 585 left = HALFPTR(inbuf)[0]; |
577 right = HALFPTR(inbuf)[1]; | 586 right = HALFPTR(inbuf)[1]; |
| 587 #if defined(IS_LITTLE_ENDIAN) |
578 BYTESWAP(left, temp); | 588 BYTESWAP(left, temp); |
579 BYTESWAP(right, temp); | 589 BYTESWAP(right, temp); |
| 590 #endif |
580 #else | 591 #else |
581 if (((ptrdiff_t)inbuf & 0x03) == 0) { | 592 if (((ptrdiff_t)inbuf & 0x03) == 0) { |
582 left = HALFPTR(inbuf)[0]; | 593 left = HALFPTR(inbuf)[0]; |
583 right = HALFPTR(inbuf)[1]; | 594 right = HALFPTR(inbuf)[1]; |
584 #if defined(IS_LITTLE_ENDIAN) | 595 #if defined(IS_LITTLE_ENDIAN) |
585 BYTESWAP(left, temp); | 596 BYTESWAP(left, temp); |
586 BYTESWAP(right, temp); | 597 BYTESWAP(right, temp); |
587 #endif | 598 #endif |
588 } else { | 599 } else { |
589 left = ((HALF)inbuf[0] << 24) | ((HALF)inbuf[1] << 16) | | 600 left = ((HALF)inbuf[0] << 24) | ((HALF)inbuf[1] << 16) | |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 | 647 |
637 /* now shift circularly right 3 bits to undo the shifting done | 648 /* now shift circularly right 3 bits to undo the shifting done |
638 ** above. switch left and right here. | 649 ** above. switch left and right here. |
639 */ | 650 */ |
640 temp = (left >> 3) | (left << 29); | 651 temp = (left >> 3) | (left << 29); |
641 left = (right >> 3) | (right << 29); | 652 left = (right >> 3) | (right << 29); |
642 right = temp; | 653 right = temp; |
643 | 654 |
644 FP(left, right, temp); | 655 FP(left, right, temp); |
645 | 656 |
646 #if defined(NSS_X86_OR_X64) | 657 #if defined(HAVE_UNALIGNED_ACCESS) |
| 658 #if defined(IS_LITTLE_ENDIAN) |
647 BYTESWAP(left, temp); | 659 BYTESWAP(left, temp); |
648 BYTESWAP(right, temp); | 660 BYTESWAP(right, temp); |
| 661 #endif |
649 HALFPTR(outbuf)[0] = left; | 662 HALFPTR(outbuf)[0] = left; |
650 HALFPTR(outbuf)[1] = right; | 663 HALFPTR(outbuf)[1] = right; |
651 #else | 664 #else |
652 if (((ptrdiff_t)outbuf & 0x03) == 0) { | 665 if (((ptrdiff_t)outbuf & 0x03) == 0) { |
653 #if defined(IS_LITTLE_ENDIAN) | 666 #if defined(IS_LITTLE_ENDIAN) |
654 BYTESWAP(left, temp); | 667 BYTESWAP(left, temp); |
655 BYTESWAP(right, temp); | 668 BYTESWAP(right, temp); |
656 #endif | 669 #endif |
657 HALFPTR(outbuf)[0] = left; | 670 HALFPTR(outbuf)[0] = left; |
658 HALFPTR(outbuf)[1] = right; | 671 HALFPTR(outbuf)[1] = right; |
(...skipping 12 matching lines...) Expand all Loading... |
671 | 684 |
672 } | 685 } |
673 | 686 |
674 /* Ackowledgements: | 687 /* Ackowledgements: |
675 ** Two ideas used in this implementation were shown to me by Dennis Ferguson | 688 ** Two ideas used in this implementation were shown to me by Dennis Ferguson |
676 ** in 1990. He credits them to Richard Outerbridge and Dan Hoey. They were: | 689 ** in 1990. He credits them to Richard Outerbridge and Dan Hoey. They were: |
677 ** 1. The method of computing the Initial and Final permutations. | 690 ** 1. The method of computing the Initial and Final permutations. |
678 ** 2. Circularly rotating the SP tables and the initial values of left and | 691 ** 2. Circularly rotating the SP tables and the initial values of left and |
679 ** right to reduce the number of shifts required during the 16 rounds. | 692 ** right to reduce the number of shifts required during the 16 rounds. |
680 */ | 693 */ |
OLD | NEW |