test: align API crypto coverage with the AES-GCM protocol

This commit is contained in:
zlwu committed 2026-10-11 09:53:48 +08:00
1 parent 5a24f3b433
commit 28990e66be
1 file changed
+8 -4
+8 -4
View File
@@ -33,7 +33,7 @@ class ApiCryptoTests(unittest.TestCase):
plain = api_crypto.decrypt_json(cipher, key)
self.assertEqual(plain, payload)
def test_encrypt_decrypt_roundtrip_cbc(self):
def test_decrypt_rejects_tampered_gcm_ciphertext(self):
key = api_crypto.derive_key(
fixed_secret="fixed-secret",
user_id="u1",
@@ -41,9 +41,13 @@ class ApiCryptoTests(unittest.TestCase):
nonce="nonce-2",
)
payload = {"list": [1, 2, 3], "ok": True}
cipher = api_crypto.encrypt_json(payload, key, alg="cbc")
plain = api_crypto.decrypt_json(cipher, key, alg="cbc")
self.assertEqual(plain, payload)
cipher = api_crypto.encrypt_json(payload, key)
raw = bytearray(api_crypto._b64url_decode(cipher))
raw[-1] ^= 1
tampered = api_crypto._b64url_encode(bytes(raw))
with self.assertRaises(api_crypto.ApiCryptoError):
api_crypto.decrypt_json(tampered, key)
if __name__ == "__main__":