1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::{
os::raw::{c_char, c_int, c_void},
path::Path,
ptr,
};
use crate::{consts::vals::EncryptionFormat, device::CryptDevice, err::LibcryptErr};
use either::Either;
use uuid::Uuid;
pub struct CryptContextHandle<'a> {
reference: &'a mut CryptDevice,
}
impl<'a> CryptContextHandle<'a> {
pub(crate) fn new(reference: &'a mut CryptDevice) -> Self {
CryptContextHandle { reference }
}
pub fn format<T>(
&mut self,
type_: EncryptionFormat,
cipher_and_mode: (&str, &str),
uuid: Option<Uuid>,
volume_key: Either<&[u8], usize>,
params: Option<&mut T>,
) -> Result<(), LibcryptErr> {
let uuid_ptr = uuid
.as_ref()
.map(|u| u.as_bytes().as_ptr())
.unwrap_or(ptr::null()) as *const c_char;
let (volume_key_ptr, volume_key_len) = match volume_key {
Either::Left(vk) => (to_byte_ptr!(vk), vk.len()),
Either::Right(len) => (ptr::null(), len),
};
let (cipher, cipher_mode) = cipher_and_mode;
let cipher_cstring = to_cstring!(cipher)?;
let cipher_mode_cstring = to_cstring!(cipher_mode)?;
errno!(mutex!(libcryptsetup_rs_sys::crypt_format(
self.reference.as_ptr(),
type_.as_ptr(),
cipher_cstring.as_ptr(),
cipher_mode_cstring.as_ptr(),
uuid_ptr,
volume_key_ptr,
volume_key_len,
params
.map(|p| p as *mut _ as *mut c_void)
.unwrap_or(ptr::null_mut()),
)))?;
Ok(())
}
pub fn convert<T>(
&mut self,
type_: EncryptionFormat,
params: &mut T,
) -> Result<(), LibcryptErr> {
errno!(mutex!(libcryptsetup_rs_sys::crypt_convert(
self.reference.as_ptr(),
type_.as_ptr(),
params as *mut _ as *mut c_void,
)))
}
pub fn set_uuid(&mut self, uuid: Option<Uuid>) -> Result<(), LibcryptErr> {
let uptr = match uuid {
Some(u) => u.as_bytes().as_ptr() as *const c_char,
None => std::ptr::null(),
};
errno!(mutex!(libcryptsetup_rs_sys::crypt_set_uuid(
self.reference.as_ptr(),
uptr
)))
}
pub fn set_label(
&mut self,
label: Option<&str>,
subsystem_label: Option<&str>,
) -> Result<(), LibcryptErr> {
let (lcstring, slcstring) = match (label, subsystem_label) {
(Some(l), Some(sl)) => (Some(to_cstring!(l)?), Some(to_cstring!(sl)?)),
(Some(l), _) => (Some(to_cstring!(l)?), None),
(_, Some(sl)) => (None, Some(to_cstring!(sl)?)),
(_, _) => (None, None),
};
errno!(mutex!(libcryptsetup_rs_sys::crypt_set_label(
self.reference.as_ptr(),
lcstring.map(|cs| cs.as_ptr()).unwrap_or(ptr::null()),
slcstring.map(|cs| cs.as_ptr()).unwrap_or(ptr::null()),
)))
}
pub fn volume_key_keyring(&mut self, enable: bool) -> Result<(), LibcryptErr> {
errno!(mutex!(libcryptsetup_rs_sys::crypt_volume_key_keyring(
self.reference.as_ptr(),
enable as c_int
)))
}
pub fn load<T>(
&mut self,
type_: Option<EncryptionFormat>,
params: Option<&mut T>,
) -> Result<(), LibcryptErr> {
errno!(mutex!(libcryptsetup_rs_sys::crypt_load(
self.reference.as_ptr(),
type_.map(|t| t.as_ptr()).unwrap_or(ptr::null()),
params
.map(|p| p as *mut _ as *mut c_void)
.unwrap_or(ptr::null_mut()),
)))?;
Ok(())
}
pub fn repair<T>(
&mut self,
type_: EncryptionFormat,
params: &mut T,
) -> Result<(), LibcryptErr> {
errno!(mutex!(libcryptsetup_rs_sys::crypt_repair(
self.reference.as_ptr(),
type_.as_ptr(),
params as *mut _ as *mut c_void,
)))
}
pub fn resize(&mut self, name: &str, new_size: u64) -> Result<(), LibcryptErr> {
let name_cstring = to_cstring!(name)?;
errno!(mutex!(libcryptsetup_rs_sys::crypt_resize(
self.reference.as_ptr(),
name_cstring.as_ptr(),
new_size,
)))
}
pub fn suspend(&mut self, name: &str) -> Result<(), LibcryptErr> {
let name_cstring = to_cstring!(name)?;
errno!(mutex!(libcryptsetup_rs_sys::crypt_suspend(
self.reference.as_ptr(),
name_cstring.as_ptr()
)))
}
pub fn resume_by_passphrase(
&mut self,
name: &str,
keyslot: c_int,
passphrase: &str,
) -> Result<c_int, LibcryptErr> {
let name_cstring = to_cstring!(name)?;
let passphrase_cstring = to_cstring!(passphrase)?;
errno_int_success!(mutex!(libcryptsetup_rs_sys::crypt_resume_by_passphrase(
self.reference.as_ptr(),
name_cstring.as_ptr(),
keyslot,
passphrase_cstring.as_ptr(),
passphrase.len() as crate::size_t,
)))
}
pub fn resume_by_keyfile_device_offset(
&mut self,
name: &str,
keyslot: c_int,
keyfile: &Path,
keyfile_size: crate::size_t,
keyfile_offset: u64,
) -> Result<c_int, LibcryptErr> {
let name_cstring = to_cstring!(name)?;
let keyfile_cstring = path_to_cstring!(keyfile)?;
errno_int_success!(mutex!(
libcryptsetup_rs_sys::crypt_resume_by_keyfile_device_offset(
self.reference.as_ptr(),
name_cstring.as_ptr(),
keyslot,
keyfile_cstring.as_ptr(),
keyfile_size,
keyfile_offset,
)
))
}
}