Skip to content

Commit c518429

Browse files
committed
standard digest tests for PGP (draft) relates to GitHub #1566
1 parent 6a5fd5e commit c518429

File tree

3 files changed

+93
-18
lines changed

3 files changed

+93
-18
lines changed

pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/OperatorHelper.java

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package org.bouncycastle.openpgp.operator.jcajce;
22

3+
import java.io.InputStream;
4+
import java.security.AlgorithmParameters;
5+
import java.security.GeneralSecurityException;
6+
import java.security.KeyFactory;
7+
import java.security.KeyPairGenerator;
8+
import java.security.MessageDigest;
9+
import java.security.NoSuchAlgorithmException;
10+
import java.security.NoSuchProviderException;
11+
import java.security.Signature;
12+
13+
import javax.crypto.Cipher;
14+
import javax.crypto.KeyAgreement;
15+
import javax.crypto.SecretKey;
16+
import javax.crypto.spec.IvParameterSpec;
17+
import javax.crypto.spec.SecretKeySpec;
18+
319
import org.bouncycastle.bcpg.HashAlgorithmTags;
420
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
521
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
@@ -11,22 +27,6 @@
1127
import org.bouncycastle.openpgp.operator.PGPDataDecryptor;
1228
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
1329

14-
import javax.crypto.Cipher;
15-
import javax.crypto.KeyAgreement;
16-
import javax.crypto.SecretKey;
17-
import javax.crypto.spec.IvParameterSpec;
18-
import javax.crypto.spec.SecretKeySpec;
19-
20-
import java.io.InputStream;
21-
import java.security.AlgorithmParameters;
22-
import java.security.GeneralSecurityException;
23-
import java.security.KeyFactory;
24-
import java.security.KeyPairGenerator;
25-
import java.security.MessageDigest;
26-
import java.security.NoSuchAlgorithmException;
27-
import java.security.NoSuchProviderException;
28-
import java.security.Signature;
29-
3030
class OperatorHelper
3131
{
3232
private JcaJceHelper helper;
@@ -65,6 +65,16 @@ String getDigestName(
6565
return "SHA-512";
6666
case HashAlgorithmTags.SHA224:
6767
return "SHA-224";
68+
case HashAlgorithmTags.SHA3_256:
69+
case HashAlgorithmTags.SHA3_256_OLD:
70+
return "SHA3-256";
71+
case HashAlgorithmTags.SHA3_384: // OLD
72+
return "SHA3-384";
73+
case HashAlgorithmTags.SHA3_512:
74+
case HashAlgorithmTags.SHA3_512_OLD:
75+
return "SHA3-512";
76+
case HashAlgorithmTags.SHA3_224:
77+
return "SHA3-224";
6878
case HashAlgorithmTags.TIGER_192:
6979
return "TIGER";
7080
default:

pg/src/test/java/org/bouncycastle/openpgp/test/OperatorBcTest.java

+33-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.security.SecureRandom;
1212
import java.security.Security;
1313
import java.security.spec.ECGenParameterSpec;
14-
1514
import java.util.Date;
1615
import java.util.Iterator;
1716

@@ -33,7 +32,6 @@
3332
import org.bouncycastle.crypto.params.KeyParameter;
3433
import org.bouncycastle.crypto.params.X25519PrivateKeyParameters;
3534
import org.bouncycastle.crypto.params.X25519PublicKeyParameters;
36-
import org.bouncycastle.jcajce.provider.asymmetric.edec.KeyAgreementSpi;
3735
import org.bouncycastle.jce.provider.BouncyCastleProvider;
3836
import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
3937
import org.bouncycastle.openpgp.PGPEncryptedData;
@@ -55,6 +53,7 @@
5553
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
5654
import org.bouncycastle.openpgp.operator.PGPContentVerifier;
5755
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
56+
import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider;
5857
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
5958
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
6059
import org.bouncycastle.openpgp.operator.bc.BcPGPDataEncryptorBuilder;
@@ -75,6 +74,7 @@
7574
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder;
7675
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator;
7776
import org.bouncycastle.util.Arrays;
77+
import org.bouncycastle.util.Strings;
7878
import org.bouncycastle.util.encoders.Hex;
7979
import org.bouncycastle.util.test.SimpleTest;
8080
import org.bouncycastle.util.test.UncloseableOutputStream;
@@ -106,6 +106,37 @@ public void performTest()
106106
testBcPGPContentVerifierBuilderProvider();
107107
//testBcPBESecretKeyDecryptorBuilder();
108108
testBcKeyFingerprintCalculator();
109+
testBcStandardDigests();
110+
}
111+
112+
private void testBcStandardDigests()
113+
throws Exception
114+
{
115+
PGPDigestCalculatorProvider digCalcBldr = new BcPGPDigestCalculatorProvider();
116+
117+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.MD5), Hex.decode("900150983cd24fb0d6963f7d28e17f72"));
118+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA1), Hex.decode("a9993e364706816aba3e25717850c26c9cd0d89d"));
119+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.RIPEMD160), Hex.decode("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"));
120+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA256), Hex.decode("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
121+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA384), Hex.decode("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"));
122+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA512), Hex.decode("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"));
123+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA224), Hex.decode("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"));
124+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA3_256), Hex.decode("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"));
125+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA3_512), Hex.decode("b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0"));
126+
}
127+
128+
private void testDigestCalc(PGPDigestCalculator digCalc, byte[] expected)
129+
throws IOException
130+
{
131+
OutputStream dOut = digCalc.getOutputStream();
132+
133+
dOut.write(Strings.toByteArray("abc"));
134+
135+
dOut.close();
136+
137+
byte[] res = digCalc.getDigest();
138+
139+
isTrue(Arrays.areEqual(res, expected));
109140
}
110141

111142
public void testBcKeyFingerprintCalculator()

pg/src/test/java/org/bouncycastle/openpgp/test/OperatorJcajceTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.openpgp.test;
22

3+
import java.io.IOException;
4+
import java.io.OutputStream;
35
import java.security.Key;
46
import java.security.KeyFactory;
57
import java.security.KeyPair;
@@ -70,6 +72,38 @@ public void performTest()
7072
testJcaPGPDigestCalculatorProviderBuilder();
7173
testJcePGPDataEncryptorBuilder();
7274
testJcaKeyFingerprintCalculator();
75+
testStandardDigests();
76+
}
77+
78+
private void testStandardDigests()
79+
throws Exception
80+
{
81+
PGPDigestCalculatorProvider digCalcBldr =
82+
new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build();
83+
84+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.MD5), Hex.decode("900150983cd24fb0d6963f7d28e17f72"));
85+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA1), Hex.decode("a9993e364706816aba3e25717850c26c9cd0d89d"));
86+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.RIPEMD160), Hex.decode("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"));
87+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA256), Hex.decode("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
88+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA384), Hex.decode("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"));
89+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA512), Hex.decode("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"));
90+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA224), Hex.decode("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"));
91+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA3_256), Hex.decode("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"));
92+
testDigestCalc(digCalcBldr.get(HashAlgorithmTags.SHA3_512), Hex.decode("b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0"));
93+
}
94+
95+
private void testDigestCalc(PGPDigestCalculator digCalc, byte[] expected)
96+
throws IOException
97+
{
98+
OutputStream dOut = digCalc.getOutputStream();
99+
100+
dOut.write(Strings.toByteArray("abc"));
101+
102+
dOut.close();
103+
104+
byte[] res = digCalc.getDigest();
105+
106+
isTrue(Arrays.areEqual(res, expected));
73107
}
74108

75109
public void testJcaKeyFingerprintCalculator()

0 commit comments

Comments
 (0)