Skip to content

Commit 03d0699

Browse files
committed
adjust nonce size
1 parent 4481a08 commit 03d0699

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

hivemind_bus_client/encryption.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ def encrypt_as_json(key: Union[str, bytes], data: Union[str, Dict[str, Any]],
6868

6969
ciphertext = encrypt_bin(key, data, cipher=bcipher)
7070

71-
# TODO - adjust depending on cipher, sizes are different
72-
nonce, ciphertext, tag = ciphertext[:16], ciphertext[16:-16], ciphertext[-16:]
71+
# extract nonce/tag depending on cipher, sizes are different
72+
if cipher in aes_ciphers:
73+
nonce, ciphertext, tag = ciphertext[:16], ciphertext[16:-16], ciphertext[-16:]
74+
else:
75+
nonce, ciphertext, tag = ciphertext[:12], ciphertext[12:-16], ciphertext[-16:]
7376

7477
encoder = pybase64.b64encode if cipher in b64_ciphers else hexlify
7578

@@ -108,7 +111,6 @@ def decrypt_from_json(key: Union[str, bytes], data: Union[str, bytes], cipher: J
108111

109112
ciphertext = decoder(data["ciphertext"])
110113
if "tag" not in data: # web crypto compatibility
111-
# TODO - adjust depending on cipher, sizes are different
112114
ciphertext, tag = ciphertext[:-16], ciphertext[-16:]
113115
else:
114116
tag = decoder(data["tag"])
@@ -169,8 +171,11 @@ def decrypt_bin(key: Union[str, bytes], ciphertext: bytes, cipher: BinaryCiphers
169171
if cipher not in BinaryCiphers:
170172
raise InvalidCipher(f"Invalid binary cipher: {str(cipher)}")
171173

172-
# TODO - adjust depending on cipher, sizes are different
173-
nonce, ciphertext, tag = ciphertext[:16], ciphertext[16:-16], ciphertext[-16:]
174+
# extract nonce/tag depending on cipher, sizes are different
175+
if cipher == BinaryCiphers.BINARY_AES_GCM_128:
176+
nonce, ciphertext, tag = ciphertext[:16], ciphertext[16:-16], ciphertext[-16:]
177+
else:
178+
nonce, ciphertext, tag = ciphertext[:12], ciphertext[12:-16], ciphertext[-16:]
174179

175180
decryptor = decrypt_AES_GCM_128 if cipher == BinaryCiphers.BINARY_AES_GCM_128 else decrypt_ChaCha20_Poly1305
176181
try:

0 commit comments

Comments
 (0)