1 | <?php |
||
24 | abstract class AESGCMKWAlgorithm extends KeyManagementAlgorithm |
||
25 | { |
||
26 | use RandomCEK; |
||
27 | |||
28 | /** |
||
29 | * Key encryption key. |
||
30 | * |
||
31 | * @var string $_kek |
||
32 | */ |
||
33 | protected $_kek; |
||
34 | |||
35 | /** |
||
36 | * Initialization vector. |
||
37 | * |
||
38 | * @var string $_iv |
||
39 | */ |
||
40 | protected $_iv; |
||
41 | |||
42 | /** |
||
43 | * Mapping from algorithm name to class name. |
||
44 | * |
||
45 | * @internal |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | const MAP_ALGO_TO_CLASS = array( |
||
50 | /* @formatter:off */ |
||
51 | JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
||
52 | JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
||
53 | JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class |
||
54 | /* @formatter:on */ |
||
55 | ); |
||
56 | |||
57 | /** |
||
58 | * Required IV size in bytes. |
||
59 | * |
||
60 | * @var int |
||
61 | */ |
||
62 | const IV_SIZE = 12; |
||
63 | |||
64 | /** |
||
65 | * Authentication tag size in bytes. |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | const AUTH_TAG_SIZE = 16; |
||
70 | |||
71 | /** |
||
72 | * Get GCM Cipher instance. |
||
73 | * |
||
74 | * @return Cipher |
||
75 | */ |
||
76 | abstract protected function _getGCMCipher(); |
||
77 | |||
78 | /** |
||
79 | * Get the required key size. |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | abstract protected function _keySize(); |
||
84 | |||
85 | /** |
||
86 | * Get GCM instance. |
||
87 | * |
||
88 | * @return GCM |
||
89 | */ |
||
90 | 9 | final protected function _getGCM() { |
|
93 | |||
94 | /** |
||
95 | * Constructor |
||
96 | * |
||
97 | * @param string $kek Key encryption key |
||
98 | * @param string $iv Initialization vector |
||
99 | */ |
||
100 | 14 | public function __construct($kek, $iv) { |
|
110 | |||
111 | /** |
||
112 | * |
||
113 | * @param JWK $jwk |
||
114 | * @param Header $header |
||
115 | * @throws \UnexpectedValueException |
||
116 | * @return AESGCMKWAlgorithm |
||
117 | */ |
||
118 | 7 | public static function fromJWK(JWK $jwk, Header $header) { |
|
131 | |||
132 | /** |
||
133 | * Initialize from key encryption key with random IV. |
||
134 | * |
||
135 | * Key size must match the underlying cipher. |
||
136 | * |
||
137 | * @param string $key Key encryption key |
||
138 | * @return self |
||
139 | */ |
||
140 | 1 | public static function fromKey($key) { |
|
144 | |||
145 | /** |
||
146 | * |
||
147 | * @see \JWX\JWE\KeyManagementAlgorithm::_encryptKey() |
||
148 | * @return string |
||
149 | */ |
||
150 | 5 | protected function _encryptKey($key, Header &$header) { |
|
158 | |||
159 | /** |
||
160 | * |
||
161 | * @see \JWX\JWE\KeyManagementAlgorithm::_decryptKey() |
||
162 | * @throws \RuntimeException For generic errors |
||
163 | * @return string |
||
164 | */ |
||
165 | 5 | protected function _decryptKey($ciphertext, Header $header) { |
|
175 | |||
176 | /** |
||
177 | * |
||
178 | * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
||
179 | * @return JWTParameter[] |
||
180 | */ |
||
181 | 2 | public function headerParameters() { |
|
186 | } |
||
187 |