Skip to content

Commit 5125d4b

Browse files
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.
1 parent 91feea8 commit 5125d4b

7 files changed

Lines changed: 11 additions & 71 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ bitflags = { version = "2.1" }
3636
cfg-if = "1.0"
3737
flexiber = { version = "0.2.0", features = ["derive", "heapless"] }
3838
generic-array = "0.14.4"
39-
heapless = { version = "0.9", features = ["serde"] }
4039
hex-literal = "0.4.1"
4140
nb = "1"
4241
postcard.workspace = true

src/key.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::ptr::write_volatile;
22
use core::sync::atomic;
33

4-
use heapless::Vec;
54
use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize};
65
use zeroize::Zeroize;
76

@@ -11,8 +10,8 @@ use crate::{
1110
Error,
1211
};
1312

14-
pub type Material = Vec<u8, { MAX_KEY_MATERIAL_LENGTH }>;
15-
pub type SerializedKeyBytes = Vec<u8, { MAX_SERIALIZED_KEY_LENGTH }>;
13+
pub type Material = Bytes<MAX_KEY_MATERIAL_LENGTH>;
14+
pub type SerializedKeyBytes = Bytes<MAX_SERIALIZED_KEY_LENGTH>;
1615

1716
// We don't implement serde to make sure nobody inadvertently still uses it
1817
// Should we use references here only?
@@ -138,7 +137,7 @@ impl Key {
138137
Ok(Key {
139138
flags,
140139
kind,
141-
material: Material::from_slice(material).map_err(|_| Error::InvalidSerializedKey)?,
140+
material: Material::try_from(material).map_err(|_| Error::InvalidSerializedKey)?,
142141
})
143142
}
144143
}

src/mechanisms/p384.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
key,
1515
service::MechanismImpl,
1616
store::keystore::Keystore,
17-
types::{KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization},
17+
types::{Bytes, KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization},
1818
Error,
1919
};
2020

@@ -44,9 +44,9 @@ fn load_public_key(keystore: &mut impl Keystore, key_id: &KeyId) -> Result<p384:
4444
p384::PublicKey::from_sec1_bytes(&compressed_public_key).map_err(|_| Error::InternalError)
4545
}
4646

47-
fn to_sec1_bytes(public_key: &p384::PublicKey) -> heapless::Vec<u8, { SCALAR_SIZE * 2 + 1 }> {
47+
fn to_sec1_bytes(public_key: &p384::PublicKey) -> Bytes<{ SCALAR_SIZE * 2 + 1 }> {
4848
let encoded_point: p384::EncodedPoint = public_key.into();
49-
encoded_point.as_bytes().try_into().unwrap()
49+
Bytes::try_from(encoded_point.as_bytes()).unwrap()
5050
}
5151

5252
impl MechanismImpl for P384 {

src/mechanisms/p521.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
key,
1515
service::MechanismImpl,
1616
store::keystore::Keystore,
17-
types::{KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization},
17+
types::{Bytes, KeyId, KeySerialization, SerializedKey, Signature, SignatureSerialization},
1818
Error,
1919
};
2020

@@ -44,9 +44,9 @@ fn load_public_key(keystore: &mut impl Keystore, key_id: &KeyId) -> Result<p521:
4444
p521::PublicKey::from_sec1_bytes(&compressed_public_key).map_err(|_| Error::InternalError)
4545
}
4646

47-
fn to_sec1_bytes(public_key: &p521::PublicKey) -> heapless::Vec<u8, { SCALAR_SIZE * 2 + 1 }> {
47+
fn to_sec1_bytes(public_key: &p521::PublicKey) -> Bytes<{ SCALAR_SIZE * 2 + 1 }> {
4848
let encoded_point: p521::EncodedPoint = public_key.into();
49-
encoded_point.as_bytes().try_into().unwrap()
49+
Bytes::try_from(encoded_point.as_bytes()).unwrap()
5050
}
5151

5252
impl MechanismImpl for P521 {

src/service/attest.rs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -479,62 +479,6 @@ impl Encodable for Name<'_> {
479479
}
480480
}
481481

482-
pub struct ParsedDatetime {
483-
year: u16,
484-
month: u8,
485-
day: u8,
486-
hour: u8,
487-
minute: u8,
488-
second: u8,
489-
}
490-
491-
impl ParsedDatetime {
492-
pub fn new(year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> Option<Self> {
493-
let valid = [
494-
year >= 2000,
495-
year <= 9999,
496-
month >= 1,
497-
month <= 12,
498-
day >= 1,
499-
day <= 31,
500-
hour <= 23,
501-
minute <= 59,
502-
second <= 59,
503-
]
504-
.iter()
505-
.all(|b| *b);
506-
507-
if valid {
508-
Some(Self {
509-
year,
510-
month,
511-
day,
512-
hour,
513-
minute,
514-
second,
515-
})
516-
} else {
517-
None
518-
}
519-
}
520-
521-
pub fn to_bytes(&self) -> [u8; 15] {
522-
let mut buffer: heapless::Vec<u8, 15> = Default::default();
523-
buffer.resize_default(15).unwrap();
524-
core::fmt::write(
525-
&mut buffer,
526-
format_args!(
527-
"{}{:02}{:02}{:02}{:02}{:02}Z",
528-
self.year, self.month, self.day, self.hour, self.minute, self.second
529-
),
530-
)
531-
.unwrap();
532-
let mut array = [0u8; 15];
533-
array.copy_from_slice(&buffer);
534-
array
535-
}
536-
}
537-
538482
#[derive(Clone, Copy, Eq, PartialEq)]
539483
/// Encoded as "YYYYMMDDHHMMSSZ", encoding takes care of truncating YYYY to YY if necessary.
540484
pub struct Datetime<'l>(&'l [u8]);

src/store/keystore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<S: Store> Keystore for ClientKeystore<S> {
113113
let key = key::Key {
114114
flags: info.flags,
115115
kind: info.kind,
116-
material: key::Material::from_slice(material).unwrap(),
116+
material: key::Material::try_from(material).unwrap(),
117117
};
118118

119119
let id = self.generate_key_id();
@@ -208,7 +208,7 @@ impl<S: Store> Keystore for ClientKeystore<S> {
208208
let key = key::Key {
209209
flags: Default::default(),
210210
kind,
211-
material: key::Material::from_slice(material).unwrap(),
211+
material: key::Material::try_from(material).unwrap(),
212212
};
213213

214214
let path = self.key_path(secrecy, id);

src/types.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
pub use generic_array::GenericArray;
22

3-
pub use heapless::{String, Vec};
4-
53
pub use crate::Bytes;
64

75
pub use littlefs2_core::{DirEntry, Metadata, Path, PathBuf, Result as LfsResult};

0 commit comments

Comments
 (0)