Skip to content

Commit 4fe36d8

Browse files
committed
migrate kbkdfhmac to rust
1 parent 15d40e2 commit 4fe36d8

File tree

5 files changed

+355
-101
lines changed

5 files changed

+355
-101
lines changed

src/cryptography/hazmat/bindings/_rust/openssl/kdf.pyi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import typing
66

77
from cryptography.hazmat.primitives.hashes import HashAlgorithm
8+
from cryptography.hazmat.primitives.kdf.kbkdf import CounterLocation, Mode
89
from cryptography.utils import Buffer
910

1011
class PBKDF2HMAC:
@@ -162,3 +163,22 @@ class ConcatKDFHMAC:
162163
def derive(self, key_material: Buffer) -> bytes: ...
163164
def derive_into(self, key_material: Buffer, buffer: Buffer) -> int: ...
164165
def verify(self, key_material: bytes, expected_key: bytes) -> None: ...
166+
167+
class KBKDFHMAC:
168+
def __init__(
169+
self,
170+
algorithm: HashAlgorithm,
171+
mode: Mode,
172+
length: int,
173+
rlen: int,
174+
llen: int | None,
175+
location: CounterLocation,
176+
label: bytes | None,
177+
context: bytes | None,
178+
fixed: bytes | None,
179+
backend: typing.Any = None,
180+
*,
181+
break_location: int | None = None,
182+
) -> None: ...
183+
def derive(self, key_material: Buffer) -> bytes: ...
184+
def verify(self, key_material: bytes, expected_key: bytes) -> None: ...

src/cryptography/hazmat/primitives/kdf/kbkdf.py

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414
UnsupportedAlgorithm,
1515
_Reasons,
1616
)
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
2419
from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
2520

2621

@@ -178,62 +173,8 @@ def _generate_fixed_input(self) -> bytes:
178173
return b"".join([self._label, b"\x00", self._context, l_val])
179174

180175

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)
237178

238179

239180
class KBKDFCMAC(KeyDerivationFunction):

0 commit comments

Comments
 (0)