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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/*
* 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.*;
public class ECCBasic {
private static final BigInteger ZERO = BigInteger.ZERO;
private static final BigInteger ONE = BigInteger.ONE;
private static final BigInteger TEN = BigInteger.TEN;
private static final ECFieldFp FP = new ECFieldFp(TEN);
private static final int F2M_M = 4;
private static final int[] F2M_KS;
static {
F2M_KS = new int[1];
F2M_KS[0] = 1;
}
private static final BigInteger F2M_RP = BigInteger.valueOf(19);
private static final ECFieldF2m F2M = new ECFieldF2m(F2M_M, F2M_KS);
private static final EllipticCurve CURVE = new EllipticCurve
(new ECFieldFp(TEN), ONE, ONE);
private static final ECPoint POINT = new ECPoint(ONE, TEN);
private static final BigInteger ORDER = ONE;
private static final int COFACTOR = 3;
private static final ECParameterSpec PARAMS = new ECParameterSpec
(CURVE, POINT, ORDER, COFACTOR);
private static final ECGenParameterSpec GENPARAMS =
new ECGenParameterSpec("prime192v1");
private static final ECPrivateKeySpec PRIV_KEY = new ECPrivateKeySpec
(TEN, PARAMS);
private static final ECPublicKeySpec PUB_KEY = new ECPublicKeySpec
(POINT, PARAMS);
private static void testECFieldFp() throws Exception {
System.out.println("Testing ECFieldFp(BigInteger)");
try {
new ECFieldFp(ZERO);
throw new Exception("...should throw IAE");
} catch (IllegalArgumentException iae) {
System.out.println("...expected IAE thrown");
}
try {
new ECFieldFp(null);
throw new Exception("...should throw NPE");
} catch (NullPointerException npe) {
System.out.println("...expected NPE thrown");
}
if (TEN.equals(FP.getP()) == false) {
throw new Exception("...error in getP()");
}
if (FP.getFieldSize() != TEN.bitLength()) {
throw new Exception("...error in getFieldSize()");
}
}
private static void 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 = new int[2];
System.out.println("(int, int[])#2");
new ECFieldF2m(F2M_M, ksBad);
break;
case 5:
ksBad = new int[1];
ksBad[0] = 5;
System.out.println("(int, int[])#3");
new ECFieldF2m(F2M_M, ksBad);
break;
case 6:
ksBad = new int[3];
ksBad[0] = 1;
ksBad[1] = 2;
ksBad[2] = 3;
System.out.println("(int, int[])#4");
new ECFieldF2m(F2M_M, ksBad);
break;
}
throw new 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;
}
throw new Exception("...should throw NPE");
} catch (NullPointerException npe) {
System.out.println("...expected NPE thrown");
}
}
if (F2M_RP.compareTo(F2M.getReductionPolynomial()) != 0) {
throw new Exception("...error in getReductionPolynomial()");
}
ECFieldF2m field = new ECFieldF2m(F2M_M, F2M_RP);
if (!(Arrays.equals(F2M_KS,
field.getMidTermsOfReductionPolynomial()))) {
throw new Exception("...error in getMidTermsOfReductionPolynomial()");
}
if (field.getFieldSize() != F2M_M) {
throw new Exception("...error in getFieldSize()");
}
}
private static void 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;
}
throw new 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;
}
throw new Exception("...should throw NPE");
} catch (NullPointerException npe) {
System.out.println("...expected NPE thrown");
}
}
if (!(CURVE.equals(PARAMS.getCurve()))) {
throw new Exception("...error in getCurve()");
}
if (!(POINT.equals(PARAMS.getGenerator()))) {
throw new Exception("...error in getGenerator()");
}
if (!(ORDER.equals(PARAMS.getOrder()))) {
throw new Exception("...error in getOrder()");
}
if (COFACTOR != PARAMS.getCofactor()) {
throw new Exception("...error in getCofactor()");
}
}
private static void testECGenParameterSpec() throws Exception {
System.out.println("Testing ECGenParameterSpec(String)");
try {
new ECGenParameterSpec(null);
throw new Exception("...should throw NPE");
} catch (NullPointerException npe) {
System.out.println("...expected NPE thrown");
}
if (!("prime192v1".equals(GENPARAMS.getName()))) {
throw new Exception("...error in getName()");
}
}
private static void 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;
}
throw new Exception("...should throw NPE");
} catch (NullPointerException npe) {
System.out.println("...expected NPE thrown");
}
}
if (!(ONE.equals(POINT.getAffineX()))) {
throw new Exception("...error in getAffineX()");
}
if (!(TEN.equals(POINT.getAffineY()))) {
throw new Exception("...error in getAffineY()");
}
}
private static void 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;
}
throw new Exception("...should throw NPE");
} catch (NullPointerException npe) {
System.out.println("...expected NPE thrown");
}
}
if (!(POINT.equals(PUB_KEY.getW()))) {
throw new Exception("...error in getW()");
}
if (!(PARAMS.equals(PUB_KEY.getParams()))) {
throw new Exception("...error in getParams()");
}
}
private static void 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;
}
throw new Exception("...should throw NPE");
} catch (NullPointerException npe) {
System.out.println("...expected NPE thrown");
}
}
if (!(TEN.equals(PRIV_KEY.getS()))) {
throw new Exception("...error in getS()");
}
if (!(PARAMS.equals(PRIV_KEY.getParams()))) {
throw new Exception("...error in getParams()");
}
}
private static void 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, new byte[8]);
break;
}
throw new 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, new byte[8]);
break;
}
throw new 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;
}
throw new Exception("...should throw NPE");
} catch (NullPointerException npe) {
System.out.println("...expected NPE thrown");
}
}
if (!(FP.equals(CURVE.getField()))) {
throw new Exception("...error in getField()");
}
if (!(ONE.equals(CURVE.getA()))) {
throw new Exception("...error in getA()");
}
if (!(ONE.equals(CURVE.getB()))) {
throw new Exception("...error in getB()");
}
if (!(CURVE.equals(new EllipticCurve(FP, ONE, ONE, null)))) {
throw new Exception("...error in equals()");
}
}
private static void 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();
}
public static void main(String[] argv) throws Exception {
testECFieldFp();
testECFieldF2m();
testECParameterSpec();
testECGenParameterSpec();
testECPoint();
testECPrivateKeySpec();
testECPublicKeySpec();
testEllipticCurve();
testAllHashCode();
System.out.println("All Test Passed");
}
}
|