Use ML-DSA-65 to sign a sequence of bytes. The recipient needs the original message, the detached signature and a trusted copy of the signer's public key.
uses
System.SysUtils, StreamSec.DSI.PKTypes, StreamSec.DSI.MLC, stAESCT;
function NewMlDsa65SigningKey(out aPublicKeyBytes: TBytes): iMLDSAPrivateKey; var lPublicKey: iMLDSAPublicKey; lPrivateKey: iMLDSAPrivateKey; begin MustGetDSIML.DSAKeyGen(mldsa65, lPublicKey, lPrivateKey); aPublicKeyBytes := lPublicKey.GetEncodedKey; Result := lPrivateKey; end;
Link StreamSec.DSI.MLC to register the implementation that MustGetDSIML returns. The sample links stAESCT for the random generator on CPUs without hardware AES. Generate random bytes and keys describes the AES provider choice.
Generate the key pair once for the signer. Keep the private key on the signing side. The function also returns the raw public key bytes for provisioning the verifier. Establish whose key those bytes represent through an authenticated channel.
function SignMlDsa65Message(const aSigningKey: iMLDSAPrivateKey; const aMessage, aContext: TBytes): TBytes; begin if (aSigningKey = nil) or (aSigningKey.GetKeySize <> mldsa65) then raise EArgumentException.Create('An ML-DSA-65 signing key is required'); Result := MustGetDSIML.DSASign(aSigningKey, aMessage, aContext); end;
Pass the original message bytes to DSASign. For text, agree on the text encoding and line endings before signing. The context is a separate byte string of up to 255 bytes. For example, an application can use the UTF-8 bytes of invoice-v1 for invoices and a different context for receipts. Pass nil when the protocol uses an empty context.
function VerifyMlDsa65Message(const aPublicKeyBytes, aMessage, aContext, aSignature: TBytes): Boolean; var lPool: iDSIML; lPublicKey: iMLDSAPublicKey; begin lPool := MustGetDSIML; lPublicKey := lPool.NewDSAPublicKey(mldsa65, aPublicKeyBytes); Result := lPool.DSAVerify(lPublicKey, aMessage, aContext, aSignature); end;
The verifier reconstructs the ML-DSA-65 public key from its 1952-byte raw encoding. Supply the context your application expects for this use. A valid signature returns True. Changing one message byte, using a different context or verifying with another signer's key returns False. A public key of the wrong length or a context longer than 255 bytes raises an exception.
For a file signature that carries signer and algorithm identifiers, see Sign a file and verify its detached CMS signature.
StreamSec.DSI.PKTypes · StreamSec.DSI.MLC · stAESCT