@@ -9,20 +9,20 @@ |
||
9 | 9 | * Interface for algorithms that may be used to compress and decompress data. |
10 | 10 | */ |
11 | 11 | interface CompressionAlgorithm extends |
12 | - CompressionAlgorithmParameterValue, |
|
13 | - HeaderParameters |
|
12 | + CompressionAlgorithmParameterValue, |
|
13 | + HeaderParameters |
|
14 | 14 | { |
15 | - /** |
|
16 | - * Compress data. |
|
17 | - * |
|
18 | - * @param string $data Compressed data |
|
19 | - */ |
|
20 | - public function compress($data); |
|
15 | + /** |
|
16 | + * Compress data. |
|
17 | + * |
|
18 | + * @param string $data Compressed data |
|
19 | + */ |
|
20 | + public function compress($data); |
|
21 | 21 | |
22 | - /** |
|
23 | - * Decompress data. |
|
24 | - * |
|
25 | - * @param string $data Uncompressed data |
|
26 | - */ |
|
27 | - public function decompress($data); |
|
22 | + /** |
|
23 | + * Decompress data. |
|
24 | + * |
|
25 | + * @param string $data Uncompressed data |
|
26 | + */ |
|
27 | + public function decompress($data); |
|
28 | 28 | } |
@@ -10,50 +10,50 @@ |
||
10 | 10 | */ |
11 | 11 | abstract class CompressionFactory |
12 | 12 | { |
13 | - /** |
|
14 | - * Mapping from algorithm name to class name. |
|
15 | - * |
|
16 | - * @internal |
|
17 | - * |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - const MAP_ALGO_TO_CLASS = array( |
|
21 | - /* @formatter:off */ |
|
22 | - JWA::ALGO_DEFLATE => DeflateAlgorithm::class |
|
23 | - /* @formatter:on */ |
|
24 | - ); |
|
13 | + /** |
|
14 | + * Mapping from algorithm name to class name. |
|
15 | + * |
|
16 | + * @internal |
|
17 | + * |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + const MAP_ALGO_TO_CLASS = array( |
|
21 | + /* @formatter:off */ |
|
22 | + JWA::ALGO_DEFLATE => DeflateAlgorithm::class |
|
23 | + /* @formatter:on */ |
|
24 | + ); |
|
25 | 25 | |
26 | - /** |
|
27 | - * Get the compression algorithm by name. |
|
28 | - * |
|
29 | - * @param string $name |
|
30 | - * @throws \UnexpectedValueException If algorithm is not supported |
|
31 | - * @return \JWX\JWE\CompressionAlgorithm |
|
32 | - */ |
|
33 | - public static function algoByName($name) |
|
34 | - { |
|
35 | - if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
36 | - throw new \UnexpectedValueException( |
|
37 | - "No compression algorithm '$name'."); |
|
38 | - } |
|
39 | - $cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
40 | - return new $cls(); |
|
41 | - } |
|
26 | + /** |
|
27 | + * Get the compression algorithm by name. |
|
28 | + * |
|
29 | + * @param string $name |
|
30 | + * @throws \UnexpectedValueException If algorithm is not supported |
|
31 | + * @return \JWX\JWE\CompressionAlgorithm |
|
32 | + */ |
|
33 | + public static function algoByName($name) |
|
34 | + { |
|
35 | + if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
36 | + throw new \UnexpectedValueException( |
|
37 | + "No compression algorithm '$name'."); |
|
38 | + } |
|
39 | + $cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
40 | + return new $cls(); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the compression algorithm as specified in the given header. |
|
45 | - * |
|
46 | - * @param Header $header Header |
|
47 | - * @throws \UnexpectedValueException If compression algorithm parameter is |
|
48 | - * not present or algorithm is not supported |
|
49 | - * @return \JWX\JWE\CompressionAlgorithm |
|
50 | - */ |
|
51 | - public static function algoByHeader(Header $header) |
|
52 | - { |
|
53 | - if (!$header->hasCompressionAlgorithm()) { |
|
54 | - throw new \UnexpectedValueException( |
|
55 | - "No compression algorithm parameter."); |
|
56 | - } |
|
57 | - return self::algoByName($header->compressionAlgorithm()->value()); |
|
58 | - } |
|
43 | + /** |
|
44 | + * Get the compression algorithm as specified in the given header. |
|
45 | + * |
|
46 | + * @param Header $header Header |
|
47 | + * @throws \UnexpectedValueException If compression algorithm parameter is |
|
48 | + * not present or algorithm is not supported |
|
49 | + * @return \JWX\JWE\CompressionAlgorithm |
|
50 | + */ |
|
51 | + public static function algoByHeader(Header $header) |
|
52 | + { |
|
53 | + if (!$header->hasCompressionAlgorithm()) { |
|
54 | + throw new \UnexpectedValueException( |
|
55 | + "No compression algorithm parameter."); |
|
56 | + } |
|
57 | + return self::algoByName($header->compressionAlgorithm()->value()); |
|
58 | + } |
|
59 | 59 | } |
@@ -14,73 +14,73 @@ |
||
14 | 14 | */ |
15 | 15 | class DeflateAlgorithm implements CompressionAlgorithm |
16 | 16 | { |
17 | - /** |
|
18 | - * Compression level. |
|
19 | - * |
|
20 | - * @var int $_compressionLevel |
|
21 | - */ |
|
22 | - protected $_compressionLevel; |
|
17 | + /** |
|
18 | + * Compression level. |
|
19 | + * |
|
20 | + * @var int $_compressionLevel |
|
21 | + */ |
|
22 | + protected $_compressionLevel; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Constructor. |
|
26 | - * |
|
27 | - * @param int $level Compression level 0..9 |
|
28 | - */ |
|
29 | - public function __construct($level = -1) |
|
30 | - { |
|
31 | - if ($level < -1 || $level > 9) { |
|
32 | - throw new \DomainException("Compression level must be -1..9."); |
|
33 | - } |
|
34 | - $this->_compressionLevel = (int) $level; |
|
35 | - } |
|
24 | + /** |
|
25 | + * Constructor. |
|
26 | + * |
|
27 | + * @param int $level Compression level 0..9 |
|
28 | + */ |
|
29 | + public function __construct($level = -1) |
|
30 | + { |
|
31 | + if ($level < -1 || $level > 9) { |
|
32 | + throw new \DomainException("Compression level must be -1..9."); |
|
33 | + } |
|
34 | + $this->_compressionLevel = (int) $level; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * |
|
39 | - * @see \JWX\JWE\CompressionAlgorithm::compress() |
|
40 | - * @throws \RuntimeException |
|
41 | - */ |
|
42 | - public function compress($data) |
|
43 | - { |
|
44 | - $ret = @gzdeflate($data, $this->_compressionLevel); |
|
45 | - if (false === $ret) { |
|
46 | - $err = error_get_last(); |
|
47 | - $msg = isset($err) ? $err["message"] : "gzdeflate() failed."; |
|
48 | - throw new \RuntimeException($msg); |
|
49 | - } |
|
50 | - return $ret; |
|
51 | - } |
|
37 | + /** |
|
38 | + * |
|
39 | + * @see \JWX\JWE\CompressionAlgorithm::compress() |
|
40 | + * @throws \RuntimeException |
|
41 | + */ |
|
42 | + public function compress($data) |
|
43 | + { |
|
44 | + $ret = @gzdeflate($data, $this->_compressionLevel); |
|
45 | + if (false === $ret) { |
|
46 | + $err = error_get_last(); |
|
47 | + $msg = isset($err) ? $err["message"] : "gzdeflate() failed."; |
|
48 | + throw new \RuntimeException($msg); |
|
49 | + } |
|
50 | + return $ret; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * |
|
55 | - * @see \JWX\JWE\CompressionAlgorithm::decompress() |
|
56 | - * @throws \RuntimeException |
|
57 | - */ |
|
58 | - public function decompress($data) |
|
59 | - { |
|
60 | - $ret = @gzinflate($data); |
|
61 | - if (false === $ret) { |
|
62 | - $err = error_get_last(); |
|
63 | - $msg = isset($err) ? $err["message"] : "gzinflate() failed."; |
|
64 | - throw new \RuntimeException($msg); |
|
65 | - } |
|
66 | - return $ret; |
|
67 | - } |
|
53 | + /** |
|
54 | + * |
|
55 | + * @see \JWX\JWE\CompressionAlgorithm::decompress() |
|
56 | + * @throws \RuntimeException |
|
57 | + */ |
|
58 | + public function decompress($data) |
|
59 | + { |
|
60 | + $ret = @gzinflate($data); |
|
61 | + if (false === $ret) { |
|
62 | + $err = error_get_last(); |
|
63 | + $msg = isset($err) ? $err["message"] : "gzinflate() failed."; |
|
64 | + throw new \RuntimeException($msg); |
|
65 | + } |
|
66 | + return $ret; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * |
|
71 | - * {@inheritdoc} |
|
72 | - */ |
|
73 | - public function compressionParamValue() |
|
74 | - { |
|
75 | - return JWA::ALGO_DEFLATE; |
|
76 | - } |
|
69 | + /** |
|
70 | + * |
|
71 | + * {@inheritdoc} |
|
72 | + */ |
|
73 | + public function compressionParamValue() |
|
74 | + { |
|
75 | + return JWA::ALGO_DEFLATE; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * |
|
80 | - * {@inheritdoc} |
|
81 | - */ |
|
82 | - public function headerParameters() |
|
83 | - { |
|
84 | - return array(CompressionAlgorithmParameter::fromAlgorithm($this)); |
|
85 | - } |
|
78 | + /** |
|
79 | + * |
|
80 | + * {@inheritdoc} |
|
81 | + */ |
|
82 | + public function headerParameters() |
|
83 | + { |
|
84 | + return array(CompressionAlgorithmParameter::fromAlgorithm($this)); |
|
85 | + } |
|
86 | 86 | } |
@@ -12,30 +12,30 @@ |
||
12 | 12 | */ |
13 | 13 | class A256GCMAlgorithm extends AESGCMAlgorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - public function encryptionAlgorithmParamValue() |
|
20 | - { |
|
21 | - return JWA::ALGO_A256GCM; |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + public function encryptionAlgorithmParamValue() |
|
20 | + { |
|
21 | + return JWA::ALGO_A256GCM; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - public function keySize() |
|
29 | - { |
|
30 | - return 32; |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + public function keySize() |
|
29 | + { |
|
30 | + return 32; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function _getGCMCipher() |
|
38 | - { |
|
39 | - return new AES256Cipher(); |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function _getGCMCipher() |
|
38 | + { |
|
39 | + return new AES256Cipher(); |
|
40 | + } |
|
41 | 41 | } |
@@ -11,66 +11,66 @@ |
||
11 | 11 | */ |
12 | 12 | class A128CBCHS256Algorithm extends AESCBCAlgorithm |
13 | 13 | { |
14 | - /** |
|
15 | - * |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - public function keySize() |
|
19 | - { |
|
20 | - return 32; |
|
21 | - } |
|
14 | + /** |
|
15 | + * |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + public function keySize() |
|
19 | + { |
|
20 | + return 32; |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - public function encryptionAlgorithmParamValue() |
|
28 | - { |
|
29 | - return JWA::ALGO_A128CBC_HS256; |
|
30 | - } |
|
23 | + /** |
|
24 | + * |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + public function encryptionAlgorithmParamValue() |
|
28 | + { |
|
29 | + return JWA::ALGO_A128CBC_HS256; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * |
|
34 | - * {@inheritdoc} |
|
35 | - */ |
|
36 | - protected function _cipherMethod() |
|
37 | - { |
|
38 | - return "AES-128-CBC"; |
|
39 | - } |
|
32 | + /** |
|
33 | + * |
|
34 | + * {@inheritdoc} |
|
35 | + */ |
|
36 | + protected function _cipherMethod() |
|
37 | + { |
|
38 | + return "AES-128-CBC"; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * |
|
43 | - * {@inheritdoc} |
|
44 | - */ |
|
45 | - protected function _hashAlgo() |
|
46 | - { |
|
47 | - return "sha256"; |
|
48 | - } |
|
41 | + /** |
|
42 | + * |
|
43 | + * {@inheritdoc} |
|
44 | + */ |
|
45 | + protected function _hashAlgo() |
|
46 | + { |
|
47 | + return "sha256"; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encKeyLen() |
|
55 | - { |
|
56 | - return 16; |
|
57 | - } |
|
50 | + /** |
|
51 | + * |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encKeyLen() |
|
55 | + { |
|
56 | + return 16; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - protected function _macKeyLen() |
|
64 | - { |
|
65 | - return 16; |
|
66 | - } |
|
59 | + /** |
|
60 | + * |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + protected function _macKeyLen() |
|
64 | + { |
|
65 | + return 16; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * |
|
70 | - * {@inheritdoc} |
|
71 | - */ |
|
72 | - protected function _tagLen() |
|
73 | - { |
|
74 | - return 16; |
|
75 | - } |
|
68 | + /** |
|
69 | + * |
|
70 | + * {@inheritdoc} |
|
71 | + */ |
|
72 | + protected function _tagLen() |
|
73 | + { |
|
74 | + return 16; |
|
75 | + } |
|
76 | 76 | } |
@@ -12,30 +12,30 @@ |
||
12 | 12 | */ |
13 | 13 | class A128GCMAlgorithm extends AESGCMAlgorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - public function encryptionAlgorithmParamValue() |
|
20 | - { |
|
21 | - return JWA::ALGO_A128GCM; |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + public function encryptionAlgorithmParamValue() |
|
20 | + { |
|
21 | + return JWA::ALGO_A128GCM; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - public function keySize() |
|
29 | - { |
|
30 | - return 16; |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + public function keySize() |
|
29 | + { |
|
30 | + return 16; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function _getGCMCipher() |
|
38 | - { |
|
39 | - return new AES128Cipher(); |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function _getGCMCipher() |
|
38 | + { |
|
39 | + return new AES128Cipher(); |
|
40 | + } |
|
41 | 41 | } |
@@ -10,55 +10,55 @@ |
||
10 | 10 | */ |
11 | 11 | abstract class EncryptionAlgorithmFactory |
12 | 12 | { |
13 | - /** |
|
14 | - * Mapping from algorithm name to class name. |
|
15 | - * |
|
16 | - * @internal |
|
17 | - * |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - const MAP_ALGO_TO_CLASS = array( |
|
21 | - /* @formatter:off */ |
|
22 | - JWA::ALGO_A128CBC_HS256 => A128CBCHS256Algorithm::class, |
|
23 | - JWA::ALGO_A192CBC_HS384 => A192CBCHS384Algorithm::class, |
|
24 | - JWA::ALGO_A256CBC_HS512 => A256CBCHS512Algorithm::class, |
|
25 | - JWA::ALGO_A128GCM => A128GCMAlgorithm::class, |
|
26 | - JWA::ALGO_A192GCM => A192GCMAlgorithm::class, |
|
27 | - JWA::ALGO_A256GCM => A256GCMAlgorithm::class |
|
28 | - /* @formatter:on */ |
|
29 | - ); |
|
13 | + /** |
|
14 | + * Mapping from algorithm name to class name. |
|
15 | + * |
|
16 | + * @internal |
|
17 | + * |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + const MAP_ALGO_TO_CLASS = array( |
|
21 | + /* @formatter:off */ |
|
22 | + JWA::ALGO_A128CBC_HS256 => A128CBCHS256Algorithm::class, |
|
23 | + JWA::ALGO_A192CBC_HS384 => A192CBCHS384Algorithm::class, |
|
24 | + JWA::ALGO_A256CBC_HS512 => A256CBCHS512Algorithm::class, |
|
25 | + JWA::ALGO_A128GCM => A128GCMAlgorithm::class, |
|
26 | + JWA::ALGO_A192GCM => A192GCMAlgorithm::class, |
|
27 | + JWA::ALGO_A256GCM => A256GCMAlgorithm::class |
|
28 | + /* @formatter:on */ |
|
29 | + ); |
|
30 | 30 | |
31 | - /** |
|
32 | - * Get the content encryption algorithm by algorithm name. |
|
33 | - * |
|
34 | - * @param string $name Algorithm name |
|
35 | - * @throws \UnexpectedValueException If algorithm is not supported. |
|
36 | - * @return \JWX\JWE\ContentEncryptionAlgorithm |
|
37 | - */ |
|
38 | - public static function algoByName($name) |
|
39 | - { |
|
40 | - if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
41 | - throw new \UnexpectedValueException( |
|
42 | - "No content encryption algorithm '$name'."); |
|
43 | - } |
|
44 | - $cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
45 | - return new $cls(); |
|
46 | - } |
|
31 | + /** |
|
32 | + * Get the content encryption algorithm by algorithm name. |
|
33 | + * |
|
34 | + * @param string $name Algorithm name |
|
35 | + * @throws \UnexpectedValueException If algorithm is not supported. |
|
36 | + * @return \JWX\JWE\ContentEncryptionAlgorithm |
|
37 | + */ |
|
38 | + public static function algoByName($name) |
|
39 | + { |
|
40 | + if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
41 | + throw new \UnexpectedValueException( |
|
42 | + "No content encryption algorithm '$name'."); |
|
43 | + } |
|
44 | + $cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
45 | + return new $cls(); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Get the content encryption algorithm as specified in the given header. |
|
50 | - * |
|
51 | - * @param Header $header Header |
|
52 | - * @throws \UnexpectedValueException If content encryption algorithm |
|
53 | - * parameter is not present or algorithm is not supported. |
|
54 | - * @return \JWX\JWE\ContentEncryptionAlgorithm |
|
55 | - */ |
|
56 | - public static function algoByHeader(Header $header) |
|
57 | - { |
|
58 | - if (!$header->hasEncryptionAlgorithm()) { |
|
59 | - throw new \UnexpectedValueException( |
|
60 | - "No encryption algorithm parameter."); |
|
61 | - } |
|
62 | - return self::algoByName($header->encryptionAlgorithm()->value()); |
|
63 | - } |
|
48 | + /** |
|
49 | + * Get the content encryption algorithm as specified in the given header. |
|
50 | + * |
|
51 | + * @param Header $header Header |
|
52 | + * @throws \UnexpectedValueException If content encryption algorithm |
|
53 | + * parameter is not present or algorithm is not supported. |
|
54 | + * @return \JWX\JWE\ContentEncryptionAlgorithm |
|
55 | + */ |
|
56 | + public static function algoByHeader(Header $header) |
|
57 | + { |
|
58 | + if (!$header->hasEncryptionAlgorithm()) { |
|
59 | + throw new \UnexpectedValueException( |
|
60 | + "No encryption algorithm parameter."); |
|
61 | + } |
|
62 | + return self::algoByName($header->encryptionAlgorithm()->value()); |
|
63 | + } |
|
64 | 64 | } |
@@ -13,208 +13,208 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class AESCBCAlgorithm implements ContentEncryptionAlgorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * Get cipher method name that is recognized by OpenSSL. |
|
18 | - * |
|
19 | - * @return string |
|
20 | - */ |
|
21 | - abstract protected function _cipherMethod(); |
|
22 | - |
|
23 | - /** |
|
24 | - * Get algorithm name that is recognized by the Hash extension. |
|
25 | - * |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - abstract protected function _hashAlgo(); |
|
29 | - |
|
30 | - /** |
|
31 | - * Get length of the encryption key. |
|
32 | - * |
|
33 | - * @return int |
|
34 | - */ |
|
35 | - abstract protected function _encKeyLen(); |
|
36 | - |
|
37 | - /** |
|
38 | - * Get length of the MAC key. |
|
39 | - * |
|
40 | - * @return int |
|
41 | - */ |
|
42 | - abstract protected function _macKeyLen(); |
|
43 | - |
|
44 | - /** |
|
45 | - * Get length of the authentication tag. |
|
46 | - * |
|
47 | - * @return int |
|
48 | - */ |
|
49 | - abstract protected function _tagLen(); |
|
50 | - |
|
51 | - /** |
|
52 | - * Get cipher method and verify that it's supported. |
|
53 | - * |
|
54 | - * @throws \RuntimeException |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - final protected function _getCipherMethod() |
|
58 | - { |
|
59 | - static $supported_ciphers; |
|
60 | - if (!isset($supported_ciphers)) { |
|
61 | - $supported_ciphers = array_flip(openssl_get_cipher_methods()); |
|
62 | - } |
|
63 | - $method = $this->_cipherMethod(); |
|
64 | - if (!isset($supported_ciphers[$method])) { |
|
65 | - throw new \RuntimeException( |
|
66 | - "Cipher method $method is not" . |
|
67 | - " supported by this version of OpenSSL."); |
|
68 | - } |
|
69 | - return $method; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Check that key is valid. |
|
74 | - * |
|
75 | - * @param string $key |
|
76 | - * @throws \RuntimeException |
|
77 | - */ |
|
78 | - final protected function _validateKey($key) |
|
79 | - { |
|
80 | - if (strlen($key) != $this->keySize()) { |
|
81 | - throw new \RuntimeException("Invalid key size."); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Check that IV is valid. |
|
87 | - * |
|
88 | - * @param string $iv |
|
89 | - * @throws \RuntimeException |
|
90 | - */ |
|
91 | - final protected function _validateIV($iv) |
|
92 | - { |
|
93 | - $len = openssl_cipher_iv_length($this->_getCipherMethod()); |
|
94 | - if ($len != strlen($iv)) { |
|
95 | - throw new \RuntimeException("Invalid IV length."); |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Get MAC key from CEK. |
|
101 | - * |
|
102 | - * @param string $key |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - final protected function _macKey($key) |
|
106 | - { |
|
107 | - return substr($key, 0, $this->_macKeyLen()); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get encryption key from CEK. |
|
112 | - * |
|
113 | - * @param string $key |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - final protected function _encKey($key) |
|
117 | - { |
|
118 | - return substr($key, -$this->_encKeyLen()); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Compute AL value. |
|
123 | - * |
|
124 | - * @param string $aad |
|
125 | - * @return string 64 bits |
|
126 | - */ |
|
127 | - final protected function _aadLen($aad) |
|
128 | - { |
|
129 | - // truncate on 32 bit hosts |
|
130 | - if (PHP_INT_SIZE < 8) { |
|
131 | - return "\0\0\0\0" . pack("N", strlen($aad) * 8); |
|
132 | - } |
|
133 | - return pack("J", strlen($aad) * 8); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Compute authentication tag. |
|
138 | - * |
|
139 | - * @param string $data |
|
140 | - * @param string $key CEK |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - final protected function _computeAuthTag($data, $key) |
|
144 | - { |
|
145 | - $tag = hash_hmac($this->_hashAlgo(), $data, $this->_macKey($key), true); |
|
146 | - return substr($tag, 0, $this->_tagLen()); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * |
|
151 | - * {@inheritdoc} |
|
152 | - */ |
|
153 | - public function encrypt($plaintext, $key, $iv, $aad) |
|
154 | - { |
|
155 | - $this->_validateKey($key); |
|
156 | - $this->_validateIV($iv); |
|
157 | - $ciphertext = openssl_encrypt($plaintext, $this->_getCipherMethod(), |
|
158 | - $this->_encKey($key), OPENSSL_RAW_DATA, $iv); |
|
159 | - if (false === $ciphertext) { |
|
160 | - throw new \RuntimeException( |
|
161 | - "openssl_encrypt() failed: " . $this->_getLastOpenSSLError()); |
|
162 | - } |
|
163 | - $auth_data = $aad . $iv . $ciphertext . $this->_aadLen($aad); |
|
164 | - $auth_tag = $this->_computeAuthTag($auth_data, $key); |
|
165 | - return [$ciphertext, $auth_tag]; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * |
|
170 | - * {@inheritdoc} |
|
171 | - */ |
|
172 | - public function decrypt($ciphertext, $key, $iv, $aad, $auth_tag) |
|
173 | - { |
|
174 | - $this->_validateKey($key); |
|
175 | - $this->_validateIV($iv); |
|
176 | - $auth_data = $aad . $iv . $ciphertext . $this->_aadLen($aad); |
|
177 | - if ($this->_computeAuthTag($auth_data, $key) != $auth_tag) { |
|
178 | - throw new AuthenticationException("Message authentication failed."); |
|
179 | - } |
|
180 | - $plaintext = openssl_decrypt($ciphertext, $this->_getCipherMethod(), |
|
181 | - $this->_encKey($key), OPENSSL_RAW_DATA, $iv); |
|
182 | - if (false === $plaintext) { |
|
183 | - throw new \RuntimeException( |
|
184 | - "openssl_decrypt() failed: " . $this->_getLastOpenSSLError()); |
|
185 | - } |
|
186 | - return $plaintext; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Get last OpenSSL error message. |
|
191 | - * |
|
192 | - * @return string|null |
|
193 | - */ |
|
194 | - protected function _getLastOpenSSLError() |
|
195 | - { |
|
196 | - $msg = null; |
|
197 | - while (false !== ($err = openssl_error_string())) { |
|
198 | - $msg = $err; |
|
199 | - } |
|
200 | - return $msg; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * |
|
205 | - * {@inheritdoc} |
|
206 | - */ |
|
207 | - public function ivSize() |
|
208 | - { |
|
209 | - return 16; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * |
|
214 | - * {@inheritdoc} |
|
215 | - */ |
|
216 | - public function headerParameters() |
|
217 | - { |
|
218 | - return array(EncryptionAlgorithmParameter::fromAlgorithm($this)); |
|
219 | - } |
|
16 | + /** |
|
17 | + * Get cipher method name that is recognized by OpenSSL. |
|
18 | + * |
|
19 | + * @return string |
|
20 | + */ |
|
21 | + abstract protected function _cipherMethod(); |
|
22 | + |
|
23 | + /** |
|
24 | + * Get algorithm name that is recognized by the Hash extension. |
|
25 | + * |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + abstract protected function _hashAlgo(); |
|
29 | + |
|
30 | + /** |
|
31 | + * Get length of the encryption key. |
|
32 | + * |
|
33 | + * @return int |
|
34 | + */ |
|
35 | + abstract protected function _encKeyLen(); |
|
36 | + |
|
37 | + /** |
|
38 | + * Get length of the MAC key. |
|
39 | + * |
|
40 | + * @return int |
|
41 | + */ |
|
42 | + abstract protected function _macKeyLen(); |
|
43 | + |
|
44 | + /** |
|
45 | + * Get length of the authentication tag. |
|
46 | + * |
|
47 | + * @return int |
|
48 | + */ |
|
49 | + abstract protected function _tagLen(); |
|
50 | + |
|
51 | + /** |
|
52 | + * Get cipher method and verify that it's supported. |
|
53 | + * |
|
54 | + * @throws \RuntimeException |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + final protected function _getCipherMethod() |
|
58 | + { |
|
59 | + static $supported_ciphers; |
|
60 | + if (!isset($supported_ciphers)) { |
|
61 | + $supported_ciphers = array_flip(openssl_get_cipher_methods()); |
|
62 | + } |
|
63 | + $method = $this->_cipherMethod(); |
|
64 | + if (!isset($supported_ciphers[$method])) { |
|
65 | + throw new \RuntimeException( |
|
66 | + "Cipher method $method is not" . |
|
67 | + " supported by this version of OpenSSL."); |
|
68 | + } |
|
69 | + return $method; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Check that key is valid. |
|
74 | + * |
|
75 | + * @param string $key |
|
76 | + * @throws \RuntimeException |
|
77 | + */ |
|
78 | + final protected function _validateKey($key) |
|
79 | + { |
|
80 | + if (strlen($key) != $this->keySize()) { |
|
81 | + throw new \RuntimeException("Invalid key size."); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Check that IV is valid. |
|
87 | + * |
|
88 | + * @param string $iv |
|
89 | + * @throws \RuntimeException |
|
90 | + */ |
|
91 | + final protected function _validateIV($iv) |
|
92 | + { |
|
93 | + $len = openssl_cipher_iv_length($this->_getCipherMethod()); |
|
94 | + if ($len != strlen($iv)) { |
|
95 | + throw new \RuntimeException("Invalid IV length."); |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Get MAC key from CEK. |
|
101 | + * |
|
102 | + * @param string $key |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + final protected function _macKey($key) |
|
106 | + { |
|
107 | + return substr($key, 0, $this->_macKeyLen()); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get encryption key from CEK. |
|
112 | + * |
|
113 | + * @param string $key |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + final protected function _encKey($key) |
|
117 | + { |
|
118 | + return substr($key, -$this->_encKeyLen()); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Compute AL value. |
|
123 | + * |
|
124 | + * @param string $aad |
|
125 | + * @return string 64 bits |
|
126 | + */ |
|
127 | + final protected function _aadLen($aad) |
|
128 | + { |
|
129 | + // truncate on 32 bit hosts |
|
130 | + if (PHP_INT_SIZE < 8) { |
|
131 | + return "\0\0\0\0" . pack("N", strlen($aad) * 8); |
|
132 | + } |
|
133 | + return pack("J", strlen($aad) * 8); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Compute authentication tag. |
|
138 | + * |
|
139 | + * @param string $data |
|
140 | + * @param string $key CEK |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + final protected function _computeAuthTag($data, $key) |
|
144 | + { |
|
145 | + $tag = hash_hmac($this->_hashAlgo(), $data, $this->_macKey($key), true); |
|
146 | + return substr($tag, 0, $this->_tagLen()); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * |
|
151 | + * {@inheritdoc} |
|
152 | + */ |
|
153 | + public function encrypt($plaintext, $key, $iv, $aad) |
|
154 | + { |
|
155 | + $this->_validateKey($key); |
|
156 | + $this->_validateIV($iv); |
|
157 | + $ciphertext = openssl_encrypt($plaintext, $this->_getCipherMethod(), |
|
158 | + $this->_encKey($key), OPENSSL_RAW_DATA, $iv); |
|
159 | + if (false === $ciphertext) { |
|
160 | + throw new \RuntimeException( |
|
161 | + "openssl_encrypt() failed: " . $this->_getLastOpenSSLError()); |
|
162 | + } |
|
163 | + $auth_data = $aad . $iv . $ciphertext . $this->_aadLen($aad); |
|
164 | + $auth_tag = $this->_computeAuthTag($auth_data, $key); |
|
165 | + return [$ciphertext, $auth_tag]; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * |
|
170 | + * {@inheritdoc} |
|
171 | + */ |
|
172 | + public function decrypt($ciphertext, $key, $iv, $aad, $auth_tag) |
|
173 | + { |
|
174 | + $this->_validateKey($key); |
|
175 | + $this->_validateIV($iv); |
|
176 | + $auth_data = $aad . $iv . $ciphertext . $this->_aadLen($aad); |
|
177 | + if ($this->_computeAuthTag($auth_data, $key) != $auth_tag) { |
|
178 | + throw new AuthenticationException("Message authentication failed."); |
|
179 | + } |
|
180 | + $plaintext = openssl_decrypt($ciphertext, $this->_getCipherMethod(), |
|
181 | + $this->_encKey($key), OPENSSL_RAW_DATA, $iv); |
|
182 | + if (false === $plaintext) { |
|
183 | + throw new \RuntimeException( |
|
184 | + "openssl_decrypt() failed: " . $this->_getLastOpenSSLError()); |
|
185 | + } |
|
186 | + return $plaintext; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Get last OpenSSL error message. |
|
191 | + * |
|
192 | + * @return string|null |
|
193 | + */ |
|
194 | + protected function _getLastOpenSSLError() |
|
195 | + { |
|
196 | + $msg = null; |
|
197 | + while (false !== ($err = openssl_error_string())) { |
|
198 | + $msg = $err; |
|
199 | + } |
|
200 | + return $msg; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * |
|
205 | + * {@inheritdoc} |
|
206 | + */ |
|
207 | + public function ivSize() |
|
208 | + { |
|
209 | + return 16; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * |
|
214 | + * {@inheritdoc} |
|
215 | + */ |
|
216 | + public function headerParameters() |
|
217 | + { |
|
218 | + return array(EncryptionAlgorithmParameter::fromAlgorithm($this)); |
|
219 | + } |
|
220 | 220 | } |
@@ -15,94 +15,94 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class AESGCMAlgorithm implements ContentEncryptionAlgorithm |
17 | 17 | { |
18 | - /** |
|
19 | - * Get GCM Cipher instance. |
|
20 | - * |
|
21 | - * @return \GCM\Cipher\Cipher |
|
22 | - */ |
|
23 | - abstract protected function _getGCMCipher(); |
|
18 | + /** |
|
19 | + * Get GCM Cipher instance. |
|
20 | + * |
|
21 | + * @return \GCM\Cipher\Cipher |
|
22 | + */ |
|
23 | + abstract protected function _getGCMCipher(); |
|
24 | 24 | |
25 | - /** |
|
26 | - * Get GCM instance. |
|
27 | - * |
|
28 | - * @return GCM |
|
29 | - */ |
|
30 | - final protected function _getGCM() |
|
31 | - { |
|
32 | - return new GCM($this->_getGCMCipher(), 16); |
|
33 | - } |
|
25 | + /** |
|
26 | + * Get GCM instance. |
|
27 | + * |
|
28 | + * @return GCM |
|
29 | + */ |
|
30 | + final protected function _getGCM() |
|
31 | + { |
|
32 | + return new GCM($this->_getGCMCipher(), 16); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Check that key is valid. |
|
37 | - * |
|
38 | - * @param string $key |
|
39 | - * @throws \RuntimeException |
|
40 | - */ |
|
41 | - final protected function _validateKey($key) |
|
42 | - { |
|
43 | - if (strlen($key) != $this->keySize()) { |
|
44 | - throw new \RuntimeException("Invalid key size."); |
|
45 | - } |
|
46 | - } |
|
35 | + /** |
|
36 | + * Check that key is valid. |
|
37 | + * |
|
38 | + * @param string $key |
|
39 | + * @throws \RuntimeException |
|
40 | + */ |
|
41 | + final protected function _validateKey($key) |
|
42 | + { |
|
43 | + if (strlen($key) != $this->keySize()) { |
|
44 | + throw new \RuntimeException("Invalid key size."); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Check that IV is valid. |
|
50 | - * |
|
51 | - * @param string $iv |
|
52 | - * @throws \RuntimeException |
|
53 | - */ |
|
54 | - final protected function _validateIV($iv) |
|
55 | - { |
|
56 | - if (strlen($iv) != $this->ivSize()) { |
|
57 | - throw new \RuntimeException("Invalid IV length."); |
|
58 | - } |
|
59 | - } |
|
48 | + /** |
|
49 | + * Check that IV is valid. |
|
50 | + * |
|
51 | + * @param string $iv |
|
52 | + * @throws \RuntimeException |
|
53 | + */ |
|
54 | + final protected function _validateIV($iv) |
|
55 | + { |
|
56 | + if (strlen($iv) != $this->ivSize()) { |
|
57 | + throw new \RuntimeException("Invalid IV length."); |
|
58 | + } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * |
|
63 | - * {@inheritdoc} |
|
64 | - */ |
|
65 | - public function encrypt($plaintext, $key, $iv, $aad) |
|
66 | - { |
|
67 | - $this->_validateKey($key); |
|
68 | - $this->_validateIV($iv); |
|
69 | - list($ciphertext, $auth_tag) = $this->_getGCM()->encrypt($plaintext, |
|
70 | - $aad, $key, $iv); |
|
71 | - return [$ciphertext, $auth_tag]; |
|
72 | - } |
|
61 | + /** |
|
62 | + * |
|
63 | + * {@inheritdoc} |
|
64 | + */ |
|
65 | + public function encrypt($plaintext, $key, $iv, $aad) |
|
66 | + { |
|
67 | + $this->_validateKey($key); |
|
68 | + $this->_validateIV($iv); |
|
69 | + list($ciphertext, $auth_tag) = $this->_getGCM()->encrypt($plaintext, |
|
70 | + $aad, $key, $iv); |
|
71 | + return [$ciphertext, $auth_tag]; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * |
|
76 | - * {@inheritdoc} |
|
77 | - */ |
|
78 | - public function decrypt($ciphertext, $key, $iv, $aad, $auth_tag) |
|
79 | - { |
|
80 | - $this->_validateKey($key); |
|
81 | - $this->_validateIV($iv); |
|
82 | - try { |
|
83 | - $plaintext = $this->_getGCM()->decrypt($ciphertext, $auth_tag, $aad, |
|
84 | - $key, $iv); |
|
85 | - } catch (GCMAuthException $e) { |
|
86 | - throw new AuthenticationException("Message authentication failed."); |
|
87 | - } |
|
88 | - return $plaintext; |
|
89 | - } |
|
74 | + /** |
|
75 | + * |
|
76 | + * {@inheritdoc} |
|
77 | + */ |
|
78 | + public function decrypt($ciphertext, $key, $iv, $aad, $auth_tag) |
|
79 | + { |
|
80 | + $this->_validateKey($key); |
|
81 | + $this->_validateIV($iv); |
|
82 | + try { |
|
83 | + $plaintext = $this->_getGCM()->decrypt($ciphertext, $auth_tag, $aad, |
|
84 | + $key, $iv); |
|
85 | + } catch (GCMAuthException $e) { |
|
86 | + throw new AuthenticationException("Message authentication failed."); |
|
87 | + } |
|
88 | + return $plaintext; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * |
|
93 | - * {@inheritdoc} |
|
94 | - */ |
|
95 | - public function ivSize() |
|
96 | - { |
|
97 | - return 12; |
|
98 | - } |
|
91 | + /** |
|
92 | + * |
|
93 | + * {@inheritdoc} |
|
94 | + */ |
|
95 | + public function ivSize() |
|
96 | + { |
|
97 | + return 12; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * |
|
102 | - * {@inheritdoc} |
|
103 | - */ |
|
104 | - public function headerParameters() |
|
105 | - { |
|
106 | - return array(EncryptionAlgorithmParameter::fromAlgorithm($this)); |
|
107 | - } |
|
100 | + /** |
|
101 | + * |
|
102 | + * {@inheritdoc} |
|
103 | + */ |
|
104 | + public function headerParameters() |
|
105 | + { |
|
106 | + return array(EncryptionAlgorithmParameter::fromAlgorithm($this)); |
|
107 | + } |
|
108 | 108 | } |