|
1 | | -from typing import Any, Dict, List, Optional, Tuple, Union |
| 1 | +from typing import Any, Dict, List, Optional, Union |
2 | 2 |
|
3 | 3 | import cryptography |
4 | | -import pyhpke |
5 | 4 | from cryptography.hazmat.primitives import hashes |
6 | 5 | from cryptography.hazmat.primitives.asymmetric import ec |
7 | 6 | from cryptography.hazmat.primitives.asymmetric.ec import ( |
|
24 | 23 | COSE_KEY_TYPES, |
25 | 24 | ) |
26 | 25 | from ..cose_key_interface import COSEKeyInterface |
27 | | -from ..exceptions import DecodeError, EncodeError, VerifyError |
28 | | -from ..hpke_cipher_suite import HPKECipherSuite |
| 26 | +from ..exceptions import EncodeError, VerifyError |
29 | 27 | from ..utils import i2osp, os2ip, to_cis |
30 | 28 | from .asymmetric import AsymmetricKey |
31 | 29 | from .symmetric import AESCCMKey, AESGCMKey, ChaCha20Key, HMACKey |
@@ -275,28 +273,6 @@ def verify(self, msg: bytes, sig: bytes): |
275 | 273 | except ValueError as err: |
276 | 274 | raise VerifyError("Invalid signature.") from err |
277 | 275 |
|
278 | | - def seal(self, suite: HPKECipherSuite, msg: bytes, aad: bytes = b"") -> Tuple[bytes, bytes]: |
279 | | - # if self._alg != -1: |
280 | | - # raise ValueError("alg should be HPKE(-1)."); |
281 | | - if self._private_key is not None: |
282 | | - raise ValueError("Private key cannot be used for HPKE encryption.") |
283 | | - |
284 | | - ctx = self._create_hpke_context(suite) |
285 | | - enc, sender = ctx.setup_send(self._public_key, b"") # TODO: Add support for info |
286 | | - return enc, sender.aead.seal(aad, msg) |
287 | | - |
288 | | - def open(self, suite: HPKECipherSuite, enc: bytes, msg: bytes, aad: bytes = b"") -> bytes: |
289 | | - # if self._alg != -1: |
290 | | - # raise ValueError("alg should be HPKE(-1)."); |
291 | | - if self._private_key is None: |
292 | | - raise ValueError("Public key cannot be used for HPKE decryption.") |
293 | | - ctx = self._create_hpke_context(suite) |
294 | | - try: |
295 | | - recipient = ctx.setup_recv(enc, self._private_key, b"") # TODO: Add support for info |
296 | | - return recipient.aead.open(aad, msg) |
297 | | - except Exception as err: |
298 | | - raise DecodeError("Failed to decrypt.") from err |
299 | | - |
300 | 276 | def derive_key( |
301 | 277 | self, |
302 | 278 | context: Union[List[Any], Dict[str, Any]], |
@@ -355,26 +331,3 @@ def _os_to_der(self, key_size: int, sig: bytes) -> bytes: |
355 | 331 | r = os2ip(sig[:num_bytes]) |
356 | 332 | s = os2ip(sig[num_bytes:]) |
357 | 333 | return encode_dss_signature(r, s) |
358 | | - |
359 | | - def _create_hpke_context(self, suite: HPKECipherSuite) -> Any: |
360 | | - if self._crv == 1: |
361 | | - if suite.kem != 0x0010: |
362 | | - raise ValueError("KEM id should be 0x0010.") |
363 | | - if suite.kdf == 0x0001 and suite.aead == 0x0001: |
364 | | - return pyhpke.Suite__DHKEM_P256_HKDF_SHA256__HKDF_SHA256__AES_128_GCM |
365 | | - if suite.kdf == 0x0001 and suite.aead == 0x0003: |
366 | | - return pyhpke.Suite__DHKEM_P256_HKDF_SHA256__HKDF_SHA256__ChaCha20Poly1305 |
367 | | - if suite.kdf == 0x0003 and suite.aead == 0x0001: |
368 | | - return pyhpke.Suite__DHKEM_P256_HKDF_SHA256__HKDF_SHA512__AES_128_GCM |
369 | | - raise ValueError("Unsupported kdf/aead combination.") |
370 | | - elif self._crv == 2: |
371 | | - if suite.kem != 0x0011: |
372 | | - raise ValueError("KEM id should be 0x0011.") |
373 | | - raise ValueError("Unsupported kdf/aead combination.") |
374 | | - elif self._crv == 3: |
375 | | - if suite.kem != 0x0012: |
376 | | - raise ValueError("KEM id should be 0x0012.") |
377 | | - if suite.kdf == 0x0001 and suite.aead == 0x0001: |
378 | | - return pyhpke.Suite__DHKEM_P521_HKDF_SHA512__HKDF_SHA512__AES_256_GCM |
379 | | - raise ValueError("Unsupported kdf/aead combination.") |
380 | | - raise ValueError("Invalid crv for HPKE.") |
0 commit comments