Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 918e75f

Browse files
Merge pull request #24 from Nitrokey/revert
Revert #18 and #22
2 parents 91d5a37 + d5f39b1 commit 918e75f

12 files changed

Lines changed: 3 additions & 991 deletions

File tree

src/api.rs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,17 @@ generate_enums! {
4343
ReadDirFilesFirst: 13
4444
ReadDirFilesNext: 14
4545
ReadFile: 15
46-
ReadChunk: 65
4746
Metadata: 26
4847
// ReadCounter: 7
4948
RandomBytes: 16
5049
SerializeKey: 17
5150
Sign: 18
5251
WriteFile: 19
53-
StartChunkedWrite: 66
54-
WriteChunk: 67
55-
FlushChunks: 68
56-
AbortChunkedWrite: 69
5752
UnsafeInjectKey: 20
5853
UnsafeInjectSharedKey: 21
5954
UnwrapKey: 22
60-
UnwrapKeyFromFile: 70
6155
Verify: 23
6256
WrapKey: 24
63-
WrapKeyToFile: 71
6457

6558
Attest: 0xFF
6659

@@ -249,10 +242,6 @@ pub mod request {
249242
ReadFile:
250243
- location: Location
251244
- path: PathBuf
252-
ReadChunk:
253-
- location: Location
254-
- pos: OpenSeekFrom
255-
- path: PathBuf
256245

257246
Metadata:
258247
- location: Location
@@ -294,24 +283,6 @@ pub mod request {
294283
- data: Message
295284
- user_attribute: Option<UserAttribute>
296285

297-
StartChunkedWrite:
298-
- location: Location
299-
- path: PathBuf
300-
- data: Message
301-
- user_attribute: Option<UserAttribute>
302-
303-
WriteChunk:
304-
- location: Location
305-
- pos: OpenSeekFrom
306-
- path: PathBuf
307-
- data: Message
308-
FlushChunks:
309-
- location: Location
310-
- path: PathBuf
311-
AbortChunkedWrite:
312-
- location: Location
313-
- path: PathBuf
314-
315286
UnsafeInjectKey:
316287
- mechanism: Mechanism // -> implies key type
317288
- raw_key: SerializedKey
@@ -343,23 +314,6 @@ pub mod request {
343314
- key: KeyId
344315
- associated_data: ShortData
345316

346-
// this should always be an AEAD algorithm
347-
WrapKeyToFile:
348-
- mechanism: Mechanism
349-
- wrapping_key: KeyId
350-
- key: KeyId
351-
- path: PathBuf
352-
- location: Location
353-
- associated_data: Message
354-
355-
UnwrapKeyFromFile:
356-
- mechanism: Mechanism
357-
- key: KeyId
358-
- path: PathBuf
359-
- file_location: Location
360-
- key_location: Location
361-
- associated_data: Message
362-
363317
RequestUserConsent:
364318
- level: consent::Level
365319
- timeout_milliseconds: u32
@@ -480,9 +434,6 @@ pub mod reply {
480434

481435
ReadFile:
482436
- data: Message
483-
ReadChunk:
484-
- data: Message
485-
- len: usize
486437

487438
Metadata:
488439
- metadata: Option<crate::types::Metadata>
@@ -507,11 +458,6 @@ pub mod reply {
507458
- signature: Signature
508459

509460
WriteFile:
510-
StartChunkedWrite:
511-
WriteChunk:
512-
FlushChunks:
513-
AbortChunkedWrite:
514-
- aborted: bool
515461

516462
Verify:
517463
- valid: bool
@@ -525,15 +471,9 @@ pub mod reply {
525471
UnwrapKey:
526472
- key: Option<KeyId>
527473

528-
UnwrapKeyFromFile:
529-
- key: Option<KeyId>
530-
531474
WrapKey:
532475
- wrapped_key: Message
533476

534-
// this should always be an AEAD algorithm
535-
WrapKeyToFile:
536-
537477
// UI
538478
RequestUserConsent:
539479
- result: consent::Result

src/client.rs

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -531,54 +531,6 @@ pub trait CryptoClient: PollClient {
531531
associated_data,
532532
})
533533
}
534-
535-
/// Wrap a key to a file
536-
/// This enables wrapping keys that don't fit in the buffers used by
537-
/// [`write_file`](FilesystemClient::write_file) and [`read_file`](FilesystemClient::read_file)
538-
fn wrap_key_to_file(
539-
&mut self,
540-
mechanism: Mechanism,
541-
wrapping_key: KeyId,
542-
key: KeyId,
543-
path: PathBuf,
544-
location: Location,
545-
associated_data: &[u8],
546-
) -> ClientResult<'_, reply::WrapKeyToFile, Self> {
547-
let associated_data =
548-
Bytes::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
549-
self.request(request::WrapKeyToFile {
550-
mechanism,
551-
wrapping_key,
552-
key,
553-
path,
554-
location,
555-
associated_data,
556-
})
557-
}
558-
559-
/// Wrap a key to a file
560-
/// This enables wrapping keys that don't fit in the buffers used by
561-
/// [`write_file`](FilesystemClient::write_file) and [`read_file`](FilesystemClient::read_file)
562-
fn unwrap_key_from_file(
563-
&mut self,
564-
mechanism: Mechanism,
565-
key: KeyId,
566-
path: PathBuf,
567-
file_location: Location,
568-
key_location: Location,
569-
associated_data: &[u8],
570-
) -> ClientResult<'_, reply::UnwrapKeyFromFile, Self> {
571-
let associated_data =
572-
Bytes::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
573-
self.request(request::UnwrapKeyFromFile {
574-
mechanism,
575-
key,
576-
path,
577-
file_location,
578-
key_location,
579-
associated_data,
580-
})
581-
}
582534
}
583535

584536
/// Create counters, increment existing counters.
@@ -670,20 +622,6 @@ pub trait FilesystemClient: PollClient {
670622
self.request(request::ReadFile { location, path })
671623
}
672624

673-
// Read part of a file, up to 1KiB starting at `pos`
674-
fn read_file_chunk(
675-
&mut self,
676-
location: Location,
677-
path: PathBuf,
678-
pos: OpenSeekFrom,
679-
) -> ClientResult<'_, reply::ReadChunk, Self> {
680-
self.request(request::ReadChunk {
681-
location,
682-
path,
683-
pos,
684-
})
685-
}
686-
687625
/// Fetch the Metadata for a file or directory
688626
///
689627
/// If the file doesn't exists, return None
@@ -722,64 +660,6 @@ pub trait FilesystemClient: PollClient {
722660
user_attribute,
723661
})
724662
}
725-
726-
/// Begin writing a file that can be larger than 1KiB
727-
///
728-
/// More chunks can be written with [`write_file_chunk`](FilesystemClient::write_file_chunk).
729-
/// Before the data becomes readable, it needs to be flushed with [`flush_chunks`](FilesystemClient::flush_chunks), or aborted with [`abort_chunked_write`](FilesystemClient::abort_chunked_write)
730-
///
731-
/// Chunked writes are buffered in memory. Failing to abort or flush a chunked write will lead to a memory leak, that can be solved by a power cycle.
732-
fn start_chunked_write(
733-
&mut self,
734-
location: Location,
735-
path: PathBuf,
736-
data: Message,
737-
user_attribute: Option<UserAttribute>,
738-
) -> ClientResult<'_, reply::StartChunkedWrite, Self> {
739-
self.request(request::StartChunkedWrite {
740-
location,
741-
path,
742-
data,
743-
user_attribute,
744-
})
745-
}
746-
747-
/// Write part of a file
748-
///
749-
/// See [`start_chunked_write`](FilesystemClient::start_chunked_write).
750-
fn write_file_chunk(
751-
&mut self,
752-
location: Location,
753-
path: PathBuf,
754-
data: Message,
755-
pos: OpenSeekFrom,
756-
) -> ClientResult<'_, reply::WriteChunk, Self> {
757-
self.request(request::WriteChunk {
758-
location,
759-
path,
760-
data,
761-
pos,
762-
})
763-
}
764-
765-
/// Flush a file opened with [`start_chunked_write`](FilesystemClient::start_chunked_write).
766-
/// Only after this will the content of the file be readable
767-
fn flush_chunks(
768-
&mut self,
769-
location: Location,
770-
path: PathBuf,
771-
) -> ClientResult<'_, reply::FlushChunks, Self> {
772-
self.request(request::FlushChunks { location, path })
773-
}
774-
775-
/// Abort writes to a file opened with [`start_chunked_write`](FilesystemClient::start_chunked_write).
776-
fn abort_chunked_write(
777-
&mut self,
778-
location: Location,
779-
path: PathBuf,
780-
) -> ClientResult<'_, reply::AbortChunkedWrite, Self> {
781-
self.request(request::AbortChunkedWrite { location, path })
782-
}
783663
}
784664

785665
/// All the other methods that are fit to expose.

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub mod serde_extensions;
3636
pub mod service;
3737
pub mod store;
3838
pub mod types;
39-
pub mod utils;
4039

4140
#[cfg(feature = "virt")]
4241
#[cfg_attr(docsrs, doc(cfg(feature = "virt")))]

src/mechanisms/chacha8poly1305.rs

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::api::*;
22
// use crate::config::*;
3-
use crate::config::MAX_SERIALIZED_KEY_LENGTH;
43
use crate::error::Error;
54
use crate::key;
65
use crate::service::*;
@@ -16,95 +15,6 @@ const TOTAL_LEN: usize = KEY_LEN + NONCE_LEN;
1615
const TAG_LEN: usize = 16;
1716
const KIND: key::Kind = key::Kind::Symmetric(KEY_LEN);
1817
const KIND_NONCE: key::Kind = key::Kind::Symmetric32Nonce(NONCE_LEN);
19-
const WRAPPED_TO_FILE_LEN: usize = MAX_SERIALIZED_KEY_LENGTH + NONCE_LEN + TAG_LEN;
20-
21-
#[cfg(feature = "chacha8-poly1305")]
22-
impl super::Chacha8Poly1305 {
23-
pub fn wrap_key_to_file(
24-
keystore: &mut impl Keystore,
25-
filestore: &mut impl Filestore,
26-
request: &request::WrapKeyToFile,
27-
) -> Result<reply::WrapKeyToFile, Error> {
28-
use chacha20poly1305::aead::{AeadMutInPlace, KeyInit};
29-
use chacha20poly1305::ChaCha8Poly1305;
30-
use rand_core::RngCore as _;
31-
32-
let serialized_key = keystore.load_key(key::Secrecy::Secret, None, &request.key)?;
33-
34-
let mut data =
35-
Bytes::<WRAPPED_TO_FILE_LEN>::from_slice(&serialized_key.serialize()).unwrap();
36-
let material_len = data.len();
37-
data.resize_default(material_len + NONCE_LEN).unwrap();
38-
let (material, nonce) = data.split_at_mut(material_len);
39-
keystore.rng().fill_bytes(nonce);
40-
let nonce = (&*nonce).try_into().unwrap();
41-
42-
let key = keystore.load_key(key::Secrecy::Secret, Some(KIND), &request.wrapping_key)?;
43-
let chachakey: [u8; KEY_LEN] = (&*key.material).try_into().unwrap();
44-
let mut aead = ChaCha8Poly1305::new(&GenericArray::clone_from_slice(&chachakey));
45-
let tag = aead
46-
.encrypt_in_place_detached(
47-
<&GenericArray<_, _> as From<&[u8; NONCE_LEN]>>::from(nonce),
48-
&request.associated_data,
49-
material,
50-
)
51-
.unwrap();
52-
data.extend_from_slice(&tag).unwrap();
53-
filestore.write(&request.path, request.location, &data)?;
54-
Ok(reply::WrapKeyToFile {})
55-
}
56-
57-
pub fn unwrap_key_from_file(
58-
keystore: &mut impl Keystore,
59-
filestore: &mut impl Filestore,
60-
request: &request::UnwrapKeyFromFile,
61-
) -> Result<reply::UnwrapKeyFromFile, Error> {
62-
use chacha20poly1305::aead::{AeadMutInPlace, KeyInit};
63-
use chacha20poly1305::ChaCha8Poly1305;
64-
let mut data: Bytes<WRAPPED_TO_FILE_LEN> =
65-
filestore.read(&request.path, request.file_location)?;
66-
67-
let data_len = data.len();
68-
if data_len < TAG_LEN + NONCE_LEN {
69-
error!("Attempt to unwrap file that doesn't contain a key");
70-
return Err(Error::InvalidSerializedKey);
71-
}
72-
let (tmp, tag) = data.split_at_mut(data_len - TAG_LEN);
73-
let tmp_len = tmp.len();
74-
let (material, nonce) = tmp.split_at_mut(tmp_len - NONCE_LEN);
75-
76-
// Coerce to array
77-
let nonce = (&*nonce).try_into().unwrap();
78-
let tag = (&*tag).try_into().unwrap();
79-
80-
let key = keystore.load_key(key::Secrecy::Secret, Some(KIND), &request.key)?;
81-
let chachakey: [u8; KEY_LEN] = (&*key.material).try_into().unwrap();
82-
let mut aead = ChaCha8Poly1305::new(&GenericArray::clone_from_slice(&chachakey));
83-
if aead
84-
.decrypt_in_place_detached(
85-
<&GenericArray<_, _> as From<&[u8; NONCE_LEN]>>::from(nonce),
86-
&request.associated_data,
87-
material,
88-
<&GenericArray<_, _> as From<&[u8; TAG_LEN]>>::from(tag),
89-
)
90-
.is_err()
91-
{
92-
return Ok(reply::UnwrapKeyFromFile { key: None });
93-
}
94-
let key = key::Key::try_deserialize(material)?;
95-
let info = key::Info {
96-
flags: key.flags,
97-
kind: key.kind,
98-
};
99-
let key = keystore.store_key(
100-
request.key_location,
101-
key::Secrecy::Secret,
102-
info,
103-
&key.material,
104-
)?;
105-
Ok(reply::UnwrapKeyFromFile { key: Some(key) })
106-
}
107-
}
10818

10919
#[cfg(feature = "chacha8-poly1305")]
11020
impl GenerateKey for super::Chacha8Poly1305 {

0 commit comments

Comments
 (0)