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  */
  30 
  31 import java.security.spec.*;
  32 import java.math.BigInteger;
  33 import java.util.Random;
  34 
  35 public class EllipticCurveMatch {
  36     static String primeP256 =
  37         "0FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF";
  38     static String aP256 =
  39         "0FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC";
  40     static String bP256 =
  41         "05AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B";
  42     static String seedP256 =
  43         "0C49D360886E704936A6678E1139D26B7819F7E90";
  44 
  45     private static EllipticCurve addSeedToCurve(EllipticCurve curve)
  46     {
  47         Random rand = new Random();
  48         byte[] seed = new byte[12];
  49         rand.nextBytes(seed);
  50 
  51         return new EllipticCurve (curve.getField(), curve.getA(), curve.getB(),
  52             seed);
  53     }
  54 
  55     private static EllipticCurve getP256Curve()
  56     {
  57         ECFieldFp field = new ECFieldFp(new BigInteger (primeP256,16));
  58         BigInteger a = new BigInteger (aP256, 16);
  59         BigInteger b = new BigInteger (bP256, 16);
  60 
  61         return new EllipticCurve (field, a, b);
  62     }
  63 
  64     public static void main (String[] argv) throws Exception {
  65         EllipticCurve firstCurve = getP256Curve();
  66         EllipticCurve secondCurve = addSeedToCurve(firstCurve);
  67         EllipticCurve thirdCurve = addSeedToCurve(firstCurve);
  68 
  69         if (!firstCurve.equals(firstCurve))
  70             throw new Exception("Original curve doesn't equal itself");
  71 
  72         if (!firstCurve.equals(secondCurve))
  73             throw new Exception ("Original curve doesn't equal seeded curve");
  74 
  75         if (!secondCurve.equals(secondCurve))
  76             throw new Exception ("Seeded curve doesn't equal itself");
  77 
  78         if (!secondCurve.equals(thirdCurve))
  79             throw new Exception ("Seeded curve doesn't equal differently " +
  80                 "seeded curve");
  81         System.out.println("Curve equals test passed");
  82     }
  83 }