Skip to content
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.lock

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

1 change: 1 addition & 0 deletions crates/k1util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ thiserror.workspace = true
k256.workspace = true
hex.workspace = true
libp2p.workspace = true
zeroize.workspace = true

[dev-dependencies]
criterion.workspace = true
Expand Down
19 changes: 15 additions & 4 deletions crates/k1util/src/k1util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use k256::{
ecdsa::{self, RecoveryId, Signature, SigningKey, hazmat::VerifyPrimitive},
};
use libp2p::identity::PublicKey as Libp2pPublicKey;
use zeroize::Zeroizing;

/// `SCALAR_LEN` is the length of secp256k1 scalar.
pub const SCALAR_LEN: usize = 32;
Expand Down Expand Up @@ -212,10 +213,15 @@ pub fn recover(hash: &[u8], sig: &[u8]) -> Result<PublicKey> {
}

/// Load loads a secret key from a file.
///
/// The hex-encoded file contents and the decoded scalar are intermediate
/// buffers holding the raw secret, so both are wrapped in [`Zeroizing`] and
/// wiped once the key has been parsed.
pub fn load(file: &Path) -> Result<SecretKey> {
let contents = std::fs::read_to_string(file).map_err(K1UtilError::FailedToReadFile)?;
let contents =
Zeroizing::new(std::fs::read_to_string(file).map_err(K1UtilError::FailedToReadFile)?);

let decoded = hex::decode(contents.trim())?;
let decoded = Zeroizing::new(hex::decode(contents.trim())?);

let key = SecretKey::from_slice(&decoded).map_err(K1UtilError::FailedToParseSecretKey)?;

Expand All @@ -228,8 +234,13 @@ pub fn load(file: &Path) -> Result<SecretKey> {
/// matching Charon's `app/k1util/k1util.go` `Save` which writes via
/// `os.WriteFile(file, ..., 0o600)`. This prevents the private key from being
/// world-readable.
///
/// The serialized scalar and its hex encoding are intermediate buffers holding
/// the raw secret, so both are wrapped in [`Zeroizing`] and wiped once the file
/// has been written.
pub fn save(key: &SecretKey, file: &Path) -> Result<()> {
let encoded = hex::encode(key.to_bytes());
let raw = Zeroizing::new(key.to_bytes());
let encoded = Zeroizing::new(hex::encode(raw.as_slice()));

#[cfg(unix)]
{
Expand Down Expand Up @@ -258,7 +269,7 @@ pub fn save(key: &SecretKey, file: &Path) -> Result<()> {

#[cfg(not(unix))]
{
std::fs::write(file, encoded).map_err(K1UtilError::FailedToWriteFile)?;
std::fs::write(file, encoded.as_bytes()).map_err(K1UtilError::FailedToWriteFile)?;
}

Ok(())
Expand Down
Loading