Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove memory allocation and replace with slices #157

Merged
merged 7 commits into from
Aug 31, 2023

Conversation

SCingolani
Copy link
Contributor

This change completely removes the memory management from Heimlig, instead relying on slices provided in the requests for both input and output parameters.

As part of this change, the Channel concept was replaced with a Source + Sink abstraction which lends itself better to a future use of the async Stream and Sink traits and simplifies the handling of lifetimes.

This change stems from a discussion in #156 where an alternative abstraction over parameters to and from Heimlig was suggested. Compared to that, this change leverages the full power of the borrow checker to make sure that neither the internal Heimlig code, or the integration code mishandles the memory used to pass in parameters and write out results.

This change completely removes the memory management from Heimlig,
instead relying on slices provided in the requests for both input and
output parameters.

As part of this change, the `Channel` concept was replaced with a
`Source` + `Sink` abstraction which lends itself better to a future use
of async `Stream` and `Sink` from futures.
heimlig/src/client/api.rs Outdated Show resolved Hide resolved
heimlig/src/hsm/workers/chachapoly_worker.rs Outdated Show resolved Hide resolved
heimlig/src/hsm/workers/chachapoly_worker.rs Outdated Show resolved Hide resolved
heimlig/src/hsm/workers/chachapoly_worker.rs Outdated Show resolved Hide resolved
heimlig/src/hsm/workers/chachapoly_worker.rs Outdated Show resolved Hide resolved
let aad = match &aad {
Some(aad) => aad.as_slice(),
Some(aad) => aad,
None => &[] as &[u8],
};
match crate::crypto::chacha20poly1305::decrypt_in_place_detached(
Copy link
Contributor Author

@SCingolani SCingolani Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we check tag size before calling decrypt_in_place_detached? or does it do a check internally? if so, what is the correct size?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decrypt_in_place_detached checks the tag size.

check_sizes_with_tag(key, nonce, tag, KEY_SIZE, NONCE_SIZE, TAG_SIZE)?;

The TAG_SIZE is 16 bytes.

pub const TAG_SIZE: usize = <ChaCha20Poly1305 as AeadCore>::TagSize::USIZE;

Copy link
Contributor Author

@SCingolani SCingolani Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See proposed change in e62f76c, i.e., make it explicit in the types that the tag should have the correct size; this means offloading the error handling to the user; see for instance the test in scheduler.

Santiago Cingolani added 2 commits August 28, 2023 18:40
- Handle request sink being full on `Api::get_random()`.
- Make `Response::GetRandom` preserve the mutability of the provided
  slice.
- Remove `common::Error::Alloc` since we do not allocate anymore.
- Small fixes in `chachapoly_worker.rs`.
@SCingolani
Copy link
Contributor Author

If we do e62f76c then we should also remove the tag size check in ChachaPolyWorker::encrypt, and we could also propagate this change to crypto/chacha20poly1305.rs. If so, then I would say we move this change to another PR and change the tag to be of fixed sized everywhere in one single commit.

@inorick
Copy link
Contributor

inorick commented Aug 29, 2023

If so, then I would say we move this change to another PR and change the tag to be of fixed sized everywhere in one single commit.

Agreed, let's split the tag size checking into a separate PR and get this one merged.

api.get_random(random_size)
let random_output = Box::leak(Box::new([0u8; 16]));
info!(target: "CLIENT", "Sending request: random data (size={})", random_output.len());
api.get_random(random_output.as_mut_slice())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try prepending a &mut instead.

);
// release the memory
drop(unsafe { Box::from_raw(data) });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify that the memory returned matches the one handed out for the request.

.expect("Failed to allocate buffer for random data")
.init([0; 16]);
// Safety: we forget about the box below, so it doesn't get dropped!
let random_buffer = unsafe {
Copy link
Contributor

@inorick inorick Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upstream as leak (and from_raw) to heapless upstream.

@inorick inorick merged commit 96c07a7 into eclipse-heimlig:main Aug 31, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants