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
4 changes: 2 additions & 2 deletions .github/workflows/ssh-key.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
with:
target: ${{ matrix.target }}
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-features default,dsa,ed25519,getrandom,p256,p384,p521,rsa,tdes,std,ppk --release
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-features default,dsa,ed25519,getrandom,p256,p384,p521,rsa,tdes,std,ppk,unstable-mldsa --release
- run: cargo build --target ${{ matrix.target }} --no-default-features --features alloc,crypto,dsa,encryption,tdes --release

test:
Expand All @@ -72,7 +72,7 @@ jobs:
- uses: actions/checkout@v7
- uses: dtolnay/rust-toolchain@stable
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack test --feature-powerset --exclude-features default,dsa,ed25519,getrandom,p256,p384,p521,rsa,tdes,std,ppk --release
- run: cargo hack test --feature-powerset --exclude-features default,dsa,ed25519,getrandom,p256,p384,p521,rsa,tdes,std,ppk,unstable-mldsa --release
- run: cargo test --release
#- run: cargo test --release --features getrandom # TODO(tarcieri): fix `getrandom` feature
- run: cargo test --release --features std
Expand Down
7 changes: 5 additions & 2 deletions ssh-key/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ rsa = ["dep:rsa", "alloc", "encoding/bigint", "rand_core", "sha2"]
sha1 = ["dep:sha1"]
sha2 = ["dep:sha2"]
tdes = ["cipher/tdes", "encryption"]
# MLDSA44-Ed25519-SHA512 composite signatures from draft-ietf-sshm-composite-sigs-01
unstable-mldsa-eddsa = ["dep:ml-dsa", "ml-dsa/rand_core", "ed25519", "alloc", "rand_core", "signature/rand_core"]

# Composite signatures from draft-ietf-sshm-composite-sigs-01
unstable-mldsa = ["dep:ml-dsa", "ml-dsa/rand_core", "alloc", "rand_core", "sha2", "signature/rand_core"]
unstable-mldsa-ed25519 = ["unstable-mldsa", "ed25519"]
unstable-mldsa-p384 = ["unstable-mldsa", "p384"]

[lints]
workspace = true
Expand Down
10 changes: 8 additions & 2 deletions ssh-key/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ methods with a type that implements `AssociatedHashAlg`.
- [x] OpenSSH certificate authority (CA) support i.e. cert builder/signer
- [x] Private key encryption/decryption (`bcrypt-pbkdf` + `aes256-ctr` only)
- [x] Private key generation support: DSA, Ed25519, ECDSA (P-256/P-384/P-521),
RSA, and MLDSA44-Ed25519
RSA, MLDSA44-Ed25519, and MLDSA87-P384
- [x] FIDO/U2F key support (`sk-*`) as specified in [PROTOCOL.u2f]
- [x] Fingerprint support
- [x] "randomart" fingerprint visualizations
Expand Down Expand Up @@ -81,7 +81,8 @@ methods with a type that implements `AssociatedHashAlg`.
| `ssh‑rsa` | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅ | `rsa` | `alloc` |
| `sk‑ecdsa‑sha2‑nistp256@openssh.com` | ✅ | ✅ | ✅ | ⛔ | ⛔️ | ✅️ | ⛔ | `alloc` |
| `sk‑ssh‑ed25519@openssh.com` | ✅ | ✅ | ✅ | ⛔ | ⛔️ | ✅️️ | `ed25519` | `alloc` |
| `ssh‑mldsa44‑ed25519` | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅️ | `unstable-mldsa-eddsa` | `alloc` |
| `ssh‑mldsa44‑ed25519` | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅️ | `unstable-mldsa-ed25519` | `alloc` |
| `ssh‑mldsa87‑p384` | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅️ | `unstable-mldsa-p384` | `alloc` |

By default *no SSH signature algorithms are enabled* and you will get an
`Error::AlgorithmUnsupported` error if you try to use them.
Expand All @@ -93,6 +94,11 @@ The "Feature" column lists the name of `ssh-key` crate features which can
be enabled to provide full support for the "Keygen", "Sign", and "Verify"
functionality for a particular SSH key algorithm.

`unstable-mldsa` enables the SSH composite for each enabled curve and requires
at least one of `ed25519` or `p384`. The `unstable-mldsa-ed25519` and
`unstable-mldsa-p384` features enable the corresponding pair. For example,
`crypto` together with `unstable-mldsa` enables both composites.

## License

Licensed under either of:
Expand Down
45 changes: 37 additions & 8 deletions ssh-key/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ const CERT_SK_ECDSA_SHA2_P256: &str = "sk-ecdsa-sha2-nistp256-cert-v01@openssh.c
const CERT_SK_SSH_ED25519: &str = "sk-ssh-ed25519-cert-v01@openssh.com";

/// OpenSSH certificate for MLDSA44-Ed25519 composite public key
#[cfg(feature = "unstable-mldsa-eddsa")]
#[cfg(all(feature = "unstable-mldsa", feature = "ed25519"))]
const CERT_MLDSA44_ED25519: &str = "ssh-mldsa44-ed25519-cert";

/// SSH certificate for MLDSA87-P384 composite public key
#[cfg(all(feature = "unstable-mldsa", feature = "p384"))]
const CERT_MLDSA87_P384: &str = "ssh-mldsa87-p384-cert";

/// ECDSA with SHA-256 + NIST P-256
const ECDSA_SHA2_P256: &str = "ecdsa-sha2-nistp256";

Expand Down Expand Up @@ -96,9 +100,13 @@ const SK_ECDSA_SHA2_P256: &str = "sk-ecdsa-sha2-nistp256@openssh.com";
const SK_SSH_ED25519: &str = "sk-ssh-ed25519@openssh.com";

/// MLDSA44-Ed25519-SHA512 composite signature algorithm
#[cfg(feature = "unstable-mldsa-eddsa")]
#[cfg(all(feature = "unstable-mldsa", feature = "ed25519"))]
const SSH_MLDSA44_ED25519: &str = "ssh-mldsa44-ed25519";

/// MLDSA87-P384-SHA512 composite signature algorithm
#[cfg(all(feature = "unstable-mldsa", feature = "p384"))]
const SSH_MLDSA87_P384: &str = "ssh-mldsa87-p384";

/// SSH key algorithms, i.e. digital signature algorithms used with SSH private/public keys.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand Down Expand Up @@ -135,9 +143,13 @@ pub enum Algorithm {
SkEd25519,

/// MLDSA44-Ed25519-SHA512 composite signature algorithm.
#[cfg(feature = "unstable-mldsa-eddsa")]
#[cfg(all(feature = "unstable-mldsa", feature = "ed25519"))]
MlDsa44Ed25519,

/// MLDSA87-P384-SHA512 composite signature algorithm.
#[cfg(all(feature = "unstable-mldsa", feature = "p384"))]
MlDsa87P384,

/// Other
#[cfg(feature = "alloc")]
Other(AlgorithmName),
Expand All @@ -156,6 +168,7 @@ impl Algorithm {
/// - `sk-ecdsa-sha2-nistp256@openssh.com` (FIDO/U2F key)
/// - `sk-ssh-ed25519@openssh.com` (FIDO/U2F key)
/// - `ssh-mldsa44-ed25519`
/// - `ssh-mldsa87-p384`
///
/// Any other algorithms are mapped to the [`Algorithm::Other`] variant.
///
Expand All @@ -180,6 +193,7 @@ impl Algorithm {
/// - `sk-ecdsa-sha2-nistp256-cert-v01@openssh.com` (FIDO/U2F key)
/// - `sk-ssh-ed25519-cert-v01@openssh.com` (FIDO/U2F key)
/// - `ssh-mldsa44-ed25519-cert`
/// - `ssh-mldsa87-p384-cert`
///
/// Any other algorithms are mapped to the [`Algorithm::Other`] variant.
///
Expand Down Expand Up @@ -209,8 +223,10 @@ impl Algorithm {
}),
CERT_SK_ECDSA_SHA2_P256 => Ok(Algorithm::SkEcdsaSha2NistP256),
CERT_SK_SSH_ED25519 => Ok(Algorithm::SkEd25519),
#[cfg(feature = "unstable-mldsa-eddsa")]
#[cfg(all(feature = "unstable-mldsa", feature = "ed25519"))]
CERT_MLDSA44_ED25519 => Ok(Algorithm::MlDsa44Ed25519),
#[cfg(all(feature = "unstable-mldsa", feature = "p384"))]
CERT_MLDSA87_P384 => Ok(Algorithm::MlDsa87P384),
#[cfg(feature = "alloc")]
_ => Ok(Algorithm::Other(AlgorithmName::from_certificate_type(id)?)),
#[cfg(not(feature = "alloc"))]
Expand All @@ -236,8 +252,10 @@ impl Algorithm {
},
Algorithm::SkEcdsaSha2NistP256 => SK_ECDSA_SHA2_P256,
Algorithm::SkEd25519 => SK_SSH_ED25519,
#[cfg(feature = "unstable-mldsa-eddsa")]
#[cfg(all(feature = "unstable-mldsa", feature = "ed25519"))]
Algorithm::MlDsa44Ed25519 => SSH_MLDSA44_ED25519,
#[cfg(all(feature = "unstable-mldsa", feature = "p384"))]
Algorithm::MlDsa87P384 => SSH_MLDSA87_P384,
#[cfg(feature = "alloc")]
Algorithm::Other(algorithm) => algorithm.as_str(),
}
Expand Down Expand Up @@ -270,8 +288,10 @@ impl Algorithm {
} => CERT_RSA_SHA2_512,
Algorithm::SkEcdsaSha2NistP256 => CERT_SK_ECDSA_SHA2_P256,
Algorithm::SkEd25519 => CERT_SK_SSH_ED25519,
#[cfg(feature = "unstable-mldsa-eddsa")]
#[cfg(all(feature = "unstable-mldsa", feature = "ed25519"))]
Algorithm::MlDsa44Ed25519 => CERT_MLDSA44_ED25519,
#[cfg(all(feature = "unstable-mldsa", feature = "p384"))]
Algorithm::MlDsa87P384 => CERT_MLDSA87_P384,
Algorithm::Other(algorithm) => return algorithm.certificate_type(),
}
.to_owned()
Expand Down Expand Up @@ -302,12 +322,19 @@ impl Algorithm {
}

/// Is the algorithm the MLDSA44-Ed25519 composite scheme?
#[cfg(feature = "unstable-mldsa-eddsa")]
#[cfg(all(feature = "unstable-mldsa", feature = "ed25519"))]
#[must_use]
pub fn is_mldsa44_ed25519(self) -> bool {
matches!(self, Algorithm::MlDsa44Ed25519)
}

/// Is the algorithm the MLDSA87-P384 composite scheme?
#[cfg(all(feature = "unstable-mldsa", feature = "p384"))]
#[must_use]
pub fn is_mldsa87_p384(self) -> bool {
matches!(self, Algorithm::MlDsa87P384)
}

/// Return an error indicating this algorithm is unsupported.
#[allow(dead_code)]
pub(crate) fn unsupported_error(self) -> Error {
Expand Down Expand Up @@ -354,8 +381,10 @@ impl str::FromStr for Algorithm {
SSH_RSA => Ok(Algorithm::Rsa { hash: None }),
SK_ECDSA_SHA2_P256 => Ok(Algorithm::SkEcdsaSha2NistP256),
SK_SSH_ED25519 => Ok(Algorithm::SkEd25519),
#[cfg(feature = "unstable-mldsa-eddsa")]
#[cfg(all(feature = "unstable-mldsa", feature = "ed25519"))]
SSH_MLDSA44_ED25519 => Ok(Algorithm::MlDsa44Ed25519),
#[cfg(all(feature = "unstable-mldsa", feature = "p384"))]
SSH_MLDSA87_P384 => Ok(Algorithm::MlDsa87P384),
#[cfg(feature = "alloc")]
_ => Ok(Algorithm::Other(AlgorithmName::from_str(id)?)),
#[cfg(not(feature = "alloc"))]
Expand Down
20 changes: 20 additions & 0 deletions ssh-key/src/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,33 @@
//! signature (2484) = mldsa_sig (2420) || ed25519_sig (64)
//! ```
//!
//! For `ssh-mldsa87-p384`, ML-DSA-87 uses `COMPSIG-MLDSA87-ECDSA-P384-SHA512` as its label
//! and FIPS 204 context. ECDSA signs `M'` with P-384 and SHA-384. The uncompressed SEC1 point
//! `Q` has its own SSH string length prefix. The private scalar and both ECDSA signature
//! components use SSH `mpint` encoding.
//!
//! ```text
//! public key = mldsa_pk (2592) || string Q
//! private key = mldsa_seed (32) || mpint d
//! signature = mldsa_sig (4627) || mpint r || mpint s
//! ```
//!
//! [draft-ietf-sshm-composite-sigs]: https://www.ietf.org/archive/id/draft-ietf-sshm-composite-sigs-01.html

#[cfg(not(any(feature = "ed25519", feature = "p384")))]
compile_error!("the `unstable-mldsa` feature requires `ed25519` or `p384`");

#[cfg(feature = "ed25519")]
mod mldsa44_ed25519;
#[cfg(feature = "p384")]
mod mldsa87_p384;

#[cfg(feature = "ed25519")]
pub use mldsa44_ed25519::{
MlDsa44Ed25519Keypair, MlDsa44Ed25519PrivateKey, MlDsa44Ed25519PublicKey,
};
#[cfg(feature = "p384")]
pub use mldsa87_p384::{MlDsa87P384Keypair, MlDsa87P384PrivateKey, MlDsa87P384PublicKey};

use crate::{Error, Result};
use alloc::vec::Vec;
Expand Down
74 changes: 74 additions & 0 deletions ssh-key/src/composite/mldsa87_p384.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! ML-DSA-87/ECDSA P-384 composite keys and signatures.

mod keys;

pub use keys::{MlDsa87P384Keypair, MlDsa87P384PrivateKey, MlDsa87P384PublicKey};

use super::CompositeMessage;
use crate::{
Algorithm, Error, Result, Signature,
signature::{encode_ecdsa_components, p384_signature_from_openssh_bytes},
};
use alloc::vec::Vec;
use ml_dsa::{EncodedSignature, MlDsa87};
use p384::ecdsa;
use rand_core::TryCryptoRng;
use signature::{RandomizedSigner, Signer, Verifier};

const LABEL: &[u8] = b"COMPSIG-MLDSA87-ECDSA-P384-SHA512";

fn encode_signature(
(pq, classical): (ml_dsa::Signature<MlDsa87>, ecdsa::Signature),
) -> Result<Signature> {
// Each mpint has a four-byte length and at most one sign byte.
let mut bytes = Vec::with_capacity(size_of::<EncodedSignature<MlDsa87>>() + 2 * (4 + 1 + 48));
bytes.extend_from_slice(&pq.encode());
let (r, s) = classical.split_bytes();
encode_ecdsa_components(&mut bytes, &r, &s)?;
Signature::new(Algorithm::MlDsa87P384, bytes)
}

impl Signer<Signature> for MlDsa87P384Keypair {
fn try_sign(&self, msg: &[u8]) -> signature::Result<Signature> {
let message = CompositeMessage::new(LABEL, msg, &[])?;
let pq = ml_dsa::SigningKey::<MlDsa87>::from_seed(&self.private().mldsa);
let classical = ecdsa::SigningKey::from(&self.private().p384);
let signatures = message.sign(pq.expanded_key(), &classical)?;
Ok(encode_signature(signatures)?)
}
}

impl RandomizedSigner<Signature> for MlDsa87P384Keypair {
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
&self,
rng: &mut R,
msg: &[u8],
) -> signature::Result<Signature> {
let message = CompositeMessage::new(LABEL, msg, &[])?;
let pq = ml_dsa::SigningKey::<MlDsa87>::from_seed(&self.private().mldsa);
let classical = ecdsa::SigningKey::from(&self.private().p384);
let signatures = message.sign_with_rng(pq.expanded_key(), &classical, rng)?;
Ok(encode_signature(signatures)?)
}
}

impl Verifier<Signature> for MlDsa87P384PublicKey {
fn verify(&self, msg: &[u8], signature: &Signature) -> signature::Result<()> {
if signature.algorithm() != self.algorithm() {
return Err(Error::Signature.into());
}
let (pq, classical) = signature
.as_bytes()
.split_at_checked(size_of::<EncodedSignature<MlDsa87>>())
.ok_or(Error::Signature)?;
let pq = ml_dsa::Signature::<MlDsa87>::try_from(pq).map_err(|_| Error::Signature)?;
let classical = p384_signature_from_openssh_bytes(classical)?;
let message = CompositeMessage::new(LABEL, msg, &[])?;
Ok(message.verify(
&ml_dsa::VerifyingKey::<MlDsa87>::decode(&self.mldsa),
&pq,
|| Ok(ecdsa::VerifyingKey::from_sec1_point(&self.p384)?),
&classical,
)?)
}
}
Loading