blackmwk commented on code in PR #2026: URL: https://github.com/apache/iceberg-rust/pull/2026#discussion_r2978520299
########## crates/iceberg/src/encryption/crypto.rs: ########## @@ -0,0 +1,533 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Core cryptographic operations for Iceberg encryption. + +use std::fmt; +use std::str::FromStr; + +use aes_gcm::aead::generic_array::typenum::U12; +use aes_gcm::aead::rand_core::RngCore; +use aes_gcm::aead::{Aead, AeadCore, KeyInit, OsRng, Payload}; +use aes_gcm::{Aes128Gcm, Aes256Gcm, AesGcm, Nonce}; +use zeroize::Zeroizing; + +/// AES-192-GCM with 96-bit nonce. Not provided by `aes-gcm` but constructible +/// from the underlying primitives, same as `Aes128Gcm` and `Aes256Gcm`. +type Aes192Gcm = AesGcm<aes_gcm::aes::Aes192, U12>; + +use crate::{Error, ErrorKind, Result}; + +/// Wrapper for sensitive byte data (encryption keys, DEKs, etc.) that: +/// - Zeroizes memory on drop +/// - Redacts content in [`Debug`] and [`Display`] output +/// - Provides only `&[u8]` access via [`as_bytes()`](Self::as_bytes) +/// - Uses `Box<[u8]>` (immutable boxed slice) since key bytes never grow +/// +/// Use this type for any struct field that holds plaintext key material. +/// Because its [`Debug`] impl always prints `[N bytes REDACTED]`, structs +/// containing `SensitiveBytes` can safely derive or implement `Debug` +/// without risk of leaking key material. +#[derive(Clone, PartialEq, Eq)] +pub struct SensitiveBytes(Zeroizing<Box<[u8]>>); + +impl SensitiveBytes { + /// Wraps the given bytes as sensitive material. + pub fn new(bytes: impl Into<Box<[u8]>>) -> Self { + Self(Zeroizing::new(bytes.into())) + } + + /// Returns the underlying bytes. + pub fn as_bytes(&self) -> &[u8] { + &self.0 + } + + /// Returns the number of bytes. + #[allow(dead_code)] // Encryption work is ongoing so currently unused + pub fn len(&self) -> usize { + self.0.len() + } + + /// Returns `true` if the byte slice is empty. + #[allow(dead_code)] // Encryption work is ongoing so currently unused + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +impl fmt::Debug for SensitiveBytes { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "[{} bytes REDACTED]", self.0.len()) + } +} + +impl fmt::Display for SensitiveBytes { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "[{} bytes REDACTED]", self.0.len()) + } +} + +/// Supported AES key sizes for AES-GCM encryption. +/// +/// The Iceberg spec supports 128, 192, and 256-bit keys for AES-GCM. +/// See: <https://iceberg.apache.org/gcm-stream-spec/#goals> +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AesKeySize { + /// 128-bit AES key (16 bytes) + Bits128 = 128, + /// 192-bit AES key (24 bytes) + Bits192 = 192, + /// 256-bit AES key (32 bytes) + Bits256 = 256, +} + +impl AesKeySize { + /// Returns the key length in bytes for this key size. + pub fn key_length(&self) -> usize { + match self { + Self::Bits128 => 16, + Self::Bits192 => 24, + Self::Bits256 => 32, + } + } + + /// Returns the key size for a given DEK length in bytes. + /// + /// Matches Java's `encryption.data-key-length` property semantics: + /// 16 → 128-bit, 24 → 192-bit, 32 → 256-bit. + pub fn from_key_length(len: usize) -> Result<Self> { + match len { + 16 => Ok(Self::Bits128), + 24 => Ok(Self::Bits192), + 32 => Ok(Self::Bits256), + _ => Err(Error::new( + ErrorKind::FeatureUnsupported, + format!("Unsupported data key length: {len} (must be 16, 24, or 32)"), + )), + } + } +} + +impl FromStr for AesKeySize { + type Err = Error; + + fn from_str(s: &str) -> Result<Self> { + match s { + "128" | "AES_GCM_128" | "AES128_GCM" => Ok(Self::Bits128), + "192" | "AES_GCM_192" | "AES192_GCM" => Ok(Self::Bits192), + "256" | "AES_GCM_256" | "AES256_GCM" => Ok(Self::Bits256), + _ => Err(Error::new( + ErrorKind::FeatureUnsupported, + format!("Unsupported AES key size: {s}"), + )), + } + } +} + +/// A secure encryption key that zeroes its memory on drop. +pub struct SecureKey { + key: SensitiveBytes, + key_size: AesKeySize, +} + +impl SecureKey { + /// Creates a new secure key with the specified key size. + /// + /// # Errors + /// Returns an error if the key length doesn't match the key size requirements. + pub fn new(key: &[u8], key_size: AesKeySize) -> Result<Self> { Review Comment: I think we could infer ```suggestion pub fn new(key: &[u8]) -> Result<Self> { ``` I think we could infer `AesKeySize` from key length? ########## crates/iceberg/src/encryption/crypto.rs: ########## @@ -0,0 +1,533 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Core cryptographic operations for Iceberg encryption. + +use std::fmt; +use std::str::FromStr; + +use aes_gcm::aead::generic_array::typenum::U12; +use aes_gcm::aead::rand_core::RngCore; +use aes_gcm::aead::{Aead, AeadCore, KeyInit, OsRng, Payload}; +use aes_gcm::{Aes128Gcm, Aes256Gcm, AesGcm, Nonce}; +use zeroize::Zeroizing; + +/// AES-192-GCM with 96-bit nonce. Not provided by `aes-gcm` but constructible +/// from the underlying primitives, same as `Aes128Gcm` and `Aes256Gcm`. +type Aes192Gcm = AesGcm<aes_gcm::aes::Aes192, U12>; + +use crate::{Error, ErrorKind, Result}; + +/// Wrapper for sensitive byte data (encryption keys, DEKs, etc.) that: +/// - Zeroizes memory on drop +/// - Redacts content in [`Debug`] and [`Display`] output +/// - Provides only `&[u8]` access via [`as_bytes()`](Self::as_bytes) +/// - Uses `Box<[u8]>` (immutable boxed slice) since key bytes never grow +/// +/// Use this type for any struct field that holds plaintext key material. +/// Because its [`Debug`] impl always prints `[N bytes REDACTED]`, structs +/// containing `SensitiveBytes` can safely derive or implement `Debug` +/// without risk of leaking key material. +#[derive(Clone, PartialEq, Eq)] +pub struct SensitiveBytes(Zeroizing<Box<[u8]>>); Review Comment: ```suggestion struct SensitiveBytes(Zeroizing<Box<[u8]>>); ``` I think we are expecting to expose `SecureKey` as public api, and we should hide this? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
