def decrypt(cls, ciphertext_and_tag, aad, key, iv): try: return aead.AESCCM(key, cls.tag_bytes).decrypt(iv, ciphertext_and_tag, aad) except cryptography.exceptions.InvalidTag: raise ProtectionInvalid("Tag invalid")
def TestInput(input_bytes): if len(input_bytes) < 12: return fdp = atheris.FuzzedDataProvider(input_bytes) choice = fdp.ConsumeIntInRange(1, 4) if choice == 1: cipher = aead.ChaCha20Poly1305(aead.ChaCha20Poly1305.generate_key()) if choice == 2: cipher = aead.AESGCM(aead.AESGCM.generate_key(bit_length=128)) if choice == 3: cipher = aead.AESOCB3(aead.AESOCB3.generate_key(bit_length=128)) if choice == 4: cipher = aead.AESCCM(aead.AESCCM.generate_key(bit_length=128)) msg = fdp.ConsumeBytes(32) authentext = fdp.ConsumeBytes(32) nonce = fdp.ConsumeBytes(12) if len(nonce) < 12: return ciphertext = cipher.encrypt(nonce, msg, authentext) plaintext = cipher.decrypt(nonce, ciphertext, authentext) assert (plaintext == msg), "Encryption/Decrption error!"
def encrypt(cls, plaintext, aad, key, iv): return aead.AESCCM(key, cls.tag_bytes).encrypt(iv, plaintext, aad)
def aes_ccm_decrypt(k, n, m, a=b"", tag_length=4): c = aead.AESCCM(k, tag_length) return c.decrypt(n, m, a)
def aes_ccm(k, n, m, a=b'', tag_length=4): c = aead.AESCCM(k, tag_length) return c.encrypt(n, m, a)