Skip to content

Commit

Permalink
added fallback tests for digest creation in PGP JCA layer
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Apr 20, 2024
1 parent a8f7e0a commit 09baab6
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ MessageDigest createDigest(int algorithm)
}
catch (NoSuchAlgorithmException e)
{
if (algorithm >= HashAlgorithmTags.SHA256 && algorithm <= HashAlgorithmTags.SHA224)
if (algorithm == HashAlgorithmTags.SHA1
|| (algorithm >= HashAlgorithmTags.SHA256 && algorithm <= HashAlgorithmTags.SHA224))
{
dig = helper.createMessageDigest("SHA-" + digestName.substring(3));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ public void operation()
public void testJcaPGPDigestCalculatorProviderBuilder()
throws Exception
{

PGPDigestCalculatorProvider digCalcBldr = new JcaPGPDigestCalculatorProviderBuilder().setProvider(new NonDashProvider()).build();
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA256), Hex.decode("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));

PGPDigestCalculatorProvider digCalcBldr2 = new JcaPGPDigestCalculatorProviderBuilder().setProvider(new DashProvider()).build();
testDigestCalc(digCalcBldr2.get(HashAlgorithmTags.SHA256), Hex.decode("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));

PGPDigestCalculatorProvider digCalcBldr3 = new JcaPGPDigestCalculatorProviderBuilder().setProvider(new NonDashProvider()).build();
testDigestCalc(digCalcBldr3.get(HashAlgorithmTags.SHA1), Hex.decode("a9993e364706816aba3e25717850c26c9cd0d89d"));

PGPDigestCalculatorProvider digCalcBldr4 = new JcaPGPDigestCalculatorProviderBuilder().setProvider(new DashProvider()).build();
testDigestCalc(digCalcBldr4.get(HashAlgorithmTags.SHA1), Hex.decode("a9993e364706816aba3e25717850c26c9cd0d89d"));


final PGPDigestCalculatorProvider provider = new JcaPGPDigestCalculatorProviderBuilder().setProvider(new NullProvider()).build();
testException("exception on setup: ", "PGPException", new TestExceptionOperation()
{
Expand Down Expand Up @@ -310,4 +324,27 @@ private class NullProvider
super("NULL", 0.0, "Null Provider");
}
}

private class NonDashProvider
extends Provider
{
NonDashProvider()
{
super("NonDash", 0.0, "NonDash Provider");
putService(new Provider.Service(this, "MessageDigest", "SHA256", "org.bouncycastle.openpgp.test.SHA256", null, null));
putService(new Provider.Service(this, "MessageDigest", "SHA1", "org.bouncycastle.openpgp.test.SHA1", null, null));
}
}

private class DashProvider
extends Provider
{
DashProvider()
{
super("Dash", 0.0, "Dash Provider");
putService(new Service(this, "MessageDigest", "SHA-256", "org.bouncycastle.openpgp.test.SHA256", null, null));
putService(new Service(this, "MessageDigest", "SHA-1", "org.bouncycastle.openpgp.test.SHA1", null, null));
}
}

}
23 changes: 23 additions & 0 deletions pg/src/test/java/org/bouncycastle/openpgp/test/SHA1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.bouncycastle.openpgp.test;

import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.jcajce.provider.digest.BCMessageDigest;

public class SHA1
extends BCMessageDigest
implements Cloneable
{
public SHA1()
{
super(new SHA1Digest());
}

public Object clone()
throws CloneNotSupportedException
{
SHA1 d = (SHA1)super.clone();
d.digest = new SHA1Digest((SHA1Digest)digest);

return d;
}
}
23 changes: 23 additions & 0 deletions pg/src/test/java/org/bouncycastle/openpgp/test/SHA256.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.bouncycastle.openpgp.test;

import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.jcajce.provider.digest.BCMessageDigest;

public class SHA256
extends BCMessageDigest
implements Cloneable
{
public SHA256()
{
super(SHA256Digest.newInstance());
}

public Object clone()
throws CloneNotSupportedException
{
SHA256 d = (SHA256)super.clone();
d.digest = SHA256Digest.newInstance(digest);

return d;
}
}

0 comments on commit 09baab6

Please sign in to comment.