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

Improve secret key memory management #328

Merged
merged 4 commits into from
Oct 25, 2021
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ sshkeys = "0.3"
reqwest = { version = "0.11", features = ["json"] }
flate2 = "1.0"
bitvec = "0.20"
clear_on_drop = "0.2.4"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
chrono = { version = "0.4", features = ["serde"] }
Expand Down
63 changes: 22 additions & 41 deletions src/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,29 +244,19 @@ impl Default for Algorithm {
impl JWK {
#[cfg(feature = "ring")]
pub fn generate_ed25519() -> Result<JWK, Error> {
use ring::signature::KeyPair;
let rng = ring::rand::SystemRandom::new();
let doc = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng)?;
let key_pkcs8 = doc.as_ref();
let keypair = ring::signature::Ed25519KeyPair::from_pkcs8(key_pkcs8)?;
let public_key = keypair.public_key().as_ref();
let mut key_pkcs8 = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng)?
.as_ref()
.to_vec();
// reference: ring/src/ec/curve25519/ed25519/signing.rs
let private_key = &key_pkcs8[0x10..0x30];
Ok(JWK {
params: Params::OKP(OctetParams {
curve: "Ed25519".to_string(),
public_key: Base64urlUInt(public_key.to_vec()),
private_key: Some(Base64urlUInt(private_key.to_vec())),
}),
public_key_use: None,
key_operations: None,
algorithm: None,
key_id: None,
x509_url: None,
x509_certificate_chain: None,
x509_thumbprint_sha1: None,
x509_thumbprint_sha256: None,
})
let private_key = key_pkcs8[0x10..0x30].to_vec();
let public_key = key_pkcs8[0x35..0x55].to_vec();
key_pkcs8.zeroize();
Ok(JWK::from(Params::OKP(OctetParams {
curve: "Ed25519".to_string(),
public_key: Base64urlUInt(public_key),
private_key: Some(Base64urlUInt(private_key)),
})))
}

#[cfg(feature = "ed25519-dalek")]
Expand All @@ -275,43 +265,34 @@ impl JWK {
let keypair = ed25519_dalek::Keypair::generate(&mut csprng);
let sk_bytes = keypair.secret.to_bytes();
let pk_bytes = keypair.public.to_bytes();
Ok(JWK {
params: Params::OKP(OctetParams {
curve: "Ed25519".to_string(),
public_key: Base64urlUInt(pk_bytes.to_vec()),
private_key: Some(Base64urlUInt(sk_bytes.to_vec())),
}),
public_key_use: None,
key_operations: None,
algorithm: None,
key_id: None,
x509_url: None,
x509_certificate_chain: None,
x509_thumbprint_sha1: None,
x509_thumbprint_sha256: None,
})
Ok(JWK::from(Params::OKP(OctetParams {
curve: "Ed25519".to_string(),
public_key: Base64urlUInt(pk_bytes.to_vec()),
private_key: Some(Base64urlUInt(sk_bytes.to_vec())),
})))
}

#[cfg(feature = "k256")]
pub fn generate_secp256k1() -> Result<JWK, Error> {
let mut rng = rand::rngs::OsRng {};
let secret_key = k256::SecretKey::random(&mut rng);
let sk_bytes = secret_key.to_bytes();
// SecretKey zeroizes on drop
let sk_bytes = secret_key.to_bytes().to_vec();
let public_key = secret_key.public_key();
let mut ec_params = ECParams::try_from(&public_key)?;
ec_params.ecc_private_key = Some(Base64urlUInt(sk_bytes.to_vec()));
ec_params.ecc_private_key = Some(Base64urlUInt(sk_bytes));
Ok(JWK::from(Params::EC(ec_params)))
}

#[cfg(feature = "p256")]
pub fn generate_p256() -> Result<JWK, Error> {
let mut rng = rand::rngs::OsRng {};
let secret_key = p256::SecretKey::random(&mut rng);
use p256::elliptic_curve::ff::PrimeField;
let sk_bytes = secret_key.secret_scalar().to_repr();
// SecretKey zeroizes on drop
let sk_bytes = secret_key.to_bytes().to_vec();
let public_key: p256::PublicKey = secret_key.public_key();
let mut ec_params = ECParams::try_from(&public_key)?;
ec_params.ecc_private_key = Some(Base64urlUInt(sk_bytes.to_vec()));
ec_params.ecc_private_key = Some(Base64urlUInt(sk_bytes));
Ok(JWK::from(Params::EC(ec_params)))
}

Expand Down
1 change: 1 addition & 0 deletions src/jws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ pub fn sign_bytes(algorithm: Algorithm, data: &[u8], key: &JWK) -> Result<Vec<u8
},
_ => return Err(Error::KeyTypeNotImplemented),
};
clear_on_drop::clear_stack(1);
Ok(signature)
}

Expand Down