(self, cipher_text, iv=None, aad=None, tag=None)
| 476 | raise JWEError(e) |
| 477 | |
| 478 | def decrypt(self, cipher_text, iv=None, aad=None, tag=None): |
| 479 | cipher_text = ensure_binary(cipher_text) |
| 480 | try: |
| 481 | iv = ensure_binary(iv) |
| 482 | mode = self._mode(iv) |
| 483 | if mode.name == "GCM": |
| 484 | if tag is None: |
| 485 | raise ValueError("tag cannot be None") |
| 486 | cipher = aead.AESGCM(self._key) |
| 487 | cipher_text_and_tag = cipher_text + tag |
| 488 | try: |
| 489 | plain_text = cipher.decrypt(iv, cipher_text_and_tag, aad) |
| 490 | except InvalidTag: |
| 491 | raise JWEError("Invalid JWE Auth Tag") |
| 492 | else: |
| 493 | cipher = Cipher(algorithms.AES(self._key), mode, backend=default_backend()) |
| 494 | decryptor = cipher.decryptor() |
| 495 | padded_plain_text = decryptor.update(cipher_text) |
| 496 | padded_plain_text += decryptor.finalize() |
| 497 | unpadder = PKCS7(algorithms.AES.block_size).unpadder() |
| 498 | plain_text = unpadder.update(padded_plain_text) |
| 499 | plain_text += unpadder.finalize() |
| 500 | |
| 501 | return plain_text |
| 502 | except Exception as e: |
| 503 | raise JWEError(e) |
| 504 | |
| 505 | def wrap_key(self, key_data): |
| 506 | key_data = ensure_binary(key_data) |
no test coverage detected