Skip to content

Commit

Permalink
Fix compilation without default features
Browse files Browse the repository at this point in the history
This patch fixes the compilation of all targets if the default features
are disabled and adds a CI job that checks this.

Fixes: trussed-dev#90
  • Loading branch information
robin-nitrokey committed Apr 25, 2023
1 parent 55ea391 commit e35e5ce
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ jobs:
- name: Build
run: cargo build --verbose --target ${{ matrix.target }}

- name: Check all targets without default features
run: cargo check --all-targets --no-default-features
if: matrix.target == 'x86_64-unknown-linux-gnu'

- name: Check all targets with default features
run: cargo check --all-targets
if: matrix.target == 'x86_64-unknown-linux-gnu'
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ cfg_if::cfg_if! {
pub const MAX_SERVICE_CLIENTS: usize = 2;
} else if #[cfg(feature = "clients-1")] {
pub const MAX_SERVICE_CLIENTS: usize = 1;
} else {
pub const MAX_SERVICE_CLIENTS: usize = 0;
}
}
pub const MAX_SHORT_DATA_LENGTH: usize = 128;
Expand Down
4 changes: 2 additions & 2 deletions src/mechanisms/aes256cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ impl UnsafeInjectKey for super::Aes256Cbc {

#[cfg(not(feature = "aes256-cbc"))]
impl UnsafeInjectKey for super::Aes256Cbc {}

#[cfg(not(feature = "aes256-cbc"))]
impl Decrypt for super::Aes256Cbc {}

#[cfg(not(feature = "aes256-cbc"))]
impl Encrypt for super::Aes256Cbc {}
#[cfg(not(feature = "aes256-cbc"))]
impl WrapKey for super::Aes256Cbc {}
10 changes: 7 additions & 3 deletions src/mechanisms/chacha8poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,12 @@ impl UnwrapKey for super::Chacha8Poly1305 {
}

#[cfg(not(feature = "chacha8-poly1305"))]
impl<P: Platform> Decrypt<P> for super::Chacha8Poly1305 {}
impl Decrypt for super::Chacha8Poly1305 {}
#[cfg(not(feature = "chacha8-poly1305"))]
impl<P: Platform> Encrypt<P> for super::Chacha8Poly1305 {}
impl Encrypt for super::Chacha8Poly1305 {}
#[cfg(not(feature = "chacha8-poly1305"))]
impl<P: Platform> GenerateKey<P> for super::Chacha8Poly1305 {}
impl WrapKey for super::Chacha8Poly1305 {}
#[cfg(not(feature = "chacha8-poly1305"))]
impl UnwrapKey for super::Chacha8Poly1305 {}
#[cfg(not(feature = "chacha8-poly1305"))]
impl GenerateKey for super::Chacha8Poly1305 {}
8 changes: 6 additions & 2 deletions src/mechanisms/ed255.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,17 @@ impl UnsafeInjectKey for super::Ed255 {
}
}

#[cfg(not(feature = "ed255"))]
impl Exists for super::Ed255 {}
#[cfg(not(feature = "ed255"))]
impl DeriveKey for super::Ed255 {}
#[cfg(not(feature = "ed255"))]
impl GenerateKey for super::Ed255 {}
#[cfg(not(feature = "ed255"))]
impl SerializeKey for super::Ed255 {}
#[cfg(not(feature = "ed255"))]
impl DeserializeKey for super::Ed255 {}
#[cfg(not(feature = "ed255"))]
impl Sign for super::Ed255 {}
#[cfg(not(feature = "ed255"))]
impl Verify for super::Ed255 {}
#[cfg(not(feature = "ed255"))]
impl UnsafeInjectKey for super::ed255 {}
10 changes: 8 additions & 2 deletions src/mechanisms/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,18 @@ impl UnsafeInjectKey for super::P256 {
#[cfg(not(feature = "p256"))]
impl Agree for super::P256 {}
#[cfg(not(feature = "p256"))]
impl Exists for super::P256 {}
#[cfg(not(feature = "p256"))]
impl DeriveKey for super::P256 {}
#[cfg(not(feature = "p256"))]
impl GenerateKey for super::P256 {}
#[cfg(not(feature = "p256"))]
impl DeserializeKey for super::P256 {}
#[cfg(not(feature = "p256"))]
impl SerializeKey for super::P256 {}
#[cfg(not(feature = "p256"))]
impl Sign for super::P256 {}
#[cfg(not(feature = "p256"))]
impl Verify for super::P256 {}
impl Sign for super::P256Prehashed {}
#[cfg(not(feature = "p256"))]
impl UnsafeInjectKey for super::P256 {}
impl Verify for super::P256 {}
7 changes: 7 additions & 0 deletions src/mechanisms/tdes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// use cortex_m_semihosting::{dbg, hprintln};

// needed to even get ::new() from des...
#[cfg(feature = "tdes")]
use des::cipher::{BlockDecrypt, BlockEncrypt, KeyInit};

use crate::api::*;
Expand Down Expand Up @@ -109,3 +110,9 @@ impl UnsafeInjectKey for super::Tdes {
Ok(reply::UnsafeInjectKey { key: key_id })
}
}

#[cfg(not(feature = "tdes"))]
impl Encrypt for super::Tdes {}

#[cfg(not(feature = "tdes"))]
impl Decrypt for super::Tdes {}
9 changes: 8 additions & 1 deletion src/mechanisms/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const TOTP_KEY_SIZE: usize = 20;

// https://tools.ietf.org/html/rfc4226#section-5.3

#[cfg(feature = "totp")]
#[inline(never)]
fn hotp_raw(key: &[u8], counter: u64, digits: u32) -> u64 {
hmac_and_truncate(key, &counter.to_be_bytes(), digits)
}

#[cfg(feature = "totp")]
#[inline(never)]
fn hmac_and_truncate(key: &[u8], message: &[u8], digits: u32) -> u64 {
use hmac::{Hmac, Mac};
Expand Down Expand Up @@ -91,7 +93,12 @@ impl Exists for super::Totp {
}
}

#[cfg(test)]
#[cfg(not(feature = "totp"))]
impl Sign for super::Totp {}
#[cfg(not(feature = "totp"))]
impl Exists for super::Totp {}

#[cfg(all(test, feature = "totp"))]
mod tests {
use super::*;

Expand Down
4 changes: 1 addition & 3 deletions src/mechanisms/x255.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ impl GenerateKey for super::X255 {}
#[cfg(not(feature = "x255"))]
impl Exists for super::X255 {}
#[cfg(not(feature = "x255"))]
impl Derive for super::X255 {}
impl DeriveKey for super::X255 {}
#[cfg(not(feature = "x255"))]
impl SerializeKey for super::X255 {}
#[cfg(not(feature = "x255"))]
impl DeserializeKey for super::X255 {}
#[cfg(not(feature = "x255"))]
impl UnsafeInjectKey for super::X255 {}
7 changes: 7 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ fn dummy() {
setup!(_client);
}

#[cfg(feature = "ed255")]
#[test]
#[serial]
fn sign_ed255() {
Expand Down Expand Up @@ -255,6 +256,7 @@ fn sign_ed255() {
assert_eq!(Err(Error::WrongSignatureLength), reply);
}

#[cfg(feature = "p255")]
#[test]
#[serial]
fn sign_p256() {
Expand Down Expand Up @@ -294,6 +296,7 @@ fn sign_p256() {
assert!(valid);
}

#[cfg(feature = "p255")]
#[test]
#[serial]
fn agree_p256() {
Expand Down Expand Up @@ -376,6 +379,7 @@ fn agree_p256() {
.signature;
}

#[cfg(feature = "chacha8-poly1305")]
#[test]
#[serial]
fn aead_rng_nonce() {
Expand Down Expand Up @@ -412,6 +416,7 @@ fn aead_rng_nonce() {
assert_eq!(&message[..], plaintext.unwrap().as_ref());
}

#[cfg(feature = "chacha8-poly1305")]
#[test]
#[serial]
fn aead_given_nonce() {
Expand Down Expand Up @@ -450,6 +455,7 @@ fn aead_given_nonce() {
}

// Same as before but key generated with a nonce
#[cfg(feature = "chacha8-poly1305")]
#[test]
#[serial]
fn aead_given_nonce_2() {
Expand Down Expand Up @@ -487,6 +493,7 @@ fn aead_given_nonce_2() {
assert_eq!(&message[..], plaintext.unwrap().as_ref());
}

#[cfg(feature = "chacha8-poly1305")]
#[test]
#[serial]
fn aead() {
Expand Down

0 comments on commit e35e5ce

Please sign in to comment.