paritytech / libsecp256k1

Pure Rust Implementation of secp256k1.
Apache License 2.0
177 stars 87 forks source link

Added Drop to SharedSecret and SecretKey #9

Closed elichai closed 5 years ago

elichai commented 5 years ago
elichai commented 5 years ago

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 commented 5 years ago

@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 https://github.com/paritytech/parity-common/pull/80 @cheme uses the clear_on_drop library to solve this issue.

elichai commented 5 years ago

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

cheme commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

@sorpass so what do you think?

sorpaas commented 5 years ago

Sorry!