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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! # Encrypted File System
//!
//! An encrypted file system that mounts with FUSE on Linux. It can be used to create encrypted directories.
//!
//! # Usage
//!
//! It can be used a library to create an encrypted file system or mount it with FUSE.
//!
//! This crate also contains examples and a `main.rs` file that can be used as examples on how to run the encrypted file system from the command line.
//! Documentation for that can be found [here](https://github.com/radumarias/rencfs#command-line-tool).
//!
//! In the following example, we will see how we can use it as a library.
//!
//! ## Using [`mount::create_mount_point`] on Linux
//!
//! ### Example
//!
//! ```no_run
//! use std::env::args;
//! use std::path::Path;
//! use std::io;
//! use tracing::info;
//!
//! use anyhow::Result;
//! use shush_rs::SecretString;
//!
//! use rencfs::encryptedfs::PasswordProvider;
//! use rencfs::mount::create_mount_point;
//! use rencfs::mount::MountPoint;
//!
//! /// This will mount and expose the mount point until you press `Enter`, then it will umount and close the program.
//! #[tokio::main]
//! #[allow(clippy::type_complexity)]
//! async fn main() -> Result<()> {
//! tracing_subscriber::fmt().init();
//!
//! let mut args = args();
//! args.next(); // skip program name
//! let mount_path = args.next().expect("mount_path expected");
//! let data_path = args.next().expect("data_path expected");
//! use rencfs::crypto::Cipher;
//!
//! struct PasswordProviderImpl {}
//! impl PasswordProvider for PasswordProviderImpl {
//! fn get_password(&self) -> Option<SecretString> {
//! // placeholder password, use some secure way to get the password like with [keyring](https://crates.io/crates/keyring) crate
//! Some(SecretString::new(Box::new(String::from("pass42"))))
//! }
//! }
//! let mount_point = create_mount_point(
//! Path::new(&mount_path),
//! Path::new(&data_path),
//! Box::new(PasswordProviderImpl {}),
//! Cipher::ChaCha20Poly1305,
//! false,
//! false,
//! false,
//! );
//! let handle = mount_point.mount().await?;
//! let mut buffer = String::new();
//! io::stdin().read_line(&mut buffer)?;
//! info!("Unmounting...");
//! info!("Bye!");
//! handle.umount().await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Or directly work with [`encryptedfs::EncryptedFs`]
//!
//! You need to specify several parameters to create an encrypted file system:
//! - `data_dir`: The directory where the file system will be mounted.
//! - `password`: The password to encrypt/decrypt the data.
//! - `Cipher`: The encryption algorithm to use.
//!
//! Currently, it supports these ciphers [Cipher](crypto::Cipher).
//!
//! ### Example
//!
//! ```
//! #![allow(unused_imports)]
//! use std::fs;
//! use shush_rs::SecretString;
//! use rencfs::encryptedfs::{EncryptedFs, FileType, PasswordProvider, CreateFileAttr};
//! use rencfs::crypto::Cipher;
//! use anyhow::Result;
//! use std::path::Path;
//! use rencfs::encryptedfs::write_all_string_to_fs;
//!
//! const ROOT_INODE: u64 = 1;
//!
//! struct PasswordProviderImpl {}
//! impl PasswordProvider for PasswordProviderImpl {
//! fn get_password(&self) -> Option<SecretString> {
//! // placeholder password, use some secure way to get the password like with [keyring](https://crates.io/crates/keyring) crate
//! Some(SecretString::new(Box::new(String::from("pass42"))))
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! tracing_subscriber::fmt().init();
//!
//! let data_dir = Path::new("/tmp/rencfs_data_test").to_path_buf();
//! let _ = fs::remove_dir_all(data_dir.to_str().unwrap());
//! let cipher = Cipher::ChaCha20Poly1305;
//! let mut fs = EncryptedFs::new(data_dir.clone(), Box::new(PasswordProviderImpl{}), cipher, false).await?;
//!
//! let file1 = SecretString::new(Box::new(String::from("file-1")));
//! let (fh, attr) = fs.create(ROOT_INODE, &file1, file_attr(), false, true).await?;
//! let data = "Hello, world!";
//! write_all_string_to_fs( &fs, attr.ino, 0,data, fh).await?;
//! fs.flush(fh).await?;
//! fs.release(fh).await?;
//! let fh = fs.open(attr.ino, true, false).await?;
//! let mut buf = vec![0; data.len()];
//! fs.read(attr.ino, 0, &mut buf, fh).await?;
//! fs.release(fh).await?;
//! assert_eq!(data, String::from_utf8(buf)?);
//! fs::remove_dir_all(data_dir)?;
//!
//! Ok(())
//! }
//!
//! fn file_attr() -> CreateFileAttr {
//! CreateFileAttr {
//! kind: FileType::RegularFile,
//! perm: 0o644,
//! uid: 0,
//! gid: 0,
//! rdev: 0,
//! flags: 0,
//! }
//! }
//! ```
//! ## Change password from code
//!
//! ### Example
//!
//! ```no_run
//! use rencfs::crypto::Cipher;
//! use rencfs::encryptedfs::{EncryptedFs, FsError};
//! use shush_rs::SecretString;
//! use std::env::args;
//! use std::path::Path;
//!
//! #[tokio::main]
//! async fn main() {
//! tracing_subscriber::fmt().init();
//!
//! let mut args = args();
//! let _ = args.next(); // skip the program name
//! let data_dir = args.next().expect("data_dir is missing");
//!
//! match EncryptedFs::passwd(
//! Path::new(&data_dir),
//! SecretString::new(Box::new(String::from("old-pass"))),
//! SecretString::new(Box::new(String::from("new-pass"))),
//! Cipher::ChaCha20Poly1305,
//! )
//! .await
//! {
//! Ok(_) => println!("Password changed successfully"),
//! Err(FsError::InvalidPassword) => println!("Invalid old password"),
//! Err(FsError::InvalidDataDirStructure) => println!("Invalid structure of data directory"),
//! Err(err) => println!("Error: {err}"),
//! }
//! }
//! ```
//! ## Change password from CLI using [rpassword](https://crates.io/crates/rpassword) crate
//!
//! ### Example
//!
//! ```no_run
//! use std::env::args;
//! use std::io;
//! use std::io::Write;
//!
//! use rpassword::read_password;
//! use shush_rs::{ExposeSecret, SecretString};
//! use tracing::{error, info};
//!
//! use rencfs::encryptedfs::{EncryptedFs, FsError};
//! #[tokio::main]
//! async fn main() {
//! tracing_subscriber::fmt().init();
//!
//! let mut args = args();
//! let _ = args.next(); // skip the program name
//! let data_dir = args.next().expect("data_dir is missing");
//!
//! use std::path::Path;
//! // read password from stdin
//! use rencfs::crypto::Cipher;
//! print!("Enter old password: ");
//! io::stdout().flush().unwrap();
//! let old_password = SecretString::new(Box::new(read_password().unwrap()));
//! print!("Enter new password: ");
//! io::stdout().flush().unwrap();
//! let new_password = SecretString::new(Box::new(read_password().unwrap()));
//! print!("Confirm new password: ");
//! io::stdout().flush().unwrap();
//! let new_password2 = SecretString::new(Box::new(read_password().unwrap()));
//! if new_password.expose_secret() != new_password2.expose_secret() {
//! error!("Passwords do not match");
//! return;
//! }
//! println!("Changing password...");
//! match EncryptedFs::passwd(
//! Path::new(&data_dir),
//! old_password,
//! new_password,
//! Cipher::ChaCha20Poly1305,
//! )
//! .await
//! {
//! Ok(_) => info!("Password changed successfully"),
//! Err(FsError::InvalidPassword) => error!("Invalid old password"),
//! Err(FsError::InvalidDataDirStructure) => error!("Invalid structure of data directory"),
//! Err(err) => error!("Error: {err}"),
//! }
//! }
//! ```
//!
//! ## Encrypted Writer and Reader
//!
//! We also expose a Writer and Reader in encrypted format, which implements [`std::io::Write`], [`std::io::Read`] and [`std::io::Seek`].
//! You can wrap any [`std::io::Write`] and [`std::io::Read`], like a file, to write and read encrypted content.
//! This is using [ring](https://crates.io/crates/ring) crate to handle encryption.
//!
//! ### Example
//! ```no_run
//! use anyhow::Result;
//! use rand_core::RngCore;
//! use std::env::args;
//! use std::fs::File;
//! use std::io;
//! use std::io::Write;
//! use std::path::Path;
//! use std::sync::Arc;
//!
//! use shush_rs::SecretVec;
//! use tracing::info;
//!
//! use rencfs::crypto;
//! use rencfs::crypto::write::CryptoWrite;
//! use rencfs::crypto::Cipher;
//!
//! fn main() -> Result<()> {
//! tracing_subscriber::fmt().init();
//!
//! let cipher = Cipher::ChaCha20Poly1305;
//! let mut key = vec![0; cipher.key_len()];
//! crypto::create_rng().fill_bytes(key.as_mut_slice());
//! let key = SecretVec::from(key);
//!
//! let mut args = args();
//! // skip the program name
//! let _ = args.next();
//! // will encrypt this file
//! let path_in = args.next().expect("path_in is missing");
//! // will save it in the same directory with .enc suffix
//! let out = Path::new(&path_in).to_path_buf().with_extension("enc");
//! if out.exists() {
//! std::fs::remove_file(&out)?;
//! }
//!
//! let mut file = File::open(path_in.clone())?;
//! let mut writer = crypto::create_write(File::create(out.clone())?, cipher, &key);
//! info!("encrypt file");
//! io::copy(&mut file, &mut writer).unwrap();
//! writer.finish()?;
//!
//! let mut reader = crypto::create_read(File::open(out)?, cipher, &key);
//! info!("read file and compare hash to original one");
//! let hash1 = crypto::hash_reader(&mut File::open(path_in)?)?;
//! let hash2 = crypto::hash_reader(&mut reader)?;
//! assert_eq!(hash1, hash2);
//!
//! Ok(())
//! }
//! ```
extern crate test;
use LazyLock;
pub
pub static UID: = new;
pub static GID: = new;
pub const