From a7b2e640a972a3275c036d678be659e484a4424f Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Wed, 2 Dec 2026 20:05:25 +0800 Subject: [PATCH] Use streaming sign/verify for ed25519 and ecdsa Newer ed25519-dalek and ecdsa allow signing and verifying messages on the fly. This should save a little bit of stack memory, and avoids ugly buffer sizing. An undocumented 40 character username limit is now removed. --- Cargo.toml | 2 +- src/sign.rs | 86 ++++++++++++++------------------------------------ src/sshwire.rs | 8 ++++- 3 files changed, 31 insertions(+), 65 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6b1ac606..2cd95022 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ digest = "0.11" zeroize.workspace = true cipher = { version = "0.5", features = ["zeroize"] } subtle.workspace = true -ed25519-dalek = { version = "3.0", default-features = false, features = ["zeroize", "rand_core"] } +ed25519-dalek = { version = "3.0", default-features = false, features = ["zeroize", "rand_core", "hazmat"] } x25519-dalek = { version = "3.0", default-features = false, features = ["zeroize"] } ml-kem = { version = "0.3.2", default-features = false, features = ["zeroize"], optional = true } rsa = { version = "0.10", default-features = false, optional = true, features = ["pkcs1v15"] } diff --git a/src/sign.rs b/src/sign.rs index ede47ea4..6f5f0bc8 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -9,7 +9,6 @@ use { }; use ed25519_dalek as dalek; -use ed25519_dalek::{Signer, Verifier}; use zeroize::ZeroizeOnDrop; use crate::*; @@ -30,7 +29,7 @@ use crate::packets::ECDSAPubKey; #[cfg(feature = "_ecdsa")] use ecdsa::VerifyingKey; #[cfg(feature = "_ecdsa")] -use ecdsa::signature::hazmat::{PrehashSigner as _, PrehashVerifier as _}; +use ecdsa::signature::hazmat::PrehashSigner as _; #[cfg(feature = "ecdsa256")] use p256::NistP256; @@ -38,23 +37,6 @@ use p256::NistP256; #[allow(unused_imports)] use digest::Digest; -// TODO remove once we use byupdate. -// signatures are for hostkey (32 byte sessiid) or pubkey (auth packet || sessid). -// we assume a max 40 character username here. -const MAX_ED25519_SIG_MSG: usize = 1 - + 4 - + 40 - + 4 - + 14 - + 4 - + 9 - + 1 - + 4 - + SSH_NAME_CURVE25519_LIBSSH.len() - + 4 - + 32 - + 32; - #[allow(clippy::upper_case_acronyms)] #[derive(Debug, Clone, Copy)] pub enum SigType { @@ -173,20 +155,10 @@ impl SigType { let s: &[u8; 64] = s.sig.0.try_into().map_err(|_| Error::BadSig)?; let s: dalek::Signature = s.into(); - // TODO: pending merge of https://github.com/dalek-cryptography/curve25519-dalek/pull/556 - // In the interim we use a fixed buffer. - // dalek::hazmat::raw_verify_byupdate( - // &k, - // |h: &mut sha2::Sha512| { - // sshwire::hash_ser(h, msg).map_err(|_| dalek::SignatureError::new()) - // }, - // &s, - // ) - // .map_err(|_| Error::BadSig) - let mut buf = [0; MAX_ED25519_SIG_MSG]; - let l = sshwire::write_ssh(&mut buf, msg)?; - let buf = &buf[..l]; - k.verify(buf, &s).map_err(|_| Error::BadSig) + + let mut v = k.verify_stream(&s).map_err(|_| Error::BadSig)?; + sshwire::hash_ser(&mut v, msg).map_err(|_| Error::BadSig)?; + v.finalize_and_verify().map_err(|_| Error::BadSig) } #[cfg(feature = "rsa")] @@ -228,6 +200,7 @@ impl SigType { // RFC5656 defined r and s as mpint, so SSH will trim leading zeros. // ecdsa crate requires exact sized inputs, so copy them in // zero padded (big endian). + let mut pad_r = ecdsa::elliptic_curve::FieldBytes::::default(); let mut pad_s = ecdsa::elliptic_curve::FieldBytes::::default(); copy_right_aligned(sig.r.as_ref(), &mut pad_r).map_err(|_| { @@ -245,20 +218,13 @@ impl SigType { error::BadSig.build() })?; - // TODO: once ecdsa is non-rc and all the crates like digest are aligned, - // can use verify_digest() instead of hazmat::verify_prehashed. - // k.verify_digest(|&mut d| { - // sshwire::hash_ser(d, msg).map_err(|_| ecdsa::Error::new()) - // }, &signature).map_err(|_| { - // trace!("ECDSA verify failed"); - // Error::BadSig - // }) - - let mut h = sha2::Sha256::new(); - sshwire::hash_ser(&mut h, msg)?; - let h = h.finalize(); - - k.verify_prehash(&h, &sig).map_err(|_| { + k.verify_digest( + |d: &mut sha2::Sha256| { + sshwire::hash_ser(d, msg).map_err(|_| ecdsa::Error::new()) + }, + &sig, + ) + .map_err(|_| { trace!("ECDSA verify failed"); Error::BadSig }) @@ -464,22 +430,16 @@ impl SignKey { pub(crate) fn sign(&self, msg: &impl SSHEncode) -> Result { let sig: OwnedSig = match self { SignKey::Ed25519(k) => { - // TODO: pending merge of https://github.com/dalek-cryptography/curve25519-dalek/pull/556 - // let exk: dalek::hazmat::ExpandedSecretKey = (&k.to_bytes()).into(); - // let sig = dalek::hazmat::raw_sign_byupdate( - // &exk, - // |h: &mut sha2::Sha512| { - // sshwire::hash_ser(h, msg) - // .map_err(|_| dalek::SignatureError::new()) - // }, - // &k.verifying_key(), - // ) - // .trap()?; - let mut buf = [0; MAX_ED25519_SIG_MSG]; - let l = sshwire::write_ssh(&mut buf, msg)?; - let buf = &buf[..l]; - let sig = k.sign(buf); - + let exk: dalek::hazmat::ExpandedSecretKey = (&k.to_bytes()).into(); + let sig = dalek::hazmat::raw_sign_byupdate( + &exk, + |h: &mut sha2::Sha512| { + sshwire::hash_ser(h, msg) + .map_err(|_| dalek::SignatureError::new()) + }, + &k.verifying_key(), + ) + .trap()?; OwnedSig::Ed25519(sig.to_bytes()) } diff --git a/src/sshwire.rs b/src/sshwire.rs index 119a55d1..d6ca0327 100644 --- a/src/sshwire.rs +++ b/src/sshwire.rs @@ -766,7 +766,7 @@ impl<'de> SSHDecode<'de> for alloc::string::String { /// Like `digest::DynDigest` but simpler. /// /// Doesn't have any optional methods that depend on `alloc`. -pub trait SSHWireDigestUpdate: Debug { +pub trait SSHWireDigestUpdate { fn digest_update(&mut self, data: &[u8]); } @@ -782,6 +782,12 @@ impl SSHWireDigestUpdate for sha2::Sha512 { } } +impl SSHWireDigestUpdate for ed25519_dalek::StreamVerifier { + fn digest_update(&mut self, data: &[u8]) { + self.update(data) + } +} + /// For debugging serialisation and packet formats, captures hashed content #[cfg(feature = "std")] #[derive(Debug)]