1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6738532
  27  * @summary Check EllipticCurve.equals() does not compare seed value of curve.
  28  * @author Mike StJohns
  29  * @key randomness
  30  */
  31 
  32 import java.security.spec.*;
  33 import java.math.BigInteger;
  34 import java.util.Random;
  35 
  36 public class EllipticCurveMatch {
  37     static String primeP256 =
  38         "0FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF";
  39     static String aP256 =
  40         "0FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC";
  41     static String bP256 =
  42         "05AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B";
  43     static String seedP256 =
  44         "0C49D360886E704936A6678E1139D26B7819F7E90";
  45 
  46     private static EllipticCurve addSeedToCurve(EllipticCurve curve)
  47     {
  48         Random rand = new Random();
  49         byte[] seed = new byte[12];
  50         rand.nextBytes(seed);
  51 
  52         return new EllipticCurve (curve.getField(), curve.getA(), curve.getB(),
  53             seed);
  54     }
  55 
  56     private static EllipticCurve getP256Curve()
  57     {
  58         ECFieldFp field = new ECFieldFp(new BigInteger (primeP256,16));
  59         BigInteger a = new BigInteger (aP256, 16);
  60         BigInteger b = new BigInteger (bP256, 16);
  61 
  62         return new EllipticCurve (field, a, b);
  63     }
  64 
  65     public static void main (String[] argv) throws Exception {
  66         EllipticCurve firstCurve = getP256Curve();
  67         EllipticCurve secondCurve = addSeedToCurve(firstCurve);
  68         EllipticCurve thirdCurve = addSeedToCurve(firstCurve);
  69 
  70         if (!firstCurve.equals(firstCurve))
  71             throw new Exception("Original curve doesn't equal itself");
  72 
  73         if (!firstCurve.equals(secondCurve))
  74             throw new Exception ("Original curve doesn't equal seeded curve");
  75 
  76         if (!secondCurve.equals(secondCurve))
  77             throw new Exception ("Seeded curve doesn't equal itself");
  78 
  79         if (!secondCurve.equals(thirdCurve))
  80             throw new Exception ("Seeded curve doesn't equal differently " +
  81                 "seeded curve");
  82         System.out.println("Curve equals test passed");
  83     }
  84 }