Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

[crypto] zeroize bls12381 secrets #733

Merged
merged 4 commits into from
Aug 10, 2022
Merged

[crypto] zeroize bls12381 secrets #733

merged 4 commits into from
Aug 10, 2022

Conversation

punwai
Copy link
Contributor

@punwai punwai commented Aug 10, 2022

Currently, deleted secret keys are kept in memory after they are dropped. This could be a problem as it could potentially later be recovered by an attacker. This PR implements zeroize on all BLS12381 secrets (KP and privkey), and includes tests that ensure that the secret key is deleted once secrets are dropped in the program.

@punwai punwai requested a review from joyqvq August 10, 2022 00:11
@punwai punwai changed the title [crypto] zeroize for bls12381 secrets [crypto] zeroize bls12381 secrets Aug 10, 2022
@@ -403,7 +404,7 @@ impl KeyPair for BLS12381KeyPair {
}

fn private(self) -> Self::PrivKey {
self.secret
BLS12381PrivateKey::from_bytes(self.secret.as_ref()).unwrap()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

required, because we implemented drop for KeyPair.

Copy link
Contributor

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

Left a few indications, but this globally looks v. good.
This is the important one: #733 (comment)

Comment on lines 429 to 443
unsafe {
for i in 0..BLS12381PrivateKey::LENGTH {
assert!(*ptr.add(i) == 0);
}
}

// Check that self.bytes is zeroized
unsafe {
let mut vec = Vec::new();
for i in 0..BLS12381PrivateKey::LENGTH {
vec.push(*bytes_ptr.add(i));
}
assert_ne!(&vec[..], &sk_bytes[..]);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

YMMV, but something like
let memory: &[u8] = unsafe { ::std::slice::from_raw_parts(ptr, BLS12381PrivateKey::LENGTH) } may just give you something easier to inspect, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this! I think it is much cleaner - I haven't touched unsafe rust much so I just didn't know about this.

Comment on lines 410 to 411
let kp = keys().pop().unwrap();
let mut sk = kp.private();
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure keys delivers some non-zero bytes in the first place for sk? Are we sure that invariant will remain the same forever if somebody modifies how keys() works?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a deterministic key generation within the test itself.

Comment on lines 454 to 460
let kp = keys().pop().unwrap();
let sk = kp.private();
sk_bytes.extend_from_slice(sk.as_ref());

ptr = std::ptr::addr_of!(sk.privkey) as *const u8;
bytes_ptr = &sk.as_ref()[0] as *const u8;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I for one am OK testing just zeroize on drop, and inferring that if it works on drop, it works everywhere.
But I won't fight against the inclusion of one more test, of course.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that makes sense, drop calls zeroize itself so it makes no sense to test manually calling zeroize.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed the PR to only zeroize on drop

@punwai punwai requested a review from huitseeker August 10, 2022 01:25
@punwai punwai added the crypto label Aug 10, 2022
Copy link
Contributor

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

LGTM!

@punwai punwai merged commit ad20f08 into MystenLabs:main Aug 10, 2022
huitseeker pushed a commit that referenced this pull request Aug 12, 2022
* [crypto] implement zeroize on bls12381 secrets
mwtian pushed a commit to mwtian/sui that referenced this pull request Sep 30, 2022
* [crypto] implement zeroize on bls12381 secrets
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants