Skip to content

Commit d13ac14

Browse files
committed
Merge branch 'tonywasher-patches/xof/blake3' into master
2 parents 92cb742 + 555471b commit d13ac14

File tree

1 file changed

+67
-40
lines changed

1 file changed

+67
-40
lines changed

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

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Blake3 implementation.
1616
*/
1717
public class Blake3Digest
18-
implements ExtendedDigest, Memoable, Xof
18+
implements ExtendedDigest, Memoable, Xof
1919
{
2020
/**
2121
* Already outputting error.
@@ -45,27 +45,27 @@ public class Blake3Digest
4545
/**
4646
* ChunkStart Flag.
4747
*/
48-
private static final int CHUNKSTART = 1;
48+
private static final int CHUNKSTART = 1;
4949

5050
/**
5151
* ChunkEnd Flag.
5252
*/
53-
private static final int CHUNKEND = 2;
53+
private static final int CHUNKEND = 2;
5454

5555
/**
5656
* Parent Flag.
5757
*/
58-
private static final int PARENT = 4;
58+
private static final int PARENT = 4;
5959

6060
/**
6161
* Root Flag.
6262
*/
63-
private static final int ROOT = 8;
63+
private static final int ROOT = 8;
6464

6565
/**
6666
* KeyedHash Flag.
6767
*/
68-
private static final int KEYEDHASH = 16;
68+
private static final int KEYEDHASH = 16;
6969

7070
/**
7171
* DeriveContext Flag.
@@ -75,7 +75,7 @@ public class Blake3Digest
7575
/**
7676
* DeriveKey Flag.
7777
*/
78-
private static final int DERIVEKEY = 64;
78+
private static final int DERIVEKEY = 64;
7979

8080
/**
8181
* Chaining0 State Locations.
@@ -120,58 +120,58 @@ public class Blake3Digest
120120
/**
121121
* IV0 State Locations.
122122
*/
123-
private static final int IV0 = 8;
123+
private static final int IV0 = 8;
124124

125125
/**
126126
* IV1 State Location.
127127
*/
128-
private static final int IV1 = 9;
128+
private static final int IV1 = 9;
129129

130130
/**
131131
* IV2 State Location.
132132
*/
133-
private static final int IV2 = 10;
133+
private static final int IV2 = 10;
134134

135135
/**
136136
* IV3 State Location.
137137
*/
138-
private static final int IV3 = 11;
138+
private static final int IV3 = 11;
139139

140140
/**
141141
* Count0 State Location.
142142
*/
143-
private static final int COUNT0 = 12;
143+
private static final int COUNT0 = 12;
144144

145145
/**
146146
* Count1 State Location.
147147
*/
148-
private static final int COUNT1 = 13;
148+
private static final int COUNT1 = 13;
149149

150150
/**
151151
* DataLen State Location.
152152
*/
153-
private static final int DATALEN = 14;
153+
private static final int DATALEN = 14;
154154

155155
/**
156156
* Flags State Location.
157157
*/
158-
private static final int FLAGS = 15;
158+
private static final int FLAGS = 15;
159159

160160
/**
161161
* Message word permutations.
162162
*/
163-
private static final byte[] SIGMA = { 2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8 };
163+
private static final byte[] SIGMA = {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8};
164164

165165
/**
166166
* Rotation constants.
167167
*/
168-
private static final byte[] ROTATE = { 16, 12, 8, 7 };
168+
private static final byte[] ROTATE = {16, 12, 8, 7};
169169

170170
/**
171171
* Blake3 Initialization Vector.
172172
*/
173173
private static final int[] IV = {
174-
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
174+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
175175
};
176176

177177
/**
@@ -219,6 +219,11 @@ public class Blake3Digest
219219
*/
220220
private boolean outputting;
221221

222+
/**
223+
* How many more bytes can we output?
224+
*/
225+
private long outputAvailable;
226+
222227
/**
223228
* The current mode.
224229
*/
@@ -259,6 +264,7 @@ public Blake3Digest()
259264

260265
/**
261266
* Constructor.
267+
*
262268
* @param pDigestLen the default digestLength
263269
*/
264270
public Blake3Digest(final int pDigestLen)
@@ -269,6 +275,7 @@ public Blake3Digest(final int pDigestLen)
269275

270276
/**
271277
* Constructor.
278+
*
272279
* @param pSource the source digest.
273280
*/
274281
private Blake3Digest(final Blake3Digest pSource)
@@ -277,7 +284,7 @@ private Blake3Digest(final Blake3Digest pSource)
277284
theDigestLen = pSource.theDigestLen;
278285

279286
/* Initialise from source */
280-
reset((Memoable) pSource);
287+
reset((Memoable)pSource);
281288
}
282289

283290
public int getByteLength()
@@ -297,6 +304,7 @@ public int getDigestSize()
297304

298305
/**
299306
* Initialise.
307+
*
300308
* @param pParams the parameters.
301309
*/
302310
public void init(final Blake3Parameters pParams)
@@ -313,7 +321,7 @@ public void init(final Blake3Parameters pParams)
313321
{
314322
/* Initialise with the key */
315323
initKey(myKey);
316-
Arrays.fill(myKey, (byte) 0);
324+
Arrays.fill(myKey, (byte)0);
317325

318326
/* else if we have a context */
319327
}
@@ -355,7 +363,7 @@ public void update(final byte b)
355363
compressBlock(theBuffer, 0);
356364

357365
/* Reset the buffer */
358-
Arrays.fill(theBuffer, (byte) 0);
366+
Arrays.fill(theBuffer, (byte)0);
359367
thePos = 0;
360368
}
361369

@@ -404,7 +412,7 @@ public void update(final byte[] pMessage,
404412

405413
/* Reset the buffer */
406414
thePos = 0;
407-
Arrays.fill(theBuffer, (byte) 0);
415+
Arrays.fill(theBuffer, (byte)0);
408416
}
409417

410418
/* process all blocks except the last one */
@@ -457,6 +465,13 @@ public int doOutput(final byte[] pOut,
457465
compressFinalBlock(thePos);
458466
}
459467

468+
/* Reject if there is insufficient Xof remaining */
469+
if (pOutLen < 0
470+
|| (outputAvailable >= 0 && pOutLen > outputAvailable))
471+
{
472+
throw new IllegalArgumentException("Insufficient bytes remaining");
473+
}
474+
460475
/* If we have some remaining data in the current buffer */
461476
int dataLeft = pOutLen;
462477
int outPos = pOutOffset;
@@ -488,6 +503,9 @@ public int doOutput(final byte[] pOut,
488503
dataLeft -= dataToCopy;
489504
}
490505

506+
/* Adjust outputAvailable */
507+
outputAvailable -= pOutLen;
508+
491509
/* Return the number of bytes transferred */
492510
return pOutLen;
493511
}
@@ -497,13 +515,13 @@ public void reset()
497515
resetBlockCount();
498516
thePos = 0;
499517
outputting = false;
500-
Arrays.fill(theBuffer, (byte) 0);
518+
Arrays.fill(theBuffer, (byte)0);
501519
}
502520

503521
public void reset(final Memoable pSource)
504522
{
505523
/* Access source */
506-
final Blake3Digest mySource = (Blake3Digest) pSource;
524+
final Blake3Digest mySource = (Blake3Digest)pSource;
507525

508526
/* Reset counter */
509527
theCounter = mySource.theCounter;
@@ -512,6 +530,7 @@ public void reset(final Memoable pSource)
512530

513531
/* Reset output state */
514532
outputting = mySource.outputting;
533+
outputAvailable = mySource.outputAvailable;
515534
theOutputMode = mySource.theOutputMode;
516535
theOutputDataLen = mySource.theOutputDataLen;
517536

@@ -522,7 +541,7 @@ public void reset(final Memoable pSource)
522541

523542
/* Copy stack */
524543
theStack.clear();
525-
for (Iterator it = mySource.theStack.iterator(); it.hasNext();)
544+
for (Iterator it = mySource.theStack.iterator(); it.hasNext(); )
526545
{
527546
theStack.push(Arrays.clone((int[])it.next()));
528547
}
@@ -539,8 +558,9 @@ public Memoable copy()
539558

540559
/**
541560
* Compress next block of the message.
561+
*
542562
* @param pMessage the message buffer
543-
* @param pMsgPos the position within the message buffer
563+
* @param pMsgPos the position within the message buffer
544564
*/
545565
private void compressBlock(final byte[] pMessage,
546566
final int pMsgPos)
@@ -591,6 +611,7 @@ private void adjustStack()
591611

592612
/**
593613
* Compress final block.
614+
*
594615
* @param pDataLen the data length
595616
*/
596617
private void compressFinalBlock(final int pDataLen)
@@ -624,12 +645,12 @@ private void processStack()
624645
setRoot();
625646
}
626647
compress();
627-
}
648+
}
628649
}
629650

630651
/**
631652
* Perform compression.
632-
*/
653+
*/
633654
private void compress()
634655
{
635656
/* Initialise the buffers */
@@ -667,8 +688,9 @@ private void performRound()
667688

668689
/**
669690
* Initialise M from message.
691+
*
670692
* @param pMessage the source message
671-
* @param pMsgPos the message position
693+
* @param pMsgPos the message position
672694
*/
673695
private void initM(final byte[] pMessage,
674696
final int pMsgPos)
@@ -716,11 +738,12 @@ private void adjustChaining()
716738

717739
/**
718740
* Mix function G.
741+
*
719742
* @param msgIdx the message index
720-
* @param posA position A in V
721-
* @param posB position B in V
722-
* @param posC position C in V
723-
* @param posD poistion D in V
743+
* @param posA position A in V
744+
* @param posB position B in V
745+
* @param posC position C in V
746+
* @param posD poistion D in V
724747
*/
725748
private void mixG(final int msgIdx,
726749
final int posA,
@@ -775,6 +798,7 @@ private void initNullKey()
775798

776799
/**
777800
* Initialise key.
801+
*
778802
* @param pKey the keyBytes
779803
*/
780804
private void initKey(final byte[] pKey)
@@ -798,21 +822,22 @@ private void initKeyFromContext()
798822

799823
/**
800824
* Initialise chunk block.
825+
*
801826
* @param pDataLen the dataLength
802-
* @param pFinal is this the final chunk?
827+
* @param pFinal is this the final chunk?
803828
*/
804829
private void initChunkBlock(final int pDataLen,
805830
final boolean pFinal)
806831
{
807832
/* Initialise the block */
808833
System.arraycopy(theCurrBytes == 0 ? theK : theChaining, 0, theV, 0, NUMWORDS);
809834
System.arraycopy(IV, 0, theV, NUMWORDS, NUMWORDS >> 1);
810-
theV[COUNT0] = (int) theCounter;
811-
theV[COUNT1] = (int) (theCounter >> Integers.SIZE);
835+
theV[COUNT0] = (int)theCounter;
836+
theV[COUNT1] = (int)(theCounter >> Integers.SIZE);
812837
theV[DATALEN] = pDataLen;
813838
theV[FLAGS] = theMode
814-
+ (theCurrBytes == 0 ? CHUNKSTART : 0)
815-
+ (pFinal ? CHUNKEND : 0);
839+
+ (theCurrBytes == 0 ? CHUNKSTART : 0)
840+
+ (pFinal ? CHUNKEND : 0);
816841

817842
/* * Adjust block count */
818843
theCurrBytes += pDataLen;
@@ -854,8 +879,8 @@ private void nextOutputBlock()
854879
/* Initialise the block */
855880
System.arraycopy(theChaining, 0, theV, 0, NUMWORDS);
856881
System.arraycopy(IV, 0, theV, NUMWORDS, NUMWORDS >> 1);
857-
theV[COUNT0] = (int) theCounter;
858-
theV[COUNT1] = (int) (theCounter >> Integers.SIZE);
882+
theV[COUNT0] = (int)theCounter;
883+
theV[COUNT1] = (int)(theCounter >> Integers.SIZE);
859884
theV[DATALEN] = theOutputDataLen;
860885
theV[FLAGS] = theOutputMode;
861886

@@ -891,6 +916,8 @@ private void setRoot()
891916
theOutputDataLen = theV[DATALEN];
892917
theCounter = 0;
893918
outputting = true;
919+
outputAvailable = -1;
894920
System.arraycopy(theV, 0, theChaining, 0, NUMWORDS);
895921
}
922+
896923
}

0 commit comments

Comments
 (0)