| @@ 655-706 (lines=52) @@ | ||
| 652 | * |
|
| 653 | * @return boolean |
|
| 654 | */ |
|
| 655 | private function expandKey128() |
|
| 656 | { |
|
| 657 | // clear the xkey, we're creating a new one |
|
| 658 | $this->xkey = ""; |
|
| 659 | $max = 0; |
|
| 660 | ||
| 661 | // the number of rounds we make depends on the block size of the text |
|
| 662 | // used during encryption/decryption |
|
| 663 | if ($this->blockSize() == 16) { |
|
| 664 | $max = 44; |
|
| 665 | } |
|
| 666 | if ($this->blockSize() == 24) { |
|
| 667 | $max = 78; |
|
| 668 | } |
|
| 669 | if ($this->blockSize() == 32) { |
|
| 670 | $max = 120; |
|
| 671 | } |
|
| 672 | ||
| 673 | // 16 byte key expands to 176 bytes |
|
| 674 | for ($i = 0; $i < $max; ++$i) |
|
| 675 | { |
|
| 676 | if ($i >= 0 && $i <= 3) { |
|
| 677 | $this->xkey .= $this->k($i * 4); |
|
| 678 | } else if (($i % 4) == 0) |
|
| 679 | { |
|
| 680 | // rotate the 4 bytes |
|
| 681 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
| 682 | ||
| 683 | // apply the sbox |
|
| 684 | $this->subWord($subword); |
|
| 685 | ||
| 686 | // return 4 byte value based on self::$_rcon table |
|
| 687 | //$rcon = $this->rcon(($i / 4) - 1); |
|
| 688 | $rcon = $this->rcon(($i / 4)); |
|
| 689 | ||
| 690 | // grab 4 bytes from $this->extended_key |
|
| 691 | $ek = $this->ek(($i - 4) * 4); |
|
| 692 | ||
| 693 | $h1 = parent::str2Hex($subword); |
|
| 694 | $h2 = parent::dec2Hex($rcon); |
|
| 695 | $h3 = parent::str2Hex($ek); |
|
| 696 | $res = parent::xorHex($h1, $h2, $h3); |
|
| 697 | $this->xkey .= parent::hex2Str($res); |
|
| 698 | } else |
|
| 699 | { |
|
| 700 | $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
| 701 | $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
| 702 | $res = parent::xorHex($h1, $h2); |
|
| 703 | $this->xkey .= parent::hex2Str($res); |
|
| 704 | } |
|
| 705 | } |
|
| 706 | ||
| 707 | return true; |
|
| 708 | } |
|
| 709 | ||
| @@ 717-768 (lines=52) @@ | ||
| 714 | * |
|
| 715 | * @return boolean |
|
| 716 | */ |
|
| 717 | private function expandKey192() |
|
| 718 | { |
|
| 719 | // clear the xkey, we're creating a new one |
|
| 720 | $this->xkey = ""; |
|
| 721 | $max = 0; |
|
| 722 | ||
| 723 | // the number of rounds we make depends on the block size of the text |
|
| 724 | // used during encryption/decryption |
|
| 725 | if ($this->blockSize() == 16) { |
|
| 726 | $max = 52; |
|
| 727 | } |
|
| 728 | if ($this->blockSize() == 24) { |
|
| 729 | $max = 78; |
|
| 730 | } |
|
| 731 | if ($this->blockSize() == 32) { |
|
| 732 | $max = 120; |
|
| 733 | } |
|
| 734 | ||
| 735 | // 24 byte key expands to 208 bytes |
|
| 736 | for ($i = 0; $i < $max; ++$i) |
|
| 737 | { |
|
| 738 | if ($i >= 0 && $i <= 5) { |
|
| 739 | $this->xkey .= $this->k($i * 4); |
|
| 740 | } else if (($i % 6) == 0) |
|
| 741 | { |
|
| 742 | // rotate the 4 bytes |
|
| 743 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
| 744 | ||
| 745 | // apply the sbox |
|
| 746 | $this->subWord($subword); |
|
| 747 | ||
| 748 | // return 4 byte value based on self::$_rcon table |
|
| 749 | //$rcon = $this->rcon(($i / 6) - 1); |
|
| 750 | $rcon = $this->rcon(($i / 6)); |
|
| 751 | ||
| 752 | // grab 4 bytes from $this->extended_key |
|
| 753 | $ek = $this->ek(($i - 6) * 4); |
|
| 754 | ||
| 755 | $h1 = parent::str2Hex($subword); |
|
| 756 | $h2 = parent::dec2Hex($rcon); |
|
| 757 | $h3 = parent::str2Hex($ek); |
|
| 758 | $res = parent::xorHex($h1, $h2, $h3); |
|
| 759 | $this->xkey .= parent::hex2Str($res); |
|
| 760 | } else |
|
| 761 | { |
|
| 762 | $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
| 763 | $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
| 764 | $res = parent::xorHex($h1, $h2); |
|
| 765 | $this->xkey .= parent::hex2Str($res); |
|
| 766 | } |
|
| 767 | } |
|
| 768 | ||
| 769 | return true; |
|
| 770 | } |
|
| 771 | ||