Skip to content

Commit ea14fe4

Browse files
committed
Added support for repeated requests for output to Xof.
1 parent 250e01c commit ea14fe4

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

crypto/src/crypto/IXof.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ namespace Org.BouncyCastle.Crypto
99
public interface IXof
1010
: IDigest
1111
{
12-
/**
13-
* Output the results of the final calculation for this digest to outLen number of bytes.
14-
*
15-
* @param out output array to write the output bytes to.
16-
* @param outOff offset to start writing the bytes at.
17-
* @param outLen the number of output bytes requested.
18-
* @return the number of bytes written
19-
*/
12+
/// <summary>
13+
/// Output the results of the final calculation for this digest to outLen number of bytes.
14+
/// </summary>
15+
/// <param name="output">output array to write the output bytes to.</param>
16+
/// <param name="outOff">offset to start writing the bytes at.</param>
17+
/// <param name="outLen">the number of output bytes requested.</param>
18+
/// <returns>the number of bytes written</returns>
2019
int DoFinal(byte[] output, int outOff, int outLen);
20+
21+
/// <summary>
22+
/// Start outputting the results of the final calculation for this digest. Unlike DoFinal, this method
23+
/// will continue producing output until the Xof is explicitly reset, or signals otherwise.
24+
/// </summary>
25+
/// <param name="output">output array to write the output bytes to.</param>
26+
/// <param name="outOff">offset to start writing the bytes at.</param>
27+
/// <param name="outLen">the number of output bytes requested.</param>
28+
/// <returns>the number of bytes written</returns>
29+
int DoOutput(byte[] output, int outOff, int outLen);
2130
}
2231
}

crypto/src/crypto/digests/KeccakDigest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ protected virtual void Absorb(byte[] data, int off, long databitlen)
248248

249249
if ((bitsInQueue % 8) != 0)
250250
{
251-
throw new InvalidOperationException("attempt to absorb with odd length queue.");
251+
throw new InvalidOperationException("attempt to absorb with odd length queue");
252252
}
253253
if (squeezing)
254254
{
255-
throw new InvalidOperationException("attempt to absorb while squeezing.");
255+
throw new InvalidOperationException("attempt to absorb while squeezing");
256256
}
257257

258258
i = 0;

crypto/src/crypto/digests/ShakeDigest.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,25 @@ public override int DoFinal(byte[] output, int outOff)
5353

5454
public virtual int DoFinal(byte[] output, int outOff, int outLen)
5555
{
56-
Absorb(new byte[]{ 0x0F }, 0, 4);
57-
58-
Squeeze(output, outOff, ((long)outLen) * 8);
56+
DoOutput(output, outOff, outLen);
5957

6058
Reset();
6159

6260
return outLen;
6361
}
6462

63+
public virtual int DoOutput(byte[] output, int outOff, int outLen)
64+
{
65+
if (!squeezing)
66+
{
67+
Absorb(new byte[] { 0x0F }, 0, 4);
68+
}
69+
70+
Squeeze(output, outOff, ((long)outLen) * 8);
71+
72+
return outLen;
73+
}
74+
6575
/*
6676
* TODO Possible API change to support partial-byte suffixes.
6777
*/

crypto/test/src/crypto/test/ShakeDigestTest.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,50 @@ private void RunTestVector(TestVector v)
199199
//Console.Error.WriteLine(v.Algorithm + " " + v.Bits + "-bit test vector hash mismatch");
200200
//Console.Error.WriteLine(Hex.ToHexString(output).ToUpper());
201201
}
202+
203+
if (partialBits == 0)
204+
{
205+
d = CreateDigest(v.Algorithm);
206+
207+
m = v.Message;
208+
209+
d.BlockUpdate(m, 0, m.Length);
210+
d.DoOutput(output, 0, outLen / 2);
211+
d.DoOutput(output, outLen / 2, output.Length - outLen / 2);
212+
213+
if (!Arrays.AreEqual(expected, output))
214+
{
215+
Fail(v.Algorithm + " " + v.Bits + "-bit test vector extended hash mismatch");
216+
}
217+
218+
try
219+
{
220+
d.Update((byte)0x01);
221+
Fail("no exception");
222+
}
223+
catch (InvalidOperationException e)
224+
{
225+
if (!"attempt to absorb while squeezing".Equals(e.Message))
226+
{
227+
Fail("wrong exception");
228+
}
229+
}
230+
231+
d = CreateDigest(v.Algorithm);
232+
233+
m = v.Message;
234+
235+
d.BlockUpdate(m, 0, m.Length);
236+
d.DoOutput(output, 0, outLen / 2);
237+
d.DoFinal(output, outLen / 2, output.Length - outLen / 2);
238+
239+
if (!Arrays.AreEqual(expected, output))
240+
{
241+
Fail(v.Algorithm + " " + v.Bits + "-bit test vector extended doFinal hash mismatch");
242+
}
243+
244+
d.Update((byte)0x01); // this should be okay as we've reset on DoFinal()
245+
}
202246
}
203247

204248
private void SkipUntil(StreamReader r, string header)

0 commit comments

Comments
 (0)