I am trying to implement End-to-end encryption support for pushbullet ephemeral messages in python3.
I'm using python-cryptography, but I get an InvalidTag
-Exception while decrypting. I have double checked the key, iv and tag, but I can't figure out where it goes wrong.
The key is derived like this:
salt = user_ident.encode()
pw = password.encode()
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=30000,
backend=backend)
dkey = kdf.derive(pw)
It is then stored in a keyring as Base64 encoded string, but I double checked if I get the right byte string when encrypting (also by doing it manually in the REPL).
Decrypt:
ciphertxt = a2b_base64(msg['ciphertext'])
version = ciphertxt[0:1]
tag = ciphertxt[1:17]
iv = ciphertxt[17:29]
enc_msg = ciphertxt[29:]
# Construct an AES-GCM Cipher object
decryptor = Cipher(
algorithms.AES(self.dkey_),
modes.GCM(iv, tag),
backend=backend
).decryptor()
cleartxt = decryptor.update(enc_msg) + decryptor.finalize()
All vars are byte strings, here the relevant docs of python-cryptography.
To clarify: I have tried my own methods to encrypt and successfully decrypt some text. But when I activate Pushbullet e2e encryption on my phone and my client and I receive a notification, I get the error above.
The encryption method assembles the encrypted message like this:
b'1' + encryptor.tag + iv + ciphertxt
And I can decipher it. Doesn't work with the tag from a received message.
Any ideas? :/