|
1 | 1 | package org.bouncycastle.crypto.test; |
2 | 2 |
|
| 3 | +import org.bouncycastle.crypto.Xof; |
3 | 4 | import org.bouncycastle.crypto.digests.Kangaroo.KangarooParameters; |
4 | 5 | import org.bouncycastle.crypto.digests.Kangaroo.KangarooTwelve; |
| 6 | +import org.bouncycastle.crypto.digests.Kangaroo.MarsupilamiFourteen; |
5 | 7 | import org.bouncycastle.util.Arrays; |
6 | 8 | import org.bouncycastle.util.encoders.Hex; |
7 | 9 | import org.bouncycastle.util.test.SimpleTest; |
@@ -36,6 +38,76 @@ public void performTest() |
36 | 38 | throws Exception |
37 | 39 | { |
38 | 40 | 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 | + } |
39 | 111 | } |
40 | 112 |
|
41 | 113 | /** |
|
0 commit comments