aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/prov/src/test/jdk1.4/org/spongycastle/jce/provider/test/ECIESTest.java
blob: 25af8f54add4b7c3beca3dcfcf21b6d18fab6c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package org.spongycastle.jce.provider.test;

import java.math.BigInteger;
import java.security.AlgorithmParameters;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;

import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.jce.spec.ECParameterSpec;
import org.spongycastle.jce.spec.IEKeySpec;
import org.spongycastle.jce.spec.IESParameterSpec;
import org.spongycastle.math.ec.ECCurve;
import org.spongycastle.util.encoders.Hex;
import org.spongycastle.util.test.SimpleTestResult;
import org.spongycastle.util.test.Test;
import org.spongycastle.util.test.TestResult;

/**
 * test for ECIES - Elliptic Curve Integrated Encryption Scheme
 */
public class ECIESTest
    implements Test
{
    ECIESTest()
    {
    }

    public String getName()
    {
        return "ECIES";
    }

    private boolean sameAs(
        byte[]  a,
        byte[]  b)
    {
        if (a.length != b.length)
        {
            return false;
        }

        for (int i = 0; i != a.length; i++)
        {
            if (a[i] != b[i])
            {
                return false;
            }
        }

        return true;
    }

    public TestResult perform()
    {
        TestResult  res;

        try 
        {
            KeyPairGenerator    g = KeyPairGenerator.getInstance("ECIES", "SC");

            ECCurve curve = new ECCurve.Fp(
                    new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q
                    new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
                    new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b

            ECParameterSpec ecSpec = new ECParameterSpec(
                    curve,
                    curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
                    new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n

            g.initialize(ecSpec, new SecureRandom());

            res = performTest(g);
            if (!res.isSuccessful())
            {
                return res;
            }

            g = KeyPairGenerator.getInstance("ECIES", "SC");

            g.initialize(192, new SecureRandom());

            res = performTest(g);
            if (!res.isSuccessful())
            {
                return res;
            }

            g = KeyPairGenerator.getInstance("ECIES", "SC");

            g.initialize(239, new SecureRandom());

            res = performTest(g);
            if (!res.isSuccessful())
            {
                return res;
            }

            g = KeyPairGenerator.getInstance("ECIES", "SC");

            g.initialize(256, new SecureRandom());

            res = performTest(g);
            if (!res.isSuccessful())
            {
                return res;
            }

            res = performDefTest(g);
            if (!res.isSuccessful())
            {
                return res;
            }
        }
        catch (Exception ex)
        {
            return new SimpleTestResult(false, this.getName() + ": stream cipher test exception " + ex.toString());
        }

        return new SimpleTestResult(true, this.getName() + ": Okay");
    }

    public TestResult performTest(
        KeyPairGenerator    g)
    {
        try 
        {
            //
            // a side
            //
            KeyPair     aKeyPair = g.generateKeyPair();
            PublicKey   aPub = aKeyPair.getPublic();
            PrivateKey  aPriv = aKeyPair.getPrivate();

            //
            // b side
            //
            KeyPair     bKeyPair = g.generateKeyPair();
            PublicKey   bPub = bKeyPair.getPublic();
            PrivateKey  bPriv = bKeyPair.getPrivate();

            //
            // stream test
            //
            Cipher c1 = Cipher.getInstance("ECIES", "SC");
            Cipher c2 = Cipher.getInstance("ECIES", "SC");

            IEKeySpec   c1Key = new IEKeySpec(aPriv, bPub);
            IEKeySpec   c2Key = new IEKeySpec(bPriv, aPub);

            byte[]  d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            byte[]  e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };

            IESParameterSpec param = new IESParameterSpec(d, e, 128);

            c1.init(Cipher.ENCRYPT_MODE, c1Key, param);

            c2.init(Cipher.DECRYPT_MODE, c2Key, param);

            byte[] message = Hex.decode("1234567890abcdef");

            byte[]   out1 = c1.doFinal(message, 0, message.length);

            byte[]   out2 = c2.doFinal(out1, 0, out1.length);

            if (!sameAs(out2, message))
            {
                return new SimpleTestResult(false, this.getName() + ": stream cipher test failed");
            }
        }
        catch (Exception ex)
        {
            return new SimpleTestResult(false, this.getName() + ": stream cipher test exception " + ex.toString());
        }

        return new SimpleTestResult(true, this.getName() + ": Okay");
    }

    public TestResult performDefTest(
        KeyPairGenerator    g)
    {
        try 
        {
            //
            // a side
            //
            KeyPair     aKeyPair = g.generateKeyPair();
            PublicKey   aPub = aKeyPair.getPublic();
            PrivateKey  aPriv = aKeyPair.getPrivate();

            //
            // b side
            //
            KeyPair     bKeyPair = g.generateKeyPair();
            PublicKey   bPub = bKeyPair.getPublic();
            PrivateKey  bPriv = bKeyPair.getPrivate();

            //
            // stream test
            //
            Cipher c1 = Cipher.getInstance("ECIES", "SC");
            Cipher c2 = Cipher.getInstance("ECIES", "SC");

            IEKeySpec   c1Key = new IEKeySpec(aPriv, bPub);
            IEKeySpec   c2Key = new IEKeySpec(bPriv, aPub);

            c1.init(Cipher.ENCRYPT_MODE, c1Key);

            AlgorithmParameters param = c1.getParameters();

            c2.init(Cipher.DECRYPT_MODE, c2Key, param);

            byte[] message = Hex.decode("1234567890abcdef");

            byte[]   out1 = c1.doFinal(message, 0, message.length);

            byte[]   out2 = c2.doFinal(out1, 0, out1.length);

            if (!sameAs(out2, message))
            {
                return new SimpleTestResult(false, this.getName() + ": stream cipher test failed");
            }
        }
        catch (Exception ex)
        {
            return new SimpleTestResult(false, this.getName() + ": stream cipher test exception " + ex.toString());
        }

        return new SimpleTestResult(true, this.getName() + ": Okay");
    }

    public static void main(
        String[]    args)
    {
        Security.addProvider(new BouncyCastleProvider());

        ECIESTest    test = new ECIESTest();
        TestResult   result = test.perform();

        System.out.println(result);
    }
}