-
Notifications
You must be signed in to change notification settings - Fork 285
Add feature zeroize #262
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
Add feature zeroize #262
Conversation
I was going to open an issue and discussion and what not but since this is pretty basic I thought I'd just learn how to contribute to |
src/key.rs
Outdated
|
||
/// Secret 256-bit key used as `x` in an ECDSA signature | ||
#[cfg_attr(feature = "zeroize_derive", derive(Zeroize))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given how trivial the generated implementation is, we might want to implement this ourselves so we don't add syn
to this dependency tree? (Assuming none of the other dependencies already pull it in but I don't think so)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has the added bonus that we can use zeroize
for the feature name :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 'no new dependencies' a hard rule @apoelstra ? I don't see how to do this without adding the dependency on zeroize
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has the added bonus that we can use
zeroize
for the feature name :)
FWIW: You could have also achieved this by enabling the custom derive support through the feature that is exposed by zeroize
instead of depending on zeroize_derive
directly: https://docs.rs/zeroize/1.2.0/zeroize/#custom-derive-support
zeroize = { version = "1.2", default-features = false, optional = true, features = ["zeroize_derive"] }
This will re-export the Zeroize
derive from the zeroize
crate as zeroize::Zeroize
. Serde uses the same pattern so you don't have to depend on serde
and serde_derive
at the same time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, cheers man.
Agree with @thomaseizinger, we might as well generate this ourselves (especially as we already have macros which do a pile of stuff for byte arrays...might as well impl Also need to add a note to the README that if you enable this feature the MSRV is 1.44, since normally it's 1.29. And I guess CI needs another case (I'd be fine if we just tacked testing this onto the |
We rejected zeroize in the past, see the discussion in #102 . We can of course reconsider this. |
@real-or-random well, this PR is far more limited in scope than the previous one. We aren't implementing |
Ok, I see. This makes sense then! |
Perhaps we should not be implementing This makes me think that all the zeroing I've done outside of this library is just smoke and mirrors :) |
Yeah, if you want to do best efforts you need to do one of 2 things:
And even after these 2 you have 2 problems:
EDIT: IMHO even though that last point seems like this is meaningless I don't agree. I think even zeroing some out the copies is better than none, because every copy makes a Heartbleed-like attack a little easier. |
So, if I'm understanding correctly; zero what we can, gather as much information as we can get about things we might be missing and document that somewhere so its explicit what we've done and where there might be holes still. And doing so adds value to users of the library even if its not perfect. I"m on it! |
9adf33b
to
1849214
Compare
Concept ACK |
Introduce new dependency `zeroize`. Implement `Zeroize` for our array types with the `impl_array_newtype` macro. Add a unit test that test a newly create `SecretKey` is zeroized after calling `zeroize()` on it.
Alternative impl without new dependency: #311 |
Feel free to close this if #311 goes in. |
Seems like @dr-orlovsky has this feature covered (#312 and #313). |
I just found a big problem with ever implementing Zeroize on let secp = Secp256k1::new();
let mut secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
secret_key.zeroize();
let sig = secp.sign(&message, &secret_key); This will cause an |
Necro-comment, yet a custom zeroize impl could call zeroize (writing zeroes without being optimized out if not further used). and then write all 1s. This will create a valid secret key, and if the key is used after zeroizing, the write 1s won't be optimized out. |
It is useful for downstream users to be able to zeroize secrets. Doing so is supported by the
zeroize
library.Add feature
zeroize
which, if enabled, implementsZeroize
for all our types that useimpl_array_newtype
(includesSecretKey
andKeyPair
).On purpose we do not implement
Drop
and we still implementCopy
. This is so as to not negatively effect users of the library (see link below from when this was attempted previously). This means that stack variables are not zerozied and there may be additional copies made by the compiler.Adds a section to the README explaining the aims of the
zeroize
feature, included here:@elichai this quote is from you in this PR's thread, I gave not attribution of the source of the quote. Pleases say so if you would like attribution (or if you would prefer not to be quoted).
Please note, PR introduces a new dependency on
zeroize v1.2
.