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