Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/javax/crypto/KeyAgreementSpi.java
41152 views
1
/*
2
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.crypto;
27
28
import java.security.*;
29
import java.security.spec.*;
30
31
/**
32
* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
33
* for the <code>KeyAgreement</code> class.
34
* All the abstract methods in this class must be implemented by each
35
* cryptographic service provider who wishes to supply the implementation
36
* of a particular key agreement algorithm.
37
*
38
* <p> The keys involved in establishing a shared secret are created by one
39
* of the
40
* key generators (<code>KeyPairGenerator</code> or
41
* <code>KeyGenerator</code>), a <code>KeyFactory</code>, or as a result from
42
* an intermediate phase of the key agreement protocol
43
* ({@link #engineDoPhase(java.security.Key, boolean) engineDoPhase}).
44
*
45
* <p> For each of the correspondents in the key exchange,
46
* <code>engineDoPhase</code>
47
* needs to be called. For example, if the key exchange is with one other
48
* party, <code>engineDoPhase</code> needs to be called once, with the
49
* <code>lastPhase</code> flag set to <code>true</code>.
50
* If the key exchange is
51
* with two other parties, <code>engineDoPhase</code> needs to be called twice,
52
* the first time setting the <code>lastPhase</code> flag to
53
* <code>false</code>, and the second time setting it to <code>true</code>.
54
* There may be any number of parties involved in a key exchange.
55
*
56
* @author Jan Luehe
57
*
58
* @see KeyGenerator
59
* @see SecretKey
60
* @since 1.4
61
*/
62
63
public abstract class KeyAgreementSpi {
64
65
/**
66
* Constructor for subclasses to call.
67
*/
68
public KeyAgreementSpi() {}
69
70
/**
71
* Initializes this key agreement with the given key and source of
72
* randomness. The given key is required to contain all the algorithm
73
* parameters required for this key agreement.
74
*
75
* <p> If the key agreement algorithm requires random bytes, it gets them
76
* from the given source of randomness, <code>random</code>.
77
* However, if the underlying
78
* algorithm implementation does not require any random bytes,
79
* <code>random</code> is ignored.
80
*
81
* @param key the party's private information. For example, in the case
82
* of the Diffie-Hellman key agreement, this would be the party's own
83
* Diffie-Hellman private key.
84
* @param random the source of randomness
85
*
86
* @exception InvalidKeyException if the given key is
87
* inappropriate for this key agreement, e.g., is of the wrong type or
88
* has an incompatible algorithm type.
89
*/
90
protected abstract void engineInit(Key key, SecureRandom random)
91
throws InvalidKeyException;
92
93
/**
94
* Initializes this key agreement with the given key, set of
95
* algorithm parameters, and source of randomness.
96
*
97
* @param key the party's private information. For example, in the case
98
* of the Diffie-Hellman key agreement, this would be the party's own
99
* Diffie-Hellman private key.
100
* @param params the key agreement parameters
101
* @param random the source of randomness
102
*
103
* @exception InvalidKeyException if the given key is
104
* inappropriate for this key agreement, e.g., is of the wrong type or
105
* has an incompatible algorithm type.
106
* @exception InvalidAlgorithmParameterException if the given parameters
107
* are inappropriate for this key agreement.
108
*/
109
protected abstract void engineInit(Key key, AlgorithmParameterSpec params,
110
SecureRandom random)
111
throws InvalidKeyException, InvalidAlgorithmParameterException;
112
113
/**
114
* Executes the next phase of this key agreement with the given
115
* key that was received from one of the other parties involved in this key
116
* agreement.
117
*
118
* @param key the key for this phase. For example, in the case of
119
* Diffie-Hellman between 2 parties, this would be the other party's
120
* Diffie-Hellman public key.
121
* @param lastPhase flag which indicates whether or not this is the last
122
* phase of this key agreement.
123
*
124
* @return the (intermediate) key resulting from this phase, or null if
125
* this phase does not yield a key
126
*
127
* @exception InvalidKeyException if the given key is inappropriate for
128
* this phase.
129
* @exception IllegalStateException if this key agreement has not been
130
* initialized.
131
*/
132
protected abstract Key engineDoPhase(Key key, boolean lastPhase)
133
throws InvalidKeyException, IllegalStateException;
134
135
/**
136
* Generates the shared secret and returns it in a new buffer.
137
*
138
* <p>This method resets this {@code KeyAgreementSpi} object to the state
139
* that it was in after the most recent call to one of the {@code init}
140
* methods. After a call to {@code generateSecret}, the object can be reused
141
* for further key agreement operations by calling {@code doPhase} to supply
142
* new keys, and then calling {@code generateSecret} to produce a new
143
* secret. In this case, the private information and algorithm parameters
144
* supplied to {@code init} will be used for multiple key agreement
145
* operations. The {@code init} method can be called after
146
* {@code generateSecret} to change the private information used in
147
* subsequent operations.
148
*
149
* @return the new buffer with the shared secret
150
*
151
* @exception IllegalStateException if this key agreement has not been
152
* initialized or if {@code doPhase} has not been called to supply the
153
* keys for all parties in the agreement
154
*/
155
protected abstract byte[] engineGenerateSecret()
156
throws IllegalStateException;
157
158
/**
159
* Generates the shared secret, and places it into the buffer
160
* <code>sharedSecret</code>, beginning at <code>offset</code> inclusive.
161
*
162
* <p>If the <code>sharedSecret</code> buffer is too small to hold the
163
* result, a <code>ShortBufferException</code> is thrown.
164
* In this case, this call should be repeated with a larger output buffer.
165
*
166
* <p>This method resets this {@code KeyAgreementSpi} object to the state
167
* that it was in after the most recent call to one of the {@code init}
168
* methods. After a call to {@code generateSecret}, the object can be reused
169
* for further key agreement operations by calling {@code doPhase} to supply
170
* new keys, and then calling {@code generateSecret} to produce a new
171
* secret. In this case, the private information and algorithm parameters
172
* supplied to {@code init} will be used for multiple key agreement
173
* operations. The {@code init} method can be called after
174
* {@code generateSecret} to change the private information used in
175
* subsequent operations.
176
*
177
* @param sharedSecret the buffer for the shared secret
178
* @param offset the offset in <code>sharedSecret</code> where the
179
* shared secret will be stored
180
*
181
* @return the number of bytes placed into <code>sharedSecret</code>
182
*
183
* @exception IllegalStateException if this key agreement has not been
184
* initialized or if {@code doPhase} has not been called to supply the
185
* keys for all parties in the agreement
186
* @exception ShortBufferException if the given output buffer is too small
187
* to hold the secret
188
*/
189
protected abstract int engineGenerateSecret(byte[] sharedSecret,
190
int offset)
191
throws IllegalStateException, ShortBufferException;
192
193
/**
194
* Creates the shared secret and returns it as a secret key object
195
* of the requested algorithm type.
196
*
197
* <p>This method resets this {@code KeyAgreementSpi} object to the state
198
* that it was in after the most recent call to one of the {@code init}
199
* methods. After a call to {@code generateSecret}, the object can be reused
200
* for further key agreement operations by calling {@code doPhase} to supply
201
* new keys, and then calling {@code generateSecret} to produce a new
202
* secret. In this case, the private information and algorithm parameters
203
* supplied to {@code init} will be used for multiple key agreement
204
* operations. The {@code init} method can be called after
205
* {@code generateSecret} to change the private information used in
206
* subsequent operations.
207
*
208
* @param algorithm the requested secret key algorithm
209
*
210
* @return the shared secret key
211
*
212
* @exception IllegalStateException if this key agreement has not been
213
* initialized or if {@code doPhase} has not been called to supply the
214
* keys for all parties in the agreement
215
* @exception NoSuchAlgorithmException if the requested secret key
216
* algorithm is not available
217
* @exception InvalidKeyException if the shared secret key material cannot
218
* be used to generate a secret key of the requested algorithm type (e.g.,
219
* the key material is too short)
220
*/
221
protected abstract SecretKey engineGenerateSecret(String algorithm)
222
throws IllegalStateException, NoSuchAlgorithmException,
223
InvalidKeyException;
224
}
225
226