2019-08-16 07:19:01 +00:00
|
|
|
use std::sync::Arc;
|
2019-08-14 11:05:11 +00:00
|
|
|
use std::io::{Read, BufRead};
|
|
|
|
|
2021-07-06 10:49:10 +00:00
|
|
|
use anyhow::{bail, Error};
|
|
|
|
|
2019-08-14 11:05:11 +00:00
|
|
|
use super::CryptConfig;
|
|
|
|
|
|
|
|
pub struct CryptReader<R> {
|
|
|
|
reader: R,
|
|
|
|
small_read_buf: Vec<u8>,
|
|
|
|
block_size: usize,
|
|
|
|
crypter: openssl::symm::Crypter,
|
|
|
|
finalized: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <R: BufRead> CryptReader<R> {
|
|
|
|
|
2019-08-16 07:19:01 +00:00
|
|
|
pub fn new(reader: R, iv: [u8; 16], tag: [u8; 16], config: Arc<CryptConfig>) -> Result<Self, Error> {
|
2019-08-14 11:05:11 +00:00
|
|
|
let block_size = config.cipher().block_size(); // Note: block size is normally 1 byte for stream ciphers
|
|
|
|
if block_size.count_ones() != 1 || block_size > 512 {
|
|
|
|
bail!("unexpected Cipher block size {}", block_size);
|
|
|
|
}
|
|
|
|
let mut crypter = config.data_crypter(&iv, openssl::symm::Mode::Decrypt)?;
|
|
|
|
crypter.set_tag(&tag)?;
|
|
|
|
|
|
|
|
Ok(Self { reader, crypter, block_size, finalized: false, small_read_buf: Vec::new() })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish(self) -> Result<R, Error> {
|
|
|
|
if !self.finalized {
|
|
|
|
bail!("CryptReader not successfully finalized.");
|
|
|
|
}
|
|
|
|
Ok(self.reader)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <R: BufRead> Read for CryptReader<R> {
|
|
|
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
|
2021-01-19 09:27:59 +00:00
|
|
|
if !self.small_read_buf.is_empty() {
|
2019-08-14 11:05:11 +00:00
|
|
|
let max = if self.small_read_buf.len() > buf.len() { buf.len() } else { self.small_read_buf.len() };
|
|
|
|
let rest = self.small_read_buf.split_off(max);
|
|
|
|
buf[..max].copy_from_slice(&self.small_read_buf);
|
|
|
|
self.small_read_buf = rest;
|
|
|
|
return Ok(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = self.reader.fill_buf()?;
|
|
|
|
|
|
|
|
// handle small read buffers
|
|
|
|
if buf.len() <= 2*self.block_size {
|
|
|
|
let mut outbuf = [0u8; 1024];
|
|
|
|
|
2021-01-19 09:27:59 +00:00
|
|
|
let count = if data.is_empty() { // EOF
|
2019-08-14 11:05:11 +00:00
|
|
|
let written = self.crypter.finalize(&mut outbuf)?;
|
|
|
|
self.finalized = true;
|
|
|
|
written
|
|
|
|
} else {
|
|
|
|
let mut read_size = outbuf.len() - self.block_size;
|
|
|
|
if read_size > data.len() {
|
|
|
|
read_size = data.len();
|
|
|
|
}
|
|
|
|
let written = self.crypter.update(&data[..read_size], &mut outbuf)?;
|
|
|
|
self.reader.consume(read_size);
|
|
|
|
written
|
|
|
|
};
|
|
|
|
|
|
|
|
if count > buf.len() {
|
|
|
|
buf.copy_from_slice(&outbuf[..buf.len()]);
|
|
|
|
self.small_read_buf = outbuf[buf.len()..count].to_vec();
|
2019-10-25 16:44:51 +00:00
|
|
|
Ok(buf.len())
|
2019-08-14 11:05:11 +00:00
|
|
|
} else {
|
|
|
|
buf[..count].copy_from_slice(&outbuf[..count]);
|
2019-10-25 16:44:51 +00:00
|
|
|
Ok(count)
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
2021-01-19 09:27:59 +00:00
|
|
|
} else if data.is_empty() { // EOF
|
2019-10-25 16:44:51 +00:00
|
|
|
let rest = self.crypter.finalize(buf)?;
|
|
|
|
self.finalized = true;
|
|
|
|
Ok(rest)
|
2019-08-14 11:05:11 +00:00
|
|
|
} else {
|
2019-10-25 16:44:51 +00:00
|
|
|
let mut read_size = buf.len() - self.block_size;
|
|
|
|
if read_size > data.len() {
|
|
|
|
read_size = data.len();
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
2019-10-25 16:44:51 +00:00
|
|
|
let count = self.crypter.update(&data[..read_size], buf)?;
|
|
|
|
self.reader.consume(read_size);
|
|
|
|
Ok(count)
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|