blob: 56567432ad62ba05ebffafe735f31602cc3e6c71 [file] [log] [blame]
Jiyong Park86c9b082021-06-04 19:03:48 +09001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// `dm` module implements part of the `device-mapper` ioctl interfaces. It currently supports
18// creation and deletion of the mapper device. It doesn't support other operations like querying
19// the status of the mapper device. And there's no plan to extend the support unless it is
20// required.
21//
22// Why in-house development? [`devicemapper`](https://crates.io/crates/devicemapper) is a public
23// Rust implementation of the device mapper APIs. However, it doesn't provide any abstraction for
24// the target-specific tables. User has to manually craft the table. Ironically, the library
25// provides a lot of APIs for the features that are not required for `apkdmverity` such as listing
26// the device mapper block devices that are currently listed in the kernel. Size is an important
27// criteria for Microdroid.
28
Shikha Panwar414ea892022-10-12 13:45:52 +000029//! A library to create device mapper spec & issue ioctls.
30
31#![allow(missing_docs)]
Andrew Walbran3fcebdb2022-11-30 11:16:17 +000032#![cfg_attr(test, allow(unused))]
Jiyong Park86c9b082021-06-04 19:03:48 +090033
Jiyong Park0553ff22021-07-15 12:25:36 +090034use anyhow::{Context, Result};
Jiyong Park86c9b082021-06-04 19:03:48 +090035use std::fs::{File, OpenOptions};
36use std::io::Write;
37use std::mem::size_of;
38use std::os::unix::io::AsRawFd;
39use std::path::{Path, PathBuf};
Frederick Mayle8f795902023-10-23 15:48:34 -070040use zerocopy::AsBytes;
41use zerocopy::FromZeroes;
Jiyong Park86c9b082021-06-04 19:03:48 +090042
Shikha Panwar27cb7e72022-10-13 20:34:45 +000043/// Exposes DmCryptTarget & related builder
44pub mod crypt;
Shikha Panwar414ea892022-10-12 13:45:52 +000045/// Expose util functions
46pub mod util;
47/// Exposes the DmVerityTarget & related builder
48pub mod verity;
Shikha Panwarb278b1c2022-10-14 12:38:32 +000049// Expose loopdevice
50pub mod loopdevice;
Shikha Panwar414ea892022-10-12 13:45:52 +000051
Jiyong Park86c9b082021-06-04 19:03:48 +090052mod sys;
Shikha Panwar27cb7e72022-10-13 20:34:45 +000053use crypt::DmCryptTarget;
Jiyong Park86c9b082021-06-04 19:03:48 +090054use sys::*;
Shikha Panwar414ea892022-10-12 13:45:52 +000055use util::*;
Shikha Panwar27cb7e72022-10-13 20:34:45 +000056use verity::DmVerityTarget;
Jiyong Park86c9b082021-06-04 19:03:48 +090057
58nix::ioctl_readwrite!(_dm_dev_create, DM_IOCTL, Cmd::DM_DEV_CREATE, DmIoctl);
Jiyong Park86c9b082021-06-04 19:03:48 +090059nix::ioctl_readwrite!(_dm_dev_suspend, DM_IOCTL, Cmd::DM_DEV_SUSPEND, DmIoctl);
60nix::ioctl_readwrite!(_dm_table_load, DM_IOCTL, Cmd::DM_TABLE_LOAD, DmIoctl);
Jiyong Park99a35b82021-06-07 10:13:44 +090061nix::ioctl_readwrite!(_dm_dev_remove, DM_IOCTL, Cmd::DM_DEV_REMOVE, DmIoctl);
Jiyong Park86c9b082021-06-04 19:03:48 +090062
Shikha Panwar414ea892022-10-12 13:45:52 +000063/// Create a new (mapper) device
Jiyong Park86c9b082021-06-04 19:03:48 +090064fn dm_dev_create(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> {
65 // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the
66 // state of this process in any way.
67 Ok(unsafe { _dm_dev_create(dm.0.as_raw_fd(), ioctl) }?)
68}
69
Jiyong Park86c9b082021-06-04 19:03:48 +090070fn dm_dev_suspend(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> {
71 // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the
72 // state of this process in any way.
73 Ok(unsafe { _dm_dev_suspend(dm.0.as_raw_fd(), ioctl) }?)
74}
75
76fn dm_table_load(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> {
77 // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the
78 // state of this process in any way.
79 Ok(unsafe { _dm_table_load(dm.0.as_raw_fd(), ioctl) }?)
80}
81
Jiyong Park99a35b82021-06-07 10:13:44 +090082fn dm_dev_remove(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> {
83 // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the
84 // state of this process in any way.
85 Ok(unsafe { _dm_dev_remove(dm.0.as_raw_fd(), ioctl) }?)
86}
87
Jiyong Park86c9b082021-06-04 19:03:48 +090088// `DmTargetSpec` is the header of the data structure for a device-mapper target. When doing the
89// ioctl, one of more `DmTargetSpec` (and its body) are appened to the `DmIoctl` struct.
90#[repr(C)]
Frederick Mayle8f795902023-10-23 15:48:34 -070091#[derive(Copy, Clone, AsBytes, FromZeroes)]
Jiyong Park86c9b082021-06-04 19:03:48 +090092struct DmTargetSpec {
93 sector_start: u64,
94 length: u64, // number of 512 sectors
95 status: i32,
96 next: u32,
97 target_type: [u8; DM_MAX_TYPE_NAME],
98}
99
100impl DmTargetSpec {
101 fn new(target_type: &str) -> Result<Self> {
Frederick Mayle8f795902023-10-23 15:48:34 -0700102 let mut spec = Self::new_zeroed();
Jiyong Park86c9b082021-06-04 19:03:48 +0900103 spec.target_type.as_mut().write_all(target_type.as_bytes())?;
104 Ok(spec)
105 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900106}
107
108impl DmIoctl {
109 fn new(name: &str) -> Result<DmIoctl> {
Frederick Mayle8f795902023-10-23 15:48:34 -0700110 let mut data: Self = Self::new_zeroed();
Jiyong Park86c9b082021-06-04 19:03:48 +0900111 data.version[0] = DM_VERSION_MAJOR;
112 data.version[1] = DM_VERSION_MINOR;
113 data.version[2] = DM_VERSION_PATCHLEVEL;
114 data.data_size = size_of::<Self>() as u32;
115 data.data_start = 0;
116 data.name.as_mut().write_all(name.as_bytes())?;
117 Ok(data)
118 }
119
120 fn set_uuid(&mut self, uuid: &str) -> Result<()> {
121 let mut dst = self.uuid.as_mut();
122 dst.fill(0);
123 dst.write_all(uuid.as_bytes())?;
124 Ok(())
125 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900126}
127
128/// `DeviceMapper` is the entry point for the device mapper framework. It essentially is a file
129/// handle to "/dev/mapper/control".
130pub struct DeviceMapper(File);
131
Jiyong Park5f0ebea2021-06-07 12:53:35 +0900132#[cfg(not(target_os = "android"))]
133const MAPPER_CONTROL: &str = "/dev/mapper/control";
134#[cfg(not(target_os = "android"))]
135const MAPPER_DEV_ROOT: &str = "/dev/mapper";
136
137#[cfg(target_os = "android")]
138const MAPPER_CONTROL: &str = "/dev/device-mapper";
139#[cfg(target_os = "android")]
140const MAPPER_DEV_ROOT: &str = "/dev/block/mapper";
141
Jiyong Park86c9b082021-06-04 19:03:48 +0900142impl DeviceMapper {
143 /// Constructs a new `DeviceMapper` entrypoint. This is essentially the same as opening
144 /// "/dev/mapper/control".
145 pub fn new() -> Result<DeviceMapper> {
Jiyong Park0553ff22021-07-15 12:25:36 +0900146 let f = OpenOptions::new()
147 .read(true)
148 .write(true)
149 .open(MAPPER_CONTROL)
150 .context(format!("failed to open {}", MAPPER_CONTROL))?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900151 Ok(DeviceMapper(f))
152 }
153
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000154 /// Creates a (crypt) device and configure it according to the `target` specification.
155 /// The path to the generated device is "/dev/mapper/<name>".
156 pub fn create_crypt_device(&self, name: &str, target: &DmCryptTarget) -> Result<PathBuf> {
157 self.create_device(name, target.as_slice(), uuid("crypto".as_bytes())?, true)
158 }
159
160 /// Creates a (verity) device and configure it according to the `target` specification.
Jiyong Park86c9b082021-06-04 19:03:48 +0900161 /// The path to the generated device is "/dev/mapper/<name>".
Shikha Panwar414ea892022-10-12 13:45:52 +0000162 pub fn create_verity_device(&self, name: &str, target: &DmVerityTarget) -> Result<PathBuf> {
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000163 self.create_device(name, target.as_slice(), uuid("apkver".as_bytes())?, false)
164 }
165
166 /// Removes a mapper device.
167 pub fn delete_device_deferred(&self, name: &str) -> Result<()> {
168 let mut data = DmIoctl::new(name)?;
169 data.flags |= Flag::DM_DEFERRED_REMOVE;
170 dm_dev_remove(self, &mut data)
171 .context(format!("failed to remove device with name {}", &name))?;
172 Ok(())
173 }
174
175 fn create_device(
176 &self,
177 name: &str,
178 target: &[u8],
179 uid: String,
180 writable: bool,
181 ) -> Result<PathBuf> {
Jiyong Park86c9b082021-06-04 19:03:48 +0900182 // Step 1: create an empty device
Chris Wailes68c39f82021-07-27 16:03:44 -0700183 let mut data = DmIoctl::new(name)?;
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000184 data.set_uuid(&uid)?;
Chris Wailes68c39f82021-07-27 16:03:44 -0700185 dm_dev_create(self, &mut data)
Jiyong Park0553ff22021-07-15 12:25:36 +0900186 .context(format!("failed to create an empty device with name {}", &name))?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900187
188 // Step 2: load table onto the device
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000189 let payload_size = size_of::<DmIoctl>() + target.len();
Jiyong Park86c9b082021-06-04 19:03:48 +0900190
Chris Wailes68c39f82021-07-27 16:03:44 -0700191 let mut data = DmIoctl::new(name)?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900192 data.data_size = payload_size as u32;
193 data.data_start = size_of::<DmIoctl>() as u32;
194 data.target_count = 1;
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000195
196 if !writable {
197 data.flags |= Flag::DM_READONLY_FLAG;
198 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900199
200 let mut payload = Vec::with_capacity(payload_size);
Frederick Mayle8f795902023-10-23 15:48:34 -0700201 payload.extend_from_slice(data.as_bytes());
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000202 payload.extend_from_slice(target);
Chris Wailes68c39f82021-07-27 16:03:44 -0700203 dm_table_load(self, payload.as_mut_ptr() as *mut DmIoctl)
Jiyong Park0553ff22021-07-15 12:25:36 +0900204 .context("failed to load table")?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900205
206 // Step 3: activate the device (note: the term 'suspend' might be misleading, but it
207 // actually activates the table. See include/uapi/linux/dm-ioctl.h
Chris Wailes68c39f82021-07-27 16:03:44 -0700208 let mut data = DmIoctl::new(name)?;
209 dm_dev_suspend(self, &mut data).context("failed to activate")?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900210
211 // Step 4: wait unti the device is created and return the device path
Chris Wailes9b866f02022-11-16 15:17:16 -0800212 let path = Path::new(MAPPER_DEV_ROOT).join(name);
Jiyong Park86c9b082021-06-04 19:03:48 +0900213 wait_for_path(&path)?;
214 Ok(path)
215 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900216}
217
218/// Used to derive a UUID that uniquely identifies a device mapper device when creating it.
Shikha Panwar414ea892022-10-12 13:45:52 +0000219fn uuid(node_id: &[u8]) -> Result<String> {
Jiyong Parkf02061f2021-06-07 09:44:44 +0900220 use std::time::{SystemTime, UNIX_EPOCH};
221 use uuid::v1::{Context, Timestamp};
222 use uuid::Uuid;
223
224 let context = Context::new(0);
225 let now = SystemTime::now().duration_since(UNIX_EPOCH)?;
Charisee96113f32023-01-26 09:00:42 +0000226 let ts = Timestamp::from_unix(context, now.as_secs(), now.subsec_nanos());
Chris Wailesc7c11442023-01-26 15:25:27 -0800227 let uuid = Uuid::new_v1(ts, node_id.try_into()?);
228 Ok(String::from(uuid.hyphenated().encode_lower(&mut Uuid::encode_buffer())))
Jiyong Park86c9b082021-06-04 19:03:48 +0900229}
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000230
231#[cfg(test)]
Andrew Walbran31e059b2023-06-29 16:33:54 +0000232rdroidtest::test_main!();
Andrew Walbran3fcebdb2022-11-30 11:16:17 +0000233
234#[cfg(test)]
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000235mod tests {
236 use super::*;
Shikha Panwar8e48a172022-11-25 19:01:28 +0000237 use crypt::{CipherType, DmCryptTargetBuilder};
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000238 use rdroidtest::{ignore_if, rdroidtest};
Shikha Panwar8e48a172022-11-25 19:01:28 +0000239 use rustutils::system_properties;
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000240 use std::fs::{read, File, OpenOptions};
241 use std::io::Write;
242
Shikha Panwar8e48a172022-11-25 19:01:28 +0000243 // Just a logical set of keys to make testing easy. This has no real meaning.
244 struct KeySet<'a> {
245 cipher: CipherType,
246 key: &'a [u8],
247 different_key: &'a [u8],
248 }
249
250 const KEY_SET_XTS: KeySet = KeySet {
251 cipher: CipherType::AES256XTS,
252 key: b"sixtyfourbyteslongsentencearerarebutletsgiveitatrycantbethathard",
253 different_key: b"drahtahtebtnacyrtatievigsteltuberareraecnetnesgnolsetybruofytxis",
254 };
255 const KEY_SET_HCTR2: KeySet = KeySet {
256 cipher: CipherType::AES256HCTR2,
257 key: b"thirtytwobyteslongreallylongword",
258 different_key: b"drowgnolyllaergnolsetybowtytriht",
259 };
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000260
261 // Create a file in given temp directory with given size
262 fn prepare_tmpfile(test_dir: &Path, filename: &str, sz: u64) -> PathBuf {
263 let filepath = test_dir.join(filename);
264 let f = File::create(&filepath).unwrap();
265 f.set_len(sz).unwrap();
266 filepath
267 }
268
269 fn write_to_dev(path: &Path, data: &[u8]) {
Chris Wailes9b866f02022-11-16 15:17:16 -0800270 let mut f = OpenOptions::new().read(true).write(true).open(path).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000271 f.write_all(data).unwrap();
272 }
273
Shikha Panwar8e48a172022-11-25 19:01:28 +0000274 // TODO(b/250880499): delete_device() doesn't really delete it even without DM_DEFERRED_REMOVE.
275 // Hence, we have to create a new device with a different name for each test. Retrying
276 // the test on same machine without reboot will also fail.
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000277 fn delete_device(dm: &DeviceMapper, name: &str) -> Result<()> {
278 dm.delete_device_deferred(name)?;
Chris Wailes9b866f02022-11-16 15:17:16 -0800279 wait_for_path_disappears(Path::new(MAPPER_DEV_ROOT).join(name))?;
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000280 Ok(())
281 }
282
Shikha Panwar8e48a172022-11-25 19:01:28 +0000283 fn is_hctr2_supported() -> bool {
284 // hctr2 is NOT enabled in kernel 5.10 or lower. We run Microdroid tests on kernel versions
285 // 5.10 or above & therefore, we don't really care to skip test on other versions.
286 if let Some(version) = system_properties::read("ro.kernel.version")
287 .expect("Unable to read system property ro.kernel.version")
288 {
289 version != "5.10"
290 } else {
291 panic!("Could not read property: kernel.version!!");
292 }
293 }
294
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000295 #[rdroidtest]
Shikha Panwar8e48a172022-11-25 19:01:28 +0000296 fn mapping_again_keeps_data_xts() {
297 mapping_again_keeps_data(&KEY_SET_XTS, "name1");
298 }
299
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000300 #[rdroidtest]
301 #[ignore_if(!is_hctr2_supported())]
Shikha Panwar8e48a172022-11-25 19:01:28 +0000302 fn mapping_again_keeps_data_hctr2() {
Shikha Panwar8e48a172022-11-25 19:01:28 +0000303 mapping_again_keeps_data(&KEY_SET_HCTR2, "name2");
304 }
Andrew Walbran3fcebdb2022-11-30 11:16:17 +0000305
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000306 #[rdroidtest]
Shikha Panwar8e48a172022-11-25 19:01:28 +0000307 fn data_inaccessible_with_diff_key_xts() {
308 data_inaccessible_with_diff_key(&KEY_SET_XTS, "name3");
309 }
310
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000311 #[rdroidtest]
312 #[ignore_if(!is_hctr2_supported())]
Shikha Panwar8e48a172022-11-25 19:01:28 +0000313 fn data_inaccessible_with_diff_key_hctr2() {
Shikha Panwar8e48a172022-11-25 19:01:28 +0000314 data_inaccessible_with_diff_key(&KEY_SET_HCTR2, "name4");
315 }
316
317 fn mapping_again_keeps_data(keyset: &KeySet, device: &str) {
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000318 // This test creates 2 different crypt devices using same key backed by same data_device
319 // -> Write data on dev1 -> Check the data is visible & same on dev2
320 let dm = DeviceMapper::new().unwrap();
321 let inputimg = include_bytes!("../testdata/rand8k");
322 let sz = inputimg.len() as u64;
323
324 let test_dir = tempfile::TempDir::new().unwrap();
325 let backing_file = prepare_tmpfile(test_dir.path(), "storage", sz);
326 let data_device = loopdevice::attach(
327 backing_file,
328 0,
329 sz,
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000330 /* direct_io */ true,
331 /* writable */ true,
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000332 )
333 .unwrap();
Shikha Panwar8e48a172022-11-25 19:01:28 +0000334 let device_diff = device.to_owned() + "_diff";
335
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000336 scopeguard::defer! {
337 loopdevice::detach(&data_device).unwrap();
Dan Albert04e5dbd2023-05-08 22:49:40 +0000338 let _ignored1 = delete_device(&dm, device);
339 let _ignored2 = delete_device(&dm, &device_diff);
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000340 }
341
Shikha Panwar8e48a172022-11-25 19:01:28 +0000342 let target = DmCryptTargetBuilder::default()
343 .data_device(&data_device, sz)
344 .cipher(keyset.cipher)
345 .key(keyset.key)
346 .build()
347 .unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000348
Shikha Panwar8e48a172022-11-25 19:01:28 +0000349 let mut crypt_device = dm.create_crypt_device(device, &target).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000350 write_to_dev(&crypt_device, inputimg);
351
352 // Recreate another device using same target spec & check if the content is the same
Shikha Panwar8e48a172022-11-25 19:01:28 +0000353 crypt_device = dm.create_crypt_device(&device_diff, &target).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000354
355 let crypt = read(crypt_device).unwrap();
356 assert_eq!(inputimg.len(), crypt.len()); // fail early if the size doesn't match
357 assert_eq!(inputimg, crypt.as_slice());
358 }
359
Shikha Panwar8e48a172022-11-25 19:01:28 +0000360 fn data_inaccessible_with_diff_key(keyset: &KeySet, device: &str) {
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000361 // This test creates 2 different crypt devices using different keys backed
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000362 // by same data_device -> Write data on dev1 -> Check the data is visible but not the same
363 // on dev2
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000364 let dm = DeviceMapper::new().unwrap();
365 let inputimg = include_bytes!("../testdata/rand8k");
366 let sz = inputimg.len() as u64;
367
368 let test_dir = tempfile::TempDir::new().unwrap();
369 let backing_file = prepare_tmpfile(test_dir.path(), "storage", sz);
370 let data_device = loopdevice::attach(
371 backing_file,
372 0,
373 sz,
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000374 /* direct_io */ true,
375 /* writable */ true,
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000376 )
377 .unwrap();
Shikha Panwar8e48a172022-11-25 19:01:28 +0000378 let device_diff = device.to_owned() + "_diff";
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000379 scopeguard::defer! {
380 loopdevice::detach(&data_device).unwrap();
Dan Albert04e5dbd2023-05-08 22:49:40 +0000381 let _ignored1 = delete_device(&dm, device);
382 let _ignored2 = delete_device(&dm, &device_diff);
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000383 }
384
Shikha Panwar8e48a172022-11-25 19:01:28 +0000385 let target = DmCryptTargetBuilder::default()
386 .data_device(&data_device, sz)
387 .cipher(keyset.cipher)
388 .key(keyset.key)
389 .build()
390 .unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000391 let target2 = DmCryptTargetBuilder::default()
392 .data_device(&data_device, sz)
Shikha Panwar8e48a172022-11-25 19:01:28 +0000393 .cipher(keyset.cipher)
394 .key(keyset.different_key)
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000395 .build()
396 .unwrap();
397
Shikha Panwar8e48a172022-11-25 19:01:28 +0000398 let mut crypt_device = dm.create_crypt_device(device, &target).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000399
400 write_to_dev(&crypt_device, inputimg);
401
402 // Recreate the crypt device again diff key & check if the content is changed
Shikha Panwar8e48a172022-11-25 19:01:28 +0000403 crypt_device = dm.create_crypt_device(&device_diff, &target2).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000404 let crypt = read(crypt_device).unwrap();
405 assert_ne!(inputimg, crypt.as_slice());
406 }
407}