Skip to content

Commit

Permalink
chore: check for compilation warnings in CI (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
louib authored Aug 26, 2024
1 parent b33d1fb commit 7bfefca
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
doc:
name: Build Doc
runs-on: ubuntu-latest
env:
RUSTFLAGS: -D warnings
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -35,6 +37,8 @@ jobs:
check:
name: Check
runs-on: ubuntu-latest
env:
RUSTFLAGS: -D warnings
steps:
- uses: actions/checkout@v4
- uses: taiki-e/install-action@v2
Expand All @@ -48,6 +52,8 @@ jobs:
test:
name: Test Suite
runs-on: ubuntu-latest
env:
RUSTFLAGS: -D warnings
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ required-features = ["utilities", "save_kdbx4", "challenge_response"]
[[bin]]
name = "kp-yk-recover"
required-features = ["utilities", "save_kdbx4", "challenge_response"]

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] }
9 changes: 8 additions & 1 deletion src/compression.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use flate2::read::GzDecoder;
#[cfg(feature = "save_kdbx4")]
use flate2::write::GzEncoder;
#[cfg(feature = "save_kdbx4")]
use flate2::Compression as Flate2Compression;
use std::io::{Read, Write};
use std::io::Read;
#[cfg(feature = "save_kdbx4")]
use std::io::Write;

pub trait Compression {
#[cfg(feature = "save_kdbx4")]
fn compress(&self, in_buffer: &[u8]) -> Result<Vec<u8>, std::io::Error>;
fn decompress(&self, in_buffer: &[u8]) -> Result<Vec<u8>, std::io::Error>;
}

pub struct NoCompression;

impl Compression for NoCompression {
#[cfg(feature = "save_kdbx4")]
fn compress(&self, in_buffer: &[u8]) -> Result<Vec<u8>, std::io::Error> {
Ok(in_buffer.to_vec())
}
Expand All @@ -22,6 +28,7 @@ impl Compression for NoCompression {
pub struct GZipCompression;

impl Compression for GZipCompression {
#[cfg(feature = "save_kdbx4")]
fn compress(&self, in_buffer: &[u8]) -> Result<Vec<u8>, std::io::Error> {
let mut res = Vec::new();
let mut encoder = GzEncoder::new(&mut res, Flate2Compression::default());
Expand Down
12 changes: 11 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use std::convert::TryFrom;

pub use crate::format::DatabaseVersion;

#[cfg(feature = "save_kdbx4")]
use crate::crypt::ciphers::Cipher;
use crate::{
compression,
crypt::{
ciphers::{self, Cipher},
ciphers::{self},
kdf,
},
error::{
Expand Down Expand Up @@ -89,6 +91,7 @@ impl OuterCipherConfig {
}
}

#[cfg(feature = "save_kdbx4")]
pub(crate) fn get_iv_size(&self) -> usize {
match self {
OuterCipherConfig::AES256 => ciphers::AES256Cipher::iv_size(),
Expand All @@ -97,6 +100,7 @@ impl OuterCipherConfig {
}
}

#[cfg(feature = "save_kdbx4")]
pub(crate) fn dump(&self) -> [u8; 16] {
match self {
OuterCipherConfig::AES256 => CIPHERSUITE_AES256,
Expand Down Expand Up @@ -139,6 +143,7 @@ impl InnerCipherConfig {
}
}

#[cfg(feature = "save_kdbx4")]
pub(crate) fn dump(&self) -> u32 {
match self {
InnerCipherConfig::Plain => PLAIN,
Expand All @@ -147,6 +152,7 @@ impl InnerCipherConfig {
}
}

#[cfg(feature = "save_kdbx4")]
pub(crate) fn get_key_size(&self) -> usize {
match self {
InnerCipherConfig::Plain => ciphers::PlainCipher::key_size(),
Expand Down Expand Up @@ -216,6 +222,7 @@ fn serialize_argon2_version<S: serde::Serializer>(
}

impl KdfConfig {
#[cfg(feature = "save_kdbx4")]
fn seed_size(&self) -> usize {
match self {
KdfConfig::Aes { .. } => 32,
Expand All @@ -226,6 +233,7 @@ impl KdfConfig {

/// For writing out a database, generate a new KDF seed from the config and return the KDF
/// and the generated seed
#[cfg(feature = "save_kdbx4")]
pub(crate) fn get_kdf_and_seed(&self) -> Result<(Box<dyn kdf::Kdf>, Vec<u8>), getrandom::Error> {
let mut kdf_seed = vec![0; self.seed_size()];
getrandom::getrandom(&mut kdf_seed)?;
Expand Down Expand Up @@ -271,6 +279,7 @@ impl KdfConfig {
}
}

#[cfg(feature = "save_kdbx4")]
pub(crate) fn to_variant_dictionary(&self, seed: &[u8]) -> VariantDictionary {
let mut vd = VariantDictionary::new();

Expand Down Expand Up @@ -394,6 +403,7 @@ impl CompressionConfig {
}
}

#[cfg(feature = "save_kdbx4")]
pub(crate) fn dump(&self) -> [u8; 4] {
match self {
CompressionConfig::None => [0, 0, 0, 0],
Expand Down
24 changes: 23 additions & 1 deletion src/crypt/ciphers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use aes::Aes256;
use cipher::{block_padding::Pkcs7, generic_array::GenericArray, BlockDecryptMut, BlockEncryptMut};
#[cfg(feature = "save_kdbx4")]
use cipher::BlockEncryptMut;
use cipher::{block_padding::Pkcs7, generic_array::GenericArray, BlockDecryptMut};
use salsa20::{
cipher::{KeyIvInit, StreamCipher},
Salsa20,
Expand All @@ -8,20 +10,24 @@ use salsa20::{
use crate::crypt::CryptographyError;

pub(crate) trait Cipher {
#[cfg(feature = "save_kdbx4")]
fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, CryptographyError>;
fn decrypt(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>, CryptographyError>;

#[cfg(feature = "save_kdbx4")]
/// The number of bytes expected by the cipher as an initialization vector.
fn iv_size() -> usize
where
Self: Sized;

#[cfg(feature = "save_kdbx4")]
/// The number of bytes expected by the cipher as a key.
fn key_size() -> usize
where
Self: Sized;
}

#[cfg(feature = "save_kdbx4")]
type Aes256CbcEncryptor = cbc::Encryptor<Aes256>;
type Aes256CbcDecryptor = cbc::Decryptor<Aes256>;
pub(crate) struct AES256Cipher {
Expand All @@ -39,6 +45,7 @@ impl AES256Cipher {
}

impl Cipher for AES256Cipher {
#[cfg(feature = "save_kdbx4")]
fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, CryptographyError> {
let cipher = Aes256CbcEncryptor::new_from_slices(&self.key, &self.iv)?;

Expand All @@ -60,15 +67,18 @@ impl Cipher for AES256Cipher {
Ok(out)
}

#[cfg(feature = "save_kdbx4")]
fn iv_size() -> usize {
16
}

#[cfg(feature = "save_kdbx4")]
fn key_size() -> usize {
32
}
}

#[cfg(feature = "save_kdbx4")]
type TwofishCbcEncryptor = cbc::Encryptor<twofish::Twofish>;
type TwofishCbcDecryptor = cbc::Decryptor<twofish::Twofish>;
pub(crate) struct TwofishCipher {
Expand All @@ -86,6 +96,7 @@ impl TwofishCipher {
}

impl Cipher for TwofishCipher {
#[cfg(feature = "save_kdbx4")]
fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, CryptographyError> {
let cipher = TwofishCbcEncryptor::new_from_slices(&self.key, &self.iv)?;

Expand All @@ -102,10 +113,12 @@ impl Cipher for TwofishCipher {
Ok(buf)
}

#[cfg(feature = "save_kdbx4")]
fn iv_size() -> usize {
16
}

#[cfg(feature = "save_kdbx4")]
fn key_size() -> usize {
32
}
Expand All @@ -127,6 +140,7 @@ impl Salsa20Cipher {
}

impl Cipher for Salsa20Cipher {
#[cfg(feature = "save_kdbx4")]
fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, CryptographyError> {
let mut buffer = Vec::from(plaintext);
self.cipher.apply_keystream(&mut buffer);
Expand All @@ -138,11 +152,13 @@ impl Cipher for Salsa20Cipher {
Ok(buffer)
}

#[cfg(feature = "save_kdbx4")]
fn iv_size() -> usize {
// or 16
32
}

#[cfg(feature = "save_kdbx4")]
fn key_size() -> usize {
32
}
Expand Down Expand Up @@ -174,6 +190,7 @@ impl ChaCha20Cipher {
}

impl Cipher for ChaCha20Cipher {
#[cfg(feature = "save_kdbx4")]
fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, CryptographyError> {
let mut buffer = Vec::from(plaintext);
self.cipher.apply_keystream(&mut buffer);
Expand All @@ -185,10 +202,12 @@ impl Cipher for ChaCha20Cipher {
Ok(buffer)
}

#[cfg(feature = "save_kdbx4")]
fn iv_size() -> usize {
12
}

#[cfg(feature = "save_kdbx4")]
fn key_size() -> usize {
32
}
Expand All @@ -201,17 +220,20 @@ impl PlainCipher {
}
}
impl Cipher for PlainCipher {
#[cfg(feature = "save_kdbx4")]
fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, CryptographyError> {
Ok(Vec::from(plaintext))
}
fn decrypt(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>, CryptographyError> {
Ok(Vec::from(ciphertext))
}

#[cfg(feature = "save_kdbx4")]
fn iv_size() -> usize {
1
}

#[cfg(feature = "save_kdbx4")]
fn key_size() -> usize {
1
}
Expand Down
7 changes: 6 additions & 1 deletion src/format/kdbx4/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "save_kdbx4")]
mod dump;
mod parse;

Expand All @@ -6,9 +7,11 @@ use crate::{
format::DatabaseVersion,
};

#[cfg(feature = "save_kdbx4")]
pub(crate) use crate::format::kdbx4::dump::dump_kdbx4;
pub(crate) use crate::format::kdbx4::parse::{decrypt_kdbx4, parse_kdbx4};

#[cfg(feature = "save_kdbx4")]
/// Size for a master seed in bytes
pub const HEADER_MASTER_SEED_SIZE: usize = 32;

Expand Down Expand Up @@ -51,14 +54,16 @@ struct KDBX4InnerHeader {
inner_random_stream_key: Vec<u8>,
}

#[cfg(feature = "save_kdbx4")]
#[cfg(test)]
mod kdbx4_tests {
use super::*;

use crate::format::kdbx4::dump::dump_kdbx4;
use crate::{
config::{CompressionConfig, DatabaseConfig, InnerCipherConfig, KdfConfig, OuterCipherConfig},
db::{Database, Entry, Group, HeaderAttachment, NodeRef, Value},
format::{kdbx4::dump::dump_kdbx4, KDBX4_CURRENT_MINOR_VERSION},
format::KDBX4_CURRENT_MINOR_VERSION,
key::DatabaseKey,
};

Expand Down
6 changes: 5 additions & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ pub(crate) mod kdb;
pub(crate) mod kdbx3;
pub(crate) mod kdbx4;

#[cfg(feature = "save_kdbx4")]
use std::io::Write;

use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
#[cfg(feature = "save_kdbx4")]
use byteorder::WriteBytesExt;
use byteorder::{ByteOrder, LittleEndian};

use crate::error::DatabaseIntegrityError;

Expand Down Expand Up @@ -70,6 +73,7 @@ impl DatabaseVersion {
Ok(response)
}

#[cfg(feature = "save_kdbx4")]
fn dump(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> {
if let DatabaseVersion::KDB4(minor_version) = self {
writer.write(&crate::format::KDBX_IDENTIFIER)?;
Expand Down
1 change: 1 addition & 0 deletions src/hmac_block_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub(crate) fn read_hmac_block_stream(
Ok(out)
}

#[cfg(feature = "save_kdbx4")]
/// Write a raw buffer as a HMAC block stream
pub(crate) fn write_hmac_block_stream(
data: &[u8],
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod db;
pub mod error;
pub(crate) mod format;
pub(crate) mod hmac_block_stream;
#[cfg(feature = "save_kdbx4")]
mod io;
mod key;
pub(crate) mod variant_dictionary;
Expand Down
Loading

0 comments on commit 7bfefca

Please sign in to comment.