From 965c07df0a2286c069c362b9db5f9b2b3a69baff Mon Sep 17 00:00:00 2001 From: oupson Date: Fri, 7 Oct 2022 20:55:56 +0200 Subject: [PATCH] Rename content_enc to content --- .../src/{content_enc.rs => content.rs} | 0 .../src/filename/dir_filename_cipher.rs | 49 +++++++++++++++++++ rustcryptfs-lib/src/lib.rs | 4 +- rustcryptfs-linux/src/encrypted_filesystem.rs | 2 +- 4 files changed, 52 insertions(+), 3 deletions(-) rename rustcryptfs-lib/src/{content_enc.rs => content.rs} (100%) create mode 100644 rustcryptfs-lib/src/filename/dir_filename_cipher.rs diff --git a/rustcryptfs-lib/src/content_enc.rs b/rustcryptfs-lib/src/content.rs similarity index 100% rename from rustcryptfs-lib/src/content_enc.rs rename to rustcryptfs-lib/src/content.rs diff --git a/rustcryptfs-lib/src/filename/dir_filename_cipher.rs b/rustcryptfs-lib/src/filename/dir_filename_cipher.rs new file mode 100644 index 0000000..deb102a --- /dev/null +++ b/rustcryptfs-lib/src/filename/dir_filename_cipher.rs @@ -0,0 +1,49 @@ +use crate::error::FilenameDecryptError; +use cipher::{block_padding::Pkcs7, inout::InOutBufReserved, Iv, Key, KeyIvInit}; + +use super::{EmeCipher, EncodedFilename, IntoDecodable}; + +pub struct DirFilenameCipher<'a, 'b> { + filename_key: &'a Key, + iv: &'b Iv, +} + +impl<'a, 'b> DirFilenameCipher<'a, 'b> { + pub fn new(filename_key: &'a Key, iv: &'b Iv) -> Self { + Self { filename_key, iv } + } + + pub fn decode_filename(&self, name: S) -> Result + where + S: IntoDecodable, + { + let cipher = EmeCipher::new(self.filename_key, self.iv); + + let mut filename = base64::decode_config(name.to_decodable(), base64::URL_SAFE_NO_PAD)?; + let filename_decoded = cipher + .decrypt_padded_mut::(&mut filename) + .map_err(|_| FilenameDecryptError::DecryptError())?; + + Ok(String::from_utf8_lossy(filename_decoded).to_string()) + } + + pub fn encrypt_filename( + &self, + plain_text_name: &str, + ) -> Result { + let mut cipher = EmeCipher::new(self.filename_key, self.iv); + let mut res = [0u8; 2048]; + + let filename_encrypted = cipher + .encrypt_padded_inout_mut::( + InOutBufReserved::from_slices(plain_text_name.as_bytes(), &mut res).unwrap(), + ) + .map_err(|_| FilenameDecryptError::DecryptError())?; // TODO RENAME ERROR + + // TODO LONG FILENAME + + let filename = base64::encode_config(filename_encrypted, base64::URL_SAFE_NO_PAD); + + Ok(filename.into()) + } +} diff --git a/rustcryptfs-lib/src/lib.rs b/rustcryptfs-lib/src/lib.rs index e785bb2..e46bf4e 100644 --- a/rustcryptfs-lib/src/lib.rs +++ b/rustcryptfs-lib/src/lib.rs @@ -2,11 +2,11 @@ use std::{fs::File, io::Read, path::Path}; -use content_enc::ContentEnc; +use content::ContentEnc; use filename::FilenameCipher; pub mod config; -pub mod content_enc; +pub mod content; pub mod error; pub mod filename; diff --git a/rustcryptfs-linux/src/encrypted_filesystem.rs b/rustcryptfs-linux/src/encrypted_filesystem.rs index 1837a74..3331375 100644 --- a/rustcryptfs-linux/src/encrypted_filesystem.rs +++ b/rustcryptfs-linux/src/encrypted_filesystem.rs @@ -10,7 +10,7 @@ use std::{ }; use fuser::{FileAttr, FileType, Filesystem, FUSE_ROOT_ID}; -use rustcryptfs_lib::{content_enc::ContentEnc, GocryptFs}; +use rustcryptfs_lib::{content::ContentEnc, GocryptFs}; use crate::{ error::Result,