Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #10808 -- allow empty plaintexts for aes-gcm-siv #12355

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ CRYPTOGRAPHY_IS_BORINGSSL: bool
CRYPTOGRAPHY_OPENSSL_300_OR_GREATER: bool
CRYPTOGRAPHY_OPENSSL_309_OR_GREATER: bool
CRYPTOGRAPHY_OPENSSL_320_OR_GREATER: bool
CRYPTOGRAPHY_OPENSSL_350_OR_GREATER: bool

class Providers: ...

Expand Down
2 changes: 1 addition & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ name = "cryptography_rust"
crate-type = ["cdylib"]

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CRYPTOGRAPHY_OPENSSL_300_OR_GREATER)', 'cfg(CRYPTOGRAPHY_OPENSSL_309_OR_GREATER)', 'cfg(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER)', 'cfg(CRYPTOGRAPHY_IS_LIBRESSL)', 'cfg(CRYPTOGRAPHY_IS_BORINGSSL)', 'cfg(CRYPTOGRAPHY_OSSLCONF, values("OPENSSL_NO_IDEA", "OPENSSL_NO_CAST", "OPENSSL_NO_BF", "OPENSSL_NO_CAMELLIA", "OPENSSL_NO_SEED", "OPENSSL_NO_SM4"))'] }
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CRYPTOGRAPHY_OPENSSL_300_OR_GREATER)', 'cfg(CRYPTOGRAPHY_OPENSSL_309_OR_GREATER)', 'cfg(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER)', 'cfg(CRYPTOGRAPHY_OPENSSL_350_OR_GREATER)', 'cfg(CRYPTOGRAPHY_IS_LIBRESSL)', 'cfg(CRYPTOGRAPHY_IS_BORINGSSL)', 'cfg(CRYPTOGRAPHY_OSSLCONF, values("OPENSSL_NO_IDEA", "OPENSSL_NO_CAST", "OPENSSL_NO_BF", "OPENSSL_NO_CAMELLIA", "OPENSSL_NO_SEED", "OPENSSL_NO_SM4"))'] }
3 changes: 3 additions & 0 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ fn main() {
if version >= 0x3_02_00_00_0 {
println!("cargo:rustc-cfg=CRYPTOGRAPHY_OPENSSL_320_OR_GREATER");
}
if version >= 0x3_05_00_00_0 {
println!("cargo:rustc-cfg=CRYPTOGRAPHY_OPENSSL_350_OR_GREATER");
}
}

if env::var("DEP_OPENSSL_LIBRESSL_VERSION_NUMBER").is_ok() {
Expand Down
1 change: 1 addition & 0 deletions src/rust/src/backend/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@ impl AesGcmSiv {
let data_bytes = data.as_bytes();
let aad = associated_data.map(Aad::Single);

#[cfg(not(any(CRYPTOGRAPHY_OPENSSL_350_OR_GREATER, CRYPTOGRAPHY_IS_BORINGSSL)))]
if data_bytes.is_empty() {
return Err(CryptographyError::from(
pyo3::exceptions::PyValueError::new_err("data must not be zero length"),
Expand Down
4 changes: 4 additions & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ mod _rust {
"CRYPTOGRAPHY_OPENSSL_320_OR_GREATER",
cfg!(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER),
)?;
openssl_mod.add(
"CRYPTOGRAPHY_OPENSSL_350_OR_GREATER",
cfg!(CRYPTOGRAPHY_OPENSSL_350_OR_GREATER),
)?;

openssl_mod.add("CRYPTOGRAPHY_IS_LIBRESSL", cfg!(CRYPTOGRAPHY_IS_LIBRESSL))?;
openssl_mod.add("CRYPTOGRAPHY_IS_BORINGSSL", cfg!(CRYPTOGRAPHY_IS_BORINGSSL))?;
Expand Down
22 changes: 19 additions & 3 deletions tests/hazmat/primitives/test_aead.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,13 +892,29 @@ def test_invalid_nonce_length(self, backend):
with pytest.raises(ValueError):
aesgcmsiv.decrypt(nonce, pt, None)

def test_no_empty_encryption(self):
def test_empty(self):
key = AESGCMSIV.generate_key(256)
aesgcmsiv = AESGCMSIV(key)
nonce = os.urandom(12)

with pytest.raises(ValueError):
aesgcmsiv.encrypt(nonce, b"", None)
if (
not rust_openssl.CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
and not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
):
with pytest.raises(ValueError):
aesgcmsiv.encrypt(nonce, b"", None)
else:
# From RFC 8452
assert (
AESGCMSIV(
b"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
).encrypt(
b"\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
b"",
b"",
)
== b"\xdc \xe2\xd8?%p[\xb4\x9eC\x9e\xcaV\xde%"
)

with pytest.raises(InvalidTag):
aesgcmsiv.decrypt(nonce, b"", None)
Expand Down