Skip to content

Commit

Permalink
Merged PR 1037
Browse files Browse the repository at this point in the history
Police Blake3 output limit

Given difficulty in testing, outputAvailable was set to 1 and exception
was observed manually.

open-keychain#2
  • Loading branch information
mwcw committed Oct 5, 2021
1 parent d4c7ce0 commit 555471b
Showing 1 changed file with 50 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Blake3 implementation.
*/
public class Blake3Digest
implements ExtendedDigest, Memoable, Xof
implements ExtendedDigest, Memoable, Xof
{
/**
* Already outputting error.
Expand Down Expand Up @@ -45,27 +45,27 @@ public class Blake3Digest
/**
* ChunkStart Flag.
*/
private static final int CHUNKSTART = 1;
private static final int CHUNKSTART = 1;

/**
* ChunkEnd Flag.
*/
private static final int CHUNKEND = 2;
private static final int CHUNKEND = 2;

/**
* Parent Flag.
*/
private static final int PARENT = 4;
private static final int PARENT = 4;

/**
* Root Flag.
*/
private static final int ROOT = 8;
private static final int ROOT = 8;

/**
* KeyedHash Flag.
*/
private static final int KEYEDHASH = 16;
private static final int KEYEDHASH = 16;

/**
* DeriveContext Flag.
Expand All @@ -75,7 +75,7 @@ public class Blake3Digest
/**
* DeriveKey Flag.
*/
private static final int DERIVEKEY = 64;
private static final int DERIVEKEY = 64;

/**
* Chaining0 State Locations.
Expand Down Expand Up @@ -120,58 +120,58 @@ public class Blake3Digest
/**
* IV0 State Locations.
*/
private static final int IV0 = 8;
private static final int IV0 = 8;

/**
* IV1 State Location.
*/
private static final int IV1 = 9;
private static final int IV1 = 9;

/**
* IV2 State Location.
*/
private static final int IV2 = 10;
private static final int IV2 = 10;

/**
* IV3 State Location.
*/
private static final int IV3 = 11;
private static final int IV3 = 11;

/**
* Count0 State Location.
*/
private static final int COUNT0 = 12;
private static final int COUNT0 = 12;

/**
* Count1 State Location.
*/
private static final int COUNT1 = 13;
private static final int COUNT1 = 13;

/**
* DataLen State Location.
*/
private static final int DATALEN = 14;
private static final int DATALEN = 14;

/**
* Flags State Location.
*/
private static final int FLAGS = 15;
private static final int FLAGS = 15;

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

/**
* Rotation constants.
*/
private static final byte[] ROTATE = { 16, 12, 8, 7 };
private static final byte[] ROTATE = {16, 12, 8, 7};

/**
* Blake3 Initialization Vector.
*/
private static final int[] IV = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
};

/**
Expand Down Expand Up @@ -264,6 +264,7 @@ public Blake3Digest()

/**
* Constructor.
*
* @param pDigestLen the default digestLength
*/
public Blake3Digest(final int pDigestLen)
Expand All @@ -274,6 +275,7 @@ public Blake3Digest(final int pDigestLen)

/**
* Constructor.
*
* @param pSource the source digest.
*/
private Blake3Digest(final Blake3Digest pSource)
Expand All @@ -282,7 +284,7 @@ private Blake3Digest(final Blake3Digest pSource)
theDigestLen = pSource.theDigestLen;

/* Initialise from source */
reset((Memoable) pSource);
reset((Memoable)pSource);
}

public int getByteLength()
Expand All @@ -302,6 +304,7 @@ public int getDigestSize()

/**
* Initialise.
*
* @param pParams the parameters.
*/
public void init(final Blake3Parameters pParams)
Expand All @@ -318,7 +321,7 @@ public void init(final Blake3Parameters pParams)
{
/* Initialise with the key */
initKey(myKey);
Arrays.fill(myKey, (byte) 0);
Arrays.fill(myKey, (byte)0);

/* else if we have a context */
}
Expand Down Expand Up @@ -360,7 +363,7 @@ public void update(final byte b)
compressBlock(theBuffer, 0);

/* Reset the buffer */
Arrays.fill(theBuffer, (byte) 0);
Arrays.fill(theBuffer, (byte)0);
thePos = 0;
}

Expand Down Expand Up @@ -409,7 +412,7 @@ public void update(final byte[] pMessage,

/* Reset the buffer */
thePos = 0;
Arrays.fill(theBuffer, (byte) 0);
Arrays.fill(theBuffer, (byte)0);
}

/* process all blocks except the last one */
Expand Down Expand Up @@ -512,13 +515,13 @@ public void reset()
resetBlockCount();
thePos = 0;
outputting = false;
Arrays.fill(theBuffer, (byte) 0);
Arrays.fill(theBuffer, (byte)0);
}

public void reset(final Memoable pSource)
{
/* Access source */
final Blake3Digest mySource = (Blake3Digest) pSource;
final Blake3Digest mySource = (Blake3Digest)pSource;

/* Reset counter */
theCounter = mySource.theCounter;
Expand All @@ -538,7 +541,7 @@ public void reset(final Memoable pSource)

/* Copy stack */
theStack.clear();
for (Iterator it = mySource.theStack.iterator(); it.hasNext();)
for (Iterator it = mySource.theStack.iterator(); it.hasNext(); )
{
theStack.push(Arrays.clone((int[])it.next()));
}
Expand All @@ -555,8 +558,9 @@ public Memoable copy()

/**
* Compress next block of the message.
*
* @param pMessage the message buffer
* @param pMsgPos the position within the message buffer
* @param pMsgPos the position within the message buffer
*/
private void compressBlock(final byte[] pMessage,
final int pMsgPos)
Expand Down Expand Up @@ -607,6 +611,7 @@ private void adjustStack()

/**
* Compress final block.
*
* @param pDataLen the data length
*/
private void compressFinalBlock(final int pDataLen)
Expand Down Expand Up @@ -640,12 +645,12 @@ private void processStack()
setRoot();
}
compress();
}
}
}

/**
* Perform compression.
*/
*/
private void compress()
{
/* Initialise the buffers */
Expand Down Expand Up @@ -683,8 +688,9 @@ private void performRound()

/**
* Initialise M from message.
*
* @param pMessage the source message
* @param pMsgPos the message position
* @param pMsgPos the message position
*/
private void initM(final byte[] pMessage,
final int pMsgPos)
Expand Down Expand Up @@ -732,11 +738,12 @@ private void adjustChaining()

/**
* Mix function G.
*
* @param msgIdx the message index
* @param posA position A in V
* @param posB position B in V
* @param posC position C in V
* @param posD poistion D in V
* @param posA position A in V
* @param posB position B in V
* @param posC position C in V
* @param posD poistion D in V
*/
private void mixG(final int msgIdx,
final int posA,
Expand Down Expand Up @@ -791,6 +798,7 @@ private void initNullKey()

/**
* Initialise key.
*
* @param pKey the keyBytes
*/
private void initKey(final byte[] pKey)
Expand All @@ -814,21 +822,22 @@ private void initKeyFromContext()

/**
* Initialise chunk block.
*
* @param pDataLen the dataLength
* @param pFinal is this the final chunk?
* @param pFinal is this the final chunk?
*/
private void initChunkBlock(final int pDataLen,
final boolean pFinal)
{
/* Initialise the block */
System.arraycopy(theCurrBytes == 0 ? theK : theChaining, 0, theV, 0, NUMWORDS);
System.arraycopy(IV, 0, theV, NUMWORDS, NUMWORDS >> 1);
theV[COUNT0] = (int) theCounter;
theV[COUNT1] = (int) (theCounter >> Integers.SIZE);
theV[COUNT0] = (int)theCounter;
theV[COUNT1] = (int)(theCounter >> Integers.SIZE);
theV[DATALEN] = pDataLen;
theV[FLAGS] = theMode
+ (theCurrBytes == 0 ? CHUNKSTART : 0)
+ (pFinal ? CHUNKEND : 0);
+ (theCurrBytes == 0 ? CHUNKSTART : 0)
+ (pFinal ? CHUNKEND : 0);

/* * Adjust block count */
theCurrBytes += pDataLen;
Expand Down Expand Up @@ -870,8 +879,8 @@ private void nextOutputBlock()
/* Initialise the block */
System.arraycopy(theChaining, 0, theV, 0, NUMWORDS);
System.arraycopy(IV, 0, theV, NUMWORDS, NUMWORDS >> 1);
theV[COUNT0] = (int) theCounter;
theV[COUNT1] = (int) (theCounter >> Integers.SIZE);
theV[COUNT0] = (int)theCounter;
theV[COUNT1] = (int)(theCounter >> Integers.SIZE);
theV[DATALEN] = theOutputDataLen;
theV[FLAGS] = theOutputMode;

Expand Down Expand Up @@ -910,4 +919,5 @@ private void setRoot()
outputAvailable = -1;
System.arraycopy(theV, 0, theChaining, 0, NUMWORDS);
}

}

0 comments on commit 555471b

Please sign in to comment.