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::{
ffi::CString,
os::raw::{c_int, c_void},
ptr,
};
use crate::{
consts::{
flags::CryptReencrypt,
vals::{CryptReencryptDirectionInfo, CryptReencryptInfo, CryptReencryptModeInfo},
},
device::CryptDevice,
err::LibcryptErr,
format::{CryptParamsLuks2, CryptParamsLuks2Ref},
};
type ReencryptProgress = unsafe extern "C" fn(size: u64, offset: u64, *mut c_void) -> c_int;
pub struct CryptParamsReencryptRef<'a> {
#[allow(missing_docs)]
pub inner: libcryptsetup_rs_sys::crypt_params_reencrypt,
#[allow(dead_code)]
reference: &'a CryptParamsReencrypt,
#[allow(dead_code)]
luks2_params: CryptParamsLuks2Ref<'a>,
#[allow(dead_code)]
resilience_cstring: CString,
#[allow(dead_code)]
hash_cstring: CString,
}
pub struct CryptParamsReencrypt {
pub mode: CryptReencryptModeInfo,
pub direction: CryptReencryptDirectionInfo,
#[allow(missing_docs)]
pub resilience: String,
#[allow(missing_docs)]
pub hash: String,
#[allow(missing_docs)]
pub data_shift: u64,
#[allow(missing_docs)]
pub max_hotzone_size: u64,
pub device_size: u64,
pub luks2: CryptParamsLuks2,
pub flags: CryptReencrypt,
}
impl<'a> TryInto<CryptParamsReencryptRef<'a>> for &'a CryptParamsReencrypt {
type Error = LibcryptErr;
fn try_into(self) -> Result<CryptParamsReencryptRef<'a>, Self::Error> {
let luks2_params: CryptParamsLuks2Ref<'a> = (&self.luks2).try_into()?;
let resilience_cstring = to_cstring!(self.resilience)?;
let hash_cstring = to_cstring!(self.hash)?;
let inner = libcryptsetup_rs_sys::crypt_params_reencrypt {
mode: self.mode.into(),
direction: self.direction.into(),
resilience: resilience_cstring.as_ptr(),
hash: hash_cstring.as_ptr(),
data_shift: self.data_shift,
max_hotzone_size: self.max_hotzone_size,
device_size: self.device_size,
luks2: &luks2_params.inner as *const _,
flags: self.flags.bits(),
};
Ok(CryptParamsReencryptRef {
inner,
reference: self,
luks2_params,
resilience_cstring,
hash_cstring,
})
}
}
pub struct CryptLuks2ReencryptHandle<'a> {
reference: &'a mut CryptDevice,
}
impl<'a> CryptLuks2ReencryptHandle<'a> {
pub(crate) fn new(reference: &'a mut CryptDevice) -> Self {
CryptLuks2ReencryptHandle { reference }
}
pub fn reencrypt_init_by_passphrase(
&mut self,
name: Option<&str>,
passphrase: &[u8],
keyslot_old: c_int,
keyslot_new: c_int,
cipher_and_mode: (&str, &str),
params: CryptParamsReencrypt,
) -> Result<c_int, LibcryptErr> {
let name_cstring = match name {
Some(n) => Some(to_cstring!(n)?),
None => None,
};
let (cipher, cipher_mode) = cipher_and_mode;
let params_reencrypt: CryptParamsReencryptRef<'_> = (¶ms).try_into()?;
let cipher_cstring = to_cstring!(cipher)?;
let cipher_mode_cstring = to_cstring!(cipher_mode)?;
errno_int_success!(mutex!(
libcryptsetup_rs_sys::crypt_reencrypt_init_by_passphrase(
self.reference.as_ptr(),
name_cstring.map(|cs| cs.as_ptr()).unwrap_or(ptr::null()),
to_byte_ptr!(passphrase),
passphrase.len(),
keyslot_old,
keyslot_new,
cipher_cstring.as_ptr(),
cipher_mode_cstring.as_ptr(),
¶ms_reencrypt.inner as *const _,
)
))
}
pub fn reecrypt_init_by_keyring(
&mut self,
name: Option<&str>,
key_description: &str,
keyslot_old: c_int,
keyslot_new: c_int,
cipher_and_mode: (&str, &str),
params: CryptParamsReencrypt,
) -> Result<c_int, LibcryptErr> {
let name_cstring = match name {
Some(n) => Some(to_cstring!(n)?),
None => None,
};
let (cipher, cipher_mode) = cipher_and_mode;
let params_reencrypt: CryptParamsReencryptRef<'_> = (¶ms).try_into()?;
let description_cstring = to_cstring!(key_description)?;
let cipher_cstring = to_cstring!(cipher)?;
let cipher_mode_cstring = to_cstring!(cipher_mode)?;
errno_int_success!(mutex!(
libcryptsetup_rs_sys::crypt_reencrypt_init_by_keyring(
self.reference.as_ptr(),
name_cstring.map(|cs| cs.as_ptr()).unwrap_or(ptr::null()),
description_cstring.as_ptr(),
keyslot_old,
keyslot_new,
cipher_cstring.as_ptr(),
cipher_mode_cstring.as_ptr(),
¶ms_reencrypt.inner as *const _,
)
))
}
pub fn reencrypt(&mut self, progress: Option<ReencryptProgress>) -> Result<(), LibcryptErr> {
errno!(mutex!(libcryptsetup_rs_sys::crypt_reencrypt(
self.reference.as_ptr(),
progress
)))
}
#[cfg(cryptsetup24supported)]
pub fn reencrypt2<T>(
&mut self,
progress: Option<ReencryptProgress>,
usrdata: Option<&mut T>,
) -> Result<(), LibcryptErr> {
let usrptr = usrdata
.map(|data| data as *mut _ as *mut c_void)
.unwrap_or_else(ptr::null_mut);
errno!(mutex!(libcryptsetup_rs_sys::crypt_reencrypt_run(
self.reference.as_ptr(),
progress,
usrptr,
)))
}
pub fn status(
&mut self,
params: CryptParamsReencrypt,
) -> Result<CryptReencryptInfo, LibcryptErr> {
let mut params_reencrypt: CryptParamsReencryptRef<'_> = (¶ms).try_into()?;
try_int_to_return!(
mutex!(libcryptsetup_rs_sys::crypt_reencrypt_status(
self.reference.as_ptr(),
&mut params_reencrypt.inner as *mut _,
)),
CryptReencryptInfo
)
}
}