@@ -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"; |
@@ -7,12 +7,12 @@ |
||
7 | 7 | */ |
8 | 8 | interface Cipher |
9 | 9 | { |
10 | - /** |
|
11 | - * Encrypt data. |
|
12 | - * |
|
13 | - * @param string $data Data to encrypt |
|
14 | - * @param string $key Encryption key |
|
15 | - * @return string Encrypted data |
|
16 | - */ |
|
17 | - public function encrypt($data, $key); |
|
10 | + /** |
|
11 | + * Encrypt data. |
|
12 | + * |
|
13 | + * @param string $data Data to encrypt |
|
14 | + * @param string $key Encryption key |
|
15 | + * @return string Encrypted data |
|
16 | + */ |
|
17 | + public function encrypt($data, $key); |
|
18 | 18 | } |
@@ -9,87 +9,87 @@ |
||
9 | 9 | */ |
10 | 10 | abstract class AESCipher implements Cipher |
11 | 11 | { |
12 | - /** |
|
13 | - * Mapping from key size in bits to AES cipher implementation class name. |
|
14 | - * |
|
15 | - * @internal |
|
16 | - * |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - const MAP_KEYSIZE_TO_CLS = array( |
|
20 | - /* @formatter:off */ |
|
21 | - 128 => AES128Cipher::class, |
|
22 | - 192 => AES192Cipher::class, |
|
23 | - 256 => AES256Cipher::class |
|
24 | - /* @formatter:on */ |
|
25 | - ); |
|
12 | + /** |
|
13 | + * Mapping from key size in bits to AES cipher implementation class name. |
|
14 | + * |
|
15 | + * @internal |
|
16 | + * |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + const MAP_KEYSIZE_TO_CLS = array( |
|
20 | + /* @formatter:off */ |
|
21 | + 128 => AES128Cipher::class, |
|
22 | + 192 => AES192Cipher::class, |
|
23 | + 256 => AES256Cipher::class |
|
24 | + /* @formatter:on */ |
|
25 | + ); |
|
26 | 26 | |
27 | - /** |
|
28 | - * Get the cipher method name recognized by OpenSSL. |
|
29 | - * |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - abstract protected function _cipherName(); |
|
27 | + /** |
|
28 | + * Get the cipher method name recognized by OpenSSL. |
|
29 | + * |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + abstract protected function _cipherName(); |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get the key size in bytes. |
|
36 | - * |
|
37 | - * @return int |
|
38 | - */ |
|
39 | - abstract protected function _keySize(); |
|
34 | + /** |
|
35 | + * Get the key size in bytes. |
|
36 | + * |
|
37 | + * @return int |
|
38 | + */ |
|
39 | + abstract protected function _keySize(); |
|
40 | 40 | |
41 | - /** |
|
42 | - * Get AES cipher instance by key length. |
|
43 | - * |
|
44 | - * @param int $len Key length in bytes |
|
45 | - * @throws \UnexpectedValueException |
|
46 | - * @return self |
|
47 | - */ |
|
48 | - public static function fromKeyLength($len) |
|
49 | - { |
|
50 | - $bits = $len << 3; |
|
51 | - if (!array_key_exists($bits, self::MAP_KEYSIZE_TO_CLS)) { |
|
52 | - throw new \UnexpectedValueException( |
|
53 | - "No AES implementation for $bits-bit key size."); |
|
54 | - } |
|
55 | - $cls = self::MAP_KEYSIZE_TO_CLS[$bits]; |
|
56 | - return new $cls(); |
|
57 | - } |
|
41 | + /** |
|
42 | + * Get AES cipher instance by key length. |
|
43 | + * |
|
44 | + * @param int $len Key length in bytes |
|
45 | + * @throws \UnexpectedValueException |
|
46 | + * @return self |
|
47 | + */ |
|
48 | + public static function fromKeyLength($len) |
|
49 | + { |
|
50 | + $bits = $len << 3; |
|
51 | + if (!array_key_exists($bits, self::MAP_KEYSIZE_TO_CLS)) { |
|
52 | + throw new \UnexpectedValueException( |
|
53 | + "No AES implementation for $bits-bit key size."); |
|
54 | + } |
|
55 | + $cls = self::MAP_KEYSIZE_TO_CLS[$bits]; |
|
56 | + return new $cls(); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * |
|
61 | - * @see \GCM\Cipher\Cipher::encrypt() |
|
62 | - * @throws \UnexpectedValueException If key size is incorrect |
|
63 | - * @throws \RuntimeException For generic errors |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - public function encrypt($data, $key) |
|
67 | - { |
|
68 | - $key_size = $this->_keySize(); |
|
69 | - if (strlen($key) != $key_size) { |
|
70 | - throw new \UnexpectedValueException( |
|
71 | - "Key size must be $key_size bytes."); |
|
72 | - } |
|
73 | - $result = openssl_encrypt($data, $this->_cipherName(), $key, |
|
74 | - OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
|
75 | - if (false === $result) { |
|
76 | - throw new \RuntimeException( |
|
77 | - "openssl_encrypt() failed: " . self::_getLastOpenSSLError()); |
|
78 | - } |
|
79 | - return $result; |
|
80 | - } |
|
59 | + /** |
|
60 | + * |
|
61 | + * @see \GCM\Cipher\Cipher::encrypt() |
|
62 | + * @throws \UnexpectedValueException If key size is incorrect |
|
63 | + * @throws \RuntimeException For generic errors |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + public function encrypt($data, $key) |
|
67 | + { |
|
68 | + $key_size = $this->_keySize(); |
|
69 | + if (strlen($key) != $key_size) { |
|
70 | + throw new \UnexpectedValueException( |
|
71 | + "Key size must be $key_size bytes."); |
|
72 | + } |
|
73 | + $result = openssl_encrypt($data, $this->_cipherName(), $key, |
|
74 | + OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
|
75 | + if (false === $result) { |
|
76 | + throw new \RuntimeException( |
|
77 | + "openssl_encrypt() failed: " . self::_getLastOpenSSLError()); |
|
78 | + } |
|
79 | + return $result; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Get latest OpenSSL error message. |
|
84 | - * |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - protected static function _getLastOpenSSLError() |
|
88 | - { |
|
89 | - $msg = null; |
|
90 | - while (false !== ($err = openssl_error_string())) { |
|
91 | - $msg = $err; |
|
92 | - } |
|
93 | - return $msg; |
|
94 | - } |
|
82 | + /** |
|
83 | + * Get latest OpenSSL error message. |
|
84 | + * |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + protected static function _getLastOpenSSLError() |
|
88 | + { |
|
89 | + $msg = null; |
|
90 | + while (false !== ($err = openssl_error_string())) { |
|
91 | + $msg = $err; |
|
92 | + } |
|
93 | + return $msg; |
|
94 | + } |
|
95 | 95 | } |
@@ -7,21 +7,21 @@ |
||
7 | 7 | */ |
8 | 8 | class AES256Cipher extends AESCipher |
9 | 9 | { |
10 | - /** |
|
11 | - * |
|
12 | - * {@inheritdoc} |
|
13 | - */ |
|
14 | - protected function _cipherName() |
|
15 | - { |
|
16 | - return "AES-256-ECB"; |
|
17 | - } |
|
10 | + /** |
|
11 | + * |
|
12 | + * {@inheritdoc} |
|
13 | + */ |
|
14 | + protected function _cipherName() |
|
15 | + { |
|
16 | + return "AES-256-ECB"; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected function _keySize() |
|
24 | - { |
|
25 | - return 32; |
|
26 | - } |
|
19 | + /** |
|
20 | + * |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected function _keySize() |
|
24 | + { |
|
25 | + return 32; |
|
26 | + } |
|
27 | 27 | } |
@@ -7,21 +7,21 @@ |
||
7 | 7 | */ |
8 | 8 | class AES192Cipher extends AESCipher |
9 | 9 | { |
10 | - /** |
|
11 | - * |
|
12 | - * {@inheritdoc} |
|
13 | - */ |
|
14 | - protected function _cipherName() |
|
15 | - { |
|
16 | - return "AES-192-ECB"; |
|
17 | - } |
|
10 | + /** |
|
11 | + * |
|
12 | + * {@inheritdoc} |
|
13 | + */ |
|
14 | + protected function _cipherName() |
|
15 | + { |
|
16 | + return "AES-192-ECB"; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected function _keySize() |
|
24 | - { |
|
25 | - return 24; |
|
26 | - } |
|
19 | + /** |
|
20 | + * |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected function _keySize() |
|
24 | + { |
|
25 | + return 24; |
|
26 | + } |
|
27 | 27 | } |
@@ -7,21 +7,21 @@ |
||
7 | 7 | */ |
8 | 8 | class AES128Cipher extends AESCipher |
9 | 9 | { |
10 | - /** |
|
11 | - * |
|
12 | - * {@inheritdoc} |
|
13 | - */ |
|
14 | - protected function _cipherName() |
|
15 | - { |
|
16 | - return "AES-128-ECB"; |
|
17 | - } |
|
10 | + /** |
|
11 | + * |
|
12 | + * {@inheritdoc} |
|
13 | + */ |
|
14 | + protected function _cipherName() |
|
15 | + { |
|
16 | + return "AES-128-ECB"; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected function _keySize() |
|
24 | - { |
|
25 | - return 16; |
|
26 | - } |
|
19 | + /** |
|
20 | + * |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected function _keySize() |
|
24 | + { |
|
25 | + return 16; |
|
26 | + } |
|
27 | 27 | } |