trussed-staging 0.5.0-rc.1

Work in progress trussed features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: Apache-2.0 or MIT

mod store;
use store::OpenSeekFrom;

use chacha20poly1305::{
    aead,
    aead::stream::{DecryptorLE31, EncryptorLE31, Nonce as StreamNonce, StreamLE31},
    ChaCha8Poly1305, KeyInit,
};
use littlefs2_core::{Path, PathBuf};
use rand_core::RngCore;
use trussed::{
    config::MAX_MESSAGE_LENGTH,
    key::{Kind, Secrecy},
    serde_extensions::ExtensionImpl,
    service::ServiceResources,
    store::{Filestore, Keystore, Store},
    types::CoreContext,
};
use trussed_chunked::{
    reply, ChunkedExtension, ChunkedReply, ChunkedRequest, CHACHA8_STREAM_NONCE_LEN,
};
use trussed_core::{
    types::{Bytes, Location, Message},
    Error,
};

use crate::StagingContext;

const POLY1305_TAG_LEN: usize = 16;
const CHACHA8_KEY_LEN: usize = 32;

struct HeaplessBuffer<'a, LenT: heapless::LenType>(&'a mut heapless_bytes::BytesView<LenT>);

impl<'a, LenT: heapless::LenType, S: heapless_bytes::BytesStorage + ?Sized>
    From<&'a mut heapless_bytes::BytesInner<LenT, S>> for HeaplessBuffer<'a, LenT>
{
    fn from(value: &'a mut heapless_bytes::BytesInner<LenT, S>) -> Self {
        Self(value.as_mut_view())
    }
}

impl<'a, LenT: heapless::LenType> AsMut<[u8]> for HeaplessBuffer<'a, LenT> {
    fn as_mut(&mut self) -> &mut [u8] {
        self.0
    }
}

impl<'a, LenT: heapless::LenType> AsRef<[u8]> for HeaplessBuffer<'a, LenT> {
    fn as_ref(&self) -> &[u8] {
        self.0
    }
}

impl<'a, LenT: heapless::LenType> aead::Buffer for HeaplessBuffer<'a, LenT> {
    fn extend_from_slice(&mut self, other: &[u8]) -> aead::Result<()> {
        self.0.extend_from_slice(other).map_err(|_| aead::Error)
    }

    fn truncate(&mut self, len: usize) {
        self.0.truncate(len);
    }
}

#[derive(Debug)]
pub struct ChunkedReadState {
    pub path: PathBuf,
    pub location: Location,
    pub offset: usize,
}

#[derive(Debug)]
pub struct ChunkedWriteState {
    pub path: PathBuf,
    pub location: Location,
}

pub struct EncryptedChunkedReadState {
    pub path: PathBuf,
    pub location: Location,
    pub offset: usize,
    pub decryptor: DecryptorLE31<ChaCha8Poly1305>,
}

pub struct EncryptedChunkedWriteState {
    pub path: PathBuf,
    pub location: Location,
    pub encryptor: EncryptorLE31<ChaCha8Poly1305>,
}

#[non_exhaustive]
pub enum ChunkedIoState {
    Read(ChunkedReadState),
    Write(ChunkedWriteState),
    EncryptedRead(EncryptedChunkedReadState),
    EncryptedWrite(EncryptedChunkedWriteState),
}

impl ExtensionImpl<ChunkedExtension> for super::StagingBackend {
    fn extension_request<P: trussed::Platform>(
        &mut self,
        core_ctx: &mut CoreContext,
        backend_ctx: &mut Self::Context,
        request: &ChunkedRequest,
        resources: &mut ServiceResources<P>,
    ) -> Result<ChunkedReply, Error> {
        let rng = &mut resources.rng()?;
        let keystore = &mut resources.keystore(core_ctx.path.clone())?;
        let filestore = &mut resources.filestore(core_ctx.path.clone());
        let client_id = &core_ctx.path;
        let store = resources.platform_mut().store();
        match request {
            ChunkedRequest::ReadChunk(_) => {
                let read_state = match &mut backend_ctx.chunked_io_state {
                    Some(ChunkedIoState::Read(read_state)) => read_state,
                    Some(ChunkedIoState::EncryptedRead(_)) => {
                        return read_encrypted_chunk(&store, client_id, backend_ctx)
                    }
                    _ => return Err(Error::MechanismNotAvailable),
                };
                let (data, len) = store::filestore_read_chunk(
                    &store,
                    client_id,
                    &read_state.path,
                    read_state.location,
                    OpenSeekFrom::Start(read_state.offset as u32),
                )?;

                read_state.offset += data.len();

                Ok(reply::ReadChunk { data, len }.into())
            }
            ChunkedRequest::StartChunkedRead(request) => {
                clear_chunked_state(&store, client_id, backend_ctx)?;
                let (data, len) = store::filestore_read_chunk(
                    &store,
                    client_id,
                    &request.path,
                    request.location,
                    OpenSeekFrom::Start(0),
                )?;
                backend_ctx.chunked_io_state = Some(ChunkedIoState::Read(ChunkedReadState {
                    path: request.path.clone(),
                    location: request.location,
                    offset: data.len(),
                }));
                Ok(reply::StartChunkedRead { data, len }.into())
            }
            ChunkedRequest::WriteChunk(request) => {
                let is_last = !request.data.is_full();
                if is_last {
                    write_last_chunk(&store, client_id, backend_ctx, &request.data)?;
                } else {
                    write_chunk(&store, client_id, backend_ctx, &request.data)?;
                }
                Ok(reply::WriteChunk {}.into())
            }
            ChunkedRequest::AbortChunkedWrite(_request) => {
                let Some(ChunkedIoState::Write(ref write_state)) = backend_ctx.chunked_io_state
                else {
                    return Ok(reply::AbortChunkedWrite { aborted: false }.into());
                };
                let aborted = store::abort_chunked_write(
                    &store,
                    client_id,
                    &write_state.path,
                    write_state.location,
                );
                Ok(reply::AbortChunkedWrite { aborted }.into())
            }
            ChunkedRequest::StartChunkedWrite(request) => {
                backend_ctx.chunked_io_state = Some(ChunkedIoState::Write(ChunkedWriteState {
                    path: request.path.clone(),
                    location: request.location,
                }));
                store::start_chunked_write(
                    &store,
                    client_id,
                    &request.path,
                    request.location,
                    &[],
                )?;
                Ok(reply::StartChunkedWrite {}.into())
            }
            ChunkedRequest::PartialReadFile(request) => {
                let (data, file_length) = store::partial_read_file(
                    &store,
                    client_id,
                    &request.path,
                    request.location,
                    request.offset,
                    request.length,
                )?;
                Ok(reply::PartialReadFile { data, file_length }.into())
            }
            ChunkedRequest::AppendFile(request) => {
                let file_length = store::append_file(
                    &store,
                    client_id,
                    &request.path,
                    request.location,
                    &request.data,
                )?;
                Ok(reply::AppendFile { file_length }.into())
            }
            ChunkedRequest::StartEncryptedChunkedWrite(request) => {
                clear_chunked_state(&store, client_id, backend_ctx)?;
                let key = keystore.load_key(
                    Secrecy::Secret,
                    Some(Kind::Symmetric(CHACHA8_KEY_LEN)),
                    &request.key,
                )?;
                let nonce = request.nonce.map(|n| *n).unwrap_or_else(|| {
                    let mut nonce = [0; CHACHA8_STREAM_NONCE_LEN];
                    rng.fill_bytes(&mut nonce);
                    nonce
                });
                let nonce: &StreamNonce<ChaCha8Poly1305, StreamLE31<ChaCha8Poly1305>> =
                    (&nonce).into();
                let aead = ChaCha8Poly1305::new((&*key.material).into());
                let encryptor = EncryptorLE31::<ChaCha8Poly1305>::from_aead(aead, nonce);
                store::start_chunked_write(
                    &store,
                    client_id,
                    &request.path,
                    request.location,
                    nonce,
                )?;
                backend_ctx.chunked_io_state =
                    Some(ChunkedIoState::EncryptedWrite(EncryptedChunkedWriteState {
                        path: request.path.clone(),
                        location: request.location,
                        encryptor,
                    }));
                Ok(reply::StartEncryptedChunkedWrite {}.into())
            }
            ChunkedRequest::StartEncryptedChunkedRead(request) => {
                clear_chunked_state(&store, client_id, backend_ctx)?;
                let key = keystore.load_key(
                    Secrecy::Secret,
                    Some(Kind::Symmetric(CHACHA8_KEY_LEN)),
                    &request.key,
                )?;
                let nonce: Bytes<CHACHA8_STREAM_NONCE_LEN> =
                    filestore.read(&request.path, request.location)?;
                let nonce: &StreamNonce<ChaCha8Poly1305, StreamLE31<ChaCha8Poly1305>> =
                    (&*nonce).into();
                let aead = ChaCha8Poly1305::new((&*key.material).into());
                let decryptor = DecryptorLE31::<ChaCha8Poly1305>::from_aead(aead, nonce);
                backend_ctx.chunked_io_state =
                    Some(ChunkedIoState::EncryptedRead(EncryptedChunkedReadState {
                        path: request.path.clone(),
                        location: request.location,
                        decryptor,
                        offset: CHACHA8_STREAM_NONCE_LEN,
                    }));
                Ok(reply::StartEncryptedChunkedRead {}.into())
            }
        }
    }
}

fn clear_chunked_state(
    store: &impl Store,
    client_id: &Path,
    ctx: &mut StagingContext,
) -> Result<(), Error> {
    match ctx.chunked_io_state.take() {
        Some(ChunkedIoState::Read(_)) | None => {}
        Some(ChunkedIoState::Write(write_state)) => {
            info!("Automatically cancelling write");
            store::abort_chunked_write(store, client_id, &write_state.path, write_state.location);
        }
        Some(ChunkedIoState::EncryptedRead(_)) => {}
        Some(ChunkedIoState::EncryptedWrite(write_state)) => {
            info!("Automatically cancelling encrypted write");
            store::abort_chunked_write(store, client_id, &write_state.path, write_state.location);
        }
    }
    Ok(())
}

fn write_chunk(
    store: &impl Store,
    client_id: &Path,
    ctx: &mut StagingContext,
    data: &Message,
) -> Result<(), Error> {
    match ctx.chunked_io_state {
        Some(ChunkedIoState::Write(ref write_state)) => {
            store::filestore_write_chunk(
                store,
                client_id,
                &write_state.path,
                write_state.location,
                data,
            )?;
        }
        Some(ChunkedIoState::EncryptedWrite(ref mut write_state)) => {
            let mut data =
                Bytes::<{ MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN }>::try_from(&**data).unwrap();
            write_state
                .encryptor
                .encrypt_next_in_place(
                    write_state.path.as_ref().as_bytes(),
                    &mut HeaplessBuffer::from(&mut data),
                )
                .map_err(|_err| {
                    error!("Failed to encrypt {:?}", _err);
                    Error::AeadError
                })?;
            store::filestore_write_chunk(
                store,
                client_id,
                &write_state.path,
                write_state.location,
                &data,
            )?;
        }
        _ => return Err(Error::MechanismNotAvailable),
    }
    Ok(())
}

fn write_last_chunk(
    store: &impl Store,
    client_id: &Path,
    ctx: &mut StagingContext,
    data: &Message,
) -> Result<(), Error> {
    match ctx.chunked_io_state.take() {
        Some(ChunkedIoState::Write(write_state)) => {
            store::filestore_write_chunk(
                store,
                client_id,
                &write_state.path,
                write_state.location,
                data,
            )?;
            store::flush_chunks(store, client_id, &write_state.path, write_state.location)?;
        }
        Some(ChunkedIoState::EncryptedWrite(write_state)) => {
            let mut data =
                Bytes::<{ MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN }>::try_from(&**data).unwrap();
            write_state
                .encryptor
                .encrypt_last_in_place(
                    &[write_state.location as u8],
                    &mut HeaplessBuffer::from(&mut data),
                )
                .map_err(|_err| {
                    error!("Failed to encrypt {:?}", _err);
                    Error::AeadError
                })?;
            store::filestore_write_chunk(
                store,
                client_id,
                &write_state.path,
                write_state.location,
                &data,
            )?;
            store::flush_chunks(store, client_id, &write_state.path, write_state.location)?;
        }
        _ => return Err(Error::MechanismNotAvailable),
    }

    Ok(())
}

fn read_encrypted_chunk(
    store: &impl Store,
    client_id: &Path,
    ctx: &mut StagingContext,
) -> Result<ChunkedReply, Error> {
    let Some(ChunkedIoState::EncryptedRead(ref mut read_state)) = ctx.chunked_io_state else {
        unreachable!(
            "Read encrypted chunk can only be called in the context encrypted chunk reads"
        );
    };
    let (mut data, len): (Bytes<{ MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN }>, usize) =
        store::filestore_read_chunk(
            store,
            client_id,
            &read_state.path,
            read_state.location,
            OpenSeekFrom::Start(read_state.offset as _),
        )?;
    read_state.offset += data.len();

    let is_last = !data.is_full();
    if is_last {
        let Some(ChunkedIoState::EncryptedRead(read_state)) = ctx.chunked_io_state.take() else {
            unreachable!();
        };

        read_state
            .decryptor
            .decrypt_last_in_place(
                &[read_state.location as u8],
                &mut HeaplessBuffer::from(&mut data),
            )
            .map_err(|_err| {
                error!("Failed to decrypt {:?}", _err);
                Error::AeadError
            })?;
        let data = Bytes::try_from(&*data).expect("decryptor removes the tag");
        Ok(reply::ReadChunk {
            data,
            len: chunked_decrypted_len(len)?,
        }
        .into())
    } else {
        read_state
            .decryptor
            .decrypt_next_in_place(
                read_state.path.as_ref().as_bytes(),
                &mut HeaplessBuffer::from(&mut data),
            )
            .map_err(|_err| {
                error!("Failed to decrypt {:?}", _err);
                Error::AeadError
            })?;
        let data = Bytes::try_from(&*data).expect("decryptor removes the tag");
        Ok(reply::ReadChunk {
            data,
            len: chunked_decrypted_len(len)?,
        }
        .into())
    }
}

/// Calculate the decrypted length of a chunked encrypted file
fn chunked_decrypted_len(len: usize) -> Result<usize, Error> {
    let len = len.checked_sub(CHACHA8_STREAM_NONCE_LEN).ok_or_else(|| {
        error!("File too small");
        Error::FilesystemReadFailure
    })?;
    const CHUNK_LEN: usize = POLY1305_TAG_LEN + MAX_MESSAGE_LENGTH;
    let chunk_count = len / CHUNK_LEN;
    let last_chunk_len = (len % CHUNK_LEN)
        .checked_sub(POLY1305_TAG_LEN)
        .ok_or_else(|| {
            error!("Incorrect last chunk length");
            Error::FilesystemReadFailure
        })?;

    Ok(chunk_count * MAX_MESSAGE_LENGTH + last_chunk_len)
}