1- from typing import Any , Dict , List , Optional , Union
1+ from typing import Any , Dict , Optional , Union
22
33import cryptography
44from cryptography .hazmat .primitives import hashes
2323 PublicFormat ,
2424)
2525
26- from ..const import ( # COSE_KEY_LEN,
26+ from ..const import (
2727 COSE_ALGORITHMS_CKDM_KEY_AGREEMENT ,
2828 COSE_ALGORITHMS_CKDM_KEY_AGREEMENT_ES ,
2929 COSE_ALGORITHMS_HPKE ,
3030 COSE_ALGORITHMS_SIG_OKP ,
31- COSE_KEY_LEN ,
3231 COSE_KEY_OPERATION_VALUES ,
3332 COSE_KEY_TYPES ,
3433)
35- from ..cose_key_interface import COSEKeyInterface
3634from ..exceptions import EncodeError , VerifyError
37- from ..utils import to_cis
3835from .asymmetric import AsymmetricKey
39- from .symmetric import AESCCMKey , AESGCMKey , ChaCha20Key , HMACKey
4036
4137
4238class OKPKey (AsymmetricKey ):
@@ -273,13 +269,7 @@ def verify(self, msg: bytes, sig: bytes):
273269 except cryptography .exceptions .InvalidSignature as err :
274270 raise VerifyError ("Failed to verify." ) from err
275271
276- def derive_bytes (
277- self ,
278- length : int ,
279- material : bytes = b"" ,
280- info : bytes = b"" ,
281- public_key : Optional [Any ] = None ,
282- ) -> bytes :
272+ def derive_bytes (self , length : int , material : bytes = b"" , info : bytes = b"" , public_key : Optional [Any ] = None ) -> bytes :
283273
284274 if self ._public_key :
285275 raise ValueError ("Public key cannot be used for key derivation." )
@@ -297,60 +287,7 @@ def derive_bytes(
297287 else :
298288 self ._key = X25519PrivateKey .generate () if self ._crv == 4 else X448PrivateKey .generate ()
299289 shared_key = self ._key .exchange (public_key .key )
300- hkdf = HKDF (
301- algorithm = self ._hash_alg (),
302- length = length ,
303- salt = None ,
304- info = info ,
305- )
290+ hkdf = HKDF (algorithm = self ._hash_alg (), length = length , salt = None , info = info )
306291 return hkdf .derive (shared_key )
307292 except Exception as err :
308293 raise EncodeError ("Failed to derive bytes." ) from err
309-
310- def derive_key (
311- self ,
312- context : Union [List [Any ], Dict [str , Any ]],
313- material : bytes = b"" ,
314- public_key : Optional [COSEKeyInterface ] = None ,
315- ) -> COSEKeyInterface :
316-
317- if self ._public_key :
318- raise ValueError ("Public key cannot be used for key derivation." )
319- if not public_key :
320- raise ValueError ("public_key should be set." )
321- if not isinstance (public_key .key , X25519PublicKey ) and not isinstance (public_key .key , X448PublicKey ):
322- raise ValueError ("public_key should be x25519/x448 public key." )
323- # if self._alg not in COSE_ALGORITHMS_CKDM_KEY_AGREEMENT.values():
324- # raise ValueError(f"Invalid alg for key derivation: {self._alg}.")
325-
326- # Validate context information.
327- if isinstance (context , dict ):
328- context = to_cis (context , self ._alg )
329- else :
330- self ._validate_context (context )
331-
332- # Derive key.
333- if self ._private_key :
334- self ._key = self ._private_key
335- else :
336- self ._key = X25519PrivateKey .generate () if self ._crv == 4 else X448PrivateKey .generate ()
337- shared_key = self ._key .exchange (public_key .key )
338- hkdf = HKDF (
339- algorithm = self ._hash_alg (),
340- length = COSE_KEY_LEN [context [0 ]] // 8 ,
341- salt = None ,
342- info = self ._dumps (context ),
343- )
344- cose_key = {
345- 1 : 4 ,
346- 3 : context [0 ],
347- - 1 : hkdf .derive (shared_key ),
348- }
349- if cose_key [3 ] in [1 , 2 , 3 ]:
350- return AESGCMKey (cose_key )
351- if cose_key [3 ] in [4 , 5 , 6 , 7 ]:
352- return HMACKey (cose_key )
353- if cose_key [3 ] in [10 , 11 , 12 , 13 , 30 , 31 , 32 , 33 ]:
354- return AESCCMKey (cose_key )
355- # cose_key[3] == 24:
356- return ChaCha20Key (cose_key )
0 commit comments