Trigger InvalidSignature exceptions in storeroom exporter tests
Marco Ricci

Marco Ricci commited on 2024-10-10 12:36:24
Zeige 1 geänderte Dateien mit 28 Einfügungen und 0 Löschungen.


So far, we haven't been explicitly testing for whether the master and
session keys decrytion routines correctly reject payloads with invalid
MAC values, and with the correct error type.  Surely the `cryptography`
primitive is implemented correctly, but there was no test whether
*we* were using the primitive correctly.  So, add one.
... ...
@@ -9,7 +9,9 @@ import json
9 9
 from typing import TYPE_CHECKING
10 10
 
11 11
 import click.testing
12
+import hypothesis
12 13
 import pytest
14
+from hypothesis import strategies
13 15
 
14 16
 import tests
15 17
 from derivepassphrase import cli
... ...
@@ -382,6 +384,32 @@ class TestStoreroom:
382 384
                 },
383 385
             )
384 386
 
387
+    @tests.hypothesis_settings_coverage_compatible
388
+    @hypothesis.given(
389
+        data=strategies.binary(
390
+            min_size=storeroom.MAC_SIZE, max_size=storeroom.MAC_SIZE
391
+        ),
392
+    )
393
+    def test_405_decrypt_keys_invalid_signature(self, data: bytes) -> None:
394
+        key = b'DEADBEEFdeadbeefDeAdBeEfdEaDbEeF'
395
+        # Guessing a correct payload plus MAC would be a pre-image
396
+        # attack on the underlying hash function (SHA-256), i.e. is
397
+        # computationally infeasible, and the chance of finding one by
398
+        # such random sampling is astronomically tiny.
399
+        with pytest.raises(cryptography.exceptions.InvalidSignature):
400
+            storeroom.decrypt_master_keys_data(
401
+                data, {'encryption_key': key, 'signing_key': key}
402
+            )
403
+        with pytest.raises(cryptography.exceptions.InvalidSignature):
404
+            storeroom.decrypt_session_keys(
405
+                data,
406
+                {
407
+                    'hashing_key': key,
408
+                    'encryption_key': key,
409
+                    'signing_key': key,
410
+                },
411
+            )
412
+
385 413
 
386 414
 class TestVaultNativeConfig:
387 415
     @pytest.mark.parametrize(
388 416