diff --git a/Cargo.lock b/Cargo.lock index 7af4387e..8ee92f0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -991,6 +991,7 @@ dependencies = [ "sgx_tseal", "sgx_types", "tiny-keccak 1.5.0", + "zeroize", ] [[package]] @@ -2787,8 +2788,7 @@ dependencies = [ [[package]] name = "libsecp256k1" version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95b09eff1b35ed3b33b877ced3a691fc7a481919c7e29c53c906226fcf55e2a1" +source = "git+https://github.com/paritytech/libsecp256k1?rev=48dabd8821852c5fe00b846f6c37e1f6b05c3d8c#48dabd8821852c5fe00b846f6c37e1f6b05c3d8c" dependencies = [ "arrayref", "base64 0.13.0", @@ -2806,8 +2806,7 @@ dependencies = [ [[package]] name = "libsecp256k1-core" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5be9b9bb642d8522a44d533eab56c16c738301965504753b03ad1de3425d5451" +source = "git+https://github.com/paritytech/libsecp256k1?rev=48dabd8821852c5fe00b846f6c37e1f6b05c3d8c#48dabd8821852c5fe00b846f6c37e1f6b05c3d8c" dependencies = [ "crunchy", "digest 0.9.0", @@ -2817,8 +2816,7 @@ dependencies = [ [[package]] name = "libsecp256k1-gen-ecmult" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3038c808c55c87e8a172643a7d87187fc6c4174468159cb3090659d55bcb4809" +source = "git+https://github.com/paritytech/libsecp256k1?rev=48dabd8821852c5fe00b846f6c37e1f6b05c3d8c#48dabd8821852c5fe00b846f6c37e1f6b05c3d8c" dependencies = [ "libsecp256k1-core", ] @@ -2826,8 +2824,7 @@ dependencies = [ [[package]] name = "libsecp256k1-gen-genmult" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3db8d6ba2cec9eacc40e6e8ccc98931840301f1006e95647ceb2dd5c3aa06f7c" +source = "git+https://github.com/paritytech/libsecp256k1?rev=48dabd8821852c5fe00b846f6c37e1f6b05c3d8c#48dabd8821852c5fe00b846f6c37e1f6b05c3d8c" dependencies = [ "libsecp256k1-core", ] @@ -6360,9 +6357,9 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" dependencies = [ "zeroize_derive", ] diff --git a/enclave-modules/ecall-handler/src/enclave_manage/enclave.rs b/enclave-modules/ecall-handler/src/enclave_manage/enclave.rs index b88087ae..4c2797db 100644 --- a/enclave-modules/ecall-handler/src/enclave_manage/enclave.rs +++ b/enclave-modules/ecall-handler/src/enclave_manage/enclave.rs @@ -9,8 +9,8 @@ pub(crate) fn generate_enclave_key( input: GenerateEnclaveKeyInput, ) -> Result { let ek = EnclaveKey::new()?; - let sealed_ek = ek.seal()?; let ek_pub = ek.get_pubkey(); + let sealed_ek = ek.seal()?; let report_data = ReportData::new(ek_pub.as_address(), input.operator); let report = match rsgx_create_report(&input.target_info, &report_data.into()) { Ok(r) => r, diff --git a/enclave/Cargo.lock b/enclave/Cargo.lock index bd0d9834..a270ed63 100644 --- a/enclave/Cargo.lock +++ b/enclave/Cargo.lock @@ -303,6 +303,7 @@ dependencies = [ "sgx_tseal", "sgx_types", "tiny-keccak 1.5.0", + "zeroize", ] [[package]] @@ -885,8 +886,7 @@ checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" [[package]] name = "libsecp256k1" version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95b09eff1b35ed3b33b877ced3a691fc7a481919c7e29c53c906226fcf55e2a1" +source = "git+https://github.com/paritytech/libsecp256k1?rev=48dabd8821852c5fe00b846f6c37e1f6b05c3d8c#48dabd8821852c5fe00b846f6c37e1f6b05c3d8c" dependencies = [ "arrayref", "base64 0.13.1", @@ -904,8 +904,7 @@ dependencies = [ [[package]] name = "libsecp256k1-core" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5be9b9bb642d8522a44d533eab56c16c738301965504753b03ad1de3425d5451" +source = "git+https://github.com/paritytech/libsecp256k1?rev=48dabd8821852c5fe00b846f6c37e1f6b05c3d8c#48dabd8821852c5fe00b846f6c37e1f6b05c3d8c" dependencies = [ "crunchy", "digest 0.9.0", @@ -915,8 +914,7 @@ dependencies = [ [[package]] name = "libsecp256k1-gen-ecmult" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3038c808c55c87e8a172643a7d87187fc6c4174468159cb3090659d55bcb4809" +source = "git+https://github.com/paritytech/libsecp256k1?rev=48dabd8821852c5fe00b846f6c37e1f6b05c3d8c#48dabd8821852c5fe00b846f6c37e1f6b05c3d8c" dependencies = [ "libsecp256k1-core", ] @@ -924,8 +922,7 @@ dependencies = [ [[package]] name = "libsecp256k1-gen-genmult" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3db8d6ba2cec9eacc40e6e8ccc98931840301f1006e95647ceb2dd5c3aa06f7c" +source = "git+https://github.com/paritytech/libsecp256k1?rev=48dabd8821852c5fe00b846f6c37e1f6b05c3d8c#48dabd8821852c5fe00b846f6c37e1f6b05c3d8c" dependencies = [ "libsecp256k1-core", ] @@ -1837,9 +1834,9 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" dependencies = [ "zeroize_derive", ] diff --git a/modules/crypto/Cargo.toml b/modules/crypto/Cargo.toml index d07926e3..839c5573 100644 --- a/modules/crypto/Cargo.toml +++ b/modules/crypto/Cargo.toml @@ -14,7 +14,8 @@ serde = { version = "1.0.184", default-features = false, features = ["alloc", "d serde-big-array = "0.5.1" hex = { version = "0.4", default-features = false, features = ["alloc"] } flex-error = { version = "0.4.4", default-features = false } -libsecp256k1 = { version = "0.7.1", default-features = false, features = ["static-context", "hmac"] } +libsecp256k1 = { rev = "48dabd8821852c5fe00b846f6c37e1f6b05c3d8c", git = "https://github.com/paritytech/libsecp256k1", default-features = false, features = ["static-context", "hmac"] } +zeroize = { version = "1.8.1", default-features = false, features = ["alloc", "zeroize_derive"] } [features] default = ["std"] diff --git a/modules/crypto/src/key.rs b/modules/crypto/src/key.rs index 221aec86..4910614b 100644 --- a/modules/crypto/src/key.rs +++ b/modules/crypto/src/key.rs @@ -2,18 +2,18 @@ use crate::prelude::*; use crate::{Error, Keccak256, Signer, Verifier}; use alloc::fmt; use core::fmt::Display; -use libsecp256k1::PublicKeyFormat; +use core::sync::atomic; use libsecp256k1::{ curve::Scalar, util::{COMPRESSED_PUBLIC_KEY_SIZE, SECRET_KEY_SIZE}, - Message, PublicKey, RecoveryId, SecretKey, Signature, + Message, PublicKey, PublicKeyFormat, RecoveryId, SecretKey, Signature, }; use serde::{Deserialize, Serialize}; use serde_big_array::BigArray; use sgx_types::sgx_sealed_data_t; use tiny_keccak::Keccak; +use zeroize::Zeroizing; -#[derive(Default)] pub struct EnclaveKey { pub(crate) secret_key: SecretKey, } @@ -42,8 +42,8 @@ impl EnclaveKey { Ok(Self { secret_key }) } - pub fn get_privkey(&self) -> [u8; SECRET_KEY_SIZE] { - self.secret_key.serialize() + pub fn get_privkey(self) -> Zeroizing<[u8; SECRET_KEY_SIZE]> { + Zeroizing::new(self.secret_key.serialize()) } pub fn get_pubkey(&self) -> EnclavePublicKey { @@ -51,6 +51,16 @@ impl EnclaveKey { } } +impl Drop for EnclaveKey { + fn drop(&mut self) { + self.secret_key.clear(); + // Use fences to prevent accesses from being reordered before this + // point, which should hopefully help ensure that all accessors + // see zeroes after this point. + atomic::compiler_fence(atomic::Ordering::SeqCst); + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnclavePublicKey(PublicKey); diff --git a/modules/crypto/src/sgx/sealing.rs b/modules/crypto/src/sgx/sealing.rs index 969f766f..71c10ce3 100644 --- a/modules/crypto/src/sgx/sealing.rs +++ b/modules/crypto/src/sgx/sealing.rs @@ -4,39 +4,40 @@ use crate::EnclaveKey; use crate::Error; use crate::Signer; use crate::{prelude::*, EnclavePublicKey}; +use core::ops::Deref; use libsecp256k1::{util::SECRET_KEY_SIZE, SecretKey}; use sgx_tseal::SgxSealedData; -use sgx_types::{marker::ContiguousMemory, sgx_sealed_data_t}; -use sgx_types::{sgx_attributes_t, SGX_KEYPOLICY_MRENCLAVE, TSEAL_DEFAULT_MISCMASK}; +use sgx_types::{ + sgx_attributes_t, sgx_sealed_data_t, SGX_KEYPOLICY_MRENCLAVE, TSEAL_DEFAULT_MISCMASK, +}; +use zeroize::Zeroizing; -#[derive(Clone, Copy)] -struct UnsealedEnclaveKey([u8; SECRET_KEY_SIZE]); - -unsafe impl ContiguousMemory for UnsealedEnclaveKey {} +#[derive(Clone)] +struct UnsealedEnclaveKey(Zeroizing<[u8; SECRET_KEY_SIZE]>); impl SealingKey for EnclaveKey { - fn seal(&self) -> Result { - seal_enclave_key(UnsealedEnclaveKey(self.get_privkey())) + fn seal(self) -> Result { + seal_enclave_key(&UnsealedEnclaveKey(self.get_privkey())) } fn unseal(sek: &SealedEnclaveKey) -> Result { let unsealed = unseal_enclave_key(&sek)?; - let secret_key = SecretKey::parse(&unsealed.0)?; + let secret_key = SecretKey::parse(unsealed.0.deref())?; Ok(Self { secret_key }) } } -fn seal_enclave_key(data: UnsealedEnclaveKey) -> Result { +fn seal_enclave_key(data: &UnsealedEnclaveKey) -> Result { let attribute_mask = sgx_attributes_t { flags: 0xffff_ffff_ffff_fff3, xfrm: 0, }; - let sealed_data = SgxSealedData::::seal_data_ex( + let sealed_data = SgxSealedData::<[u8; SECRET_KEY_SIZE]>::seal_data_ex( SGX_KEYPOLICY_MRENCLAVE, attribute_mask, TSEAL_DEFAULT_MISCMASK, Default::default(), - &data, + data.0.deref(), ) .map_err(|e| Error::sgx_error(e, "failed to seal enclave key".to_string()))?; let mut sek = SealedEnclaveKey([0; SEALED_DATA_32_USIZE]); @@ -56,16 +57,18 @@ fn seal_enclave_key(data: UnsealedEnclaveKey) -> Result fn unseal_enclave_key(sek: &SealedEnclaveKey) -> Result { let mut sek = sek.clone(); let sealed = unsafe { - SgxSealedData::::from_raw_sealed_data_t( + SgxSealedData::<[u8; SECRET_KEY_SIZE]>::from_raw_sealed_data_t( sek.0.as_mut_ptr() as *mut sgx_sealed_data_t, SEALED_DATA_32_SIZE, ) } .ok_or_else(|| Error::failed_unseal("failed to convert from raw sealed data".to_owned()))?; - Ok(*sealed - .unseal_data() - .map_err(|e| Error::sgx_error(e, "failed to unseal enclave key".to_string()))? - .get_decrypt_txt()) + Ok(UnsealedEnclaveKey(Zeroizing::new( + *sealed + .unseal_data() + .map_err(|e| Error::sgx_error(e, "failed to unseal enclave key".to_string()))? + .get_decrypt_txt(), + ))) } impl Signer for SealedEnclaveKey { diff --git a/modules/crypto/src/traits.rs b/modules/crypto/src/traits.rs index a4310154..96f5d688 100644 --- a/modules/crypto/src/traits.rs +++ b/modules/crypto/src/traits.rs @@ -17,7 +17,7 @@ pub trait SealingKey where Self: core::marker::Sized, { - fn seal(&self) -> Result; + fn seal(self) -> Result; fn unseal(sek: &SealedEnclaveKey) -> Result; }