|
14 | 14 | UnsupportedAlgorithm, |
15 | 15 | _Reasons, |
16 | 16 | ) |
17 | | -from cryptography.hazmat.primitives import ( |
18 | | - ciphers, |
19 | | - cmac, |
20 | | - constant_time, |
21 | | - hashes, |
22 | | - hmac, |
23 | | -) |
| 17 | +from cryptography.hazmat.bindings._rust import openssl as rust_openssl |
| 18 | +from cryptography.hazmat.primitives import ciphers, cmac, constant_time |
24 | 19 | from cryptography.hazmat.primitives.kdf import KeyDerivationFunction |
25 | 20 |
|
26 | 21 |
|
@@ -178,62 +173,8 @@ def _generate_fixed_input(self) -> bytes: |
178 | 173 | return b"".join([self._label, b"\x00", self._context, l_val]) |
179 | 174 |
|
180 | 175 |
|
181 | | -class KBKDFHMAC(KeyDerivationFunction): |
182 | | - def __init__( |
183 | | - self, |
184 | | - algorithm: hashes.HashAlgorithm, |
185 | | - mode: Mode, |
186 | | - length: int, |
187 | | - rlen: int, |
188 | | - llen: int | None, |
189 | | - location: CounterLocation, |
190 | | - label: bytes | None, |
191 | | - context: bytes | None, |
192 | | - fixed: bytes | None, |
193 | | - backend: typing.Any = None, |
194 | | - *, |
195 | | - break_location: int | None = None, |
196 | | - ): |
197 | | - if not isinstance(algorithm, hashes.HashAlgorithm): |
198 | | - raise UnsupportedAlgorithm( |
199 | | - "Algorithm supplied is not a supported hash algorithm.", |
200 | | - _Reasons.UNSUPPORTED_HASH, |
201 | | - ) |
202 | | - |
203 | | - from cryptography.hazmat.backends.openssl.backend import ( |
204 | | - backend as ossl, |
205 | | - ) |
206 | | - |
207 | | - if not ossl.hmac_supported(algorithm): |
208 | | - raise UnsupportedAlgorithm( |
209 | | - "Algorithm supplied is not a supported hmac algorithm.", |
210 | | - _Reasons.UNSUPPORTED_HASH, |
211 | | - ) |
212 | | - |
213 | | - self._algorithm = algorithm |
214 | | - |
215 | | - self._deriver = _KBKDFDeriver( |
216 | | - self._prf, |
217 | | - mode, |
218 | | - length, |
219 | | - rlen, |
220 | | - llen, |
221 | | - location, |
222 | | - break_location, |
223 | | - label, |
224 | | - context, |
225 | | - fixed, |
226 | | - ) |
227 | | - |
228 | | - def _prf(self, key_material: bytes) -> hmac.HMAC: |
229 | | - return hmac.HMAC(key_material, self._algorithm) |
230 | | - |
231 | | - def derive(self, key_material: utils.Buffer) -> bytes: |
232 | | - return self._deriver.derive(key_material, self._algorithm.digest_size) |
233 | | - |
234 | | - def verify(self, key_material: bytes, expected_key: bytes) -> None: |
235 | | - if not constant_time.bytes_eq(self.derive(key_material), expected_key): |
236 | | - raise InvalidKey |
| 176 | +KBKDFHMAC = rust_openssl.kdf.KBKDFHMAC |
| 177 | +KeyDerivationFunction.register(KBKDFHMAC) |
237 | 178 |
|
238 | 179 |
|
239 | 180 | class KBKDFCMAC(KeyDerivationFunction): |
|
0 commit comments