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

Zeroize secret keys upon drop #277

Merged
merged 4 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/277-zeroize-secret-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Zeroize secret keys from memory
([#277](https://github.com/anoma/namada/pull/277))
50 changes: 30 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ wasmer-engine-dylib = {version = "=2.2.0", optional = true}
wasmer-engine-universal = {version = "=2.2.0", optional = true}
wasmer-vm = {version = "2.2.0", optional = true}
wasmparser = "0.83.0"
zeroize = "1.5.5"

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
21 changes: 15 additions & 6 deletions shared/src/types/key/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
#[cfg(feature = "rand")]
use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;

use super::{
ParsePublicKeyError, ParseSecretKeyError, ParseSignatureError, RefTo,
Expand Down Expand Up @@ -122,8 +123,8 @@ impl FromStr for PublicKey {
}

/// Ed25519 secret key
#[derive(Debug, Serialize, Deserialize)]
pub struct SecretKey(pub ed25519_consensus::SigningKey);
#[derive(Debug, Serialize, Deserialize, Zeroize)]
pub struct SecretKey(pub Box<ed25519_consensus::SigningKey>);

impl super::SecretKey for SecretKey {
type PublicKey = PublicKey;
Expand Down Expand Up @@ -157,13 +158,15 @@ impl RefTo<PublicKey> for SecretKey {

impl Clone for SecretKey {
fn clone(&self) -> SecretKey {
SecretKey(ed25519_consensus::SigningKey::from(self.0.to_bytes()))
SecretKey(Box::new(ed25519_consensus::SigningKey::from(
self.0.to_bytes(),
)))
}
}

impl BorshDeserialize for SecretKey {
fn deserialize(buf: &mut &[u8]) -> std::io::Result<Self> {
Ok(SecretKey(
Ok(SecretKey(Box::new(
ed25519_consensus::SigningKey::try_from(
<[u8; SECRET_KEY_LENGTH] as BorshDeserialize>::deserialize(
buf,
Expand All @@ -173,7 +176,7 @@ impl BorshDeserialize for SecretKey {
.map_err(|e| {
std::io::Error::new(std::io::ErrorKind::InvalidInput, e)
})?,
))
)))
}
}

Expand Down Expand Up @@ -218,6 +221,12 @@ impl FromStr for SecretKey {
}
}

impl Drop for SecretKey {
fn drop(&mut self) {
self.0.zeroize();
}
}

/// Ed25519 signature
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Signature(pub ed25519_consensus::Signature);
Expand Down Expand Up @@ -325,7 +334,7 @@ impl super::SigScheme for SigScheme {
where
R: CryptoRng + RngCore,
{
SecretKey(ed25519_consensus::SigningKey::new(csprng))
SecretKey(Box::new(ed25519_consensus::SigningKey::new(csprng)))
}

fn sign(keypair: &SecretKey, data: impl AsRef<[u8]>) -> Self::Signature {
Expand Down
22 changes: 22 additions & 0 deletions shared/src/types/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,25 @@ macro_rules! sigscheme_test {

#[cfg(test)]
sigscheme_test! {ed25519_test, ed25519::SigScheme}

#[cfg(test)]
mod more_tests {
use super::*;

#[test]
fn zeroize_keypair_ed25519() {
use rand::thread_rng;

let sk = ed25519::SecretKey(Box::new(
ed25519_consensus::SigningKey::new(thread_rng()),
));
let len = sk.0.as_bytes().len();
let ptr = sk.0.as_bytes().as_ptr();

drop(sk);

assert_eq!(&[0u8; 32], unsafe {
core::slice::from_raw_parts(ptr, len)
});
}
}
34 changes: 17 additions & 17 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"tx_bond.wasm": "tx_bond.302172cc7d0a7e2278ce58299809875f354925364b25c9b64e92461995c51950.wasm",
"tx_from_intent.wasm": "tx_from_intent.19099ad11a5f59272bd5dbd8b7bc7093ae66ae7a2b25034300f1b34d3e37ffd1.wasm",
"tx_ibc.wasm": "tx_ibc.9aec1969a37705f9ae5d6e95d13126e0f37e78171ef37c2f0fdd0eb16ac49270.wasm",
"tx_init_account.wasm": "tx_init_account.7a45233a98978c67ff005bf8a1fb8f3f7689a75f98684c1735617776f98fabad.wasm",
"tx_init_nft.wasm": "tx_init_nft.b0dd29e0e982c3bd04c7a8c4dcd0184d9d827df6a4211794dd74fbdced1e7430.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.45be75dc2c22048dce23ae346c895b2be19737a39844416436aac62914847388.wasm",
"tx_init_validator.wasm": "tx_init_validator.5a7c9a3a115883359246a4145af11f748ded043b5b36d1cb71e54fb3ef169183.wasm",
"tx_mint_nft.wasm": "tx_mint_nft.fd8932515db71638263193930f60c14cec54c11e72e6ab40d8201d0247db0c1a.wasm",
"tx_transfer.wasm": "tx_transfer.9e51e5b48ba3ddee699fed334e14fe9a81b7e299b0cfcbf10482b9f784c092c2.wasm",
"tx_unbond.wasm": "tx_unbond.020d22fa306850b0a4aade96f711087d03568ed45276fff60226a80de47cc455.wasm",
"tx_update_vp.wasm": "tx_update_vp.00d828bdf510d92d971ca59015945c8c00f285a802a655451cc5ee87c5ee99bc.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.b8aa22d6d22c31fa6c1f4619a81eaa43aa40c8865b91f71449a5f3c65b84eacf.wasm",
"tx_withdraw.wasm": "tx_withdraw.b8538e5acfc2945e98b76cc17eb11a03c545021e8f70cf6e5b436219e749b559.wasm",
"vp_nft.wasm": "vp_nft.d272e0c50f17c020fe806e03278e83377ec45b81e88432316ce836ee24605f6e.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.79c1da702d67f464453af0b145872bba28913b029508f1257b4a22f69123ec1e.wasm",
"vp_token.wasm": "vp_token.04482a8132e91ab726b4a9027f44a84c885c36e3d608e9c4e153d0cfe4f88449.wasm",
"vp_user.wasm": "vp_user.e390d55fc2e797fcc4c43bd40b93ea1c3d58544d5f086301a0dbc38bd93388ba.wasm"
"tx_bond.wasm": "tx_bond.e8fcf7413067810f82e1c0f54fdc9675a4c686b172db9c44c714310d3f19baf6.wasm",
"tx_from_intent.wasm": "tx_from_intent.cd5f7af4d3af9300df8dc16e891f39f85d6497a43a2cd74d30d1dc506649291e.wasm",
"tx_ibc.wasm": "tx_ibc.3805f69ceafd60b857a860d26e6fbe2973f309289147cf59c10e0bd65a30057e.wasm",
"tx_init_account.wasm": "tx_init_account.8abb68c5f40fa9ea39f03ddb9c387bc84a642bac4f40c7e9ceebb8418b3ccde7.wasm",
"tx_init_nft.wasm": "tx_init_nft.564390006d7b8f4f46d215704b05561a7279c92a6fe8f3ec61118a5486a3e092.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.5fc912c847cb4d04743b4dea650418a4eae233f4abf91493791a92e3de5050aa.wasm",
"tx_init_validator.wasm": "tx_init_validator.92d11bc1cc27818864e13d122179a199043a274f7bba901b11c6474261eb97eb.wasm",
"tx_mint_nft.wasm": "tx_mint_nft.9f5b723dd4ed195b489b8456a6d83167829f0b2b8fb479be19b877a60b81c803.wasm",
"tx_transfer.wasm": "tx_transfer.032bfa997c8f2ee2cc5bf053f0128880a79a57ad3d8c3ecc6ff0c22e27f47ebd.wasm",
"tx_unbond.wasm": "tx_unbond.17444d665b8a7bebb624691eaab318c845c8ce18c557d0af440b7bfdb7efa806.wasm",
"tx_update_vp.wasm": "tx_update_vp.d47bfe6ef55a86bce36093feeff74fe09ec7cffebf9696dd37658756a4eb793d.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.1bee09ffd3a1e577dc9b9d6d5f91c4a6f91a9b2ac9e3a3d8258c00cd12902304.wasm",
"tx_withdraw.wasm": "tx_withdraw.75dbdc78924d8072ffcefaed67715d9480dd9f702ac77cf18ee15d0e44a38ab0.wasm",
"vp_nft.wasm": "vp_nft.3711eac06bda0c39ab52618bc458df7e6b16ed096bb3930b93ade93c9b7cf23a.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.400be39dcf0fedf099232bdecfa42c2dbe1f0aaede34fb99ec26da3b6623c09a.wasm",
"vp_token.wasm": "vp_token.118aef31933858351cf77315069fc2f7c61e7db8891c7990e77f9ffd6ef90af0.wasm",
"vp_user.wasm": "vp_user.73136ffcd68e46e2f88d27ae9e48a0731b06d883dd3defcb41b604cc7968c638.wasm"
}
5 changes: 3 additions & 2 deletions wasm/tx_template/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions wasm/vp_template/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions wasm/wasm_source/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions wasm_for_tests/wasm_source/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.