Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions test/test_decryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def test_cbc_decryption_fails_with_wrong_key(self):

plaintext = b"hello world, this is a CBC test!!"
real_key = os.urandom(32)
wrong_key = os.urandom(32)
iv = os.urandom(16)

padder = PKCS7(128).padder()
Expand All @@ -185,21 +184,29 @@ def test_cbc_decryption_fails_with_wrong_key(self):
"x-amz-matdesc": '{"kms_cmk_id": "key-id"}',
}

dec_mats = DecryptionMaterials(
iv=iv,
plaintext_data_key=wrong_key,
algorithm_suite=AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF,
)
pipeline = _make_pipeline(
enable_legacy=True,
commitment_policy=CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
keyring_return=dec_mats,
)

with pytest.raises(S3EncryptionClientSecurityError, match="Failed to decrypt CBC content"):
pipeline.decrypt(
_response(metadata, ciphertext), ".instruction", enable_delayed_authentication=False
).read()
# ~1/256 chance random garbage has valid PKCS7 padding, so retry
for _ in range(10):
wrong_key = os.urandom(32)
dec_mats = DecryptionMaterials(
iv=iv,
plaintext_data_key=wrong_key,
algorithm_suite=AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF,
)
pipeline = _make_pipeline(
enable_legacy=True,
commitment_policy=CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
keyring_return=dec_mats,
)
try:
pipeline.decrypt(
_response(metadata, ciphertext),
".instruction",
enable_delayed_authentication=False,
).read()
except S3EncryptionClientSecurityError as e:
assert "Failed to decrypt CBC content" in str(e)
return
pytest.fail("Wrong key did not produce CBC decryption error after 10 attempts")
Comment on lines +200 to +209

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda liked the pytest.raises style :)



# ---------------------------------------------------------------------------
Expand Down
Loading