jdk/src/share/classes/com/sun/crypto/provider/DHKeyAgreement.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 12201 d77ed23f4992
child 16080 0e6266b88242
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2012, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.SecureRandom;
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    36
import java.security.ProviderException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.KeyAgreementSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.ShortBufferException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class implements the Diffie-Hellman key agreement protocol between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * any number of parties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public final class DHKeyAgreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
extends KeyAgreementSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean generateSecret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private BigInteger init_p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private BigInteger init_g = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private BigInteger x = BigInteger.ZERO; // the private value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private BigInteger y = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
3353
ddbd63234844 6647452: Remove obfuscation, framework and provider self-verification checking
wetmore
parents: 2
diff changeset
    62
     * Empty constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public DHKeyAgreement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Initializes this key agreement with the given key and source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * randomness. The given key is required to contain all the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * parameters required for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p> If the key agreement algorithm requires random bytes, it gets them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * from the given source of randomness, <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * However, if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * algorithm implementation does not require any random bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <code>random</code> is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected void engineInit(Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            engineInit(key, null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // never happens, because we did not pass any parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Initializes this key agreement with the given key, set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * algorithm parameters, and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param params the key agreement parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * are inappropriate for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    protected void engineInit(Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // ignore "random" parameter, because our implementation does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // require any source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        generateSecret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        init_p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        init_g = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if ((params != null) && !(params instanceof DHParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                ("Diffie-Hellman parameters expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (!(key instanceof javax.crypto.interfaces.DHPrivateKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new InvalidKeyException("Diffie-Hellman private key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                          + "expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        javax.crypto.interfaces.DHPrivateKey dhPrivKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        dhPrivKey = (javax.crypto.interfaces.DHPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // check if private key parameters are compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // initialized ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            init_p = ((DHParameterSpec)params).getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            init_g = ((DHParameterSpec)params).getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        BigInteger priv_p = dhPrivKey.getParams().getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        BigInteger priv_g = dhPrivKey.getParams().getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (init_p != null && priv_p != null && !(init_p.equals(priv_p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new InvalidKeyException("Incompatible parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (init_g != null && priv_g != null && !(init_g.equals(priv_g))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new InvalidKeyException("Incompatible parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if ((init_p == null && priv_p == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            || (init_g == null && priv_g == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new InvalidKeyException("Missing parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        init_p = priv_p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        init_g = priv_g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // store the x value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.x = dhPrivKey.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Executes the next phase of this key agreement with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * key that was received from one of the other parties involved in this key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param key the key for this phase. For example, in the case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Diffie-Hellman between 2 parties, this would be the other party's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Diffie-Hellman public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param lastPhase flag which indicates whether or not this is the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * phase of this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return the (intermediate) key resulting from this phase, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * this phase does not yield a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * this phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected Key engineDoPhase(Key key, boolean lastPhase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        throws InvalidKeyException, IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new InvalidKeyException("Diffie-Hellman public key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                          + "expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        javax.crypto.interfaces.DHPublicKey dhPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (init_p == null || init_g == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new IllegalStateException("Not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // check if public key parameters are compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // initialized ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        BigInteger pub_p = dhPubKey.getParams().getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        BigInteger pub_g = dhPubKey.getParams().getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (pub_p != null && !(init_p.equals(pub_p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new InvalidKeyException("Incompatible parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (pub_g != null && !(init_g.equals(pub_g))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new InvalidKeyException("Incompatible parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // store the y value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        this.y = dhPubKey.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // we've received a public key (from one of the other parties),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // so we are ready to create the secret, which may be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // intermediate secret, in which case we wrap it into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // Diffie-Hellman public key object and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        generateSecret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (lastPhase == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            byte[] intermediate = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return new DHPublicKey(new BigInteger(1, intermediate),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                   init_p, init_g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Generates the shared secret and returns it in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return the new buffer with the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    protected byte[] engineGenerateSecret()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    {
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   238
        int expectedLen = (init_p.bitLength() + 7) >>> 3;
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   239
        byte[] result = new byte[expectedLen];
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   240
        try {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   241
            engineGenerateSecret(result, 0);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   242
        } catch (ShortBufferException sbe) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   243
            // should never happen since length are identical
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   245
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Generates the shared secret, and places it into the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <code>sharedSecret</code>, beginning at <code>offset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>If the <code>sharedSecret</code> buffer is too small to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * result, a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * In this case, this call should be repeated with a larger output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param sharedSecret the buffer for the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param offset the offset in <code>sharedSecret</code> where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * shared secret will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return the number of bytes placed into <code>sharedSecret</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * to hold the secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    protected int engineGenerateSecret(byte[] sharedSecret, int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        throws IllegalStateException, ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (generateSecret == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                ("Key agreement has not been completed yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (sharedSecret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                ("No buffer provided for shared secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        BigInteger modulus = init_p;
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   288
        int expectedLen = (modulus.bitLength() + 7) >>> 3;
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   289
        if ((sharedSecret.length - offset) < expectedLen) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   290
            throw new ShortBufferException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    ("Buffer too short for shared secret");
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   292
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   294
        // Reset the key agreement after checking for ShortBufferException
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   295
        // above, so user can recover w/o losing internal state
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   296
        generateSecret = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   298
        /*
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   299
         * NOTE: BigInteger.toByteArray() returns a byte array containing
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   300
         * the two's-complement representation of this BigInteger with
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   301
         * the most significant byte is in the zeroth element. This
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   302
         * contains the minimum number of bytes required to represent
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   303
         * this BigInteger, including at least one sign bit whose value
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   304
         * is always 0.
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   305
         *
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   306
         * Keys are always positive, and the above sign bit isn't
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   307
         * actually used when representing keys.  (i.e. key = new
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   308
         * BigInteger(1, byteArray))  To obtain an array containing
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   309
         * exactly expectedLen bytes of magnitude, we strip any extra
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   310
         * leading 0's, or pad with 0's in case of a "short" secret.
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   311
         */
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   312
        byte[] secret = this.y.modPow(this.x, modulus).toByteArray();
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   313
        if (secret.length == expectedLen) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   314
            System.arraycopy(secret, 0, sharedSecret, offset,
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   315
                             secret.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        } else {
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   317
            // Array too short, pad it w/ leading 0s
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   318
            if (secret.length < expectedLen) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   319
                System.arraycopy(secret, 0, sharedSecret,
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   320
                    offset + (expectedLen - secret.length),
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   321
                    secret.length);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   322
            } else {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   323
                // Array too long, check and trim off the excess
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   324
                if ((secret.length == (expectedLen+1)) && secret[0] == 0) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   325
                    // ignore the leading sign byte
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   326
                    System.arraycopy(secret, 1, sharedSecret, offset, expectedLen);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   327
                } else {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   328
                    throw new ProviderException("Generated secret is out-of-range");
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   329
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   332
        return expectedLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Creates the shared secret and returns it as a secret key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * of the requested algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param algorithm the requested secret key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the shared secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @exception NoSuchAlgorithmException if the requested secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @exception InvalidKeyException if the shared secret key material cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * be used to generate a secret key of the requested algorithm type (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * the key material is too short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    protected SecretKey engineGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (algorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            throw new NoSuchAlgorithmException("null algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        byte[] secret = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (algorithm.equalsIgnoreCase("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            // DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return new DESKey(secret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        } else if (algorithm.equalsIgnoreCase("DESede")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                   || algorithm.equalsIgnoreCase("TripleDES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // Triple DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return new DESedeKey(secret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else if (algorithm.equalsIgnoreCase("Blowfish")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // Blowfish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            int keysize = secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (keysize >= BlowfishConstants.BLOWFISH_MAX_KEYSIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                keysize = BlowfishConstants.BLOWFISH_MAX_KEYSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            SecretKeySpec skey = new SecretKeySpec(secret, 0, keysize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                                   "Blowfish");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return skey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        } else if (algorithm.equalsIgnoreCase("AES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            // AES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            int keysize = secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            SecretKeySpec skey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            int idx = AESConstants.AES_KEYSIZES.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            while (skey == null && idx >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                // Generate the strongest key using the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                // assuming the key sizes in AESConstants class are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                // in ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                if (keysize >= AESConstants.AES_KEYSIZES[idx]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    keysize = AESConstants.AES_KEYSIZES[idx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    skey = new SecretKeySpec(secret, 0, keysize, "AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                idx--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (skey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                throw new InvalidKeyException("Key material is too short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return skey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else if (algorithm.equals("TlsPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // return entire secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return new SecretKeySpec(secret, "TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new NoSuchAlgorithmException("Unsupported secret key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                               + "algorithm: "+ algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
}