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