001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
004: import org.bouncycastle.crypto.generators.DSAKeyPairGenerator;
005: import org.bouncycastle.crypto.generators.DSAParametersGenerator;
006: import org.bouncycastle.crypto.params.DSAKeyGenerationParameters;
007: import org.bouncycastle.crypto.params.DSAParameters;
008: import org.bouncycastle.crypto.params.DSAValidationParameters;
009: import org.bouncycastle.crypto.params.ParametersWithRandom;
010: import org.bouncycastle.crypto.signers.DSASigner;
011: import org.bouncycastle.util.BigIntegers;
012: import org.bouncycastle.util.encoders.Hex;
013: import org.bouncycastle.util.test.FixedSecureRandom;
014: import org.bouncycastle.util.test.SimpleTest;
015:
016: import java.math.BigInteger;
017: import java.security.SecureRandom;
018:
019: /**
020: * Test based on FIPS 186-2, Appendix 5, an example of DSA.
021: */
022: public class DSATest extends SimpleTest {
023: byte[] k1 = Hex.decode("d5014e4b60ef2ba8b6211b4062ba3224e0427dd3");
024: byte[] k2 = Hex
025: .decode("345e8d05c075c3a508df729a1685690e68fcfb8c8117847e89063bca1f85d968fd281540b6e13bd1af989a1fbf17e06462bf511f9d0b140fb48ac1b1baa5bded");
026:
027: SecureRandom random = new FixedSecureRandom(new byte[][] { k1, k2 });
028:
029: byte[] keyData = Hex
030: .decode("b5014e4b60ef2ba8b6211b4062ba3224e0427dd3");
031:
032: SecureRandom keyRandom = new FixedSecureRandom(new byte[][] {
033: keyData, keyData });
034:
035: BigInteger pValue = new BigInteger(
036: "8df2a494492276aa3d25759bb06869cbeac0d83afb8d0cf7cbb8324f0d7882e5d0762fc5b7210eafc2e9adac32ab7aac49693dfbf83724c2ec0736ee31c80291",
037: 16);
038: BigInteger qValue = new BigInteger(
039: "c773218c737ec8ee993b4f2ded30f48edace915f", 16);
040:
041: public String getName() {
042: return "DSA";
043: }
044:
045: public void performTest() {
046: BigInteger r = new BigInteger(
047: "68076202252361894315274692543577577550894681403");
048: BigInteger s = new BigInteger(
049: "1089214853334067536215539335472893651470583479365");
050: DSAParametersGenerator pGen = new DSAParametersGenerator();
051:
052: pGen.init(512, 80, random);
053:
054: DSAParameters params = pGen.generateParameters();
055: DSAValidationParameters pValid = params
056: .getValidationParameters();
057:
058: if (pValid.getCounter() != 105) {
059: fail("Counter wrong");
060: }
061:
062: if (!pValue.equals(params.getP())
063: || !qValue.equals(params.getQ())) {
064: fail("p or q wrong");
065: }
066:
067: DSAKeyPairGenerator dsaKeyGen = new DSAKeyPairGenerator();
068: DSAKeyGenerationParameters genParam = new DSAKeyGenerationParameters(
069: keyRandom, params);
070:
071: dsaKeyGen.init(genParam);
072:
073: AsymmetricCipherKeyPair pair = dsaKeyGen.generateKeyPair();
074:
075: ParametersWithRandom param = new ParametersWithRandom(pair
076: .getPrivate(), keyRandom);
077:
078: DSASigner dsa = new DSASigner();
079:
080: dsa.init(true, param);
081:
082: byte[] message = BigIntegers
083: .asUnsignedByteArray(new BigInteger(
084: "968236873715988614170569073515315707566766479517"));
085: BigInteger[] sig = dsa.generateSignature(message);
086:
087: if (!r.equals(sig[0])) {
088: fail("r component wrong.", r, sig[0]);
089: }
090:
091: if (!s.equals(sig[1])) {
092: fail("s component wrong.", s, sig[1]);
093: }
094:
095: dsa.init(false, pair.getPublic());
096:
097: if (!dsa.verifySignature(message, sig[0], sig[1])) {
098: fail("verification fails");
099: }
100: }
101:
102: public static void main(String[] args) {
103: runTest(new DSATest());
104: }
105: }
|