author | vinnie |
Thu, 11 Apr 2013 17:57:08 +0100 | |
changeset 16909 | 78a1749a43e2 |
parent 15010 | ec6b49ce42b1 |
child 20752 | f0f0acea9113 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15008 | 2 |
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
15008 | 28 |
import java.util.Arrays; |
2 | 29 |
import java.util.Locale; |
30 |
||
31 |
import java.security.*; |
|
32 |
import java.security.spec.*; |
|
33 |
import javax.crypto.*; |
|
34 |
import javax.crypto.spec.*; |
|
35 |
import javax.crypto.BadPaddingException; |
|
36 |
||
37 |
/** |
|
38 |
* This class represents the symmetric algorithms in its various modes |
|
39 |
* (<code>ECB</code>, <code>CFB</code>, <code>OFB</code>, <code>CBC</code>, |
|
40 |
* <code>PCBC</code>, <code>CTR</code>, and <code>CTS</code>) and |
|
41 |
* padding schemes (<code>PKCS5Padding</code>, <code>NoPadding</code>, |
|
42 |
* <code>ISO10126Padding</code>). |
|
43 |
* |
|
44 |
* @author Gigi Ankeny |
|
45 |
* @author Jan Luehe |
|
46 |
* @see ElectronicCodeBook |
|
47 |
* @see CipherFeedback |
|
48 |
* @see OutputFeedback |
|
49 |
* @see CipherBlockChaining |
|
50 |
* @see PCBC |
|
51 |
* @see CounterMode |
|
52 |
* @see CipherTextStealing |
|
53 |
*/ |
|
54 |
||
55 |
final class CipherCore { |
|
56 |
||
57 |
/* |
|
58 |
* internal buffer |
|
59 |
*/ |
|
60 |
private byte[] buffer = null; |
|
61 |
||
62 |
/* |
|
15008 | 63 |
* block size of cipher in bytes |
2 | 64 |
*/ |
65 |
private int blockSize = 0; |
|
66 |
||
67 |
/* |
|
68 |
* unit size (number of input bytes that can be processed at a time) |
|
69 |
*/ |
|
70 |
private int unitBytes = 0; |
|
71 |
||
72 |
/* |
|
73 |
* index of the content size left in the buffer |
|
74 |
*/ |
|
75 |
private int buffered = 0; |
|
76 |
||
77 |
/* |
|
78 |
* minimum number of bytes in the buffer required for |
|
79 |
* FeedbackCipher.encryptFinal()/decryptFinal() call. |
|
15008 | 80 |
* update() must buffer this many bytes before starting |
2 | 81 |
* to encrypt/decrypt data. |
15008 | 82 |
* currently, only the following cases have non-zero values: |
83 |
* 1) CTS mode - due to its special handling on the last two blocks |
|
84 |
* (the last one may be incomplete). |
|
85 |
* 2) GCM mode + decryption - due to its trailing tag bytes |
|
2 | 86 |
*/ |
87 |
private int minBytes = 0; |
|
88 |
||
89 |
/* |
|
90 |
* number of bytes needed to make the total input length a multiple |
|
91 |
* of the blocksize (this is used in feedback mode, when the number of |
|
92 |
* input bytes that are processed at a time is different from the block |
|
93 |
* size) |
|
94 |
*/ |
|
95 |
private int diffBlocksize = 0; |
|
96 |
||
97 |
/* |
|
98 |
* padding class |
|
99 |
*/ |
|
100 |
private Padding padding = null; |
|
101 |
||
102 |
/* |
|
103 |
* internal cipher engine |
|
104 |
*/ |
|
105 |
private FeedbackCipher cipher = null; |
|
106 |
||
107 |
/* |
|
108 |
* the cipher mode |
|
109 |
*/ |
|
110 |
private int cipherMode = ECB_MODE; |
|
111 |
||
112 |
/* |
|
113 |
* are we encrypting or decrypting? |
|
114 |
*/ |
|
115 |
private boolean decrypting = false; |
|
116 |
||
117 |
/* |
|
118 |
* Block Mode constants |
|
119 |
*/ |
|
120 |
private static final int ECB_MODE = 0; |
|
121 |
private static final int CBC_MODE = 1; |
|
122 |
private static final int CFB_MODE = 2; |
|
123 |
private static final int OFB_MODE = 3; |
|
124 |
private static final int PCBC_MODE = 4; |
|
125 |
private static final int CTR_MODE = 5; |
|
126 |
private static final int CTS_MODE = 6; |
|
15008 | 127 |
private static final int GCM_MODE = 7; |
128 |
||
129 |
/* |
|
130 |
* variables used for performing the GCM (key+iv) uniqueness check. |
|
131 |
* To use GCM mode safely, the cipher object must be re-initialized |
|
132 |
* with a different combination of key + iv values for each |
|
133 |
* encryption operation. However, checking all past key + iv values |
|
134 |
* isn't feasible. Thus, we only do a per-instance check of the |
|
135 |
* key + iv values used in previous encryption. |
|
136 |
* For decryption operations, no checking is necessary. |
|
137 |
* NOTE: this key+iv check have to be done inside CipherCore class |
|
138 |
* since CipherCore class buffers potential tag bytes in GCM mode |
|
139 |
* and may not call GaloisCounterMode when there isn't sufficient |
|
140 |
* input to process. |
|
141 |
*/ |
|
142 |
private boolean requireReinit = false; |
|
143 |
private byte[] lastEncKey = null; |
|
144 |
private byte[] lastEncIv = null; |
|
2 | 145 |
|
146 |
/** |
|
147 |
* Creates an instance of CipherCore with default ECB mode and |
|
148 |
* PKCS5Padding. |
|
149 |
*/ |
|
150 |
CipherCore(SymmetricCipher impl, int blkSize) { |
|
151 |
blockSize = blkSize; |
|
152 |
unitBytes = blkSize; |
|
153 |
diffBlocksize = blkSize; |
|
154 |
||
155 |
/* |
|
156 |
* The buffer should be usable for all cipher mode and padding |
|
157 |
* schemes. Thus, it has to be at least (blockSize+1) for CTS. |
|
158 |
* In decryption mode, it also hold the possible padding block. |
|
159 |
*/ |
|
160 |
buffer = new byte[blockSize*2]; |
|
161 |
||
162 |
// set mode and padding |
|
163 |
cipher = new ElectronicCodeBook(impl); |
|
164 |
padding = new PKCS5Padding(blockSize); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Sets the mode of this cipher. |
|
169 |
* |
|
170 |
* @param mode the cipher mode |
|
171 |
* |
|
172 |
* @exception NoSuchAlgorithmException if the requested cipher mode does |
|
15008 | 173 |
* not exist for this cipher |
2 | 174 |
*/ |
175 |
void setMode(String mode) throws NoSuchAlgorithmException { |
|
176 |
if (mode == null) |
|
177 |
throw new NoSuchAlgorithmException("null mode"); |
|
178 |
||
179 |
String modeUpperCase = mode.toUpperCase(Locale.ENGLISH); |
|
180 |
||
181 |
if (modeUpperCase.equals("ECB")) { |
|
182 |
return; |
|
183 |
} |
|
184 |
||
185 |
SymmetricCipher rawImpl = cipher.getEmbeddedCipher(); |
|
186 |
if (modeUpperCase.equals("CBC")) { |
|
187 |
cipherMode = CBC_MODE; |
|
188 |
cipher = new CipherBlockChaining(rawImpl); |
|
15008 | 189 |
} else if (modeUpperCase.equals("CTS")) { |
2 | 190 |
cipherMode = CTS_MODE; |
191 |
cipher = new CipherTextStealing(rawImpl); |
|
192 |
minBytes = blockSize+1; |
|
193 |
padding = null; |
|
15008 | 194 |
} else if (modeUpperCase.equals("CTR")) { |
2 | 195 |
cipherMode = CTR_MODE; |
196 |
cipher = new CounterMode(rawImpl); |
|
197 |
unitBytes = 1; |
|
198 |
padding = null; |
|
15008 | 199 |
} else if (modeUpperCase.startsWith("GCM")) { |
200 |
// can only be used for block ciphers w/ 128-bit block size |
|
201 |
if (blockSize != 16) { |
|
202 |
throw new NoSuchAlgorithmException |
|
203 |
("GCM mode can only be used for AES cipher"); |
|
204 |
} |
|
205 |
cipherMode = GCM_MODE; |
|
206 |
cipher = new GaloisCounterMode(rawImpl); |
|
207 |
padding = null; |
|
208 |
} else if (modeUpperCase.startsWith("CFB")) { |
|
2 | 209 |
cipherMode = CFB_MODE; |
210 |
unitBytes = getNumOfUnit(mode, "CFB".length(), blockSize); |
|
211 |
cipher = new CipherFeedback(rawImpl, unitBytes); |
|
15008 | 212 |
} else if (modeUpperCase.startsWith("OFB")) { |
2 | 213 |
cipherMode = OFB_MODE; |
214 |
unitBytes = getNumOfUnit(mode, "OFB".length(), blockSize); |
|
215 |
cipher = new OutputFeedback(rawImpl, unitBytes); |
|
15008 | 216 |
} else if (modeUpperCase.equals("PCBC")) { |
2 | 217 |
cipherMode = PCBC_MODE; |
218 |
cipher = new PCBC(rawImpl); |
|
219 |
} |
|
220 |
else { |
|
221 |
throw new NoSuchAlgorithmException("Cipher mode: " + mode |
|
222 |
+ " not found"); |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
private static int getNumOfUnit(String mode, int offset, int blockSize) |
|
227 |
throws NoSuchAlgorithmException { |
|
228 |
int result = blockSize; // use blockSize as default value |
|
229 |
if (mode.length() > offset) { |
|
230 |
int numInt; |
|
231 |
try { |
|
232 |
Integer num = Integer.valueOf(mode.substring(offset)); |
|
233 |
numInt = num.intValue(); |
|
234 |
result = numInt >> 3; |
|
235 |
} catch (NumberFormatException e) { |
|
236 |
throw new NoSuchAlgorithmException |
|
237 |
("Algorithm mode: " + mode + " not implemented"); |
|
238 |
} |
|
239 |
if ((numInt % 8 != 0) || (result > blockSize)) { |
|
240 |
throw new NoSuchAlgorithmException |
|
241 |
("Invalid algorithm mode: " + mode); |
|
242 |
} |
|
243 |
} |
|
244 |
return result; |
|
245 |
} |
|
246 |
||
15008 | 247 |
|
2 | 248 |
/** |
249 |
* Sets the padding mechanism of this cipher. |
|
250 |
* |
|
251 |
* @param padding the padding mechanism |
|
252 |
* |
|
253 |
* @exception NoSuchPaddingException if the requested padding mechanism |
|
254 |
* does not exist |
|
255 |
*/ |
|
256 |
void setPadding(String paddingScheme) |
|
257 |
throws NoSuchPaddingException |
|
258 |
{ |
|
259 |
if (paddingScheme == null) { |
|
260 |
throw new NoSuchPaddingException("null padding"); |
|
261 |
} |
|
262 |
if (paddingScheme.equalsIgnoreCase("NoPadding")) { |
|
263 |
padding = null; |
|
264 |
} else if (paddingScheme.equalsIgnoreCase("ISO10126Padding")) { |
|
265 |
padding = new ISO10126Padding(blockSize); |
|
266 |
} else if (!paddingScheme.equalsIgnoreCase("PKCS5Padding")) { |
|
267 |
throw new NoSuchPaddingException("Padding: " + paddingScheme |
|
268 |
+ " not implemented"); |
|
269 |
} |
|
270 |
if ((padding != null) && |
|
15008 | 271 |
((cipherMode == CTR_MODE) || (cipherMode == CTS_MODE) |
272 |
|| (cipherMode == GCM_MODE))) { |
|
2 | 273 |
padding = null; |
15008 | 274 |
String modeStr = null; |
275 |
switch (cipherMode) { |
|
276 |
case CTR_MODE: |
|
277 |
modeStr = "CTR"; |
|
278 |
break; |
|
279 |
case GCM_MODE: |
|
280 |
modeStr = "GCM"; |
|
281 |
break; |
|
282 |
case CTS_MODE: |
|
283 |
modeStr = "CTS"; |
|
284 |
break; |
|
285 |
default: |
|
286 |
// should never happen |
|
287 |
} |
|
288 |
if (modeStr != null) { |
|
289 |
throw new NoSuchPaddingException |
|
290 |
(modeStr + " mode must be used with NoPadding"); |
|
291 |
} |
|
2 | 292 |
} |
293 |
} |
|
294 |
||
295 |
/** |
|
296 |
* Returns the length in bytes that an output buffer would need to be in |
|
297 |
* order to hold the result of the next <code>update</code> or |
|
298 |
* <code>doFinal</code> operation, given the input length |
|
299 |
* <code>inputLen</code> (in bytes). |
|
300 |
* |
|
301 |
* <p>This call takes into account any unprocessed (buffered) data from a |
|
15008 | 302 |
* previous <code>update</code> call, padding, and AEAD tagging. |
2 | 303 |
* |
304 |
* <p>The actual output length of the next <code>update</code> or |
|
305 |
* <code>doFinal</code> call may be smaller than the length returned by |
|
306 |
* this method. |
|
307 |
* |
|
308 |
* @param inputLen the input length (in bytes) |
|
309 |
* |
|
310 |
* @return the required output buffer size (in bytes) |
|
311 |
*/ |
|
312 |
int getOutputSize(int inputLen) { |
|
313 |
int totalLen = buffered + inputLen; |
|
314 |
||
15008 | 315 |
// GCM: this call may be for either update() or doFinal(), so have to |
316 |
// return the larger value of both |
|
317 |
// Encryption: based on doFinal value: inputLen + tag |
|
318 |
// Decryption: based on update value: inputLen |
|
319 |
if (!decrypting && (cipherMode == GCM_MODE)) { |
|
320 |
return (totalLen + ((GaloisCounterMode) cipher).getTagLen()); |
|
321 |
} |
|
2 | 322 |
|
15008 | 323 |
if (padding == null) { |
2 | 324 |
return totalLen; |
15008 | 325 |
} |
326 |
||
327 |
if (decrypting) { |
|
328 |
return totalLen; |
|
329 |
} |
|
2 | 330 |
|
331 |
if (unitBytes != blockSize) { |
|
15008 | 332 |
if (totalLen < diffBlocksize) { |
2 | 333 |
return diffBlocksize; |
15008 | 334 |
} else { |
2 | 335 |
return (totalLen + blockSize - |
336 |
((totalLen - diffBlocksize) % blockSize)); |
|
15008 | 337 |
} |
2 | 338 |
} else { |
339 |
return totalLen + padding.padLength(totalLen); |
|
340 |
} |
|
341 |
} |
|
342 |
||
15008 | 343 |
private int getOutputSizeByOperation(int inputLen, boolean isDoFinal) { |
344 |
int totalLen = 0; |
|
345 |
switch (cipherMode) { |
|
346 |
case GCM_MODE: |
|
347 |
totalLen = buffered + inputLen; |
|
348 |
if (isDoFinal) { |
|
349 |
int tagLen = ((GaloisCounterMode) cipher).getTagLen(); |
|
350 |
if (decrypting) { |
|
351 |
// need to get the actual value from cipher?? |
|
352 |
// deduct tagLen |
|
353 |
totalLen -= tagLen; |
|
354 |
} else { |
|
355 |
totalLen += tagLen; |
|
356 |
} |
|
357 |
} |
|
358 |
if (totalLen < 0) { |
|
359 |
totalLen = 0; |
|
360 |
} |
|
361 |
break; |
|
362 |
default: |
|
363 |
totalLen = getOutputSize(inputLen); |
|
364 |
break; |
|
365 |
} |
|
366 |
return totalLen; |
|
367 |
} |
|
368 |
||
2 | 369 |
/** |
370 |
* Returns the initialization vector (IV) in a new buffer. |
|
371 |
* |
|
372 |
* <p>This is useful in the case where a random IV has been created |
|
373 |
* (see <a href = "#init">init</a>), |
|
374 |
* or in the context of password-based encryption or |
|
375 |
* decryption, where the IV is derived from a user-provided password. |
|
376 |
* |
|
377 |
* @return the initialization vector in a new buffer, or null if the |
|
378 |
* underlying algorithm does not use an IV, or if the IV has not yet |
|
379 |
* been set. |
|
380 |
*/ |
|
381 |
byte[] getIV() { |
|
382 |
byte[] iv = cipher.getIV(); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
383 |
return (iv == null) ? null : iv.clone(); |
2 | 384 |
} |
385 |
||
386 |
/** |
|
387 |
* Returns the parameters used with this cipher. |
|
388 |
* |
|
389 |
* <p>The returned parameters may be the same that were used to initialize |
|
390 |
* this cipher, or may contain the default set of parameters or a set of |
|
391 |
* randomly generated parameters used by the underlying cipher |
|
392 |
* implementation (provided that the underlying cipher implementation |
|
393 |
* uses a default set of parameters or creates new parameters if it needs |
|
394 |
* parameters but was not initialized with any). |
|
395 |
* |
|
396 |
* @return the parameters used with this cipher, or null if this cipher |
|
397 |
* does not use any parameters. |
|
398 |
*/ |
|
399 |
AlgorithmParameters getParameters(String algName) { |
|
15008 | 400 |
if (cipherMode == ECB_MODE) { |
401 |
return null; |
|
402 |
} |
|
2 | 403 |
AlgorithmParameters params = null; |
15008 | 404 |
AlgorithmParameterSpec spec; |
2 | 405 |
byte[] iv = getIV(); |
15008 | 406 |
if (iv == null) { |
407 |
// generate spec using default value |
|
408 |
if (cipherMode == GCM_MODE) { |
|
409 |
iv = new byte[GaloisCounterMode.DEFAULT_IV_LEN]; |
|
2 | 410 |
} else { |
15008 | 411 |
iv = new byte[blockSize]; |
2 | 412 |
} |
15010 | 413 |
SunJCE.getRandom().nextBytes(iv); |
15008 | 414 |
} |
415 |
if (cipherMode == GCM_MODE) { |
|
416 |
algName = "GCM"; |
|
417 |
spec = new GCMParameterSpec |
|
418 |
(((GaloisCounterMode) cipher).getTagLen()*8, iv); |
|
419 |
} else { |
|
420 |
if (algName.equals("RC2")) { |
|
421 |
RC2Crypt rawImpl = (RC2Crypt) cipher.getEmbeddedCipher(); |
|
422 |
spec = new RC2ParameterSpec |
|
423 |
(rawImpl.getEffectiveKeyBits(), iv); |
|
424 |
} else { |
|
425 |
spec = new IvParameterSpec(iv); |
|
426 |
} |
|
427 |
} |
|
428 |
try { |
|
16909
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
429 |
params = AlgorithmParameters.getInstance(algName, |
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
430 |
SunJCE.getInstance()); |
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
431 |
params.init(spec); |
15008 | 432 |
} catch (NoSuchAlgorithmException nsae) { |
433 |
// should never happen |
|
434 |
throw new RuntimeException("Cannot find " + algName + |
|
435 |
" AlgorithmParameters implementation in SunJCE provider"); |
|
436 |
} catch (InvalidParameterSpecException ipse) { |
|
437 |
// should never happen |
|
438 |
throw new RuntimeException(spec.getClass() + " not supported"); |
|
2 | 439 |
} |
440 |
return params; |
|
441 |
} |
|
442 |
||
443 |
/** |
|
444 |
* Initializes this cipher with a key and a source of randomness. |
|
445 |
* |
|
446 |
* <p>The cipher is initialized for one of the following four operations: |
|
447 |
* encryption, decryption, key wrapping or key unwrapping, depending on |
|
448 |
* the value of <code>opmode</code>. |
|
449 |
* |
|
450 |
* <p>If this cipher requires an initialization vector (IV), it will get |
|
451 |
* it from <code>random</code>. |
|
452 |
* This behaviour should only be used in encryption or key wrapping |
|
453 |
* mode, however. |
|
454 |
* When initializing a cipher that requires an IV for decryption or |
|
455 |
* key unwrapping, the IV |
|
456 |
* (same IV that was used for encryption or key wrapping) must be provided |
|
457 |
* explicitly as a |
|
458 |
* parameter, in order to get the correct result. |
|
459 |
* |
|
460 |
* <p>This method also cleans existing buffer and other related state |
|
461 |
* information. |
|
462 |
* |
|
463 |
* @param opmode the operation mode of this cipher (this is one of |
|
464 |
* the following: |
|
465 |
* <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>, |
|
466 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) |
|
467 |
* @param key the secret key |
|
468 |
* @param random the source of randomness |
|
469 |
* |
|
470 |
* @exception InvalidKeyException if the given key is inappropriate for |
|
471 |
* initializing this cipher |
|
472 |
*/ |
|
473 |
void init(int opmode, Key key, SecureRandom random) |
|
474 |
throws InvalidKeyException { |
|
475 |
try { |
|
476 |
init(opmode, key, (AlgorithmParameterSpec)null, random); |
|
477 |
} catch (InvalidAlgorithmParameterException e) { |
|
478 |
throw new InvalidKeyException(e.getMessage()); |
|
479 |
} |
|
480 |
} |
|
481 |
||
482 |
/** |
|
483 |
* Initializes this cipher with a key, a set of |
|
484 |
* algorithm parameters, and a source of randomness. |
|
485 |
* |
|
486 |
* <p>The cipher is initialized for one of the following four operations: |
|
487 |
* encryption, decryption, key wrapping or key unwrapping, depending on |
|
488 |
* the value of <code>opmode</code>. |
|
489 |
* |
|
490 |
* <p>If this cipher (including its underlying feedback or padding scheme) |
|
491 |
* requires any random bytes, it will get them from <code>random</code>. |
|
492 |
* |
|
493 |
* @param opmode the operation mode of this cipher (this is one of |
|
494 |
* the following: |
|
495 |
* <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>, |
|
496 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) |
|
497 |
* @param key the encryption key |
|
498 |
* @param params the algorithm parameters |
|
499 |
* @param random the source of randomness |
|
500 |
* |
|
501 |
* @exception InvalidKeyException if the given key is inappropriate for |
|
502 |
* initializing this cipher |
|
503 |
* @exception InvalidAlgorithmParameterException if the given algorithm |
|
504 |
* parameters are inappropriate for this cipher |
|
505 |
*/ |
|
506 |
void init(int opmode, Key key, AlgorithmParameterSpec params, |
|
507 |
SecureRandom random) |
|
508 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
509 |
decrypting = (opmode == Cipher.DECRYPT_MODE) |
|
510 |
|| (opmode == Cipher.UNWRAP_MODE); |
|
511 |
||
512 |
byte[] keyBytes = getKeyBytes(key); |
|
15008 | 513 |
int tagLen = -1; |
514 |
byte[] ivBytes = null; |
|
515 |
if (params != null) { |
|
516 |
if (cipherMode == GCM_MODE) { |
|
517 |
if (params instanceof GCMParameterSpec) { |
|
518 |
tagLen = ((GCMParameterSpec)params).getTLen(); |
|
519 |
if (tagLen < 96 || tagLen > 128 || ((tagLen & 0x07) != 0)) { |
|
520 |
throw new InvalidAlgorithmParameterException |
|
521 |
("Unsupported TLen value; must be one of " + |
|
522 |
"{128, 120, 112, 104, 96}"); |
|
523 |
} |
|
524 |
tagLen = tagLen >> 3; |
|
525 |
ivBytes = ((GCMParameterSpec)params).getIV(); |
|
526 |
} else { |
|
527 |
throw new InvalidAlgorithmParameterException |
|
528 |
("Unsupported parameter: " + params); |
|
529 |
} |
|
530 |
} else { |
|
531 |
if (params instanceof IvParameterSpec) { |
|
532 |
ivBytes = ((IvParameterSpec)params).getIV(); |
|
533 |
if ((ivBytes == null) || (ivBytes.length != blockSize)) { |
|
534 |
throw new InvalidAlgorithmParameterException |
|
535 |
("Wrong IV length: must be " + blockSize + |
|
536 |
" bytes long"); |
|
537 |
} |
|
538 |
} else if (params instanceof RC2ParameterSpec) { |
|
539 |
ivBytes = ((RC2ParameterSpec)params).getIV(); |
|
540 |
if ((ivBytes != null) && (ivBytes.length != blockSize)) { |
|
541 |
throw new InvalidAlgorithmParameterException |
|
542 |
("Wrong IV length: must be " + blockSize + |
|
543 |
" bytes long"); |
|
544 |
} |
|
545 |
} else { |
|
546 |
throw new InvalidAlgorithmParameterException |
|
547 |
("Unsupported parameter: " + params); |
|
548 |
} |
|
2 | 549 |
} |
550 |
} |
|
551 |
if (cipherMode == ECB_MODE) { |
|
552 |
if (ivBytes != null) { |
|
553 |
throw new InvalidAlgorithmParameterException |
|
554 |
("ECB mode cannot use IV"); |
|
555 |
} |
|
15008 | 556 |
} else if (ivBytes == null) { |
2 | 557 |
if (decrypting) { |
558 |
throw new InvalidAlgorithmParameterException("Parameters " |
|
559 |
+ "missing"); |
|
560 |
} |
|
15008 | 561 |
|
2 | 562 |
if (random == null) { |
15010 | 563 |
random = SunJCE.getRandom(); |
2 | 564 |
} |
15008 | 565 |
if (cipherMode == GCM_MODE) { |
566 |
ivBytes = new byte[GaloisCounterMode.DEFAULT_IV_LEN]; |
|
567 |
} else { |
|
568 |
ivBytes = new byte[blockSize]; |
|
569 |
} |
|
2 | 570 |
random.nextBytes(ivBytes); |
571 |
} |
|
572 |
||
573 |
buffered = 0; |
|
574 |
diffBlocksize = blockSize; |
|
575 |
||
576 |
String algorithm = key.getAlgorithm(); |
|
577 |
||
15008 | 578 |
// GCM mode needs additional handling |
579 |
if (cipherMode == GCM_MODE) { |
|
580 |
if(tagLen == -1) { |
|
581 |
tagLen = GaloisCounterMode.DEFAULT_TAG_LEN; |
|
582 |
} |
|
583 |
if (decrypting) { |
|
584 |
minBytes = tagLen; |
|
585 |
} else { |
|
586 |
// check key+iv for encryption in GCM mode |
|
587 |
requireReinit = |
|
588 |
Arrays.equals(ivBytes, lastEncIv) && |
|
589 |
Arrays.equals(keyBytes, lastEncKey); |
|
590 |
if (requireReinit) { |
|
591 |
throw new InvalidAlgorithmParameterException |
|
592 |
("Cannot reuse iv for GCM encryption"); |
|
593 |
} |
|
594 |
lastEncIv = ivBytes; |
|
595 |
lastEncKey = keyBytes; |
|
596 |
} |
|
597 |
((GaloisCounterMode) cipher).init |
|
598 |
(decrypting, algorithm, keyBytes, ivBytes, tagLen); |
|
599 |
} else { |
|
600 |
cipher.init(decrypting, algorithm, keyBytes, ivBytes); |
|
601 |
} |
|
602 |
// skip checking key+iv from now on until after doFinal() |
|
603 |
requireReinit = false; |
|
2 | 604 |
} |
605 |
||
606 |
void init(int opmode, Key key, AlgorithmParameters params, |
|
607 |
SecureRandom random) |
|
608 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
15008 | 609 |
AlgorithmParameterSpec spec = null; |
610 |
String paramType = null; |
|
2 | 611 |
if (params != null) { |
612 |
try { |
|
15008 | 613 |
if (cipherMode == GCM_MODE) { |
614 |
paramType = "GCM"; |
|
615 |
spec = params.getParameterSpec(GCMParameterSpec.class); |
|
616 |
} else { |
|
617 |
// NOTE: RC2 parameters are always handled through |
|
618 |
// init(..., AlgorithmParameterSpec,...) method, so |
|
619 |
// we can assume IvParameterSpec type here. |
|
620 |
paramType = "IV"; |
|
621 |
spec = params.getParameterSpec(IvParameterSpec.class); |
|
622 |
} |
|
2 | 623 |
} catch (InvalidParameterSpecException ipse) { |
15008 | 624 |
throw new InvalidAlgorithmParameterException |
625 |
("Wrong parameter type: " + paramType + " expected"); |
|
2 | 626 |
} |
627 |
} |
|
15008 | 628 |
init(opmode, key, spec, random); |
2 | 629 |
} |
630 |
||
631 |
/** |
|
632 |
* Return the key bytes of the specified key. Throw an InvalidKeyException |
|
633 |
* if the key is not usable. |
|
634 |
*/ |
|
635 |
static byte[] getKeyBytes(Key key) throws InvalidKeyException { |
|
636 |
if (key == null) { |
|
637 |
throw new InvalidKeyException("No key given"); |
|
638 |
} |
|
639 |
// note: key.getFormat() may return null |
|
640 |
if (!"RAW".equalsIgnoreCase(key.getFormat())) { |
|
641 |
throw new InvalidKeyException("Wrong format: RAW bytes needed"); |
|
642 |
} |
|
643 |
byte[] keyBytes = key.getEncoded(); |
|
644 |
if (keyBytes == null) { |
|
645 |
throw new InvalidKeyException("RAW key bytes missing"); |
|
646 |
} |
|
647 |
return keyBytes; |
|
648 |
} |
|
649 |
||
15008 | 650 |
|
2 | 651 |
/** |
652 |
* Continues a multiple-part encryption or decryption operation |
|
653 |
* (depending on how this cipher was initialized), processing another data |
|
654 |
* part. |
|
655 |
* |
|
656 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
657 |
* buffer, starting at <code>inputOffset</code>, are processed, and the |
|
658 |
* result is stored in a new buffer. |
|
659 |
* |
|
660 |
* @param input the input buffer |
|
661 |
* @param inputOffset the offset in <code>input</code> where the input |
|
662 |
* starts |
|
663 |
* @param inputLen the input length |
|
664 |
* |
|
665 |
* @return the new buffer with the result |
|
666 |
* |
|
667 |
* @exception IllegalStateException if this cipher is in a wrong state |
|
668 |
* (e.g., has not been initialized) |
|
669 |
*/ |
|
670 |
byte[] update(byte[] input, int inputOffset, int inputLen) { |
|
15008 | 671 |
if (requireReinit) { |
672 |
throw new IllegalStateException |
|
673 |
("Must use either different key or iv for GCM encryption"); |
|
674 |
} |
|
675 |
||
2 | 676 |
byte[] output = null; |
677 |
try { |
|
15008 | 678 |
output = new byte[getOutputSizeByOperation(inputLen, false)]; |
2 | 679 |
int len = update(input, inputOffset, inputLen, output, |
680 |
0); |
|
681 |
if (len == output.length) { |
|
15008 | 682 |
return output; |
2 | 683 |
} else { |
15008 | 684 |
return Arrays.copyOf(output, len); |
2 | 685 |
} |
686 |
} catch (ShortBufferException e) { |
|
15008 | 687 |
// should never happen |
688 |
throw new ProviderException("Unexpected exception", e); |
|
2 | 689 |
} |
690 |
} |
|
691 |
||
692 |
/** |
|
693 |
* Continues a multiple-part encryption or decryption operation |
|
694 |
* (depending on how this cipher was initialized), processing another data |
|
695 |
* part. |
|
696 |
* |
|
697 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
698 |
* buffer, starting at <code>inputOffset</code>, are processed, and the |
|
699 |
* result is stored in the <code>output</code> buffer, starting at |
|
700 |
* <code>outputOffset</code>. |
|
701 |
* |
|
702 |
* @param input the input buffer |
|
703 |
* @param inputOffset the offset in <code>input</code> where the input |
|
704 |
* starts |
|
705 |
* @param inputLen the input length |
|
706 |
* @param output the buffer for the result |
|
707 |
* @param outputOffset the offset in <code>output</code> where the result |
|
708 |
* is stored |
|
709 |
* |
|
710 |
* @return the number of bytes stored in <code>output</code> |
|
711 |
* |
|
712 |
* @exception ShortBufferException if the given output buffer is too small |
|
713 |
* to hold the result |
|
714 |
*/ |
|
715 |
int update(byte[] input, int inputOffset, int inputLen, byte[] output, |
|
716 |
int outputOffset) throws ShortBufferException { |
|
15008 | 717 |
if (requireReinit) { |
718 |
throw new IllegalStateException |
|
719 |
("Must use either different key or iv for GCM encryption"); |
|
720 |
} |
|
721 |
||
2 | 722 |
// figure out how much can be sent to crypto function |
723 |
int len = buffered + inputLen - minBytes; |
|
724 |
if (padding != null && decrypting) { |
|
725 |
// do not include the padding bytes when decrypting |
|
726 |
len -= blockSize; |
|
727 |
} |
|
728 |
// do not count the trailing bytes which do not make up a unit |
|
729 |
len = (len > 0 ? (len - (len%unitBytes)) : 0); |
|
730 |
||
731 |
// check output buffer capacity |
|
732 |
if ((output == null) || ((output.length - outputOffset) < len)) { |
|
733 |
throw new ShortBufferException("Output buffer must be " |
|
734 |
+ "(at least) " + len |
|
735 |
+ " bytes long"); |
|
736 |
} |
|
15008 | 737 |
|
2 | 738 |
if (len != 0) { |
739 |
// there is some work to do |
|
740 |
byte[] in = new byte[len]; |
|
741 |
||
742 |
int inputConsumed = len - buffered; |
|
743 |
int bufferedConsumed = buffered; |
|
744 |
if (inputConsumed < 0) { |
|
745 |
inputConsumed = 0; |
|
746 |
bufferedConsumed = len; |
|
747 |
} |
|
748 |
||
749 |
if (buffered != 0) { |
|
750 |
System.arraycopy(buffer, 0, in, 0, bufferedConsumed); |
|
751 |
} |
|
752 |
if (inputConsumed > 0) { |
|
753 |
System.arraycopy(input, inputOffset, in, |
|
754 |
bufferedConsumed, inputConsumed); |
|
755 |
} |
|
756 |
if (decrypting) { |
|
757 |
cipher.decrypt(in, 0, len, output, outputOffset); |
|
758 |
} else { |
|
759 |
cipher.encrypt(in, 0, len, output, outputOffset); |
|
760 |
} |
|
761 |
||
762 |
// Let's keep track of how many bytes are needed to make |
|
763 |
// the total input length a multiple of blocksize when |
|
764 |
// padding is applied |
|
765 |
if (unitBytes != blockSize) { |
|
15008 | 766 |
if (len < diffBlocksize) { |
2 | 767 |
diffBlocksize -= len; |
15008 | 768 |
} else { |
2 | 769 |
diffBlocksize = blockSize - |
770 |
((len - diffBlocksize) % blockSize); |
|
15008 | 771 |
} |
2 | 772 |
} |
773 |
||
774 |
inputLen -= inputConsumed; |
|
775 |
inputOffset += inputConsumed; |
|
776 |
outputOffset += len; |
|
777 |
buffered -= bufferedConsumed; |
|
778 |
if (buffered > 0) { |
|
779 |
System.arraycopy(buffer, bufferedConsumed, buffer, 0, |
|
780 |
buffered); |
|
781 |
} |
|
782 |
} |
|
783 |
// left over again |
|
784 |
if (inputLen > 0) { |
|
785 |
System.arraycopy(input, inputOffset, buffer, buffered, |
|
786 |
inputLen); |
|
787 |
} |
|
788 |
buffered += inputLen; |
|
789 |
return len; |
|
790 |
} |
|
791 |
||
792 |
/** |
|
793 |
* Encrypts or decrypts data in a single-part operation, |
|
794 |
* or finishes a multiple-part operation. |
|
795 |
* The data is encrypted or decrypted, depending on how this cipher was |
|
796 |
* initialized. |
|
797 |
* |
|
798 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
799 |
* buffer, starting at <code>inputOffset</code>, and any input bytes that |
|
800 |
* may have been buffered during a previous <code>update</code> operation, |
|
801 |
* are processed, with padding (if requested) being applied. |
|
802 |
* The result is stored in a new buffer. |
|
803 |
* |
|
804 |
* <p>The cipher is reset to its initial state (uninitialized) after this |
|
805 |
* call. |
|
806 |
* |
|
807 |
* @param input the input buffer |
|
808 |
* @param inputOffset the offset in <code>input</code> where the input |
|
809 |
* starts |
|
810 |
* @param inputLen the input length |
|
811 |
* |
|
812 |
* @return the new buffer with the result |
|
813 |
* |
|
814 |
* @exception IllegalBlockSizeException if this cipher is a block cipher, |
|
815 |
* no padding has been requested (only in encryption mode), and the total |
|
816 |
* input length of the data processed by this cipher is not a multiple of |
|
817 |
* block size |
|
818 |
* @exception BadPaddingException if this cipher is in decryption mode, |
|
819 |
* and (un)padding has been requested, but the decrypted data is not |
|
820 |
* bounded by the appropriate padding bytes |
|
821 |
*/ |
|
822 |
byte[] doFinal(byte[] input, int inputOffset, int inputLen) |
|
823 |
throws IllegalBlockSizeException, BadPaddingException { |
|
824 |
byte[] output = null; |
|
825 |
try { |
|
15008 | 826 |
output = new byte[getOutputSizeByOperation(inputLen, true)]; |
2 | 827 |
int len = doFinal(input, inputOffset, inputLen, output, 0); |
828 |
if (len < output.length) { |
|
15008 | 829 |
return Arrays.copyOf(output, len); |
2 | 830 |
} else { |
15008 | 831 |
return output; |
2 | 832 |
} |
833 |
} catch (ShortBufferException e) { |
|
834 |
// never thrown |
|
15008 | 835 |
throw new ProviderException("Unexpected exception", e); |
2 | 836 |
} |
837 |
} |
|
838 |
||
839 |
/** |
|
840 |
* Encrypts or decrypts data in a single-part operation, |
|
841 |
* or finishes a multiple-part operation. |
|
842 |
* The data is encrypted or decrypted, depending on how this cipher was |
|
843 |
* initialized. |
|
844 |
* |
|
845 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
846 |
* buffer, starting at <code>inputOffset</code>, and any input bytes that |
|
847 |
* may have been buffered during a previous <code>update</code> operation, |
|
848 |
* are processed, with padding (if requested) being applied. |
|
849 |
* The result is stored in the <code>output</code> buffer, starting at |
|
850 |
* <code>outputOffset</code>. |
|
851 |
* |
|
852 |
* <p>The cipher is reset to its initial state (uninitialized) after this |
|
853 |
* call. |
|
854 |
* |
|
855 |
* @param input the input buffer |
|
856 |
* @param inputOffset the offset in <code>input</code> where the input |
|
857 |
* starts |
|
858 |
* @param inputLen the input length |
|
859 |
* @param output the buffer for the result |
|
860 |
* @param outputOffset the offset in <code>output</code> where the result |
|
861 |
* is stored |
|
862 |
* |
|
863 |
* @return the number of bytes stored in <code>output</code> |
|
864 |
* |
|
865 |
* @exception IllegalBlockSizeException if this cipher is a block cipher, |
|
866 |
* no padding has been requested (only in encryption mode), and the total |
|
867 |
* input length of the data processed by this cipher is not a multiple of |
|
868 |
* block size |
|
869 |
* @exception ShortBufferException if the given output buffer is too small |
|
870 |
* to hold the result |
|
871 |
* @exception BadPaddingException if this cipher is in decryption mode, |
|
872 |
* and (un)padding has been requested, but the decrypted data is not |
|
873 |
* bounded by the appropriate padding bytes |
|
874 |
*/ |
|
875 |
int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, |
|
876 |
int outputOffset) |
|
877 |
throws IllegalBlockSizeException, ShortBufferException, |
|
878 |
BadPaddingException { |
|
15008 | 879 |
if (requireReinit) { |
880 |
throw new IllegalStateException |
|
881 |
("Must use either different key or iv for GCM encryption"); |
|
882 |
} |
|
2 | 883 |
|
884 |
// calculate the total input length |
|
885 |
int totalLen = buffered + inputLen; |
|
886 |
int paddedLen = totalLen; |
|
887 |
int paddingLen = 0; |
|
888 |
||
889 |
// will the total input length be a multiple of blockSize? |
|
890 |
if (unitBytes != blockSize) { |
|
891 |
if (totalLen < diffBlocksize) { |
|
892 |
paddingLen = diffBlocksize - totalLen; |
|
893 |
} else { |
|
894 |
paddingLen = blockSize - |
|
895 |
((totalLen - diffBlocksize) % blockSize); |
|
896 |
} |
|
897 |
} else if (padding != null) { |
|
898 |
paddingLen = padding.padLength(totalLen); |
|
899 |
} |
|
900 |
||
901 |
if ((paddingLen > 0) && (paddingLen != blockSize) && |
|
902 |
(padding != null) && decrypting) { |
|
903 |
throw new IllegalBlockSizeException |
|
904 |
("Input length must be multiple of " + blockSize + |
|
905 |
" when decrypting with padded cipher"); |
|
906 |
} |
|
907 |
||
908 |
// if encrypting and padding not null, add padding |
|
15008 | 909 |
if (!decrypting && padding != null) { |
2 | 910 |
paddedLen += paddingLen; |
15008 | 911 |
} |
2 | 912 |
|
913 |
// check output buffer capacity. |
|
914 |
// if we are decrypting with padding applied, we can perform this |
|
915 |
// check only after we have determined how many padding bytes there |
|
916 |
// are. |
|
917 |
if (output == null) { |
|
918 |
throw new ShortBufferException("Output buffer is null"); |
|
919 |
} |
|
920 |
int outputCapacity = output.length - outputOffset; |
|
15008 | 921 |
|
922 |
if (((!decrypting) && (outputCapacity < paddedLen)) || |
|
2 | 923 |
(decrypting && (outputCapacity < (paddedLen - blockSize)))) { |
924 |
throw new ShortBufferException("Output buffer too short: " |
|
925 |
+ outputCapacity + " bytes given, " |
|
926 |
+ paddedLen + " bytes needed"); |
|
927 |
} |
|
928 |
||
929 |
// prepare the final input avoiding copying if possible |
|
930 |
byte[] finalBuf = input; |
|
931 |
int finalOffset = inputOffset; |
|
932 |
if ((buffered != 0) || (!decrypting && padding != null)) { |
|
933 |
finalOffset = 0; |
|
934 |
finalBuf = new byte[paddedLen]; |
|
935 |
if (buffered != 0) { |
|
936 |
System.arraycopy(buffer, 0, finalBuf, 0, buffered); |
|
937 |
} |
|
938 |
if (inputLen != 0) { |
|
939 |
System.arraycopy(input, inputOffset, finalBuf, |
|
940 |
buffered, inputLen); |
|
941 |
} |
|
942 |
if (!decrypting && padding != null) { |
|
943 |
padding.padWithLen(finalBuf, totalLen, paddingLen); |
|
944 |
} |
|
945 |
} |
|
946 |
||
947 |
if (decrypting) { |
|
948 |
// if the size of specified output buffer is less than |
|
949 |
// the length of the cipher text, then the current |
|
950 |
// content of cipher has to be preserved in order for |
|
951 |
// users to retry the call with a larger buffer in the |
|
952 |
// case of ShortBufferException. |
|
953 |
if (outputCapacity < paddedLen) { |
|
954 |
cipher.save(); |
|
955 |
} |
|
956 |
// create temporary output buffer so that only "real" |
|
957 |
// data bytes are passed to user's output buffer. |
|
958 |
byte[] outWithPadding = new byte[totalLen]; |
|
959 |
totalLen = finalNoPadding(finalBuf, finalOffset, outWithPadding, |
|
960 |
0, totalLen); |
|
961 |
||
962 |
if (padding != null) { |
|
963 |
int padStart = padding.unpad(outWithPadding, 0, totalLen); |
|
964 |
if (padStart < 0) { |
|
965 |
throw new BadPaddingException("Given final block not " |
|
966 |
+ "properly padded"); |
|
967 |
} |
|
968 |
totalLen = padStart; |
|
969 |
} |
|
15008 | 970 |
|
2 | 971 |
if ((output.length - outputOffset) < totalLen) { |
972 |
// restore so users can retry with a larger buffer |
|
973 |
cipher.restore(); |
|
974 |
throw new ShortBufferException("Output buffer too short: " |
|
975 |
+ (output.length-outputOffset) |
|
976 |
+ " bytes given, " + totalLen |
|
977 |
+ " bytes needed"); |
|
978 |
} |
|
979 |
for (int i = 0; i < totalLen; i++) { |
|
980 |
output[outputOffset + i] = outWithPadding[i]; |
|
981 |
} |
|
982 |
} else { // encrypting |
|
15008 | 983 |
try { |
984 |
totalLen = finalNoPadding(finalBuf, finalOffset, output, |
|
985 |
outputOffset, paddedLen); |
|
986 |
} finally { |
|
987 |
// reset after doFinal() for GCM encryption |
|
988 |
requireReinit = (cipherMode == GCM_MODE); |
|
989 |
} |
|
2 | 990 |
} |
991 |
||
992 |
buffered = 0; |
|
993 |
diffBlocksize = blockSize; |
|
994 |
if (cipherMode != ECB_MODE) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
995 |
cipher.reset(); |
2 | 996 |
} |
997 |
return totalLen; |
|
998 |
} |
|
999 |
||
15008 | 1000 |
private int finalNoPadding(byte[] in, int inOfs, byte[] out, int outOfs, |
2 | 1001 |
int len) |
15008 | 1002 |
throws IllegalBlockSizeException, AEADBadTagException { |
2 | 1003 |
|
15008 | 1004 |
if ((cipherMode != GCM_MODE) && (in == null || len == 0)) { |
1005 |
return 0; |
|
2 | 1006 |
} |
15008 | 1007 |
if ((cipherMode != CFB_MODE) && (cipherMode != OFB_MODE) && |
1008 |
(cipherMode != GCM_MODE) && |
|
1009 |
((len % unitBytes) != 0) && (cipherMode != CTS_MODE)) { |
|
1010 |
if (padding != null) { |
|
1011 |
throw new IllegalBlockSizeException |
|
1012 |
("Input length (with padding) not multiple of " + |
|
1013 |
unitBytes + " bytes"); |
|
1014 |
} else { |
|
1015 |
throw new IllegalBlockSizeException |
|
1016 |
("Input length not multiple of " + unitBytes |
|
1017 |
+ " bytes"); |
|
1018 |
} |
|
1019 |
} |
|
1020 |
int outLen = 0; |
|
2 | 1021 |
if (decrypting) { |
15008 | 1022 |
outLen = cipher.decryptFinal(in, inOfs, len, out, outOfs); |
2 | 1023 |
} else { |
15008 | 1024 |
outLen = cipher.encryptFinal(in, inOfs, len, out, outOfs); |
2 | 1025 |
} |
15008 | 1026 |
return outLen; |
2 | 1027 |
} |
1028 |
||
1029 |
// Note: Wrap() and Unwrap() are the same in |
|
1030 |
// each of SunJCE CipherSpi implementation classes. |
|
1031 |
// They are duplicated due to export control requirements: |
|
1032 |
// All CipherSpi implementation must be final. |
|
1033 |
/** |
|
1034 |
* Wrap a key. |
|
1035 |
* |
|
1036 |
* @param key the key to be wrapped. |
|
1037 |
* |
|
1038 |
* @return the wrapped key. |
|
1039 |
* |
|
1040 |
* @exception IllegalBlockSizeException if this cipher is a block |
|
1041 |
* cipher, no padding has been requested, and the length of the |
|
1042 |
* encoding of the key to be wrapped is not a |
|
1043 |
* multiple of the block size. |
|
1044 |
* |
|
1045 |
* @exception InvalidKeyException if it is impossible or unsafe to |
|
1046 |
* wrap the key with this cipher (e.g., a hardware protected key is |
|
1047 |
* being passed to a software only cipher). |
|
1048 |
*/ |
|
1049 |
byte[] wrap(Key key) |
|
1050 |
throws IllegalBlockSizeException, InvalidKeyException { |
|
1051 |
byte[] result = null; |
|
1052 |
||
1053 |
try { |
|
1054 |
byte[] encodedKey = key.getEncoded(); |
|
1055 |
if ((encodedKey == null) || (encodedKey.length == 0)) { |
|
1056 |
throw new InvalidKeyException("Cannot get an encoding of " + |
|
1057 |
"the key to be wrapped"); |
|
1058 |
} |
|
1059 |
result = doFinal(encodedKey, 0, encodedKey.length); |
|
1060 |
} catch (BadPaddingException e) { |
|
1061 |
// Should never happen |
|
1062 |
} |
|
1063 |
return result; |
|
1064 |
} |
|
1065 |
||
1066 |
/** |
|
1067 |
* Unwrap a previously wrapped key. |
|
1068 |
* |
|
1069 |
* @param wrappedKey the key to be unwrapped. |
|
1070 |
* |
|
1071 |
* @param wrappedKeyAlgorithm the algorithm the wrapped key is for. |
|
1072 |
* |
|
1073 |
* @param wrappedKeyType the type of the wrapped key. |
|
1074 |
* This is one of <code>Cipher.SECRET_KEY</code>, |
|
1075 |
* <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>. |
|
1076 |
* |
|
1077 |
* @return the unwrapped key. |
|
1078 |
* |
|
1079 |
* @exception NoSuchAlgorithmException if no installed providers |
|
1080 |
* can create keys of type <code>wrappedKeyType</code> for the |
|
1081 |
* <code>wrappedKeyAlgorithm</code>. |
|
1082 |
* |
|
1083 |
* @exception InvalidKeyException if <code>wrappedKey</code> does not |
|
1084 |
* represent a wrapped key of type <code>wrappedKeyType</code> for |
|
1085 |
* the <code>wrappedKeyAlgorithm</code>. |
|
1086 |
*/ |
|
1087 |
Key unwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, |
|
1088 |
int wrappedKeyType) |
|
1089 |
throws InvalidKeyException, NoSuchAlgorithmException { |
|
1090 |
byte[] encodedKey; |
|
1091 |
try { |
|
1092 |
encodedKey = doFinal(wrappedKey, 0, wrappedKey.length); |
|
1093 |
} catch (BadPaddingException ePadding) { |
|
1094 |
throw new InvalidKeyException("The wrapped key is not padded " + |
|
1095 |
"correctly"); |
|
1096 |
} catch (IllegalBlockSizeException eBlockSize) { |
|
1097 |
throw new InvalidKeyException("The wrapped key does not have " + |
|
1098 |
"the correct length"); |
|
1099 |
} |
|
1100 |
return ConstructKeys.constructKey(encodedKey, wrappedKeyAlgorithm, |
|
1101 |
wrappedKeyType); |
|
1102 |
} |
|
15008 | 1103 |
|
1104 |
/** |
|
1105 |
* Continues a multi-part update of the Additional Authentication |
|
1106 |
* Data (AAD), using a subset of the provided buffer. |
|
1107 |
* <p> |
|
1108 |
* Calls to this method provide AAD to the cipher when operating in |
|
1109 |
* modes such as AEAD (GCM/CCM). If this cipher is operating in |
|
1110 |
* either GCM or CCM mode, all AAD must be supplied before beginning |
|
1111 |
* operations on the ciphertext (via the {@code update} and {@code |
|
1112 |
* doFinal} methods). |
|
1113 |
* |
|
1114 |
* @param src the buffer containing the AAD |
|
1115 |
* @param offset the offset in {@code src} where the AAD input starts |
|
1116 |
* @param len the number of AAD bytes |
|
1117 |
* |
|
1118 |
* @throws IllegalStateException if this cipher is in a wrong state |
|
1119 |
* (e.g., has not been initialized), does not accept AAD, or if |
|
1120 |
* operating in either GCM or CCM mode and one of the {@code update} |
|
1121 |
* methods has already been called for the active |
|
1122 |
* encryption/decryption operation |
|
1123 |
* @throws UnsupportedOperationException if this method |
|
1124 |
* has not been overridden by an implementation |
|
1125 |
* |
|
1126 |
* @since 1.8 |
|
1127 |
*/ |
|
1128 |
void updateAAD(byte[] src, int offset, int len) { |
|
1129 |
if (requireReinit) { |
|
1130 |
throw new IllegalStateException |
|
1131 |
("Must use either different key or iv for GCM encryption"); |
|
1132 |
} |
|
1133 |
cipher.updateAAD(src, offset, len); |
|
1134 |
} |
|
2 | 1135 |
} |