1#![deny(warnings, missing_docs)]
2use aes::cipher::generic_array::GenericArray;
12use aes::{Aes128, BlockCipher};
13pub use decrypt::RoaesSource;
14pub use encrypt::RoaesSink;
15use snafu::{Backtrace, GenerateBacktrace, Snafu};
16use std::io::ErrorKind as IOErrorKind;
17
18mod decrypt;
19mod encrypt;
20mod util;
21
22const OAES_BLOCK_SIZE: usize = 16;
23const OAES_BLOCK_SIZE64: u64 = OAES_BLOCK_SIZE as u64;
24const FILE_BLOCK_SIZE: usize = 4096;
25const FILE_BLOCK_SIZE64: u64 = FILE_BLOCK_SIZE as u64;
26
27type AesBlock = GenericArray<u8, <Aes128 as BlockCipher>::BlockSize>;
28
29#[allow(missing_docs)]
31#[derive(Debug, Snafu)]
32pub enum RoaesError {
33 NoMagicNumber,
35 Eof,
37 Incompatible { desc: String, backtrace: Backtrace },
39 Crypto {
41 source: Box<dyn std::error::Error + Send + Sync + 'static>,
42 backtrace: Backtrace,
43 desc: String,
44 },
45 General { backtrace: Backtrace, desc: String },
47 IO {
49 source: std::io::Error,
50 backtrace: Backtrace,
51 desc: String,
52 },
53}
54
55impl RoaesError {
56 fn underflow_multiple_aes_block(read: usize) -> Self {
57 RoaesError::IO {
58 source: std::io::Error::new(
59 IOErrorKind::UnexpectedEof,
60 "did not read multiple of AES block size",
61 ),
62 backtrace: Backtrace::generate(),
63 desc: format!(
64 "did not read multiple of AES block size, read: {} byte",
65 read
66 ),
67 }
68 }
69
70 fn general<S: Into<String>>(desc: S) -> Self {
71 RoaesError::General {
72 backtrace: Backtrace::generate(),
73 desc: desc.into(),
74 }
75 }
76
77 fn no_magic_number() -> Self {
78 RoaesError::NoMagicNumber {}
79 }
80
81 fn incompatibility<S: Into<String>>(desc: S) -> Self {
82 RoaesError::Incompatible {
83 backtrace: Backtrace::generate(),
84 desc: desc.into(),
85 }
86 }
87
88 fn crypto<E: std::error::Error + Send + Sync + 'static, S: Into<String>>(
89 err: E,
90 desc: S,
91 ) -> Self {
92 RoaesError::Crypto {
93 source: Box::new(err),
94 backtrace: Backtrace::generate(),
95 desc: desc.into(),
96 }
97 }
98
99 pub fn is_no_valid_magic_number(&self) -> bool {
101 matches!(self, RoaesError::NoMagicNumber)
102 }
103}