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