Skip to content

Commit

Permalink
Bump heapless
Browse files Browse the repository at this point in the history
  • Loading branch information
nickray committed Jun 10, 2021
1 parent c02ecc4 commit 817a9e7
Show file tree
Hide file tree
Showing 26 changed files with 104 additions and 104 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ cfg-if = "1.0"
embedded-hal = { version = "0.2.3", features = ["unproven"] }
flexiber = { git = "https://github.com/nickray/flexiber", branch = "main", features = ["derive", "heapless"] }
generic-array = "0.14.4"
heapless = { version = "0.6", features = ["serde"] }
heapless = { version = "0.7", features = ["serde"] }
hex-literal = "0.3.1"
nb = "1"
postcard = "0.6.0"
postcard = "0.7.0"
rand_core = "0.6"
serde = { version = "1.0", default-features = false }
zeroize = { version = "1.2", default-features = false, features = ["zeroize_derive"] }
Expand All @@ -39,12 +39,12 @@ sha-1 = { version = "0.9", default-features = false, optional = true }
sha2 = { version = "0.9", default-features = false }

# ours
cosey = "0.2.0"
cosey = "0.3"
delog = "0.1.0"
cbor-smol = "0.3.0"
heapless-bytes = { version = "0.2.0", features = ["cbor"] }
cbor-smol = "0.4"
heapless-bytes = { version = "0.3.0", features = ["cbor"] }
interchange = "0.2.1"
littlefs2 = "0.2.2"
littlefs2 = "0.3.1"
p256-cortex-m4 = { version = "0.1.0-alpha.5", features = ["prehash", "sec1-signatures"] }
serde-indexed = "0.1.0"

Expand All @@ -56,7 +56,7 @@ features = ["cose"]
[dev-dependencies]
# Testing
serial_test = { version = "*" }
entropy = "0.3.0"
entropy = "0.4.0"
# Somehow, this is causing a regression.
# rand_core = { version = "0.5", features = ["getrandom"] }

Expand Down
30 changes: 15 additions & 15 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub trait CertificateClient: PollClient {
fn write_certificate(&mut self, location: Location, der: &[u8])
-> ClientResult<'_, reply::WriteCertificate, Self>
{
let der = Message::try_from_slice(der).map_err(|_| ClientError::DataTooLarge)?;
let der = Message::from_slice(der).map_err(|_| ClientError::DataTooLarge)?;
let r = self.request(request::WriteCertificate { location, der })?;
r.client.syscall();
Ok(r)
Expand Down Expand Up @@ -306,10 +306,10 @@ pub trait CryptoClient: PollClient {
)
-> ClientResult<'c, reply::Decrypt, Self>
{
let message = Message::try_from_slice(message).map_err(|_| ClientError::DataTooLarge)?;
let associated_data = Message::try_from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let nonce = ShortData::try_from_slice(nonce).map_err(|_| ClientError::DataTooLarge)?;
let tag = ShortData::try_from_slice(tag).map_err(|_| ClientError::DataTooLarge)?;
let message = Message::from_slice(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)?;
let r = self.request(request::Decrypt { mechanism, key, message, associated_data, nonce, tag })?;
r.client.syscall();
Ok(r)
Expand Down Expand Up @@ -352,7 +352,7 @@ pub trait CryptoClient: PollClient {
format: KeySerialization, attributes: StorageAttributes)
-> ClientResult<'c, reply::DeserializeKey, Self>
{
let serialized_key = Message::try_from_slice(serialized_key).map_err(|_| ClientError::DataTooLarge)?;
let serialized_key = Message::from_slice(serialized_key).map_err(|_| ClientError::DataTooLarge)?;
let r = self.request(request::DeserializeKey {
mechanism, serialized_key, format, attributes
} )?;
Expand All @@ -364,8 +364,8 @@ pub trait CryptoClient: PollClient {
message: &[u8], associated_data: &[u8], nonce: Option<ShortData>)
-> ClientResult<'c, reply::Encrypt, Self>
{
let message = Message::try_from_slice(message).map_err(|_| ClientError::DataTooLarge)?;
let associated_data = ShortData::try_from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let message = Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?;
let associated_data = ShortData::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let r = self.request(request::Encrypt { mechanism, key, message, associated_data, nonce })?;
r.client.syscall();
Ok(r)
Expand Down Expand Up @@ -444,7 +444,7 @@ pub trait CryptoClient: PollClient {
let r = self.request(request::Sign {
key,
mechanism,
message: Bytes::try_from_slice(data).map_err(|_| ClientError::DataTooLarge)?,
message: Bytes::from_slice(data).map_err(|_| ClientError::DataTooLarge)?,
format,
})?;
r.client.syscall();
Expand All @@ -464,8 +464,8 @@ pub trait CryptoClient: PollClient {
let r = self.request(request::Verify {
mechanism,
key,
message: Message::try_from_slice(&message).expect("all good"),
signature: Signature::try_from_slice(&signature).expect("all good"),
message: Message::from_slice(&message).expect("all good"),
signature: Signature::from_slice(&signature).expect("all good"),
format,
})?;
r.client.syscall();
Expand All @@ -484,7 +484,7 @@ pub trait CryptoClient: PollClient {
{
let r = self.request(request::UnsafeInjectKey {
mechanism,
raw_key: ShortData::try_from_slice(raw_key).unwrap(),
raw_key: ShortData::from_slice(raw_key).unwrap(),
attributes: StorageAttributes::new().set_persistence(persistence),
})?;
r.client.syscall();
Expand All @@ -495,7 +495,7 @@ pub trait CryptoClient: PollClient {
-> ClientResult<'_, reply::UnsafeInjectSharedKey, Self>
{
let r = self.request(request::UnsafeInjectSharedKey {
raw_key: ShortData::try_from_slice(raw_key).unwrap(),
raw_key: ShortData::from_slice(raw_key).unwrap(),
location,
})?;
r.client.syscall();
Expand All @@ -506,7 +506,7 @@ pub trait CryptoClient: PollClient {
associated_data: &[u8], attributes: StorageAttributes)
-> ClientResult<'c, reply::UnwrapKey, Self>
{
let associated_data = Message::try_from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let associated_data = Message::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let r = self.request(request::UnwrapKey {
mechanism,
wrapping_key,
Expand All @@ -522,7 +522,7 @@ pub trait CryptoClient: PollClient {
associated_data: &[u8])
-> ClientResult<'_, reply::WrapKey, Self>
{
let associated_data = Message::try_from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let associated_data = Message::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let r = self.request(request::WrapKey { mechanism, wrapping_key, key, associated_data })?;
r.client.syscall();
Ok(r)
Expand Down
14 changes: 7 additions & 7 deletions src/client/mechanisms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait Chacha8Poly1305: CryptoClient {
-> ClientResult<'c, reply::Encrypt, Self>
{
self.encrypt(Mechanism::Chacha8Poly1305, key, message, associated_data,
nonce.and_then(|nonce| ShortData::try_from_slice(nonce).ok()))
nonce.and_then(|nonce| ShortData::from_slice(nonce).ok()))
}

fn generate_chacha8poly1305_key(&mut self, persistence: Location)
Expand All @@ -49,7 +49,7 @@ pub trait Chacha8Poly1305: CryptoClient {
-> ClientResult<'c, reply::UnwrapKey, Self>
{
self.unwrap_key(Mechanism::Chacha8Poly1305, wrapping_key,
Message::try_from_slice(wrapped_key).map_err(|_| ClientError::DataTooLarge)?,
Message::from_slice(wrapped_key).map_err(|_| ClientError::DataTooLarge)?,
associated_data,
StorageAttributes::new().set_persistence(location))
}
Expand All @@ -71,7 +71,7 @@ pub trait HmacBlake2s: CryptoClient {
{
self.derive_key(
Mechanism::HmacBlake2s, base_key,
Some(MediumData::try_from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence))
}

Expand All @@ -92,7 +92,7 @@ pub trait HmacSha1: CryptoClient {
{
self.derive_key(
Mechanism::HmacSha1, base_key,
Some(MediumData::try_from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence))
}

Expand All @@ -113,7 +113,7 @@ pub trait HmacSha256: CryptoClient {
{
self.derive_key(
Mechanism::HmacSha256, base_key,
Some(MediumData::try_from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence))
}

Expand All @@ -134,7 +134,7 @@ pub trait HmacSha512: CryptoClient {
{
self.derive_key(
Mechanism::HmacSha512, base_key,
Some(MediumData::try_from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence))
}

Expand Down Expand Up @@ -258,7 +258,7 @@ pub trait Sha256: CryptoClient {
fn hash_sha256<'c>(&'c mut self, message: &[u8])
-> ClientResult<'c, reply::Hash, Self>
{
self.hash(Mechanism::Sha256, Message::try_from_slice(message).map_err(|_| ClientError::DataTooLarge)?)
self.hash(Mechanism::Sha256, Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?)
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#![allow(non_camel_case_types)]
#![allow(clippy::upper_case_acronyms)]

use heapless::consts;
use littlefs2::consts;

// TODO: this needs to be overridable.
// Should we use the "config crate that can have a replacement patched in" idea?

pub type MAX_APPLICATION_NAME_LENGTH = consts::U256;
pub type MAX_LONG_DATA_LENGTH = consts::U1024;
pub type MAX_MESSAGE_LENGTH = consts::U1024;
pub const MAX_LONG_DATA_LENGTH: usize = 1024;
pub const MAX_MESSAGE_LENGTH: usize = 1024;
pub type MAX_OBJECT_HANDLES = consts::U16;
pub type MAX_LABEL_LENGTH = consts::U256;
pub type MAX_MEDIUM_DATA_LENGTH = consts::U256;
pub const MAX_MEDIUM_DATA_LENGTH: usize = 256;
pub type MAX_PATH_LENGTH = consts::U256;
pub type MAX_KEY_MATERIAL_LENGTH = consts::U128;
pub type MAX_SERIALIZED_KEY_LENGTH = <MAX_KEY_MATERIAL_LENGTH as core::ops::Add<consts::U4>>::Output;
pub const MAX_KEY_MATERIAL_LENGTH: usize = 128;
// must be above + 4
pub const MAX_SERIALIZED_KEY_LENGTH: usize = 132;
pub type MAX_SERVICE_CLIENTS = consts::U5;
pub type MAX_SHORT_DATA_LENGTH = consts::U128;
pub type MAX_SIGNATURE_LENGTH = consts::U72;
pub type MAX_USER_ATTRIBUTE_LENGTH = consts::U256;
pub const MAX_SHORT_DATA_LENGTH: usize = 128;
pub const MAX_SIGNATURE_LENGTH: usize = 72;
pub const MAX_USER_ATTRIBUTE_LENGTH: usize = 256;

pub const USER_ATTRIBUTE_NUMBER: u8 = 37;

7 changes: 4 additions & 3 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use heapless::Vec;
use serde::{Deserialize, Serialize};
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
use zeroize::Zeroize;
Expand All @@ -8,8 +9,8 @@ use crate::{
config::{MAX_KEY_MATERIAL_LENGTH, MAX_SERIALIZED_KEY_LENGTH},
};

pub type Material = Bytes<MAX_KEY_MATERIAL_LENGTH>;
pub type SerializedKeyBytes = Bytes<MAX_SERIALIZED_KEY_LENGTH>;
pub type Material = Vec<u8, {MAX_KEY_MATERIAL_LENGTH}>;
pub type SerializedKeyBytes = Vec<u8, {MAX_SERIALIZED_KEY_LENGTH}>;

// We don't implement serde to make sure nobody inadvertently still uses it
// Should we use references here only?
Expand Down Expand Up @@ -112,7 +113,7 @@ impl Key {
Ok(Key {
flags,
kind,
material: Material::try_from_slice(material).map_err(|_| Error::InvalidSerializedKey)?,
material: Material::from_slice(material).map_err(|_| Error::InvalidSerializedKey)?,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ pub use platform::Platform;
pub use service::Service;

pub use cbor_smol::{cbor_serialize, cbor_serialize_bytes, cbor_deserialize};
pub use heapless_bytes::{ArrayLength, Bytes, consts};
pub use heapless_bytes::Bytes;
pub use postcard::{from_bytes as postcard_deserialize, to_slice as postcard_serialize};

pub fn postcard_serialize_bytes<N: ArrayLength<u8>, T: serde::Serialize>(
pub fn postcard_serialize_bytes<T: serde::Serialize, const N: usize>(
object: &T,
) -> postcard::Result<Bytes<N>> {
let vec = postcard::to_vec(object)?;
Expand Down
12 changes: 6 additions & 6 deletions src/mechanisms/aes256cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Encrypt for super::Aes256Cbc

let symmetric_key: [u8; 32] = keystore
.load_key(key::Secrecy::Secret, None, &key_id)?
.material.as_ref().try_into()
.material.as_slice().try_into()
.map_err(|_| Error::InternalError)?;

let zero_iv = [0u8; 16];
Expand All @@ -47,7 +47,7 @@ impl Encrypt for super::Aes256Cbc
// The padding space should be big enough for padding, otherwise method will return Err(BlockModeError).
let ciphertext = cipher.encrypt(&mut buffer, l).unwrap();

let ciphertext = Message::try_from_slice(&ciphertext).unwrap();
let ciphertext = Message::from_slice(&ciphertext).unwrap();
Ok(reply::Encrypt { ciphertext, nonce: ShortData::new(), tag: ShortData::new() })
}
}
Expand All @@ -64,9 +64,9 @@ impl WrapKey for super::Aes256Cbc

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

let message: Message = crate::Bytes::try_from_slice(keystore
let message = Message::from_slice(keystore
.load_key(key::Secrecy::Secret, None, &request.key)?
.material.as_ref()).map_err(|_| Error::InternalError)?;
.material.as_slice()).map_err(|_| Error::InternalError)?;

let encryption_request = request::Encrypt {
mechanism: Mechanism::Aes256Cbc,
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Decrypt for super::Aes256Cbc
let key_id = request.key;
let symmetric_key: [u8; 32] = keystore
.load_key(key::Secrecy::Secret, None, &key_id)?
.material.as_ref()
.material.as_slice()
.try_into()
.map_err(|_| Error::InternalError)?;

Expand All @@ -121,7 +121,7 @@ impl Decrypt for super::Aes256Cbc
// hprintln!("symmetric key: {:?}", &symmetric_key).ok();
let plaintext = cipher.decrypt(&mut buffer).unwrap();
// hprintln!("decrypted: {:?}", &plaintext).ok();
let plaintext = Message::try_from_slice(&plaintext).unwrap();
let plaintext = Message::from_slice(&plaintext).unwrap();

Ok(reply::Decrypt { plaintext: Some(plaintext) })
}
Expand Down
16 changes: 8 additions & 8 deletions src/mechanisms/chacha8poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Decrypt for super::Chacha8Poly1305
let serialized_material = keystore
.load_key(key::Secrecy::Secret, Some(key::Kind::Symmetric32Nonce(12)), &request.key)?
.material;
let serialized = serialized_material.as_ref();
let serialized = serialized_material.as_slice();

// if serialized.len() != 44 {
// return Error::InternalError;
Expand Down Expand Up @@ -114,7 +114,7 @@ impl Encrypt for super::Chacha8Poly1305
let mut serialized_material = keystore
.load_key(secrecy, Some(key_kind), key_id)?
.material;
let serialized = serialized_material.as_mut();
let serialized: &mut [u8] = serialized_material.as_mut();

assert!(serialized.len() == 44);

Expand Down Expand Up @@ -154,10 +154,10 @@ impl Encrypt for super::Chacha8Poly1305
&mut ciphertext,
).unwrap().as_slice().try_into().unwrap();

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

// let ciphertext = Message::try_from_slice(&ciphertext).unwrap();
// let ciphertext = Message::from_slice(&ciphertext).unwrap();
Ok(reply::Encrypt { ciphertext, nonce, tag })
}
}
Expand All @@ -175,7 +175,7 @@ impl WrapKey for super::Chacha8Poly1305
let serialized_key = keystore
.load_key(key::Secrecy::Secret, None, &request.key)?;

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

let encryption_request = request::Encrypt {
mechanism: Mechanism::Chacha8Poly1305,
Expand Down Expand Up @@ -301,7 +301,7 @@ impl UnwrapKey for super::Chacha8Poly1305
// // Returns an error if buffer length is not multiple of block size and
// // if after decoding message has malformed padding.
// let plaintext = cipher.decrypt(&mut buffer).unwrap();
// let plaintext = Message::try_from_slice(&plaintext).unwrap();
// let plaintext = Message::from_slice(&plaintext).unwrap();

// Ok(reply::Decrypt { plaintext: Ok(plaintext) })
// }
Expand Down Expand Up @@ -350,7 +350,7 @@ impl UnwrapKey for super::Chacha8Poly1305
// // // The padding space should be big enough for padding, otherwise method will return Err(BlockModeError).
// // let ciphertext = cipher.encrypt(&mut buffer, l).unwrap();

// // let ciphertext = Message::try_from_slice(&ciphertext).unwrap();
// // let ciphertext = Message::from_slice(&ciphertext).unwrap();
// Ok(reply::Encrypt { ciphertext })
// }
// }
Expand Down
Loading

0 comments on commit 817a9e7

Please sign in to comment.