author | iris |
Wed, 20 Jan 2016 11:02:36 -0800 | |
changeset 35302 | e4d2275861c3 |
parent 35287 | e59d934ce2ba |
child 39750 | 982b75e31495 |
permissions | -rw-r--r-- |
27182 | 1 |
/* |
30915
504f95d17f58
8081792: buffer size calculation issue in NativeGCMCipher
xuelei
parents:
27182
diff
changeset
|
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
27182 | 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.oracle.security.ucrypto; |
|
27 |
||
28 |
import java.io.ByteArrayOutputStream; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
||
31 |
import java.util.Set; |
|
32 |
import java.util.Arrays; |
|
33 |
import java.security.*; |
|
34 |
import java.security.spec.*; |
|
35 |
import javax.crypto.*; |
|
36 |
import javax.crypto.spec.SecretKeySpec; |
|
37 |
import javax.crypto.spec.GCMParameterSpec; |
|
38 |
||
35287 | 39 |
import sun.security.jca.JCAUtil; |
40 |
||
27182 | 41 |
/** |
42 |
* Cipher wrapper class utilizing ucrypto APIs. This class currently supports |
|
43 |
* - AES/GCM/NoPADDING |
|
44 |
* |
|
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
35287
diff
changeset
|
45 |
* @since 9 |
27182 | 46 |
*/ |
47 |
class NativeGCMCipher extends NativeCipher { |
|
48 |
||
49 |
public static final class AesGcmNoPadding extends NativeGCMCipher { |
|
50 |
public AesGcmNoPadding() throws NoSuchAlgorithmException { |
|
51 |
super(-1); |
|
52 |
} |
|
31270
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
30915
diff
changeset
|
53 |
public AesGcmNoPadding(int keySize) throws NoSuchAlgorithmException { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
30915
diff
changeset
|
54 |
super(keySize); |
27182 | 55 |
} |
56 |
} |
|
57 |
||
58 |
private static final int DEFAULT_TAG_LEN = 128; // same as SunJCE provider |
|
59 |
||
60 |
// buffer for storing AAD data; if null, meaning buffer content has been |
|
61 |
// supplied to native context |
|
62 |
private ByteArrayOutputStream aadBuffer = new ByteArrayOutputStream(); |
|
63 |
||
64 |
// buffer for storing input in decryption, not used for encryption |
|
65 |
private ByteArrayOutputStream ibuffer = null; |
|
66 |
||
67 |
private int tagLen = DEFAULT_TAG_LEN; |
|
68 |
||
69 |
/* |
|
70 |
* variables used for performing the GCM (key+iv) uniqueness check. |
|
71 |
* To use GCM mode safely, the cipher object must be re-initialized |
|
72 |
* with a different combination of key + iv values for each |
|
73 |
* ENCRYPTION operation. However, checking all past key + iv values |
|
74 |
* isn't feasible. Thus, we only do a per-instance check of the |
|
75 |
* key + iv values used in previous encryption. |
|
76 |
* For decryption operations, no checking is necessary. |
|
77 |
*/ |
|
78 |
private boolean requireReinit = false; |
|
79 |
private byte[] lastEncKey = null; |
|
80 |
private byte[] lastEncIv = null; |
|
81 |
||
82 |
NativeGCMCipher(int fixedKeySize) throws NoSuchAlgorithmException { |
|
83 |
super(UcryptoMech.CRYPTO_AES_GCM, fixedKeySize); |
|
84 |
} |
|
85 |
||
86 |
@Override |
|
87 |
protected void ensureInitialized() { |
|
88 |
if (!initialized) { |
|
89 |
if (aadBuffer != null && aadBuffer.size() > 0) { |
|
90 |
init(encrypt, keyValue, iv, tagLen, aadBuffer.toByteArray()); |
|
91 |
aadBuffer = null; |
|
92 |
} else { |
|
93 |
init(encrypt, keyValue, iv, tagLen, null); |
|
94 |
} |
|
95 |
if (!initialized) { |
|
96 |
throw new UcryptoException("Cannot initialize Cipher"); |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
@Override |
|
102 |
protected int getOutputSizeByOperation(int inLen, boolean isDoFinal) { |
|
103 |
if (inLen < 0) return 0; |
|
104 |
||
105 |
if (!isDoFinal && (inLen == 0)) { |
|
106 |
return 0; |
|
107 |
} |
|
108 |
||
109 |
int result = inLen + bytesBuffered; |
|
110 |
if (encrypt) { |
|
111 |
if (isDoFinal) { |
|
112 |
result += tagLen/8; |
|
113 |
} |
|
114 |
} else { |
|
115 |
if (ibuffer != null) { |
|
116 |
result += ibuffer.size(); |
|
117 |
} |
|
30915
504f95d17f58
8081792: buffer size calculation issue in NativeGCMCipher
xuelei
parents:
27182
diff
changeset
|
118 |
result -= tagLen/8; |
27182 | 119 |
} |
120 |
if (result < 0) { |
|
121 |
result = 0; |
|
122 |
} |
|
123 |
return result; |
|
124 |
} |
|
125 |
||
126 |
@Override |
|
127 |
protected void reset(boolean doCancel) { |
|
128 |
super.reset(doCancel); |
|
129 |
if (aadBuffer == null) { |
|
130 |
aadBuffer = new ByteArrayOutputStream(); |
|
131 |
} else { |
|
132 |
aadBuffer.reset(); |
|
133 |
} |
|
134 |
||
135 |
if (ibuffer != null) { |
|
136 |
ibuffer.reset(); |
|
137 |
} |
|
138 |
if (!encrypt) requireReinit = false; |
|
139 |
} |
|
140 |
||
141 |
// actual init() implementation - caller should clone key and iv if needed |
|
142 |
protected void init(boolean encrypt, byte[] keyVal, byte[] ivVal, int tLen, byte[] aad) { |
|
143 |
reset(true); |
|
144 |
this.encrypt = encrypt; |
|
145 |
this.keyValue = keyVal; |
|
146 |
this.iv = ivVal; |
|
147 |
long pCtxtVal = NativeCipher.nativeInit(mech.value(), encrypt, keyValue, iv, |
|
148 |
tLen, aad); |
|
149 |
initialized = (pCtxtVal != 0L); |
|
150 |
if (initialized) { |
|
151 |
pCtxt = new CipherContextRef(this, pCtxtVal, encrypt); |
|
152 |
} else { |
|
153 |
throw new UcryptoException("Cannot initialize Cipher"); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
// see JCE spec |
|
158 |
@Override |
|
159 |
protected synchronized AlgorithmParameters engineGetParameters() { |
|
160 |
AlgorithmParameters params = null; |
|
161 |
try { |
|
162 |
if (iv != null) { |
|
163 |
GCMParameterSpec gcmSpec = new GCMParameterSpec(tagLen, iv.clone()); |
|
164 |
params = AlgorithmParameters.getInstance("GCM"); |
|
165 |
params.init(gcmSpec); |
|
166 |
} |
|
167 |
} catch (GeneralSecurityException e) { |
|
168 |
// NoSuchAlgorithmException, NoSuchProviderException |
|
169 |
// InvalidParameterSpecException |
|
170 |
throw new UcryptoException("Could not encode parameters", e); |
|
171 |
} |
|
172 |
return params; |
|
173 |
} |
|
174 |
||
175 |
// see JCE spec |
|
176 |
@Override |
|
177 |
protected synchronized void engineInit(int opmode, Key key, |
|
178 |
AlgorithmParameterSpec params, SecureRandom random) |
|
179 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
180 |
checkKey(key); |
|
181 |
if (opmode != Cipher.ENCRYPT_MODE && |
|
182 |
opmode != Cipher.DECRYPT_MODE && |
|
183 |
opmode != Cipher.WRAP_MODE && |
|
184 |
opmode != Cipher.UNWRAP_MODE) { |
|
185 |
throw new InvalidAlgorithmParameterException |
|
186 |
("Unsupported mode: " + opmode); |
|
187 |
} |
|
188 |
boolean doEncrypt = (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE); |
|
189 |
byte[] keyBytes = key.getEncoded().clone(); |
|
190 |
byte[] ivBytes = null; |
|
191 |
if (params != null) { |
|
192 |
if (!(params instanceof GCMParameterSpec)) { |
|
32646
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
193 |
throw new InvalidAlgorithmParameterException("GCMParameterSpec required." + |
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
194 |
" Received: " + params.getClass().getName()); |
27182 | 195 |
} else { |
196 |
tagLen = ((GCMParameterSpec) params).getTLen(); |
|
197 |
ivBytes = ((GCMParameterSpec) params).getIV(); |
|
198 |
} |
|
199 |
} else { |
|
200 |
if (doEncrypt) { |
|
201 |
tagLen = DEFAULT_TAG_LEN; |
|
202 |
||
203 |
// generate IV if none supplied for encryption |
|
204 |
ivBytes = new byte[blockSize]; |
|
35287 | 205 |
if (random == null) { |
206 |
random = JCAUtil.getSecureRandom(); |
|
207 |
} |
|
208 |
random.nextBytes(ivBytes); |
|
27182 | 209 |
} else { |
210 |
throw new InvalidAlgorithmParameterException("Parameters required for decryption"); |
|
211 |
} |
|
212 |
} |
|
213 |
if (doEncrypt) { |
|
214 |
requireReinit = Arrays.equals(ivBytes, lastEncIv) && |
|
31695 | 215 |
MessageDigest.isEqual(keyBytes, lastEncKey); |
27182 | 216 |
if (requireReinit) { |
217 |
throw new InvalidAlgorithmParameterException |
|
218 |
("Cannot reuse iv for GCM encryption"); |
|
219 |
} |
|
220 |
lastEncIv = ivBytes; |
|
221 |
lastEncKey = keyBytes; |
|
222 |
} else { |
|
223 |
requireReinit = false; |
|
224 |
ibuffer = new ByteArrayOutputStream(); |
|
225 |
} |
|
226 |
init(doEncrypt, keyBytes, ivBytes, tagLen, null); |
|
227 |
} |
|
228 |
||
229 |
// see JCE spec |
|
230 |
@Override |
|
231 |
protected synchronized void engineInit(int opmode, Key key, AlgorithmParameters params, |
|
232 |
SecureRandom random) |
|
233 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
234 |
AlgorithmParameterSpec spec = null; |
|
235 |
if (params != null) { |
|
236 |
try { |
|
237 |
// mech must be UcryptoMech.CRYPTO_AES_GCM |
|
238 |
spec = params.getParameterSpec(GCMParameterSpec.class); |
|
239 |
} catch (InvalidParameterSpecException iaps) { |
|
240 |
throw new InvalidAlgorithmParameterException(iaps); |
|
241 |
} |
|
242 |
} |
|
243 |
engineInit(opmode, key, spec, random); |
|
244 |
} |
|
245 |
||
246 |
// see JCE spec |
|
247 |
@Override |
|
248 |
protected synchronized byte[] engineUpdate(byte[] in, int inOfs, int inLen) { |
|
249 |
if (aadBuffer != null && aadBuffer.size() > 0) { |
|
250 |
// init again with AAD data |
|
251 |
init(encrypt, keyValue, iv, tagLen, aadBuffer.toByteArray()); |
|
252 |
aadBuffer = null; |
|
253 |
} |
|
254 |
if (requireReinit) { |
|
255 |
throw new IllegalStateException |
|
256 |
("Must use either different key or iv for GCM encryption"); |
|
257 |
} |
|
258 |
if (inLen > 0) { |
|
259 |
if (!encrypt) { |
|
260 |
ibuffer.write(in, inOfs, inLen); |
|
261 |
return null; |
|
262 |
} |
|
263 |
return super.engineUpdate(in, inOfs, inLen); |
|
264 |
} else return null; |
|
265 |
} |
|
266 |
||
267 |
// see JCE spec |
|
268 |
@Override |
|
269 |
protected synchronized int engineUpdate(byte[] in, int inOfs, int inLen, byte[] out, |
|
270 |
int outOfs) throws ShortBufferException { |
|
271 |
int len = getOutputSizeByOperation(inLen, false); |
|
272 |
if (out.length - outOfs < len) { |
|
32646
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
273 |
throw new ShortBufferException("Output buffer must be " + |
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
274 |
"(at least) " + len + " bytes long. Got: " + |
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
275 |
(out.length - outOfs)); |
27182 | 276 |
} |
277 |
if (aadBuffer != null && aadBuffer.size() > 0) { |
|
278 |
// init again with AAD data |
|
279 |
init(encrypt, keyValue, iv, tagLen, aadBuffer.toByteArray()); |
|
280 |
aadBuffer = null; |
|
281 |
} |
|
282 |
if (requireReinit) { |
|
283 |
throw new IllegalStateException |
|
284 |
("Must use either different key or iv for GCM encryption"); |
|
285 |
} |
|
286 |
if (inLen > 0) { |
|
287 |
if (!encrypt) { |
|
288 |
ibuffer.write(in, inOfs, inLen); |
|
289 |
return 0; |
|
290 |
} else { |
|
291 |
return super.engineUpdate(in, inOfs, inLen, out, outOfs); |
|
292 |
} |
|
293 |
} |
|
294 |
return 0; |
|
295 |
} |
|
296 |
||
297 |
// see JCE spec |
|
298 |
@Override |
|
299 |
protected synchronized void engineUpdateAAD(byte[] src, int srcOfs, int srcLen) |
|
300 |
throws IllegalStateException { |
|
301 |
||
302 |
if ((src == null) || (srcOfs < 0) || (srcOfs + srcLen > src.length)) { |
|
303 |
throw new IllegalArgumentException("Invalid AAD"); |
|
304 |
} |
|
305 |
if (keyValue == null) { |
|
306 |
throw new IllegalStateException("Need to initialize Cipher first"); |
|
307 |
} |
|
308 |
if (requireReinit) { |
|
309 |
throw new IllegalStateException |
|
310 |
("Must use either different key or iv for GCM encryption"); |
|
311 |
} |
|
312 |
if (aadBuffer != null) { |
|
313 |
aadBuffer.write(src, srcOfs, srcLen); |
|
314 |
} else { |
|
315 |
// update has already been called |
|
316 |
throw new IllegalStateException |
|
317 |
("Update has been called; no more AAD data"); |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
// see JCE spec |
|
322 |
@Override |
|
323 |
protected void engineUpdateAAD(ByteBuffer src) |
|
324 |
throws IllegalStateException { |
|
325 |
if (src == null) { |
|
326 |
throw new IllegalArgumentException("Invalid AAD"); |
|
327 |
} |
|
328 |
if (keyValue == null) { |
|
329 |
throw new IllegalStateException("Need to initialize Cipher first"); |
|
330 |
} |
|
331 |
if (requireReinit) { |
|
332 |
throw new IllegalStateException |
|
333 |
("Must use either different key or iv for GCM encryption"); |
|
334 |
} |
|
335 |
if (aadBuffer != null) { |
|
336 |
if (src.hasRemaining()) { |
|
337 |
byte[] srcBytes = new byte[src.remaining()]; |
|
338 |
src.get(srcBytes); |
|
339 |
aadBuffer.write(srcBytes, 0, srcBytes.length); |
|
340 |
} |
|
341 |
} else { |
|
342 |
// update has already been called |
|
343 |
throw new IllegalStateException |
|
344 |
("Update has been called; no more AAD data"); |
|
345 |
} |
|
346 |
} |
|
347 |
||
348 |
// see JCE spec |
|
349 |
@Override |
|
350 |
protected synchronized byte[] engineDoFinal(byte[] in, int inOfs, int inLen) |
|
351 |
throws IllegalBlockSizeException, BadPaddingException { |
|
352 |
byte[] out = new byte[getOutputSizeByOperation(inLen, true)]; |
|
353 |
try { |
|
354 |
// delegate to the other engineDoFinal(...) method |
|
355 |
int k = engineDoFinal(in, inOfs, inLen, out, 0); |
|
356 |
if (out.length != k) { |
|
357 |
out = Arrays.copyOf(out, k); |
|
358 |
} |
|
359 |
return out; |
|
360 |
} catch (ShortBufferException e) { |
|
361 |
throw new UcryptoException("Internal Error", e); |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
// see JCE spec |
|
366 |
@Override |
|
367 |
protected synchronized int engineDoFinal(byte[] in, int inOfs, int inLen, |
|
368 |
byte[] out, int outOfs) |
|
369 |
throws ShortBufferException, IllegalBlockSizeException, |
|
370 |
BadPaddingException { |
|
371 |
int len = getOutputSizeByOperation(inLen, true); |
|
372 |
if (out.length - outOfs < len) { |
|
373 |
throw new ShortBufferException("Output buffer must be " |
|
32646
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
374 |
+ "(at least) " + len + " bytes long. Got: " + |
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
375 |
(out.length - outOfs)); |
27182 | 376 |
} |
377 |
if (aadBuffer != null && aadBuffer.size() > 0) { |
|
378 |
// init again with AAD data |
|
379 |
init(encrypt, keyValue, iv, tagLen, aadBuffer.toByteArray()); |
|
380 |
aadBuffer = null; |
|
381 |
} |
|
382 |
if (requireReinit) { |
|
383 |
throw new IllegalStateException |
|
384 |
("Must use either different key or iv for GCM encryption"); |
|
385 |
} |
|
386 |
if (!encrypt) { |
|
387 |
if (inLen > 0) { |
|
388 |
ibuffer.write(in, inOfs, inLen); |
|
389 |
} |
|
390 |
inLen = ibuffer.size(); |
|
391 |
if (inLen < tagLen/8) { |
|
392 |
// Otherwise, Solaris lib will error out w/ CRYPTO_BUFFER_TOO_SMALL |
|
393 |
// when ucrypto_decrypt_final() is called |
|
32646
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
394 |
throw new AEADBadTagException("Input too short - need tag." + |
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
31695
diff
changeset
|
395 |
" inLen: " + inLen + ". tagLen: " + tagLen); |
27182 | 396 |
} |
397 |
// refresh 'in' to all buffered-up bytes |
|
398 |
in = ibuffer.toByteArray(); |
|
399 |
inOfs = 0; |
|
400 |
ibuffer.reset(); |
|
401 |
} |
|
402 |
try { |
|
403 |
return super.engineDoFinal(in, inOfs, inLen, out, outOfs); |
|
404 |
} catch (UcryptoException ue) { |
|
405 |
if (ue.getMessage().equals("CRYPTO_INVALID_MAC")) { |
|
406 |
throw new AEADBadTagException("Tag does not match"); |
|
407 |
} else { |
|
408 |
// pass it up |
|
409 |
throw ue; |
|
410 |
} |
|
411 |
} finally { |
|
412 |
requireReinit = encrypt; |
|
413 |
} |
|
414 |
} |
|
415 |
} |