Skip to content
Open
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
22 changes: 13 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "Apache-2.0 OR MIT"
repository = "https://github.com/trussed-dev/trussed"

[workspace.dependencies]
heapless-bytes = "0.3"
heapless-bytes = { version = "0.5", features = ["heapless-0.9"]}
littlefs2-core = { version = "0.1", features = ["serde"] }
postcard = "0.7.0"
rand_core = "0.6"
Expand All @@ -34,9 +34,9 @@ trussed-core = { version = "0.1.0" }
bitflags = { version = "2.1" }
# const-oid = "0.4.5"
cfg-if = "1.0"
flexiber = { version = "0.1.0", features = ["derive", "heapless"] }
flexiber = { version = "0.2.0", features = ["derive", "heapless"] }
generic-array = "0.14.4"
heapless = { version = "0.7", features = ["serde"] }
heapless = { version = "0.9", features = ["serde"] }
hex-literal = "0.4.1"
nb = "1"
postcard.workspace = true
Expand All @@ -50,20 +50,20 @@ aes = { version = "0.8", default-features = false }
cbc = "0.1.2"
blake2 = { version = "0.10", default-features = false, optional = true }
chacha20 = { version = "0.9", default-features = false }
chacha20poly1305 = { version = "0.10", default-features = false, features = ["heapless", "reduced-round"] }
chacha20poly1305 = { version = "0.10", default-features = false, features = ["reduced-round"] }
des = { version = "0.8", optional = true }
hmac = "0.12"
sha-1 = { version = "0.10", default-features = false, optional = true }
sha2 = { version = "0.10", default-features = false }

# ours
cosey = "0.3"
cosey = "0.4"
delog = "0.1.0"
cbor-smol = { version = "0.5", features = ["heapless-bytes-v0-3"] }
cbor-smol = { version = "0.5", features = ["heapless-bytes-v0-5"] }
heapless-bytes.workspace = true
interchange = "0.3.0"
littlefs2 = { version = "0.6.1", optional = true }
littlefs2-core = { workspace = true, features = ["heapless-bytes03"] }
littlefs2 = "0.7.0"
littlefs2-core = { workspace = true, features = ["heapless-bytes05"] }
p256-cortex-m4 = { version = "0.1.0-alpha.6", features = ["prehash", "sec1-signatures"] }
salty = { version = "0.3.0", features = ["cose"] }
p384 = { version = "0.13.0", optional = true, default-features = false, features = ["sha384", "ecdh", "ecdsa"] }
Expand All @@ -77,7 +77,7 @@ entropy = "0.4.0"
once_cell = "1.13.0"
serde_test = "1"
trussed-derive = { path = "derive" }
littlefs2 = "0.6"
littlefs2 = "0.7"
# Somehow, this is causing a regression.
# rand_core = { version = "0.5", features = ["getrandom"] }

Expand Down Expand Up @@ -203,3 +203,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[patch.crates-io]
trussed-core.path = "core"

littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "e9d3a1ca98f80e92cd20ee9b94707067810b9036" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance we can get rid of the patches for littlefs2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a breaking change. I think we would want cut a new littlefs2 release including trussed-dev/littlefs2#101

littlefs2-core = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "e9d3a1ca98f80e92cd20ee9b94707067810b9036" }
littlefs2-sys = { git = "https://github.com/trussed-dev/littlefs2-sys", rev = "v0.3.1-nitrokey.1" }
2 changes: 1 addition & 1 deletion core/src/client/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait CertificateClient: PollClient {
location: Location,
der: &[u8],
) -> ClientResult<'_, reply::WriteCertificate, Self> {
let der = Message::from_slice(der).map_err(|_| ClientError::DataTooLarge)?;
let der = Message::try_from(der).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::WriteCertificate { location, der })
}
}
30 changes: 15 additions & 15 deletions core/src/client/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ pub trait CryptoClient: PollClient {
nonce: &[u8],
tag: &[u8],
) -> ClientResult<'c, reply::Decrypt, Self> {
let message = Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?;
let message = Message::try_from(message).map_err(|_| ClientError::DataTooLarge)?;
let associated_data =
Message::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let nonce = ShortData::from_slice(nonce).map_err(|_| ClientError::DataTooLarge)?;
let tag = ShortData::from_slice(tag).map_err(|_| ClientError::DataTooLarge)?;
Message::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let nonce = ShortData::try_from(nonce).map_err(|_| ClientError::DataTooLarge)?;
let tag = ShortData::try_from(tag).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::Decrypt {
mechanism,
key,
Expand Down Expand Up @@ -99,7 +99,7 @@ pub trait CryptoClient: PollClient {
attributes: StorageAttributes,
) -> ClientResult<'c, reply::DeserializeKey, Self> {
let serialized_key =
SerializedKey::from_slice(serialized_key).map_err(|_| ClientError::DataTooLarge)?;
SerializedKey::try_from(serialized_key).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::DeserializeKey {
mechanism,
serialized_key,
Expand All @@ -116,9 +116,9 @@ pub trait CryptoClient: PollClient {
associated_data: &[u8],
nonce: Option<ShortData>,
) -> ClientResult<'c, reply::Encrypt, Self> {
let message = Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?;
let message = Message::try_from(message).map_err(|_| ClientError::DataTooLarge)?;
let associated_data =
ShortData::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
ShortData::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::Encrypt {
mechanism,
key,
Expand Down Expand Up @@ -193,7 +193,7 @@ pub trait CryptoClient: PollClient {
self.request(request::Sign {
key,
mechanism,
message: Bytes::from_slice(data).map_err(|_| ClientError::DataTooLarge)?,
message: Bytes::try_from(data).map_err(|_| ClientError::DataTooLarge)?,
format,
})
}
Expand All @@ -209,8 +209,8 @@ pub trait CryptoClient: PollClient {
self.request(request::Verify {
mechanism,
key,
message: Message::from_slice(message).expect("all good"),
signature: Signature::from_slice(signature).expect("all good"),
message: Message::try_from(message).expect("all good"),
signature: Signature::try_from(signature).expect("all good"),
format,
})
}
Expand All @@ -224,7 +224,7 @@ pub trait CryptoClient: PollClient {
) -> ClientResult<'_, reply::UnsafeInjectKey, Self> {
self.request(request::UnsafeInjectKey {
mechanism,
raw_key: SerializedKey::from_slice(raw_key).unwrap(),
raw_key: SerializedKey::try_from(raw_key).unwrap(),
attributes: StorageAttributes::new().set_persistence(persistence),
format,
})
Expand All @@ -236,7 +236,7 @@ pub trait CryptoClient: PollClient {
location: Location,
) -> ClientResult<'_, reply::UnsafeInjectSharedKey, Self> {
self.request(request::UnsafeInjectSharedKey {
raw_key: ShortData::from_slice(raw_key).unwrap(),
raw_key: ShortData::try_from(raw_key).unwrap(),
location,
})
}
Expand All @@ -251,8 +251,8 @@ pub trait CryptoClient: PollClient {
attributes: StorageAttributes,
) -> ClientResult<'c, reply::UnwrapKey, Self> {
let associated_data =
Message::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let nonce = ShortData::from_slice(nonce).map_err(|_| ClientError::DataTooLarge)?;
Message::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let nonce = ShortData::try_from(nonce).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::UnwrapKey {
mechanism,
wrapping_key,
Expand All @@ -272,7 +272,7 @@ pub trait CryptoClient: PollClient {
nonce: Option<ShortData>,
) -> ClientResult<'_, reply::WrapKey, Self> {
let associated_data =
Bytes::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
Bytes::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::WrapKey {
mechanism,
wrapping_key,
Expand Down
18 changes: 9 additions & 9 deletions core/src/mechanisms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait Aes256Cbc: CryptoClient {
wrapping_key,
key,
&[],
iv.and_then(|iv| ShortData::from_slice(iv).ok()),
iv.map(ShortData::from),
)
}
}
Expand Down Expand Up @@ -66,7 +66,7 @@ pub trait Chacha8Poly1305: CryptoClient {
key,
message,
associated_data,
nonce.and_then(|nonce| ShortData::from_slice(nonce).ok()),
nonce.map(ShortData::from),
)
}

Expand All @@ -90,7 +90,7 @@ pub trait Chacha8Poly1305: CryptoClient {
self.unwrap_key(
Mechanism::Chacha8Poly1305,
wrapping_key,
Message::from_slice(wrapped_key).map_err(|_| ClientError::DataTooLarge)?,
Message::try_from(wrapped_key).map_err(|_| ClientError::DataTooLarge)?,
associated_data,
&[],
StorageAttributes::new().set_persistence(location),
Expand All @@ -109,7 +109,7 @@ pub trait Chacha8Poly1305: CryptoClient {
wrapping_key,
key,
associated_data,
nonce.and_then(|nonce| ShortData::from_slice(nonce).ok()),
nonce.map(ShortData::from),
)
}
}
Expand All @@ -125,7 +125,7 @@ pub trait HmacBlake2s: CryptoClient {
self.derive_key(
Mechanism::HmacBlake2s,
base_key,
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
Some(MediumData::try_from(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence),
)
}
Expand Down Expand Up @@ -155,7 +155,7 @@ pub trait HmacSha1: CryptoClient {
self.derive_key(
Mechanism::HmacSha1,
base_key,
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
Some(MediumData::try_from(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence),
)
}
Expand Down Expand Up @@ -185,7 +185,7 @@ pub trait HmacSha256: CryptoClient {
self.derive_key(
Mechanism::HmacSha256,
base_key,
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
Some(MediumData::try_from(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence),
)
}
Expand Down Expand Up @@ -215,7 +215,7 @@ pub trait HmacSha512: CryptoClient {
self.derive_key(
Mechanism::HmacSha512,
base_key,
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
Some(MediumData::try_from(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence),
)
}
Expand Down Expand Up @@ -579,7 +579,7 @@ pub trait Sha256: CryptoClient {
fn hash_sha256<'c>(&'c mut self, message: &[u8]) -> ClientResult<'c, reply::Hash, Self> {
self.hash(
Mechanism::Sha256,
Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?,
Message::try_from(message).map_err(|_| ClientError::DataTooLarge)?,
)
}
}
Expand Down
16 changes: 12 additions & 4 deletions core/src/serde_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ use crate::{
types::Bytes,
};

pub(crate) fn postcard_serialize_bytes<T: serde::Serialize, const N: usize>(
object: &T,
) -> postcard::Result<Bytes<N>> {
let mut vec = Bytes::new();
vec.resize_to_capacity();
let serialized = postcard::to_slice(object, &mut vec)?.len();
vec.resize(serialized, 0).unwrap();
Ok(vec)
}

/// A Trussed API extension.
pub trait Extension {
/// The requests supported by this extension.
Expand All @@ -37,8 +47,7 @@ pub trait Extension {
id: u8,
request: &Self::Request,
) -> Result<request::SerdeExtension, ClientError> {
postcard::to_vec(request)
.map(Bytes::from)
postcard_serialize_bytes(request)
.map(|request| request::SerdeExtension { id, request })
.map_err(|_| ClientError::SerializationFailed)
}
Expand All @@ -60,8 +69,7 @@ pub trait Extension {
/// crate releases.
#[inline(never)]
fn serialize_reply(reply: &Self::Reply) -> Result<reply::SerdeExtension, Error> {
postcard::to_vec(reply)
.map(Bytes::from)
postcard_serialize_bytes(reply)
.map(|reply| reply::SerdeExtension { reply })
.map_err(|_| Error::ReplySerializationFailure)
}
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ 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>> {
let vec = postcard::to_vec(object)?;
Ok(Bytes::from(vec))
let mut vec = Bytes::new();
vec.resize_to_capacity();
let serialized = postcard::to_slice(object, &mut vec)?.len();
vec.resize(serialized, 0).unwrap();
Ok(vec)
}

#[cfg(all(test, feature = "crypto-client", feature = "filesystem-client"))]
Expand Down
6 changes: 3 additions & 3 deletions src/mechanisms/aes256cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl MechanismImpl for super::Aes256Cbc {
.encrypt_padded_mut::<NoPadding>(&mut buffer, l)
.map_err(|_| Error::MechanismParamInvalid)?;

let ciphertext = Message::from_slice(ciphertext).unwrap();
let ciphertext = Message::try_from(ciphertext).unwrap();
Ok(reply::Encrypt {
ciphertext,
nonce: ShortData::new(),
Expand All @@ -73,7 +73,7 @@ impl MechanismImpl for super::Aes256Cbc {

// let message: Message = serialized_key.material.try_to_byte_buf().map_err(|_| Error::InternalError)?;

let message = Message::from_slice(
let message = Message::try_from(
keystore
.load_key(key::Secrecy::Secret, None, &request.key)?
.material
Expand Down Expand Up @@ -143,7 +143,7 @@ impl MechanismImpl for super::Aes256Cbc {
.decrypt_padded_mut::<NoPadding>(&mut buffer)
.map_err(|_| Error::MechanismParamInvalid)?;
// hprintln!("decrypted: {:?}", &plaintext).ok();
let plaintext = Message::from_slice(plaintext).unwrap();
let plaintext = Message::try_from(plaintext).unwrap();

Ok(reply::Decrypt {
plaintext: Some(plaintext),
Expand Down
6 changes: 3 additions & 3 deletions src/mechanisms/chacha8poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ impl MechanismImpl for super::Chacha8Poly1305 {
.try_into()
.unwrap();

let nonce = ShortData::from_slice(&nonce).unwrap();
let tag = ShortData::from_slice(&tag).unwrap();
let nonce = ShortData::from(&nonce);
let tag = ShortData::from(&tag);

// let ciphertext = Message::from_slice(&ciphertext).unwrap();
Ok(reply::Encrypt {
Expand All @@ -174,7 +174,7 @@ impl MechanismImpl for super::Chacha8Poly1305 {
// TODO: need to check both secret and private keys
let serialized_key = keystore.load_key(key::Secrecy::Secret, None, &request.key)?;

let message = Message::from_slice(&serialized_key.serialize()).unwrap();
let message = Message::try_from(&*serialized_key.serialize()).unwrap();

let encryption_request = request::Encrypt {
mechanism: Mechanism::Chacha8Poly1305,
Expand Down
4 changes: 2 additions & 2 deletions src/mechanisms/ed255.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl MechanismImpl for super::Ed255 {
let cose_pk = cosey::Ed25519PublicKey {
// x: Bytes::from_slice(public_key.x_coordinate()).unwrap(),
// x: Bytes::from_slice(&buf).unwrap(),
x: Bytes::from_slice(public_key.as_bytes()).unwrap(),
x: Bytes::from(public_key.as_bytes()),
};
crate::cbor_serialize_bytes(&cose_pk).map_err(|_| Error::CborError)?
}
Expand Down Expand Up @@ -187,7 +187,7 @@ impl MechanismImpl for super::Ed255 {
let keypair = load_keypair(keystore, &key_id)?;

let native_signature = keypair.sign(&request.message);
let our_signature = Signature::from_slice(&native_signature.to_bytes()).unwrap();
let our_signature = Signature::from(&native_signature.to_bytes());

// hprintln!("Ed255 signature:").ok();
// hprintln!("msg: {:?}", &request.message).ok();
Expand Down
2 changes: 1 addition & 1 deletion src/mechanisms/hmacblake2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl MechanismImpl for super::HmacBlake2s {

mac.update(&request.message);
let result = mac.finalize();
let signature = Signature::from_slice(&result.into_bytes()).unwrap();
let signature = Signature::try_from(&*result.into_bytes()).unwrap();

Ok(reply::Sign { signature })
}
Expand Down
2 changes: 1 addition & 1 deletion src/mechanisms/hmacsha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl MechanismImpl for super::HmacSha1 {

mac.update(&request.message);
let result = mac.finalize();
let signature = Signature::from_slice(&result.into_bytes()).unwrap();
let signature = Signature::try_from(&*result.into_bytes()).unwrap();

Ok(reply::Sign { signature })
}
Expand Down
2 changes: 1 addition & 1 deletion src/mechanisms/hmacsha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl MechanismImpl for super::HmacSha256 {

mac.update(&request.message);
let result = mac.finalize();
let signature = Signature::from_slice(&result.into_bytes()).unwrap();
let signature = Signature::try_from(&*result.into_bytes()).unwrap();

Ok(reply::Sign { signature })
}
Expand Down
Loading