@@ -14,267 +14,267 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class GCM |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Block of 64 zero bits. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - const ZB_64 = "\0\0\0\0\0\0\0\0"; |
|
| 17 | + /** |
|
| 18 | + * Block of 64 zero bits. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + const ZB_64 = "\0\0\0\0\0\0\0\0"; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Block of 128 zero bits. |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - const ZB_128 = self::ZB_64 . self::ZB_64; |
|
| 24 | + /** |
|
| 25 | + * Block of 128 zero bits. |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + const ZB_128 = self::ZB_64 . self::ZB_64; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Array of supported t-values, that is, the bit length of the |
|
| 33 | - * authentication tag. |
|
| 34 | - * |
|
| 35 | - * See NIST SP-800-38D section 5.2.1.2 for the details. |
|
| 36 | - * |
|
| 37 | - * @internal |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - const SUPPORTED_T_LEN = array(128, 120, 112, 104, 96, 64, 32); |
|
| 31 | + /** |
|
| 32 | + * Array of supported t-values, that is, the bit length of the |
|
| 33 | + * authentication tag. |
|
| 34 | + * |
|
| 35 | + * See NIST SP-800-38D section 5.2.1.2 for the details. |
|
| 36 | + * |
|
| 37 | + * @internal |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + const SUPPORTED_T_LEN = array(128, 120, 112, 104, 96, 64, 32); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Cipher. |
|
| 45 | - * |
|
| 46 | - * @var Cipher $_cipher |
|
| 47 | - */ |
|
| 48 | - protected $_cipher; |
|
| 43 | + /** |
|
| 44 | + * Cipher. |
|
| 45 | + * |
|
| 46 | + * @var Cipher $_cipher |
|
| 47 | + */ |
|
| 48 | + protected $_cipher; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Authentication tag length in bytes. |
|
| 52 | - * |
|
| 53 | - * @var int |
|
| 54 | - */ |
|
| 55 | - protected $_tagLength; |
|
| 50 | + /** |
|
| 51 | + * Authentication tag length in bytes. |
|
| 52 | + * |
|
| 53 | + * @var int |
|
| 54 | + */ |
|
| 55 | + protected $_tagLength; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Constructor. |
|
| 59 | - * |
|
| 60 | - * @param Cipher $cipher Cipher implementation |
|
| 61 | - * @param int $tag_length Authentication tag length in bytes |
|
| 62 | - * @throws \DomainException If tag length is not supported |
|
| 63 | - */ |
|
| 64 | - public function __construct(Cipher $cipher, $tag_length = 16) |
|
| 65 | - { |
|
| 66 | - if (!in_array($tag_length << 3, self::SUPPORTED_T_LEN)) { |
|
| 67 | - throw new \DomainException( |
|
| 68 | - "Tag length $tag_length is not supported."); |
|
| 69 | - } |
|
| 70 | - $this->_cipher = $cipher; |
|
| 71 | - $this->_tagLength = $tag_length; |
|
| 72 | - } |
|
| 57 | + /** |
|
| 58 | + * Constructor. |
|
| 59 | + * |
|
| 60 | + * @param Cipher $cipher Cipher implementation |
|
| 61 | + * @param int $tag_length Authentication tag length in bytes |
|
| 62 | + * @throws \DomainException If tag length is not supported |
|
| 63 | + */ |
|
| 64 | + public function __construct(Cipher $cipher, $tag_length = 16) |
|
| 65 | + { |
|
| 66 | + if (!in_array($tag_length << 3, self::SUPPORTED_T_LEN)) { |
|
| 67 | + throw new \DomainException( |
|
| 68 | + "Tag length $tag_length is not supported."); |
|
| 69 | + } |
|
| 70 | + $this->_cipher = $cipher; |
|
| 71 | + $this->_tagLength = $tag_length; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Encrypt plaintext. |
|
| 76 | - * |
|
| 77 | - * @param string $P Plaintext |
|
| 78 | - * @param string $A Additional authenticated data |
|
| 79 | - * @param string $K Encryption key |
|
| 80 | - * @param string $IV Initialization vector |
|
| 81 | - * @throws \RuntimeException For generic errors |
|
| 82 | - * @return array Tuple of ciphertext <code>C</code> and authentication tag |
|
| 83 | - * <code>T</code> |
|
| 84 | - */ |
|
| 85 | - public function encrypt($P, $A, $K, $IV) |
|
| 86 | - { |
|
| 87 | - $ghash = new GHASH($this->_cipher->encrypt(self::ZB_128, $K)); |
|
| 88 | - // generate pre-counter block |
|
| 89 | - $J0 = $this->_generateJ0($IV, $ghash); |
|
| 90 | - // encrypt |
|
| 91 | - $C = $this->_gctr(self::_inc32($J0), $P, $K); |
|
| 92 | - // generate authentication tag |
|
| 93 | - $T = $this->_computeAuthTag($A, $C, $J0, $K, $ghash); |
|
| 94 | - return [$C, $T]; |
|
| 95 | - } |
|
| 74 | + /** |
|
| 75 | + * Encrypt plaintext. |
|
| 76 | + * |
|
| 77 | + * @param string $P Plaintext |
|
| 78 | + * @param string $A Additional authenticated data |
|
| 79 | + * @param string $K Encryption key |
|
| 80 | + * @param string $IV Initialization vector |
|
| 81 | + * @throws \RuntimeException For generic errors |
|
| 82 | + * @return array Tuple of ciphertext <code>C</code> and authentication tag |
|
| 83 | + * <code>T</code> |
|
| 84 | + */ |
|
| 85 | + public function encrypt($P, $A, $K, $IV) |
|
| 86 | + { |
|
| 87 | + $ghash = new GHASH($this->_cipher->encrypt(self::ZB_128, $K)); |
|
| 88 | + // generate pre-counter block |
|
| 89 | + $J0 = $this->_generateJ0($IV, $ghash); |
|
| 90 | + // encrypt |
|
| 91 | + $C = $this->_gctr(self::_inc32($J0), $P, $K); |
|
| 92 | + // generate authentication tag |
|
| 93 | + $T = $this->_computeAuthTag($A, $C, $J0, $K, $ghash); |
|
| 94 | + return [$C, $T]; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Decrypt ciphertext. |
|
| 99 | - * |
|
| 100 | - * @param string $C Ciphertext |
|
| 101 | - * @param string $T Authentication tag |
|
| 102 | - * @param string $A Additional authenticated data |
|
| 103 | - * @param string $K Encryption key |
|
| 104 | - * @param string $IV Initialization vector |
|
| 105 | - * @throws AuthenticationException If message authentication fails |
|
| 106 | - * @throws \RuntimeException For generic errors |
|
| 107 | - * @return string Plaintext <code>P</code> |
|
| 108 | - */ |
|
| 109 | - public function decrypt($C, $T, $A, $K, $IV) |
|
| 110 | - { |
|
| 111 | - $ghash = new GHASH($this->_cipher->encrypt(self::ZB_128, $K)); |
|
| 112 | - // generate pre-counter block |
|
| 113 | - $J0 = $this->_generateJ0($IV, $ghash); |
|
| 114 | - // generate authentication tag |
|
| 115 | - $T2 = $this->_computeAuthTag($A, $C, $J0, $K, $ghash); |
|
| 116 | - // check that authentication tag matches |
|
| 117 | - if ($T !== $T2) { |
|
| 118 | - throw new AuthenticationException("Authentication failed."); |
|
| 119 | - } |
|
| 120 | - // decrypt |
|
| 121 | - return $this->_gctr(self::_inc32($J0), $C, $K); |
|
| 122 | - } |
|
| 97 | + /** |
|
| 98 | + * Decrypt ciphertext. |
|
| 99 | + * |
|
| 100 | + * @param string $C Ciphertext |
|
| 101 | + * @param string $T Authentication tag |
|
| 102 | + * @param string $A Additional authenticated data |
|
| 103 | + * @param string $K Encryption key |
|
| 104 | + * @param string $IV Initialization vector |
|
| 105 | + * @throws AuthenticationException If message authentication fails |
|
| 106 | + * @throws \RuntimeException For generic errors |
|
| 107 | + * @return string Plaintext <code>P</code> |
|
| 108 | + */ |
|
| 109 | + public function decrypt($C, $T, $A, $K, $IV) |
|
| 110 | + { |
|
| 111 | + $ghash = new GHASH($this->_cipher->encrypt(self::ZB_128, $K)); |
|
| 112 | + // generate pre-counter block |
|
| 113 | + $J0 = $this->_generateJ0($IV, $ghash); |
|
| 114 | + // generate authentication tag |
|
| 115 | + $T2 = $this->_computeAuthTag($A, $C, $J0, $K, $ghash); |
|
| 116 | + // check that authentication tag matches |
|
| 117 | + if ($T !== $T2) { |
|
| 118 | + throw new AuthenticationException("Authentication failed."); |
|
| 119 | + } |
|
| 120 | + // decrypt |
|
| 121 | + return $this->_gctr(self::_inc32($J0), $C, $K); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Generate pre-counter block. |
|
| 126 | - * |
|
| 127 | - * See NIST SP-300-38D section 7.1 step 2 for the details. |
|
| 128 | - * |
|
| 129 | - * @param string $IV Initialization vector |
|
| 130 | - * @param GHASH $ghash GHASH functor |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - private function _generateJ0($IV, GHASH $ghash) |
|
| 134 | - { |
|
| 135 | - // if len(IV) = 96 |
|
| 136 | - if (12 == strlen($IV)) { |
|
| 137 | - return $IV . "\0\0\0\1"; |
|
| 138 | - } |
|
| 139 | - $data = self::_pad128($IV) . self::ZB_64 . self::_uint64( |
|
| 140 | - strlen($IV) << 3); |
|
| 141 | - return $ghash($data); |
|
| 142 | - } |
|
| 124 | + /** |
|
| 125 | + * Generate pre-counter block. |
|
| 126 | + * |
|
| 127 | + * See NIST SP-300-38D section 7.1 step 2 for the details. |
|
| 128 | + * |
|
| 129 | + * @param string $IV Initialization vector |
|
| 130 | + * @param GHASH $ghash GHASH functor |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + private function _generateJ0($IV, GHASH $ghash) |
|
| 134 | + { |
|
| 135 | + // if len(IV) = 96 |
|
| 136 | + if (12 == strlen($IV)) { |
|
| 137 | + return $IV . "\0\0\0\1"; |
|
| 138 | + } |
|
| 139 | + $data = self::_pad128($IV) . self::ZB_64 . self::_uint64( |
|
| 140 | + strlen($IV) << 3); |
|
| 141 | + return $ghash($data); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Apply GCTR algorithm. |
|
| 146 | - * |
|
| 147 | - * See NIST SP-300-38D section 6.5 for the details. |
|
| 148 | - * |
|
| 149 | - * @param string $ICB Initial counter block |
|
| 150 | - * @param string $X Input data |
|
| 151 | - * @param string $K Encryption key |
|
| 152 | - * @return string Output data |
|
| 153 | - */ |
|
| 154 | - private function _gctr($ICB, $X, $K) |
|
| 155 | - { |
|
| 156 | - // if data is an empty string, return an empty string |
|
| 157 | - if ("" == $X) { |
|
| 158 | - return ""; |
|
| 159 | - } |
|
| 160 | - // number of blocks |
|
| 161 | - $n = ceil(strlen($X) / 16); |
|
| 162 | - $CB = $ICB; |
|
| 163 | - $Y = ""; |
|
| 164 | - for ($i = 0; $i < $n - 1; ++$i) { |
|
| 165 | - // plaintext block |
|
| 166 | - $xi = substr($X, $i << 4, 16); |
|
| 167 | - // encrypt block and append to Y |
|
| 168 | - $Y .= $xi ^ $this->_cipher->encrypt($CB, $K); |
|
| 169 | - // increment counter block |
|
| 170 | - $CB = self::_inc32($CB); |
|
| 171 | - } |
|
| 172 | - // final block |
|
| 173 | - $xn = substr($X, $i << 4); |
|
| 174 | - // XOR against partial block |
|
| 175 | - $Y .= $xn ^ substr($this->_cipher->encrypt($CB, $K), 0, strlen($xn)); |
|
| 176 | - return $Y; |
|
| 177 | - } |
|
| 144 | + /** |
|
| 145 | + * Apply GCTR algorithm. |
|
| 146 | + * |
|
| 147 | + * See NIST SP-300-38D section 6.5 for the details. |
|
| 148 | + * |
|
| 149 | + * @param string $ICB Initial counter block |
|
| 150 | + * @param string $X Input data |
|
| 151 | + * @param string $K Encryption key |
|
| 152 | + * @return string Output data |
|
| 153 | + */ |
|
| 154 | + private function _gctr($ICB, $X, $K) |
|
| 155 | + { |
|
| 156 | + // if data is an empty string, return an empty string |
|
| 157 | + if ("" == $X) { |
|
| 158 | + return ""; |
|
| 159 | + } |
|
| 160 | + // number of blocks |
|
| 161 | + $n = ceil(strlen($X) / 16); |
|
| 162 | + $CB = $ICB; |
|
| 163 | + $Y = ""; |
|
| 164 | + for ($i = 0; $i < $n - 1; ++$i) { |
|
| 165 | + // plaintext block |
|
| 166 | + $xi = substr($X, $i << 4, 16); |
|
| 167 | + // encrypt block and append to Y |
|
| 168 | + $Y .= $xi ^ $this->_cipher->encrypt($CB, $K); |
|
| 169 | + // increment counter block |
|
| 170 | + $CB = self::_inc32($CB); |
|
| 171 | + } |
|
| 172 | + // final block |
|
| 173 | + $xn = substr($X, $i << 4); |
|
| 174 | + // XOR against partial block |
|
| 175 | + $Y .= $xn ^ substr($this->_cipher->encrypt($CB, $K), 0, strlen($xn)); |
|
| 176 | + return $Y; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Compute authentication tag |
|
| 181 | - * |
|
| 182 | - * See NIST SP-300-38D section 7.1 steps 5-6 for the details. |
|
| 183 | - * |
|
| 184 | - * @param string $A Additional authenticated data |
|
| 185 | - * @param string $C Ciphertext |
|
| 186 | - * @param string $J0 Pre-counter block |
|
| 187 | - * @param string $K Encryption key |
|
| 188 | - * @param GHASH $ghash GHASH functor |
|
| 189 | - * @return string Authentication tag <code>T</code> |
|
| 190 | - */ |
|
| 191 | - private function _computeAuthTag($A, $C, $J0, $K, GHASH $ghash) |
|
| 192 | - { |
|
| 193 | - $data = self::_pad128($A) . self::_pad128($C) . |
|
| 194 | - self::_uint64(strlen($A) << 3) . self::_uint64(strlen($C) << 3); |
|
| 195 | - $S = $ghash($data); |
|
| 196 | - return substr($this->_gctr($J0, $S, $K), 0, $this->_tagLength); |
|
| 197 | - } |
|
| 179 | + /** |
|
| 180 | + * Compute authentication tag |
|
| 181 | + * |
|
| 182 | + * See NIST SP-300-38D section 7.1 steps 5-6 for the details. |
|
| 183 | + * |
|
| 184 | + * @param string $A Additional authenticated data |
|
| 185 | + * @param string $C Ciphertext |
|
| 186 | + * @param string $J0 Pre-counter block |
|
| 187 | + * @param string $K Encryption key |
|
| 188 | + * @param GHASH $ghash GHASH functor |
|
| 189 | + * @return string Authentication tag <code>T</code> |
|
| 190 | + */ |
|
| 191 | + private function _computeAuthTag($A, $C, $J0, $K, GHASH $ghash) |
|
| 192 | + { |
|
| 193 | + $data = self::_pad128($A) . self::_pad128($C) . |
|
| 194 | + self::_uint64(strlen($A) << 3) . self::_uint64(strlen($C) << 3); |
|
| 195 | + $S = $ghash($data); |
|
| 196 | + return substr($this->_gctr($J0, $S, $K), 0, $this->_tagLength); |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * Pad data to 128 bit block boundary. |
|
| 201 | - * |
|
| 202 | - * @param string $data |
|
| 203 | - * @return string |
|
| 204 | - */ |
|
| 205 | - private static function _pad128($data) |
|
| 206 | - { |
|
| 207 | - $padlen = 16 - strlen($data) % 16; |
|
| 208 | - if (16 != $padlen) { |
|
| 209 | - $data .= str_repeat("\0", $padlen); |
|
| 210 | - } |
|
| 211 | - return $data; |
|
| 212 | - } |
|
| 199 | + /** |
|
| 200 | + * Pad data to 128 bit block boundary. |
|
| 201 | + * |
|
| 202 | + * @param string $data |
|
| 203 | + * @return string |
|
| 204 | + */ |
|
| 205 | + private static function _pad128($data) |
|
| 206 | + { |
|
| 207 | + $padlen = 16 - strlen($data) % 16; |
|
| 208 | + if (16 != $padlen) { |
|
| 209 | + $data .= str_repeat("\0", $padlen); |
|
| 210 | + } |
|
| 211 | + return $data; |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Increment 32 rightmost bits of the counter block. |
|
| 216 | - * |
|
| 217 | - * See NIST SP-300-38D section 6.2 for the details. |
|
| 218 | - * |
|
| 219 | - * @param string $X |
|
| 220 | - * @return string |
|
| 221 | - */ |
|
| 222 | - private static function _inc32($X) |
|
| 223 | - { |
|
| 224 | - $Y = substr($X, 0, -4); |
|
| 225 | - // increment counter |
|
| 226 | - $n = self::strToGMP(substr($X, -4)) + 1; |
|
| 227 | - // wrap by using only the 32 rightmost bits |
|
| 228 | - $Y .= substr(self::gmpToStr($n, 4), -4); |
|
| 229 | - return $Y; |
|
| 230 | - } |
|
| 214 | + /** |
|
| 215 | + * Increment 32 rightmost bits of the counter block. |
|
| 216 | + * |
|
| 217 | + * See NIST SP-300-38D section 6.2 for the details. |
|
| 218 | + * |
|
| 219 | + * @param string $X |
|
| 220 | + * @return string |
|
| 221 | + */ |
|
| 222 | + private static function _inc32($X) |
|
| 223 | + { |
|
| 224 | + $Y = substr($X, 0, -4); |
|
| 225 | + // increment counter |
|
| 226 | + $n = self::strToGMP(substr($X, -4)) + 1; |
|
| 227 | + // wrap by using only the 32 rightmost bits |
|
| 228 | + $Y .= substr(self::gmpToStr($n, 4), -4); |
|
| 229 | + return $Y; |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Convert integer to 64 bit big endian binary string. |
|
| 234 | - * |
|
| 235 | - * @param int $num |
|
| 236 | - * @return string |
|
| 237 | - */ |
|
| 238 | - private static function _uint64($num) |
|
| 239 | - { |
|
| 240 | - // truncate on 32 bit hosts |
|
| 241 | - if (PHP_INT_SIZE < 8) { |
|
| 242 | - return "\0\0\0\0" . pack("N", $num); |
|
| 243 | - } |
|
| 244 | - return pack("J", $num); |
|
| 245 | - } |
|
| 232 | + /** |
|
| 233 | + * Convert integer to 64 bit big endian binary string. |
|
| 234 | + * |
|
| 235 | + * @param int $num |
|
| 236 | + * @return string |
|
| 237 | + */ |
|
| 238 | + private static function _uint64($num) |
|
| 239 | + { |
|
| 240 | + // truncate on 32 bit hosts |
|
| 241 | + if (PHP_INT_SIZE < 8) { |
|
| 242 | + return "\0\0\0\0" . pack("N", $num); |
|
| 243 | + } |
|
| 244 | + return pack("J", $num); |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * Convert string to GMP number. |
|
| 249 | - * |
|
| 250 | - * String is interpreted as an unsigned integer with big endian order and |
|
| 251 | - * the most significant byte first. |
|
| 252 | - * |
|
| 253 | - * @param string $data Binary data |
|
| 254 | - * @return \GMP |
|
| 255 | - */ |
|
| 256 | - public static function strToGMP($data) |
|
| 257 | - { |
|
| 258 | - return gmp_import($data, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 259 | - } |
|
| 247 | + /** |
|
| 248 | + * Convert string to GMP number. |
|
| 249 | + * |
|
| 250 | + * String is interpreted as an unsigned integer with big endian order and |
|
| 251 | + * the most significant byte first. |
|
| 252 | + * |
|
| 253 | + * @param string $data Binary data |
|
| 254 | + * @return \GMP |
|
| 255 | + */ |
|
| 256 | + public static function strToGMP($data) |
|
| 257 | + { |
|
| 258 | + return gmp_import($data, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Convert GMP number to string. |
|
| 263 | - * |
|
| 264 | - * Returned string represents an unsigned integer with big endian order and |
|
| 265 | - * the most significant byte first. |
|
| 266 | - * |
|
| 267 | - * @param \GMP $num GMP number |
|
| 268 | - * @param int $size Width of the string in bytes |
|
| 269 | - * @return string Binary data |
|
| 270 | - */ |
|
| 271 | - public static function gmpToStr(\GMP $num, $size) |
|
| 272 | - { |
|
| 273 | - $data = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 274 | - $len = strlen($data); |
|
| 275 | - if ($len < $size) { |
|
| 276 | - $data = str_repeat("\0", $size - $len) . $data; |
|
| 277 | - } |
|
| 278 | - return $data; |
|
| 279 | - } |
|
| 261 | + /** |
|
| 262 | + * Convert GMP number to string. |
|
| 263 | + * |
|
| 264 | + * Returned string represents an unsigned integer with big endian order and |
|
| 265 | + * the most significant byte first. |
|
| 266 | + * |
|
| 267 | + * @param \GMP $num GMP number |
|
| 268 | + * @param int $size Width of the string in bytes |
|
| 269 | + * @return string Binary data |
|
| 270 | + */ |
|
| 271 | + public static function gmpToStr(\GMP $num, $size) |
|
| 272 | + { |
|
| 273 | + $data = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 274 | + $len = strlen($data); |
|
| 275 | + if ($len < $size) { |
|
| 276 | + $data = str_repeat("\0", $size - $len) . $data; |
|
| 277 | + } |
|
| 278 | + return $data; |
|
| 279 | + } |
|
| 280 | 280 | } |
@@ -11,94 +11,94 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class GHASH |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Fixed R-block. |
|
| 16 | - * |
|
| 17 | - * @var string |
|
| 18 | - */ |
|
| 19 | - const R = "\xE1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
|
| 14 | + /** |
|
| 15 | + * Fixed R-block. |
|
| 16 | + * |
|
| 17 | + * @var string |
|
| 18 | + */ |
|
| 19 | + const R = "\xE1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Hash subkey. |
|
| 23 | - * |
|
| 24 | - * @var string $_subkey |
|
| 25 | - */ |
|
| 26 | - protected $_subkey; |
|
| 21 | + /** |
|
| 22 | + * Hash subkey. |
|
| 23 | + * |
|
| 24 | + * @var string $_subkey |
|
| 25 | + */ |
|
| 26 | + protected $_subkey; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param string $subkey Hash subkey |
|
| 32 | - */ |
|
| 33 | - public function __construct($subkey) |
|
| 34 | - { |
|
| 35 | - if (strlen($subkey) != 16) { |
|
| 36 | - throw new \LengthException("Subkey must be 128 bits."); |
|
| 37 | - } |
|
| 38 | - $this->_subkey = $subkey; |
|
| 39 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param string $subkey Hash subkey |
|
| 32 | + */ |
|
| 33 | + public function __construct($subkey) |
|
| 34 | + { |
|
| 35 | + if (strlen($subkey) != 16) { |
|
| 36 | + throw new \LengthException("Subkey must be 128 bits."); |
|
| 37 | + } |
|
| 38 | + $this->_subkey = $subkey; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Compute hash. |
|
| 43 | - * |
|
| 44 | - * @param string $X Input string |
|
| 45 | - * @return string Hash |
|
| 46 | - */ |
|
| 47 | - public function compute($X) |
|
| 48 | - { |
|
| 49 | - $len = strlen($X); |
|
| 50 | - if (0 != $len % 16) { |
|
| 51 | - throw new \UnexpectedValueException( |
|
| 52 | - "Input string must be a multiple of 128 bits."); |
|
| 53 | - } |
|
| 54 | - $Y = GCM::ZB_128; |
|
| 55 | - // number of 128-bit blocks |
|
| 56 | - $m = $len >> 4; |
|
| 57 | - for ($i = 0; $i < $m; ++$i) { |
|
| 58 | - $xi = substr($X, $i << 4, 16); |
|
| 59 | - $Y = $this->_mult($Y ^ $xi, $this->_subkey); |
|
| 60 | - } |
|
| 61 | - return $Y; |
|
| 62 | - } |
|
| 41 | + /** |
|
| 42 | + * Compute hash. |
|
| 43 | + * |
|
| 44 | + * @param string $X Input string |
|
| 45 | + * @return string Hash |
|
| 46 | + */ |
|
| 47 | + public function compute($X) |
|
| 48 | + { |
|
| 49 | + $len = strlen($X); |
|
| 50 | + if (0 != $len % 16) { |
|
| 51 | + throw new \UnexpectedValueException( |
|
| 52 | + "Input string must be a multiple of 128 bits."); |
|
| 53 | + } |
|
| 54 | + $Y = GCM::ZB_128; |
|
| 55 | + // number of 128-bit blocks |
|
| 56 | + $m = $len >> 4; |
|
| 57 | + for ($i = 0; $i < $m; ++$i) { |
|
| 58 | + $xi = substr($X, $i << 4, 16); |
|
| 59 | + $Y = $this->_mult($Y ^ $xi, $this->_subkey); |
|
| 60 | + } |
|
| 61 | + return $Y; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Functor method for <code>compute</code>. |
|
| 66 | - * |
|
| 67 | - * @param string $arg |
|
| 68 | - * @return string |
|
| 69 | - */ |
|
| 70 | - public function __invoke($arg) |
|
| 71 | - { |
|
| 72 | - return $this->compute($arg); |
|
| 73 | - } |
|
| 64 | + /** |
|
| 65 | + * Functor method for <code>compute</code>. |
|
| 66 | + * |
|
| 67 | + * @param string $arg |
|
| 68 | + * @return string |
|
| 69 | + */ |
|
| 70 | + public function __invoke($arg) |
|
| 71 | + { |
|
| 72 | + return $this->compute($arg); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Apply block multiplication operation. |
|
| 77 | - * |
|
| 78 | - * See NIST SP-800-38D, chapter 6.3 for the details. |
|
| 79 | - * |
|
| 80 | - * @param string $X |
|
| 81 | - * @param string $Y |
|
| 82 | - * @return string |
|
| 83 | - */ |
|
| 84 | - private function _mult($X, $Y) |
|
| 85 | - { |
|
| 86 | - $x = GCM::strToGMP($X); |
|
| 87 | - $Z = GCM::strToGMP(GCM::ZB_128); |
|
| 88 | - $V = GCM::strToGMP($Y); |
|
| 89 | - $R = GCM::strToGMP(self::R); |
|
| 90 | - for ($i = 0; $i < 128; ++$i) { |
|
| 91 | - // if bit at X[i] is set |
|
| 92 | - if (gmp_testbit($x, 127 - $i)) { |
|
| 93 | - $Z ^= $V; |
|
| 94 | - } |
|
| 95 | - // if LSB(Vi) = 0 |
|
| 96 | - if (!gmp_testbit($V, 0)) { |
|
| 97 | - $V >>= 1; |
|
| 98 | - } else { |
|
| 99 | - $V = ($V >> 1) ^ $R; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - return GCM::gmpToStr($Z, 16); |
|
| 103 | - } |
|
| 75 | + /** |
|
| 76 | + * Apply block multiplication operation. |
|
| 77 | + * |
|
| 78 | + * See NIST SP-800-38D, chapter 6.3 for the details. |
|
| 79 | + * |
|
| 80 | + * @param string $X |
|
| 81 | + * @param string $Y |
|
| 82 | + * @return string |
|
| 83 | + */ |
|
| 84 | + private function _mult($X, $Y) |
|
| 85 | + { |
|
| 86 | + $x = GCM::strToGMP($X); |
|
| 87 | + $Z = GCM::strToGMP(GCM::ZB_128); |
|
| 88 | + $V = GCM::strToGMP($Y); |
|
| 89 | + $R = GCM::strToGMP(self::R); |
|
| 90 | + for ($i = 0; $i < 128; ++$i) { |
|
| 91 | + // if bit at X[i] is set |
|
| 92 | + if (gmp_testbit($x, 127 - $i)) { |
|
| 93 | + $Z ^= $V; |
|
| 94 | + } |
|
| 95 | + // if LSB(Vi) = 0 |
|
| 96 | + if (!gmp_testbit($V, 0)) { |
|
| 97 | + $V >>= 1; |
|
| 98 | + } else { |
|
| 99 | + $V = ($V >> 1) ^ $R; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + return GCM::gmpToStr($Z, 16); |
|
| 103 | + } |
|
| 104 | 104 | } |