From 2b213109d388f82b046cf45508044bf870e07720 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 11 Mar 2025 09:45:52 +0100 Subject: [PATCH] Replace heapless dependency with heapless-bytes We already use heapless-bytes, so replacing the remaining heapless::Vec usages with heapless_bytes::Bytes makes it easier to reason over the dependencies and eventually update to a new version. --- Cargo.toml | 1 - src/key.rs | 5 ++-- src/mechanisms/p384.rs | 6 ++--- src/mechanisms/p521.rs | 6 ++--- src/service/attest.rs | 56 ------------------------------------------ src/types.rs | 2 -- 6 files changed, 8 insertions(+), 68 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 57dc5f996cd..a2788358c11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ bitflags = { version = "2.1" } cfg-if = "1.0" flexiber = { version = "0.1.0", features = ["derive", "heapless"] } generic-array = "0.14.4" -heapless = { version = "0.7", features = ["serde"] } hex-literal = "0.4.1" nb = "1" postcard.workspace = true diff --git a/src/key.rs b/src/key.rs index b071d324a9c..1410a194b99 100644 --- a/src/key.rs +++ b/src/key.rs @@ -1,7 +1,6 @@ use core::ptr::write_volatile; use core::sync::atomic; -use heapless::Vec; use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize}; use zeroize::Zeroize; @@ -11,8 +10,8 @@ use crate::{ Error, }; -pub type Material = Vec; -pub type SerializedKeyBytes = Vec; +pub type Material = Bytes; +pub type SerializedKeyBytes = Bytes; // We don't implement serde to make sure nobody inadvertently still uses it // Should we use references here only? diff --git a/src/mechanisms/p384.rs b/src/mechanisms/p384.rs index 91eaf6b488e..55673fba7c0 100644 --- a/src/mechanisms/p384.rs +++ b/src/mechanisms/p384.rs @@ -14,7 +14,7 @@ use crate::{ key, service::MechanismImpl, store::keystore::Keystore, - types::{KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization}, + types::{Bytes, KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization}, Error, }; @@ -44,9 +44,9 @@ fn load_public_key(keystore: &mut impl Keystore, key_id: &KeyId) -> Result heapless::Vec { +fn to_sec1_bytes(public_key: &p384::PublicKey) -> Bytes<{ SCALAR_SIZE * 2 + 1 }> { let encoded_point: p384::EncodedPoint = public_key.into(); - encoded_point.as_bytes().try_into().unwrap() + Bytes::from_slice(encoded_point.as_bytes()).unwrap() } impl MechanismImpl for P384 { diff --git a/src/mechanisms/p521.rs b/src/mechanisms/p521.rs index 227eab2947b..9d9212dba5c 100644 --- a/src/mechanisms/p521.rs +++ b/src/mechanisms/p521.rs @@ -14,7 +14,7 @@ use crate::{ key, service::MechanismImpl, store::keystore::Keystore, - types::{KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization}, + types::{Bytes, KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization}, Error, }; @@ -44,9 +44,9 @@ fn load_public_key(keystore: &mut impl Keystore, key_id: &KeyId) -> Result heapless::Vec { +fn to_sec1_bytes(public_key: &p521::PublicKey) -> Bytes<{ SCALAR_SIZE * 2 + 1 }> { let encoded_point: p521::EncodedPoint = public_key.into(); - encoded_point.as_bytes().try_into().unwrap() + Bytes::from_slice(encoded_point.as_bytes()).unwrap() } impl MechanismImpl for P521 { diff --git a/src/service/attest.rs b/src/service/attest.rs index b7feb073573..9fe53ded755 100644 --- a/src/service/attest.rs +++ b/src/service/attest.rs @@ -479,62 +479,6 @@ impl Encodable for Name<'_> { } } -pub struct ParsedDatetime { - year: u16, - month: u8, - day: u8, - hour: u8, - minute: u8, - second: u8, -} - -impl ParsedDatetime { - pub fn new(year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> Option { - let valid = [ - year >= 2000, - year <= 9999, - month >= 1, - month <= 12, - day >= 1, - day <= 31, - hour <= 23, - minute <= 59, - second <= 59, - ] - .iter() - .all(|b| *b); - - if valid { - Some(Self { - year, - month, - day, - hour, - minute, - second, - }) - } else { - None - } - } - - pub fn to_bytes(&self) -> [u8; 15] { - let mut buffer: heapless::Vec = Default::default(); - buffer.resize_default(15).unwrap(); - core::fmt::write( - &mut buffer, - format_args!( - "{}{:02}{:02}{:02}{:02}{:02}Z", - self.year, self.month, self.day, self.hour, self.minute, self.second - ), - ) - .unwrap(); - let mut array = [0u8; 15]; - array.copy_from_slice(&buffer); - array - } -} - #[derive(Clone, Copy, Eq, PartialEq)] /// Encoded as "YYYYMMDDHHMMSSZ", encoding takes care of truncating YYYY to YY if necessary. pub struct Datetime<'l>(&'l [u8]); diff --git a/src/types.rs b/src/types.rs index 2a419889781..a1f03307c79 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,7 +1,5 @@ pub use generic_array::GenericArray; -pub use heapless::{String, Vec}; - pub use crate::Bytes; pub use littlefs2_core::{DirEntry, Metadata, Path, PathBuf, Result as LfsResult};