Class SphincsPlusSignatureContext

java.lang.Object
zeroecho.core.alg.sphincsplus.SphincsPlusSignatureContext
All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, SignatureContext, TagEngine<Signature>

public final class SphincsPlusSignatureContext extends Object implements SignatureContext
SPHINCS+ signature context that adapts a JCA engine for streaming sign and verify.

This context uses the Bouncy Castle provider to obtain a JCA Signature instance for the "SLH-DSA" algorithm and exposes it via the SignatureContext and TagEngine contracts. In produce (SIGN) mode, the wrapped stream emits the original data followed by a detached signature trailer. In verify (VERIFY) mode, the wrapped stream emits only the body and compares the computed signature at EOF against a caller-supplied expected value.

Delegation

The implementation delegates to GenericJcaSignatureContext for the streaming mechanics and JCA interaction, including trailer handling and verification strategy configuration.

Signature length

SPHINCS+ signatures have fixed byte lengths that depend on the parameter set attached to the key. When constructed with a public key, this context resolves the expected signature length from SLHDSAParameterSpec on the key.

Canonical sizes (bytes)

  • 128s: 7856, 128f: 17088
  • 192s: 16224, 192f: 35664
  • 256s: 29792, 256f: 49856

Examples

Signing pipeline


TagEngine<java.security.Signature> eng =
    new SphincsPlusSignatureContext(alg, privateKey);

try (java.io.InputStream in = eng.wrap(upstream)) {
    in.transferTo(out); // body, then SPHINCS+ signature trailer
}

Verification pipeline (detached)


byte[] expectedSig = ...; // obtained via a trusted channel

TagEngine<java.security.Signature> eng =
    new SphincsPlusSignatureContext(alg, publicKey);

// Optional: throw on mismatch at EOF
eng.setVerificationApproach(eng.getVerificationCore().getThrowOnMismatch());
eng.setExpectedTag(expectedSig);

try (java.io.InputStream in = eng.wrap(bodyWithoutTrailer)) {
    in.transferTo(java.io.OutputStream.nullOutputStream());
}

Thread-safety

Instances are not thread-safe and are single-use within a pipeline. Use one instance per signing or verification stream.

Since:
1.0
  • Constructor Details

    • SphincsPlusSignatureContext

      public SphincsPlusSignatureContext(CryptoAlgorithm algorithm, PrivateKey privateKey) throws GeneralSecurityException
      Creates a SPHINCS+ signing context bound to a private key.

      The JCA engine is obtained from the "BC" provider using the "SLH-DSA" algorithm name. The signature length resolver probes the JCA engine to determine the produced signature size.

      Parameters:
      algorithm - the parent cryptographic algorithm abstraction; must not be null
      privateKey - a SPHINCS+ private key; must not be null
      Throws:
      GeneralSecurityException - if the JCA signature engine cannot be initialized
    • SphincsPlusSignatureContext

      public SphincsPlusSignatureContext(CryptoAlgorithm algorithm, PublicKey publicKey) throws GeneralSecurityException
      Creates a SPHINCS+ verification context bound to a public key.

      The expected signature length is derived from the public key's SLHDSAParameterSpec.

      Parameters:
      algorithm - the parent cryptographic algorithm abstraction; must not be null
      publicKey - a SPHINCS+ public key; must not be null
      Throws:
      GeneralSecurityException - if the key is invalid or the signature length cannot be determined
  • Method Details

    • algorithm

      public CryptoAlgorithm algorithm()
      Returns the algorithm associated with this context.
      Specified by:
      algorithm in interface CryptoContext
      Returns:
      the parent CryptoAlgorithm
    • key

      public Key key()
      Returns the key bound to this context.
      Specified by:
      key in interface CryptoContext
      Returns:
      the signing PrivateKey or verification PublicKey
    • close

      public void close()
      Closes this context and releases underlying resources.

      Once closed, the context should not be reused. If auditing is enabled, closing may also trigger key destruction attempts.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface CryptoContext
    • wrap

      public InputStream wrap(InputStream upstream) throws IOException
      Wraps an input stream to produce or verify a SPHINCS+ signature as data is consumed.

      For SIGN mode this feeds bytes into the signing engine and appends the signature at EOF. For VERIFY mode this feeds bytes into the verification engine and compares the computed signature at EOF against the expected value (supplied via setExpectedTag(byte[])).

      Specified by:
      wrap in interface TagEngine<Signature>
      Parameters:
      upstream - the input stream providing raw data; must not be null
      Returns:
      a wrapped input stream that updates the signature engine
      Throws:
      IOException - if wrapping fails
    • tagLength

      public int tagLength()
      Returns the length of the signature (tag) produced or expected.
      Specified by:
      tagLength in interface TagEngine<Signature>
      Returns:
      signature length in bytes
    • setExpectedTag

      public void setExpectedTag(byte[] expected)
      Supplies the expected signature for verification mode.
      Specified by:
      setExpectedTag in interface TagEngine<Signature>
      Parameters:
      expected - the expected signature bytes; implementations may defensively copy the array
    • setVerificationApproach

      public void setVerificationApproach(ThrowingBiPredicate.VerificationBiPredicate<Signature> strategy)
      Sets the verification approach to compare computed and expected signatures.
      Specified by:
      setVerificationApproach in interface TagEngine<Signature>
      Parameters:
      strategy - verification predicate; must not be null
    • getVerificationCore

      Returns the core verification predicate in use.
      Specified by:
      getVerificationCore in interface TagEngine<Signature>
      Returns:
      the verification predicate