Skip to content

Commit

Permalink
Use sync encrypt and decrypt reader
Browse files Browse the repository at this point in the history
  • Loading branch information
bancek committed Dec 1, 2023
1 parent 3ffaa26 commit c55be50
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 149 deletions.
3 changes: 1 addition & 2 deletions vault-core-tests/src/fixtures/repo_fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ impl RepoFixture {
})
}

pub async fn unlock(&self) {
pub fn unlock(&self) {
self.vault
.repos_service
.unlock_repo(&self.repo_id, "password", RepoUnlockMode::Unlock)
.await
.unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion vault-core-tests/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn with_repo(

user_fixture.load().await;

repo_fixture.unlock().await;
repo_fixture.unlock();

f(repo_fixture).await;
}
Expand Down
1 change: 0 additions & 1 deletion vault-core-tests/tests/integration/repo_create_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ fn test_create() {
.vault
.repos_service
.unlock_repo(&repo_id, "password", RepoUnlockMode::Unlock)
.await
.unwrap();
fixture
.vault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn test_repo_lock_unlock_remove() {
}
);

fixture.unlock().await;
fixture.unlock();

let state_after_unlock = get_state();
assert_eq!(
Expand Down Expand Up @@ -597,7 +597,7 @@ fn test_eventstream() {
let fixture1 = fixture.new_session();
fixture1.user_fixture.login();
fixture1.user_fixture.load().await;
fixture1.unlock().await;
fixture1.unlock();

let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ fn test_repo_lock_unlock_remove() {
}
);

fixture.unlock().await;
fixture.unlock();

let state_after_unlock = get_state();
assert_eq!(
Expand Down Expand Up @@ -749,7 +749,7 @@ fn test_eventstream() {
let fixture1 = fixture.new_session();
fixture1.user_fixture.login();
fixture1.user_fixture.load().await;
fixture1.unlock().await;
fixture1.unlock();

let (details_id, load_future) = fixture.vault.repo_files_details_create(
fixture.repo_id.clone(),
Expand Down
2 changes: 1 addition & 1 deletion vault-core-tests/tests/integration/repo_files_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn test_repo_lock_unlock_remove() {

assert_eq!(state_after_lock, RepoFilesState::default());

fixture.unlock().await;
fixture.unlock();

let state_after_unlock = get_state();

Expand Down
85 changes: 41 additions & 44 deletions vault-core/src/cipher/cipher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use futures::{io::Cursor, AsyncReadExt};
use std::{str, sync::Arc};
use std::{
io::{Cursor, Read},
str,
sync::Arc,
};
use xsalsa20poly1305::XSalsa20Poly1305;

use crate::types::{DecryptedName, DecryptedPath, EncryptedName, EncryptedPath};
Expand All @@ -8,8 +11,8 @@ use super::{
cipher_keys::{derive_keys, DerivedKeys},
constants::{DATA_KEY_LEN, NAME_CIPHER_BLOCK_SIZE, NAME_KEY_LEN},
data_cipher::get_data_cipher,
decrypt_reader::AsyncDecryptReader,
encrypt_reader::AsyncEncryptReader,
decrypt_reader::{AsyncDecryptReader, SyncDecryptReader},
encrypt_reader::{AsyncEncryptReader, SyncEncryptReader},
errors::DecryptFilenameError,
name_cipher::{
decrypt_filename, decrypt_path, encrypt_filename, encrypt_path, get_name_cipher,
Expand Down Expand Up @@ -90,35 +93,35 @@ impl Cipher {
AsyncEncryptReader::new(reader, self.data_cipher.clone(), nonce)
}

pub async fn encrypt_data(
&self,
data: &[u8],
out: &mut Vec<u8>,
) -> Result<usize, std::io::Error> {
pub fn encrypt_reader_sync<R>(&self, reader: R) -> SyncEncryptReader<R> {
let nonce = Nonce::new_random().unwrap();

SyncEncryptReader::new(reader, self.data_cipher.clone(), nonce)
}

pub fn encrypt_data(&self, data: &[u8], out: &mut Vec<u8>) -> Result<usize, std::io::Error> {
let reader = Cursor::new(data);

self.encrypt_reader_async(reader).read_to_end(out).await
self.encrypt_reader_sync(reader).read_to_end(out)
}

pub fn decrypt_reader_async<R>(&self, reader: R) -> AsyncDecryptReader<R> {
AsyncDecryptReader::new(reader, self.data_cipher.clone())
}

pub async fn decrypt_data(
&self,
data: &[u8],
out: &mut Vec<u8>,
) -> Result<usize, std::io::Error> {
pub fn decrypt_reader_sync<R>(&self, reader: R) -> SyncDecryptReader<R> {
SyncDecryptReader::new(reader, self.data_cipher.clone())
}

pub fn decrypt_data(&self, data: &[u8], out: &mut Vec<u8>) -> Result<usize, std::io::Error> {
let reader = Cursor::new(data);

self.decrypt_reader_async(reader).read_to_end(out).await
self.decrypt_reader_sync(reader).read_to_end(out)
}
}

#[cfg(test)]
mod tests {
use futures::executor::block_on;

use crate::types::{DecryptedName, DecryptedPath, EncryptedName, EncryptedPath};

use super::Cipher;
Expand Down Expand Up @@ -293,47 +296,41 @@ mod tests {

#[test]
fn test_encrypt_data() {
block_on(async {
// tested with rclone 1.60
let cipher = Cipher::new("testpassword", None);
// tested with rclone 1.60
let cipher = Cipher::new("testpassword", None);

let mut encrypted = Vec::new();
let mut encrypted = Vec::new();

let res = cipher
.encrypt_data("testdata".as_bytes(), &mut encrypted)
.await;
let res = cipher.encrypt_data("testdata".as_bytes(), &mut encrypted);

assert_eq!(res.unwrap(), 56);
assert_eq!(res.unwrap(), 56);

let mut decrypted = Vec::new();
let mut decrypted = Vec::new();

let res = cipher.decrypt_data(&encrypted, &mut decrypted).await;
let res = cipher.decrypt_data(&encrypted, &mut decrypted);

assert_eq!(res.unwrap(), 8);
assert_eq!(res.unwrap(), 8);

assert_eq!(std::str::from_utf8(&decrypted).unwrap(), "testdata");
})
assert_eq!(std::str::from_utf8(&decrypted).unwrap(), "testdata");
}

#[test]
fn test_decrypt_data() {
block_on(async {
// tested with rclone 1.60
let cipher = Cipher::new("testpassword", None);
// tested with rclone 1.60
let cipher = Cipher::new("testpassword", None);

let encrypted = vec![
82, 67, 76, 79, 78, 69, 0, 0, 209, 27, 246, 23, 134, 105, 131, 148, 3, 49, 228, 74,
200, 43, 245, 170, 123, 102, 24, 137, 45, 77, 53, 115, 206, 216, 221, 4, 40, 177,
52, 14, 5, 190, 84, 192, 246, 157, 207, 154, 11, 178, 94, 181, 135, 59, 240, 115,
];
let encrypted = vec![
82, 67, 76, 79, 78, 69, 0, 0, 209, 27, 246, 23, 134, 105, 131, 148, 3, 49, 228, 74,
200, 43, 245, 170, 123, 102, 24, 137, 45, 77, 53, 115, 206, 216, 221, 4, 40, 177, 52,
14, 5, 190, 84, 192, 246, 157, 207, 154, 11, 178, 94, 181, 135, 59, 240, 115,
];

let mut decrypted = Vec::new();
let mut decrypted = Vec::new();

let res = cipher.decrypt_data(&encrypted, &mut decrypted).await;
let res = cipher.decrypt_data(&encrypted, &mut decrypted);

assert_eq!(res.unwrap(), 8);
assert_eq!(res.unwrap(), 8);

assert_eq!(std::str::from_utf8(&decrypted).unwrap(), "testdata");
})
assert_eq!(std::str::from_utf8(&decrypted).unwrap(), "testdata");
}
}
4 changes: 2 additions & 2 deletions vault-core/src/repo_config_backup/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ impl RepoConfigBackupService {
.mutate(|state, notify, _, _| mutations::create(state, notify, repo_id))
}

pub async fn generate(&self, backup_id: u32, password: &str) -> Result<(), UnlockRepoError> {
pub fn generate(&self, backup_id: u32, password: &str) -> Result<(), UnlockRepoError> {
let repo_id = self
.store
.mutate(|state, notify, _, _| mutations::generating(state, notify, backup_id))?;

let res = self.repos_service.get_repo_config(&repo_id, password).await;
let res = self.repos_service.get_repo_config(&repo_id, password);

let res_err = res.as_ref().map(|_| ()).map_err(|err| err.clone());

Expand Down
7 changes: 2 additions & 5 deletions vault-core/src/repo_unlock/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ impl RepoUnlockService {
})
}

pub async fn unlock(&self, unlock_id: u32, password: &str) -> Result<(), UnlockRepoError> {
pub fn unlock(&self, unlock_id: u32, password: &str) -> Result<(), UnlockRepoError> {
let (repo_id, mode) = self
.store
.mutate(|state, notify, _, _| mutations::unlocking(state, notify, unlock_id))?;

let res = self
.repos_service
.unlock_repo(&repo_id, password, mode)
.await;
let res = self.repos_service.unlock_repo(&repo_id, password, mode);

self.store.mutate(|state, notify, _, _| {
mutations::unlocked(state, notify, unlock_id, res.clone());
Expand Down
Loading

0 comments on commit c55be50

Please sign in to comment.