0N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4794996
0N/A * @summary Ensure basic functionality of these new ECC classes.
0N/A * @author Valerie Peng
0N/A */
0N/Aimport java.math.BigInteger;
0N/Aimport java.util.Arrays;
0N/Aimport java.security.*;
0N/Aimport java.security.spec.*;
0N/A
0N/Apublic class ECCBasic {
0N/A private static final BigInteger ZERO = BigInteger.ZERO;
0N/A private static final BigInteger ONE = BigInteger.ONE;
0N/A private static final BigInteger TEN = BigInteger.TEN;
0N/A private static final ECFieldFp FP = new ECFieldFp(TEN);
0N/A private static final int F2M_M = 4;
0N/A private static final int[] F2M_KS;
0N/A static {
0N/A F2M_KS = new int[1];
0N/A F2M_KS[0] = 1;
0N/A }
0N/A private static final BigInteger F2M_RP = BigInteger.valueOf(19);
0N/A private static final ECFieldF2m F2M = new ECFieldF2m(F2M_M, F2M_KS);
0N/A private static final EllipticCurve CURVE = new EllipticCurve
0N/A (new ECFieldFp(TEN), ONE, ONE);
0N/A private static final ECPoint POINT = new ECPoint(ONE, TEN);
0N/A private static final BigInteger ORDER = ONE;
0N/A private static final int COFACTOR = 3;
0N/A private static final ECParameterSpec PARAMS = new ECParameterSpec
0N/A (CURVE, POINT, ORDER, COFACTOR);
0N/A private static final ECGenParameterSpec GENPARAMS =
0N/A new ECGenParameterSpec("prime192v1");
0N/A private static final ECPrivateKeySpec PRIV_KEY = new ECPrivateKeySpec
0N/A (TEN, PARAMS);
0N/A private static final ECPublicKeySpec PUB_KEY = new ECPublicKeySpec
0N/A (POINT, PARAMS);
0N/A
0N/A private static void testECFieldFp() throws Exception {
0N/A System.out.println("Testing ECFieldFp(BigInteger)");
0N/A try {
0N/A new ECFieldFp(ZERO);
0N/A throw new Exception("...should throw IAE");
0N/A } catch (IllegalArgumentException iae) {
0N/A System.out.println("...expected IAE thrown");
0N/A }
0N/A try {
0N/A new ECFieldFp(null);
0N/A throw new Exception("...should throw NPE");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("...expected NPE thrown");
0N/A }
0N/A if (TEN.equals(FP.getP()) == false) {
0N/A throw new Exception("...error in getP()");
0N/A }
0N/A if (FP.getFieldSize() != TEN.bitLength()) {
0N/A throw new Exception("...error in getFieldSize()");
0N/A }
0N/A }
0N/A private static void testECFieldF2m() throws Exception {
0N/A // invalid parameters
0N/A int mBad = 0;
0N/A int[] ksBad;
0N/A for (int i=0; i<7; i++) {
0N/A System.out.print("Testing ECFieldF2m");
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A System.out.println("(int)");
0N/A new ECFieldF2m(mBad);
0N/A break;
0N/A case 1:
0N/A System.out.println("(int, BigInteger)#1");
0N/A new ECFieldF2m(mBad, F2M_RP);
0N/A break;
0N/A case 2:
0N/A System.out.println("(int, BigInteger)#2");
0N/A new ECFieldF2m(mBad, F2M_RP);
0N/A break;
0N/A case 3:
0N/A System.out.println("(int, int[])#1");
0N/A new ECFieldF2m(mBad, F2M_KS);
0N/A break;
0N/A case 4:
0N/A ksBad = new int[2];
0N/A System.out.println("(int, int[])#2");
0N/A new ECFieldF2m(F2M_M, ksBad);
0N/A break;
0N/A case 5:
0N/A ksBad = new int[1];
0N/A ksBad[0] = 5;
0N/A System.out.println("(int, int[])#3");
0N/A new ECFieldF2m(F2M_M, ksBad);
0N/A break;
0N/A case 6:
0N/A ksBad = new int[3];
0N/A ksBad[0] = 1;
0N/A ksBad[1] = 2;
0N/A ksBad[2] = 3;
0N/A System.out.println("(int, int[])#4");
0N/A new ECFieldF2m(F2M_M, ksBad);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw IAE");
0N/A } catch (IllegalArgumentException iae) {
0N/A System.out.println("...expected IAE thrown");
0N/A }
0N/A }
0N/A for (int i=0; i<2; i++) {
0N/A System.out.print("Testing ECFieldF2m");
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A System.out.println("(int, BigInteger)");
0N/A new ECFieldF2m(F2M_M, (BigInteger) null);
0N/A break;
0N/A case 1:
0N/A System.out.println("(int, int[])#1");
0N/A new ECFieldF2m(F2M_M, (int[]) null);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw NPE");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("...expected NPE thrown");
0N/A }
0N/A }
0N/A if (F2M_RP.compareTo(F2M.getReductionPolynomial()) != 0) {
0N/A throw new Exception("...error in getReductionPolynomial()");
0N/A }
0N/A ECFieldF2m field = new ECFieldF2m(F2M_M, F2M_RP);
0N/A if (!(Arrays.equals(F2M_KS,
0N/A field.getMidTermsOfReductionPolynomial()))) {
0N/A throw new Exception("...error in getMidTermsOfReductionPolynomial()");
0N/A }
0N/A if (field.getFieldSize() != F2M_M) {
0N/A throw new Exception("...error in getFieldSize()");
0N/A }
0N/A }
0N/A private static void testECParameterSpec() throws Exception {
0N/A System.out.println("Testing ECParameterSpec(...)");
0N/A for (int i = 0; i < 2; i++) {
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A System.out.println("with zero order");
0N/A new ECParameterSpec(CURVE, POINT, ZERO, COFACTOR);
0N/A break;
0N/A case 1:
0N/A System.out.println("with negative cofactor");
0N/A new ECParameterSpec(CURVE, POINT, ORDER, -1);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw IAE");
0N/A } catch (IllegalArgumentException iae) {
0N/A System.out.println("...expected IAE thrown");
0N/A }
0N/A }
0N/A for (int i = 0; i < 3; i++) {
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A System.out.println("with null curve");
0N/A new ECParameterSpec(null, POINT, ORDER, COFACTOR);
0N/A break;
0N/A case 1:
0N/A System.out.println("with null generator");
0N/A new ECParameterSpec(CURVE, null, ORDER, COFACTOR);
0N/A break;
0N/A case 2:
0N/A System.out.println("with null order");
0N/A new ECParameterSpec(CURVE, POINT, null, COFACTOR);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw NPE");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("...expected NPE thrown");
0N/A }
0N/A }
0N/A if (!(CURVE.equals(PARAMS.getCurve()))) {
0N/A throw new Exception("...error in getCurve()");
0N/A }
0N/A if (!(POINT.equals(PARAMS.getGenerator()))) {
0N/A throw new Exception("...error in getGenerator()");
0N/A }
0N/A if (!(ORDER.equals(PARAMS.getOrder()))) {
0N/A throw new Exception("...error in getOrder()");
0N/A }
0N/A if (COFACTOR != PARAMS.getCofactor()) {
0N/A throw new Exception("...error in getCofactor()");
0N/A }
0N/A }
0N/A private static void testECGenParameterSpec() throws Exception {
0N/A System.out.println("Testing ECGenParameterSpec(String)");
0N/A try {
0N/A new ECGenParameterSpec(null);
0N/A throw new Exception("...should throw NPE");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("...expected NPE thrown");
0N/A }
0N/A if (!("prime192v1".equals(GENPARAMS.getName()))) {
0N/A throw new Exception("...error in getName()");
0N/A }
0N/A }
0N/A private static void testECPoint() throws Exception {
0N/A System.out.println("Testing ECParameterSpec(...)");
0N/A for (int i = 0; i < 2; i++) {
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A System.out.println("with null x-coordinate");
0N/A new ECPoint(null, TEN);
0N/A break;
0N/A case 1:
0N/A System.out.println("with null y-coordinate");
0N/A new ECPoint(ONE, null);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw NPE");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("...expected NPE thrown");
0N/A }
0N/A }
0N/A if (!(ONE.equals(POINT.getAffineX()))) {
0N/A throw new Exception("...error in getAffineX()");
0N/A }
0N/A if (!(TEN.equals(POINT.getAffineY()))) {
0N/A throw new Exception("...error in getAffineY()");
0N/A }
0N/A }
0N/A private static void testECPublicKeySpec() throws Exception {
0N/A System.out.println("Testing ECPublicKeySpec(...)");
0N/A for (int i = 0; i < 2; i++) {
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A System.out.println("with null public value");
0N/A new ECPublicKeySpec(null, PARAMS);
0N/A break;
0N/A case 1:
0N/A System.out.println("with null params");
0N/A new ECPublicKeySpec(POINT, null);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw NPE");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("...expected NPE thrown");
0N/A }
0N/A }
0N/A if (!(POINT.equals(PUB_KEY.getW()))) {
0N/A throw new Exception("...error in getW()");
0N/A }
0N/A if (!(PARAMS.equals(PUB_KEY.getParams()))) {
0N/A throw new Exception("...error in getParams()");
0N/A }
0N/A }
0N/A private static void testECPrivateKeySpec() throws Exception {
0N/A System.out.println("Testing ECPrivateKeySpec(...)");
0N/A for (int i = 0; i < 2; i++) {
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A System.out.println("with null private value");
0N/A new ECPrivateKeySpec(null, PARAMS);
0N/A break;
0N/A case 1:
0N/A System.out.println("with null param");
0N/A new ECPrivateKeySpec(ONE, null);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw NPE");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("...expected NPE thrown");
0N/A }
0N/A }
0N/A if (!(TEN.equals(PRIV_KEY.getS()))) {
0N/A throw new Exception("...error in getS()");
0N/A }
0N/A if (!(PARAMS.equals(PRIV_KEY.getParams()))) {
0N/A throw new Exception("...error in getParams()");
0N/A }
0N/A }
0N/A private static void testEllipticCurve() throws Exception {
0N/A System.out.println("Testing EllipticCurve(...)");
0N/A for (int j = 0; j < 2; j++) {
0N/A BigInteger a = ONE;
0N/A BigInteger b = ONE;
0N/A if (j == 0) { // test a out of range
0N/A System.out.println("with a's value out of range");
0N/A a = BigInteger.valueOf(20);
0N/A } else { // test b out of range
0N/A System.out.println("with b's value out of range");
0N/A b = BigInteger.valueOf(20);
0N/A }
0N/A System.out.println(">> over ECFieldFp");
0N/A for (int i = 0; i < 3; i++) {
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A new EllipticCurve(FP, a, b);
0N/A break;
0N/A case 1:
0N/A new EllipticCurve(FP, a, b, null);
0N/A break;
0N/A case 2:
0N/A new EllipticCurve(FP, a, b, new byte[8]);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw IAE");
0N/A } catch (IllegalArgumentException iae) {
0N/A System.out.println("...expected IAE thrown for #" + (i+1));
0N/A }
0N/A }
0N/A System.out.println(">> over ECFieldF2m");
0N/A for (int i = 0; i < 3; i++) {
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A new EllipticCurve(F2M, a, b);
0N/A break;
0N/A case 1:
0N/A new EllipticCurve(F2M, a, b, null);
0N/A break;
0N/A case 2:
0N/A new EllipticCurve(F2M, a, b, new byte[8]);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw IAE");
0N/A } catch (IllegalArgumentException iae) {
0N/A System.out.println("...expected IAE thrown for #" + (i+1));
0N/A }
0N/A }
0N/A }
0N/A for (int i = 0; i < 6; i++) {
0N/A try {
0N/A switch (i) {
0N/A case 0:
0N/A new EllipticCurve(null, ONE, TEN);
0N/A break;
0N/A case 1:
0N/A new EllipticCurve(FP, null, TEN);
0N/A break;
0N/A case 2:
0N/A new EllipticCurve(FP, ONE, null);
0N/A break;
0N/A case 3:
0N/A new EllipticCurve(null, ONE, TEN, null);
0N/A break;
0N/A case 4:
0N/A new EllipticCurve(FP, null, TEN, null);
0N/A break;
0N/A case 5:
0N/A new EllipticCurve(FP, ONE, null, null);
0N/A break;
0N/A }
0N/A throw new Exception("...should throw NPE");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("...expected NPE thrown");
0N/A }
0N/A }
0N/A if (!(FP.equals(CURVE.getField()))) {
0N/A throw new Exception("...error in getField()");
0N/A }
0N/A if (!(ONE.equals(CURVE.getA()))) {
0N/A throw new Exception("...error in getA()");
0N/A }
0N/A if (!(ONE.equals(CURVE.getB()))) {
0N/A throw new Exception("...error in getB()");
0N/A }
0N/A if (!(CURVE.equals(new EllipticCurve(FP, ONE, ONE, null)))) {
0N/A throw new Exception("...error in equals()");
0N/A }
0N/A }
0N/A private static void testAllHashCode() throws Exception {
0N/A // make sure no unexpected exception when hashCode() is called.
0N/A FP.hashCode();
0N/A F2M.hashCode();
0N/A GENPARAMS.hashCode();
0N/A PARAMS.hashCode();
0N/A POINT.hashCode();
0N/A PRIV_KEY.hashCode();
0N/A PUB_KEY.hashCode();
0N/A CURVE.hashCode();
0N/A }
0N/A public static void main(String[] argv) throws Exception {
0N/A testECFieldFp();
0N/A testECFieldF2m();
0N/A testECParameterSpec();
0N/A testECGenParameterSpec();
0N/A testECPoint();
0N/A testECPrivateKeySpec();
0N/A testECPublicKeySpec();
0N/A testEllipticCurve();
0N/A testAllHashCode();
0N/A System.out.println("All Test Passed");
0N/A }
0N/A}