@@ -13,94 +13,94 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class GHASH |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Fixed R-block. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - const R = "\xE1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
|
| 16 | + /** |
|
| 17 | + * Fixed R-block. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + const R = "\xE1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Hash subkey. |
|
| 25 | - * |
|
| 26 | - * @var string $_subkey |
|
| 27 | - */ |
|
| 28 | - protected $_subkey; |
|
| 23 | + /** |
|
| 24 | + * Hash subkey. |
|
| 25 | + * |
|
| 26 | + * @var string $_subkey |
|
| 27 | + */ |
|
| 28 | + protected $_subkey; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $subkey Hash subkey |
|
| 34 | - */ |
|
| 35 | - public function __construct(string $subkey) |
|
| 36 | - { |
|
| 37 | - if (strlen($subkey) != 16) { |
|
| 38 | - throw new \LengthException("Subkey must be 128 bits."); |
|
| 39 | - } |
|
| 40 | - $this->_subkey = $subkey; |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $subkey Hash subkey |
|
| 34 | + */ |
|
| 35 | + public function __construct(string $subkey) |
|
| 36 | + { |
|
| 37 | + if (strlen($subkey) != 16) { |
|
| 38 | + throw new \LengthException("Subkey must be 128 bits."); |
|
| 39 | + } |
|
| 40 | + $this->_subkey = $subkey; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Compute hash. |
|
| 45 | - * |
|
| 46 | - * @param string $X Input string |
|
| 47 | - * @return string Hash |
|
| 48 | - */ |
|
| 49 | - public function compute(string $X): string |
|
| 50 | - { |
|
| 51 | - $len = strlen($X); |
|
| 52 | - if (0 != $len % 16) { |
|
| 53 | - throw new \UnexpectedValueException( |
|
| 54 | - "Input string must be a multiple of 128 bits."); |
|
| 55 | - } |
|
| 56 | - $Y = GCM::ZB_128; |
|
| 57 | - // number of 128-bit blocks |
|
| 58 | - $m = $len >> 4; |
|
| 59 | - for ($i = 0; $i < $m; ++$i) { |
|
| 60 | - $xi = substr($X, $i << 4, 16); |
|
| 61 | - $Y = $this->_mult($Y ^ $xi, $this->_subkey); |
|
| 62 | - } |
|
| 63 | - return $Y; |
|
| 64 | - } |
|
| 43 | + /** |
|
| 44 | + * Compute hash. |
|
| 45 | + * |
|
| 46 | + * @param string $X Input string |
|
| 47 | + * @return string Hash |
|
| 48 | + */ |
|
| 49 | + public function compute(string $X): string |
|
| 50 | + { |
|
| 51 | + $len = strlen($X); |
|
| 52 | + if (0 != $len % 16) { |
|
| 53 | + throw new \UnexpectedValueException( |
|
| 54 | + "Input string must be a multiple of 128 bits."); |
|
| 55 | + } |
|
| 56 | + $Y = GCM::ZB_128; |
|
| 57 | + // number of 128-bit blocks |
|
| 58 | + $m = $len >> 4; |
|
| 59 | + for ($i = 0; $i < $m; ++$i) { |
|
| 60 | + $xi = substr($X, $i << 4, 16); |
|
| 61 | + $Y = $this->_mult($Y ^ $xi, $this->_subkey); |
|
| 62 | + } |
|
| 63 | + return $Y; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Functor method for <code>compute</code>. |
|
| 68 | - * |
|
| 69 | - * @param string $arg |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public function __invoke(string $arg): string |
|
| 73 | - { |
|
| 74 | - return $this->compute($arg); |
|
| 75 | - } |
|
| 66 | + /** |
|
| 67 | + * Functor method for <code>compute</code>. |
|
| 68 | + * |
|
| 69 | + * @param string $arg |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public function __invoke(string $arg): string |
|
| 73 | + { |
|
| 74 | + return $this->compute($arg); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Apply block multiplication operation. |
|
| 79 | - * |
|
| 80 | - * See NIST SP-800-38D, chapter 6.3 for the details. |
|
| 81 | - * |
|
| 82 | - * @param string $X |
|
| 83 | - * @param string $Y |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - private function _mult(string $X, string $Y): string |
|
| 87 | - { |
|
| 88 | - $x = GCM::strToGMP($X); |
|
| 89 | - $Z = GCM::strToGMP(GCM::ZB_128); |
|
| 90 | - $V = GCM::strToGMP($Y); |
|
| 91 | - $R = GCM::strToGMP(self::R); |
|
| 92 | - for ($i = 0; $i < 128; ++$i) { |
|
| 93 | - // if bit at X[i] is set |
|
| 94 | - if (gmp_testbit($x, 127 - $i)) { |
|
| 95 | - $Z ^= $V; |
|
| 96 | - } |
|
| 97 | - // if LSB(Vi) = 0 |
|
| 98 | - if (!gmp_testbit($V, 0)) { |
|
| 99 | - $V >>= 1; |
|
| 100 | - } else { |
|
| 101 | - $V = ($V >> 1) ^ $R; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - return GCM::gmpToStr($Z, 16); |
|
| 105 | - } |
|
| 77 | + /** |
|
| 78 | + * Apply block multiplication operation. |
|
| 79 | + * |
|
| 80 | + * See NIST SP-800-38D, chapter 6.3 for the details. |
|
| 81 | + * |
|
| 82 | + * @param string $X |
|
| 83 | + * @param string $Y |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + private function _mult(string $X, string $Y): string |
|
| 87 | + { |
|
| 88 | + $x = GCM::strToGMP($X); |
|
| 89 | + $Z = GCM::strToGMP(GCM::ZB_128); |
|
| 90 | + $V = GCM::strToGMP($Y); |
|
| 91 | + $R = GCM::strToGMP(self::R); |
|
| 92 | + for ($i = 0; $i < 128; ++$i) { |
|
| 93 | + // if bit at X[i] is set |
|
| 94 | + if (gmp_testbit($x, 127 - $i)) { |
|
| 95 | + $Z ^= $V; |
|
| 96 | + } |
|
| 97 | + // if LSB(Vi) = 0 |
|
| 98 | + if (!gmp_testbit($V, 0)) { |
|
| 99 | + $V >>= 1; |
|
| 100 | + } else { |
|
| 101 | + $V = ($V >> 1) ^ $R; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + return GCM::gmpToStr($Z, 16); |
|
| 105 | + } |
|
| 106 | 106 | } |
@@ -23,4 +23,4 @@ |
||
| 23 | 23 | // print the ciphertext along with the authentication tag |
| 24 | 24 | // and the initialization vector |
| 25 | 25 | echo bin2hex($ciphertext) . "\n" . bin2hex($auth_tag) . "\n" . bin2hex($iv) . |
| 26 | - "\n"; |
|
| 26 | + "\n"; |
|