2021-06-25 23:01:50 +02:00
|
|
|
pub mod cli;
|
|
|
|
pub mod crypto;
|
|
|
|
|
2021-07-04 19:24:20 +02:00
|
|
|
use std::{fs::File, path::Path, io::{self, Read, Write}};
|
2021-06-27 20:35:23 +02:00
|
|
|
use crypto::{DobyCipher, EncryptionParams};
|
2021-07-04 16:24:44 +02:00
|
|
|
use zeroize::Zeroize;
|
2021-06-25 23:01:50 +02:00
|
|
|
|
|
|
|
pub const MAGIC_BYTES: &[u8; 4] = b"DOBY";
|
|
|
|
|
2021-07-04 16:24:44 +02:00
|
|
|
pub struct Password(Option<String>);
|
|
|
|
|
|
|
|
impl Password {
|
|
|
|
fn unwrap_or_ask(self) -> String {
|
|
|
|
self.0.unwrap_or_else(|| rpassword::read_password_from_tty(Some("Password: ")).unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Option<&str>> for Password {
|
|
|
|
fn from(s: Option<&str>) -> Self {
|
2021-08-31 11:23:15 +02:00
|
|
|
Self(s.map(String::from))
|
2021-07-04 16:24:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for Password {
|
|
|
|
fn from(s: &str) -> Self {
|
|
|
|
Some(s).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Zeroize for Password {
|
|
|
|
fn zeroize(&mut self) {
|
|
|
|
self.0.zeroize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 19:24:20 +02:00
|
|
|
pub struct LazyWriter<P: AsRef<Path>> {
|
|
|
|
path: Option<P>,
|
|
|
|
writer: Option<Box<dyn Write>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: AsRef<Path>> LazyWriter<P> {
|
|
|
|
fn from_path(path: P) -> Self {
|
|
|
|
Self {
|
|
|
|
path: Some(path),
|
|
|
|
writer: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn from_writer<T: 'static + Write>(writer: T) -> Self {
|
|
|
|
Self {
|
|
|
|
path: None,
|
|
|
|
writer: Some(Box::new(writer)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: AsRef<Path>> Write for LazyWriter<P> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
if self.writer.is_none() {
|
|
|
|
self.writer = Some(Box::new(File::create(self.path.as_ref().unwrap()).unwrap()));
|
|
|
|
}
|
|
|
|
self.writer.as_mut().unwrap().write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.writer.as_mut().unwrap().flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:23:15 +02:00
|
|
|
pub fn encrypt<R: Read, W: Write>(reader: &mut R, writer: &mut W, params: &EncryptionParams, mut cipher: DobyCipher, block_size: usize, already_read: Option<&[u8]>) -> io::Result<()> {
|
2021-06-25 23:01:50 +02:00
|
|
|
writer.write_all(MAGIC_BYTES)?;
|
|
|
|
params.write(writer)?;
|
|
|
|
let mut buff = vec![0; block_size];
|
|
|
|
let mut n = 1;
|
|
|
|
if let Some(already_read) = already_read {
|
|
|
|
buff[..already_read.len()].clone_from_slice(&already_read);
|
|
|
|
n = reader.read(&mut buff[already_read.len()..])?;
|
|
|
|
cipher.encrypt_chunk(&mut buff[..n+already_read.len()], writer)?;
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
loop {
|
|
|
|
n = reader.read(&mut buff)?;
|
|
|
|
if n == 0 {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
cipher.encrypt_chunk(&mut buff[..n], writer)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 12:21:14 +02:00
|
|
|
cipher.write_hmac(writer)?;
|
2021-06-25 23:01:50 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-27 20:35:23 +02:00
|
|
|
pub fn decrypt<R: Read, W: Write>(reader: &mut R, writer: &mut W, mut cipher: DobyCipher, block_size: usize) -> io::Result<bool> {
|
2021-06-25 23:01:50 +02:00
|
|
|
let mut buff = vec![0; block_size];
|
|
|
|
loop {
|
|
|
|
let n = cipher.decrypt_chunk(reader, &mut buff)?;
|
|
|
|
if n == 0 {
|
|
|
|
break;
|
|
|
|
} else {
|
2021-08-31 11:23:15 +02:00
|
|
|
writer.write_all(&buff[..n])?;
|
2021-06-25 23:01:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(cipher.verify_hmac())
|
|
|
|
}
|