jdk/test/com/sun/crypto/provider/KeyAgreement/DHKeyAgreement2.java
author chegar
Mon, 14 Dec 2015 19:24:33 +0000
changeset 34687 d302ed125dc9
parent 31422 67cd03bc9cce
permissions -rw-r--r--
8144995: Move sun.misc.HexDumpEncoder to sun.security.util Reviewed-by: psandoz, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 12201
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    26
 * @bug 7146728
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary DHKeyAgreement2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This test utility executes the Diffie-Hellman key agreement protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * between 2 parties: Alice and Bob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * By default, preconfigured parameters (1024 bit prime modulus and base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * generator used by SKIP) are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * If this program is called with the "-gen" option, a new set of parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * are created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class DHKeyAgreement2 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    52
    private static final String SUNJCE = "SunJCE";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private DHKeyAgreement2() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static void main(String argv[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            String mode = "USE_SKIP_DH_PARAMS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            DHKeyAgreement2 keyAgree = new DHKeyAgreement2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (argv.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                keyAgree.usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                throw new Exception("Wrong number of command options");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            } else if (argv.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                if (!(argv[0].equals("-gen"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    keyAgree.usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    throw new Exception("Unrecognized flag: " + argv[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                mode = "GENERATE_DH_PARAMS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            keyAgree.run(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            System.out.println("Test Passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private void run(String mode) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        DHParameterSpec dhSkipParamSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (mode.equals("GENERATE_DH_PARAMS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            // Some central authority creates new DH parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.err.println("Creating Diffie-Hellman parameters ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            AlgorithmParameterGenerator paramGen
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    83
                = AlgorithmParameterGenerator.getInstance("DH", SUNJCE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            paramGen.init(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            AlgorithmParameters params = paramGen.generateParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            dhSkipParamSpec = (DHParameterSpec)params.getParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                (DHParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // use some pre-generated, default DH parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.err.println("Using SKIP Diffie-Hellman parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            dhSkipParamSpec = new DHParameterSpec(skip1024Modulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                                  skip1024Base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * Alice creates her own DH key pair, using the DH parameters from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.err.println("ALICE: Generate DH keypair ...");
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   100
        KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH", SUNJCE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        aliceKpairGen.initialize(dhSkipParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.out.println("Alice DH public key:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                           aliceKpair.getPublic().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.out.println("Alice DH private key:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                           aliceKpair.getPrivate().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        DHParameterSpec dhParamSpec =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            ((DHPublicKey)aliceKpair.getPublic()).getParams();
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   109
        AlgorithmParameters algParams = AlgorithmParameters.getInstance("DH", SUNJCE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        algParams.init(dhParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.out.println("Alice DH parameters:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                           + algParams.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Alice executes Phase1 of her version of the DH protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.err.println("ALICE: Execute PHASE1 ...");
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   116
        KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH", SUNJCE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        aliceKeyAgree.init(aliceKpair.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // Alice encodes her public key, and sends it over to Bob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        byte[] alicePubKeyEnc = aliceKpair.getPublic().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * Let's turn over to Bob. Bob has received Alice's public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * in encoded format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         * He instantiates a DH public key from the encoded key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         */
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   127
        KeyFactory bobKeyFac = KeyFactory.getInstance("DH", SUNJCE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            (alicePubKeyEnc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        PublicKey alicePubKey = bobKeyFac.generatePublic(x509KeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * Bob gets the DH parameters associated with Alice's public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * He must use the same parameters when he generates his own key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        dhParamSpec = ((DHPublicKey)alicePubKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // Bob creates his own DH key pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        System.err.println("BOB: Generate DH keypair ...");
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   141
        KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH", SUNJCE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        bobKpairGen.initialize(dhParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        KeyPair bobKpair = bobKpairGen.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.out.println("Bob DH public key:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                           bobKpair.getPublic().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        System.out.println("Bob DH private key:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                           bobKpair.getPrivate().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // Bob executes Phase1 of his version of the DH protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        System.err.println("BOB: Execute PHASE1 ...");
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   151
        KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH", SUNJCE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        bobKeyAgree.init(bobKpair.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // Bob encodes his public key, and sends it over to Alice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * Alice uses Bob's public key for Phase2 of her version of the DH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * Before she can do so, she has to instanticate a DH public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * from Bob's encoded key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         */
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   163
        KeyFactory aliceKeyFac = KeyFactory.getInstance("DH", SUNJCE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        x509KeySpec = new X509EncodedKeySpec(bobPubKeyEnc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        PublicKey bobPubKey = aliceKeyFac.generatePublic(x509KeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        System.err.println("ALICE: Execute PHASE2 ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        aliceKeyAgree.doPhase(bobPubKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * Bob uses Alice's public key for Phase2 of his version of the DH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.err.println("BOB: Execute PHASE2 ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        bobKeyAgree.doPhase(alicePubKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * At this stage, both Alice and Bob have completed the DH key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * agreement protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * Each generates the (same) shared secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int aliceLen = aliceSharedSecret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   184
        // check if alice's key agreement has been reset afterwards
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   185
        try {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   186
            aliceKeyAgree.generateSecret();
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   187
            throw new Exception("Error: alice's KeyAgreement not reset");
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   188
        } catch (IllegalStateException e) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   189
            System.out.println("EXPECTED:  " + e.getMessage());
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   190
        }
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   191
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        byte[] bobSharedSecret = new byte[aliceLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int bobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // provide output buffer that is too short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            bobLen = bobKeyAgree.generateSecret(bobSharedSecret, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (ShortBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            System.out.println("EXPECTED:  " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   200
        // retry w/ output buffer of required size
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   201
        bobLen = bobKeyAgree.generateSecret(bobSharedSecret, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   203
        // check if bob's key agreement has been reset afterwards
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   204
        try {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   205
            bobKeyAgree.generateSecret(bobSharedSecret, 0);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   206
            throw new Exception("Error: bob's KeyAgreement not reset");
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   207
        } catch (IllegalStateException e) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   208
            System.out.println("EXPECTED:  " + e.getMessage());
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   209
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        System.out.println("Alice secret: " + toHexString(aliceSharedSecret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        System.out.println("Bob secret: " + toHexString(bobSharedSecret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (aliceLen != bobLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new Exception("Shared secrets have different lengths");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        for (int i=0; i<aliceLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (aliceSharedSecret[i] != bobSharedSecret[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                throw new Exception("Shared secrets differ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        System.err.println("Shared secrets are the same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // Now let's return the shared secret as a SecretKey object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // and use it for encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        System.out.println("Return shared secret as SecretKey object ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        bobKeyAgree.doPhase(alicePubKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        SecretKey desKey = bobKeyAgree.generateSecret("DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        desCipher.init(Cipher.ENCRYPT_MODE, desKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        byte[] cleartext = "This is just an example".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        byte[] ciphertext = desCipher.doFinal(cleartext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        desCipher.init(Cipher.DECRYPT_MODE, desKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        byte[] cleartext1 = desCipher.doFinal(ciphertext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int clearLen = cleartext.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int clear1Len = cleartext1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (clearLen != clear1Len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new Exception("DIFFERENT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        for (int i=0; i < clear1Len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (cleartext[i] != cleartext1[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                throw new Exception("DIFFERENT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        System.err.println("SAME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Converts a byte to hex digit and writes to the supplied buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private void byte2hex(byte b, StringBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            '9', 'A', 'B', 'C', 'D', 'E', 'F' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int high = ((b & 0xf0) >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int low = (b & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        buf.append(hexChars[high]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        buf.append(hexChars[low]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Converts a byte array to hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private String toHexString(byte[] block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int len = block.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
             byte2hex(block[i], buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
             if (i < len-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                 buf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Prints the usage of this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        System.err.print("DHKeyAgreement usage: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        System.err.println("[-gen]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    // The 1024 bit Diffie-Hellman modulus values used by SKIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private static final byte skip1024ModulusBytes[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        (byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        (byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        (byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        (byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        (byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        (byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        (byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        (byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        (byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        (byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        (byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        (byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        (byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        (byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        (byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        (byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        (byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        (byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        (byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        (byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        (byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        (byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        (byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        (byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        (byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        (byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        (byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        (byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        (byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        (byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        (byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        (byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    // The SKIP 1024 bit modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private static final BigInteger skip1024Modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    = new BigInteger(1, skip1024ModulusBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    // The base used with the SKIP 1024 bit modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private static final BigInteger skip1024Base = BigInteger.valueOf(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}