| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM; | 
| 6 | 6 | |
| @@ -13,97 +13,97 @@ | ||
| 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 | |
| 27 | - */ | |
| 28 | - protected $_subkey; | |
| 23 | + /** | |
| 24 | + * Hash subkey. | |
| 25 | + * | |
| 26 | + * @var string | |
| 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 (16 !== strlen($subkey)) { | |
| 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 (16 !== strlen($subkey)) { | |
| 38 | +			throw new \LengthException('Subkey must be 128 bits.'); | |
| 39 | + } | |
| 40 | + $this->_subkey = $subkey; | |
| 41 | + } | |
| 42 | 42 | |
| 43 | - /** | |
| 44 | - * Functor method for `compute`. | |
| 45 | - * | |
| 46 | - * @param string $arg | |
| 47 | - * | |
| 48 | - * @return string | |
| 49 | - */ | |
| 50 | - public function __invoke(string $arg): string | |
| 51 | -    { | |
| 52 | - return $this->compute($arg); | |
| 53 | - } | |
| 43 | + /** | |
| 44 | + * Functor method for `compute`. | |
| 45 | + * | |
| 46 | + * @param string $arg | |
| 47 | + * | |
| 48 | + * @return string | |
| 49 | + */ | |
| 50 | + public function __invoke(string $arg): string | |
| 51 | +	{ | |
| 52 | + return $this->compute($arg); | |
| 53 | + } | |
| 54 | 54 | |
| 55 | - /** | |
| 56 | - * Compute hash. | |
| 57 | - * | |
| 58 | - * @param string $X Input string | |
| 59 | - * | |
| 60 | - * @return string Hash | |
| 61 | - */ | |
| 62 | - public function compute(string $X): string | |
| 63 | -    { | |
| 64 | - $len = strlen($X); | |
| 65 | -        if (0 !== $len % 16) { | |
| 66 | - throw new \UnexpectedValueException( | |
| 67 | - 'Input string must be a multiple of 128 bits.'); | |
| 68 | - } | |
| 69 | - $Y = GCM::ZB_128; | |
| 70 | - // number of 128-bit blocks | |
| 71 | - $m = $len >> 4; | |
| 72 | -        for ($i = 0; $i < $m; ++$i) { | |
| 73 | - $xi = substr($X, $i << 4, 16); | |
| 74 | - $Y = $this->_mult($Y ^ $xi, $this->_subkey); | |
| 75 | - } | |
| 76 | - return $Y; | |
| 77 | - } | |
| 55 | + /** | |
| 56 | + * Compute hash. | |
| 57 | + * | |
| 58 | + * @param string $X Input string | |
| 59 | + * | |
| 60 | + * @return string Hash | |
| 61 | + */ | |
| 62 | + public function compute(string $X): string | |
| 63 | +	{ | |
| 64 | + $len = strlen($X); | |
| 65 | +		if (0 !== $len % 16) { | |
| 66 | + throw new \UnexpectedValueException( | |
| 67 | + 'Input string must be a multiple of 128 bits.'); | |
| 68 | + } | |
| 69 | + $Y = GCM::ZB_128; | |
| 70 | + // number of 128-bit blocks | |
| 71 | + $m = $len >> 4; | |
| 72 | +		for ($i = 0; $i < $m; ++$i) { | |
| 73 | + $xi = substr($X, $i << 4, 16); | |
| 74 | + $Y = $this->_mult($Y ^ $xi, $this->_subkey); | |
| 75 | + } | |
| 76 | + return $Y; | |
| 77 | + } | |
| 78 | 78 | |
| 79 | - /** | |
| 80 | - * Apply block multiplication operation. | |
| 81 | - * | |
| 82 | - * See NIST SP-800-38D, chapter 6.3 for the details. | |
| 83 | - * | |
| 84 | - * @param string $X | |
| 85 | - * @param string $Y | |
| 86 | - * | |
| 87 | - * @return string | |
| 88 | - */ | |
| 89 | - private function _mult(string $X, string $Y): string | |
| 90 | -    { | |
| 91 | - $x = GCM::strToGMP($X); | |
| 92 | - $Z = GCM::strToGMP(GCM::ZB_128); | |
| 93 | - $V = GCM::strToGMP($Y); | |
| 94 | - $R = GCM::strToGMP(self::R); | |
| 95 | -        for ($i = 0; $i < 128; ++$i) { | |
| 96 | - // if bit at X[i] is set | |
| 97 | -            if (gmp_testbit($x, 127 - $i)) { | |
| 98 | - $Z ^= $V; | |
| 99 | - } | |
| 100 | - // if LSB(Vi) = 0 | |
| 101 | -            if (!gmp_testbit($V, 0)) { | |
| 102 | - $V >>= 1; | |
| 103 | -            } else { | |
| 104 | - $V = ($V >> 1) ^ $R; | |
| 105 | - } | |
| 106 | - } | |
| 107 | - return GCM::gmpToStr($Z, 16); | |
| 108 | - } | |
| 79 | + /** | |
| 80 | + * Apply block multiplication operation. | |
| 81 | + * | |
| 82 | + * See NIST SP-800-38D, chapter 6.3 for the details. | |
| 83 | + * | |
| 84 | + * @param string $X | |
| 85 | + * @param string $Y | |
| 86 | + * | |
| 87 | + * @return string | |
| 88 | + */ | |
| 89 | + private function _mult(string $X, string $Y): string | |
| 90 | +	{ | |
| 91 | + $x = GCM::strToGMP($X); | |
| 92 | + $Z = GCM::strToGMP(GCM::ZB_128); | |
| 93 | + $V = GCM::strToGMP($Y); | |
| 94 | + $R = GCM::strToGMP(self::R); | |
| 95 | +		for ($i = 0; $i < 128; ++$i) { | |
| 96 | + // if bit at X[i] is set | |
| 97 | +			if (gmp_testbit($x, 127 - $i)) { | |
| 98 | + $Z ^= $V; | |
| 99 | + } | |
| 100 | + // if LSB(Vi) = 0 | |
| 101 | +			if (!gmp_testbit($V, 0)) { | |
| 102 | + $V >>= 1; | |
| 103 | +			} else { | |
| 104 | + $V = ($V >> 1) ^ $R; | |
| 105 | + } | |
| 106 | + } | |
| 107 | + return GCM::gmpToStr($Z, 16); | |
| 108 | + } | |
| 109 | 109 | } | 
| @@ -9,13 +9,13 @@ | ||
| 9 | 9 | */ | 
| 10 | 10 | interface Cipher | 
| 11 | 11 |  { | 
| 12 | - /** | |
| 13 | - * Encrypt data. | |
| 14 | - * | |
| 15 | - * @param string $data Data to encrypt | |
| 16 | - * @param string $key Encryption key | |
| 17 | - * | |
| 18 | - * @return string Encrypted data | |
| 19 | - */ | |
| 20 | - public function encrypt(string $data, string $key): string; | |
| 12 | + /** | |
| 13 | + * Encrypt data. | |
| 14 | + * | |
| 15 | + * @param string $data Data to encrypt | |
| 16 | + * @param string $key Encryption key | |
| 17 | + * | |
| 18 | + * @return string Encrypted data | |
| 19 | + */ | |
| 20 | + public function encrypt(string $data, string $key): string; | |
| 21 | 21 | } | 
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM\Cipher; | 
| 6 | 6 | |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM\Cipher\AES; | 
| 6 | 6 | |
| @@ -12,158 +12,158 @@ | ||
| 12 | 12 | */ | 
| 13 | 13 | abstract class AESCipher implements Cipher | 
| 14 | 14 |  { | 
| 15 | - /** | |
| 16 | - * Mapping from key size in bits to AES cipher implementation class name. | |
| 17 | - * | |
| 18 | - * @internal | |
| 19 | - * | |
| 20 | - * @var array | |
| 21 | - */ | |
| 22 | - const MAP_KEYSIZE_TO_CLS = [ | |
| 23 | - 128 => AES128Cipher::class, | |
| 24 | - 192 => AES192Cipher::class, | |
| 25 | - 256 => AES256Cipher::class, | |
| 26 | - ]; | |
| 15 | + /** | |
| 16 | + * Mapping from key size in bits to AES cipher implementation class name. | |
| 17 | + * | |
| 18 | + * @internal | |
| 19 | + * | |
| 20 | + * @var array | |
| 21 | + */ | |
| 22 | + const MAP_KEYSIZE_TO_CLS = [ | |
| 23 | + 128 => AES128Cipher::class, | |
| 24 | + 192 => AES192Cipher::class, | |
| 25 | + 256 => AES256Cipher::class, | |
| 26 | + ]; | |
| 27 | 27 | |
| 28 | - /** | |
| 29 | - * Get AES cipher instance by key length. | |
| 30 | - * | |
| 31 | - * @param int $len Key length in bytes | |
| 32 | - * | |
| 33 | - * @throws \UnexpectedValueException | |
| 34 | - * | |
| 35 | - * @return self | |
| 36 | - */ | |
| 37 | - public static function fromKeyLength(int $len): self | |
| 38 | -    { | |
| 39 | - $bits = $len << 3; | |
| 40 | -        if (!array_key_exists($bits, self::MAP_KEYSIZE_TO_CLS)) { | |
| 41 | - throw new \UnexpectedValueException( | |
| 42 | -                "No AES implementation for {$bits}-bit key size."); | |
| 43 | - } | |
| 44 | - $cls = self::MAP_KEYSIZE_TO_CLS[$bits]; | |
| 45 | - return new $cls(); | |
| 46 | - } | |
| 28 | + /** | |
| 29 | + * Get AES cipher instance by key length. | |
| 30 | + * | |
| 31 | + * @param int $len Key length in bytes | |
| 32 | + * | |
| 33 | + * @throws \UnexpectedValueException | |
| 34 | + * | |
| 35 | + * @return self | |
| 36 | + */ | |
| 37 | + public static function fromKeyLength(int $len): self | |
| 38 | +	{ | |
| 39 | + $bits = $len << 3; | |
| 40 | +		if (!array_key_exists($bits, self::MAP_KEYSIZE_TO_CLS)) { | |
| 41 | + throw new \UnexpectedValueException( | |
| 42 | +				"No AES implementation for {$bits}-bit key size."); | |
| 43 | + } | |
| 44 | + $cls = self::MAP_KEYSIZE_TO_CLS[$bits]; | |
| 45 | + return new $cls(); | |
| 46 | + } | |
| 47 | 47 | |
| 48 | - /** | |
| 49 | -     * {@inheritdoc} | |
| 50 | - * | |
| 51 | - * @throws \UnexpectedValueException If key size is incorrect | |
| 52 | - * @throws \RuntimeException For generic errors | |
| 53 | - */ | |
| 54 | - public function encrypt(string $data, string $key): string | |
| 55 | -    { | |
| 56 | - $this->_checkKeySize($key); | |
| 57 | - $result = openssl_encrypt($data, $this->_cipherName(), $key, | |
| 58 | - OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); | |
| 59 | -        if (false === $result) { | |
| 60 | - throw new \RuntimeException( | |
| 61 | - 'openssl_encrypt() failed: ' . self::_getLastOpenSSLError()); | |
| 62 | - } | |
| 63 | - return $result; | |
| 64 | - } | |
| 48 | + /** | |
| 49 | +	 * {@inheritdoc} | |
| 50 | + * | |
| 51 | + * @throws \UnexpectedValueException If key size is incorrect | |
| 52 | + * @throws \RuntimeException For generic errors | |
| 53 | + */ | |
| 54 | + public function encrypt(string $data, string $key): string | |
| 55 | +	{ | |
| 56 | + $this->_checkKeySize($key); | |
| 57 | + $result = openssl_encrypt($data, $this->_cipherName(), $key, | |
| 58 | + OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); | |
| 59 | +		if (false === $result) { | |
| 60 | + throw new \RuntimeException( | |
| 61 | + 'openssl_encrypt() failed: ' . self::_getLastOpenSSLError()); | |
| 62 | + } | |
| 63 | + return $result; | |
| 64 | + } | |
| 65 | 65 | |
| 66 | - /** | |
| 67 | - * Check whether OpenSSL has native AES-GCM cipher available. | |
| 68 | - * | |
| 69 | - * @return bool | |
| 70 | - */ | |
| 71 | - public function hasNativeCipher(): bool | |
| 72 | -    { | |
| 73 | - static $supported_methods; | |
| 74 | -        if (!isset($supported_methods)) { | |
| 75 | - $supported_methods = array_flip(openssl_get_cipher_methods(false)); | |
| 76 | - } | |
| 77 | - $method = $this->_nativeCipherName(); | |
| 78 | - return isset($supported_methods[$method]); | |
| 79 | - } | |
| 66 | + /** | |
| 67 | + * Check whether OpenSSL has native AES-GCM cipher available. | |
| 68 | + * | |
| 69 | + * @return bool | |
| 70 | + */ | |
| 71 | + public function hasNativeCipher(): bool | |
| 72 | +	{ | |
| 73 | + static $supported_methods; | |
| 74 | +		if (!isset($supported_methods)) { | |
| 75 | + $supported_methods = array_flip(openssl_get_cipher_methods(false)); | |
| 76 | + } | |
| 77 | + $method = $this->_nativeCipherName(); | |
| 78 | + return isset($supported_methods[$method]); | |
| 79 | + } | |
| 80 | 80 | |
| 81 | - /** | |
| 82 | - * Encrypt plaintext using native OpenSSL. | |
| 83 | - * | |
| 84 | - * @param string $plaintext Plaintext to encrypt | |
| 85 | - * @param string $aad Additional authenticated data | |
| 86 | - * @param string $key Encryption key | |
| 87 | - * @param string $iv Initialization vector | |
| 88 | - * @param int $tag_length Authentication tag length in bytes | |
| 89 | - * | |
| 90 | - * @return array Tuple of ciphertext and authentication tag | |
| 91 | - */ | |
| 92 | - public function nativeEncrypt(string $plaintext, string $aad, string $key, | |
| 93 | - string $iv, int $tag_length = 16): array | |
| 94 | -    { | |
| 95 | - $this->_checkKeySize($key); | |
| 96 | - $ciphertext = @openssl_encrypt($plaintext, $this->_nativeCipherName(), | |
| 97 | - $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv, $tag, $aad, $tag_length); | |
| 98 | - // should never fail, since key size is already checked | |
| 99 | -        if (false === $ciphertext) { | |
| 100 | - throw new \RuntimeException( | |
| 101 | - 'openssl_encrypt() failed: ' . self::_getLastOpenSSLError()); | |
| 102 | - } | |
| 103 | - return [$ciphertext, $tag]; | |
| 104 | - } | |
| 81 | + /** | |
| 82 | + * Encrypt plaintext using native OpenSSL. | |
| 83 | + * | |
| 84 | + * @param string $plaintext Plaintext to encrypt | |
| 85 | + * @param string $aad Additional authenticated data | |
| 86 | + * @param string $key Encryption key | |
| 87 | + * @param string $iv Initialization vector | |
| 88 | + * @param int $tag_length Authentication tag length in bytes | |
| 89 | + * | |
| 90 | + * @return array Tuple of ciphertext and authentication tag | |
| 91 | + */ | |
| 92 | + public function nativeEncrypt(string $plaintext, string $aad, string $key, | |
| 93 | + string $iv, int $tag_length = 16): array | |
| 94 | +	{ | |
| 95 | + $this->_checkKeySize($key); | |
| 96 | + $ciphertext = @openssl_encrypt($plaintext, $this->_nativeCipherName(), | |
| 97 | + $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv, $tag, $aad, $tag_length); | |
| 98 | + // should never fail, since key size is already checked | |
| 99 | +		if (false === $ciphertext) { | |
| 100 | + throw new \RuntimeException( | |
| 101 | + 'openssl_encrypt() failed: ' . self::_getLastOpenSSLError()); | |
| 102 | + } | |
| 103 | + return [$ciphertext, $tag]; | |
| 104 | + } | |
| 105 | 105 | |
| 106 | - /** | |
| 107 | - * Decrypt ciphertext using native OpenSSL. | |
| 108 | - * | |
| 109 | - * @param string $ciphertext Ciphertext to decrypt | |
| 110 | - * @param string $auth_tag Authentication tag to verify | |
| 111 | - * @param string $aad Additional authenticated data | |
| 112 | - * @param string $key Encryption key | |
| 113 | - * @param string $iv Initialization vector | |
| 114 | - * | |
| 115 | - * @return string Plaintext | |
| 116 | - */ | |
| 117 | - public function nativeDecrypt(string $ciphertext, string $auth_tag, | |
| 118 | - string $aad, string $key, string $iv): string | |
| 119 | -    { | |
| 120 | - $this->_checkKeySize($key); | |
| 121 | - $plaintext = openssl_decrypt($ciphertext, $this->_nativeCipherName(), | |
| 122 | - $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv, $auth_tag, $aad); | |
| 123 | -        if (false === $plaintext) { | |
| 124 | -            throw new AuthenticationException('Authentication failed.'); | |
| 125 | - } | |
| 126 | - return $plaintext; | |
| 127 | - } | |
| 106 | + /** | |
| 107 | + * Decrypt ciphertext using native OpenSSL. | |
| 108 | + * | |
| 109 | + * @param string $ciphertext Ciphertext to decrypt | |
| 110 | + * @param string $auth_tag Authentication tag to verify | |
| 111 | + * @param string $aad Additional authenticated data | |
| 112 | + * @param string $key Encryption key | |
| 113 | + * @param string $iv Initialization vector | |
| 114 | + * | |
| 115 | + * @return string Plaintext | |
| 116 | + */ | |
| 117 | + public function nativeDecrypt(string $ciphertext, string $auth_tag, | |
| 118 | + string $aad, string $key, string $iv): string | |
| 119 | +	{ | |
| 120 | + $this->_checkKeySize($key); | |
| 121 | + $plaintext = openssl_decrypt($ciphertext, $this->_nativeCipherName(), | |
| 122 | + $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv, $auth_tag, $aad); | |
| 123 | +		if (false === $plaintext) { | |
| 124 | +			throw new AuthenticationException('Authentication failed.'); | |
| 125 | + } | |
| 126 | + return $plaintext; | |
| 127 | + } | |
| 128 | 128 | |
| 129 | - /** | |
| 130 | - * Get the AES-ECB cipher method name recognized by OpenSSL. | |
| 131 | - * | |
| 132 | - * @return string | |
| 133 | - */ | |
| 134 | - abstract protected function _cipherName(): string; | |
| 129 | + /** | |
| 130 | + * Get the AES-ECB cipher method name recognized by OpenSSL. | |
| 131 | + * | |
| 132 | + * @return string | |
| 133 | + */ | |
| 134 | + abstract protected function _cipherName(): string; | |
| 135 | 135 | |
| 136 | - /** | |
| 137 | - * Get the AES-GCM cipher method recognized by OpenSSL. | |
| 138 | - */ | |
| 139 | - abstract protected function _nativeCipherName(): string; | |
| 136 | + /** | |
| 137 | + * Get the AES-GCM cipher method recognized by OpenSSL. | |
| 138 | + */ | |
| 139 | + abstract protected function _nativeCipherName(): string; | |
| 140 | 140 | |
| 141 | - /** | |
| 142 | - * Get the key size in bytes. | |
| 143 | - * | |
| 144 | - * @return int | |
| 145 | - */ | |
| 146 | - abstract protected function _keySize(): int; | |
| 141 | + /** | |
| 142 | + * Get the key size in bytes. | |
| 143 | + * | |
| 144 | + * @return int | |
| 145 | + */ | |
| 146 | + abstract protected function _keySize(): int; | |
| 147 | 147 | |
| 148 | - protected function _checkKeySize(string $key): void | |
| 149 | -    { | |
| 150 | -        if (strlen($key) !== $this->_keySize()) { | |
| 151 | -            throw new \UnexpectedValueException('Key size must be ' . | |
| 152 | - $this->_keySize() . ' bytes.'); | |
| 153 | - } | |
| 154 | - } | |
| 148 | + protected function _checkKeySize(string $key): void | |
| 149 | +	{ | |
| 150 | +		if (strlen($key) !== $this->_keySize()) { | |
| 151 | +			throw new \UnexpectedValueException('Key size must be ' . | |
| 152 | + $this->_keySize() . ' bytes.'); | |
| 153 | + } | |
| 154 | + } | |
| 155 | 155 | |
| 156 | - /** | |
| 157 | - * Get latest OpenSSL error message. | |
| 158 | - * | |
| 159 | - * @return string | |
| 160 | - */ | |
| 161 | - protected static function _getLastOpenSSLError(): string | |
| 162 | -    { | |
| 163 | - $msg = ''; | |
| 164 | -        while (false !== ($err = openssl_error_string())) { | |
| 165 | - $msg = $err; | |
| 166 | - } | |
| 167 | - return $msg; | |
| 168 | - } | |
| 156 | + /** | |
| 157 | + * Get latest OpenSSL error message. | |
| 158 | + * | |
| 159 | + * @return string | |
| 160 | + */ | |
| 161 | + protected static function _getLastOpenSSLError(): string | |
| 162 | +	{ | |
| 163 | + $msg = ''; | |
| 164 | +		while (false !== ($err = openssl_error_string())) { | |
| 165 | + $msg = $err; | |
| 166 | + } | |
| 167 | + return $msg; | |
| 168 | + } | |
| 169 | 169 | } | 
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM\Exception; | 
| 6 | 6 | |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM; | 
| 6 | 6 | |
| @@ -11,62 +11,62 @@ | ||
| 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 | - * @param int $tag_length Authentication tag length in bytes | |
| 22 | - * | |
| 23 | - * @return array Tuple of ciphertext and authentication tag | |
| 24 | - */ | |
| 25 | - public static function encrypt(string $plaintext, string $aad, string $key, | |
| 26 | - string $iv, int $tag_length = 16): array | |
| 27 | -    { | |
| 28 | - $cipher = AESCipher::fromKeyLength(strlen($key)); | |
| 29 | -        if ($cipher->hasNativeCipher()) { | |
| 30 | - return $cipher->nativeEncrypt($plaintext, $aad, $key, $iv, $tag_length); | |
| 31 | - } | |
| 32 | - return self::_getGCM($cipher, $tag_length) | |
| 33 | - ->encrypt($plaintext, $aad, $key, $iv); | |
| 34 | - } | |
| 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 | + * @param int $tag_length Authentication tag length in bytes | |
| 22 | + * | |
| 23 | + * @return array Tuple of ciphertext and authentication tag | |
| 24 | + */ | |
| 25 | + public static function encrypt(string $plaintext, string $aad, string $key, | |
| 26 | + string $iv, int $tag_length = 16): array | |
| 27 | +	{ | |
| 28 | + $cipher = AESCipher::fromKeyLength(strlen($key)); | |
| 29 | +		if ($cipher->hasNativeCipher()) { | |
| 30 | + return $cipher->nativeEncrypt($plaintext, $aad, $key, $iv, $tag_length); | |
| 31 | + } | |
| 32 | + return self::_getGCM($cipher, $tag_length) | |
| 33 | + ->encrypt($plaintext, $aad, $key, $iv); | |
| 34 | + } | |
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * Decrypt ciphertext. | |
| 38 | - * | |
| 39 | - * @param string $ciphertext Ciphertext to decrypt | |
| 40 | - * @param string $auth_tag Authentication tag to verify | |
| 41 | - * @param string $aad Additional authenticated data | |
| 42 | - * @param string $key Encryption key | |
| 43 | - * @param string $iv Initialization vector | |
| 44 | - * | |
| 45 | - * @throws \Sop\GCM\Exception\AuthenticationException If message authentication fails | |
| 46 | - * | |
| 47 | - * @return string Plaintext | |
| 48 | - */ | |
| 49 | - public static function decrypt(string $ciphertext, string $auth_tag, | |
| 50 | - string $aad, string $key, string $iv): string | |
| 51 | -    { | |
| 52 | - $cipher = AESCipher::fromKeyLength(strlen($key)); | |
| 53 | -        if ($cipher->hasNativeCipher()) { | |
| 54 | - return $cipher->nativeDecrypt($ciphertext, $auth_tag, $aad, $key, $iv); | |
| 55 | - } | |
| 56 | - return self::_getGCM($cipher, strlen($auth_tag)) | |
| 57 | - ->decrypt($ciphertext, $auth_tag, $aad, $key, $iv); | |
| 58 | - } | |
| 36 | + /** | |
| 37 | + * Decrypt ciphertext. | |
| 38 | + * | |
| 39 | + * @param string $ciphertext Ciphertext to decrypt | |
| 40 | + * @param string $auth_tag Authentication tag to verify | |
| 41 | + * @param string $aad Additional authenticated data | |
| 42 | + * @param string $key Encryption key | |
| 43 | + * @param string $iv Initialization vector | |
| 44 | + * | |
| 45 | + * @throws \Sop\GCM\Exception\AuthenticationException If message authentication fails | |
| 46 | + * | |
| 47 | + * @return string Plaintext | |
| 48 | + */ | |
| 49 | + public static function decrypt(string $ciphertext, string $auth_tag, | |
| 50 | + string $aad, string $key, string $iv): string | |
| 51 | +	{ | |
| 52 | + $cipher = AESCipher::fromKeyLength(strlen($key)); | |
| 53 | +		if ($cipher->hasNativeCipher()) { | |
| 54 | + return $cipher->nativeDecrypt($ciphertext, $auth_tag, $aad, $key, $iv); | |
| 55 | + } | |
| 56 | + return self::_getGCM($cipher, strlen($auth_tag)) | |
| 57 | + ->decrypt($ciphertext, $auth_tag, $aad, $key, $iv); | |
| 58 | + } | |
| 59 | 59 | |
| 60 | - /** | |
| 61 | - * Get GCM instance. | |
| 62 | - * | |
| 63 | - * @param AESCipher $cipher Cipher instance | |
| 64 | - * @param int $tag_length Authentication tag length | |
| 65 | - * | |
| 66 | - * @return GCM | |
| 67 | - */ | |
| 68 | - protected static function _getGCM(AESCipher $cipher, int $tag_length): GCM | |
| 69 | -    { | |
| 70 | - return new GCM($cipher, $tag_length); | |
| 71 | - } | |
| 60 | + /** | |
| 61 | + * Get GCM instance. | |
| 62 | + * | |
| 63 | + * @param AESCipher $cipher Cipher instance | |
| 64 | + * @param int $tag_length Authentication tag length | |
| 65 | + * | |
| 66 | + * @return GCM | |
| 67 | + */ | |
| 68 | + protected static function _getGCM(AESCipher $cipher, int $tag_length): GCM | |
| 69 | +	{ | |
| 70 | + return new GCM($cipher, $tag_length); | |
| 71 | + } | |
| 72 | 72 | } | 
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM; | 
| 6 | 6 | |
| @@ -15,281 +15,281 @@ | ||
| 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 `C` and authentication tag `T` | |
| 87 | - */ | |
| 88 | - public function encrypt(string $P, string $A, string $K, string $IV): array | |
| 89 | -    { | |
| 90 | - $ghash = new GHASH($this->_cipher->encrypt(self::ZB_128, $K)); | |
| 91 | - // generate pre-counter block | |
| 92 | - $J0 = $this->_generateJ0($IV, $ghash); | |
| 93 | - // encrypt | |
| 94 | - $C = $this->_gctr(self::_inc32($J0), $P, $K); | |
| 95 | - // generate authentication tag | |
| 96 | - $T = $this->_computeAuthTag($A, $C, $J0, $K, $ghash); | |
| 97 | - return [$C, $T]; | |
| 98 | - } | |
| 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 `C` and authentication tag `T` | |
| 87 | + */ | |
| 88 | + public function encrypt(string $P, string $A, string $K, string $IV): array | |
| 89 | +	{ | |
| 90 | + $ghash = new GHASH($this->_cipher->encrypt(self::ZB_128, $K)); | |
| 91 | + // generate pre-counter block | |
| 92 | + $J0 = $this->_generateJ0($IV, $ghash); | |
| 93 | + // encrypt | |
| 94 | + $C = $this->_gctr(self::_inc32($J0), $P, $K); | |
| 95 | + // generate authentication tag | |
| 96 | + $T = $this->_computeAuthTag($A, $C, $J0, $K, $ghash); | |
| 97 | + return [$C, $T]; | |
| 98 | + } | |
| 99 | 99 | |
| 100 | - /** | |
| 101 | - * Decrypt ciphertext. | |
| 102 | - * | |
| 103 | - * @param string $C Ciphertext | |
| 104 | - * @param string $T Authentication tag | |
| 105 | - * @param string $A Additional authenticated data | |
| 106 | - * @param string $K Encryption key | |
| 107 | - * @param string $IV Initialization vector | |
| 108 | - * | |
| 109 | - * @throws AuthenticationException If message authentication fails | |
| 110 | - * @throws \RuntimeException For generic errors | |
| 111 | - * | |
| 112 | - * @return string Plaintext `P` | |
| 113 | - */ | |
| 114 | - public function decrypt(string $C, string $T, string $A, string $K, string $IV): string | |
| 115 | -    { | |
| 116 | - $ghash = new GHASH($this->_cipher->encrypt(self::ZB_128, $K)); | |
| 117 | - // generate pre-counter block | |
| 118 | - $J0 = $this->_generateJ0($IV, $ghash); | |
| 119 | - // generate authentication tag | |
| 120 | - $T2 = $this->_computeAuthTag($A, $C, $J0, $K, $ghash); | |
| 121 | - // check that authentication tag matches | |
| 122 | -        if (!hash_equals($T2, $T)) { | |
| 123 | -            throw new AuthenticationException('Authentication failed.'); | |
| 124 | - } | |
| 125 | - // decrypt | |
| 126 | - return $this->_gctr(self::_inc32($J0), $C, $K); | |
| 127 | - } | |
| 100 | + /** | |
| 101 | + * Decrypt ciphertext. | |
| 102 | + * | |
| 103 | + * @param string $C Ciphertext | |
| 104 | + * @param string $T Authentication tag | |
| 105 | + * @param string $A Additional authenticated data | |
| 106 | + * @param string $K Encryption key | |
| 107 | + * @param string $IV Initialization vector | |
| 108 | + * | |
| 109 | + * @throws AuthenticationException If message authentication fails | |
| 110 | + * @throws \RuntimeException For generic errors | |
| 111 | + * | |
| 112 | + * @return string Plaintext `P` | |
| 113 | + */ | |
| 114 | + public function decrypt(string $C, string $T, string $A, string $K, string $IV): string | |
| 115 | +	{ | |
| 116 | + $ghash = new GHASH($this->_cipher->encrypt(self::ZB_128, $K)); | |
| 117 | + // generate pre-counter block | |
| 118 | + $J0 = $this->_generateJ0($IV, $ghash); | |
| 119 | + // generate authentication tag | |
| 120 | + $T2 = $this->_computeAuthTag($A, $C, $J0, $K, $ghash); | |
| 121 | + // check that authentication tag matches | |
| 122 | +		if (!hash_equals($T2, $T)) { | |
| 123 | +			throw new AuthenticationException('Authentication failed.'); | |
| 124 | + } | |
| 125 | + // decrypt | |
| 126 | + return $this->_gctr(self::_inc32($J0), $C, $K); | |
| 127 | + } | |
| 128 | 128 | |
| 129 | - /** | |
| 130 | - * Convert string to GMP number. | |
| 131 | - * | |
| 132 | - * String is interpreted as an unsigned integer with big endian order and | |
| 133 | - * the most significant byte first. | |
| 134 | - * | |
| 135 | - * @param string $data Binary data | |
| 136 | - * | |
| 137 | - * @return \GMP | |
| 138 | - */ | |
| 139 | - public static function strToGMP(string $data): \GMP | |
| 140 | -    { | |
| 141 | - $num = gmp_import($data, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 142 | -        assert($num instanceof \GMP, new \RuntimeException('gmp_import() failed.')); | |
| 143 | - return $num; | |
| 144 | - } | |
| 129 | + /** | |
| 130 | + * Convert string to GMP number. | |
| 131 | + * | |
| 132 | + * String is interpreted as an unsigned integer with big endian order and | |
| 133 | + * the most significant byte first. | |
| 134 | + * | |
| 135 | + * @param string $data Binary data | |
| 136 | + * | |
| 137 | + * @return \GMP | |
| 138 | + */ | |
| 139 | + public static function strToGMP(string $data): \GMP | |
| 140 | +	{ | |
| 141 | + $num = gmp_import($data, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 142 | +		assert($num instanceof \GMP, new \RuntimeException('gmp_import() failed.')); | |
| 143 | + return $num; | |
| 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(strlen($IV) << 3); | |
| 184 | - return $ghash($data); | |
| 185 | - } | |
| 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(strlen($IV) << 3); | |
| 184 | + return $ghash($data); | |
| 185 | + } | |
| 186 | 186 | |
| 187 | - /** | |
| 188 | - * Apply GCTR algorithm. | |
| 189 | - * | |
| 190 | - * See NIST SP-300-38D section 6.5 for the details. | |
| 191 | - * | |
| 192 | - * @param string $ICB Initial counter block | |
| 193 | - * @param string $X Input data | |
| 194 | - * @param string $K Encryption key | |
| 195 | - * | |
| 196 | - * @return string Output data | |
| 197 | - */ | |
| 198 | - private function _gctr(string $ICB, string $X, string $K): string | |
| 199 | -    { | |
| 200 | - // if data is an empty string, return an empty string | |
| 201 | -        if ('' === $X) { | |
| 202 | - return ''; | |
| 203 | - } | |
| 204 | - // number of blocks | |
| 205 | - $n = ceil(strlen($X) / 16); | |
| 206 | - $CB = $ICB; | |
| 207 | - $Y = ''; | |
| 208 | -        for ($i = 0; $i < $n - 1; ++$i) { | |
| 209 | - // plaintext block | |
| 210 | - $xi = substr($X, $i << 4, 16); | |
| 211 | - // encrypt block and append to Y | |
| 212 | - $Y .= $xi ^ $this->_cipher->encrypt($CB, $K); | |
| 213 | - // increment counter block | |
| 214 | - $CB = self::_inc32($CB); | |
| 215 | - } | |
| 216 | - // final block | |
| 217 | - $xn = substr($X, $i << 4); | |
| 218 | - // XOR against partial block | |
| 219 | - $Y .= $xn ^ substr($this->_cipher->encrypt($CB, $K), 0, strlen($xn)); | |
| 220 | - return $Y; | |
| 221 | - } | |
| 187 | + /** | |
| 188 | + * Apply GCTR algorithm. | |
| 189 | + * | |
| 190 | + * See NIST SP-300-38D section 6.5 for the details. | |
| 191 | + * | |
| 192 | + * @param string $ICB Initial counter block | |
| 193 | + * @param string $X Input data | |
| 194 | + * @param string $K Encryption key | |
| 195 | + * | |
| 196 | + * @return string Output data | |
| 197 | + */ | |
| 198 | + private function _gctr(string $ICB, string $X, string $K): string | |
| 199 | +	{ | |
| 200 | + // if data is an empty string, return an empty string | |
| 201 | +		if ('' === $X) { | |
| 202 | + return ''; | |
| 203 | + } | |
| 204 | + // number of blocks | |
| 205 | + $n = ceil(strlen($X) / 16); | |
| 206 | + $CB = $ICB; | |
| 207 | + $Y = ''; | |
| 208 | +		for ($i = 0; $i < $n - 1; ++$i) { | |
| 209 | + // plaintext block | |
| 210 | + $xi = substr($X, $i << 4, 16); | |
| 211 | + // encrypt block and append to Y | |
| 212 | + $Y .= $xi ^ $this->_cipher->encrypt($CB, $K); | |
| 213 | + // increment counter block | |
| 214 | + $CB = self::_inc32($CB); | |
| 215 | + } | |
| 216 | + // final block | |
| 217 | + $xn = substr($X, $i << 4); | |
| 218 | + // XOR against partial block | |
| 219 | + $Y .= $xn ^ substr($this->_cipher->encrypt($CB, $K), 0, strlen($xn)); | |
| 220 | + return $Y; | |
| 221 | + } | |
| 222 | 222 | |
| 223 | - /** | |
| 224 | - * Compute authentication tag. | |
| 225 | - * | |
| 226 | - * See NIST SP-300-38D section 7.1 steps 5-6 for the details. | |
| 227 | - * | |
| 228 | - * @param string $A Additional authenticated data | |
| 229 | - * @param string $C Ciphertext | |
| 230 | - * @param string $J0 Pre-counter block | |
| 231 | - * @param string $K Encryption key | |
| 232 | - * @param GHASH $ghash GHASH functor | |
| 233 | - * | |
| 234 | - * @return string Authentication tag `T` | |
| 235 | - */ | |
| 236 | - private function _computeAuthTag(string $A, string $C, string $J0, string $K, | |
| 237 | - GHASH $ghash): string | |
| 238 | -    { | |
| 239 | - $data = self::_pad128($A) . self::_pad128($C) . | |
| 240 | - self::_uint64(strlen($A) << 3) . self::_uint64(strlen($C) << 3); | |
| 241 | - $S = $ghash($data); | |
| 242 | - return substr($this->_gctr($J0, $S, $K), 0, $this->_tagLength); | |
| 243 | - } | |
| 223 | + /** | |
| 224 | + * Compute authentication tag. | |
| 225 | + * | |
| 226 | + * See NIST SP-300-38D section 7.1 steps 5-6 for the details. | |
| 227 | + * | |
| 228 | + * @param string $A Additional authenticated data | |
| 229 | + * @param string $C Ciphertext | |
| 230 | + * @param string $J0 Pre-counter block | |
| 231 | + * @param string $K Encryption key | |
| 232 | + * @param GHASH $ghash GHASH functor | |
| 233 | + * | |
| 234 | + * @return string Authentication tag `T` | |
| 235 | + */ | |
| 236 | + private function _computeAuthTag(string $A, string $C, string $J0, string $K, | |
| 237 | + GHASH $ghash): string | |
| 238 | +	{ | |
| 239 | + $data = self::_pad128($A) . self::_pad128($C) . | |
| 240 | + self::_uint64(strlen($A) << 3) . self::_uint64(strlen($C) << 3); | |
| 241 | + $S = $ghash($data); | |
| 242 | + return substr($this->_gctr($J0, $S, $K), 0, $this->_tagLength); | |
| 243 | + } | |
| 244 | 244 | |
| 245 | - /** | |
| 246 | - * Pad data to 128 bit block boundary. | |
| 247 | - * | |
| 248 | - * @param string $data | |
| 249 | - * | |
| 250 | - * @return string | |
| 251 | - */ | |
| 252 | - private static function _pad128(string $data): string | |
| 253 | -    { | |
| 254 | - $padlen = 16 - strlen($data) % 16; | |
| 255 | -        if (16 !== $padlen) { | |
| 256 | -            $data .= str_repeat("\0", $padlen); | |
| 257 | - } | |
| 258 | - return $data; | |
| 259 | - } | |
| 245 | + /** | |
| 246 | + * Pad data to 128 bit block boundary. | |
| 247 | + * | |
| 248 | + * @param string $data | |
| 249 | + * | |
| 250 | + * @return string | |
| 251 | + */ | |
| 252 | + private static function _pad128(string $data): string | |
| 253 | +	{ | |
| 254 | + $padlen = 16 - strlen($data) % 16; | |
| 255 | +		if (16 !== $padlen) { | |
| 256 | +			$data .= str_repeat("\0", $padlen); | |
| 257 | + } | |
| 258 | + return $data; | |
| 259 | + } | |
| 260 | 260 | |
| 261 | - /** | |
| 262 | - * Increment 32 rightmost bits of the counter block. | |
| 263 | - * | |
| 264 | - * See NIST SP-300-38D section 6.2 for the details. | |
| 265 | - * | |
| 266 | - * @param string $X | |
| 267 | - * | |
| 268 | - * @return string | |
| 269 | - */ | |
| 270 | - private static function _inc32(string $X): string | |
| 271 | -    { | |
| 272 | - $Y = substr($X, 0, -4); | |
| 273 | - // increment counter | |
| 274 | - $n = self::strToGMP(substr($X, -4)) + 1; | |
| 275 | - // wrap by using only the 32 rightmost bits | |
| 276 | - $Y .= substr(self::gmpToStr($n, 4), -4); | |
| 277 | - return $Y; | |
| 278 | - } | |
| 261 | + /** | |
| 262 | + * Increment 32 rightmost bits of the counter block. | |
| 263 | + * | |
| 264 | + * See NIST SP-300-38D section 6.2 for the details. | |
| 265 | + * | |
| 266 | + * @param string $X | |
| 267 | + * | |
| 268 | + * @return string | |
| 269 | + */ | |
| 270 | + private static function _inc32(string $X): string | |
| 271 | +	{ | |
| 272 | + $Y = substr($X, 0, -4); | |
| 273 | + // increment counter | |
| 274 | + $n = self::strToGMP(substr($X, -4)) + 1; | |
| 275 | + // wrap by using only the 32 rightmost bits | |
| 276 | + $Y .= substr(self::gmpToStr($n, 4), -4); | |
| 277 | + return $Y; | |
| 278 | + } | |
| 279 | 279 | |
| 280 | - /** | |
| 281 | - * Convert integer to 64 bit big endian binary string. | |
| 282 | - * | |
| 283 | - * @param int $num | |
| 284 | - * | |
| 285 | - * @return string | |
| 286 | - */ | |
| 287 | - private static function _uint64(int $num): string | |
| 288 | -    { | |
| 289 | - // truncate on 32 bit hosts | |
| 290 | -        if (PHP_INT_SIZE < 8) { | |
| 291 | -            return "\0\0\0\0" . pack('N', $num); | |
| 292 | - } | |
| 293 | -        return pack('J', $num); | |
| 294 | - } | |
| 280 | + /** | |
| 281 | + * Convert integer to 64 bit big endian binary string. | |
| 282 | + * | |
| 283 | + * @param int $num | |
| 284 | + * | |
| 285 | + * @return string | |
| 286 | + */ | |
| 287 | + private static function _uint64(int $num): string | |
| 288 | +	{ | |
| 289 | + // truncate on 32 bit hosts | |
| 290 | +		if (PHP_INT_SIZE < 8) { | |
| 291 | +			return "\0\0\0\0" . pack('N', $num); | |
| 292 | + } | |
| 293 | +		return pack('J', $num); | |
| 294 | + } | |
| 295 | 295 | } | 
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM\Cipher\AES; | 
| 6 | 6 | |
| @@ -9,27 +9,27 @@ | ||
| 9 | 9 | */ | 
| 10 | 10 | class AES256Cipher extends AESCipher | 
| 11 | 11 |  { | 
| 12 | - /** | |
| 13 | -     * {@inheritdoc} | |
| 14 | - */ | |
| 15 | - protected function _cipherName(): string | |
| 16 | -    { | |
| 17 | - return 'aes-256-ecb'; | |
| 18 | - } | |
| 12 | + /** | |
| 13 | +	 * {@inheritdoc} | |
| 14 | + */ | |
| 15 | + protected function _cipherName(): string | |
| 16 | +	{ | |
| 17 | + return 'aes-256-ecb'; | |
| 18 | + } | |
| 19 | 19 | |
| 20 | - /** | |
| 21 | -     * {@inheritdoc} | |
| 22 | - */ | |
| 23 | - protected function _nativeCipherName(): string | |
| 24 | -    { | |
| 25 | - return 'aes-256-gcm'; | |
| 26 | - } | |
| 20 | + /** | |
| 21 | +	 * {@inheritdoc} | |
| 22 | + */ | |
| 23 | + protected function _nativeCipherName(): string | |
| 24 | +	{ | |
| 25 | + return 'aes-256-gcm'; | |
| 26 | + } | |
| 27 | 27 | |
| 28 | - /** | |
| 29 | -     * {@inheritdoc} | |
| 30 | - */ | |
| 31 | - protected function _keySize(): int | |
| 32 | -    { | |
| 33 | - return 32; | |
| 34 | - } | |
| 28 | + /** | |
| 29 | +	 * {@inheritdoc} | |
| 30 | + */ | |
| 31 | + protected function _keySize(): int | |
| 32 | +	{ | |
| 33 | + return 32; | |
| 34 | + } | |
| 35 | 35 | } | 
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM\Cipher\AES; | 
| 6 | 6 | |
| @@ -9,27 +9,27 @@ | ||
| 9 | 9 | */ | 
| 10 | 10 | class AES192Cipher extends AESCipher | 
| 11 | 11 |  { | 
| 12 | - /** | |
| 13 | -     * {@inheritdoc} | |
| 14 | - */ | |
| 15 | - protected function _cipherName(): string | |
| 16 | -    { | |
| 17 | - return 'aes-192-ecb'; | |
| 18 | - } | |
| 12 | + /** | |
| 13 | +	 * {@inheritdoc} | |
| 14 | + */ | |
| 15 | + protected function _cipherName(): string | |
| 16 | +	{ | |
| 17 | + return 'aes-192-ecb'; | |
| 18 | + } | |
| 19 | 19 | |
| 20 | - /** | |
| 21 | -     * {@inheritdoc} | |
| 22 | - */ | |
| 23 | - protected function _nativeCipherName(): string | |
| 24 | -    { | |
| 25 | - return 'aes-192-gcm'; | |
| 26 | - } | |
| 20 | + /** | |
| 21 | +	 * {@inheritdoc} | |
| 22 | + */ | |
| 23 | + protected function _nativeCipherName(): string | |
| 24 | +	{ | |
| 25 | + return 'aes-192-gcm'; | |
| 26 | + } | |
| 27 | 27 | |
| 28 | - /** | |
| 29 | -     * {@inheritdoc} | |
| 30 | - */ | |
| 31 | - protected function _keySize(): int | |
| 32 | -    { | |
| 33 | - return 24; | |
| 34 | - } | |
| 28 | + /** | |
| 29 | +	 * {@inheritdoc} | |
| 30 | + */ | |
| 31 | + protected function _keySize(): int | |
| 32 | +	{ | |
| 33 | + return 24; | |
| 34 | + } | |
| 35 | 35 | } | 
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\GCM\Cipher\AES; | 
| 6 | 6 | |
| @@ -9,27 +9,27 @@ | ||
| 9 | 9 | */ | 
| 10 | 10 | class AES128Cipher extends AESCipher | 
| 11 | 11 |  { | 
| 12 | - /** | |
| 13 | -     * {@inheritdoc} | |
| 14 | - */ | |
| 15 | - protected function _cipherName(): string | |
| 16 | -    { | |
| 17 | - return 'aes-128-ecb'; | |
| 18 | - } | |
| 12 | + /** | |
| 13 | +	 * {@inheritdoc} | |
| 14 | + */ | |
| 15 | + protected function _cipherName(): string | |
| 16 | +	{ | |
| 17 | + return 'aes-128-ecb'; | |
| 18 | + } | |
| 19 | 19 | |
| 20 | - /** | |
| 21 | -     * {@inheritdoc} | |
| 22 | - */ | |
| 23 | - protected function _nativeCipherName(): string | |
| 24 | -    { | |
| 25 | - return 'aes-128-gcm'; | |
| 26 | - } | |
| 20 | + /** | |
| 21 | +	 * {@inheritdoc} | |
| 22 | + */ | |
| 23 | + protected function _nativeCipherName(): string | |
| 24 | +	{ | |
| 25 | + return 'aes-128-gcm'; | |
| 26 | + } | |
| 27 | 27 | |
| 28 | - /** | |
| 29 | -     * {@inheritdoc} | |
| 30 | - */ | |
| 31 | - protected function _keySize(): int | |
| 32 | -    { | |
| 33 | - return 16; | |
| 34 | - } | |
| 28 | + /** | |
| 29 | +	 * {@inheritdoc} | |
| 30 | + */ | |
| 31 | + protected function _keySize(): int | |
| 32 | +	{ | |
| 33 | + return 16; | |
| 34 | + } | |
| 35 | 35 | } |