@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A192GCMKWAlgorithm extends AESGCMKWAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_A192GCMKW; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_A192GCMKW; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _keySize(): int |
|
| 28 | - { |
|
| 29 | - return 24; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _keySize(): int |
|
| 28 | + { |
|
| 29 | + return 24; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -22,147 +22,147 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | abstract class AESGCMKWAlgorithm extends KeyManagementAlgorithm |
| 24 | 24 | { |
| 25 | - use RandomCEK; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Mapping from algorithm name to class name. |
|
| 29 | - * |
|
| 30 | - * @internal |
|
| 31 | - * |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - const MAP_ALGO_TO_CLASS = [ |
|
| 35 | - JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
| 36 | - JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
| 37 | - JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class, |
|
| 38 | - ]; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Required IV size in bytes. |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - const IV_SIZE = 12; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Authentication tag size in bytes. |
|
| 49 | - * |
|
| 50 | - * @var int |
|
| 51 | - */ |
|
| 52 | - const AUTH_TAG_SIZE = 16; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Key encryption key. |
|
| 56 | - * |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected $_kek; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Initialization vector. |
|
| 63 | - * |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - protected $_iv; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Constructor. |
|
| 70 | - * |
|
| 71 | - * @param string $kek Key encryption key |
|
| 72 | - * @param string $iv Initialization vector |
|
| 73 | - */ |
|
| 74 | - public function __construct(string $kek, string $iv) |
|
| 75 | - { |
|
| 76 | - if (strlen($kek) !== $this->_keySize()) { |
|
| 77 | - throw new \LengthException('Invalid key size.'); |
|
| 78 | - } |
|
| 79 | - if (self::IV_SIZE !== strlen($iv)) { |
|
| 80 | - throw new \LengthException('Initialization vector must be 96 bits.'); |
|
| 81 | - } |
|
| 82 | - $this->_kek = $kek; |
|
| 83 | - $this->_iv = $iv; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Initialize from JWK. |
|
| 88 | - * |
|
| 89 | - * @param JWK $jwk |
|
| 90 | - * @param Header $header |
|
| 91 | - * |
|
| 92 | - * @throws \UnexpectedValueException |
|
| 93 | - * |
|
| 94 | - * @return self |
|
| 95 | - */ |
|
| 96 | - public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
| 97 | - { |
|
| 98 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 99 | - if (!$header->hasInitializationVector()) { |
|
| 100 | - throw new \UnexpectedValueException('No initialization vector.'); |
|
| 101 | - } |
|
| 102 | - $iv = $header->initializationVector()->initializationVector(); |
|
| 103 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 104 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 105 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 106 | - } |
|
| 107 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 108 | - return new $cls($jwk->key(), $iv); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Initialize from key encryption key with random IV. |
|
| 113 | - * |
|
| 114 | - * Key size must match the underlying cipher. |
|
| 115 | - * |
|
| 116 | - * @param string $key Key encryption key |
|
| 117 | - * |
|
| 118 | - * @return self |
|
| 119 | - */ |
|
| 120 | - public static function fromKey(string $key): self |
|
| 121 | - { |
|
| 122 | - $iv = openssl_random_pseudo_bytes(self::IV_SIZE); |
|
| 123 | - return new static($key, $iv); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * {@inheritdoc} |
|
| 128 | - */ |
|
| 129 | - public function headerParameters(): array |
|
| 130 | - { |
|
| 131 | - return array_merge(parent::headerParameters(), |
|
| 132 | - [AlgorithmParameter::fromAlgorithm($this), |
|
| 133 | - InitializationVectorParameter::fromString($this->_iv), ]); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Get the required key size. |
|
| 138 | - * |
|
| 139 | - * @return int |
|
| 140 | - */ |
|
| 141 | - abstract protected function _keySize(): int; |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * {@inheritdoc} |
|
| 145 | - */ |
|
| 146 | - protected function _encryptKey(string $key, Header &$header): string |
|
| 147 | - { |
|
| 148 | - [$ciphertext, $auth_tag] = AESGCM::encrypt($key, '', $this->_kek, |
|
| 149 | - $this->_iv, self::AUTH_TAG_SIZE); |
|
| 150 | - // insert authentication tag to the header |
|
| 151 | - $header = $header->withParameters( |
|
| 152 | - AuthenticationTagParameter::fromString($auth_tag)); |
|
| 153 | - return $ciphertext; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * {@inheritdoc} |
|
| 158 | - */ |
|
| 159 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
| 160 | - { |
|
| 161 | - if (!$header->hasAuthenticationTag()) { |
|
| 162 | - throw new \RuntimeException( |
|
| 163 | - "Header doesn't contain authentication tag."); |
|
| 164 | - } |
|
| 165 | - $auth_tag = $header->authenticationTag()->authenticationTag(); |
|
| 166 | - return AESGCM::decrypt($ciphertext, $auth_tag, '', $this->_kek, $this->_iv); |
|
| 167 | - } |
|
| 25 | + use RandomCEK; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Mapping from algorithm name to class name. |
|
| 29 | + * |
|
| 30 | + * @internal |
|
| 31 | + * |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + const MAP_ALGO_TO_CLASS = [ |
|
| 35 | + JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
| 36 | + JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
| 37 | + JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class, |
|
| 38 | + ]; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Required IV size in bytes. |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + const IV_SIZE = 12; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Authentication tag size in bytes. |
|
| 49 | + * |
|
| 50 | + * @var int |
|
| 51 | + */ |
|
| 52 | + const AUTH_TAG_SIZE = 16; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Key encryption key. |
|
| 56 | + * |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected $_kek; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Initialization vector. |
|
| 63 | + * |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + protected $_iv; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Constructor. |
|
| 70 | + * |
|
| 71 | + * @param string $kek Key encryption key |
|
| 72 | + * @param string $iv Initialization vector |
|
| 73 | + */ |
|
| 74 | + public function __construct(string $kek, string $iv) |
|
| 75 | + { |
|
| 76 | + if (strlen($kek) !== $this->_keySize()) { |
|
| 77 | + throw new \LengthException('Invalid key size.'); |
|
| 78 | + } |
|
| 79 | + if (self::IV_SIZE !== strlen($iv)) { |
|
| 80 | + throw new \LengthException('Initialization vector must be 96 bits.'); |
|
| 81 | + } |
|
| 82 | + $this->_kek = $kek; |
|
| 83 | + $this->_iv = $iv; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Initialize from JWK. |
|
| 88 | + * |
|
| 89 | + * @param JWK $jwk |
|
| 90 | + * @param Header $header |
|
| 91 | + * |
|
| 92 | + * @throws \UnexpectedValueException |
|
| 93 | + * |
|
| 94 | + * @return self |
|
| 95 | + */ |
|
| 96 | + public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
| 97 | + { |
|
| 98 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 99 | + if (!$header->hasInitializationVector()) { |
|
| 100 | + throw new \UnexpectedValueException('No initialization vector.'); |
|
| 101 | + } |
|
| 102 | + $iv = $header->initializationVector()->initializationVector(); |
|
| 103 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 104 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 105 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 106 | + } |
|
| 107 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 108 | + return new $cls($jwk->key(), $iv); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Initialize from key encryption key with random IV. |
|
| 113 | + * |
|
| 114 | + * Key size must match the underlying cipher. |
|
| 115 | + * |
|
| 116 | + * @param string $key Key encryption key |
|
| 117 | + * |
|
| 118 | + * @return self |
|
| 119 | + */ |
|
| 120 | + public static function fromKey(string $key): self |
|
| 121 | + { |
|
| 122 | + $iv = openssl_random_pseudo_bytes(self::IV_SIZE); |
|
| 123 | + return new static($key, $iv); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * {@inheritdoc} |
|
| 128 | + */ |
|
| 129 | + public function headerParameters(): array |
|
| 130 | + { |
|
| 131 | + return array_merge(parent::headerParameters(), |
|
| 132 | + [AlgorithmParameter::fromAlgorithm($this), |
|
| 133 | + InitializationVectorParameter::fromString($this->_iv), ]); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Get the required key size. |
|
| 138 | + * |
|
| 139 | + * @return int |
|
| 140 | + */ |
|
| 141 | + abstract protected function _keySize(): int; |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * {@inheritdoc} |
|
| 145 | + */ |
|
| 146 | + protected function _encryptKey(string $key, Header &$header): string |
|
| 147 | + { |
|
| 148 | + [$ciphertext, $auth_tag] = AESGCM::encrypt($key, '', $this->_kek, |
|
| 149 | + $this->_iv, self::AUTH_TAG_SIZE); |
|
| 150 | + // insert authentication tag to the header |
|
| 151 | + $header = $header->withParameters( |
|
| 152 | + AuthenticationTagParameter::fromString($auth_tag)); |
|
| 153 | + return $ciphertext; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * {@inheritdoc} |
|
| 158 | + */ |
|
| 159 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
| 160 | + { |
|
| 161 | + if (!$header->hasAuthenticationTag()) { |
|
| 162 | + throw new \RuntimeException( |
|
| 163 | + "Header doesn't contain authentication tag."); |
|
| 164 | + } |
|
| 165 | + $auth_tag = $header->authenticationTag()->authenticationTag(); |
|
| 166 | + return AESGCM::decrypt($ciphertext, $auth_tag, '', $this->_kek, $this->_iv); |
|
| 167 | + } |
|
| 168 | 168 | } |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A128GCMKWAlgorithm extends AESGCMKWAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_A128GCMKW; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_A128GCMKW; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _keySize(): int |
|
| 28 | - { |
|
| 29 | - return 16; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _keySize(): int |
|
| 28 | + { |
|
| 29 | + return 16; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A256GCMKWAlgorithm extends AESGCMKWAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_A256GCMKW; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_A256GCMKW; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _keySize(): int |
|
| 28 | - { |
|
| 29 | - return 32; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _keySize(): int |
|
| 28 | + { |
|
| 29 | + return 32; |
|
| 30 | + } |
|
| 31 | 31 | } |