From ea14fe42f35364f2785f9baf979366ed4aae6118 Mon Sep 17 00:00:00 2001 From: David Hook Date: Sun, 7 Feb 2016 22:46:57 +1100 Subject: Added support for repeated requests for output to Xof. --- crypto/src/crypto/IXof.cs | 25 +++++++++++++++++-------- crypto/src/crypto/digests/KeccakDigest.cs | 4 ++-- crypto/src/crypto/digests/ShakeDigest.cs | 16 +++++++++++++--- 3 files changed, 32 insertions(+), 13 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/crypto/IXof.cs b/crypto/src/crypto/IXof.cs index e9e2253a0..f76304d48 100644 --- a/crypto/src/crypto/IXof.cs +++ b/crypto/src/crypto/IXof.cs @@ -9,14 +9,23 @@ namespace Org.BouncyCastle.Crypto public interface IXof : IDigest { - /** - * Output the results of the final calculation for this digest to outLen number of bytes. - * - * @param out output array to write the output bytes to. - * @param outOff offset to start writing the bytes at. - * @param outLen the number of output bytes requested. - * @return the number of bytes written - */ + /// + /// Output the results of the final calculation for this digest to outLen number of bytes. + /// + /// output array to write the output bytes to. + /// offset to start writing the bytes at. + /// the number of output bytes requested. + /// the number of bytes written int DoFinal(byte[] output, int outOff, int outLen); + + /// + /// Start outputting the results of the final calculation for this digest. Unlike DoFinal, this method + /// will continue producing output until the Xof is explicitly reset, or signals otherwise. + /// + /// output array to write the output bytes to. + /// offset to start writing the bytes at. + /// the number of output bytes requested. + /// the number of bytes written + int DoOutput(byte[] output, int outOff, int outLen); } } diff --git a/crypto/src/crypto/digests/KeccakDigest.cs b/crypto/src/crypto/digests/KeccakDigest.cs index 2d6cf393c..20aa225b8 100644 --- a/crypto/src/crypto/digests/KeccakDigest.cs +++ b/crypto/src/crypto/digests/KeccakDigest.cs @@ -248,11 +248,11 @@ namespace Org.BouncyCastle.Crypto.Digests if ((bitsInQueue % 8) != 0) { - throw new InvalidOperationException("attempt to absorb with odd length queue."); + throw new InvalidOperationException("attempt to absorb with odd length queue"); } if (squeezing) { - throw new InvalidOperationException("attempt to absorb while squeezing."); + throw new InvalidOperationException("attempt to absorb while squeezing"); } i = 0; diff --git a/crypto/src/crypto/digests/ShakeDigest.cs b/crypto/src/crypto/digests/ShakeDigest.cs index fd7d85681..a7bddccba 100644 --- a/crypto/src/crypto/digests/ShakeDigest.cs +++ b/crypto/src/crypto/digests/ShakeDigest.cs @@ -53,15 +53,25 @@ namespace Org.BouncyCastle.Crypto.Digests public virtual int DoFinal(byte[] output, int outOff, int outLen) { - Absorb(new byte[]{ 0x0F }, 0, 4); - - Squeeze(output, outOff, ((long)outLen) * 8); + DoOutput(output, outOff, outLen); Reset(); return outLen; } + public virtual int DoOutput(byte[] output, int outOff, int outLen) + { + if (!squeezing) + { + Absorb(new byte[] { 0x0F }, 0, 4); + } + + Squeeze(output, outOff, ((long)outLen) * 8); + + return outLen; + } + /* * TODO Possible API change to support partial-byte suffixes. */ -- cgit 1.5.1