@@ -23,4 +23,4 @@ |
||
23 | 23 | // print the ciphertext along with the authentication tag |
24 | 24 | // and the initialization vector |
25 | 25 | echo bin2hex($ciphertext) . "\n" . bin2hex($auth_tag) . "\n" . bin2hex($iv) . |
26 | - "\n"; |
|
26 | + "\n"; |
@@ -14,7 +14,7 @@ |
||
14 | 14 | $key = "012345678901234567890123"; |
15 | 15 | // read ciphertext, authentication tag and initialization vector from the stdin |
16 | 16 | list($ciphertext, $auth_tag, $iv) = array_map("hex2bin", |
17 | - file("php://stdin", FILE_IGNORE_NEW_LINES)); |
|
17 | + file("php://stdin", FILE_IGNORE_NEW_LINES)); |
|
18 | 18 | // configure GCM object with AES-192 cipher and 13-bytes long authentication tag |
19 | 19 | $gcm = new GCM(new AES192Cipher(), 13); |
20 | 20 | // decrypt and authenticate |
@@ -16,7 +16,7 @@ |
||
16 | 16 | $key = "some 128 bit key"; |
17 | 17 | // read ciphertext, authentication tag and initialization vector from the stdin |
18 | 18 | list($ciphertext, $auth_tag, $iv) = array_map("hex2bin", |
19 | - file("php://stdin", FILE_IGNORE_NEW_LINES)); |
|
19 | + file("php://stdin", FILE_IGNORE_NEW_LINES)); |
|
20 | 20 | // decrypt and authenticate |
21 | 21 | $plaintext = AESGCM::decrypt($ciphertext, $auth_tag, $aad, $key, $iv); |
22 | 22 | echo "$plaintext\n"; |
@@ -23,4 +23,4 @@ |
||
23 | 23 | // print the ciphertext along with the authentication tag |
24 | 24 | // and the initialization vector |
25 | 25 | echo bin2hex($ciphertext) . "\n" . bin2hex($auth_tag) . "\n" . bin2hex($iv) . |
26 | - "\n"; |
|
26 | + "\n"; |
@@ -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 <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 | } |
@@ -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 |
@@ -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 |
@@ -11,88 +11,88 @@ |
||
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 | } |
@@ -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,19 +9,19 @@ |
||
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 | } |
@@ -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,19 +9,19 @@ |
||
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 | } |
@@ -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 |