Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/FeedbackCipher.java
41161 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 com.sun.crypto.provider;
27
28
import java.nio.ByteBuffer;
29
import java.security.InvalidKeyException;
30
import java.security.InvalidAlgorithmParameterException;
31
import javax.crypto.*;
32
33
/**
34
* This class represents a block cipher in one of its modes. It wraps
35
* a SymmetricCipher maintaining the mode state and providing
36
* the capability to encrypt amounts of data larger than a single block.
37
*
38
* @author Jan Luehe
39
* @see ElectronicCodeBook
40
* @see CipherBlockChaining
41
* @see CipherFeedback
42
* @see OutputFeedback
43
* @see PCBC
44
*/
45
abstract class FeedbackCipher {
46
47
// the embedded block cipher
48
final SymmetricCipher embeddedCipher;
49
50
// the block size of the embedded block cipher
51
final int blockSize;
52
53
// the initialization vector
54
byte[] iv;
55
56
FeedbackCipher(SymmetricCipher embeddedCipher) {
57
this.embeddedCipher = embeddedCipher;
58
blockSize = embeddedCipher.getBlockSize();
59
}
60
61
final SymmetricCipher getEmbeddedCipher() {
62
return embeddedCipher;
63
}
64
65
/**
66
* Gets the block size of the embedded cipher.
67
*
68
* @return the block size of the embedded cipher
69
*/
70
final int getBlockSize() {
71
return blockSize;
72
}
73
74
/**
75
* Gets the name of the feedback mechanism
76
*
77
* @return the name of the feedback mechanism
78
*/
79
abstract String getFeedback();
80
81
/**
82
* Save the current content of this cipher.
83
*/
84
abstract void save();
85
86
/**
87
* Restores the content of this cipher to the previous saved one.
88
*/
89
abstract void restore();
90
91
/**
92
* Initializes the cipher in the specified mode with the given key
93
* and iv.
94
*
95
* @param decrypting flag indicating encryption or decryption mode
96
* @param algorithm the algorithm name (never null)
97
* @param key the key (never null)
98
* @param iv the iv (either null or blockSize bytes long)
99
*
100
* @exception InvalidKeyException if the given key is inappropriate for
101
* initializing this cipher
102
*/
103
abstract void init(boolean decrypting, String algorithm, byte[] key,
104
byte[] iv) throws InvalidKeyException,
105
InvalidAlgorithmParameterException;
106
107
/**
108
* Gets the initialization vector.
109
*
110
* @return the initialization vector
111
*/
112
final byte[] getIV() {
113
return iv;
114
}
115
116
/**
117
* Resets the iv to its original value.
118
* This is used when doFinal is called in the Cipher class, so that the
119
* cipher can be reused (with its original iv).
120
*/
121
abstract void reset();
122
123
/**
124
* Performs encryption operation.
125
*
126
* <p>The input <code>plain</code>, starting at <code>plainOffset</code>
127
* and ending at <code>(plainOffset+plainLen-1)</code>, is encrypted.
128
* The result is stored in <code>cipher</code>, starting at
129
* <code>cipherOffset</code>.
130
*
131
* <p>The subclass that implements Cipher should ensure that
132
* <code>init</code> has been called before this method is called.
133
*
134
* @param plain the input buffer with the data to be encrypted
135
* @param plainOffset the offset in <code>plain</code>
136
* @param plainLen the length of the input data
137
* @param cipher the buffer for the encryption result
138
* @param cipherOffset the offset in <code>cipher</code>
139
* @return the number of bytes placed into <code>cipher</code>
140
*/
141
abstract int encrypt(byte[] plain, int plainOffset, int plainLen,
142
byte[] cipher, int cipherOffset);
143
/**
144
* Performs encryption operation for the last time.
145
*
146
* <p>NOTE: For cipher feedback modes which does not perform
147
* special handling for the last few blocks, this is essentially
148
* the same as <code>encrypt(...)</code>. Given most modes do
149
* not do special handling, the default impl for this method is
150
* to simply call <code>encrypt(...)</code>.
151
*
152
* @param plain the input buffer with the data to be encrypted
153
* @param plainOffset the offset in <code>plain</code>
154
* @param plainLen the length of the input data
155
* @param cipher the buffer for the encryption result
156
* @param cipherOffset the offset in <code>cipher</code>
157
* @return the number of bytes placed into <code>cipher</code>
158
*/
159
int encryptFinal(byte[] plain, int plainOffset, int plainLen,
160
byte[] cipher, int cipherOffset)
161
throws IllegalBlockSizeException, ShortBufferException {
162
return encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
163
}
164
/**
165
* Performs decryption operation.
166
*
167
* <p>The input <code>cipher</code>, starting at <code>cipherOffset</code>
168
* and ending at <code>(cipherOffset+cipherLen-1)</code>, is decrypted.
169
* The result is stored in <code>plain</code>, starting at
170
* <code>plainOffset</code>.
171
*
172
* <p>The subclass that implements Cipher should ensure that
173
* <code>init</code> has been called before this method is called.
174
*
175
* @param cipher the input buffer with the data to be decrypted
176
* @param cipherOffset the offset in <code>cipher</code>
177
* @param cipherLen the length of the input data
178
* @param plain the buffer for the decryption result
179
* @param plainOffset the offset in <code>plain</code>
180
* @return the number of bytes placed into <code>plain</code>
181
*/
182
abstract int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
183
byte[] plain, int plainOffset);
184
185
/**
186
* Performs decryption operation for the last time.
187
*
188
* <p>NOTE: For cipher feedback modes which does not perform
189
* special handling for the last few blocks, this is essentially
190
* the same as <code>encrypt(...)</code>. Given most modes do
191
* not do special handling, the default impl for this method is
192
* to simply call <code>decrypt(...)</code>.
193
*
194
* @param cipher the input buffer with the data to be decrypted
195
* @param cipherOffset the offset in <code>cipher</code>
196
* @param cipherLen the length of the input data
197
* @param plain the buffer for the decryption result
198
* @param plainOffset the offset in <code>plain</code>
199
* @return the number of bytes placed into <code>plain</code>
200
*/
201
int decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,
202
byte[] plain, int plainOffset)
203
throws IllegalBlockSizeException, AEADBadTagException,
204
ShortBufferException {
205
return decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
206
}
207
208
/**
209
* Continues a multi-part update of the Additional Authentication
210
* Data (AAD), using a subset of the provided buffer. If this
211
* cipher is operating in either GCM or CCM mode, all AAD must be
212
* supplied before beginning operations on the ciphertext (via the
213
* {@code update} and {@code doFinal} methods).
214
* <p>
215
* NOTE: Given most modes do not accept AAD, default impl for this
216
* method throws IllegalStateException.
217
*
218
* @param src the buffer containing the AAD
219
* @param offset the offset in {@code src} where the AAD input starts
220
* @param len the number of AAD bytes
221
*
222
* @throws IllegalStateException if this cipher is in a wrong state
223
* (e.g., has not been initialized), does not accept AAD, or if
224
* operating in either GCM or CCM mode and one of the {@code update}
225
* methods has already been called for the active
226
* encryption/decryption operation
227
* @throws UnsupportedOperationException if this method
228
* has not been overridden by an implementation
229
*
230
* @since 1.8
231
*/
232
void updateAAD(byte[] src, int offset, int len) {
233
throw new IllegalStateException("No AAD accepted");
234
}
235
236
/**
237
* @return the number of bytes that are buffered internally inside
238
* this FeedbackCipher instance.
239
* @since 1.8
240
*/
241
int getBufferedLength() {
242
// Currently only AEAD cipher impl, e.g. GCM, buffers data
243
// internally during decryption mode
244
return 0;
245
}
246
247
/*
248
* ByteBuffer methods should not be accessed as CipherCore and AESCipher
249
* copy the data to byte arrays. These methods are to satisfy the compiler.
250
*/
251
int encrypt(ByteBuffer src, ByteBuffer dst) {
252
throw new UnsupportedOperationException("ByteBuffer not supported");
253
};
254
255
int decrypt(ByteBuffer src, ByteBuffer dst) {
256
throw new UnsupportedOperationException("ByteBuffer not supported");
257
};
258
259
int encryptFinal(ByteBuffer src, ByteBuffer dst)
260
throws IllegalBlockSizeException, ShortBufferException {
261
throw new UnsupportedOperationException("ByteBuffer not supported");
262
};
263
264
int decryptFinal(ByteBuffer src, ByteBuffer dst)
265
throws IllegalBlockSizeException, AEADBadTagException,
266
ShortBufferException {
267
throw new UnsupportedOperationException("ByteBuffer not supported");
268
}
269
}
270
271