001: package org.bouncycastle.jce.provider.test;
002:
003: import java.math.BigInteger;
004: import java.security.AlgorithmParameters;
005: import java.security.KeyPair;
006: import java.security.KeyPairGenerator;
007: import java.security.PrivateKey;
008: import java.security.PublicKey;
009: import java.security.SecureRandom;
010: import java.security.Security;
011:
012: import javax.crypto.Cipher;
013: import javax.crypto.spec.DHParameterSpec;
014:
015: import org.bouncycastle.jce.provider.BouncyCastleProvider;
016: import org.bouncycastle.jce.spec.ECParameterSpec;
017: import org.bouncycastle.jce.spec.IEKeySpec;
018: import org.bouncycastle.jce.spec.IESParameterSpec;
019: import org.bouncycastle.math.ec.ECCurve;
020: import org.bouncycastle.util.encoders.Hex;
021: import org.bouncycastle.util.test.SimpleTest;
022:
023: /**
024: * test for ECIES - Elliptic Curve Integrated Encryption Scheme
025: */
026: public class IESTest extends SimpleTest {
027: private BigInteger g512 = new BigInteger(
028: "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc",
029: 16);
030: private BigInteger p512 = new BigInteger(
031: "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b",
032: 16);
033:
034: IESTest() {
035: }
036:
037: public String getName() {
038: return "IES";
039: }
040:
041: public void performTest() throws Exception {
042: KeyPairGenerator g = KeyPairGenerator
043: .getInstance("ECIES", "BC");
044:
045: ECCurve curve = new ECCurve.Fp(
046: new BigInteger(
047: "883423532389192164791648750360308885314476597252960362792450860609699839"), // q
048: new BigInteger(
049: "7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc",
050: 16), // a
051: new BigInteger(
052: "6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a",
053: 16)); // b
054:
055: ECParameterSpec ecSpec = new ECParameterSpec(
056: curve,
057: curve
058: .decodePoint(Hex
059: .decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
060: new BigInteger(
061: "883423532389192164791648750360308884807550341691627752275345424702807307")); // n
062:
063: g.initialize(ecSpec, new SecureRandom());
064:
065: Cipher c1 = Cipher.getInstance("ECIES", "BC");
066: Cipher c2 = Cipher.getInstance("ECIES", "BC");
067:
068: doTest(g, c1, c2);
069:
070: g = KeyPairGenerator.getInstance("ECIES", "BC");
071:
072: g.initialize(192, new SecureRandom());
073:
074: doTest(g, c1, c2);
075:
076: g = KeyPairGenerator.getInstance("ECIES", "BC");
077:
078: g.initialize(239, new SecureRandom());
079:
080: doTest(g, c1, c2);
081:
082: g = KeyPairGenerator.getInstance("ECIES", "BC");
083:
084: g.initialize(256, new SecureRandom());
085:
086: doTest(g, c1, c2);
087:
088: doDefTest(g, c1, c2);
089:
090: DHParameterSpec dhParams = new DHParameterSpec(p512, g512);
091:
092: c1 = Cipher.getInstance("IES", "BC");
093: c2 = Cipher.getInstance("IES", "BC");
094:
095: g = KeyPairGenerator.getInstance("DH", "BC");
096:
097: g.initialize(dhParams);
098:
099: doTest(g, c1, c2);
100:
101: doDefTest(g, c1, c2);
102: }
103:
104: public void doTest(KeyPairGenerator g, Cipher c1, Cipher c2)
105: throws Exception {
106: //
107: // a side
108: //
109: KeyPair aKeyPair = g.generateKeyPair();
110: PublicKey aPub = aKeyPair.getPublic();
111: PrivateKey aPriv = aKeyPair.getPrivate();
112:
113: //
114: // b side
115: //
116: KeyPair bKeyPair = g.generateKeyPair();
117: PublicKey bPub = bKeyPair.getPublic();
118: PrivateKey bPriv = bKeyPair.getPrivate();
119:
120: //
121: // stream test
122: //
123:
124: IEKeySpec c1Key = new IEKeySpec(aPriv, bPub);
125: IEKeySpec c2Key = new IEKeySpec(bPriv, aPub);
126:
127: byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
128: byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
129:
130: IESParameterSpec param = new IESParameterSpec(d, e, 128);
131:
132: c1.init(Cipher.ENCRYPT_MODE, c1Key, param);
133:
134: c2.init(Cipher.DECRYPT_MODE, c2Key, param);
135:
136: byte[] message = Hex.decode("1234567890abcdef");
137:
138: byte[] out1 = c1.doFinal(message, 0, message.length);
139:
140: byte[] out2 = c2.doFinal(out1, 0, out1.length);
141:
142: if (!areEqual(out2, message)) {
143: fail("stream cipher test failed");
144: }
145: }
146:
147: public void doDefTest(KeyPairGenerator g, Cipher c1, Cipher c2)
148: throws Exception {
149: //
150: // a side
151: //
152: KeyPair aKeyPair = g.generateKeyPair();
153: PublicKey aPub = aKeyPair.getPublic();
154: PrivateKey aPriv = aKeyPair.getPrivate();
155:
156: //
157: // b side
158: //
159: KeyPair bKeyPair = g.generateKeyPair();
160: PublicKey bPub = bKeyPair.getPublic();
161: PrivateKey bPriv = bKeyPair.getPrivate();
162:
163: //
164: // stream test
165: //
166: IEKeySpec c1Key = new IEKeySpec(aPriv, bPub);
167: IEKeySpec c2Key = new IEKeySpec(bPriv, aPub);
168:
169: c1.init(Cipher.ENCRYPT_MODE, c1Key);
170:
171: AlgorithmParameters param = c1.getParameters();
172:
173: c2.init(Cipher.DECRYPT_MODE, c2Key, param);
174:
175: byte[] message = Hex.decode("1234567890abcdef");
176:
177: byte[] out1 = c1.doFinal(message, 0, message.length);
178:
179: byte[] out2 = c2.doFinal(out1, 0, out1.length);
180:
181: if (!areEqual(out2, message)) {
182: fail("stream cipher test failed");
183: }
184:
185: //
186: // int doFinal
187: //
188: int len1 = c1.doFinal(message, 0, message.length, out1, 0);
189:
190: if (len1 != out1.length) {
191: fail("encryption length wrong");
192: }
193:
194: int len2 = c2.doFinal(out1, 0, out1.length, out2, 0);
195:
196: if (len2 != out2.length) {
197: fail("decryption length wrong");
198: }
199:
200: if (!areEqual(out2, message)) {
201: fail("stream cipher test failed");
202: }
203:
204: //
205: // int doFinal with update
206: //
207: len1 = c1.update(message, 0, 2, out1, 0);
208:
209: len1 += c1.doFinal(message, 2, message.length - 2, out1, len1);
210:
211: if (len1 != out1.length) {
212: fail("update encryption length wrong");
213: }
214:
215: len2 = c2.update(out1, 0, 2, out2, 0);
216:
217: len2 += c2.doFinal(out1, 2, out1.length - 2, out2, len2);
218:
219: if (len2 != out2.length) {
220: fail("update decryption length wrong");
221: }
222:
223: if (!areEqual(out2, message)) {
224: fail("update stream cipher test failed");
225: }
226: }
227:
228: public static void main(String[] args) {
229: Security.addProvider(new BouncyCastleProvider());
230:
231: runTest(new IESTest());
232: }
233: }
|