jdk/test/sun/security/pkcs11/ec/TestECDH2.java
author valeriep
Tue, 04 Sep 2012 18:41:06 -0700
changeset 13672 604588823b5a
child 17491 7a33824ec8c5
permissions -rw-r--r--
7044060: Need to support NSA Suite B Cryptography algorithms Summary: Add support for DSA parameter generation and OIDs for NSA Suite B algorithms. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     1
/*
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     4
 *
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     8
 *
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    13
 * accompanied this code).
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    14
 *
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    18
 *
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    21
 * questions.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    22
 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    23
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    24
/**
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    25
 * @test
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    26
 * @bug 6405536
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    27
 * @summary basic test of ECDSA signatures for P-256 and P-384 from the
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    28
 * example data in "Suite B Implementer's Guide to FIPS 186-3".
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    29
 * @library ..
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    30
 * @library ../../../../java/security/testlibrary
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    31
 * @compile -XDignore.symbol.file TestECDH2.java
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    32
 * @run main TestECDH2
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    33
 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    34
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    35
import java.io.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    36
import java.util.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    37
import java.math.BigInteger;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    38
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    39
import java.security.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    40
import java.security.spec.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    41
import java.security.interfaces.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    42
import javax.crypto.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    43
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    44
import sun.security.ec.NamedCurve;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    45
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    46
public class TestECDH2 extends PKCS11Test {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    47
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    48
    // values of the keys we use for the tests
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    49
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    50
    // keypair using NIST P-256
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    51
    private final static String privD256 = "70a12c2db16845ed56ff68cfc21a472b3f04d7d6851bf6349f2d7d5b3452b38a";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    52
    private final static String pubX256 = "8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    53
    private final static String pubY256 = "d8a12ba61d599235f67d9cb4d58f1783d3ca43e78f0a5abaa624079936c0c3a9";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    54
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    55
    // keypair using NIST P-384
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    56
    private final static String privD384 = "c838b85253ef8dc7394fa5808a5183981c7deef5a69ba8f4f2117ffea39cfcd90e95f6cbc854abacab701d50c1f3cf24";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    57
    private final static String pubX384 = "1fbac8eebd0cbf35640b39efe0808dd774debff20a2a329e91713baf7d7f3c3e81546d883730bee7e48678f857b02ca0";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    58
    private final static String pubY384 = "eb213103bd68ce343365a8a4c3d4555fa385f5330203bdd76ffad1f3affb95751c132007e1b240353cb0a4cf1693bdf9";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    59
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    60
    private KeyFactory kf = null;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    61
    private KeyPairGenerator kpg = null;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    62
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    63
    private static void testKeyAgreement(KeyPair kpA, KeyPair kpB, Provider p)
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    64
        throws Exception {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    65
        KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    66
        ka1.init(kpA.getPrivate());
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    67
        ka1.doPhase(kpB.getPublic(), true);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    68
        byte[] s1 = ka1.generateSecret();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    69
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    70
        KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    71
        ka2.init(kpB.getPrivate());
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    72
        ka2.doPhase(kpA.getPublic(), true);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    73
        byte[] s2 = ka2.generateSecret();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    74
        if (Arrays.equals(s1, s2) == false) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    75
            System.out.println("expected: " + toString(s1));
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    76
            System.out.println("actual:   " + toString(s2));
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    77
            throw new Exception("Generated secrets do not match");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    78
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    79
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    80
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    81
    private KeyPair genECKeyPair(String curvName, String privD, String pubX,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    82
                                 String pubY) throws Exception {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    83
        ECParameterSpec ecParams = NamedCurve.getECParameterSpec(curvName);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    84
        ECPrivateKeySpec privKeySpec =
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    85
            new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    86
        ECPublicKeySpec pubKeySpec =
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    87
            new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    88
                                            new BigInteger(pubY, 16)),
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    89
                                ecParams);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    90
        PrivateKey privKey = kf.generatePrivate(privKeySpec);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    91
        PublicKey pubKey = kf.generatePublic(pubKeySpec);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    92
        return new KeyPair(pubKey, privKey);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    93
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    94
    private KeyPair genECKeyPair(String curvName) throws Exception {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    95
        ECGenParameterSpec genParams = new ECGenParameterSpec(curvName);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    96
        kpg.initialize(genParams, null);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    97
        return kpg.generateKeyPair();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    98
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    99
    public static void main(String[] args) throws Exception {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   100
        main(new TestECDH2());
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   101
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   102
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   103
    public void main(Provider provider) throws Exception {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   104
        if (provider.getService("KeyAgreement", "ECDH") == null) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   105
            System.out.println("ECDH not supported, skipping");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   106
            return;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   107
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   108
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   109
        kf = KeyFactory.getInstance("EC", provider);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   110
        kpg = KeyPairGenerator.getInstance("EC", provider);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   111
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   112
        System.out.println("Testing against NIST P-256");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   113
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   114
        long start = System.currentTimeMillis();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   115
        KeyPair kp256A = genECKeyPair("secp256r1", privD256, pubX256, pubY256);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   116
        KeyPair kp256B = genECKeyPair("secp256r1");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   117
        testKeyAgreement(kp256A, kp256B, provider);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   118
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   119
        System.out.println("Testing against NIST P-384");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   120
        KeyPair kp384A = genECKeyPair("secp384r1", privD384, pubX384, pubY384);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   121
        KeyPair kp384B = genECKeyPair("secp384r1");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   122
        testKeyAgreement(kp384A, kp384B, provider);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   123
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   124
        long stop = System.currentTimeMillis();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   125
        System.out.println("All tests passed (" + (stop - start) + " ms).");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   126
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
   127
}