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

Add ZeroizeOnDrop to ed25519 keys #1956

Merged
merged 4 commits into from
Oct 24, 2023
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/miscellaneous/1958-k256.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Switched from using `libsecp256k1` to `k256` crate.
([\#1958](https://github.com/anoma/namada/pull/1958))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Tag `ed25519` keys with `ZeroizeOnDrop`
([\#1958](https://github.com/anoma/namada/pull/1958))
77 changes: 18 additions & 59 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ git2 = "0.13.25"
ics23 = "0.9.0"
index-set = {git = "https://github.com/heliaxdev/index-set", tag = "v0.7.1", features = ["serialize-borsh", "serialize-serde"]}
itertools = "0.10.0"
k256 = { version = "0.13.0", default-features = false, features = ["ecdsa", "pkcs8", "precomputed-tables", "serde", "std"]}
lazy_static = "1.4.0"
libc = "0.2.97"
libloading = "0.7.2"
libsecp256k1 = {git = "https://github.com/heliaxdev/libsecp256k1", rev = "bbb3bd44a49db361f21d9db80f9a087c194c0ae9", default-features = false, features = ["std", "static-context"]}
# branch = "murisi/namada-integration"
masp_primitives = { git = "https://github.com/anoma/masp", rev = "50acc5028fbcd52a05970fe7991c7850ab04358e" }
masp_proofs = { git = "https://github.com/anoma/masp", rev = "50acc5028fbcd52a05970fe7991c7850ab04358e", default-features = false, features = ["local-prover"] }
Expand Down
6 changes: 3 additions & 3 deletions apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1612,11 +1612,11 @@ mod test_utils {
ref sig,
ref recovery_id,
)) => {
let mut sig_bytes = sig.serialize();
let recovery_id_bytes = recovery_id.serialize();
let mut sig_bytes = sig.to_vec();
let recovery_id_bytes = recovery_id.to_byte();
sig_bytes[0] = sig_bytes[0].wrapping_add(1);
let bytes: [u8; 65] =
[sig_bytes.as_slice(), [recovery_id_bytes].as_slice()]
[sig_bytes.as_slice(), &[recovery_id_bytes]]
.concat()
.try_into()
.unwrap();
Expand Down
8 changes: 1 addition & 7 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ ferveo-tpke = [
wasm-runtime = [
"rayon",
]
# secp256k1 key signing, disabled in WASM build by default as it bloats the
# build a lot
secp256k1-sign = [
"libsecp256k1/hmac",
]

abciplus = [
"ibc",
Expand Down Expand Up @@ -78,7 +73,7 @@ ics23.workspace = true
impl-num-traits = "0.1.2"
index-set.workspace = true
itertools.workspace = true
libsecp256k1.workspace = true
k256.workspace = true
masp_primitives.workspace = true
num256.workspace = true
num-integer = "0.1.45"
Expand All @@ -104,7 +99,6 @@ zeroize.workspace = true

[dev-dependencies]
assert_matches.workspace = true
libsecp256k1 = {workspace = true, features = ["hmac"]}
pretty_assertions.workspace = true
proptest.workspace = true
rand.workspace = true
Expand Down
10 changes: 2 additions & 8 deletions core/src/types/key/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use data_encoding::HEXLOWER;
#[cfg(feature = "rand")]
use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;
use zeroize::{Zeroize, ZeroizeOnDrop};

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

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

impl super::SecretKey for SecretKey {
Expand Down Expand Up @@ -223,12 +223,6 @@ 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
16 changes: 7 additions & 9 deletions core/src/types/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,17 +692,15 @@ mod more_tests {
fn zeroize_keypair_secp256k1() {
use rand::thread_rng;

let mut sk = secp256k1::SigScheme::generate(&mut thread_rng());
let sk_scalar = sk.0.to_scalar_ref();
let len = sk_scalar.0.len();
let ptr = sk_scalar.0.as_ref().as_ptr();

let original_data = sk_scalar.0;

let sk = secp256k1::SigScheme::generate(&mut thread_rng());
let (ptr, original_data) = {
let sk_scalar = sk.0.as_scalar_primitive().as_ref();
(sk_scalar.as_ptr(), sk_scalar.to_owned())
};
drop(sk);

assert_ne!(&original_data, unsafe {
core::slice::from_raw_parts(ptr, len)
assert_ne!(original_data.as_slice(), unsafe {
core::slice::from_raw_parts(ptr, secp256k1::SECRET_KEY_SIZE)
});
}
}
Loading