001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.jce.provider.BouncyCastleProvider;
004: import org.bouncycastle.util.encoders.Hex;
005: import org.bouncycastle.util.test.SimpleTest;
006:
007: import javax.crypto.Cipher;
008: import javax.crypto.interfaces.DHPrivateKey;
009: import javax.crypto.interfaces.DHPublicKey;
010: import javax.crypto.spec.DHParameterSpec;
011: import java.io.ByteArrayInputStream;
012: import java.io.ByteArrayOutputStream;
013: import java.io.ObjectInputStream;
014: import java.io.ObjectOutputStream;
015: import java.math.BigInteger;
016: import java.security.AlgorithmParameterGenerator;
017: import java.security.AlgorithmParameters;
018: import java.security.KeyFactory;
019: import java.security.KeyPair;
020: import java.security.KeyPairGenerator;
021: import java.security.SecureRandom;
022: import java.security.Security;
023: import java.security.spec.PKCS8EncodedKeySpec;
024: import java.security.spec.X509EncodedKeySpec;
025:
026: public class ElGamalTest extends SimpleTest {
027: private BigInteger g512 = new BigInteger(
028: "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc",
029: 16);
030: private BigInteger p512 = new BigInteger(
031: "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b",
032: 16);
033:
034: private BigInteger g768 = new BigInteger(
035: "7c240073c1316c621df461b71ebb0cdcc90a6e5527e5e126633d131f87461c4dc4afc60c2cb0f053b6758871489a69613e2a8b4c8acde23954c08c81cbd36132cfd64d69e4ed9f8e51ed6e516297206672d5c0a69135df0a5dcf010d289a9ca1",
036: 16);
037: private BigInteger p768 = new BigInteger(
038: "8c9dd223debed1b80103b8b309715be009d48860ed5ae9b9d5d8159508efd802e3ad4501a7f7e1cfec78844489148cd72da24b21eddd01aa624291c48393e277cfc529e37075eccef957f3616f962d15b44aeab4039d01b817fde9eaa12fd73f",
039: 16);
040:
041: private BigInteger g1024 = new BigInteger(
042: "1db17639cdf96bc4eabba19454f0b7e5bd4e14862889a725c96eb61048dcd676ceb303d586e30f060dbafd8a571a39c4d823982117da5cc4e0f89c77388b7a08896362429b94a18a327604eb7ff227bffbc83459ade299e57b5f77b50fb045250934938efa145511166e3197373e1b5b1e52de713eb49792bedde722c6717abf",
043: 16);
044: private BigInteger p1024 = new BigInteger(
045: "a00e283b3c624e5b2b4d9fbc2653b5185d99499b00fd1bf244c6f0bb817b4d1c451b2958d62a0f8a38caef059fb5ecd25d75ed9af403f5b5bdab97a642902f824e3c13789fed95fa106ddfe0ff4a707c85e2eb77d49e68f2808bcea18ce128b178cd287c6bc00efa9a1ad2a673fe0dceace53166f75b81d6709d5f8af7c66bb7",
046: 16);
047:
048: public String getName() {
049: return "ElGamal";
050: }
051:
052: private void testGP(int size, int privateValueSize, BigInteger g,
053: BigInteger p) throws Exception {
054: DHParameterSpec elParams = new DHParameterSpec(p, g,
055: privateValueSize);
056: KeyPairGenerator keyGen = KeyPairGenerator.getInstance(
057: "ElGamal", "BC");
058: byte[] in = "This is a test".getBytes();
059:
060: keyGen.initialize(elParams);
061:
062: KeyPair keyPair = keyGen.generateKeyPair();
063: SecureRandom rand = new SecureRandom();
064:
065: checkKeySize(privateValueSize, keyPair);
066:
067: Cipher cipher = Cipher.getInstance("ElGamal", "BC");
068:
069: cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), rand);
070:
071: if (cipher.getOutputSize(in.length) != (size / 8) * 2) {
072: fail("getOutputSize wrong on encryption");
073: }
074:
075: byte[] out = cipher.doFinal(in);
076:
077: cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
078:
079: if (cipher.getOutputSize(out.length) != (size / 8) - 1) {
080: fail("getOutputSize wrong on decryption");
081: }
082:
083: //
084: // No Padding - maximum length
085: //
086: byte[] modBytes = ((DHPublicKey) keyPair.getPublic())
087: .getParams().getP().toByteArray();
088: byte[] maxInput = new byte[modBytes.length - 1];
089:
090: maxInput[0] |= 0x7f;
091:
092: cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), rand);
093:
094: out = cipher.doFinal(maxInput);
095:
096: cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
097:
098: out = cipher.doFinal(out);
099:
100: if (!areEqual(out, maxInput)) {
101: fail("NoPadding test failed on decrypt expected "
102: + new String(Hex.encode(maxInput)) + " got "
103: + new String(Hex.encode(out)));
104: }
105:
106: //
107: // encrypt/decrypt
108: //
109:
110: Cipher c1 = Cipher.getInstance("ElGamal", "BC");
111: Cipher c2 = Cipher.getInstance("ElGamal", "BC");
112:
113: c1.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), rand);
114:
115: byte[] out1 = c1.doFinal(in);
116:
117: c2.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
118:
119: byte[] out2 = c2.doFinal(out1);
120:
121: if (!areEqual(in, out2)) {
122: fail(size + " encrypt test failed");
123: }
124:
125: //
126: // encrypt/decrypt with update
127: //
128: int outLen = c1.update(in, 0, 2, out1, 0);
129:
130: outLen += c1.doFinal(in, 2, in.length - 2, out1, outLen);
131:
132: outLen = c2.update(out1, 0, 2, out2, 0);
133:
134: outLen += c2.doFinal(out1, 2, out1.length - 2, out2, outLen);
135:
136: if (!areEqual(in, out2)) {
137: fail(size + " encrypt with update test failed");
138: }
139:
140: //
141: // public key encoding test
142: //
143: byte[] pubEnc = keyPair.getPublic().getEncoded();
144: KeyFactory keyFac = KeyFactory.getInstance("ElGamal", "BC");
145: X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(pubEnc);
146: DHPublicKey pubKey = (DHPublicKey) keyFac
147: .generatePublic(pubX509);
148: DHParameterSpec spec = pubKey.getParams();
149:
150: if (!spec.getG().equals(elParams.getG())
151: || !spec.getP().equals(elParams.getP())) {
152: fail(size
153: + " bit public key encoding/decoding test failed on parameters");
154: }
155:
156: if (!((DHPublicKey) keyPair.getPublic()).getY().equals(
157: pubKey.getY())) {
158: fail(size
159: + " bit public key encoding/decoding test failed on y value");
160: }
161:
162: //
163: // public key serialisation test
164: //
165: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
166: ObjectOutputStream oOut = new ObjectOutputStream(bOut);
167:
168: oOut.writeObject(keyPair.getPublic());
169:
170: ByteArrayInputStream bIn = new ByteArrayInputStream(bOut
171: .toByteArray());
172: ObjectInputStream oIn = new ObjectInputStream(bIn);
173:
174: pubKey = (DHPublicKey) oIn.readObject();
175: spec = pubKey.getParams();
176:
177: if (!spec.getG().equals(elParams.getG())
178: || !spec.getP().equals(elParams.getP())) {
179: fail(size
180: + " bit public key serialisation test failed on parameters");
181: }
182:
183: if (!((DHPublicKey) keyPair.getPublic()).getY().equals(
184: pubKey.getY())) {
185: fail(size
186: + " bit public key serialisation test failed on y value");
187: }
188:
189: //
190: // private key encoding test
191: //
192: byte[] privEnc = keyPair.getPrivate().getEncoded();
193: PKCS8EncodedKeySpec privPKCS8 = new PKCS8EncodedKeySpec(privEnc);
194: DHPrivateKey privKey = (DHPrivateKey) keyFac
195: .generatePrivate(privPKCS8);
196:
197: spec = privKey.getParams();
198:
199: if (!spec.getG().equals(elParams.getG())
200: || !spec.getP().equals(elParams.getP())) {
201: fail(size
202: + " bit private key encoding/decoding test failed on parameters");
203: }
204:
205: if (!((DHPrivateKey) keyPair.getPrivate()).getX().equals(
206: privKey.getX())) {
207: fail(size
208: + " bit private key encoding/decoding test failed on y value");
209: }
210:
211: //
212: // private key serialisation test
213: //
214: bOut = new ByteArrayOutputStream();
215: oOut = new ObjectOutputStream(bOut);
216:
217: oOut.writeObject(keyPair.getPrivate());
218:
219: bIn = new ByteArrayInputStream(bOut.toByteArray());
220: oIn = new ObjectInputStream(bIn);
221:
222: privKey = (DHPrivateKey) oIn.readObject();
223: spec = privKey.getParams();
224:
225: if (!spec.getG().equals(elParams.getG())
226: || !spec.getP().equals(elParams.getP())) {
227: fail(size
228: + " bit private key serialisation test failed on parameters");
229: }
230:
231: if (!((DHPrivateKey) keyPair.getPrivate()).getX().equals(
232: privKey.getX())) {
233: fail(size
234: + " bit private key serialisation test failed on y value");
235: }
236: }
237:
238: private void checkKeySize(int privateValueSize, KeyPair aKeyPair) {
239: if (privateValueSize != 0) {
240: DHPrivateKey key = (DHPrivateKey) aKeyPair.getPrivate();
241:
242: if (key.getX().bitLength() != privateValueSize) {
243: fail("limited key check failed for key size "
244: + privateValueSize);
245: }
246: }
247: }
248:
249: private void testRandom(int size) throws Exception {
250: AlgorithmParameterGenerator a = AlgorithmParameterGenerator
251: .getInstance("ElGamal", "BC");
252: a.init(size, new SecureRandom());
253: AlgorithmParameters params = a.generateParameters();
254:
255: byte[] encodeParams = params.getEncoded();
256:
257: AlgorithmParameters a2 = AlgorithmParameters.getInstance(
258: "ElGamal", "BC");
259: a2.init(encodeParams);
260:
261: // a and a2 should be equivalent!
262: byte[] encodeParams_2 = a2.getEncoded();
263:
264: if (!areEqual(encodeParams, encodeParams_2)) {
265: fail(this .getName() + ": encode/decode parameters failed");
266: }
267:
268: DHParameterSpec elP = (DHParameterSpec) params
269: .getParameterSpec(DHParameterSpec.class);
270:
271: testGP(size, 0, elP.getG(), elP.getP());
272: }
273:
274: public void performTest() throws Exception {
275: testGP(512, 0, g512, p512);
276: testGP(768, 0, g768, p768);
277: testGP(1024, 0, g1024, p1024);
278:
279: testGP(512, 64, g512, p512);
280: testGP(768, 128, g768, p768);
281: testGP(1024, 256, g1024, p1024);
282:
283: testRandom(256);
284: }
285:
286: public static void main(String[] args) {
287: Security.addProvider(new BouncyCastleProvider());
288:
289: runTest(new ElGamalTest());
290: }
291: }
|