Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/crypto/KeyAgreementSpi.java
38829 views
/*1* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto;2627import java.security.*;28import java.security.spec.*;2930/**31* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)32* for the <code>KeyAgreement</code> class.33* All the abstract methods in this class must be implemented by each34* cryptographic service provider who wishes to supply the implementation35* of a particular key agreement algorithm.36*37* <p> The keys involved in establishing a shared secret are created by one38* of the39* key generators (<code>KeyPairGenerator</code> or40* <code>KeyGenerator</code>), a <code>KeyFactory</code>, or as a result from41* an intermediate phase of the key agreement protocol42* ({@link #engineDoPhase(java.security.Key, boolean) engineDoPhase}).43*44* <p> For each of the correspondents in the key exchange,45* <code>engineDoPhase</code>46* needs to be called. For example, if the key exchange is with one other47* party, <code>engineDoPhase</code> needs to be called once, with the48* <code>lastPhase</code> flag set to <code>true</code>.49* If the key exchange is50* with two other parties, <code>engineDoPhase</code> needs to be called twice,51* the first time setting the <code>lastPhase</code> flag to52* <code>false</code>, and the second time setting it to <code>true</code>.53* There may be any number of parties involved in a key exchange.54*55* @author Jan Luehe56*57* @see KeyGenerator58* @see SecretKey59* @since 1.460*/6162public abstract class KeyAgreementSpi {6364/**65* Initializes this key agreement with the given key and source of66* randomness. The given key is required to contain all the algorithm67* parameters required for this key agreement.68*69* <p> If the key agreement algorithm requires random bytes, it gets them70* from the given source of randomness, <code>random</code>.71* However, if the underlying72* algorithm implementation does not require any random bytes,73* <code>random</code> is ignored.74*75* @param key the party's private information. For example, in the case76* of the Diffie-Hellman key agreement, this would be the party's own77* Diffie-Hellman private key.78* @param random the source of randomness79*80* @exception InvalidKeyException if the given key is81* inappropriate for this key agreement, e.g., is of the wrong type or82* has an incompatible algorithm type.83*/84protected abstract void engineInit(Key key, SecureRandom random)85throws InvalidKeyException;8687/**88* Initializes this key agreement with the given key, set of89* algorithm parameters, and source of randomness.90*91* @param key the party's private information. For example, in the case92* of the Diffie-Hellman key agreement, this would be the party's own93* Diffie-Hellman private key.94* @param params the key agreement parameters95* @param random the source of randomness96*97* @exception InvalidKeyException if the given key is98* inappropriate for this key agreement, e.g., is of the wrong type or99* has an incompatible algorithm type.100* @exception InvalidAlgorithmParameterException if the given parameters101* are inappropriate for this key agreement.102*/103protected abstract void engineInit(Key key, AlgorithmParameterSpec params,104SecureRandom random)105throws InvalidKeyException, InvalidAlgorithmParameterException;106107/**108* Executes the next phase of this key agreement with the given109* key that was received from one of the other parties involved in this key110* agreement.111*112* @param key the key for this phase. For example, in the case of113* Diffie-Hellman between 2 parties, this would be the other party's114* Diffie-Hellman public key.115* @param lastPhase flag which indicates whether or not this is the last116* phase of this key agreement.117*118* @return the (intermediate) key resulting from this phase, or null if119* this phase does not yield a key120*121* @exception InvalidKeyException if the given key is inappropriate for122* this phase.123* @exception IllegalStateException if this key agreement has not been124* initialized.125*/126protected abstract Key engineDoPhase(Key key, boolean lastPhase)127throws InvalidKeyException, IllegalStateException;128129/**130* Generates the shared secret and returns it in a new buffer.131*132* <p>This method resets this <code>KeyAgreementSpi</code> object,133* so that it134* can be reused for further key agreements. Unless this key agreement is135* reinitialized with one of the <code>engineInit</code> methods, the same136* private information and algorithm parameters will be used for137* subsequent key agreements.138*139* @return the new buffer with the shared secret140*141* @exception IllegalStateException if this key agreement has not been142* completed yet143*/144protected abstract byte[] engineGenerateSecret()145throws IllegalStateException;146147/**148* Generates the shared secret, and places it into the buffer149* <code>sharedSecret</code>, beginning at <code>offset</code> inclusive.150*151* <p>If the <code>sharedSecret</code> buffer is too small to hold the152* result, a <code>ShortBufferException</code> is thrown.153* In this case, this call should be repeated with a larger output buffer.154*155* <p>This method resets this <code>KeyAgreementSpi</code> object,156* so that it157* can be reused for further key agreements. Unless this key agreement is158* reinitialized with one of the <code>engineInit</code> methods, the same159* private information and algorithm parameters will be used for160* subsequent key agreements.161*162* @param sharedSecret the buffer for the shared secret163* @param offset the offset in <code>sharedSecret</code> where the164* shared secret will be stored165*166* @return the number of bytes placed into <code>sharedSecret</code>167*168* @exception IllegalStateException if this key agreement has not been169* completed yet170* @exception ShortBufferException if the given output buffer is too small171* to hold the secret172*/173protected abstract int engineGenerateSecret(byte[] sharedSecret,174int offset)175throws IllegalStateException, ShortBufferException;176177/**178* Creates the shared secret and returns it as a secret key object179* of the requested algorithm type.180*181* <p>This method resets this <code>KeyAgreementSpi</code> object,182* so that it183* can be reused for further key agreements. Unless this key agreement is184* reinitialized with one of the <code>engineInit</code> methods, the same185* private information and algorithm parameters will be used for186* subsequent key agreements.187*188* @param algorithm the requested secret key algorithm189*190* @return the shared secret key191*192* @exception IllegalStateException if this key agreement has not been193* completed yet194* @exception NoSuchAlgorithmException if the requested secret key195* algorithm is not available196* @exception InvalidKeyException if the shared secret key material cannot197* be used to generate a secret key of the requested algorithm type (e.g.,198* the key material is too short)199*/200protected abstract SecretKey engineGenerateSecret(String algorithm)201throws IllegalStateException, NoSuchAlgorithmException,202InvalidKeyException;203}204205206