Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce re-exports #161

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
of being generic over the storage implementation.
- Add `nonce` argument to `wrap_key` and `unwrap_key` syscalls.
- Use nonce as IV for Aes256Cbc mechanism.
- Reduce re-exports ([#155][]):
- Remove most re-exports of external types
- Remove all re-exports of internal types

### Fixed

Expand All @@ -58,6 +61,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#64]: https://github.com/trussed-dev/trussed/issues/64
[#65]: https://github.com/trussed-dev/trussed/issues/65
[#99]: https://github.com/trussed-dev/trussed/issues/99
[#155]: https://github.com/trussed-dev/trussed/issues/155

## [0.1.0] - 2022-01-26

Expand Down
4 changes: 2 additions & 2 deletions derive/src/extension_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ impl ExtensionId {
}

impl #impl_generics ::core::convert::TryFrom<u8> for #name #ty_generics #where_clause {
type Error = ::trussed::Error;
type Error = ::trussed::error::Error;

fn try_from(value: u8) -> ::core::result::Result<Self, Self::Error> {
match value {
#(#try_from)*
_ => Err(::trussed::Error::InternalError),
_ => Err(::trussed::error::Error::InternalError),
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
//! [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::error::Error;
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 Expand Up @@ -139,11 +144,11 @@ generate_enums! {
SerdeExtension: 0x5E
}

pub trait RequestVariant: Into<Request> + TryFrom<Request, Error = crate::Error> {
pub trait RequestVariant: Into<Request> + TryFrom<Request, Error = Error> {
type Reply: ReplyVariant<Request = Self>;
}

pub trait ReplyVariant: Into<Reply> + TryFrom<Reply, Error = crate::Error> {
pub trait ReplyVariant: Into<Reply> + TryFrom<Reply, Error = Error> {
type Request: RequestVariant<Reply = Self>;
}

Expand Down Expand Up @@ -366,7 +371,7 @@ pub mod request {
Uptime:

Wink:
- duration: core::time::Duration
- duration: Duration

SetCustomStatus:
- status: u8
Expand Down
8 changes: 4 additions & 4 deletions src/api/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ macro_rules! impl_request {
}
}
impl core::convert::TryFrom<Request> for $request {
type Error = crate::Error;
type Error = crate::error::Error;
fn try_from(request: Request) -> Result<request::$request, Self::Error> {
match request {
Request::$request(request) => Ok(request),
_ => Err(crate::Error::InternalError),
_ => Err(crate::error::Error::InternalError),
}
}
}
Expand Down Expand Up @@ -118,11 +118,11 @@ macro_rules! impl_reply {

$(#[$attr])?
impl core::convert::TryFrom<Reply> for $reply {
type Error = crate::Error;
type Error = crate::error::Error;
fn try_from(reply: Reply) -> Result<reply::$reply, Self::Error> {
match reply {
Reply::$reply(reply) => Ok(reply),
_ => Err(crate::Error::InternalError),
_ => Err(crate::error::Error::InternalError),
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,20 @@
//!
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::platform::{Platform, Syscall};
use crate::service::Service;
use crate::types::*;

pub use crate::platform::Syscall;
use crate::types::{
consent, reboot, Bytes, CertId, CounterId, KeyId, KeySerialization, Location, Mechanism,
MediumData, Message, PathBuf, SerializedKey, ShortData, Signature, SignatureSerialization,
StorageAttributes, UserAttribute,
};

pub mod mechanisms;
pub use mechanisms::*;

// to be fair, this is a programmer error,
// and could also just panic
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
3 changes: 1 addition & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use heapless::Vec;
use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize};
use zeroize::Zeroize;

pub use crate::Bytes;
use crate::{
config::{MAX_KEY_MATERIAL_LENGTH, MAX_SERIALIZED_KEY_LENGTH},
Error,
error::Error,
};

pub type Material = Vec<u8, { MAX_KEY_MATERIAL_LENGTH }>;
Expand Down
14 changes: 2 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
extern crate delog;
generate_macros!();

pub use interchange::Interchange;

pub mod api;
pub mod backend;
pub mod client;
Expand All @@ -42,23 +40,15 @@ pub mod types;
#[cfg_attr(docsrs, doc(cfg(feature = "virt")))]
pub mod virt;

pub use api::Reply;
pub use client::{Client, ClientImplementation};
pub use error::Error;
/// The trait that platforms need to implement to use Trussed.
pub use platform::Platform;
pub use service::Service;

pub use cbor_smol::{cbor_deserialize, cbor_serialize_bytes};
pub use heapless_bytes::Bytes;

pub(crate) use postcard::from_bytes as postcard_deserialize;

pub(crate) fn postcard_serialize_bytes<T: serde::Serialize, const N: usize>(
object: &T,
) -> postcard::Result<Bytes<N>> {
) -> postcard::Result<types::Bytes<N>> {
let vec = postcard::to_vec(object)?;
Ok(Bytes::from(vec))
Ok(types::Bytes::from(vec))
}

#[cfg(test)]
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
9 changes: 6 additions & 3 deletions src/mechanisms/totp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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;
use crate::types::Bytes;

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

Expand Down Expand Up @@ -70,7 +73,7 @@ impl Sign for super::Totp {

// return signature (encode as LE)
Ok(reply::Sign {
signature: crate::Bytes::from_slice(totp_material.to_le_bytes().as_ref()).unwrap(),
signature: Bytes::from_slice(totp_material.to_le_bytes().as_ref()).unwrap(),
})
}
}
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
Loading
Loading