def compute_encrypted_message(key: AES128Key, auth_tag: Nonce, message: BaseMessage, authenticated_data: bytes, ) -> bytes: encrypted_message = aesgcm_encrypt( key=key, nonce=auth_tag, plain_text=message.to_bytes(), authenticated_data=authenticated_data, ) return encrypted_message
def compute_encrypted_auth_response(auth_response_key: AES128Key, id_nonce_signature: bytes, enr: Optional[ENR], ) -> bytes: if enr: plain_text_auth_response = rlp.encode([AUTH_RESPONSE_VERSION, id_nonce_signature, enr]) else: plain_text_auth_response = rlp.encode([AUTH_RESPONSE_VERSION, id_nonce_signature, []]) encrypted_auth_response = aesgcm_encrypt( key=auth_response_key, nonce=ZERO_NONCE, plain_text=plain_text_auth_response, authenticated_data=b"", ) return encrypted_auth_response
def test_decryption_with_wrong_inputs(): key = AES128Key(b"\x00" * 16) nonce = Nonce(b"\x11" * 12) plain_text = b"\x33" * 5 aad = b"\x44" * 5 cipher_text = aesgcm_encrypt(key, nonce, plain_text, aad) assert aesgcm_decrypt(key, nonce, cipher_text, aad) == plain_text with pytest.raises(ValidationError): aesgcm_decrypt(b"", nonce, cipher_text, aad) with pytest.raises(ValidationError): aesgcm_decrypt(key, b"", cipher_text, aad) with pytest.raises(DecryptionError): aesgcm_decrypt(key, nonce, b"", aad) with pytest.raises(DecryptionError): aesgcm_decrypt(key, nonce, cipher_text, b"")
def test_roundtrip(key, nonce, plain_text, aad): cipher_text = aesgcm_encrypt(key, nonce, plain_text, aad) plain_text_recovered = aesgcm_decrypt(key, nonce, cipher_text, aad) assert plain_text_recovered == plain_text
def test_encryption_official(key, nonce, plain_text, aad, cipher_text): encrypted = aesgcm_encrypt(key, nonce, plain_text, aad) assert encrypted == cipher_text assert aesgcm_decrypt(key, nonce, cipher_text, aad) == plain_text