Skip to content

Commit 1aab369

Browse files
committed
tests/encrypt: fix flaky test_encrypt_decrypt_base64 bad-password check
AES-CBC with EVP_BytesToKey has no MAC, so wolfssl enc -d detects a wrong password only by validating PKCS#7 padding of the garbage plaintext. With a random salt, the last byte(s) may satisfy valid padding by chance (measured ~0.4% / ~1 in 256 over 2000 trials), causing rc==0 and the assertNotEqual(rc, 0) check to fire. Also verify the output file does not match the original plaintext: random garbage will not collide with the source, so the assertion is deterministic regardless of padding luck. Verified stable over 2000 trials (0 false passes) vs 5/2000 with the old assertion.
1 parent 22dfb6a commit 1aab369

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

tests/encrypt/enc-test.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,18 @@ def test_encrypt_decrypt_base64(self):
8181
password="test password")
8282
self.assertEqual(r.returncode, 0, r.stderr)
8383

84-
# Bad password should fail
84+
# Bad password: AES-CBC with EVP_BytesToKey (no MAC) detects a wrong
85+
# password only via PKCS#7 padding validation of the garbage plaintext.
86+
# Random salt makes that check probabilistic (~1/256 false accept), so
87+
# also verify the output does not match the original.
8588
r = run_enc("enc", "-base64", "-d", "-aes-256-cbc",
8689
"-in", enc, "-out", dec,
8790
password="bad password")
88-
self.assertNotEqual(r.returncode, 0)
91+
bad_recovered = (r.returncode == 0
92+
and os.path.exists(dec)
93+
and filecmp.cmp(orig, dec, shallow=False))
94+
self.assertFalse(bad_recovered,
95+
"bad password must not recover original plaintext")
8996

9097
r = run_enc("enc", "-base64", "-d", "-aes-256-cbc",
9198
"-in", enc, "-out", dec,

0 commit comments

Comments
 (0)