GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — php72 ( 18e574...be7a5a )
by Joni
01:52
created
lib/GCM/GHASH.php 2 patches
Indentation   +88 added lines, -88 removed lines patch added patch discarded remove patch
@@ -13,97 +13,97 @@
 block discarded – undo
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 <code>compute</code>.
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 <code>compute</code>.
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
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\GCM;
6 6
 
Please login to merge, or discard this patch.
lib/GCM/Cipher/Cipher.php 2 patches
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -9,13 +9,13 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
lib/GCM/Cipher/AES/AESCipher.php 2 patches
Indentation   +79 added lines, -79 removed lines patch added patch discarded remove patch
@@ -11,88 +11,88 @@
 block discarded – undo
11 11
  */
12 12
 abstract class AESCipher implements Cipher
13 13
 {
14
-    /**
15
-     * Mapping from key size in bits to AES cipher implementation class name.
16
-     *
17
-     * @internal
18
-     *
19
-     * @var array
20
-     */
21
-    const MAP_KEYSIZE_TO_CLS = [
22
-        128 => AES128Cipher::class,
23
-        192 => AES192Cipher::class,
24
-        256 => AES256Cipher::class,
25
-    ];
14
+	/**
15
+	 * Mapping from key size in bits to AES cipher implementation class name.
16
+	 *
17
+	 * @internal
18
+	 *
19
+	 * @var array
20
+	 */
21
+	const MAP_KEYSIZE_TO_CLS = [
22
+		128 => AES128Cipher::class,
23
+		192 => AES192Cipher::class,
24
+		256 => AES256Cipher::class,
25
+	];
26 26
 
27
-    /**
28
-     * Get AES cipher instance by key length.
29
-     *
30
-     * @param int $len Key length in bytes
31
-     *
32
-     * @throws \UnexpectedValueException
33
-     *
34
-     * @return self
35
-     */
36
-    public static function fromKeyLength(int $len): self
37
-    {
38
-        $bits = $len << 3;
39
-        if (!array_key_exists($bits, self::MAP_KEYSIZE_TO_CLS)) {
40
-            throw new \UnexpectedValueException(
41
-                "No AES implementation for ${bits}-bit key size.");
42
-        }
43
-        $cls = self::MAP_KEYSIZE_TO_CLS[$bits];
44
-        return new $cls();
45
-    }
27
+	/**
28
+	 * Get AES cipher instance by key length.
29
+	 *
30
+	 * @param int $len Key length in bytes
31
+	 *
32
+	 * @throws \UnexpectedValueException
33
+	 *
34
+	 * @return self
35
+	 */
36
+	public static function fromKeyLength(int $len): self
37
+	{
38
+		$bits = $len << 3;
39
+		if (!array_key_exists($bits, self::MAP_KEYSIZE_TO_CLS)) {
40
+			throw new \UnexpectedValueException(
41
+				"No AES implementation for ${bits}-bit key size.");
42
+		}
43
+		$cls = self::MAP_KEYSIZE_TO_CLS[$bits];
44
+		return new $cls();
45
+	}
46 46
 
47
-    /**
48
-     * @see \Sop\GCM\Cipher\Cipher::encrypt()
49
-     *
50
-     * @throws \UnexpectedValueException If key size is incorrect
51
-     * @throws \RuntimeException         For generic errors
52
-     *
53
-     * @return string
54
-     */
55
-    public function encrypt(string $data, string $key): string
56
-    {
57
-        $key_size = $this->_keySize();
58
-        if (strlen($key) != $key_size) {
59
-            throw new \UnexpectedValueException(
60
-                "Key size must be ${key_size} bytes.");
61
-        }
62
-        $result = openssl_encrypt($data, $this->_cipherName(), $key,
63
-            OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
64
-        if (false === $result) {
65
-            throw new \RuntimeException(
66
-                'openssl_encrypt() failed: ' . self::_getLastOpenSSLError());
67
-        }
68
-        return $result;
69
-    }
47
+	/**
48
+	 * @see \Sop\GCM\Cipher\Cipher::encrypt()
49
+	 *
50
+	 * @throws \UnexpectedValueException If key size is incorrect
51
+	 * @throws \RuntimeException         For generic errors
52
+	 *
53
+	 * @return string
54
+	 */
55
+	public function encrypt(string $data, string $key): string
56
+	{
57
+		$key_size = $this->_keySize();
58
+		if (strlen($key) != $key_size) {
59
+			throw new \UnexpectedValueException(
60
+				"Key size must be ${key_size} bytes.");
61
+		}
62
+		$result = openssl_encrypt($data, $this->_cipherName(), $key,
63
+			OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
64
+		if (false === $result) {
65
+			throw new \RuntimeException(
66
+				'openssl_encrypt() failed: ' . self::_getLastOpenSSLError());
67
+		}
68
+		return $result;
69
+	}
70 70
 
71
-    /**
72
-     * Get the cipher method name recognized by OpenSSL.
73
-     *
74
-     * @return string
75
-     */
76
-    abstract protected function _cipherName(): string;
71
+	/**
72
+	 * Get the cipher method name recognized by OpenSSL.
73
+	 *
74
+	 * @return string
75
+	 */
76
+	abstract protected function _cipherName(): string;
77 77
 
78
-    /**
79
-     * Get the key size in bytes.
80
-     *
81
-     * @return int
82
-     */
83
-    abstract protected function _keySize(): int;
78
+	/**
79
+	 * Get the key size in bytes.
80
+	 *
81
+	 * @return int
82
+	 */
83
+	abstract protected function _keySize(): int;
84 84
 
85
-    /**
86
-     * Get latest OpenSSL error message.
87
-     *
88
-     * @return string
89
-     */
90
-    protected static function _getLastOpenSSLError(): string
91
-    {
92
-        $msg = '';
93
-        while (false !== ($err = openssl_error_string())) {
94
-            $msg = $err;
95
-        }
96
-        return $msg;
97
-    }
85
+	/**
86
+	 * Get latest OpenSSL error message.
87
+	 *
88
+	 * @return string
89
+	 */
90
+	protected static function _getLastOpenSSLError(): string
91
+	{
92
+		$msg = '';
93
+		while (false !== ($err = openssl_error_string())) {
94
+			$msg = $err;
95
+		}
96
+		return $msg;
97
+	}
98 98
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
lib/GCM/Cipher/AES/AES128Cipher.php 2 patches
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -9,19 +9,19 @@
 block discarded – undo
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 _keySize(): int
24
-    {
25
-        return 16;
26
-    }
20
+	/**
21
+	 * {@inheritdoc}
22
+	 */
23
+	protected function _keySize(): int
24
+	{
25
+		return 16;
26
+	}
27 27
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
lib/GCM/Cipher/AES/AES256Cipher.php 2 patches
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -9,19 +9,19 @@
 block discarded – undo
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 _keySize(): int
24
-    {
25
-        return 32;
26
-    }
20
+	/**
21
+	 * {@inheritdoc}
22
+	 */
23
+	protected function _keySize(): int
24
+	{
25
+		return 32;
26
+	}
27 27
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
lib/GCM/Cipher/AES/AES192Cipher.php 2 patches
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -9,19 +9,19 @@
 block discarded – undo
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 _keySize(): int
24
-    {
25
-        return 24;
26
-    }
20
+	/**
21
+	 * {@inheritdoc}
22
+	 */
23
+	protected function _keySize(): int
24
+	{
25
+		return 24;
26
+	}
27 27
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
lib/GCM/Exception/AuthenticationException.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
lib/GCM/AESGCM.php 2 patches
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -11,51 +11,51 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\GCM;
6 6
 
Please login to merge, or discard this patch.
lib/GCM/GCM.php 2 patches
Indentation   +265 added lines, -265 removed lines patch added patch discarded remove patch
@@ -15,284 +15,284 @@
 block discarded – undo
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
-        $num = gmp_import($data, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
144
-        assert($num instanceof \GMP, new \RuntimeException('gmp_import() failed.'));
145
-        return $num;
146
-    }
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
+		$num = gmp_import($data, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
144
+		assert($num instanceof \GMP, new \RuntimeException('gmp_import() failed.'));
145
+		return $num;
146
+	}
147 147
 
148
-    /**
149
-     * Convert GMP number to string.
150
-     *
151
-     * Returned string represents an unsigned integer with big endian order and
152
-     * the most significant byte first.
153
-     *
154
-     * @param \GMP $num  GMP number
155
-     * @param int  $size Width of the string in bytes
156
-     *
157
-     * @return string Binary data
158
-     */
159
-    public static function gmpToStr(\GMP $num, int $size): string
160
-    {
161
-        $data = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
162
-        $len = strlen($data);
163
-        if ($len < $size) {
164
-            $data = str_repeat("\0", $size - $len) . $data;
165
-        }
166
-        return $data;
167
-    }
148
+	/**
149
+	 * Convert GMP number to string.
150
+	 *
151
+	 * Returned string represents an unsigned integer with big endian order and
152
+	 * the most significant byte first.
153
+	 *
154
+	 * @param \GMP $num  GMP number
155
+	 * @param int  $size Width of the string in bytes
156
+	 *
157
+	 * @return string Binary data
158
+	 */
159
+	public static function gmpToStr(\GMP $num, int $size): string
160
+	{
161
+		$data = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
162
+		$len = strlen($data);
163
+		if ($len < $size) {
164
+			$data = str_repeat("\0", $size - $len) . $data;
165
+		}
166
+		return $data;
167
+	}
168 168
 
169
-    /**
170
-     * Generate pre-counter block.
171
-     *
172
-     * See NIST SP-300-38D section 7.1 step 2 for the details.
173
-     *
174
-     * @param string $IV    Initialization vector
175
-     * @param GHASH  $ghash GHASH functor
176
-     *
177
-     * @return string
178
-     */
179
-    private function _generateJ0(string $IV, GHASH $ghash): string
180
-    {
181
-        // if len(IV) = 96
182
-        if (12 == strlen($IV)) {
183
-            return $IV . "\0\0\0\1";
184
-        }
185
-        $data = self::_pad128($IV) . self::ZB_64 . self::_uint64(
186
-            strlen($IV) << 3);
187
-        return $ghash($data);
188
-    }
169
+	/**
170
+	 * Generate pre-counter block.
171
+	 *
172
+	 * See NIST SP-300-38D section 7.1 step 2 for the details.
173
+	 *
174
+	 * @param string $IV    Initialization vector
175
+	 * @param GHASH  $ghash GHASH functor
176
+	 *
177
+	 * @return string
178
+	 */
179
+	private function _generateJ0(string $IV, GHASH $ghash): string
180
+	{
181
+		// if len(IV) = 96
182
+		if (12 == strlen($IV)) {
183
+			return $IV . "\0\0\0\1";
184
+		}
185
+		$data = self::_pad128($IV) . self::ZB_64 . self::_uint64(
186
+			strlen($IV) << 3);
187
+		return $ghash($data);
188
+	}
189 189
 
190
-    /**
191
-     * Apply GCTR algorithm.
192
-     *
193
-     * See NIST SP-300-38D section 6.5 for the details.
194
-     *
195
-     * @param string $ICB Initial counter block
196
-     * @param string $X   Input data
197
-     * @param string $K   Encryption key
198
-     *
199
-     * @return string Output data
200
-     */
201
-    private function _gctr(string $ICB, string $X, string $K): string
202
-    {
203
-        // if data is an empty string, return an empty string
204
-        if ('' == $X) {
205
-            return '';
206
-        }
207
-        // number of blocks
208
-        $n = ceil(strlen($X) / 16);
209
-        $CB = $ICB;
210
-        $Y = '';
211
-        for ($i = 0; $i < $n - 1; ++$i) {
212
-            // plaintext block
213
-            $xi = substr($X, $i << 4, 16);
214
-            // encrypt block and append to Y
215
-            $Y .= $xi ^ $this->_cipher->encrypt($CB, $K);
216
-            // increment counter block
217
-            $CB = self::_inc32($CB);
218
-        }
219
-        // final block
220
-        $xn = substr($X, $i << 4);
221
-        // XOR against partial block
222
-        $Y .= $xn ^ substr($this->_cipher->encrypt($CB, $K), 0, strlen($xn));
223
-        return $Y;
224
-    }
190
+	/**
191
+	 * Apply GCTR algorithm.
192
+	 *
193
+	 * See NIST SP-300-38D section 6.5 for the details.
194
+	 *
195
+	 * @param string $ICB Initial counter block
196
+	 * @param string $X   Input data
197
+	 * @param string $K   Encryption key
198
+	 *
199
+	 * @return string Output data
200
+	 */
201
+	private function _gctr(string $ICB, string $X, string $K): string
202
+	{
203
+		// if data is an empty string, return an empty string
204
+		if ('' == $X) {
205
+			return '';
206
+		}
207
+		// number of blocks
208
+		$n = ceil(strlen($X) / 16);
209
+		$CB = $ICB;
210
+		$Y = '';
211
+		for ($i = 0; $i < $n - 1; ++$i) {
212
+			// plaintext block
213
+			$xi = substr($X, $i << 4, 16);
214
+			// encrypt block and append to Y
215
+			$Y .= $xi ^ $this->_cipher->encrypt($CB, $K);
216
+			// increment counter block
217
+			$CB = self::_inc32($CB);
218
+		}
219
+		// final block
220
+		$xn = substr($X, $i << 4);
221
+		// XOR against partial block
222
+		$Y .= $xn ^ substr($this->_cipher->encrypt($CB, $K), 0, strlen($xn));
223
+		return $Y;
224
+	}
225 225
 
226
-    /**
227
-     * Compute authentication tag.
228
-     *
229
-     * See NIST SP-300-38D section 7.1 steps 5-6 for the details.
230
-     *
231
-     * @param string $A     Additional authenticated data
232
-     * @param string $C     Ciphertext
233
-     * @param string $J0    Pre-counter block
234
-     * @param string $K     Encryption key
235
-     * @param GHASH  $ghash GHASH functor
236
-     *
237
-     * @return string Authentication tag <code>T</code>
238
-     */
239
-    private function _computeAuthTag(string $A, string $C, string $J0, string $K,
240
-        GHASH $ghash): string
241
-    {
242
-        $data = self::_pad128($A) . self::_pad128($C) .
243
-             self::_uint64(strlen($A) << 3) . self::_uint64(strlen($C) << 3);
244
-        $S = $ghash($data);
245
-        return substr($this->_gctr($J0, $S, $K), 0, $this->_tagLength);
246
-    }
226
+	/**
227
+	 * Compute authentication tag.
228
+	 *
229
+	 * See NIST SP-300-38D section 7.1 steps 5-6 for the details.
230
+	 *
231
+	 * @param string $A     Additional authenticated data
232
+	 * @param string $C     Ciphertext
233
+	 * @param string $J0    Pre-counter block
234
+	 * @param string $K     Encryption key
235
+	 * @param GHASH  $ghash GHASH functor
236
+	 *
237
+	 * @return string Authentication tag <code>T</code>
238
+	 */
239
+	private function _computeAuthTag(string $A, string $C, string $J0, string $K,
240
+		GHASH $ghash): string
241
+	{
242
+		$data = self::_pad128($A) . self::_pad128($C) .
243
+			 self::_uint64(strlen($A) << 3) . self::_uint64(strlen($C) << 3);
244
+		$S = $ghash($data);
245
+		return substr($this->_gctr($J0, $S, $K), 0, $this->_tagLength);
246
+	}
247 247
 
248
-    /**
249
-     * Pad data to 128 bit block boundary.
250
-     *
251
-     * @param string $data
252
-     *
253
-     * @return string
254
-     */
255
-    private static function _pad128(string $data): string
256
-    {
257
-        $padlen = 16 - strlen($data) % 16;
258
-        if (16 != $padlen) {
259
-            $data .= str_repeat("\0", $padlen);
260
-        }
261
-        return $data;
262
-    }
248
+	/**
249
+	 * Pad data to 128 bit block boundary.
250
+	 *
251
+	 * @param string $data
252
+	 *
253
+	 * @return string
254
+	 */
255
+	private static function _pad128(string $data): string
256
+	{
257
+		$padlen = 16 - strlen($data) % 16;
258
+		if (16 != $padlen) {
259
+			$data .= str_repeat("\0", $padlen);
260
+		}
261
+		return $data;
262
+	}
263 263
 
264
-    /**
265
-     * Increment 32 rightmost bits of the counter block.
266
-     *
267
-     * See NIST SP-300-38D section 6.2 for the details.
268
-     *
269
-     * @param string $X
270
-     *
271
-     * @return string
272
-     */
273
-    private static function _inc32(string $X): string
274
-    {
275
-        $Y = substr($X, 0, -4);
276
-        // increment counter
277
-        $n = self::strToGMP(substr($X, -4)) + 1;
278
-        // wrap by using only the 32 rightmost bits
279
-        $Y .= substr(self::gmpToStr($n, 4), -4);
280
-        return $Y;
281
-    }
264
+	/**
265
+	 * Increment 32 rightmost bits of the counter block.
266
+	 *
267
+	 * See NIST SP-300-38D section 6.2 for the details.
268
+	 *
269
+	 * @param string $X
270
+	 *
271
+	 * @return string
272
+	 */
273
+	private static function _inc32(string $X): string
274
+	{
275
+		$Y = substr($X, 0, -4);
276
+		// increment counter
277
+		$n = self::strToGMP(substr($X, -4)) + 1;
278
+		// wrap by using only the 32 rightmost bits
279
+		$Y .= substr(self::gmpToStr($n, 4), -4);
280
+		return $Y;
281
+	}
282 282
 
283
-    /**
284
-     * Convert integer to 64 bit big endian binary string.
285
-     *
286
-     * @param int $num
287
-     *
288
-     * @return string
289
-     */
290
-    private static function _uint64(int $num): string
291
-    {
292
-        // truncate on 32 bit hosts
293
-        if (PHP_INT_SIZE < 8) {
294
-            return "\0\0\0\0" . pack('N', $num);
295
-        }
296
-        return pack('J', $num);
297
-    }
283
+	/**
284
+	 * Convert integer to 64 bit big endian binary string.
285
+	 *
286
+	 * @param int $num
287
+	 *
288
+	 * @return string
289
+	 */
290
+	private static function _uint64(int $num): string
291
+	{
292
+		// truncate on 32 bit hosts
293
+		if (PHP_INT_SIZE < 8) {
294
+			return "\0\0\0\0" . pack('N', $num);
295
+		}
296
+		return pack('J', $num);
297
+	}
298 298
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\GCM;
6 6
 
Please login to merge, or discard this patch.