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

Box secret memory #400

Merged
merged 23 commits into from
Jun 19, 2020
Merged

Box secret memory #400

merged 23 commits into from
Jun 19, 2020

Conversation

cheme
Copy link
Collaborator

@cheme cheme commented Jun 17, 2020

This pr box memory for Secret.

@cheme cheme requested review from ordian and dvdplm June 17, 2020 15:23
Copy link
Member

@ordian ordian left a comment

Choose a reason for hiding this comment

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

LGTM.

@ordian
Copy link
Member

ordian commented Jun 17, 2020

fmt should make travis happy

@ordian
Copy link
Member

ordian commented Jun 17, 2020

Reading https://doc.rust-lang.org/src/alloc/boxed.rs.html#1074-1097, I'm actually not sure if Pin<Box<T>> is different from Box<T> for our use-case.

@cheme
Copy link
Collaborator Author

cheme commented Jun 17, 2020

Yes indeed, let's remove it.

@cheme cheme changed the title Pin secret memory Box secret memory Jun 17, 2020
@ordian ordian requested a review from drahnr June 18, 2020 12:31
@ordian
Copy link
Member

ordian commented Jun 18, 2020

LGTM. Just needs a second approval.

@cheme cheme requested review from tomusdrw and arkpar June 18, 2020 12:32
Copy link
Contributor

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

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

lgtm % TryFrom stuff

@@ -232,7 +238,7 @@ impl TryFrom<&[u8]> for Secret {
if b.len() != SECP256K1_SECRET_KEY_SIZE {
Copy link
Contributor

Choose a reason for hiding this comment

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

Both TryFrom implementations can't really guarantee that the source slice is zeroized, there is no way to inform the user that it should be.
Not a security expert, but perhaps it's worth to get rid of these in favour of some more explicit methods which would allow you to add extra security notes.

Copy link
Collaborator Author

@cheme cheme Jun 18, 2020

Choose a reason for hiding this comment

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

I am more having an issue with the From implementation, try_from never zeroized the input, but yes we do not have any related doc.
I will be in favor of replacing those from and try_from implementation with specific functions, but then it would be a breaking PR, so I am thinking this PR could be parity-crypto 6.2 and another one could be a 7.0 with a more significant work for library users.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually that is not such a big change, maybe doing it now and releasing breaking 0.7.0 would be better, I will do it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me!

Copy link

@drahnr drahnr Jun 18, 2020

Choose a reason for hiding this comment

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

I am a big fan of deprecation warnings, so people get a hint on how to migrate directly from the build - that could be applied to the TryFrom impl while exposing a more explicit API already

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could try 6.2 with dep warning indeed.

@drahnr
Copy link

drahnr commented Jun 18, 2020

From my understanding of Pin, as already stated by @ordian and @cheme, there is no advantage in using it in this context.

@arkpar
Copy link
Member

arkpar commented Jun 18, 2020

Why does it need to be boxed? Stack memory can be zeroed in drop handler just as well.
Wouldn't type Secret = zeroize::Zeroizing<H256> work?

@cheme
Copy link
Collaborator Author

cheme commented Jun 18, 2020

We would need to pin memory to avoid mem copy which does not looks trivial to me (I need to check again but it would mean following a &'a H256 since H256 is not Deref), box was more straightforward.

@cheme
Copy link
Collaborator Author

cheme commented Jun 18, 2020

In 8ddd0da I am putting the 'from' 'try_from' function as deprecated, following this I switched usage in the crate and I fear there is a possible performance regression in ec_math_utils du to zeroize.
Furthermore there are part of the code that do not apply the zeroize (but it would require changing api which would not be ok for a 6.2).
maybe we should stick to only add the deprecated info and the new functions, or revert it
CC\ @ordian @tomusdrw @drahnr @arkpar

Edit : also it includes codes from @tomusdrw rustweb3 for zeroing secp secret key memory

@drahnr
Copy link

drahnr commented Jun 18, 2020

It depends on what kind of release you want to slate and what kind of implications a deprecations has in code that uses it?
Regarding perf, did you measure how much it actually costs? I think the cost is something you have to live with, security takes precedence, if perf is bad, the way of zeroing should be revisited, not the if keys should be zeroed.

parity-crypto/CHANGELOG.md Outdated Show resolved Hide resolved
@cheme
Copy link
Collaborator Author

cheme commented Jun 19, 2020

Lastest commit I did switch back some of the previous api by simply take advantage of #[inline(always)].

Co-authored-by: Andronik Ordian <write@reusable.software>
parity-crypto/CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Andronik Ordian <write@reusable.software>
Co-authored-by: Andronik Ordian <write@reusable.software>
@cheme cheme merged commit 03e3d34 into paritytech:master Jun 19, 2020
rsandrini-forte pushed a commit to fortelabsinc/parity-common that referenced this pull request Aug 12, 2021
Put `Secret` memory on heap.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants