jdk/src/share/classes/com/sun/crypto/provider/DHKeyAgreement.java
author wetmore
Mon, 03 Aug 2009 18:06:51 -0700
changeset 3353 ddbd63234844
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6647452: Remove obfuscation, framework and provider self-verification checking Reviewed-by: valeriep, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3353
ddbd63234844 6647452: Remove obfuscation, framework and provider self-verification checking
wetmore
parents: 2
diff changeset
     2
 * Copyright 1997-2009 Sun Microsystems, Inc.  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.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.KeyAgreementSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.ShortBufferException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class implements the Diffie-Hellman key agreement protocol between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * any number of parties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public final class DHKeyAgreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
extends KeyAgreementSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean generateSecret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private BigInteger init_p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private BigInteger init_g = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private BigInteger x = BigInteger.ZERO; // the private value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private BigInteger y = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
3353
ddbd63234844 6647452: Remove obfuscation, framework and provider self-verification checking
wetmore
parents: 2
diff changeset
    61
     * Empty constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public DHKeyAgreement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Initializes this key agreement with the given key and source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * randomness. The given key is required to contain all the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * parameters required for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p> If the key agreement algorithm requires random bytes, it gets them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * from the given source of randomness, <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * However, if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * algorithm implementation does not require any random bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <code>random</code> is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected void engineInit(Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            engineInit(key, null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // never happens, because we did not pass any parameters
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Initializes this key agreement with the given key, set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * algorithm parameters, and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param params the key agreement parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * are inappropriate for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected void engineInit(Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // ignore "random" parameter, because our implementation does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // require any source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        generateSecret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        init_p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        init_g = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if ((params != null) && !(params instanceof DHParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                ("Diffie-Hellman parameters expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (!(key instanceof javax.crypto.interfaces.DHPrivateKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new InvalidKeyException("Diffie-Hellman private key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                          + "expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        javax.crypto.interfaces.DHPrivateKey dhPrivKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        dhPrivKey = (javax.crypto.interfaces.DHPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // check if private key parameters are compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // initialized ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            init_p = ((DHParameterSpec)params).getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            init_g = ((DHParameterSpec)params).getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        BigInteger priv_p = dhPrivKey.getParams().getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        BigInteger priv_g = dhPrivKey.getParams().getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (init_p != null && priv_p != null && !(init_p.equals(priv_p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new InvalidKeyException("Incompatible parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (init_g != null && priv_g != null && !(init_g.equals(priv_g))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new InvalidKeyException("Incompatible parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if ((init_p == null && priv_p == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            || (init_g == null && priv_g == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new InvalidKeyException("Missing parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        init_p = priv_p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        init_g = priv_g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // store the x value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.x = dhPrivKey.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Executes the next phase of this key agreement with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * key that was received from one of the other parties involved in this key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param key the key for this phase. For example, in the case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Diffie-Hellman between 2 parties, this would be the other party's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Diffie-Hellman public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param lastPhase flag which indicates whether or not this is the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * phase of this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return the (intermediate) key resulting from this phase, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * this phase does not yield a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * this phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected Key engineDoPhase(Key key, boolean lastPhase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        throws InvalidKeyException, IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new InvalidKeyException("Diffie-Hellman public key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                          + "expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        javax.crypto.interfaces.DHPublicKey dhPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (init_p == null || init_g == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new IllegalStateException("Not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // check if public key parameters are compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // initialized ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        BigInteger pub_p = dhPubKey.getParams().getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        BigInteger pub_g = dhPubKey.getParams().getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (pub_p != null && !(init_p.equals(pub_p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new InvalidKeyException("Incompatible parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (pub_g != null && !(init_g.equals(pub_g))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new InvalidKeyException("Incompatible parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // store the y value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        this.y = dhPubKey.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // we've received a public key (from one of the other parties),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // so we are ready to create the secret, which may be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // intermediate secret, in which case we wrap it into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // Diffie-Hellman public key object and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        generateSecret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (lastPhase == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            byte[] intermediate = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return new DHPublicKey(new BigInteger(1, intermediate),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                   init_p, init_g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
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
     * Generates the shared secret and returns it in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return the new buffer with the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    protected byte[] engineGenerateSecret()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (generateSecret == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                ("Key agreement has not been completed yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // Reset the key agreement here (in case anything goes wrong)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        generateSecret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // get the modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        BigInteger modulus = init_p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        BigInteger tmpResult = y.modPow(x, modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        byte[] secret = tmpResult.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * BigInteger.toByteArray will sometimes put a sign byte up front, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * we NEVER want one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if ((tmpResult.bitLength() % 8) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            byte retval[] = new byte[secret.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            System.arraycopy(secret, 1, retval, 0, retval.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return secret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
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
     * Generates the shared secret, and places it into the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <code>sharedSecret</code>, beginning at <code>offset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p>If the <code>sharedSecret</code> buffer is too small to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * result, a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * In this case, this call should be repeated with a larger output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param sharedSecret the buffer for the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param offset the offset in <code>sharedSecret</code> where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * shared secret will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return the number of bytes placed into <code>sharedSecret</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * to hold the secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    protected int engineGenerateSecret(byte[] sharedSecret, int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        throws IllegalStateException, ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (generateSecret == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                ("Key agreement has not been completed yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (sharedSecret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                ("No buffer provided for shared secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        BigInteger modulus = init_p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        byte[] secret = this.y.modPow(this.x, modulus).toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // BigInteger.toByteArray will sometimes put a sign byte up front,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // but we NEVER want one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if ((secret.length << 3) != modulus.bitLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if ((sharedSecret.length - offset) < (secret.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    ("Buffer too short for shared secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            System.arraycopy(secret, 1, sharedSecret, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                             secret.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // Reset the key agreement here (not earlier!), so that people
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            // can recover from ShortBufferException above without losing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            // internal state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            generateSecret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return secret.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if ((sharedSecret.length - offset) < secret.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    ("Buffer too short to hold shared secret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            System.arraycopy(secret, 0, sharedSecret, offset, secret.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // Reset the key agreement here (not earlier!), so that people
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // can recover from ShortBufferException above without losing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // internal state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            generateSecret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Creates the shared secret and returns it as a secret key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * of the requested algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param algorithm the requested secret key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return the shared secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @exception NoSuchAlgorithmException if the requested secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @exception InvalidKeyException if the shared secret key material cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * be used to generate a secret key of the requested algorithm type (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * the key material is too short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    protected SecretKey engineGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (algorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw new NoSuchAlgorithmException("null algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        byte[] secret = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (algorithm.equalsIgnoreCase("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return new DESKey(secret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else if (algorithm.equalsIgnoreCase("DESede")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                   || algorithm.equalsIgnoreCase("TripleDES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            // Triple DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return new DESedeKey(secret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } else if (algorithm.equalsIgnoreCase("Blowfish")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // Blowfish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            int keysize = secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (keysize >= BlowfishConstants.BLOWFISH_MAX_KEYSIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                keysize = BlowfishConstants.BLOWFISH_MAX_KEYSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            SecretKeySpec skey = new SecretKeySpec(secret, 0, keysize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                                   "Blowfish");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return skey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } else if (algorithm.equalsIgnoreCase("AES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // AES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            int keysize = secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            SecretKeySpec skey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            int idx = AESConstants.AES_KEYSIZES.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            while (skey == null && idx >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                // Generate the strongest key using the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                // assuming the key sizes in AESConstants class are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                // in ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                if (keysize >= AESConstants.AES_KEYSIZES[idx]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    keysize = AESConstants.AES_KEYSIZES[idx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    skey = new SecretKeySpec(secret, 0, keysize, "AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                idx--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (skey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                throw new InvalidKeyException("Key material is too short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return skey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        } else if (algorithm.equals("TlsPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            // return entire secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return new SecretKeySpec(secret, "TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new NoSuchAlgorithmException("Unsupported secret key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                               + "algorithm: "+ algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
}