| /* | |
| * Copyright (C) 2025 The Android Open Source Project | |
| * | |
| * Licensed 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. | |
| */ | |
| use crate::DmTargetSpec; | |
| /// `default-key` module implements the "default-key" target in the device mapper | |
| /// framework. Specifically, it provides `DmDefaultKeyTargetBuilder` struct which | |
| /// is used to construct a `DmDefaultKeyTarget` struct which is then given to | |
| /// `DeviceMapper` to create a mapper device. | |
| use crate::crypt::CipherType; | |
| use anyhow::{Context, Result, ensure}; | |
| use std::io::Write; | |
| use std::mem::size_of; | |
| use std::path::Path; | |
| use zerocopy::IntoBytes; | |
| const SECTOR_SIZE: u64 = 512; | |
| pub struct DmDefaultKeyTarget(Box<[u8]>); | |
| /// Device-Mapper’s dm-default-key target is designed to be used in conjunction with | |
| /// file based encryption in either ext4 or f2fs and provides encryption via a default | |
| /// key of blocks, typically metadata blocks, that the file system does not encrypt. | |
| impl DmDefaultKeyTarget { | |
| /// Flatten into slice | |
| pub fn as_slice(&self) -> &[u8] { | |
| self.0.as_ref() | |
| } | |
| } | |
| pub struct DmDefaultKeyTargetBuilder<'a> { | |
| cipher: CipherType, | |
| key: Option<&'a [u8]>, | |
| iv_offset: u64, | |
| device_path: Option<&'a Path>, | |
| offset: u64, | |
| device_size: u64, | |
| sector_size: Option<u32>, | |
| allow_discards: bool, | |
| iv_large_sectors: bool, | |
| is_hw_wrapped: bool, | |
| } | |
| /// A builder that constructs `DmDefaultKeyTarget` struct. | |
| impl Default for DmDefaultKeyTargetBuilder<'_> { | |
| fn default() -> Self { | |
| DmDefaultKeyTargetBuilder { | |
| cipher: CipherType::AES256XTS, | |
| key: None, | |
| iv_offset: 0, | |
| device_path: None, | |
| offset: 0, | |
| device_size: 0, | |
| sector_size: None, | |
| allow_discards: false, | |
| iv_large_sectors: false, | |
| is_hw_wrapped: false, | |
| } | |
| } | |
| } | |
| impl<'a> DmDefaultKeyTargetBuilder<'a> { | |
| /// Sets the device that will be used as the data device (i.e. providing actual data). | |
| pub fn data_device(&mut self, p: &'a Path, size: u64) -> &mut Self { | |
| self.device_path = Some(p); | |
| self.device_size = size; | |
| self | |
| } | |
| /// Sets the encryption cipher. | |
| pub fn cipher(&mut self, cipher: CipherType) -> &mut Self { | |
| self.cipher = cipher; | |
| self | |
| } | |
| /// Sets the key used for encryption. Input is byte array. | |
| pub fn key(&mut self, key: &'a [u8]) -> &mut Self { | |
| self.key = Some(key); | |
| self | |
| } | |
| /// The IV offset is a sector count that is added to the sector number before creating the IV. | |
| pub fn iv_offset(&mut self, iv_offset: u64) -> &mut Self { | |
| self.iv_offset = iv_offset; | |
| self | |
| } | |
| /// Starting sector within the device where the encrypted data begins | |
| pub fn offset(&mut self, offset: u64) -> &mut Self { | |
| self.offset = offset; | |
| self | |
| } | |
| /// Sets the sector size. | |
| pub fn sector_size(&mut self, sector_size: impl Into<Option<u32>>) -> &mut Self { | |
| self.sector_size = sector_size.into(); | |
| self | |
| } | |
| /// Allows discard requests to be passed to the underlying device. | |
| pub fn allow_discards(&mut self, allow_discards: bool) -> &mut Self { | |
| self.allow_discards = allow_discards; | |
| self | |
| } | |
| /// Enables large IV sectors. | |
| pub fn iv_large_sectors(&mut self, iv_large_sectors: bool) -> &mut Self { | |
| self.iv_large_sectors = iv_large_sectors; | |
| self | |
| } | |
| /// Sets whether the key is hardware-wrapped. | |
| pub fn is_hw_wrapped(&mut self, is_hw_wrapped: bool) -> &mut Self { | |
| self.is_hw_wrapped = is_hw_wrapped; | |
| self | |
| } | |
| /// Constructs a `DmDefaultKeyTarget`. | |
| pub fn build(&self) -> Result<DmDefaultKeyTarget> { | |
| // The `DmDefaultKeyTarget` struct actually is a flattened data consisting of a header and | |
| // body. The format of the header is `dm_target_spec` as defined in | |
| // include/uapi/linux/dm-ioctl.h. | |
| let device_path = self | |
| .device_path | |
| .context("data device is not set")? | |
| .to_str() | |
| .context("data device path is not encoded in utf8")?; | |
| ensure!(self.key.is_some(), "key is not set"); | |
| let key_len = self.key.unwrap().len(); | |
| if !self.is_hw_wrapped { | |
| ensure!( | |
| self.cipher.validata_key_size(key_len), | |
| format!("Invalid key size for cipher:{}", self.cipher.get_kernel_crypto_name()) | |
| ); | |
| } else { | |
| // Hardware wrapped keys may have different sizes depending on the hardware, | |
| // so we only perform a minimal check. | |
| ensure!(key_len > 0, "wrapped key cannot be empty"); | |
| } | |
| let key = hex::encode(self.key.unwrap()); | |
| // Step2: serialize the information according to the spec, which is ... | |
| // DmTargetSpec{...} | |
| // <cipher> <key> <iv_offset> <device path> <offset> [<num_opt_args> <opt_args...>] | |
| let mut body = String::new(); | |
| use std::fmt::Write; | |
| write!(&mut body, "{} ", self.cipher.get_kernel_crypto_name())?; | |
| write!(&mut body, "{key} ")?; | |
| write!(&mut body, "{} ", self.iv_offset)?; | |
| write!(&mut body, "{device_path} ")?; | |
| write!(&mut body, "{} ", self.offset)?; | |
| let mut opt_args = Vec::new(); | |
| if self.allow_discards { | |
| opt_args.push("allow_discards".to_string()); | |
| } | |
| if let Some(sector_size) = self.sector_size { | |
| opt_args.push(format!("sector_size:{}", sector_size)); | |
| } | |
| if self.iv_large_sectors { | |
| opt_args.push("iv_large_sectors".to_string()); | |
| } | |
| if self.is_hw_wrapped { | |
| opt_args.push("wrappedkey_v0".to_string()); | |
| } | |
| if !opt_args.is_empty() { | |
| write!(&mut body, "{} ", opt_args.len())?; | |
| for arg in opt_args { | |
| write!(&mut body, "{} ", arg)?; | |
| } | |
| } | |
| write!(&mut body, "\0")?; // null terminator | |
| let size = size_of::<DmTargetSpec>() + body.len(); | |
| let aligned_size = (size + 7) & !7; // align to 8 byte boundaries | |
| let padding = aligned_size - size; | |
| let mut header = DmTargetSpec::new("default-key")?; | |
| header.sector_start = 0; | |
| header.length = self.device_size / SECTOR_SIZE; // number of 512-byte sectors | |
| header.next = aligned_size as u32; | |
| let mut buf = Vec::with_capacity(aligned_size); | |
| buf.write_all(header.as_bytes())?; | |
| buf.write_all(body.as_bytes())?; | |
| buf.write_all(vec![0; padding].as_slice())?; | |
| Ok(DmDefaultKeyTarget(buf.into_boxed_slice())) | |
| } | |
| } |