Expand description
This implements the HashML-DSA algorithm specified in FIPS 204 which is useful for cases
it is necessary to process the message to be signed in chunks, and it is not possible to use the external mu
mode of MLDSA; possibly because it is necessary to digest the message before knowing which public key
will sign it.
HashML-DSA is a full signature algorithm implementing the Signer and SignatureVerifier traits:
use bouncycastle_core::errors::SignatureError;
use bouncycastle_core::traits::{Signer, SignatureVerifier};
use bouncycastle_mldsa_lowmemory::{MLDSATrait, HashMLDSA65_with_SHA512, HashMLDSA44_with_SHA512};
let msg = b"The quick brown fox jumped over the lazy dog";
let (pk, sk) = HashMLDSA65_with_SHA512::keygen().unwrap();
let sig = HashMLDSA65_with_SHA512::sign(&sk, msg, None).unwrap();
// This is the signature value that can be saved to a file or whatever it is needed.
match HashMLDSA65_with_SHA512::verify(&pk, msg, None, &sig) {
Ok(()) => println!("Signature is valid!"),
Err(SignatureError::SignatureVerificationFailed) => println!("Signature is invalid!"),
Err(e) => panic!("Something else went wrong: {:?}", e),
}There is also access to the pre-hashed function available from PHSigner and PHSignatureVerifier:
use bouncycastle_core::errors::SignatureError;
use bouncycastle_core::traits::{
Hash, PHSignatureVerifier, PHSigner, SignatureVerifier, Signer,
};
use bouncycastle_sha2::SHA512;
use bouncycastle_mldsa_lowmemory::{MLDSATrait, HashMLDSA65_with_SHA512, HashMLDSA44_with_SHA512};
let msg = b"The quick brown fox jumped over the lazy dog";
// Here, and in contrast to External Mu mode of ML-DSA, the message can be pre-hashed before
// generating the signing key.
let ph: [u8; 64] = SHA512::default().hash(msg).as_slice().try_into().unwrap();
let (pk, sk) = HashMLDSA65_with_SHA512::keygen().unwrap();
let sig = HashMLDSA65_with_SHA512::sign_ph(&sk, &ph, None).unwrap();
// This is the signature value that can be saved to a file or whatever it is need.
// This verifies either through the usual one-shot API of the [SignatureVerifier] trait
match HashMLDSA65_with_SHA512::verify(&pk, msg, None, &sig) {
Ok(()) => println!("Signature is valid!"),
Err(SignatureError::SignatureVerificationFailed) => println!("Signature is invalid!"),
Err(e) => panic!("Something else went wrong: {:?}", e),
}
// Or though the verify_ph of the [PHSignatureVerifier] trait
match HashMLDSA65_with_SHA512::verify_ph(&pk, &ph, None, &sig) {
Ok(()) => println!("Signature is valid!"),
Err(SignatureError::SignatureVerificationFailed) => println!("Signature is invalid!"),
Err(e) => panic!("Something else went wrong: {:?}", e),
}Note that the HashMLDSA object is just a light wrapper around MLDSA, and, for example, they share key types.
Thereofre, if the user needs the more sophisticated keygen functions, they should just use them from MLDSA.
But a simple HashMLDSA::keygen is provided.
Structs§
- HashMLDSA
- An instance of the HashML-DSA algorithm.
Constants§
- HASH_
ML_ DSA_ 44_ with_ SHA256_ NAME - HASH_
ML_ DSA_ 44_ with_ SHA512_ NAME - HASH_
ML_ DSA_ 65_ WITH_ SHA256_ NAME - HASH_
ML_ DSA_ 65_ WITH_ SHA512_ NAME - HASH_
ML_ DSA_ 87_ WITH_ SHA512_ NAME - HASH_
ML_ DSA_ 87_ with_ SHA256_ NAME
Type Aliases§
- HashMLDS
A44_ with_ SHA256 - The HashML-DSA-44_with_SHA512 signature algorithm.
- HashMLDS
A44_ with_ SHA512 - The HashML-DSA-44_with_SHA512 signature algorithm.
- HashMLDS
A65_ with_ SHA256 - The HashML-DSA-65_with_SHA256 signature algorithm.
- HashMLDS
A65_ with_ SHA512 - The HashML-DSA-65_with_SHA512 signature algorithm.
- HashMLDS
A87_ with_ SHA256 - The HashML-DSA-87_with_SHA256 signature algorithm.
- HashMLDS
A87_ with_ SHA512 - The HashML-DSA-87_with_SHA512 signature algorithm.