Skip to content

Commit 0979953

Browse files
committed
Blake2s refactoring
1 parent 1b66ed8 commit 0979953

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

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

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.bouncycastle.crypto.CryptoServicesRegistrar;
2828
import org.bouncycastle.crypto.ExtendedDigest;
2929
import org.bouncycastle.util.Arrays;
30+
import org.bouncycastle.util.Integers;
3031
import org.bouncycastle.util.Pack;
3132

3233
/**
@@ -285,41 +286,36 @@ private void init(byte[] salt, byte[] personalization, byte[] key)
285286

286287
if (key != null && key.length > 0)
287288
{
288-
if (key.length > 32)
289+
keyLength = key.length;
290+
if (keyLength > 32)
289291
{
290-
throw new IllegalArgumentException(
291-
"Keys > 32 bytes are not supported");
292+
throw new IllegalArgumentException("Keys > 32 bytes are not supported");
292293
}
293-
this.key = new byte[key.length];
294-
System.arraycopy(key, 0, this.key, 0, key.length);
295-
296-
keyLength = key.length;
297-
System.arraycopy(key, 0, buffer, 0, key.length);
294+
this.key = new byte[keyLength];
295+
System.arraycopy(key, 0, this.key, 0, keyLength);
296+
System.arraycopy(key, 0, buffer, 0, keyLength);
298297
bufferPos = BLOCK_LENGTH_BYTES; // zero padding
299298
}
300299

301300
if (chainValue == null)
302301
{
303302
chainValue = new int[8];
304303

305-
chainValue[0] = blake2s_IV[0]
306-
^ (digestLength | (keyLength << 8) | ((fanout << 16) | (depth << 24)));
304+
chainValue[0] = blake2s_IV[0] ^ (digestLength | (keyLength << 8) | ((fanout << 16) | (depth << 24)));
307305
chainValue[1] = blake2s_IV[1] ^ leafLength;
308306

309307
int nofHi = (int)(nodeOffset >> 32);
310308
int nofLo = (int)nodeOffset;
311309
chainValue[2] = blake2s_IV[2] ^ nofLo;
312-
chainValue[3] = blake2s_IV[3] ^ (nofHi |
313-
(nodeDepth << 16) | (innerHashLength << 24));
310+
chainValue[3] = blake2s_IV[3] ^ (nofHi | (nodeDepth << 16) | (innerHashLength << 24));
314311

315312
chainValue[4] = blake2s_IV[4];
316313
chainValue[5] = blake2s_IV[5];
317314
if (salt != null)
318315
{
319316
if (salt.length != 8)
320317
{
321-
throw new IllegalArgumentException(
322-
"Salt length must be exactly 8 bytes");
318+
throw new IllegalArgumentException("Salt length must be exactly 8 bytes");
323319
}
324320
this.salt = new byte[8];
325321
System.arraycopy(salt, 0, this.salt, 0, salt.length);
@@ -334,12 +330,10 @@ private void init(byte[] salt, byte[] personalization, byte[] key)
334330
{
335331
if (personalization.length != 8)
336332
{
337-
throw new IllegalArgumentException(
338-
"Personalization length must be exactly 8 bytes");
333+
throw new IllegalArgumentException("Personalization length must be exactly 8 bytes");
339334
}
340335
this.personalization = new byte[8];
341-
System.arraycopy(personalization, 0, this.personalization, 0,
342-
personalization.length);
336+
System.arraycopy(personalization, 0, this.personalization, 0, personalization.length);
343337

344338
chainValue[6] ^= Pack.littleEndianToInt(personalization, 0);
345339
chainValue[7] ^= Pack.littleEndianToInt(personalization, 4);
@@ -542,18 +536,13 @@ private void compress(byte[] message, int messagePos)
542536
private void G(int m1, int m2, int posA, int posB, int posC, int posD)
543537
{
544538
internalState[posA] = internalState[posA] + internalState[posB] + m1;
545-
internalState[posD] = rotr32(internalState[posD] ^ internalState[posA], 16);
539+
internalState[posD] = Integers.rotateRight(internalState[posD] ^ internalState[posA], 16);
546540
internalState[posC] = internalState[posC] + internalState[posD];
547-
internalState[posB] = rotr32(internalState[posB] ^ internalState[posC], 12);
541+
internalState[posB] = Integers.rotateRight(internalState[posB] ^ internalState[posC], 12);
548542
internalState[posA] = internalState[posA] + internalState[posB] + m2;
549-
internalState[posD] = rotr32(internalState[posD] ^ internalState[posA], 8);
543+
internalState[posD] = Integers.rotateRight(internalState[posD] ^ internalState[posA], 8);
550544
internalState[posC] = internalState[posC] + internalState[posD];
551-
internalState[posB] = rotr32(internalState[posB] ^ internalState[posC], 7);
552-
}
553-
554-
private int rotr32(int x, int rot)
555-
{
556-
return x >>> rot | (x << (32 - rot));
545+
internalState[posB] = Integers.rotateRight(internalState[posB] ^ internalState[posC], 7);
557546
}
558547

559548
/**

0 commit comments

Comments
 (0)