jdk/test/java/security/spec/ECCBasic.java
author alanb
Mon, 11 Apr 2011 12:52:39 +0100
changeset 9252 9484925c9c2b
parent 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4794996
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Ensure basic functionality of these new ECC classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class ECCBasic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static final BigInteger ZERO = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static final BigInteger ONE = BigInteger.ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static final BigInteger TEN = BigInteger.TEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final ECFieldFp FP = new ECFieldFp(TEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final int F2M_M = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final int[] F2M_KS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        F2M_KS = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        F2M_KS[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final BigInteger F2M_RP = BigInteger.valueOf(19);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final ECFieldF2m F2M = new ECFieldF2m(F2M_M, F2M_KS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final EllipticCurve CURVE = new EllipticCurve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        (new ECFieldFp(TEN), ONE, ONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final ECPoint POINT = new ECPoint(ONE, TEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final BigInteger ORDER = ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int COFACTOR = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final ECParameterSpec PARAMS = new ECParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        (CURVE, POINT, ORDER, COFACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final ECGenParameterSpec GENPARAMS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        new ECGenParameterSpec("prime192v1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final ECPrivateKeySpec PRIV_KEY = new ECPrivateKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        (TEN, PARAMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final ECPublicKeySpec PUB_KEY = new ECPublicKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        (POINT, PARAMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static void testECFieldFp() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.out.println("Testing ECFieldFp(BigInteger)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            new ECFieldFp(ZERO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new Exception("...should throw IAE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println("...expected IAE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            new ECFieldFp(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new Exception("...should throw NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.out.println("...expected NPE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (TEN.equals(FP.getP()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new Exception("...error in getP()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (FP.getFieldSize() != TEN.bitLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new Exception("...error in getFieldSize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static void testECFieldF2m() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // invalid parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int mBad = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int[] ksBad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i=0; i<7; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.out.print("Testing ECFieldF2m");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    System.out.println("(int)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    new ECFieldF2m(mBad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    System.out.println("(int, BigInteger)#1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    new ECFieldF2m(mBad, F2M_RP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    System.out.println("(int, BigInteger)#2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    new ECFieldF2m(mBad, F2M_RP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    System.out.println("(int, int[])#1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    new ECFieldF2m(mBad, F2M_KS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    ksBad = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    System.out.println("(int, int[])#2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    new ECFieldF2m(F2M_M, ksBad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    ksBad = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    ksBad[0] = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    System.out.println("(int, int[])#3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    new ECFieldF2m(F2M_M, ksBad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                case 6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    ksBad = new int[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    ksBad[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    ksBad[1] = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    ksBad[2] = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    System.out.println("(int, int[])#4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    new ECFieldF2m(F2M_M, ksBad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                throw new Exception("...should throw IAE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                System.out.println("...expected IAE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (int i=0; i<2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.print("Testing ECFieldF2m");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    System.out.println("(int, BigInteger)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    new ECFieldF2m(F2M_M, (BigInteger) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    System.out.println("(int, int[])#1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    new ECFieldF2m(F2M_M, (int[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                throw new Exception("...should throw NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                System.out.println("...expected NPE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (F2M_RP.compareTo(F2M.getReductionPolynomial()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new Exception("...error in getReductionPolynomial()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        ECFieldF2m field = new ECFieldF2m(F2M_M, F2M_RP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (!(Arrays.equals(F2M_KS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                            field.getMidTermsOfReductionPolynomial()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new Exception("...error in getMidTermsOfReductionPolynomial()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (field.getFieldSize() != F2M_M) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new Exception("...error in getFieldSize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static void testECParameterSpec() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        System.out.println("Testing ECParameterSpec(...)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        for (int i = 0; i < 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    System.out.println("with zero order");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    new ECParameterSpec(CURVE, POINT, ZERO, COFACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    System.out.println("with negative cofactor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    new ECParameterSpec(CURVE, POINT, ORDER, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                throw new Exception("...should throw IAE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                System.out.println("...expected IAE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    System.out.println("with null curve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    new ECParameterSpec(null, POINT, ORDER, COFACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    System.out.println("with null generator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    new ECParameterSpec(CURVE, null, ORDER, COFACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    System.out.println("with null order");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    new ECParameterSpec(CURVE, POINT, null, COFACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                throw new Exception("...should throw NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                System.out.println("...expected NPE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (!(CURVE.equals(PARAMS.getCurve()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new Exception("...error in getCurve()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (!(POINT.equals(PARAMS.getGenerator()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new Exception("...error in getGenerator()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (!(ORDER.equals(PARAMS.getOrder()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new Exception("...error in getOrder()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (COFACTOR != PARAMS.getCofactor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new Exception("...error in getCofactor()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private static void testECGenParameterSpec() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        System.out.println("Testing ECGenParameterSpec(String)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            new ECGenParameterSpec(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new Exception("...should throw NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            System.out.println("...expected NPE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (!("prime192v1".equals(GENPARAMS.getName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new Exception("...error in getName()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private static void testECPoint() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        System.out.println("Testing ECParameterSpec(...)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (int i = 0; i < 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    System.out.println("with null x-coordinate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    new ECPoint(null, TEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    System.out.println("with null y-coordinate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    new ECPoint(ONE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                throw new Exception("...should throw NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                System.out.println("...expected NPE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (!(ONE.equals(POINT.getAffineX()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new Exception("...error in getAffineX()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (!(TEN.equals(POINT.getAffineY()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new Exception("...error in getAffineY()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private static void testECPublicKeySpec() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        System.out.println("Testing ECPublicKeySpec(...)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        for (int i = 0; i < 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    System.out.println("with null public value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    new ECPublicKeySpec(null, PARAMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    System.out.println("with null params");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    new ECPublicKeySpec(POINT, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw new Exception("...should throw NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                System.out.println("...expected NPE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (!(POINT.equals(PUB_KEY.getW()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new Exception("...error in getW()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (!(PARAMS.equals(PUB_KEY.getParams()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            throw new Exception("...error in getParams()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static void testECPrivateKeySpec() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        System.out.println("Testing ECPrivateKeySpec(...)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        for (int i = 0; i < 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    System.out.println("with null private value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    new ECPrivateKeySpec(null, PARAMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    System.out.println("with null param");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    new ECPrivateKeySpec(ONE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                throw new Exception("...should throw NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                System.out.println("...expected NPE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (!(TEN.equals(PRIV_KEY.getS()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new Exception("...error in getS()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (!(PARAMS.equals(PRIV_KEY.getParams()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new Exception("...error in getParams()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private static void testEllipticCurve() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        System.out.println("Testing EllipticCurve(...)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        for (int j = 0; j < 2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            BigInteger a = ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            BigInteger b = ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (j == 0) { // test a out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                System.out.println("with a's value out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                a = BigInteger.valueOf(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            } else { // test b out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                System.out.println("with b's value out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                b = BigInteger.valueOf(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            System.out.println(">> over ECFieldFp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        new EllipticCurve(FP, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        new EllipticCurve(FP, a, b, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        new EllipticCurve(FP, a, b, new byte[8]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    throw new Exception("...should throw IAE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    System.out.println("...expected IAE thrown for #" + (i+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            System.out.println(">> over ECFieldF2m");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        new EllipticCurve(F2M, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        new EllipticCurve(F2M, a, b, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        new EllipticCurve(F2M, a, b, new byte[8]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    throw new Exception("...should throw IAE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    System.out.println("...expected IAE thrown for #" + (i+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        for (int i = 0; i < 6; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    new EllipticCurve(null, ONE, TEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    new EllipticCurve(FP, null, TEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    new EllipticCurve(FP, ONE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    new EllipticCurve(null, ONE, TEN, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    new EllipticCurve(FP, null, TEN, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    new EllipticCurve(FP, ONE, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                throw new Exception("...should throw NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                System.out.println("...expected NPE thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (!(FP.equals(CURVE.getField()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new Exception("...error in getField()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (!(ONE.equals(CURVE.getA()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            throw new Exception("...error in getA()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (!(ONE.equals(CURVE.getB()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            throw new Exception("...error in getB()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (!(CURVE.equals(new EllipticCurve(FP, ONE, ONE, null)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            throw new Exception("...error in equals()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private static void testAllHashCode() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // make sure no unexpected exception when hashCode() is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        FP.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        F2M.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        GENPARAMS.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        PARAMS.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        POINT.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        PRIV_KEY.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        PUB_KEY.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        CURVE.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        testECFieldFp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        testECFieldF2m();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        testECParameterSpec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        testECGenParameterSpec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        testECPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        testECPrivateKeySpec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        testECPublicKeySpec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        testEllipticCurve();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        testAllHashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        System.out.println("All Test Passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
}