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