|
13 | 13 | from cryptography.hazmat.primitives.ciphers import ( |
14 | 14 | Cipher, algorithms, modes |
15 | 15 | ) |
| 16 | +from cryptography.hazmat.primitives.ciphers.aead import AESGCM |
16 | 17 |
|
17 | 18 |
|
18 | 19 | @pytest.mark.requires_backend_interface(interface=CipherBackend) |
@@ -40,3 +41,56 @@ def test_aes_cbc_pkcs5(backend, wycheproof): |
40 | 41 | assert computed_ct != ct |
41 | 42 | with pytest.raises(ValueError): |
42 | 43 | unpadder.update(padded_msg) + unpadder.finalize() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.requires_backend_interface(interface=CipherBackend) |
| 47 | +@pytest.mark.wycheproof_tests("aes_gcm_test.json") |
| 48 | +def test_aes_gcm(backend, wycheproof): |
| 49 | + key = binascii.unhexlify(wycheproof.testcase["key"]) |
| 50 | + iv = binascii.unhexlify(wycheproof.testcase["iv"]) |
| 51 | + aad = binascii.unhexlify(wycheproof.testcase["aad"]) |
| 52 | + msg = binascii.unhexlify(wycheproof.testcase["msg"]) |
| 53 | + ct = binascii.unhexlify(wycheproof.testcase["ct"]) |
| 54 | + tag = binascii.unhexlify(wycheproof.testcase["tag"]) |
| 55 | + if wycheproof.valid or wycheproof.acceptable: |
| 56 | + enc = Cipher(algorithms.AES(key), modes.GCM(iv), backend).encryptor() |
| 57 | + enc.authenticate_additional_data(aad) |
| 58 | + computed_ct = enc.update(msg) + enc.finalize() |
| 59 | + computed_tag = enc.tag |
| 60 | + assert computed_ct == ct |
| 61 | + assert computed_tag == tag |
| 62 | + dec = Cipher( |
| 63 | + algorithms.AES(key), |
| 64 | + modes.GCM(iv, tag, min_tag_length=len(tag)), |
| 65 | + backend |
| 66 | + ).decryptor() |
| 67 | + dec.authenticate_additional_data(aad) |
| 68 | + computed_msg = dec.update(ct) + dec.finalize() |
| 69 | + assert computed_msg == msg |
| 70 | + else: |
| 71 | + # All invalid GCM tests are IV len 0 right now |
| 72 | + assert len(iv) == 0 |
| 73 | + with pytest.raises(ValueError): |
| 74 | + Cipher(algorithms.AES(key), modes.GCM(iv), backend) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.requires_backend_interface(interface=CipherBackend) |
| 78 | +@pytest.mark.wycheproof_tests("aes_gcm_test.json") |
| 79 | +def test_aes_gcm_aead_api(backend, wycheproof): |
| 80 | + key = binascii.unhexlify(wycheproof.testcase["key"]) |
| 81 | + iv = binascii.unhexlify(wycheproof.testcase["iv"]) |
| 82 | + aad = binascii.unhexlify(wycheproof.testcase["aad"]) |
| 83 | + msg = binascii.unhexlify(wycheproof.testcase["msg"]) |
| 84 | + ct = binascii.unhexlify(wycheproof.testcase["ct"]) |
| 85 | + tag = binascii.unhexlify(wycheproof.testcase["tag"]) |
| 86 | + aesgcm = AESGCM(key) |
| 87 | + if wycheproof.valid or wycheproof.acceptable: |
| 88 | + computed_ct = aesgcm.encrypt(iv, msg, aad) |
| 89 | + assert computed_ct == ct + tag |
| 90 | + computed_msg = aesgcm.decrypt(iv, ct + tag, aad) |
| 91 | + assert computed_msg == msg |
| 92 | + else: |
| 93 | + # All invalid GCM tests are IV len 0 right now |
| 94 | + assert len(iv) == 0 |
| 95 | + with pytest.raises(ValueError): |
| 96 | + aesgcm.encrypt(iv, msg, aad) |
0 commit comments