Skip to content

Commit 73345af

Browse files
lomigmegardpeterdettman
authored andcommitted
Blake2bDigest: defensive improvements and cleanup
Defensive changes: - Validate key length before copying to fail fast and avoid unnecessary allocation on invalid input - Add null check before zeroing chainValue in reset() - Centralize state cleanup in reset() to avoid double-zeroing and ensure consistent cleanup path Code quality: - Use explicit 64-bit literals for long fields (f1) for clarity - Add final modifier to ROUNDS constant - Remove redundant return statement Minor fixes: - Fix typo in Javadoc (Blakbe2b -> Blake2b) - Use more efficient Pack.longToLittleEndian overload for partial digest output, avoiding intermediate byte[] allocation
1 parent 98d81fc commit 73345af

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434

3535
/**
36-
* Implementation of the cryptographic hash function Blakbe2b.
36+
* Implementation of the cryptographic hash function Blake2b.
3737
* <p>
3838
* Blake2b offers a built-in keying mechanism to be used directly
3939
* for authentication ("Prefix-MAC") rather than a HMAC construction.
@@ -74,7 +74,7 @@ public class Blake2bDigest
7474
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}
7575
};
7676

77-
private static int ROUNDS = 12; // to use for Catenas H'
77+
private final static int ROUNDS = 12; // to use for Catenas H'
7878
private final static int BLOCK_LENGTH_BYTES = 128;// bytes
7979

8080
// General parameters:
@@ -195,14 +195,13 @@ public Blake2bDigest(byte[] key, CryptoServicePurpose purpose)
195195
buffer = new byte[BLOCK_LENGTH_BYTES];
196196
if (key != null)
197197
{
198-
this.key = new byte[key.length];
199-
System.arraycopy(key, 0, this.key, 0, key.length);
200-
201198
if (key.length > 64)
202199
{
203200
throw new IllegalArgumentException(
204201
"Keys > 64 are not supported");
205202
}
203+
this.key = new byte[key.length];
204+
System.arraycopy(key, 0, this.key, 0, key.length);
206205
keyLength = key.length;
207206
System.arraycopy(key, 0, buffer, 0, key.length);
208207
bufferPos = BLOCK_LENGTH_BYTES; // zero padding
@@ -264,14 +263,13 @@ public Blake2bDigest(byte[] key, int digestLength, byte[] salt, byte[] personali
264263
}
265264
if (key != null)
266265
{
267-
this.key = new byte[key.length];
268-
System.arraycopy(key, 0, this.key, 0, key.length);
269-
270266
if (key.length > 64)
271267
{
272268
throw new IllegalArgumentException(
273269
"Keys > 64 are not supported");
274270
}
271+
this.key = new byte[key.length];
272+
System.arraycopy(key, 0, this.key, 0, key.length);
275273
keyLength = key.length;
276274
System.arraycopy(key, 0, buffer, 0, key.length);
277275
bufferPos = BLOCK_LENGTH_BYTES; // zero padding
@@ -386,7 +384,6 @@ public void update(byte b)
386384
{
387385
buffer[bufferPos] = b;
388386
bufferPos++;
389-
return;
390387
}
391388
}
392389

@@ -471,28 +468,23 @@ public int doFinal(byte[] out, int outOffset)
471468
f0 = 0xFFFFFFFFFFFFFFFFL;
472469
if(isLastNode)
473470
{
474-
f1 = 0xFFFFFFFF;
471+
f1 = 0xFFFFFFFFFFFFFFFFL;
475472
}
476473
t0 += bufferPos;
477474
if (bufferPos > 0 && t0 == 0)
478475
{
479476
t1++;
480477
}
481478
compress(buffer, 0);
482-
Arrays.fill(buffer, (byte)0);// Holds eventually the key if input is null
483479
Arrays.fill(internalState, 0L);
484480

485481
int full = digestLength >>> 3, partial = digestLength & 7;
486482
Pack.longToLittleEndian(chainValue, 0, full, out, outOffset);
487483
if (partial > 0)
488484
{
489-
byte[] bytes = new byte[8];
490-
Pack.longToLittleEndian(chainValue[full], bytes, 0);
491-
System.arraycopy(bytes, 0, out, outOffset + digestLength - partial, partial);
485+
Pack.longToLittleEndian(chainValue[full], out, outOffset + digestLength - partial, partial);
492486
}
493487

494-
Arrays.fill(chainValue, 0L);
495-
496488
reset();
497489

498490
return digestLength;
@@ -507,11 +499,15 @@ public void reset()
507499
{
508500
bufferPos = 0;
509501
f0 = 0L;
510-
f1 = 0;
502+
f1 = 0L;
511503
t0 = 0L;
512504
t1 = 0L;
513505
isLastNode = false;
514-
chainValue = null;
506+
if (chainValue != null)
507+
{
508+
Arrays.fill(chainValue, 0L);
509+
chainValue = null;
510+
}
515511
Arrays.fill(buffer, (byte)0);
516512
if (key != null)
517513
{

0 commit comments

Comments
 (0)