Skip to content

Commit c598333

Browse files
Update to heapless 0.9
1 parent e107ed3 commit c598333

24 files changed

+107
-92
lines changed

Cargo.toml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "Apache-2.0 OR MIT"
99
repository = "https://github.com/trussed-dev/trussed"
1010

1111
[workspace.dependencies]
12-
heapless-bytes = "0.3"
12+
heapless-bytes = { version = "0.5", features = ["heapless-0.9"]}
1313
littlefs2-core = { version = "0.1", features = ["serde"] }
1414
postcard = "0.7.0"
1515
rand_core = "0.6"
@@ -34,9 +34,9 @@ trussed-core = { version = "0.1.0" }
3434
bitflags = { version = "2.1" }
3535
# const-oid = "0.4.5"
3636
cfg-if = "1.0"
37-
flexiber = { version = "0.1.0", features = ["derive", "heapless"] }
37+
flexiber = { version = "0.2.0", features = ["derive", "heapless"] }
3838
generic-array = "0.14.4"
39-
heapless = { version = "0.7", features = ["serde"] }
39+
heapless = { version = "0.9", features = ["serde"] }
4040
hex-literal = "0.4.1"
4141
nb = "1"
4242
postcard.workspace = true
@@ -50,20 +50,20 @@ aes = { version = "0.8", default-features = false }
5050
cbc = "0.1.2"
5151
blake2 = { version = "0.10", default-features = false, optional = true }
5252
chacha20 = { version = "0.9", default-features = false }
53-
chacha20poly1305 = { version = "0.10", default-features = false, features = ["heapless", "reduced-round"] }
53+
chacha20poly1305 = { version = "0.10", default-features = false, features = ["reduced-round"] }
5454
des = { version = "0.8", optional = true }
5555
hmac = "0.12"
5656
sha-1 = { version = "0.10", default-features = false, optional = true }
5757
sha2 = { version = "0.10", default-features = false }
5858

5959
# ours
60-
cosey = "0.3"
60+
cosey = "0.4"
6161
delog = "0.1.0"
62-
cbor-smol = { version = "0.5", features = ["heapless-bytes-v0-3"] }
62+
cbor-smol = { version = "0.5", features = ["heapless-bytes-v0-5"] }
6363
heapless-bytes.workspace = true
6464
interchange = "0.3.0"
65-
littlefs2 = { version = "0.6.1", optional = true }
66-
littlefs2-core = { workspace = true, features = ["heapless-bytes03"] }
65+
littlefs2 = "0.7.0"
66+
littlefs2-core = { workspace = true, features = ["heapless-bytes05"] }
6767
p256-cortex-m4 = { version = "0.1.0-alpha.6", features = ["prehash", "sec1-signatures"] }
6868
salty = { version = "0.3.0", features = ["cose"] }
6969
p384 = { version = "0.13.0", optional = true, default-features = false, features = ["sha384", "ecdh", "ecdsa"] }
@@ -77,7 +77,7 @@ entropy = "0.4.0"
7777
once_cell = "1.13.0"
7878
serde_test = "1"
7979
trussed-derive = { path = "derive" }
80-
littlefs2 = "0.6"
80+
littlefs2 = "0.7"
8181
# Somehow, this is causing a regression.
8282
# rand_core = { version = "0.5", features = ["getrandom"] }
8383

@@ -203,3 +203,7 @@ rustdoc-args = ["--cfg", "docsrs"]
203203

204204
[patch.crates-io]
205205
trussed-core.path = "core"
206+
207+
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "e9d3a1ca98f80e92cd20ee9b94707067810b9036" }
208+
littlefs2-core = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "e9d3a1ca98f80e92cd20ee9b94707067810b9036" }
209+
littlefs2-sys = { git = "https://github.com/trussed-dev/littlefs2-sys", rev = "v0.3.1-nitrokey.1" }

core/src/client/certificate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub trait CertificateClient: PollClient {
2626
location: Location,
2727
der: &[u8],
2828
) -> ClientResult<'_, reply::WriteCertificate, Self> {
29-
let der = Message::from_slice(der).map_err(|_| ClientError::DataTooLarge)?;
29+
let der = Message::try_from(der).map_err(|_| ClientError::DataTooLarge)?;
3030
self.request(request::WriteCertificate { location, der })
3131
}
3232
}

core/src/client/crypto.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ pub trait CryptoClient: PollClient {
3737
nonce: &[u8],
3838
tag: &[u8],
3939
) -> ClientResult<'c, reply::Decrypt, Self> {
40-
let message = Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?;
40+
let message = Message::try_from(message).map_err(|_| ClientError::DataTooLarge)?;
4141
let associated_data =
42-
Message::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
43-
let nonce = ShortData::from_slice(nonce).map_err(|_| ClientError::DataTooLarge)?;
44-
let tag = ShortData::from_slice(tag).map_err(|_| ClientError::DataTooLarge)?;
42+
Message::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
43+
let nonce = ShortData::try_from(nonce).map_err(|_| ClientError::DataTooLarge)?;
44+
let tag = ShortData::try_from(tag).map_err(|_| ClientError::DataTooLarge)?;
4545
self.request(request::Decrypt {
4646
mechanism,
4747
key,
@@ -99,7 +99,7 @@ pub trait CryptoClient: PollClient {
9999
attributes: StorageAttributes,
100100
) -> ClientResult<'c, reply::DeserializeKey, Self> {
101101
let serialized_key =
102-
SerializedKey::from_slice(serialized_key).map_err(|_| ClientError::DataTooLarge)?;
102+
SerializedKey::try_from(serialized_key).map_err(|_| ClientError::DataTooLarge)?;
103103
self.request(request::DeserializeKey {
104104
mechanism,
105105
serialized_key,
@@ -116,9 +116,9 @@ pub trait CryptoClient: PollClient {
116116
associated_data: &[u8],
117117
nonce: Option<ShortData>,
118118
) -> ClientResult<'c, reply::Encrypt, Self> {
119-
let message = Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?;
119+
let message = Message::try_from(message).map_err(|_| ClientError::DataTooLarge)?;
120120
let associated_data =
121-
ShortData::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
121+
ShortData::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
122122
self.request(request::Encrypt {
123123
mechanism,
124124
key,
@@ -193,7 +193,7 @@ pub trait CryptoClient: PollClient {
193193
self.request(request::Sign {
194194
key,
195195
mechanism,
196-
message: Bytes::from_slice(data).map_err(|_| ClientError::DataTooLarge)?,
196+
message: Bytes::try_from(data).map_err(|_| ClientError::DataTooLarge)?,
197197
format,
198198
})
199199
}
@@ -209,8 +209,8 @@ pub trait CryptoClient: PollClient {
209209
self.request(request::Verify {
210210
mechanism,
211211
key,
212-
message: Message::from_slice(message).expect("all good"),
213-
signature: Signature::from_slice(signature).expect("all good"),
212+
message: Message::try_from(message).expect("all good"),
213+
signature: Signature::try_from(signature).expect("all good"),
214214
format,
215215
})
216216
}
@@ -224,7 +224,7 @@ pub trait CryptoClient: PollClient {
224224
) -> ClientResult<'_, reply::UnsafeInjectKey, Self> {
225225
self.request(request::UnsafeInjectKey {
226226
mechanism,
227-
raw_key: SerializedKey::from_slice(raw_key).unwrap(),
227+
raw_key: SerializedKey::try_from(raw_key).unwrap(),
228228
attributes: StorageAttributes::new().set_persistence(persistence),
229229
format,
230230
})
@@ -236,7 +236,7 @@ pub trait CryptoClient: PollClient {
236236
location: Location,
237237
) -> ClientResult<'_, reply::UnsafeInjectSharedKey, Self> {
238238
self.request(request::UnsafeInjectSharedKey {
239-
raw_key: ShortData::from_slice(raw_key).unwrap(),
239+
raw_key: ShortData::try_from(raw_key).unwrap(),
240240
location,
241241
})
242242
}
@@ -251,8 +251,8 @@ pub trait CryptoClient: PollClient {
251251
attributes: StorageAttributes,
252252
) -> ClientResult<'c, reply::UnwrapKey, Self> {
253253
let associated_data =
254-
Message::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
255-
let nonce = ShortData::from_slice(nonce).map_err(|_| ClientError::DataTooLarge)?;
254+
Message::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
255+
let nonce = ShortData::try_from(nonce).map_err(|_| ClientError::DataTooLarge)?;
256256
self.request(request::UnwrapKey {
257257
mechanism,
258258
wrapping_key,
@@ -272,7 +272,7 @@ pub trait CryptoClient: PollClient {
272272
nonce: Option<ShortData>,
273273
) -> ClientResult<'_, reply::WrapKey, Self> {
274274
let associated_data =
275-
Bytes::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
275+
Bytes::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
276276
self.request(request::WrapKey {
277277
mechanism,
278278
wrapping_key,

core/src/mechanisms.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub trait Aes256Cbc: CryptoClient {
2929
wrapping_key,
3030
key,
3131
&[],
32-
iv.and_then(|iv| ShortData::from_slice(iv).ok()),
32+
iv.and_then(|iv| ShortData::try_from(iv).ok()),
3333
)
3434
}
3535
}
@@ -66,7 +66,7 @@ pub trait Chacha8Poly1305: CryptoClient {
6666
key,
6767
message,
6868
associated_data,
69-
nonce.and_then(|nonce| ShortData::from_slice(nonce).ok()),
69+
nonce.and_then(|nonce| ShortData::try_from(nonce).ok()),
7070
)
7171
}
7272

@@ -90,7 +90,7 @@ pub trait Chacha8Poly1305: CryptoClient {
9090
self.unwrap_key(
9191
Mechanism::Chacha8Poly1305,
9292
wrapping_key,
93-
Message::from_slice(wrapped_key).map_err(|_| ClientError::DataTooLarge)?,
93+
Message::try_from(wrapped_key).map_err(|_| ClientError::DataTooLarge)?,
9494
associated_data,
9595
&[],
9696
StorageAttributes::new().set_persistence(location),
@@ -109,7 +109,7 @@ pub trait Chacha8Poly1305: CryptoClient {
109109
wrapping_key,
110110
key,
111111
associated_data,
112-
nonce.and_then(|nonce| ShortData::from_slice(nonce).ok()),
112+
nonce.and_then(|nonce| ShortData::try_from(nonce).ok()),
113113
)
114114
}
115115
}
@@ -125,7 +125,7 @@ pub trait HmacBlake2s: CryptoClient {
125125
self.derive_key(
126126
Mechanism::HmacBlake2s,
127127
base_key,
128-
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
128+
Some(MediumData::try_from(message).map_err(|_| ClientError::DataTooLarge)?),
129129
StorageAttributes::new().set_persistence(persistence),
130130
)
131131
}
@@ -155,7 +155,7 @@ pub trait HmacSha1: CryptoClient {
155155
self.derive_key(
156156
Mechanism::HmacSha1,
157157
base_key,
158-
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
158+
Some(MediumData::try_from(message).map_err(|_| ClientError::DataTooLarge)?),
159159
StorageAttributes::new().set_persistence(persistence),
160160
)
161161
}
@@ -185,7 +185,7 @@ pub trait HmacSha256: CryptoClient {
185185
self.derive_key(
186186
Mechanism::HmacSha256,
187187
base_key,
188-
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
188+
Some(MediumData::try_from(message).map_err(|_| ClientError::DataTooLarge)?),
189189
StorageAttributes::new().set_persistence(persistence),
190190
)
191191
}
@@ -215,7 +215,7 @@ pub trait HmacSha512: CryptoClient {
215215
self.derive_key(
216216
Mechanism::HmacSha512,
217217
base_key,
218-
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
218+
Some(MediumData::try_from(message).map_err(|_| ClientError::DataTooLarge)?),
219219
StorageAttributes::new().set_persistence(persistence),
220220
)
221221
}
@@ -579,7 +579,7 @@ pub trait Sha256: CryptoClient {
579579
fn hash_sha256<'c>(&'c mut self, message: &[u8]) -> ClientResult<'c, reply::Hash, Self> {
580580
self.hash(
581581
Mechanism::Sha256,
582-
Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?,
582+
Message::try_from(message).map_err(|_| ClientError::DataTooLarge)?,
583583
)
584584
}
585585
}

core/src/serde_extensions.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ use crate::{
2020
types::Bytes,
2121
};
2222

23+
pub(crate) fn postcard_serialize_bytes<T: serde::Serialize, const N: usize>(
24+
object: &T,
25+
) -> postcard::Result<Bytes<N>> {
26+
let mut vec = Bytes::new();
27+
vec.resize_to_capacity();
28+
let serialized = postcard::to_slice(object, &mut vec)?.len();
29+
vec.resize(serialized, 0).unwrap();
30+
Ok(vec)
31+
}
32+
2333
/// A Trussed API extension.
2434
pub trait Extension {
2535
/// The requests supported by this extension.
@@ -37,8 +47,7 @@ pub trait Extension {
3747
id: u8,
3848
request: &Self::Request,
3949
) -> Result<request::SerdeExtension, ClientError> {
40-
postcard::to_vec(request)
41-
.map(Bytes::from)
50+
postcard_serialize_bytes(request)
4251
.map(|request| request::SerdeExtension { id, request })
4352
.map_err(|_| ClientError::SerializationFailed)
4453
}
@@ -60,8 +69,7 @@ pub trait Extension {
6069
/// crate releases.
6170
#[inline(never)]
6271
fn serialize_reply(reply: &Self::Reply) -> Result<reply::SerdeExtension, Error> {
63-
postcard::to_vec(reply)
64-
.map(Bytes::from)
72+
postcard_serialize_bytes(reply)
6573
.map(|reply| reply::SerdeExtension { reply })
6674
.map_err(|_| Error::ReplySerializationFailure)
6775
}

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ pub(crate) use postcard::from_bytes as postcard_deserialize;
7272
pub(crate) fn postcard_serialize_bytes<T: serde::Serialize, const N: usize>(
7373
object: &T,
7474
) -> postcard::Result<Bytes<N>> {
75-
let vec = postcard::to_vec(object)?;
76-
Ok(Bytes::from(vec))
75+
let mut vec = Bytes::new();
76+
vec.resize_to_capacity();
77+
let serialized = postcard::to_slice(object, &mut vec)?.len();
78+
vec.resize(serialized, 0).unwrap();
79+
Ok(vec)
7780
}
7881

7982
#[cfg(all(test, feature = "crypto-client", feature = "filesystem-client"))]

src/mechanisms/aes256cbc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl MechanismImpl for super::Aes256Cbc {
5454
.encrypt_padded_mut::<NoPadding>(&mut buffer, l)
5555
.map_err(|_| Error::MechanismParamInvalid)?;
5656

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

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

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

148148
Ok(reply::Decrypt {
149149
plaintext: Some(plaintext),

src/mechanisms/chacha8poly1305.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ impl MechanismImpl for super::Chacha8Poly1305 {
152152
.try_into()
153153
.unwrap();
154154

155-
let nonce = ShortData::from_slice(&nonce).unwrap();
156-
let tag = ShortData::from_slice(&tag).unwrap();
155+
let nonce = ShortData::try_from(&nonce).unwrap();
156+
let tag = ShortData::try_from(&tag).unwrap();
157157

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

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

179179
let encryption_request = request::Encrypt {
180180
mechanism: Mechanism::Chacha8Poly1305,

src/mechanisms/ed255.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl MechanismImpl for super::Ed255 {
132132
let cose_pk = cosey::Ed25519PublicKey {
133133
// x: Bytes::from_slice(public_key.x_coordinate()).unwrap(),
134134
// x: Bytes::from_slice(&buf).unwrap(),
135-
x: Bytes::from_slice(public_key.as_bytes()).unwrap(),
135+
x: Bytes::try_from(public_key.as_bytes()).unwrap(),
136136
};
137137
crate::cbor_serialize_bytes(&cose_pk).map_err(|_| Error::CborError)?
138138
}
@@ -187,7 +187,7 @@ impl MechanismImpl for super::Ed255 {
187187
let keypair = load_keypair(keystore, &key_id)?;
188188

189189
let native_signature = keypair.sign(&request.message);
190-
let our_signature = Signature::from_slice(&native_signature.to_bytes()).unwrap();
190+
let our_signature = Signature::try_from(&native_signature.to_bytes()).unwrap();
191191

192192
// hprintln!("Ed255 signature:").ok();
193193
// hprintln!("msg: {:?}", &request.message).ok();

src/mechanisms/hmacblake2s.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl MechanismImpl for super::HmacBlake2s {
6060

6161
mac.update(&request.message);
6262
let result = mac.finalize();
63-
let signature = Signature::from_slice(&result.into_bytes()).unwrap();
63+
let signature = Signature::try_from(&*result.into_bytes()).unwrap();
6464

6565
Ok(reply::Sign { signature })
6666
}

0 commit comments

Comments
 (0)