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

Added Drop to SharedSecret and SecretKey#9

Merged
sorpaas merged 3 commits into
paritytech:masterfrom
elichai:master
Mar 7, 2019
Merged

Added Drop to SharedSecret and SecretKey#9
sorpaas merged 3 commits into
paritytech:masterfrom
elichai:master

Conversation

@elichai
Copy link
Copy Markdown
Contributor

@elichai elichai commented Feb 25, 2019

  • I added the Drop trait to SecretKey and SharedSecret which calls clear(),
  • I replaced the clear function with write_volatile to prevent the compiler from optimizing it out.

Comment thread src/lib.rs
}
}

impl Into<Scalar> for SecretKey {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is Into<Scalar> removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because this creates an error.
You can't move out the inside of a type that implements Drop

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe this will work:

let ret = self.0.clone();
ret

I understand that this may be a little bit more inefficient but it preserves the API, as someone might need it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm ok, although that will leak the scalar and then the Drop won't run on it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread src/lib.rs Outdated
@elichai
Copy link
Copy Markdown
Contributor Author

elichai commented Feb 25, 2019

BTW, one thing about this is that if you pass one of these things by value to somewhere they will be moved in memory and create another copy of the data that won't be removed.
the solution to this is either put the array in a Box or Pin or just always pass by reference.

e.g:
With box: https://play.rust-lang.org/?gist=fade1db77af5f902f8403a97c9341b44
without box: https://play.rust-lang.org/?gist=9425accba3793bbc777fa767e265b7e8

Edit:
There's no Box without std, I'm still looking for a different solution for my code too but for now it just means always passing by reference :/

@sorpaas
Copy link
Copy Markdown
Contributor

sorpaas commented Feb 25, 2019

@elichai Do you think this may be better suited for libraries using libsecp256k1? If you're under no_std, you probably run things in wasm or something, in which I presume whether clearing the memory or not doesn't matter. If you're not on no_std, then like you said, there's Pin and Box (which can easily create wrapper on SecretKey to make sure nothing is leaked).

FYI, on paritytech/parity-common#80 @cheme uses the clear_on_drop library to solve this issue.

@elichai
Copy link
Copy Markdown
Contributor Author

elichai commented Feb 25, 2019

Hey, You might be right and it is more suited for the users to put SharedSecret and SecretKey in Boxs.
and by looking at paritytech/parity-common#80 I couldn't find how he addressed the passing by value problem

@cheme
Copy link
Copy Markdown

cheme commented Feb 25, 2019

@elichai I think @sorpaas was referring to the possibility to use clear_on_drop instead of write_volatile (I do not remember what was the pros and cons), I agree with you that it does not fix the passing by value issue, using 'pin' seems definitely like an interesting option.

@elichai
Copy link
Copy Markdown
Contributor Author

elichai commented Feb 25, 2019

@elichai I think @sorpaas was referring to the possibility to use clear_on_drop instead of write_volatile (I do not remember what was the pros and cons), I agree with you that it does not fix the passing by value issue, using 'pin' seems definitely like an interesting option.

oh yeah, I personally don't like clear_on_drop because it uses gymnastics to make the compiler not optimize this away instead of using the builitin functions,
so in my opinion this isn't future proof (as llvms optimizing properties can change over time)

About the Pin, it's nice because it's accessible from core and you don't need std, but I still haven't managed to make it work as I want and the docs around it are lacking

@elichai
Copy link
Copy Markdown
Contributor Author

elichai commented Mar 7, 2019

@sorpass so what do you think?

@sorpaas sorpaas merged commit ff0d746 into paritytech:master Mar 7, 2019
@sorpaas
Copy link
Copy Markdown
Contributor

sorpaas commented Mar 7, 2019

Sorry!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants