Path: blob/master/test/jdk/java/security/spec/EllipticCurveMatch.java
41149 views
/*1* Copyright (c) 2011, 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 673853226* @summary Check EllipticCurve.equals() does not compare seed value of curve.27* @author Mike StJohns28* @key randomness29*/3031import java.security.spec.*;32import java.math.BigInteger;33import java.util.Random;3435public class EllipticCurveMatch {36static String primeP256 =37"0FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF";38static String aP256 =39"0FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC";40static String bP256 =41"05AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B";42static String seedP256 =43"0C49D360886E704936A6678E1139D26B7819F7E90";4445private static EllipticCurve addSeedToCurve(EllipticCurve curve)46{47Random rand = new Random();48byte[] seed = new byte[12];49rand.nextBytes(seed);5051return new EllipticCurve (curve.getField(), curve.getA(), curve.getB(),52seed);53}5455private static EllipticCurve getP256Curve()56{57ECFieldFp field = new ECFieldFp(new BigInteger (primeP256,16));58BigInteger a = new BigInteger (aP256, 16);59BigInteger b = new BigInteger (bP256, 16);6061return new EllipticCurve (field, a, b);62}6364public static void main (String[] argv) throws Exception {65EllipticCurve firstCurve = getP256Curve();66EllipticCurve secondCurve = addSeedToCurve(firstCurve);67EllipticCurve thirdCurve = addSeedToCurve(firstCurve);6869if (!firstCurve.equals(firstCurve))70throw new Exception("Original curve doesn't equal itself");7172if (!firstCurve.equals(secondCurve))73throw new Exception ("Original curve doesn't equal seeded curve");7475if (!secondCurve.equals(secondCurve))76throw new Exception ("Seeded curve doesn't equal itself");7778if (!secondCurve.equals(thirdCurve))79throw new Exception ("Seeded curve doesn't equal differently " +80"seeded curve");81System.out.println("Curve equals test passed");82}83}848586