Skip to content

More encodable digests #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 57 additions & 9 deletions core/src/main/java/org/bouncycastle/crypto/digests/LongDigest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
* Base class for SHA-384 and SHA-512.
*/
public abstract class LongDigest
implements ExtendedDigest, Memoable
implements ExtendedDigest, Memoable, EncodableDigest
{
private static final int BYTE_LENGTH = 128;
private byte[] xBuf;

private byte[] xBuf = new byte[8];
private int xBufOff;

private long byteCount1;
Expand All @@ -28,7 +28,6 @@ public abstract class LongDigest
*/
protected LongDigest()
{
xBuf = new byte[8];
xBufOff = 0;

reset();
Expand All @@ -41,8 +40,6 @@ protected LongDigest()
*/
protected LongDigest(LongDigest t)
{
xBuf = new byte[t.xBuf.length];

copyIn(t);
}

Expand All @@ -67,6 +64,56 @@ protected void copyIn(LongDigest t)
wOff = t.wOff;
}

protected void populateState(byte[] state)
{
System.arraycopy(xBuf, 0, state, 0, xBufOff);
Pack.intToBigEndian(xBufOff, state, 8);
Pack.longToBigEndian(byteCount1, state, 12);
Pack.longToBigEndian(byteCount2, state, 20);
Pack.longToBigEndian(H1, state, 28);
Pack.longToBigEndian(H2, state, 36);
Pack.longToBigEndian(H3, state, 44);
Pack.longToBigEndian(H4, state, 52);
Pack.longToBigEndian(H5, state, 60);
Pack.longToBigEndian(H6, state, 68);
Pack.longToBigEndian(H7, state, 76);
Pack.longToBigEndian(H8, state, 84);

Pack.intToBigEndian(wOff, state, 92);
for (int i = 0; i < wOff; i++)
{
Pack.longToBigEndian(W[i], state, 96 + (i * 8));
}
}

protected void restoreState(byte[] encodedState)
{
xBufOff = Pack.bigEndianToInt(encodedState, 8);
System.arraycopy(encodedState, 0, xBuf, 0, xBufOff);
byteCount1 = Pack.bigEndianToLong(encodedState, 12);
byteCount2 = Pack.bigEndianToLong(encodedState, 20);

H1 = Pack.bigEndianToLong(encodedState, 28);
H2 = Pack.bigEndianToLong(encodedState, 36);
H3 = Pack.bigEndianToLong(encodedState, 44);
H4 = Pack.bigEndianToLong(encodedState, 52);
H5 = Pack.bigEndianToLong(encodedState, 60);
H6 = Pack.bigEndianToLong(encodedState, 68);
H7 = Pack.bigEndianToLong(encodedState, 76);
H8 = Pack.bigEndianToLong(encodedState, 84);

wOff = Pack.bigEndianToInt(encodedState, 92);
for (int i = 0; i < wOff; i++)
{
W[i] = Pack.bigEndianToLong(encodedState, 96 + (i * 8));
}
}

protected int getEncodedStateSize()
{
return 96 + (wOff * 8);
}

public void update(
byte in)
{
Expand Down Expand Up @@ -165,7 +212,7 @@ public int getByteLength()
{
return BYTE_LENGTH;
}

protected void processWord(
byte[] in,
int inOff)
Expand Down Expand Up @@ -228,7 +275,7 @@ protected void processBlock()
long g = H7;
long h = H8;

int t = 0;
int t = 0;
for(int i = 0; i < 10; i ++)
{
// t = 8 * i
Expand Down Expand Up @@ -271,7 +318,7 @@ protected void processBlock()
e += a;
a += Sum0(b) + Maj(b, c, d);
}

H1 += a;
H2 += b;
H3 += c;
Expand Down Expand Up @@ -358,4 +405,5 @@ private long Sigma1(
0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL,
0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public SHA384Digest(SHA384Digest t)
super(t);
}

public SHA384Digest(byte[] encodedState)
{
restoreState(encodedState);
}

public String getAlgorithmName()
{
return "SHA-384";
Expand Down Expand Up @@ -96,4 +101,11 @@ public void reset(Memoable other)

super.copyIn(d);
}

public byte[] getEncodedState()
{
byte[] encoded = new byte[getEncodedStateSize()];
super.populateState(encoded);
return encoded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public SHA512Digest(SHA512Digest t)
super(t);
}

public SHA512Digest(byte[] encodedState)
{
restoreState(encodedState);
}

public String getAlgorithmName()
{
return "SHA-512";
Expand Down Expand Up @@ -98,5 +103,12 @@ public void reset(Memoable other)

copyIn(d);
}

public byte[] getEncodedState()
{
byte[] encoded = new byte[getEncodedStateSize()];
super.populateState(encoded);
return encoded;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bouncycastle.util.Memoable;
import org.bouncycastle.util.MemoableResetException;
import org.bouncycastle.util.Pack;

/**
* FIPS 180-4 implementation of SHA-512/t
Expand Down Expand Up @@ -53,6 +54,17 @@ public SHA512tDigest(SHA512tDigest t)
reset(t);
}

public SHA512tDigest(byte[] encodedState)
{
this(readDigestLength(encodedState));
restoreState(encodedState);
}

private static int readDigestLength(byte[] encodedState)
{
return Pack.bigEndianToInt(encodedState, encodedState.length - 4);
}

public String getAlgorithmName()
{
return "SHA-512/" + Integer.toString(digestLength * 8);
Expand Down Expand Up @@ -202,4 +214,14 @@ public void reset(Memoable other)
this.H7t = t.H7t;
this.H8t = t.H8t;
}

public byte[] getEncodedState()
{
final int baseSize = getEncodedStateSize();
byte[] encoded = new byte[baseSize + 4];
populateState(encoded);
Pack.intToBigEndian(digestLength * 8, encoded, baseSize);
return encoded;
}

}
102 changes: 75 additions & 27 deletions core/src/test/java/org/bouncycastle/crypto/test/DigestTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.bouncycastle.crypto.test;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.EncodableDigest;
import org.bouncycastle.util.Memoable;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;

public abstract class DigestTest
public abstract class DigestTest
extends SimpleTest
{
private Digest digest;
Expand Down Expand Up @@ -42,71 +43,113 @@ public void performTest()
byte[] lastDigest = Hex.decode(results[input.length - 1]);

vectorTest(digest, input.length - 1, resBuf, lastV, Hex.decode(results[input.length - 1]));

//
// clone test
//
digest.update(lastV, 0, lastV.length/2);

// clone the Digest
Digest d = cloneDigest(digest);

digest.update(lastV, lastV.length/2, lastV.length - lastV.length/2);
testClone(resBuf, lastV, lastDigest);
testMemo(resBuf, lastV, lastDigest);
if (digest instanceof EncodableDigest)
{
testEncodedState(resBuf, lastV, lastDigest);
}
}

private void testEncodedState(byte[] resBuf, byte[] input, byte[] expected)
{
// test state encoding;
digest.update(input, 0, input.length / 2);

// copy the Digest
Digest copy1 = cloneDigest(((EncodableDigest)digest).getEncodedState());
Digest copy2 = cloneDigest(((EncodableDigest)copy1).getEncodedState());

digest.update(input, input.length / 2, input.length - input.length / 2);

digest.doFinal(resBuf, 0);

if (!areEqual(lastDigest, resBuf))
if (!areEqual(expected, resBuf))
{
fail("failing clone vector test", results[results.length - 1], new String(Hex.encode(resBuf)));
fail("failing state vector test", expected, new String(Hex.encode(resBuf)));
}

d.update(lastV, lastV.length/2, lastV.length - lastV.length/2);
d.doFinal(resBuf, 0);
copy1.update(input, input.length / 2, input.length - input.length / 2);
copy1.doFinal(resBuf, 0);

if (!areEqual(lastDigest, resBuf))
if (!areEqual(expected, resBuf))
{
fail("failing second clone vector test", results[results.length - 1], new String(Hex.encode(resBuf)));
fail("failing state copy1 vector test", expected, new String(Hex.encode(resBuf)));
}

//
// memo test
//
copy2.update(input, input.length / 2, input.length - input.length / 2);
copy2.doFinal(resBuf, 0);

if (!areEqual(expected, resBuf))
{
fail("failing state copy2 vector test", expected, new String(Hex.encode(resBuf)));
}
}

private void testMemo(byte[] resBuf, byte[] input, byte[] expected)
{
Memoable m = (Memoable)digest;

digest.update(lastV, 0, lastV.length/2);
digest.update(input, 0, input.length/2);

// copy the Digest
Memoable copy1 = m.copy();
Memoable copy2 = copy1.copy();

digest.update(lastV, lastV.length/2, lastV.length - lastV.length/2);
digest.update(input, input.length/2, input.length - input.length/2);
digest.doFinal(resBuf, 0);

if (!areEqual(lastDigest, resBuf))
if (!areEqual(expected, resBuf))
{
fail("failing memo vector test", results[results.length - 1], new String(Hex.encode(resBuf)));
}

m.reset(copy1);

digest.update(lastV, lastV.length/2, lastV.length - lastV.length/2);
digest.update(input, input.length/2, input.length - input.length/2);
digest.doFinal(resBuf, 0);

if (!areEqual(lastDigest, resBuf))
if (!areEqual(expected, resBuf))
{
fail("failing memo reset vector test", results[results.length - 1], new String(Hex.encode(resBuf)));
}

Digest md = (Digest)copy2;

md.update(lastV, lastV.length/2, lastV.length - lastV.length/2);
md.update(input, input.length/2, input.length - input.length/2);
md.doFinal(resBuf, 0);

if (!areEqual(lastDigest, resBuf))
if (!areEqual(expected, resBuf))
{
fail("failing memo copy vector test", results[results.length - 1], new String(Hex.encode(resBuf)));
}
}

private void testClone(byte[] resBuf, byte[] input, byte[] expected)
{
digest.update(input, 0, input.length/2);

// clone the Digest
Digest d = cloneDigest(digest);

digest.update(input, input.length/2, input.length - input.length/2);
digest.doFinal(resBuf, 0);

if (!areEqual(expected, resBuf))
{
fail("failing clone vector test", results[results.length - 1], new String(Hex.encode(resBuf)));
}

d.update(input, input.length/2, input.length - input.length/2);
d.doFinal(resBuf, 0);

if (!areEqual(expected, resBuf))
{
fail("failing second clone vector test", results[results.length - 1], new String(Hex.encode(resBuf)));
}
}

protected byte[] toByteArray(String input)
{
byte[] bytes = new byte[input.length()];
Expand Down Expand Up @@ -136,7 +179,12 @@ private void vectorTest(
}

protected abstract Digest cloneDigest(Digest digest);


protected Digest cloneDigest(byte[] encodedState)
{
throw new UnsupportedOperationException();
}

//
// optional tests
//
Expand Down
Loading