Skip to content

Commit

Permalink
Test for Bob's key after roundtrip, fix missing nonce.
Browse files Browse the repository at this point in the history
- after add_recipient(bob), test whether the key was actually used for reading
- fix missing nonce in header packet serialization
  • Loading branch information
dzoep committed Oct 21, 2024
1 parent 99fdedb commit 72317da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion oarepo_c4gh/crypt4gh/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,23 @@ def packets(self) -> list:
data.write(ekey.public_key)
symmetric_key = ekey.compute_write_key(public_key)
nonce = secrets.token_bytes(12)
data.write(nonce)
content = crypto_aead_chacha20poly1305_ietf_encrypt(
packet.content, None, nonce, symmetric_key
)
data.write(content)
# This packet is useful only for serialization
temp_packets.append(HeaderPacket(packet.length, data.getvalue(), None, None, None, None, None))
temp_packets.append(
HeaderPacket(
packet.length,
data.getvalue(),
None,
None,
None,
None,
None,
)
)
return temp_packets

@property
Expand Down
4 changes: 4 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def test_roundtrip(self):
header = crypt4ghb.header
packets = header.packets
assert len(packets) == 2, "Exactly two header packets expected"
assert len(header.reader_keys_used) == 1, "One reader key expected"
assert (
header.reader_keys_used[0] == bkey.public_key
), "Bob's key expected"


if __name__ == "__main__":
Expand Down

0 comments on commit 72317da

Please sign in to comment.