Skip to content

Commit 7ca8f9f

Browse files
Update to heapless 0.9
1 parent 55f7db0 commit 7ca8f9f

File tree

18 files changed

+121
-70
lines changed

18 files changed

+121
-70
lines changed

Cargo.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,18 @@ trussed-hpke = { version = "0.2.0", optional = true }
5454
trussed-manage = { version = "0.2.1", optional = true }
5555
trussed-wrap-key-to-file = { version = "0.2.0", optional = true }
5656
trussed-fs-info = { version = "0.2.0", optional = true }
57+
heapless = { version = "0.9.1", optional = true }
58+
heapless-bytes = { version = "0.5.0", optional = true }
5759

5860
[dev-dependencies]
5961
hex-literal = "0.4.0"
6062
hmac = "0.12.0"
61-
trussed = { version = "0.1.0", default-features = false, features = ["aes256-cbc", "hmac-sha256", "virt", "x255"] }
63+
trussed = { version = "0.1.0", default-features = false, features = ["aes256-cbc", "crypto-client", "filesystem-client", "hmac-sha256", "virt", "x255"] }
6264

6365
[features]
64-
default = []
66+
default = ["chunked"]
6567

66-
chunked = ["trussed-chunked", "chacha20poly1305/stream"]
68+
chunked = ["trussed-chunked", "chacha20poly1305/stream", "dep:heapless", "dep:heapless-bytes"]
6769
hkdf = ["trussed-hkdf", "dep:hkdf", "dep:sha2"]
6870
hpke = ["trussed-hpke", "dep:hkdf", "dep:sha2", "dep:hex-literal", "dep:aead", "dep:chacha20poly1305"]
6971
manage = ["trussed-manage"]
@@ -83,8 +85,12 @@ log-warn = []
8385
log-error = []
8486

8587
[patch.crates-io]
86-
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "43ed1efcb19dc9c8bee45d4a1d3ad7dee2bba5ae" }
87-
trussed-core = { git = "https://github.com/trussed-dev/trussed.git", rev = "43ed1efcb19dc9c8bee45d4a1d3ad7dee2bba5ae"}
88+
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "1e7b09a983dc8ae64a7ad8401ce541a9a77e5939" }
89+
trussed-core = { git = "https://github.com/trussed-dev/trussed.git", rev = "1e7b09a983dc8ae64a7ad8401ce541a9a77e5939"}
90+
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "e9d3a1ca98f80e92cd20ee9b94707067810b9036" }
91+
littlefs2-core = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "e9d3a1ca98f80e92cd20ee9b94707067810b9036" }
92+
littlefs2-sys = { git = "https://github.com/trussed-dev/littlefs2-sys", rev = "v0.3.1-nitrokey.1" }
93+
8894
trussed-chunked = { path = "extensions/chunked" }
8995
trussed-hkdf = { path = "extensions/hkdf" }
9096
trussed-hpke = { path = "extensions/hpke" }

extensions/chunked/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ pub mod utils;
1010
use serde::{Deserialize, Serialize};
1111
use serde_byte_array::ByteArray;
1212
use trussed_core::{
13-
client::FilesystemClient,
1413
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
1514
types::{KeyId, Location, Message, PathBuf, UserAttribute},
15+
FilesystemClient,
1616
};
1717

1818
pub const CHACHA8_STREAM_NONCE_LEN: usize = 8;
@@ -57,8 +57,8 @@ pub mod request {
5757
use super::*;
5858
use serde::{Deserialize, Serialize};
5959
use serde_byte_array::ByteArray;
60-
use trussed_core::error::Error;
6160
use trussed_core::types::{KeyId, Location, Message, PathBuf, UserAttribute};
61+
use trussed_core::Error;
6262

6363
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
6464
pub struct ReadChunk {}
@@ -263,8 +263,8 @@ pub mod request {
263263
pub mod reply {
264264
use super::*;
265265
use serde::{Deserialize, Serialize};
266-
use trussed_core::error::Error;
267266
use trussed_core::types::Message;
267+
use trussed_core::Error;
268268

269269
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
270270
pub struct ReadChunk {

extensions/chunked/src/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
use serde_byte_array::ByteArray;
55
use trussed_core::{
6-
error::Error,
76
syscall, try_syscall,
87
types::{KeyId, Location, Message, PathBuf, UserAttribute},
8+
Error,
99
};
1010

1111
use crate::{ChunkedClient, CHACHA8_STREAM_NONCE_LEN};
@@ -27,7 +27,7 @@ pub fn write_all(
2727
user_attribute: Option<UserAttribute>,
2828
encryption: Option<EncryptionData>,
2929
) -> Result<(), Error> {
30-
if let (Ok(msg), None) = (Message::from_slice(data), encryption) {
30+
if let (Ok(msg), None) = (Message::try_from(data), encryption) {
3131
// Fast path for small files
3232
try_syscall!(client.write_file(location, path, msg, user_attribute))?;
3333
Ok(())
@@ -63,7 +63,7 @@ fn write_chunked_inner(
6363
let msg = Message::new();
6464
let chunk_size = msg.capacity();
6565
let chunks = data.chunks(chunk_size).map(|chunk| {
66-
Message::from_slice(chunk).expect("Iteration over chunks yields maximum of chunk_size")
66+
Message::try_from(chunk).expect("Iteration over chunks yields maximum of chunk_size")
6767
});
6868
if let Some(encryption_data) = encryption {
6969
try_syscall!(client.start_encrypted_chunked_write(

extensions/fs-info/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
use serde::{Deserialize, Serialize};
99
use trussed_core::{
10-
error::Error,
1110
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
1211
types::Location,
12+
Error,
1313
};
1414

1515
pub struct FsInfoExtension;

extensions/hkdf/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use serde::{Deserialize, Serialize};
99
use trussed_core::{
1010
config::MAX_MEDIUM_DATA_LENGTH,
11-
error::Error,
1211
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
1312
types::{Bytes, KeyId, Location, Message},
13+
Error,
1414
};
1515

1616
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]

extensions/hpke/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use serde::{Deserialize, Serialize};
1212
use serde_byte_array::ByteArray;
1313

14-
use trussed_core::error::Error;
1514
use trussed_core::serde_extensions::{Extension, ExtensionClient, ExtensionResult};
1615
use trussed_core::types::{KeyId, Location, Message, PathBuf, ShortData};
16+
use trussed_core::Error;
1717

1818
#[derive(Deserialize, Serialize)]
1919
pub enum HpkeRequest {

extensions/manage/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use littlefs2_core::{path, Path, PathBuf};
99
use serde::{Deserialize, Serialize};
1010
use trussed_core::{
11-
error::Error,
1211
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
12+
Error,
1313
};
1414

1515
pub struct ManageExtension;

extensions/wrap-key-to-file/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
use serde::{Deserialize, Serialize};
99
use trussed_core::{
10-
client::ClientError,
1110
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
1211
types::{Bytes, KeyId, Location, Mechanism, PathBuf},
12+
ClientError,
1313
};
1414

1515
#[derive(Debug, Default)]
@@ -25,8 +25,8 @@ pub enum WrapKeyToFileRequest {
2525
pub mod request {
2626
use super::*;
2727
use serde::{Deserialize, Serialize};
28-
use trussed_core::error::Error;
2928
use trussed_core::types::{KeyId, Location, Mechanism, Message, PathBuf};
29+
use trussed_core::Error;
3030

3131
#[derive(Debug, Deserialize, Serialize)]
3232
pub struct WrapKeyToFile {
@@ -90,7 +90,7 @@ pub enum WrapKeyToFileReply {
9090

9191
pub mod reply {
9292
use serde::{Deserialize, Serialize};
93-
use trussed_core::{error::Error, types::KeyId};
93+
use trussed_core::{types::KeyId, Error};
9494

9595
use super::*;
9696

@@ -156,7 +156,7 @@ pub trait WrapKeyToFileClient: ExtensionClient<WrapKeyToFileExtension> {
156156
associated_data: &[u8],
157157
) -> WrapKeyToFileResult<'_, reply::WrapKeyToFile, Self> {
158158
let associated_data =
159-
Bytes::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
159+
Bytes::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
160160
self.extension(request::WrapKeyToFile {
161161
mechanism,
162162
wrapping_key,
@@ -180,7 +180,7 @@ pub trait WrapKeyToFileClient: ExtensionClient<WrapKeyToFileExtension> {
180180
associated_data: &[u8],
181181
) -> WrapKeyToFileResult<'_, reply::UnwrapKeyFromFile, Self> {
182182
let associated_data =
183-
Bytes::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
183+
Bytes::try_from(associated_data).map_err(|_| ClientError::DataTooLarge)?;
184184
self.extension(request::UnwrapKeyFromFile {
185185
mechanism,
186186
key,

src/chunked/mod.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod store;
55
use store::OpenSeekFrom;
66

77
use chacha20poly1305::{
8+
aead,
89
aead::stream::{DecryptorLE31, EncryptorLE31, Nonce as StreamNonce, StreamLE31},
910
ChaCha8Poly1305, KeyInit,
1011
};
@@ -27,6 +28,38 @@ use crate::StagingContext;
2728
const POLY1305_TAG_LEN: usize = 16;
2829
const CHACHA8_KEY_LEN: usize = 32;
2930

31+
struct HeaplessBuffer<'a, LenT: heapless::LenType>(&'a mut heapless_bytes::BytesView<LenT>);
32+
33+
impl<'a, LenT: heapless::LenType, S: heapless_bytes::BytesStorage + ?Sized>
34+
From<&'a mut heapless_bytes::BytesInner<LenT, S>> for HeaplessBuffer<'a, LenT>
35+
{
36+
fn from(value: &'a mut heapless_bytes::BytesInner<LenT, S>) -> Self {
37+
Self(value.as_mut_view())
38+
}
39+
}
40+
41+
impl<'a, LenT: heapless::LenType> AsMut<[u8]> for HeaplessBuffer<'a, LenT> {
42+
fn as_mut(&mut self) -> &mut [u8] {
43+
&mut self.0
44+
}
45+
}
46+
47+
impl<'a, LenT: heapless::LenType> AsRef<[u8]> for HeaplessBuffer<'a, LenT> {
48+
fn as_ref(&self) -> &[u8] {
49+
&self.0
50+
}
51+
}
52+
53+
impl<'a, LenT: heapless::LenType> aead::Buffer for HeaplessBuffer<'a, LenT> {
54+
fn extend_from_slice(&mut self, other: &[u8]) -> aead::Result<()> {
55+
self.0.extend_from_slice(other).map_err(|_| aead::Error)
56+
}
57+
58+
fn truncate(&mut self, len: usize) {
59+
self.0.truncate(len);
60+
}
61+
}
62+
3063
#[derive(Debug)]
3164
pub struct ChunkedReadState {
3265
pub path: PathBuf,
@@ -209,7 +242,7 @@ impl ExtensionImpl<ChunkedExtension> for super::StagingBackend {
209242
let nonce: Bytes<CHACHA8_STREAM_NONCE_LEN> =
210243
filestore.read(&request.path, request.location)?;
211244
let nonce: &StreamNonce<ChaCha8Poly1305, StreamLE31<ChaCha8Poly1305>> =
212-
(&**nonce).into();
245+
(&*nonce).into();
213246
let aead = ChaCha8Poly1305::new((&*key.material).into());
214247
let decryptor = DecryptorLE31::<ChaCha8Poly1305>::from_aead(aead, nonce);
215248
backend_ctx.chunked_io_state =
@@ -263,10 +296,13 @@ fn write_chunk(
263296
}
264297
Some(ChunkedIoState::EncryptedWrite(ref mut write_state)) => {
265298
let mut data =
266-
Bytes::<{ MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN }>::from_slice(data).unwrap();
299+
Bytes::<{ MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN }>::try_from(&**data).unwrap();
267300
write_state
268301
.encryptor
269-
.encrypt_next_in_place(write_state.path.as_ref().as_bytes(), &mut *data)
302+
.encrypt_next_in_place(
303+
write_state.path.as_ref().as_bytes(),
304+
&mut HeaplessBuffer::from(&mut data),
305+
)
270306
.map_err(|_err| {
271307
error!("Failed to encrypt {:?}", _err);
272308
Error::AeadError
@@ -303,10 +339,13 @@ fn write_last_chunk(
303339
}
304340
Some(ChunkedIoState::EncryptedWrite(write_state)) => {
305341
let mut data =
306-
Bytes::<{ MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN }>::from_slice(data).unwrap();
342+
Bytes::<{ MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN }>::try_from(&**data).unwrap();
307343
write_state
308344
.encryptor
309-
.encrypt_last_in_place(&[write_state.location as u8], &mut *data)
345+
.encrypt_last_in_place(
346+
&[write_state.location as u8],
347+
&mut HeaplessBuffer::from(&mut data),
348+
)
310349
.map_err(|_err| {
311350
error!("Failed to encrypt {:?}", _err);
312351
Error::AeadError
@@ -354,12 +393,15 @@ fn read_encrypted_chunk(
354393

355394
read_state
356395
.decryptor
357-
.decrypt_last_in_place(&[read_state.location as u8], &mut *data)
396+
.decrypt_last_in_place(
397+
&[read_state.location as u8],
398+
&mut HeaplessBuffer::from(&mut data),
399+
)
358400
.map_err(|_err| {
359401
error!("Failed to decrypt {:?}", _err);
360402
Error::AeadError
361403
})?;
362-
let data = Bytes::from_slice(&data).expect("decryptor removes the tag");
404+
let data = Bytes::try_from(&*data).expect("decryptor removes the tag");
363405
Ok(reply::ReadChunk {
364406
data,
365407
len: chunked_decrypted_len(len)?,
@@ -368,12 +410,15 @@ fn read_encrypted_chunk(
368410
} else {
369411
read_state
370412
.decryptor
371-
.decrypt_next_in_place(read_state.path.as_ref().as_bytes(), &mut *data)
413+
.decrypt_next_in_place(
414+
read_state.path.as_ref().as_bytes(),
415+
&mut HeaplessBuffer::from(&mut data),
416+
)
372417
.map_err(|_err| {
373418
error!("Failed to decrypt {:?}", _err);
374419
Error::AeadError
375420
})?;
376-
let data = Bytes::from_slice(&data).expect("decryptor removes the tag");
421+
let data = Bytes::try_from(&*data).expect("decryptor removes the tag");
377422
Ok(reply::ReadChunk {
378423
data,
379424
len: chunked_decrypted_len(len)?,

src/chunked/store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn fs_read_chunk<const N: usize>(
3939
if length > contents.capacity() {
4040
return Err(Error::FilesystemReadFailure);
4141
}
42-
contents.resize_default(length).unwrap();
42+
contents.resize_zero(length).unwrap();
4343
let file_len = fs
4444
.open_file_and_then(path, &mut |file| {
4545
file.seek(pos.into())?;

0 commit comments

Comments
 (0)