001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
004: import org.bouncycastle.crypto.BufferedBlockCipher;
005: import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
006: import org.bouncycastle.crypto.digests.SHA1Digest;
007: import org.bouncycastle.crypto.engines.IESEngine;
008: import org.bouncycastle.crypto.engines.TwofishEngine;
009: import org.bouncycastle.crypto.generators.KDF2BytesGenerator;
010: import org.bouncycastle.crypto.macs.HMac;
011: import org.bouncycastle.crypto.modes.CBCBlockCipher;
012: import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
013: import org.bouncycastle.crypto.params.ECDomainParameters;
014: import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
015: import org.bouncycastle.crypto.params.ECPublicKeyParameters;
016: import org.bouncycastle.crypto.params.IESParameters;
017: import org.bouncycastle.crypto.params.IESWithCipherParameters;
018: import org.bouncycastle.math.ec.ECCurve;
019: import org.bouncycastle.util.encoders.Hex;
020: import org.bouncycastle.util.test.SimpleTest;
021:
022: import java.math.BigInteger;
023:
024: /**
025: * test for ECIES - Elliptic Curve Integrated Encryption Scheme
026: */
027: public class ECIESTest extends SimpleTest {
028: ECIESTest() {
029: }
030:
031: public String getName() {
032: return "ECIES";
033: }
034:
035: public void performTest() throws Exception {
036: ECCurve.Fp curve = new ECCurve.Fp(
037: new BigInteger(
038: "6277101735386680763835789423207666416083908700390324961279"), // q
039: new BigInteger(
040: "fffffffffffffffffffffffffffffffefffffffffffffffc",
041: 16), // a
042: new BigInteger(
043: "64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1",
044: 16)); // b
045:
046: ECDomainParameters params = new ECDomainParameters(
047: curve,
048: curve
049: .decodePoint(Hex
050: .decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G
051: new BigInteger(
052: "6277101735386680763835789423176059013767194773182842284081")); // n
053:
054: ECPrivateKeyParameters priKey = new ECPrivateKeyParameters(
055: new BigInteger(
056: "651056770906015076056810763456358567190100156695615665659"), // d
057: params);
058:
059: ECPublicKeyParameters pubKey = new ECPublicKeyParameters(
060: curve
061: .decodePoint(Hex
062: .decode("0262b12d60690cdcf330babab6e69763b471f994dd702d16a5")), // Q
063: params);
064:
065: AsymmetricCipherKeyPair p1 = new AsymmetricCipherKeyPair(
066: pubKey, priKey);
067: AsymmetricCipherKeyPair p2 = new AsymmetricCipherKeyPair(
068: pubKey, priKey);
069:
070: //
071: // stream test
072: //
073: IESEngine i1 = new IESEngine(new ECDHBasicAgreement(),
074: new KDF2BytesGenerator(new SHA1Digest()), new HMac(
075: new SHA1Digest()));
076: IESEngine i2 = new IESEngine(new ECDHBasicAgreement(),
077: new KDF2BytesGenerator(new SHA1Digest()), new HMac(
078: new SHA1Digest()));
079: byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
080: byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
081: IESParameters p = new IESParameters(d, e, 64);
082:
083: i1.init(true, p1.getPrivate(), p2.getPublic(), p);
084: i2.init(false, p2.getPrivate(), p1.getPublic(), p);
085:
086: byte[] message = Hex.decode("1234567890abcdef");
087:
088: byte[] out1 = i1.processBlock(message, 0, message.length);
089:
090: if (!areEqual(
091: out1,
092: Hex
093: .decode("2442ae1fbf90dd9c06b0dcc3b27e69bd11c9aee4ad4cfc9e50eceb44"))) {
094: fail("stream cipher test failed on enc");
095: }
096:
097: byte[] out2 = i2.processBlock(out1, 0, out1.length);
098:
099: if (!areEqual(out2, message)) {
100: fail("stream cipher test failed");
101: }
102:
103: //
104: // twofish with CBC
105: //
106: BufferedBlockCipher c1 = new PaddedBufferedBlockCipher(
107: new CBCBlockCipher(new TwofishEngine()));
108: BufferedBlockCipher c2 = new PaddedBufferedBlockCipher(
109: new CBCBlockCipher(new TwofishEngine()));
110: i1 = new IESEngine(new ECDHBasicAgreement(),
111: new KDF2BytesGenerator(new SHA1Digest()), new HMac(
112: new SHA1Digest()), c1);
113: i2 = new IESEngine(new ECDHBasicAgreement(),
114: new KDF2BytesGenerator(new SHA1Digest()), new HMac(
115: new SHA1Digest()), c2);
116: d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
117: e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
118: p = new IESWithCipherParameters(d, e, 64, 128);
119:
120: i1.init(true, p1.getPrivate(), p2.getPublic(), p);
121: i2.init(false, p2.getPrivate(), p1.getPublic(), p);
122:
123: message = Hex.decode("1234567890abcdef");
124:
125: out1 = i1.processBlock(message, 0, message.length);
126:
127: if (!areEqual(
128: out1,
129: Hex
130: .decode("2ea288651e21576215f2424bbb3f68816e282e3931b44bd1c429ebdb5f1b290cf1b13309"))) {
131: fail("twofish cipher test failed on enc");
132: }
133:
134: out2 = i2.processBlock(out1, 0, out1.length);
135:
136: if (!areEqual(out2, message)) {
137: fail("twofish cipher test failed");
138: }
139: }
140:
141: public static void main(String[] args) {
142: runTest(new ECIESTest());
143: }
144: }
|