jdk/src/share/classes/javax/crypto/KeyAgreementSpi.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
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 javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * for the <code>KeyAgreement</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * All the abstract methods in this class must be implemented by each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * cryptographic service provider who wishes to supply the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * of a particular key agreement algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p> The keys involved in establishing a shared secret are created by one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * key generators (<code>KeyPairGenerator</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>KeyGenerator</code>), a <code>KeyFactory</code>, or as a result from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * an intermediate phase of the key agreement protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ({@link #engineDoPhase(java.security.Key, boolean) engineDoPhase}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> For each of the correspondents in the key exchange,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>engineDoPhase</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * needs to be called. For example, if the key exchange is with one other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * party, <code>engineDoPhase</code> needs to be called once, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>lastPhase</code> flag set to <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * If the key exchange is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * with two other parties, <code>engineDoPhase</code> needs to be called twice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the first time setting the <code>lastPhase</code> flag to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>false</code>, and the second time setting it to <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * There may be any number of parties involved in a key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see KeyGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public abstract class KeyAgreementSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Initializes this key agreement with the given key and source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * randomness. The given key is required to contain all the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * parameters required for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <p> If the key agreement algorithm requires random bytes, it gets them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * from the given source of randomness, <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * However, if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * algorithm implementation does not require any random bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <code>random</code> is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected abstract void engineInit(Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Initializes this key agreement with the given key, set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * algorithm parameters, and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param params the key agreement parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * are inappropriate for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected abstract void engineInit(Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                       SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        throws InvalidKeyException, InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Executes the next phase of this key agreement with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * key that was received from one of the other parties involved in this key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param key the key for this phase. For example, in the case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Diffie-Hellman between 2 parties, this would be the other party's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Diffie-Hellman public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param lastPhase flag which indicates whether or not this is the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * phase of this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return the (intermediate) key resulting from this phase, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * this phase does not yield a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * this phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected abstract Key engineDoPhase(Key key, boolean lastPhase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        throws InvalidKeyException, IllegalStateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Generates the shared secret and returns it in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return the new buffer with the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected abstract byte[] engineGenerateSecret()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        throws IllegalStateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Generates the shared secret, and places it into the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <code>sharedSecret</code>, beginning at <code>offset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p>If the <code>sharedSecret</code> buffer is too small to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * result, a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * In this case, this call should be repeated with a larger output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param sharedSecret the buffer for the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param offset the offset in <code>sharedSecret</code> where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * shared secret will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the number of bytes placed into <code>sharedSecret</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * to hold the secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected abstract int engineGenerateSecret(byte[] sharedSecret,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                                int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throws IllegalStateException, ShortBufferException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Creates the shared secret and returns it as a secret key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * of the requested algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * reinitialized with one of the <code>engineInit</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param algorithm the requested secret key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return the shared secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception NoSuchAlgorithmException if the requested secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @exception InvalidKeyException if the shared secret key material cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * be used to generate a secret key of the requested algorithm type (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * the key material is too short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected abstract SecretKey engineGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}