001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
004: import org.bouncycastle.crypto.DataLengthException;
005: import org.bouncycastle.crypto.engines.ElGamalEngine;
006: import org.bouncycastle.crypto.generators.ElGamalKeyPairGenerator;
007: import org.bouncycastle.crypto.generators.ElGamalParametersGenerator;
008: import org.bouncycastle.crypto.params.ElGamalKeyGenerationParameters;
009: import org.bouncycastle.crypto.params.ElGamalParameters;
010: import org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters;
011: import org.bouncycastle.crypto.params.ElGamalPublicKeyParameters;
012: import org.bouncycastle.crypto.params.ParametersWithRandom;
013: import org.bouncycastle.util.test.SimpleTest;
014:
015: import java.math.BigInteger;
016: import java.security.SecureRandom;
017:
018: public class ElGamalTest extends SimpleTest {
019: private BigInteger g512 = new BigInteger(
020: "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc",
021: 16);
022: private BigInteger p512 = new BigInteger(
023: "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b",
024: 16);
025:
026: private BigInteger g768 = new BigInteger(
027: "7c240073c1316c621df461b71ebb0cdcc90a6e5527e5e126633d131f87461c4dc4afc60c2cb0f053b6758871489a69613e2a8b4c8acde23954c08c81cbd36132cfd64d69e4ed9f8e51ed6e516297206672d5c0a69135df0a5dcf010d289a9ca1",
028: 16);
029: private BigInteger p768 = new BigInteger(
030: "8c9dd223debed1b80103b8b309715be009d48860ed5ae9b9d5d8159508efd802e3ad4501a7f7e1cfec78844489148cd72da24b21eddd01aa624291c48393e277cfc529e37075eccef957f3616f962d15b44aeab4039d01b817fde9eaa12fd73f",
031: 16);
032:
033: private BigInteger g1024 = new BigInteger(
034: "1db17639cdf96bc4eabba19454f0b7e5bd4e14862889a725c96eb61048dcd676ceb303d586e30f060dbafd8a571a39c4d823982117da5cc4e0f89c77388b7a08896362429b94a18a327604eb7ff227bffbc83459ade299e57b5f77b50fb045250934938efa145511166e3197373e1b5b1e52de713eb49792bedde722c6717abf",
035: 16);
036: private BigInteger p1024 = new BigInteger(
037: "a00e283b3c624e5b2b4d9fbc2653b5185d99499b00fd1bf244c6f0bb817b4d1c451b2958d62a0f8a38caef059fb5ecd25d75ed9af403f5b5bdab97a642902f824e3c13789fed95fa106ddfe0ff4a707c85e2eb77d49e68f2808bcea18ce128b178cd287c6bc00efa9a1ad2a673fe0dceace53166f75b81d6709d5f8af7c66bb7",
038: 16);
039:
040: public String getName() {
041: return "ElGamal";
042: }
043:
044: private void testEnc(int size, int privateValueSize, BigInteger g,
045: BigInteger p) {
046: ElGamalParameters dhParams = new ElGamalParameters(p, g,
047: privateValueSize);
048: ElGamalKeyGenerationParameters params = new ElGamalKeyGenerationParameters(
049: new SecureRandom(), dhParams);
050: ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();
051:
052: kpGen.init(params);
053:
054: //
055: // generate pair
056: //
057: AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();
058:
059: ElGamalPublicKeyParameters pu = (ElGamalPublicKeyParameters) pair
060: .getPublic();
061: ElGamalPrivateKeyParameters pv = (ElGamalPrivateKeyParameters) pair
062: .getPrivate();
063:
064: checkKeySize(privateValueSize, pv);
065:
066: ElGamalEngine e = new ElGamalEngine();
067:
068: e.init(true, pu);
069:
070: if (e.getOutputBlockSize() != size / 4) {
071: fail(size + " getOutputBlockSize() on encryption failed.");
072: }
073:
074: String message = "This is a test";
075:
076: byte[] pText = message.getBytes();
077: byte[] cText = e.processBlock(pText, 0, pText.length);
078:
079: e.init(false, pv);
080:
081: if (e.getOutputBlockSize() != (size / 8) - 1) {
082: fail(size + " getOutputBlockSize() on decryption failed.");
083: }
084:
085: pText = e.processBlock(cText, 0, cText.length);
086:
087: if (!message.equals(new String(pText))) {
088: fail(size + " bit test failed");
089: }
090:
091: e.init(true, pu);
092:
093: byte[] bytes = new byte[e.getInputBlockSize() + 2];
094:
095: try {
096: e.processBlock(bytes, 0, bytes.length);
097:
098: fail("out of range block not detected");
099: } catch (DataLengthException ex) {
100: // expected
101: }
102:
103: try {
104: bytes[0] = (byte) 0xff;
105:
106: e.processBlock(bytes, 0, bytes.length - 1);
107:
108: fail("out of range block not detected");
109: } catch (DataLengthException ex) {
110: // expected
111: }
112:
113: try {
114: bytes[0] = (byte) 0x7f;
115:
116: e.processBlock(bytes, 0, bytes.length - 1);
117: } catch (DataLengthException ex) {
118: fail("in range block failed");
119: }
120: }
121:
122: private void checkKeySize(int privateValueSize,
123: ElGamalPrivateKeyParameters priv) {
124: if (privateValueSize != 0) {
125: if (priv.getX().bitLength() != privateValueSize) {
126: fail("limited key check failed for key size "
127: + privateValueSize);
128: }
129: }
130: }
131:
132: /**
133: * this test is can take quiet a while
134: */
135: private void testGeneration(int size) {
136: ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
137:
138: pGen.init(size, 10, new SecureRandom());
139:
140: ElGamalParameters elParams = pGen.generateParameters();
141:
142: if (elParams.getL() != 0) {
143: fail("ElGamalParametersGenerator failed to set L to 0 in generated ElGamalParameters");
144: }
145:
146: ElGamalKeyGenerationParameters params = new ElGamalKeyGenerationParameters(
147: new SecureRandom(), elParams);
148:
149: ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();
150:
151: kpGen.init(params);
152:
153: //
154: // generate first pair
155: //
156: AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();
157:
158: ElGamalPublicKeyParameters pu = (ElGamalPublicKeyParameters) pair
159: .getPublic();
160: ElGamalPrivateKeyParameters pv = (ElGamalPrivateKeyParameters) pair
161: .getPrivate();
162:
163: ElGamalEngine e = new ElGamalEngine();
164:
165: e.init(true, new ParametersWithRandom(pu, new SecureRandom()));
166:
167: String message = "This is a test";
168:
169: byte[] pText = message.getBytes();
170: byte[] cText = e.processBlock(pText, 0, pText.length);
171:
172: e.init(false, pv);
173:
174: pText = e.processBlock(cText, 0, cText.length);
175:
176: if (!message.equals(new String(pText))) {
177: fail("generation test failed");
178: }
179: }
180:
181: private void testInitCheck() {
182: try {
183: new ElGamalEngine().processBlock(new byte[] { 1 }, 0, 1);
184: fail("failed initialisation check");
185: } catch (IllegalStateException e) {
186: // expected
187: }
188: }
189:
190: public void performTest() {
191: testEnc(512, 0, g512, p512);
192: testEnc(768, 0, g768, p768);
193: testEnc(1024, 0, g1024, p1024);
194:
195: testEnc(512, 64, g512, p512);
196: testEnc(768, 128, g768, p768);
197:
198: //
199: // generation test.
200: //
201: testGeneration(258);
202:
203: testInitCheck();
204: }
205:
206: public static void main(String[] args) {
207: runTest(new ElGamalTest());
208: }
209: }
|