001: package org.bouncycastle.openpgp.test;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.bouncycastle.bcpg.BCPGOutputStream;
007: import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
008: import org.bouncycastle.openpgp.PGPCompressedData;
009: import org.bouncycastle.openpgp.PGPLiteralData;
010: import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
011: import org.bouncycastle.openpgp.PGPObjectFactory;
012: import org.bouncycastle.openpgp.PGPOnePassSignature;
013: import org.bouncycastle.openpgp.PGPOnePassSignatureList;
014: import org.bouncycastle.openpgp.PGPPublicKeyRing;
015: import org.bouncycastle.openpgp.PGPSecretKeyRing;
016: import org.bouncycastle.openpgp.PGPSignature;
017: import org.bouncycastle.openpgp.PGPSignatureGenerator;
018: import org.bouncycastle.openpgp.PGPSignatureList;
019: import org.bouncycastle.openpgp.PGPUtil;
020: import org.bouncycastle.util.test.UncloseableOutputStream;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.FileInputStream;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.security.Security;
028: import java.util.Date;
029:
030: /**
031: * GPG compatability test vectors
032: */
033: public class DSA2Test extends TestCase {
034: private static final String TEST_DATA_HOME = "bc.test.data.home";
035:
036: public void setUp() {
037: if (Security.getProvider("BC") == null) {
038: Security
039: .addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
040: }
041: }
042:
043: public void testK1024H160() throws Exception {
044: doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-160-sign.gpg");
045: }
046:
047: public void testK1024H224() throws Exception {
048: doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-224-sign.gpg");
049: }
050:
051: public void testK1024H256() throws Exception {
052: doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-256-sign.gpg");
053: }
054:
055: public void testK1024H384() throws Exception {
056: doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-384-sign.gpg");
057: }
058:
059: public void testK1024H512() throws Exception {
060: doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-512-sign.gpg");
061: }
062:
063: public void testK2048H224() throws Exception {
064: doSigVerifyTest("DSA-2048-224.pub", "dsa-2048-224-sign.gpg");
065: }
066:
067: public void testK3072H256() throws Exception {
068: doSigVerifyTest("DSA-3072-256.pub", "dsa-3072-256-sign.gpg");
069: }
070:
071: public void testK7680H384() throws Exception {
072: doSigVerifyTest("DSA-7680-384.pub", "dsa-7680-384-sign.gpg");
073: }
074:
075: public void testK15360H512() throws Exception {
076: doSigVerifyTest("DSA-15360-512.pub", "dsa-15360-512-sign.gpg");
077: }
078:
079: public void testGenerateK1024H224() throws Exception {
080: doSigGenerateTest("DSA-1024-160.sec", "DSA-1024-160.pub",
081: PGPUtil.SHA224);
082: }
083:
084: public void testGenerateK1024H256() throws Exception {
085: doSigGenerateTest("DSA-1024-160.sec", "DSA-1024-160.pub",
086: PGPUtil.SHA256);
087: }
088:
089: public void testGenerateK1024H384() throws Exception {
090: doSigGenerateTest("DSA-1024-160.sec", "DSA-1024-160.pub",
091: PGPUtil.SHA384);
092: }
093:
094: public void testGenerateK1024H512() throws Exception {
095: doSigGenerateTest("DSA-1024-160.sec", "DSA-1024-160.pub",
096: PGPUtil.SHA512);
097: }
098:
099: public void testGenerateK2048H256() throws Exception {
100: doSigGenerateTest("DSA-2048-224.sec", "DSA-2048-224.pub",
101: PGPUtil.SHA256);
102: }
103:
104: public void testGenerateK2048H512() throws Exception {
105: doSigGenerateTest("DSA-2048-224.sec", "DSA-2048-224.pub",
106: PGPUtil.SHA512);
107: }
108:
109: private void doSigGenerateTest(String privateKeyFile,
110: String publicKeyFile, int digest) throws Exception {
111: PGPSecretKeyRing secRing = loadSecretKey(privateKeyFile);
112: PGPPublicKeyRing pubRing = loadPublicKey(publicKeyFile);
113: String data = "hello world!";
114: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
115: ByteArrayInputStream testIn = new ByteArrayInputStream(data
116: .getBytes());
117: PGPSignatureGenerator sGen = new PGPSignatureGenerator(
118: PublicKeyAlgorithmTags.DSA, digest, "BC");
119:
120: sGen.initSign(PGPSignature.BINARY_DOCUMENT, secRing
121: .getSecretKey().extractPrivateKey("test".toCharArray(),
122: "BC"));
123:
124: BCPGOutputStream bcOut = new BCPGOutputStream(bOut);
125:
126: sGen.generateOnePassVersion(false).encode(bcOut);
127:
128: PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
129:
130: Date testDate = new Date(
131: (System.currentTimeMillis() / 1000) * 1000);
132: OutputStream lOut = lGen.open(
133: new UncloseableOutputStream(bcOut),
134: PGPLiteralData.BINARY, "_CONSOLE",
135: data.getBytes().length, testDate);
136:
137: int ch;
138: while ((ch = testIn.read()) >= 0) {
139: lOut.write(ch);
140: sGen.update((byte) ch);
141: }
142:
143: lGen.close();
144:
145: sGen.generate().encode(bcOut);
146:
147: PGPObjectFactory pgpFact = new PGPObjectFactory(bOut
148: .toByteArray());
149: PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact
150: .nextObject();
151: PGPOnePassSignature ops = p1.get(0);
152:
153: assertEquals(digest, ops.getHashAlgorithm());
154: assertEquals(PublicKeyAlgorithmTags.DSA, ops.getKeyAlgorithm());
155:
156: PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
157: if (!p2.getModificationTime().equals(testDate)) {
158: fail("Modification time not preserved");
159: }
160:
161: InputStream dIn = p2.getInputStream();
162:
163: ops.initVerify(pubRing.getPublicKey(), "BC");
164:
165: while ((ch = dIn.read()) >= 0) {
166: ops.update((byte) ch);
167: }
168:
169: PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();
170: PGPSignature sig = p3.get(0);
171:
172: assertEquals(digest, sig.getHashAlgorithm());
173: assertEquals(PublicKeyAlgorithmTags.DSA, sig.getKeyAlgorithm());
174:
175: assertTrue(ops.verify(sig));
176: }
177:
178: private void doSigVerifyTest(String publicKeyFile, String sigFile)
179: throws Exception {
180: PGPPublicKeyRing publicKey = loadPublicKey(publicKeyFile);
181: PGPObjectFactory pgpFact = loadSig(sigFile);
182:
183: PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();
184:
185: pgpFact = new PGPObjectFactory(c1.getDataStream());
186:
187: PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact
188: .nextObject();
189: PGPOnePassSignature ops = p1.get(0);
190:
191: PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
192:
193: InputStream dIn = p2.getInputStream();
194:
195: ops.initVerify(publicKey.getPublicKey(), "BC");
196:
197: int ch;
198: while ((ch = dIn.read()) >= 0) {
199: ops.update((byte) ch);
200: }
201:
202: PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();
203:
204: assertTrue(ops.verify(p3.get(0)));
205: }
206:
207: private PGPObjectFactory loadSig(String sigName) throws Exception {
208: FileInputStream fIn = new FileInputStream(getDataHome()
209: + "/sigs/" + sigName);
210:
211: return new PGPObjectFactory(fIn);
212: }
213:
214: private PGPPublicKeyRing loadPublicKey(String keyName)
215: throws Exception {
216: FileInputStream fIn = new FileInputStream(getDataHome()
217: + "/keys/" + keyName);
218:
219: return new PGPPublicKeyRing(fIn);
220: }
221:
222: private PGPSecretKeyRing loadSecretKey(String keyName)
223: throws Exception {
224: FileInputStream fIn = new FileInputStream(getDataHome()
225: + "/keys/" + keyName);
226:
227: return new PGPSecretKeyRing(fIn);
228: }
229:
230: private String getDataHome() {
231: String dataHome = System.getProperty(TEST_DATA_HOME);
232:
233: if (dataHome == null) {
234: throw new IllegalStateException(TEST_DATA_HOME
235: + " property not set");
236: }
237:
238: return dataHome + "/openpgp/dsa";
239: }
240:
241: public static void main(String[] args) throws Exception {
242: junit.textui.TestRunner.run(suite());
243: }
244:
245: public static Test suite() throws Exception {
246: TestSuite suite = new TestSuite("GPG DSA2 tests");
247:
248: suite.addTestSuite(DSA2Test.class);
249:
250: return suite;
251: }
252: }
|