|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import pytest |
| 19 | +from pytest_mock import MockFixture |
| 20 | + |
| 21 | +from pyiceberg.encryption.ciphers import AesGcmCipher, AesKeySize, SecureKey |
| 22 | +from pyiceberg.exceptions import NotInstalledError |
| 23 | + |
| 24 | +AES128_KEY = b"0123456789012345" |
| 25 | +PLAINTEXT = b"the quick brown fox" |
| 26 | + |
| 27 | +# Known-answer vectors from McGrew & Viega, "The Galois/Counter Mode of Operation |
| 28 | +# (GCM)", shared with the NIST GCM validation suite and the Java and iceberg-rust |
| 29 | +# test suites. They pin the `nonce || ciphertext || tag` layout against changes |
| 30 | +# that stay self-consistent on round trip but break cross-client interoperability. |
| 31 | +GCM_TEST_VECTORS = [ |
| 32 | + pytest.param( |
| 33 | + "feffe9928665731c6d6a8f9467308308", |
| 34 | + "cafebabefacedbaddecaf888", |
| 35 | + "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255", |
| 36 | + "", |
| 37 | + "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985", |
| 38 | + "4d5c2af327cd64a62cf35abd2ba6fab4", |
| 39 | + id="aes128-no-aad", |
| 40 | + ), |
| 41 | + pytest.param( |
| 42 | + "feffe9928665731c6d6a8f9467308308", |
| 43 | + "cafebabefacedbaddecaf888", |
| 44 | + "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39", |
| 45 | + "feedfacedeadbeeffeedfacedeadbeefabaddad2", |
| 46 | + "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091", |
| 47 | + "5bc94fbc3221a5db94fae95ae7121a47", |
| 48 | + id="aes128-with-aad", |
| 49 | + ), |
| 50 | + pytest.param( |
| 51 | + "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308", |
| 52 | + "cafebabefacedbaddecaf888", |
| 53 | + "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39", |
| 54 | + "feedfacedeadbeeffeedfacedeadbeefabaddad2", |
| 55 | + "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662", |
| 56 | + "76fc6ece0f4e1768cddf8853bb2d551b", |
| 57 | + id="aes256-with-aad", |
| 58 | + ), |
| 59 | +] |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "key_length, key_size", |
| 64 | + [(16, AesKeySize.BITS_128), (24, AesKeySize.BITS_192), (32, AesKeySize.BITS_256)], |
| 65 | +) |
| 66 | +def test_key_size_from_key_length(key_length: int, key_size: AesKeySize) -> None: |
| 67 | + assert AesKeySize.from_key_length(key_length) == key_size |
| 68 | + assert key_size.key_length == key_length |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("key_length", [0, 4, 15, 20, 33]) |
| 72 | +def test_key_size_rejects_invalid_key_length(key_length: int) -> None: |
| 73 | + with pytest.raises(ValueError, match=f"Unsupported key length: {key_length}"): |
| 74 | + AesKeySize.from_key_length(key_length) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("key_length", [0, 4, 15, 20, 33]) |
| 78 | +def test_secure_key_rejects_invalid_key_length(key_length: int) -> None: |
| 79 | + with pytest.raises(ValueError, match="Unsupported key length"): |
| 80 | + SecureKey(bytes(key_length)) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize("key_size", list(AesKeySize)) |
| 84 | +def test_secure_key_generate(key_size: AesKeySize) -> None: |
| 85 | + key = SecureKey.generate(key_size) |
| 86 | + |
| 87 | + assert len(key.key) == key_size.key_length |
| 88 | + assert key.key_size == key_size |
| 89 | + assert SecureKey.generate(key_size) != key |
| 90 | + |
| 91 | + |
| 92 | +def test_secure_key_repr_redacts_key() -> None: |
| 93 | + key = SecureKey(AES128_KEY) |
| 94 | + |
| 95 | + assert repr(key) == "SecureKey()" |
| 96 | + assert repr(AES128_KEY) not in repr(key) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize("key_size", list(AesKeySize)) |
| 100 | +@pytest.mark.parametrize("aad", [None, b"", b"aad"]) |
| 101 | +def test_encrypt_decrypt_round_trip(key_size: AesKeySize, aad: bytes | None) -> None: |
| 102 | + cipher = AesGcmCipher(SecureKey.generate(key_size)) |
| 103 | + |
| 104 | + ciphertext = cipher.encrypt(PLAINTEXT, aad) |
| 105 | + |
| 106 | + assert ciphertext != PLAINTEXT |
| 107 | + assert cipher.decrypt(ciphertext, aad) == PLAINTEXT |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize("key, nonce, plaintext, aad, ciphertext, tag", GCM_TEST_VECTORS) |
| 111 | +def test_decrypt_known_answer(key: str, nonce: str, plaintext: str, aad: str, ciphertext: str, tag: str) -> None: |
| 112 | + cipher = AesGcmCipher(SecureKey(bytes.fromhex(key))) |
| 113 | + stored = bytes.fromhex(nonce + ciphertext + tag) |
| 114 | + |
| 115 | + assert cipher.decrypt(stored, bytes.fromhex(aad) or None) == bytes.fromhex(plaintext) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize("key, nonce, plaintext, aad, ciphertext, tag", GCM_TEST_VECTORS) |
| 119 | +def test_encrypt_known_answer( |
| 120 | + monkeypatch: pytest.MonkeyPatch, key: str, nonce: str, plaintext: str, aad: str, ciphertext: str, tag: str |
| 121 | +) -> None: |
| 122 | + monkeypatch.setattr("pyiceberg.encryption.ciphers.os.urandom", lambda _: bytes.fromhex(nonce)) |
| 123 | + cipher = AesGcmCipher(SecureKey(bytes.fromhex(key))) |
| 124 | + |
| 125 | + assert cipher.encrypt(bytes.fromhex(plaintext), bytes.fromhex(aad) or None) == bytes.fromhex(nonce + ciphertext + tag) |
| 126 | + |
| 127 | + |
| 128 | +def test_encrypt_empty_plaintext() -> None: |
| 129 | + cipher = AesGcmCipher(SecureKey(AES128_KEY)) |
| 130 | + |
| 131 | + assert cipher.decrypt(cipher.encrypt(b"")) == b"" |
| 132 | + |
| 133 | + |
| 134 | +def test_ciphertext_layout() -> None: |
| 135 | + cipher = AesGcmCipher(SecureKey(AES128_KEY)) |
| 136 | + |
| 137 | + ciphertext = cipher.encrypt(PLAINTEXT) |
| 138 | + |
| 139 | + assert len(ciphertext) == AesGcmCipher.NONCE_LENGTH + len(PLAINTEXT) + AesGcmCipher.TAG_LENGTH |
| 140 | + |
| 141 | + |
| 142 | +def test_nonce_is_not_reused() -> None: |
| 143 | + cipher = AesGcmCipher(SecureKey(AES128_KEY)) |
| 144 | + |
| 145 | + first, second = cipher.encrypt(PLAINTEXT), cipher.encrypt(PLAINTEXT) |
| 146 | + |
| 147 | + assert first[: AesGcmCipher.NONCE_LENGTH] != second[: AesGcmCipher.NONCE_LENGTH] |
| 148 | + assert first != second |
| 149 | + |
| 150 | + |
| 151 | +def test_decrypt_with_wrong_key() -> None: |
| 152 | + ciphertext = AesGcmCipher(SecureKey(AES128_KEY)).encrypt(PLAINTEXT) |
| 153 | + |
| 154 | + with pytest.raises(ValueError, match="wrong decryption key; or corrupt/tampered data"): |
| 155 | + AesGcmCipher(SecureKey(b"5432109876543210")).decrypt(ciphertext) |
| 156 | + |
| 157 | + |
| 158 | +def test_decrypt_with_mismatched_aad() -> None: |
| 159 | + cipher = AesGcmCipher(SecureKey(AES128_KEY)) |
| 160 | + |
| 161 | + ciphertext = cipher.encrypt(PLAINTEXT, b"aad") |
| 162 | + |
| 163 | + with pytest.raises(ValueError, match="wrong decryption key; or corrupt/tampered data"): |
| 164 | + cipher.decrypt(ciphertext, b"other aad") |
| 165 | + |
| 166 | + |
| 167 | +def test_decrypt_tampered_ciphertext() -> None: |
| 168 | + cipher = AesGcmCipher(SecureKey(AES128_KEY)) |
| 169 | + |
| 170 | + ciphertext = bytearray(cipher.encrypt(PLAINTEXT)) |
| 171 | + ciphertext[-1] ^= 0xFF |
| 172 | + |
| 173 | + with pytest.raises(ValueError, match="wrong decryption key; or corrupt/tampered data"): |
| 174 | + cipher.decrypt(bytes(ciphertext)) |
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.parametrize("length", [0, 1, 27]) |
| 178 | +def test_decrypt_ciphertext_too_short(length: int) -> None: |
| 179 | + cipher = AesGcmCipher(SecureKey(AES128_KEY)) |
| 180 | + |
| 181 | + with pytest.raises(ValueError, match=f"Ciphertext too short: expected at least 28 bytes, got {length}"): |
| 182 | + cipher.decrypt(bytes(length)) |
| 183 | + |
| 184 | + |
| 185 | +def test_cipher_without_cryptography_installed_raises_not_installed_error(mocker: MockFixture) -> None: |
| 186 | + mocker.patch.dict("sys.modules", {"cryptography.hazmat.primitives.ciphers.aead": None}) |
| 187 | + |
| 188 | + with pytest.raises(NotInstalledError, match=r"pyiceberg\[encryption\]"): |
| 189 | + AesGcmCipher(SecureKey(AES128_KEY)) |
0 commit comments