Skip to content

Commit a76535f

Browse files
committed
Output buffer guards in Blake digests
1 parent 4d0f95f commit a76535f

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/Blake2bDigest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.bouncycastle.crypto.CryptoServicePurpose;
2727
import org.bouncycastle.crypto.CryptoServicesRegistrar;
2828
import org.bouncycastle.crypto.ExtendedDigest;
29+
import org.bouncycastle.crypto.OutputLengthException;
2930
import org.bouncycastle.util.Arrays;
3031
import org.bouncycastle.util.Longs;
3132
import org.bouncycastle.util.Pack;
@@ -428,6 +429,11 @@ public void update(byte[] message, int offset, int len)
428429
*/
429430
public int doFinal(byte[] out, int outOffset)
430431
{
432+
if (outOffset > (out.length - digestLength))
433+
{
434+
throw new OutputLengthException("output buffer too short");
435+
}
436+
431437
f0 = 0xFFFFFFFFFFFFFFFFL;
432438
t0 += bufferPos;
433439
if (bufferPos > 0 && t0 == 0)

core/src/main/java/org/bouncycastle/crypto/digests/Blake2sDigest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.bouncycastle.crypto.CryptoServicePurpose;
2727
import org.bouncycastle.crypto.CryptoServicesRegistrar;
2828
import org.bouncycastle.crypto.ExtendedDigest;
29+
import org.bouncycastle.crypto.OutputLengthException;
2930
import org.bouncycastle.util.Arrays;
3031
import org.bouncycastle.util.Integers;
3132
import org.bouncycastle.util.Pack;
@@ -455,6 +456,11 @@ public void update(byte[] message, int offset, int len)
455456
*/
456457
public int doFinal(byte[] out, int outOffset)
457458
{
459+
if (outOffset > (out.length - digestLength))
460+
{
461+
throw new OutputLengthException("output buffer too short");
462+
}
463+
458464
f0 = 0xFFFFFFFF;
459465
t0 += bufferPos;
460466
// bufferPos may be < 64, so (t0 == 0) does not work

core/src/main/java/org/bouncycastle/crypto/digests/Blake2xsDigest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import org.bouncycastle.crypto.CryptoServicePurpose;
12+
import org.bouncycastle.crypto.OutputLengthException;
1213
import org.bouncycastle.crypto.Xof;
1314
import org.bouncycastle.util.Arrays;
1415

@@ -268,6 +269,11 @@ public int doFinal(byte[] out, int outOff, int outLen)
268269
*/
269270
public int doOutput(byte[] out, int outOff, int outLen)
270271
{
272+
if (outOff > (out.length - outLen))
273+
{
274+
throw new OutputLengthException("output buffer too short");
275+
}
276+
271277
if (h0 == null)
272278
{
273279
h0 = new byte[hash.getDigestSize()];

core/src/main/java/org/bouncycastle/crypto/digests/Blake3Digest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.bouncycastle.crypto.CryptoServicePurpose;
77
import org.bouncycastle.crypto.CryptoServicesRegistrar;
88
import org.bouncycastle.crypto.ExtendedDigest;
9+
import org.bouncycastle.crypto.OutputLengthException;
910
import org.bouncycastle.crypto.Xof;
1011
import org.bouncycastle.crypto.params.Blake3Parameters;
1112
import org.bouncycastle.util.Arrays;
@@ -459,12 +460,6 @@ public int doFinal(final byte[] pOut,
459460
final int pOutOffset,
460461
final int pOutLen)
461462
{
462-
/* Reject if we are already outputting */
463-
if (outputting)
464-
{
465-
throw new IllegalStateException(ERR_OUTPUTTING);
466-
}
467-
468463
/* Build the required output */
469464
final int length = doOutput(pOut, pOutOffset, pOutLen);
470465

@@ -477,6 +472,11 @@ public int doOutput(final byte[] pOut,
477472
final int pOutOffset,
478473
final int pOutLen)
479474
{
475+
if (pOutOffset > (pOut.length - pOutLen))
476+
{
477+
throw new OutputLengthException("output buffer too short");
478+
}
479+
480480
/* If we have not started outputting yet */
481481
if (!outputting)
482482
{

0 commit comments

Comments
 (0)