Skip to content

Commit 4d51343

Browse files
Update to heapless 0.9
1 parent 0c75880 commit 4d51343

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license = "Apache-2.0 OR MIT"
1111
repository = "https://github.com/trussed-dev/trussed-rsa-backend"
1212

1313
[workspace.dependencies]
14-
heapless-bytes = "0.3"
14+
heapless-bytes = "0.5"
1515
trussed-core = "0.1"
1616

1717
[package]
@@ -32,7 +32,7 @@ num-bigint-dig = { version = "0.8.2", default-features = false }
3232
rsa = { version = "0.9", default-features = false, features = ["sha2"]}
3333

3434
trussed = { version = "0.1", default-features = false }
35-
trussed-core = { workspace = true, features = ["crypto-client", "rsa2048", "rsa3072", "rsa4096"] }
35+
trussed-core.workspace = true
3636
trussed-rsa-types = "0.1"
3737

3838
[dev-dependencies]
@@ -42,7 +42,7 @@ delog = { version = "0.1.6", features = ["std-log"] }
4242
test-log = "0.2.11"
4343
env_logger = "0.10.0"
4444
rand = "0.8.5"
45-
trussed = { version = "0.1", default-features = false, features = ["certificate-client", "clients-1", "crypto-client"] }
45+
trussed = { version = "0.1", default-features = false, features = ["clients-1"] }
4646

4747
[features]
4848

src/crypto_traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use trussed_core::{
66
reply,
77
request::{DeserializeKey, UnsafeInjectKey},
88
},
9+
client::{ClientError, ClientResult, CryptoClient},
910
types::{
1011
KeyId, KeySerialization, Location, Mechanism, SignatureSerialization, StorageAttributes,
1112
},
12-
ClientError, ClientResult, CryptoClient,
1313
};
1414
use trussed_rsa_types::{RsaImportFormat, RsaPublicParts};
1515

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ fn sign(
270270
Error::InternalError
271271
})?;
272272
let our_signature =
273-
Signature::from_slice(&native_signature.to_bytes()).unwrap_or_else(|_| panic!());
273+
Signature::try_from(&*native_signature.to_bytes()).unwrap_or_else(|_| panic!());
274274

275275
Ok(reply::Sign {
276276
signature: our_signature,
@@ -337,7 +337,7 @@ fn decrypt(
337337
})?;
338338

339339
Ok(reply::Decrypt {
340-
plaintext: Some(Bytes::from_slice(&res).map_err(|_| {
340+
plaintext: Some(Bytes::try_from(&*res).map_err(|_| {
341341
error!("Failed type conversion");
342342
Error::InternalError
343343
})?),

types/src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ pub struct Error {
2525
kind: ErrorKind,
2626
}
2727

28+
pub(crate) fn postcard_serialize_bytes<T: serde::Serialize, const N: usize>(
29+
object: &T,
30+
) -> postcard::Result<Bytes<N>> {
31+
let mut vec = Bytes::new();
32+
vec.resize_to_capacity();
33+
let serialized = postcard::to_slice(object, &mut vec)?.len();
34+
vec.resize(serialized, 0).unwrap();
35+
Ok(vec)
36+
}
37+
2838
/// Structure containing the public part of an RSA key
2939
///
3040
/// Given how Trussed extensions are implemented, this structure cannot be sent as-is to the backend,
@@ -42,15 +52,14 @@ pub struct RsaPublicParts<'d> {
4252
impl<'d> RsaPublicParts<'d> {
4353
pub fn serialize(&self) -> Result<SerializedKey, Error> {
4454
use postcard::Error as PError;
45-
let vec = postcard::to_vec(self).map_err(|err| match err {
55+
Ok(postcard_serialize_bytes(self).map_err(|err| match err {
4656
PError::SerializeBufferFull => Error {
4757
kind: ErrorKind::SerializeBufferFull,
4858
},
4959
_ => Error {
5060
kind: ErrorKind::SerializeCustom,
5161
},
52-
})?;
53-
Ok(Bytes::from(vec))
62+
})?)
5463
}
5564

5665
pub fn deserialize(data: &'d [u8]) -> Result<Self, Error> {
@@ -79,15 +88,14 @@ pub struct RsaImportFormat<'d> {
7988
impl<'d> RsaImportFormat<'d> {
8089
pub fn serialize(&self) -> Result<SerializedKey, Error> {
8190
use postcard::Error as PError;
82-
let vec = postcard::to_vec(self).map_err(|err| match err {
91+
Ok(postcard_serialize_bytes(self).map_err(|err| match err {
8392
PError::SerializeBufferFull => Error {
8493
kind: ErrorKind::SerializeBufferFull,
8594
},
8695
_ => Error {
8796
kind: ErrorKind::SerializeCustom,
8897
},
89-
})?;
90-
Ok(Bytes::from(vec))
98+
})?)
9199
}
92100

93101
pub fn deserialize(data: &'d [u8]) -> Result<Self, Error> {

0 commit comments

Comments
 (0)