001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
004: import org.bouncycastle.crypto.generators.DHKeyPairGenerator;
005: import org.bouncycastle.crypto.generators.ElGamalKeyPairGenerator;
006: import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
007: import org.bouncycastle.crypto.params.DHKeyParameters;
008: import org.bouncycastle.crypto.params.DHParameters;
009: import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
010: import org.bouncycastle.crypto.params.DHPublicKeyParameters;
011: import org.bouncycastle.crypto.params.DHValidationParameters;
012: import org.bouncycastle.crypto.params.DSAParameters;
013: import org.bouncycastle.crypto.params.DSAValidationParameters;
014: import org.bouncycastle.crypto.params.ElGamalKeyGenerationParameters;
015: import org.bouncycastle.crypto.params.ElGamalKeyParameters;
016: import org.bouncycastle.crypto.params.ElGamalParameters;
017: import org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters;
018: import org.bouncycastle.crypto.params.ElGamalPublicKeyParameters;
019: import org.bouncycastle.crypto.params.GOST3410Parameters;
020: import org.bouncycastle.crypto.params.GOST3410ValidationParameters;
021: import org.bouncycastle.util.test.SimpleTest;
022:
023: import java.math.BigInteger;
024: import java.security.SecureRandom;
025:
026: class DHTestKeyParameters extends DHKeyParameters {
027: protected DHTestKeyParameters(boolean isPrivate, DHParameters params) {
028: super (isPrivate, params);
029: }
030: }
031:
032: class ElGamalTestKeyParameters extends ElGamalKeyParameters {
033: protected ElGamalTestKeyParameters(boolean isPrivate,
034: ElGamalParameters params) {
035: super (isPrivate, params);
036: }
037: }
038:
039: public class EqualsHashCodeTest extends SimpleTest {
040: private static Object OTHER = new Object();
041:
042: public String getName() {
043: return "EqualsHashCode";
044: }
045:
046: private void doTest(Object a, Object equalsA, Object notEqualsA) {
047: if (a.equals(null)) {
048: fail("a equaled null");
049: }
050:
051: if (!a.equals(equalsA) || !equalsA.equals(a)) {
052: fail("equality failed");
053: }
054:
055: if (a.equals(OTHER)) {
056: fail("other inequality failed");
057: }
058:
059: if (a.equals(notEqualsA) || notEqualsA.equals(a)) {
060: fail("inequality failed");
061: }
062:
063: if (a.hashCode() != equalsA.hashCode()) {
064: fail("hashCode equality failed");
065: }
066: }
067:
068: private void dhTest() {
069: BigInteger g512 = new BigInteger(
070: "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc",
071: 16);
072: BigInteger p512 = new BigInteger(
073: "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b",
074: 16);
075:
076: DHParameters dhParams = new DHParameters(p512, g512);
077: DHKeyGenerationParameters params = new DHKeyGenerationParameters(
078: new SecureRandom(), dhParams);
079: DHKeyPairGenerator kpGen = new DHKeyPairGenerator();
080:
081: kpGen.init(params);
082:
083: AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();
084: DHPublicKeyParameters pu1 = (DHPublicKeyParameters) pair
085: .getPublic();
086: DHPrivateKeyParameters pv1 = (DHPrivateKeyParameters) pair
087: .getPrivate();
088:
089: DHPublicKeyParameters pu2 = new DHPublicKeyParameters(pu1
090: .getY(), pu1.getParameters());
091: DHPrivateKeyParameters pv2 = new DHPrivateKeyParameters(pv1
092: .getX(), pv1.getParameters());
093: DHPublicKeyParameters pu3 = new DHPublicKeyParameters(pv1
094: .getX(), pu1.getParameters());
095: DHPrivateKeyParameters pv3 = new DHPrivateKeyParameters(pu1
096: .getY(), pu1.getParameters());
097:
098: doTest(pu1, pu2, pu3);
099: doTest(pv1, pv2, pv3);
100:
101: DHParameters pr1 = pu1.getParameters();
102: DHParameters pr2 = new DHParameters(pr1.getP(), pr1.getG(), pr1
103: .getQ(), pr1.getJ(), pr1.getValidationParameters());
104: DHParameters pr3 = new DHParameters(pr1.getG(), pr1.getP(), pr1
105: .getQ(), pr1.getJ(), pr1.getValidationParameters());
106:
107: doTest(pr1, pr2, pr3);
108:
109: pr3 = new DHParameters(pr1.getG(), pr1.getP(), null,
110: pr1.getJ(), pr1.getValidationParameters());
111:
112: doTest(pr1, pr2, pr3);
113:
114: pu2 = new DHPublicKeyParameters(pu1.getY(), pr2);
115: pv2 = new DHPrivateKeyParameters(pv1.getX(), pr2);
116:
117: doTest(pu1, pu2, pu3);
118: doTest(pv1, pv2, pv3);
119:
120: DHValidationParameters vp1 = new DHValidationParameters(
121: new byte[20], 1024);
122: DHValidationParameters vp2 = new DHValidationParameters(
123: new byte[20], 1024);
124: DHValidationParameters vp3 = new DHValidationParameters(
125: new byte[24], 1024);
126:
127: doTest(vp1, vp1, vp3);
128: doTest(vp1, vp2, vp3);
129:
130: byte[] bytes = new byte[20];
131: bytes[0] = 1;
132:
133: vp3 = new DHValidationParameters(bytes, 1024);
134:
135: doTest(vp1, vp2, vp3);
136:
137: vp3 = new DHValidationParameters(new byte[20], 2048);
138:
139: doTest(vp1, vp2, vp3);
140:
141: DHTestKeyParameters k1 = new DHTestKeyParameters(false, null);
142: DHTestKeyParameters k2 = new DHTestKeyParameters(false, null);
143: DHTestKeyParameters k3 = new DHTestKeyParameters(false, pu1
144: .getParameters());
145:
146: doTest(k1, k2, k3);
147: }
148:
149: private void elGamalTest() {
150: BigInteger g512 = new BigInteger(
151: "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc",
152: 16);
153: BigInteger p512 = new BigInteger(
154: "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b",
155: 16);
156:
157: ElGamalParameters dhParams = new ElGamalParameters(p512, g512);
158: ElGamalKeyGenerationParameters params = new ElGamalKeyGenerationParameters(
159: new SecureRandom(), dhParams);
160: ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();
161:
162: kpGen.init(params);
163:
164: AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();
165: ElGamalPublicKeyParameters pu1 = (ElGamalPublicKeyParameters) pair
166: .getPublic();
167: ElGamalPrivateKeyParameters pv1 = (ElGamalPrivateKeyParameters) pair
168: .getPrivate();
169:
170: ElGamalPublicKeyParameters pu2 = new ElGamalPublicKeyParameters(
171: pu1.getY(), pu1.getParameters());
172: ElGamalPrivateKeyParameters pv2 = new ElGamalPrivateKeyParameters(
173: pv1.getX(), pv1.getParameters());
174: ElGamalPublicKeyParameters pu3 = new ElGamalPublicKeyParameters(
175: pv1.getX(), pu1.getParameters());
176: ElGamalPrivateKeyParameters pv3 = new ElGamalPrivateKeyParameters(
177: pu1.getY(), pu1.getParameters());
178:
179: doTest(pu1, pu2, pu3);
180: doTest(pv1, pv2, pv3);
181:
182: ElGamalParameters pr1 = pu1.getParameters();
183: ElGamalParameters pr2 = new ElGamalParameters(pr1.getP(), pr1
184: .getG());
185: ElGamalParameters pr3 = new ElGamalParameters(pr1.getG(), pr1
186: .getP());
187:
188: doTest(pr1, pr2, pr3);
189:
190: pu2 = new ElGamalPublicKeyParameters(pu1.getY(), pr2);
191: pv2 = new ElGamalPrivateKeyParameters(pv1.getX(), pr2);
192:
193: doTest(pu1, pu2, pu3);
194: doTest(pv1, pv2, pv3);
195:
196: ElGamalTestKeyParameters k1 = new ElGamalTestKeyParameters(
197: false, null);
198: ElGamalTestKeyParameters k2 = new ElGamalTestKeyParameters(
199: false, null);
200: ElGamalTestKeyParameters k3 = new ElGamalTestKeyParameters(
201: false, pu1.getParameters());
202:
203: doTest(k1, k2, k3);
204: }
205:
206: private void dsaTest() {
207: BigInteger a = BigInteger.valueOf(1), b = BigInteger.valueOf(2), c = BigInteger
208: .valueOf(3);
209:
210: DSAParameters dsaP1 = new DSAParameters(a, b, c);
211: DSAParameters dsaP2 = new DSAParameters(a, b, c);
212: DSAParameters dsaP3 = new DSAParameters(b, c, a);
213:
214: doTest(dsaP1, dsaP2, dsaP3);
215:
216: DSAValidationParameters vp1 = new DSAValidationParameters(
217: new byte[20], 1024);
218: DSAValidationParameters vp2 = new DSAValidationParameters(
219: new byte[20], 1024);
220: DSAValidationParameters vp3 = new DSAValidationParameters(
221: new byte[24], 1024);
222:
223: doTest(vp1, vp1, vp3);
224: doTest(vp1, vp2, vp3);
225:
226: byte[] bytes = new byte[20];
227: bytes[0] = 1;
228:
229: vp3 = new DSAValidationParameters(bytes, 1024);
230:
231: doTest(vp1, vp2, vp3);
232:
233: vp3 = new DSAValidationParameters(new byte[20], 2048);
234:
235: doTest(vp1, vp2, vp3);
236: }
237:
238: private void gost3410Test() {
239: BigInteger a = BigInteger.valueOf(1), b = BigInteger.valueOf(2), c = BigInteger
240: .valueOf(3);
241:
242: GOST3410Parameters g1 = new GOST3410Parameters(a, b, c);
243: GOST3410Parameters g2 = new GOST3410Parameters(a, b, c);
244: GOST3410Parameters g3 = new GOST3410Parameters(a, c, c);
245:
246: doTest(g1, g2, g3);
247:
248: GOST3410ValidationParameters v1 = new GOST3410ValidationParameters(
249: 100, 1);
250: GOST3410ValidationParameters v2 = new GOST3410ValidationParameters(
251: 100, 1);
252: GOST3410ValidationParameters v3 = new GOST3410ValidationParameters(
253: 101, 1);
254:
255: doTest(v1, v2, v3);
256:
257: v3 = new GOST3410ValidationParameters(100, 2);
258:
259: doTest(v1, v2, v3);
260:
261: v1 = new GOST3410ValidationParameters(100L, 1L);
262: v2 = new GOST3410ValidationParameters(100L, 1L);
263: v3 = new GOST3410ValidationParameters(101L, 1L);
264:
265: doTest(v1, v2, v3);
266:
267: v3 = new GOST3410ValidationParameters(100L, 2L);
268:
269: doTest(v1, v2, v3);
270:
271: }
272:
273: public void performTest() throws Exception {
274: dhTest();
275: elGamalTest();
276: gost3410Test();
277: dsaTest();
278: }
279:
280: public static void main(String[] args) {
281: runTest(new EqualsHashCodeTest());
282: }
283: }
|