libcryptsetup_rs/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5//! This is a wrapper library for libcryptsetup. The intention is to provide as much safety as
6//! possible when crossing FFI boundaries to the cryptsetup C library.
7
8// Keyfile reading functions are supported through a workaround in these bindings due
9// to how memory is handled in these functions - memory for keys is allocated
10// and the corresponding free functions are not part of the public API.
11// The function is copied and pasted from libcryptsetup and compiled into the bindings
12// for now to work around this. This will be supported by libcryptsetup at a later
13// time.
14
15pub use either::Either;
16
17#[macro_use]
18mod macros;
19
20mod activate;
21mod backup;
22pub mod consts;
23mod context;
24mod debug;
25mod device;
26mod err;
27mod format;
28mod key;
29mod keyfile;
30mod keyslot;
31mod log;
32mod luks2;
33mod mem;
34mod runtime;
35mod settings;
36mod status;
37#[cfg(test)]
38mod tests;
39mod wipe;
40
41#[cfg(cryptsetup23supported)]
42pub use crate::mem::{SafeBorrowedMemZero, SafeMemzero, SafeOwnedMemZero};
43pub use crate::{
44    activate::CryptActivationHandle,
45    backup::CryptBackupHandle,
46    context::CryptContextHandle,
47    debug::set_debug_level,
48    device::{CryptDevice, CryptInit},
49    err::LibcryptErr,
50    format::{
51        CryptFormatHandle, CryptParamsIntegrity, CryptParamsIntegrityRef, CryptParamsLoopaes,
52        CryptParamsLoopaesRef, CryptParamsLuks1, CryptParamsLuks1Ref, CryptParamsLuks2,
53        CryptParamsLuks2Ref, CryptParamsPlain, CryptParamsPlainRef, CryptParamsTcrypt,
54        CryptParamsTcryptRef, CryptParamsVerity, CryptParamsVerityRef,
55    },
56    key::CryptVolumeKeyHandle,
57    keyfile::{CryptKeyfileContents, CryptKeyfileHandle},
58    keyslot::CryptKeyslotHandle,
59    log::{log, set_log_callback},
60    luks2::{
61        flags::CryptLuks2FlagsHandle,
62        reencrypt::{CryptLuks2ReencryptHandle, CryptParamsReencrypt, CryptParamsReencryptRef},
63        token::{register, CryptLuks2TokenHandle, CryptTokenInfo, TokenInput},
64    },
65    mem::SafeMemHandle,
66    runtime::{ActiveDevice, CryptRuntimeHandle},
67    settings::{CryptPbkdfType, CryptPbkdfTypeRef, CryptSettingsHandle},
68    status::{get_sector_size, status, CryptDeviceStatusHandle},
69    wipe::CryptWipeHandle,
70};
71
72/// Re-exports `libc` types in API
73pub use libc::{c_int, c_uint, size_t};
74
75/// Result type to be used with `libcryptsetup-rs`
76pub type Result<T> = std::result::Result<T, LibcryptErr>;
77
78#[cfg(feature = "mutex")]
79static MUTEX: std::sync::LazyLock<per_thread_mutex::PerThreadMutex> =
80    std::sync::LazyLock::new(per_thread_mutex::PerThreadMutex::default);
81
82#[cfg(not(feature = "mutex"))]
83static THREAD_ID: std::sync::LazyLock<std::thread::ThreadId> =
84    std::sync::LazyLock::new(|| std::thread::current().id());
85
86#[cfg(test)]
87mod test {
88    use crate::tests;
89
90    #[ignore]
91    #[test]
92    fn test_encrypt_by_password() {
93        tests::encrypt::test_encrypt_by_password();
94    }
95
96    #[ignore]
97    #[test]
98    #[cfg(cryptsetup24supported)]
99    fn test_reencrypt_by_password() {
100        tests::reencrypt::test_reencrypt_by_password();
101    }
102
103    #[ignore]
104    #[test]
105    fn test_encrypt_by_keyfile() {
106        tests::encrypt::test_encrypt_by_keyfile();
107    }
108
109    #[ignore]
110    #[test]
111    fn test_encrypt_by_password_without_explicit_format() {
112        tests::encrypt::test_encrypt_by_password_without_explicit_format();
113    }
114
115    #[ignore]
116    #[test]
117    fn test_unencrypted() {
118        tests::encrypt::test_unencrypted();
119    }
120
121    #[ignore]
122    #[test]
123    fn test_crypt_setup_free_exists() {
124        tests::keyfile::test_keyfile_cleanup();
125    }
126}