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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
86 changes: 23 additions & 63 deletions src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use {
};

use ed25519_dalek as dalek;
use ed25519_dalek::{Signer, Verifier};
use zeroize::ZeroizeOnDrop;

use crate::*;
Expand All @@ -30,31 +29,14 @@ 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;

// only required for some configurations
#[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 {
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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::<C>::default();
let mut pad_s = ecdsa::elliptic_curve::FieldBytes::<C>::default();
copy_right_aligned(sig.r.as_ref(), &mut pad_r).map_err(|_| {
Expand All @@ -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
})
Expand Down Expand Up @@ -464,22 +430,16 @@ impl SignKey {
pub(crate) fn sign(&self, msg: &impl SSHEncode) -> Result<OwnedSig> {
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())
}

Expand Down
8 changes: 7 additions & 1 deletion src/sshwire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand All @@ -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)]
Expand Down