Skip to content

Commit

Permalink
Support Alice&Bob fixture private key encryption in `misc/test_expect…
Browse files Browse the repository at this point in the history
…ed_payload_cooker.py`
  • Loading branch information
touilleMan committed Nov 20, 2024
1 parent 7fdfe77 commit a11038e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions misc/test_expected_payload_cooker.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
raise SystemExit("zstandard not installed. Run `pip install zstandard`")
try:
import nacl.exceptions # type: ignore
import nacl.public # type: ignore
import nacl.secret # type: ignore
except ImportError:
raise SystemExit("pynacl not installed. Run `pip install pynacl`")
Expand All @@ -90,20 +91,33 @@
## println!("***expected: {:?}", expected.dump().unwrap());
TAG_PATTERN = re.compile(r"\W*\*\*\*expected: \[([ 0-9,]*)\]")

KEY_CANDIDATES = [
binascii.unhexlify("b1b52e16c1b46ab133c8bf576e82d26c887f1e9deae1af80043a258c36fcabf3")
SECRET_KEY_CANDIDATES = [
binascii.unhexlify("b1b52e16c1b46ab133c8bf576e82d26c887f1e9deae1af80043a258c36fcabf3"),
]
PRIVATE_KEY_CANDIDATES = [
# Alice fixture privkey
binascii.unhexlify("74e860967fd90d063ebd64fb1ba6824c4c010099dd37508b7f2875a5db2ef8c9"),
# Bob fixture privkey
binascii.unhexlify("16767ec446f2611f971c36f19c2dc11614d853475ac395d6c1d70ba46d07dd49"),
]


def decode_expected_raw(raw: bytes) -> dict[str, object]:
def attempt_decrypt(raw: bytes) -> bytes | None:
for key in KEY_CANDIDATES:
for key in SECRET_KEY_CANDIDATES:
key = nacl.secret.SecretBox(key)
try:
return key.decrypt(raw)
except nacl.exceptions.CryptoError:
continue

for key in PRIVATE_KEY_CANDIDATES:
box = nacl.public.SealedBox(nacl.public.PrivateKey(key))
try:
return box.decrypt(raw)
except nacl.exceptions.CryptoError:
continue

def attempt_msgpack_deserialization(raw: bytes) -> dict[str, object] | None:
try:
# `strict_map_key` is needed because shamir_recovery_brief_certificate
Expand Down

0 comments on commit a11038e

Please sign in to comment.