Class SphincsPlusSignatureContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, SignatureContext, TagEngine<Signature>
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
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionSphincsPlusSignatureContext(CryptoAlgorithm algorithm, PrivateKey privateKey) Creates a SPHINCS+ signing context bound to a private key.SphincsPlusSignatureContext(CryptoAlgorithm algorithm, PublicKey publicKey) Creates a SPHINCS+ verification context bound to a public key. -
Method Summary
Modifier and TypeMethodDescriptionReturns the algorithm associated with this context.voidclose()Closes this context and releases underlying resources.Returns the core verification predicate in use.key()Returns the key bound to this context.voidsetExpectedTag(byte[] expected) Supplies the expected signature for verification mode.voidSets the verification approach to compare computed and expected signatures.intReturns the length of the signature (tag) produced or expected.wrap(InputStream upstream) Wraps an input stream to produce or verify a SPHINCS+ signature as data is consumed.
-
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 benullprivateKey- a SPHINCS+ private key; must not benull- 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 benullpublicKey- a SPHINCS+ public key; must not benull- Throws:
GeneralSecurityException- if the key is invalid or the signature length cannot be determined
-
-
Method Details
-
algorithm
Returns the algorithm associated with this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the parent
CryptoAlgorithm
-
key
Returns the key bound to this context.- Specified by:
keyin interfaceCryptoContext- Returns:
- the signing
PrivateKeyor verificationPublicKey
-
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
wrap
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:
wrapin interfaceTagEngine<Signature>- Parameters:
upstream- the input stream providing raw data; must not benull- Returns:
- a wrapped input stream that updates the signature engine
- Throws:
IOException- if wrapping fails
-
tagLength
-
setExpectedTag
public void setExpectedTag(byte[] expected) Supplies the expected signature for verification mode.- Specified by:
setExpectedTagin interfaceTagEngine<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:
setVerificationApproachin interfaceTagEngine<Signature>- Parameters:
strategy- verification predicate; must not benull
-
getVerificationCore
Returns the core verification predicate in use.- Specified by:
getVerificationCorein interfaceTagEngine<Signature>- Returns:
- the verification predicate
-