Skip to content

Commit 41478e3

Browse files
committed
Let Kangaroo produce its output in more than one piece: switchToSqueezing() now records that it has run, so repeated doOutput() and doFinal() after doOutput() work, incorporating the doFinal() guard removal from github PR #2409, relates to github #2409.
1 parent d143504 commit 41478e3

4 files changed

Lines changed: 79 additions & 7 deletions

File tree

‎CONTRIBUTORS.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
<li>fgrieu &lt;fgrieu&#064gmail.com&gt; identification and suggested fixes for possible timing vulnerability in OAEPEncoding and RSACoreEngine.</li>
458458
<li>MTG &lt;https://github.com/mtgag&gt; patch for decoding issues in PKIPublicationInfo and CertifiedKeyPair, patch for adding jurisdiction{C,ST,L} to X500 name style.</li>
459459
<li>Andreas Gadermaier &lt;up.gadermaier&#064gmail.com&gt; initial version of Argon2 PBKDF algorithm.</li>
460-
<li>Tony Washer &lt;https://github.com/tonywasher&gt; ECIESKeyEncapsulation fix for use of OldCofactor mode. Submitted ChaCha20Poly1305 prototype. Remove support for maxXofLen in Kangaroo. Police Blake3 output limit. Add LEAEngine. Review of qTesla, Java 1.9 module code, additional test code and debugging for GOST, DSTU, and ECNR algorithms. Initial lightweight implementation of the ZUC ciphers and macs. Additions to LMS/HSS API implementations, fix for truncation issue with big HSS keys, contributions to optimization of LMS/HSS. Patch for XDH/EdDSA key handling and mcEliece decryption using kobaraImai. Initial GCM-SIV, Blake3, and Kangaroo implementation. Corrections to length outputs for getUpdateOutputSize()/doFinal() in ISAP, PhotonBeetle, and Xoodyak. Fix GCFB reset. Fix Elephant multi-part process. Fix AsconXof support multi-part outputs. Reset KGCM associated text on re-init. Support variable-length associated data and short nonces in KCCM.</li>
460+
<li>Tony Washer &lt;https://github.com/tonywasher&gt; ECIESKeyEncapsulation fix for use of OldCofactor mode. Submitted ChaCha20Poly1305 prototype. Remove support for maxXofLen in Kangaroo. Police Blake3 output limit. Add LEAEngine. Review of qTesla, Java 1.9 module code, additional test code and debugging for GOST, DSTU, and ECNR algorithms. Initial lightweight implementation of the ZUC ciphers and macs. Additions to LMS/HSS API implementations, fix for truncation issue with big HSS keys, contributions to optimization of LMS/HSS. Patch for XDH/EdDSA key handling and mcEliece decryption using kobaraImai. Initial GCM-SIV, Blake3, and Kangaroo implementation. Corrections to length outputs for getUpdateOutputSize()/doFinal() in ISAP, PhotonBeetle, and Xoodyak. Fix GCFB reset. Fix Elephant multi-part process. Fix AsconXof support multi-part outputs. Reset KGCM associated text on re-init. Support variable-length associated data and short nonces in KCCM. Fix Kangaroo to support multi-part output (PR #2409).</li>
461461
<li>Vincent Bouckaert &lt;https://github.com/veebee&gt; initial version of RFC 4998 ASN.1 classes. Debugging and testing of high level RFC 4998 implementation.</li>
462462
<li>Aurimas Liutikas &lt;https://github.com/liutikas&gt; JavaDoc patches to ReasonsMask.</li>
463463
<li>Gabriel Sroka &lt;https://github.com/gabrielsroka&gt; corrected comments in RSA validation.</li>

‎core/src/main/java/org/bouncycastle/crypto/digests/Kangaroo.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,6 @@ public int doFinal(final byte[] pOut,
312312
final int pOutOffset,
313313
final int pOutLen)
314314
{
315-
/* Check that we are not already outputting */
316-
if (squeezing)
317-
{
318-
throw new IllegalStateException("Already outputting");
319-
}
320-
321315
/* Build the required output */
322316
final int length = doOutput(pOut, pOutOffset, pOutLen);
323317

@@ -467,6 +461,11 @@ private void switchToSqueezing()
467461
{
468462
switchFinal();
469463
}
464+
465+
/* Record that we are now squeezing. Without this doOutput() re-runs this method on
466+
* every call and the second one fails absorbing into an already-squeezing sponge, so
467+
* neither repeated doOutput() nor doFinal() after doOutput() was possible. */
468+
squeezing = true;
470469
}
471470

472471
/**

‎core/src/test/java/org/bouncycastle/crypto/test/KangarooTest.java‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.bouncycastle.crypto.test;
22

3+
import org.bouncycastle.crypto.Xof;
34
import org.bouncycastle.crypto.digests.Kangaroo.KangarooParameters;
45
import org.bouncycastle.crypto.digests.Kangaroo.KangarooTwelve;
6+
import org.bouncycastle.crypto.digests.Kangaroo.MarsupilamiFourteen;
57
import org.bouncycastle.util.Arrays;
68
import org.bouncycastle.util.encoders.Hex;
79
import org.bouncycastle.util.test.SimpleTest;
@@ -36,6 +38,76 @@ public void performTest()
3638
throws Exception
3739
{
3840
new Kangaroo12Test().checkDigests(this);
41+
42+
testMultiPartOutput("KangarooTwelve", new KangarooTwelve(), new KangarooTwelve());
43+
testMultiPartOutput("MarsupilamiFourteen", new MarsupilamiFourteen(), new MarsupilamiFourteen());
44+
testAbsorbAfterSqueezeRejected("KangarooTwelve", new KangarooTwelve());
45+
testAbsorbAfterSqueezeRejected("MarsupilamiFourteen", new MarsupilamiFourteen());
46+
}
47+
48+
/**
49+
* A squeeze split across several doOutput() calls, terminated by doFinal(), has to produce the
50+
* same stream as a single doFinal() of the total length - the behaviour SHAKE, Blake3 and
51+
* AsconXof already have. Kangaroo used to reject the second call outright, because
52+
* switchToSqueezing() never recorded that it had run (github PR #2409).
53+
*
54+
* @param pName the digest name
55+
* @param pSingle the digest used for the one-shot result
56+
* @param pSplit the digest used for the split result
57+
*/
58+
void testMultiPartOutput(final String pName,
59+
final Xof pSingle,
60+
final Xof pSplit)
61+
{
62+
final byte[] myMsg = new byte[137];
63+
buildStdBuffer(myMsg);
64+
65+
/* One-shot: a single doFinal() covering the whole stream */
66+
final byte[] myExpected = new byte[64];
67+
pSingle.update(myMsg, 0, myMsg.length);
68+
isTrue(pName + ": one-shot length", 64 == pSingle.doFinal(myExpected, 0, 64));
69+
70+
/* Split: two doOutput() calls, then a doFinal() for the tail */
71+
final byte[] myActual = new byte[64];
72+
pSplit.update(myMsg, 0, myMsg.length);
73+
pSplit.doOutput(myActual, 0, 16);
74+
pSplit.doOutput(myActual, 16, 16);
75+
isTrue(pName + ": doFinal length", 32 == pSplit.doFinal(myActual, 32, 32));
76+
77+
isTrue(pName + ": split output mismatch", Arrays.areEqual(myExpected, myActual));
78+
79+
/* doFinal() resets, so the digest is reusable and repeats the one-shot result */
80+
final byte[] myReused = new byte[64];
81+
pSplit.update(myMsg, 0, myMsg.length);
82+
pSplit.doFinal(myReused, 0, 64);
83+
84+
isTrue(pName + ": not reset by doFinal", Arrays.areEqual(myExpected, myReused));
85+
}
86+
87+
/**
88+
* Absorbing more data once squeezing has started stays refused.
89+
*
90+
* @param pName the digest name
91+
* @param pDigest the digest
92+
*/
93+
void testAbsorbAfterSqueezeRejected(final String pName,
94+
final Xof pDigest)
95+
{
96+
final byte[] myMsg = new byte[64];
97+
buildStdBuffer(myMsg);
98+
99+
pDigest.update(myMsg, 0, myMsg.length);
100+
pDigest.doOutput(new byte[32], 0, 32);
101+
102+
try
103+
{
104+
pDigest.update(myMsg, 0, myMsg.length);
105+
fail(pName + ": absorb after squeeze was accepted");
106+
}
107+
catch (IllegalStateException e)
108+
{
109+
isTrue(pName + ": wrong message", "attempt to absorb while squeezing".equals(e.getMessage()));
110+
}
39111
}
40112

41113
/**

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ <h3>2.1.2 Defects Fixed</h3>
152152
<li>Signature.verify() on a malformed LMS signature threw an unchecked exception out of the lightweight API rather than reporting the problem the way the JCA does, so an empty, truncated or otherwise damaged signature reached the caller as IllegalStateException("cannot parse signature"), and one naming an OTS type other than the verifying key's as IllegalArgumentException. LMSSignatureSpi.engineVerify() now answers false for a signature it cannot parse, as ML-DSA, SLH-DSA, XMSS and XMSS^MT already did, and throws SignatureException for one it can parse but cannot process, which is the OTS type mismatch. Either way the accumulated message is cleared, so the signature object stays usable afterwards (github #2408).</li>
153153
<li>An XMSS signature carrying trailing data was accepted as valid. XMSSSignature.Builder.withSignature() read the index, randomness, WOTS+ signature and authentication path at their fixed offsets and ignored anything past them, so appending arbitrary bytes to a valid signature produced a second, different encoding that still verified. RFC 8391 sec. 4.1.8 fixes an XMSS signature at 4 + n + (len + h) * n bytes, and the builder now rejects any other length the way XMSSMTSignature always has, which also covers the truncated case (github #2408).</li>
154154
<li>KeyPairGenerator.getInstance("XMSSMT").generateKeyPair() without a preceding initialize() threw IllegalArgumentException("layers must divide totalHeight without remainder"): the parameters used when uninitialised were height 10 with 20 layers, which is not a constructible XMSS^MT parameter set, leaving the default unusable. The default is now XMSSMT-SHA2_20/2_512 (RFC 8391 sec. 5.4). The same path in both the XMSS and XMSS^MT generators also left the returned key without a tree digest, so equals(), hashCode() and getTreeDigest() threw NullPointerException on a default-generated key; both now set it (github #2408).</li>
155+
<li>Kangaroo (KangarooTwelve / MarsupilamiFourteen) could not produce its output in more than one piece. KangarooBase.switchToSqueezing() never recorded that it had run, so doOutput() re-entered it on every call and the second one failed trying to absorb into an already-squeezing sponge - neither repeated doOutput() nor a doFinal() closing out a squeeze already begun with doOutput() was possible, both of which SHAKE, Blake3 and AsconXof support. The squeezing state is now recorded, and the guard in doFinal() that rejected the pattern - dead code, since the flag it tested was never set - has been removed. Absorbing after a squeeze has begun is still refused (github PR #2409).</li>
155156
</ul>
156157

157158
<h3>2.1.3 Additional Features and Functionality</h3>

0 commit comments

Comments
 (0)