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