Rewrite DSTU7564 padding
private int columns;
private int rounds;
- private byte[] padded;
private byte[][] state;
private byte[][] tempState1;
@@ -63,7 +62,6 @@ private void copyIn(DSTU7564Digest digest)
this.columns = digest.columns;
this.rounds = digest.rounds;
- this.padded = Arrays.clone(digest.padded);
this.state = Arrays.clone(digest.state);
this.tempState1 = Arrays.clone(digest.tempState1);
@@ -108,7 +106,6 @@ public DSTU7564Digest(int hashSizeBits)
}
this.state[0][0] = (byte)state.length; // Defined in standard
- this.padded = null;
this.tempState1 = new byte[columns][];
this.tempState2 = new byte[columns][];
@@ -178,16 +175,33 @@ public void update(byte[] in, int inOff, int len)
public int doFinal(byte[] out, int outOff)
{
- padded = pad(buf, 0, bufOff);
+ // Apply padding: terminator byte and 96-bit length field
+ {
+ buf[bufOff++] = (byte)0x80;
- int paddedLen = padded.length;
- int paddedOff = 0;
+ int lenPos = blockSize - 12;
+ if (bufOff > lenPos)
+ {
+ while (bufOff < blockSize)
+ {
+ buf[bufOff++] = 0;
+ }
+ bufOff = 0;
+ processBlock(buf, 0);
+ }
- while (paddedLen != 0)
+ while (bufOff < lenPos)
{
- processBlock(padded, paddedOff);
- paddedOff += blockSize;
- paddedLen -= blockSize;
+ buf[bufOff++] = 0;
+ }
+ // TODO inputLength has to be restricted to 61 bits
+ Pack.longToLittleEndian(inputLength << 3, buf, bufOff);
+ bufOff += 8;
+ while (bufOff < blockSize)
+ {
+ buf[bufOff++] = 0;
+ }
+ processBlock(buf, 0);
}
byte[][] temp = new byte[STATE_BYTES_SIZE_1024][];
@@ -290,11 +304,6 @@ public void reset()
bufOff = 0;
Arrays.fill(buf, (byte)0);
-
- if (padded != null)
- {
- Arrays.fill(padded, (byte)0);
- }
}
private void processBlock(byte[] input, int inOff)
@@ -473,27 +482,6 @@ private void mixColumns(byte[][] state)
}
}
- private byte[] pad(byte[] in, int inOff, int len)
- {
- byte[] padded;
- if (blockSize - len < 13) // terminator byte + 96 bits of length
- {
- padded = new byte[2 * blockSize];
- }
- else
- {
- padded = new byte[blockSize];
- }
-
- System.arraycopy(in, inOff, padded, 0, len);
-
- padded[len] = (byte)0x80; // Defined in standard;
- // Defined in standard;
- Pack.longToLittleEndian(inputLength * BITS_IN_BYTE, padded, padded.length - 12);
-
- return padded;
- }
-
private static final byte[][] sBoxes = new byte[][]{
new byte[]{
(byte)0xa8, (byte)0x43, (byte)0x5f, (byte)0x06, (byte)0x6b, (byte)0x75, (byte)0x6c, (byte)0x59, (byte)0x71, (byte)0xdf, (byte)0x87, (byte)0x95, (byte)0x17, (byte)0xf0, (byte)0xd8, (byte)0x09,
