Skip to main content

Module hash_mldsa

Module hash_mldsa 

Source
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§

HashMLDSA44_with_SHA256
The HashML-DSA-44_with_SHA512 signature algorithm.
HashMLDSA44_with_SHA512
The HashML-DSA-44_with_SHA512 signature algorithm.
HashMLDSA65_with_SHA256
The HashML-DSA-65_with_SHA256 signature algorithm.
HashMLDSA65_with_SHA512
The HashML-DSA-65_with_SHA512 signature algorithm.
HashMLDSA87_with_SHA256
The HashML-DSA-87_with_SHA256 signature algorithm.
HashMLDSA87_with_SHA512
The HashML-DSA-87_with_SHA512 signature algorithm.