Skip to content

Commit

Permalink
Reduce wildcard imports
Browse files Browse the repository at this point in the history
This patch removes some wildcard imports to make the dependencies
between modules clearer and to refactoring easier.
  • Loading branch information
robin-nitrokey committed Apr 3, 2024
1 parent 20b0acb commit 83cf940
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 84 deletions.
6 changes: 5 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
//! [pkcs11-v3]: https://docs.oasis-open.org/pkcs11/pkcs11-base/v3.0/pkcs11-base-v3.0.html
//! [pkcs11-headers]: https://docs.oasis-open.org/pkcs11/pkcs11-base/v3.0/cs01/include/pkcs11-v3.0/
use crate::types::*;
use crate::types::{
consent, reboot, Bytes, CertId, CounterId, DirEntry, KeyId, KeySerialization, Location,
Mechanism, MediumData, Message, PathBuf, SerializedKey, ShortData, Signature,
SignatureSerialization, StorageAttributes, UserAttribute,
};
use core::time::Duration;

#[macro_use]
Expand Down
10 changes: 7 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@
//!
use core::{marker::PhantomData, task::Poll};

use crate::api::*;
use crate::api::{reply, request, NotBefore, Reply, ReplyVariant, RequestVariant};
use crate::backend::{BackendId, CoreOnly, Dispatch};
use crate::error::*;
use crate::error::{Error, Result};
use crate::interrupt::InterruptFlag;
use crate::pipe::{TrussedRequester, TRUSSED_INTERCHANGE};
use crate::service::Service;
use crate::types::*;
use crate::types::{
consent, reboot, Bytes, CertId, CounterId, KeyId, KeySerialization, Location, Mechanism,
MediumData, Message, PathBuf, Platform, SerializedKey, ShortData, Signature,
SignatureSerialization, StorageAttributes, UserAttribute,
};

pub use crate::platform::Syscall;

Expand Down
8 changes: 7 additions & 1 deletion src/client/mechanisms.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use super::*;
use super::{ClientError, ClientImplementation, ClientResult, CryptoClient};
use crate::api::reply;
use crate::platform::Syscall;
use crate::types::{
KeyId, KeySerialization, Location, Mechanism, MediumData, Message, ShortData,
SignatureSerialization, StorageAttributes,
};

#[cfg(feature = "aes256-cbc")]
impl<S: Syscall, E> Aes256Cbc for ClientImplementation<S, E> {}
Expand Down
9 changes: 5 additions & 4 deletions src/mechanisms/aes256cbc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::api::*;
// use crate::config::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{Decrypt, Encrypt, UnsafeInjectKey, WrapKey};
use crate::store::keystore::Keystore;
use crate::types::{Mechanism, Message, ShortData};

const AES256_KEY_SIZE: usize = 32;

Expand Down
11 changes: 7 additions & 4 deletions src/mechanisms/chacha8poly1305.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::api::*;
// use crate::config::*;
use generic_array::GenericArray;
use rand_core::RngCore;

use crate::api::{reply, request};
use crate::error::Error;
use crate::key;
use crate::service::*;
use crate::types::*;
use crate::service::{Decrypt, Encrypt, GenerateKey, UnwrapKey, WrapKey};
use crate::store::keystore::Keystore;
use crate::types::{Mechanism, Message, ShortData};

// TODO: The non-detached versions seem better.
// This needs a bit of additional type gymnastics.
Expand Down
16 changes: 10 additions & 6 deletions src/mechanisms/ed255.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use core::convert::{TryFrom, TryInto};
use rand_core::RngCore;

use crate::api::*;
// use crate::config::*;
// use crate::debug;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{
DeriveKey, DeserializeKey, Exists, GenerateKey, SerializeKey, Sign, UnsafeInjectKey, Verify,
};
use crate::store::keystore::Keystore;
use crate::types::{
Bytes, KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization,
};

#[inline(never)]
fn load_public_key(
Expand Down
8 changes: 5 additions & 3 deletions src/mechanisms/hmacblake2s.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{DeriveKey, Sign};
use crate::store::keystore::Keystore;
use crate::types::Signature;

#[cfg(feature = "hmac-blake2s")]
impl DeriveKey for super::HmacBlake2s {
Expand Down
8 changes: 5 additions & 3 deletions src/mechanisms/hmacsha1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{DeriveKey, Sign};
use crate::store::keystore::Keystore;
use crate::types::Signature;

#[cfg(feature = "hmac-sha1")]
impl DeriveKey for super::HmacSha1 {
Expand Down
8 changes: 5 additions & 3 deletions src/mechanisms/hmacsha256.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{DeriveKey, Sign};
use crate::store::keystore::Keystore;
use crate::types::Signature;

#[cfg(feature = "hmac-sha256")]
impl DeriveKey for super::HmacSha256 {
Expand Down
8 changes: 5 additions & 3 deletions src/mechanisms/hmacsha512.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{DeriveKey, Sign};
use crate::store::keystore::Keystore;
use crate::types::Signature;

#[cfg(feature = "hmac-sha512")]
impl DeriveKey for super::HmacSha512 {
Expand Down
15 changes: 10 additions & 5 deletions src/mechanisms/p256.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// use core::convert::{TryFrom, TryInto};

use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{
Agree, DeriveKey, DeserializeKey, Exists, GenerateKey, SerializeKey, Sign, UnsafeInjectKey,
Verify,
};
use crate::store::keystore::Keystore;
use crate::types::{
Bytes, KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization,
};

#[inline(never)]
fn load_secret_key(
Expand Down
8 changes: 5 additions & 3 deletions src/mechanisms/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{DeriveKey, Hash};
use crate::store::keystore::Keystore;
use crate::types::ShortData;

#[cfg(feature = "sha256")]
impl DeriveKey for super::Sha256 {
Expand Down
7 changes: 4 additions & 3 deletions src/mechanisms/shared_secret.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::key;
use crate::service::*;
use crate::types::*;
use crate::service::{SerializeKey, UnsafeInjectKey};
use crate::store::keystore::Keystore;
use crate::types::{KeySerialization, SerializedKey};

impl SerializeKey for super::SharedSecret {
#[inline(never)]
Expand Down
8 changes: 5 additions & 3 deletions src/mechanisms/tdes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
// needed to even get ::new() from des...
#[cfg(feature = "tdes")]
use des::cipher::{BlockDecrypt, BlockEncrypt, KeyInit};
use generic_array::GenericArray;

use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;
use crate::key;
use crate::service::{Decrypt, Encrypt, UnsafeInjectKey};
use crate::store::keystore::Keystore;

const TDES_KEY_SIZE: usize = 24;

Expand Down
6 changes: 4 additions & 2 deletions src/mechanisms/totp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::api::*;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::key;
use crate::service::{Exists, Sign};
use crate::store::keystore::Keystore;

// code copied from https://github.com/avacariu/rust-oath

Expand Down
8 changes: 6 additions & 2 deletions src/mechanisms/trng.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::api::*;
use rand_core::RngCore;

use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::key;
use crate::service::GenerateKey;
use crate::store::keystore::Keystore;

#[cfg(feature = "trng")]
impl GenerateKey for super::Trng {
Expand Down
17 changes: 9 additions & 8 deletions src/mechanisms/x255.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use core::convert::TryInto;
use rand_core::RngCore;
use salty::agreement;

use crate::api::*;
// use crate::config::*;
// use crate::debug;
use crate::api::{reply, request};
use crate::error::Error;
use crate::service::*;
use crate::types::*;

use salty::agreement;
use crate::key;
use crate::service::{
Agree, DeriveKey, DeserializeKey, Exists, GenerateKey, SerializeKey, UnsafeInjectKey,
};
use crate::store::keystore::Keystore;
use crate::types::{KeyId, KeySerialization, SerializedKey};

fn load_public_key(
keystore: &mut impl Keystore,
Expand Down
13 changes: 8 additions & 5 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use littlefs2::{
object_safe::DynFilesystem,
path,
path::{Path, PathBuf},
};
Expand All @@ -7,25 +8,27 @@ pub use rand_core::{RngCore, SeedableRng};

use crate::backend::{BackendId, CoreOnly, Dispatch};
use crate::client::{ClientBuilder, ClientImplementation};
use crate::config::*;
use crate::config::{MAX_MESSAGE_LENGTH, MAX_SERVICE_CLIENTS};
use crate::error::{Error, Result};
pub use crate::key;
use crate::mechanisms;
pub use crate::pipe::ServiceEndpoint;
use crate::pipe::TrussedResponder;
use crate::platform::*;
use crate::platform::{consent, ui, Platform, Store, Syscall, UserInterface};
pub use crate::store::{
self,
certstore::{Certstore as _, ClientCertstore},
counterstore::{ClientCounterstore, Counterstore as _},
filestore::{ClientFilestore, Filestore, ReadDirFilesState, ReadDirState},
keystore::{ClientKeystore, Keystore},
DynFilesystem,
};
use crate::types::ui::Status;
use crate::types::*;
use crate::types::{Context, CoreContext, Location, Mechanism, MediumData, Message, Vec};
use crate::Bytes;
use crate::{api::*, interrupt::InterruptFlag};
use crate::{
api::{reply, request, Reply, Request},
interrupt::InterruptFlag,
};

pub mod attest;

Expand Down
16 changes: 9 additions & 7 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@
//! - Alternative: subdirectory <==> RP hash, everything else in flat files
//! - In any case need to "list dirs excluding . and .." or similar
use littlefs2::{driver::Storage, fs::Filesystem};

use crate::error::Error;
use crate::types::*;
use crate::types::{Bytes, Location, PathBuf};
#[allow(unused_imports)]
#[cfg(feature = "semihosting")]
use cortex_m_semihosting::hprintln;
Expand Down Expand Up @@ -127,9 +129,9 @@ pub mod keystore;
//
// This makes everything using it *much* more ergonomic.
pub unsafe trait Store: Copy {
type I: 'static + LfsStorage;
type E: 'static + LfsStorage;
type V: 'static + LfsStorage;
type I: 'static + Storage;
type E: 'static + Storage;
type V: 'static + Storage;
fn ifs(self) -> &'static Fs<Self::I>;
fn efs(self) -> &'static Fs<Self::E>;
fn vfs(self) -> &'static Fs<Self::V>;
Expand All @@ -142,18 +144,18 @@ pub unsafe trait Store: Copy {
}
}

pub struct Fs<S: 'static + LfsStorage> {
pub struct Fs<S: 'static + Storage> {
fs: &'static Filesystem<'static, S>,
}

impl<S: 'static + LfsStorage> core::ops::Deref for Fs<S> {
impl<S: 'static + Storage> core::ops::Deref for Fs<S> {
type Target = Filesystem<'static, S>;
fn deref(&self) -> &Self::Target {
self.fs
}
}

impl<S: 'static + LfsStorage> Fs<S> {
impl<S: 'static + Storage> Fs<S> {
pub fn new(fs: &'static Filesystem<'static, S>) -> Self {
Self { fs }
}
Expand Down
13 changes: 8 additions & 5 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
#![allow(static_mut_refs)]

use chacha20::ChaCha20;

use crate::types::*;
use crate::*;
use entropy::shannon_entropy;
use generic_array::GenericArray;
use littlefs2::const_ram_storage;
use littlefs2::driver::Storage as LfsStorage;
use littlefs2::fs::{Allocation, Filesystem};
use littlefs2::io::Result as LfsResult;
use rand_core::{CryptoRng, RngCore};

use crate::client::{CryptoClient as _, FilesystemClient as _};
use crate::types::{consent, reboot, ui, Bytes, Location, PathBuf};
use crate::{api, block, platform, store, Error};

pub struct MockRng(ChaCha20);

Expand All @@ -23,9 +26,9 @@ impl MockRng {
}
}

impl rand_core::CryptoRng for MockRng {}
impl CryptoRng for MockRng {}

impl crate::service::RngCore for MockRng {
impl RngCore for MockRng {
fn fill_bytes(&mut self, buf: &mut [u8]) {
use chacha20::cipher::StreamCipher;
self.0.apply_keystream(buf);
Expand Down
Loading

0 comments on commit 83cf940

Please sign in to comment.