author | weijun |
Thu, 12 May 2016 13:06:03 +0800 | |
changeset 37896 | cd841af7dcd0 |
parent 37895 | f59fdd7fb4fb |
child 38462 | e3d8ddb3512c |
permissions | -rw-r--r-- |
37796 | 1 |
/* |
2 |
* Copyright (c) 2016, 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 sun.security.provider; |
|
27 |
||
28 |
import javax.crypto.Cipher; |
|
29 |
import javax.crypto.NoSuchPaddingException; |
|
30 |
import javax.crypto.spec.SecretKeySpec; |
|
31 |
import java.io.IOException; |
|
32 |
import java.security.*; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.Locale; |
|
35 |
||
36 |
public class CtrDrbg extends AbstractDrbg { |
|
37 |
||
38 |
private static final long serialVersionUID = 9L; |
|
39 |
private static final int AES_LIMIT; |
|
40 |
||
41 |
static { |
|
42 |
try { |
|
43 |
AES_LIMIT = Cipher.getMaxAllowedKeyLength("AES"); |
|
44 |
} catch (Exception e) { |
|
45 |
// should not happen |
|
46 |
throw new AssertionError("Cannot detect AES", e); |
|
47 |
} |
|
48 |
} |
|
49 |
||
50 |
private transient Cipher cipher; |
|
51 |
||
52 |
private String cipherAlg; |
|
53 |
private String keyAlg; |
|
54 |
||
55 |
private int ctrLen; |
|
56 |
private int blockLen; |
|
57 |
private int keyLen; |
|
58 |
private int seedLen; |
|
59 |
||
60 |
private transient byte[] v; |
|
61 |
private transient byte[] k; |
|
62 |
||
63 |
public CtrDrbg(SecureRandomParameters params) { |
|
64 |
mechName = "CTR_DRBG"; |
|
65 |
configure(params); |
|
66 |
} |
|
67 |
||
68 |
private static int alg2strength(String algorithm) { |
|
69 |
switch (algorithm.toUpperCase(Locale.ROOT)) { |
|
70 |
case "AES-128": |
|
71 |
return 128; |
|
72 |
case "AES-192": |
|
73 |
return 192; |
|
74 |
case "AES-256": |
|
75 |
return 256; |
|
76 |
default: |
|
77 |
throw new IllegalArgumentException(algorithm + |
|
78 |
" not supported in CTR_DBRG"); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
@Override |
|
83 |
protected void chooseAlgorithmAndStrength() { |
|
84 |
if (requestedAlgorithm != null) { |
|
85 |
algorithm = requestedAlgorithm.toUpperCase(); |
|
86 |
int supportedStrength = alg2strength(algorithm); |
|
87 |
if (requestedInstantiationSecurityStrength >= 0) { |
|
88 |
int tryStrength = getStandardStrength( |
|
89 |
requestedInstantiationSecurityStrength); |
|
90 |
if (tryStrength > supportedStrength) { |
|
91 |
throw new IllegalArgumentException(algorithm + |
|
92 |
" does not support strength " + |
|
93 |
requestedInstantiationSecurityStrength); |
|
94 |
} |
|
95 |
this.securityStrength = tryStrength; |
|
96 |
} else { |
|
97 |
this.securityStrength = (DEFAULT_STRENGTH > supportedStrength) ? |
|
98 |
supportedStrength : DEFAULT_STRENGTH; |
|
99 |
} |
|
100 |
} else { |
|
101 |
int tryStrength = (requestedInstantiationSecurityStrength < 0) ? |
|
102 |
DEFAULT_STRENGTH : requestedInstantiationSecurityStrength; |
|
103 |
tryStrength = getStandardStrength(tryStrength); |
|
104 |
// Default algorithm, use AES-128 if AES-256 is not available. |
|
105 |
// Remember to sync with "securerandom.drbg.config" in java.security |
|
106 |
if (tryStrength <= 128 && AES_LIMIT < 256) { |
|
107 |
algorithm = "AES-128"; |
|
108 |
} else if (AES_LIMIT >= 256) { |
|
109 |
algorithm = "AES-256"; |
|
110 |
} else { |
|
111 |
throw new IllegalArgumentException("unsupported strength " + |
|
112 |
requestedInstantiationSecurityStrength); |
|
113 |
} |
|
114 |
this.securityStrength = tryStrength; |
|
115 |
} |
|
116 |
switch (algorithm.toUpperCase(Locale.ROOT)) { |
|
117 |
case "AES-128": |
|
118 |
case "AES-192": |
|
119 |
case "AES-256": |
|
120 |
this.keyAlg = "AES"; |
|
121 |
this.cipherAlg = "AES/ECB/NoPadding"; |
|
122 |
switch (algorithm) { |
|
123 |
case "AES-128": |
|
124 |
this.keyLen = 128 / 8; |
|
125 |
break; |
|
126 |
case "AES-192": |
|
127 |
this.keyLen = 192 / 8; |
|
128 |
if (AES_LIMIT < 192) { |
|
129 |
throw new IllegalArgumentException(algorithm + |
|
130 |
" not available (because policy) in CTR_DBRG"); |
|
131 |
} |
|
132 |
break; |
|
133 |
case "AES-256": |
|
134 |
this.keyLen = 256 / 8; |
|
135 |
if (AES_LIMIT < 256) { |
|
136 |
throw new IllegalArgumentException(algorithm + |
|
137 |
" not available (because policy) in CTR_DBRG"); |
|
138 |
} |
|
139 |
break; |
|
140 |
default: |
|
141 |
throw new IllegalArgumentException(algorithm + |
|
142 |
" not supported in CTR_DBRG"); |
|
143 |
} |
|
144 |
this.blockLen = 128 / 8; |
|
145 |
break; |
|
146 |
default: |
|
147 |
throw new IllegalArgumentException(algorithm + |
|
148 |
" not supported in CTR_DBRG"); |
|
149 |
} |
|
150 |
this.seedLen = this.blockLen + this.keyLen; |
|
151 |
this.ctrLen = this.blockLen; // TODO |
|
152 |
if (usedf) { |
|
153 |
this.minLength = this.securityStrength / 8; |
|
154 |
} else { |
|
155 |
this.minLength = this.maxLength = |
|
156 |
this.maxPersonalizationStringLength = |
|
157 |
this.maxAdditionalInputLength = seedLen; |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* This call, used by the constructors, instantiates the digest. |
|
163 |
*/ |
|
164 |
@Override |
|
165 |
protected void initEngine() { |
|
166 |
try { |
|
167 |
/* |
|
168 |
* Use the local SUN implementation to avoid native |
|
169 |
* performance overhead. |
|
170 |
*/ |
|
171 |
cipher = Cipher.getInstance(cipherAlg, "SunJCE"); |
|
172 |
} catch (NoSuchProviderException | NoSuchAlgorithmException |
|
173 |
| NoSuchPaddingException e) { |
|
174 |
// Fallback to any available. |
|
175 |
try { |
|
176 |
cipher = Cipher.getInstance(cipherAlg); |
|
177 |
} catch (NoSuchAlgorithmException | NoSuchPaddingException exc) { |
|
178 |
throw new InternalError( |
|
179 |
"internal error: " + cipherAlg + " not available.", exc); |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
private void status() { |
|
185 |
if (debug != null) { |
|
186 |
debug.println(this, "Key = " + hex(k)); |
|
187 |
debug.println(this, "V = " + hex(v)); |
|
188 |
debug.println(this, "reseed counter = " + reseedCounter); |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
// 800-90Ar1 10.2.1.2. CTR_DRBG_Update |
|
193 |
private void update(byte[] input) { |
|
194 |
if (input.length != seedLen) { |
|
195 |
// Should not happen |
|
196 |
throw new IllegalArgumentException("input length not seedLen: " |
|
197 |
+ input.length); |
|
198 |
} |
|
199 |
try { |
|
200 |
||
201 |
int m = (seedLen + blockLen - 1) / blockLen; |
|
202 |
byte[] temp = new byte[m * blockLen]; |
|
203 |
||
204 |
// Step 1. temp = Null. |
|
205 |
||
206 |
// Step 2. Loop |
|
207 |
for (int i = 0; i < m; i++) { |
|
208 |
// Step 2.1. Increment |
|
209 |
addOne(v, ctrLen); |
|
210 |
// Step 2.2. Block_Encrypt |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37895
diff
changeset
|
211 |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg)); |
37796 | 212 |
// Step 2.3. Encrypt into right position, no need to cat |
213 |
cipher.doFinal(v, 0, blockLen, temp, i * blockLen); |
|
214 |
} |
|
215 |
||
216 |
// Step 3. Truncate |
|
217 |
temp = Arrays.copyOf(temp, seedLen); |
|
218 |
||
219 |
// Step 4: Add |
|
220 |
for (int i = 0; i < seedLen; i++) { |
|
221 |
temp[i] ^= input[i]; |
|
222 |
} |
|
223 |
||
224 |
// Step 5: leftmost |
|
225 |
k = Arrays.copyOf(temp, keyLen); |
|
226 |
||
227 |
// Step 6: rightmost |
|
228 |
v = Arrays.copyOfRange(temp, seedLen - blockLen, seedLen); |
|
229 |
||
230 |
// Step 7. Return |
|
231 |
} catch (GeneralSecurityException e) { |
|
232 |
throw new InternalError(e); |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
@Override |
|
237 |
protected void instantiateAlgorithm(byte[] ei) { |
|
238 |
if (debug != null) { |
|
239 |
debug.println(this, "instantiate"); |
|
240 |
} |
|
241 |
byte[] more; |
|
242 |
if (usedf) { |
|
243 |
// 800-90Ar1 10.2.1.3.2 Step 1-2. cat bytes |
|
244 |
if (personalizationString == null) { |
|
245 |
more = nonce; |
|
246 |
} else { |
|
247 |
more = Arrays.copyOf( |
|
248 |
nonce, nonce.length + personalizationString.length); |
|
249 |
System.arraycopy(personalizationString, 0, more, nonce.length, |
|
250 |
personalizationString.length); |
|
251 |
} |
|
252 |
} else { |
|
253 |
// 800-90Ar1 10.2.1.3.1 |
|
254 |
// Step 1-2, no need to expand personalizationString, we only XOR |
|
255 |
// with shorter length |
|
256 |
more = personalizationString; |
|
257 |
} |
|
258 |
reseedAlgorithm(ei, more); |
|
259 |
} |
|
260 |
||
261 |
private byte[] df(byte[] input) { |
|
262 |
int l = input.length; |
|
263 |
int n = seedLen; |
|
264 |
int slen = 4 + 4 + l + 1; |
|
265 |
byte[] s = new byte[(slen + blockLen - 1) / blockLen * blockLen]; |
|
266 |
s[0] = (byte)(l >> 24); |
|
267 |
s[1] = (byte)(l >> 16); |
|
268 |
s[2] = (byte)(l >> 8); |
|
269 |
s[3] = (byte)(l); |
|
270 |
s[4] = (byte)(n >> 24); |
|
271 |
s[5] = (byte)(n >> 16); |
|
272 |
s[6] = (byte)(n >> 8); |
|
273 |
s[7] = (byte)(n); |
|
274 |
System.arraycopy(input, 0, s, 8, l); |
|
275 |
s[8+l] = (byte)0x80; |
|
276 |
||
277 |
byte[] k = new byte[keyLen]; |
|
278 |
for (int i = 0; i < k.length; i++) { |
|
279 |
k[i] = (byte)i; |
|
280 |
} |
|
281 |
||
282 |
byte[] temp = new byte[seedLen]; |
|
283 |
||
284 |
for (int i = 0; i * blockLen < temp.length; i++) { |
|
285 |
byte[] iv = new byte[blockLen + s.length]; |
|
286 |
iv[0] = (byte)(i >> 24); |
|
287 |
iv[1] = (byte)(i >> 16); |
|
288 |
iv[2] = (byte)(i >> 8); |
|
289 |
iv[3] = (byte)(i); |
|
290 |
System.arraycopy(s, 0, iv, blockLen, s.length); |
|
291 |
int tailLen = temp.length - blockLen*i; |
|
292 |
if (tailLen > blockLen) { |
|
293 |
tailLen = blockLen; |
|
294 |
} |
|
295 |
System.arraycopy(bcc(k, iv), 0, temp, blockLen*i, tailLen); |
|
296 |
} |
|
297 |
||
298 |
k = Arrays.copyOf(temp, keyLen); |
|
299 |
byte[] x = Arrays.copyOfRange(temp, keyLen, temp.length); |
|
300 |
||
301 |
for (int i = 0; i * blockLen < seedLen; i++) { |
|
302 |
try { |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37895
diff
changeset
|
303 |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg)); |
37796 | 304 |
int tailLen = temp.length - blockLen*i; |
305 |
if (tailLen > blockLen) { |
|
306 |
tailLen = blockLen; |
|
307 |
} |
|
308 |
x = cipher.doFinal(x); |
|
309 |
System.arraycopy(x, 0, temp, blockLen * i, tailLen); |
|
310 |
} catch (GeneralSecurityException e) { |
|
311 |
throw new InternalError(e); |
|
312 |
} |
|
313 |
} |
|
314 |
return temp; |
|
315 |
} |
|
316 |
||
317 |
private byte[] bcc(byte[] k, byte[] data) { |
|
318 |
byte[] chain = new byte[blockLen]; |
|
319 |
int n = data.length / blockLen; |
|
320 |
for (int i = 0; i < n; i++) { |
|
321 |
byte[] inputBlock = Arrays.copyOfRange( |
|
322 |
data, i * blockLen, i * blockLen + blockLen); |
|
323 |
for (int j = 0; j < blockLen; j++) { |
|
324 |
inputBlock[j] ^= chain[j]; |
|
325 |
} |
|
326 |
try { |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37895
diff
changeset
|
327 |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg)); |
37796 | 328 |
chain = cipher.doFinal(inputBlock); |
329 |
} catch (GeneralSecurityException e) { |
|
330 |
throw new InternalError(e); |
|
331 |
} |
|
332 |
} |
|
333 |
return chain; |
|
334 |
} |
|
335 |
||
336 |
@Override |
|
37895 | 337 |
protected synchronized void reseedAlgorithm( |
37796 | 338 |
byte[] ei, |
339 |
byte[] additionalInput) { |
|
340 |
if (usedf) { |
|
341 |
// 800-90Ar1 10.2.1.3.2 Instantiate. |
|
342 |
// 800-90Ar1 10.2.1.4.2 Reseed. |
|
343 |
||
344 |
// Step 1: cat bytes |
|
345 |
if (additionalInput != null) { |
|
346 |
byte[] temp = Arrays.copyOf( |
|
347 |
ei, ei.length + additionalInput.length); |
|
348 |
System.arraycopy(additionalInput, 0, temp, ei.length, |
|
349 |
additionalInput.length); |
|
350 |
ei = temp; |
|
351 |
} |
|
352 |
// Step 2. df (seed_material, seedlen). |
|
353 |
ei = df(ei); |
|
354 |
} else { |
|
355 |
// 800-90Ar1 10.2.1.3.1 Instantiate |
|
356 |
// 800-90Ar1 10.2.1.4.1 Reseed |
|
357 |
// Step 1-2. Needless |
|
358 |
// Step 3. seed_material = entropy_input XOR more |
|
359 |
if (additionalInput != null) { |
|
360 |
// additionalInput.length <= seedLen |
|
361 |
for (int i = 0; i < additionalInput.length; i++) { |
|
362 |
ei[i] ^= additionalInput[i]; |
|
363 |
} |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
if (v == null) { |
|
368 |
// 800-90Ar1 10.2.1.3.2 Instantiate. Step 3-4 |
|
369 |
// 800-90Ar1 10.2.1.3.1 Instantiate. Step 4-5 |
|
370 |
k = new byte[keyLen]; |
|
371 |
v = new byte[blockLen]; |
|
372 |
} |
|
373 |
//status(); |
|
374 |
||
375 |
// 800-90Ar1 10.2.1.3.1 Instantiate. Step 6 |
|
376 |
// 800-90Ar1 10.2.1.3.2 Instantiate. Step 5 |
|
377 |
// 800-90Ar1 10.2.1.4.1 Reseed. Step 4 |
|
378 |
// 800-90Ar1 10.2.1.4.2 Reseed. Step 3 |
|
379 |
update(ei); |
|
380 |
// 800-90Ar1 10.2.1.3.1 Instantiate. Step 7 |
|
381 |
// 800-90Ar1 10.2.1.3.2 Instantiate. Step 6 |
|
382 |
// 800-90Ar1 10.2.1.4.1 Reseed. Step 5 |
|
383 |
// 800-90Ar1 10.2.1.4.2 Reseed. Step 4 |
|
384 |
reseedCounter = 1; |
|
385 |
//status(); |
|
386 |
||
387 |
// Whatever step. Return |
|
388 |
} |
|
389 |
||
390 |
/** |
|
391 |
* Add one to data, only touch the last len bytes. |
|
392 |
*/ |
|
393 |
private static void addOne(byte[] data, int len) { |
|
394 |
for (int i = 0; i < len; i++) { |
|
395 |
data[data.length - 1 - i]++; |
|
396 |
if (data[data.length - 1 - i] != 0) { |
|
397 |
break; |
|
398 |
} |
|
399 |
} |
|
400 |
} |
|
401 |
||
402 |
@Override |
|
403 |
public synchronized void generateAlgorithm( |
|
404 |
byte[] result, byte[] additionalInput) { |
|
405 |
||
406 |
if (debug != null) { |
|
407 |
debug.println(this, "generateAlgorithm"); |
|
408 |
} |
|
409 |
||
410 |
// 800-90Ar1 10.2.1.5.1 Generate |
|
411 |
// 800-90Ar1 10.2.1.5.2 Generate |
|
412 |
||
413 |
// Step 1: Check reseed_counter. Will not fail. Already checked in |
|
414 |
// AbstractDrbg#engineNextBytes. |
|
415 |
||
416 |
if (additionalInput != null) { |
|
417 |
if (usedf) { |
|
418 |
// 10.2.1.5.2 Step 2.1 |
|
419 |
additionalInput = df(additionalInput); |
|
420 |
} else { |
|
421 |
// 10.2.1.5.1 Step 2.1-2.2 |
|
422 |
additionalInput = Arrays.copyOf(additionalInput, seedLen); |
|
423 |
} |
|
424 |
// 10.2.1.5.1 Step 2.3 |
|
425 |
// 10.2.1.5.2 Step 2.2 |
|
426 |
update(additionalInput); |
|
427 |
} else { |
|
428 |
// 10.2.1.5.1 Step 2 Else |
|
429 |
// 10.2.1.5.2 Step 2 Else |
|
430 |
additionalInput = new byte[seedLen]; |
|
431 |
} |
|
432 |
||
433 |
// Step 3. temp = Null |
|
434 |
int pos = 0; |
|
435 |
||
436 |
// Step 4. Loop |
|
437 |
while (pos < result.length) { |
|
438 |
int tailLen = result.length - pos; |
|
439 |
// Step 4.1. Increment |
|
440 |
addOne(v, ctrLen); |
|
441 |
try { |
|
442 |
// Step 4.2. Encrypt |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37895
diff
changeset
|
443 |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg)); |
37796 | 444 |
byte[] out = cipher.doFinal(v); |
445 |
||
446 |
// Step 4.3 and 5. Cat bytes and leftmost |
|
447 |
System.arraycopy(out, 0, result, pos, |
|
448 |
(tailLen > blockLen) ? blockLen : tailLen); |
|
449 |
} catch (GeneralSecurityException e) { |
|
450 |
throw new InternalError(e); |
|
451 |
} |
|
452 |
pos += blockLen; |
|
453 |
} |
|
454 |
||
455 |
// Step 6. Update |
|
456 |
update(additionalInput); |
|
457 |
||
458 |
// Step 7. reseed_counter++ |
|
459 |
reseedCounter++; |
|
460 |
||
461 |
//status(); |
|
462 |
||
463 |
// Step 8. Return |
|
464 |
} |
|
465 |
||
466 |
private void readObject(java.io.ObjectInputStream s) |
|
467 |
throws IOException, ClassNotFoundException { |
|
468 |
s.defaultReadObject (); |
|
469 |
initEngine(); |
|
470 |
} |
|
471 |
||
472 |
@Override |
|
473 |
public String toString() { |
|
474 |
return super.toString() + "/" |
|
475 |
+ (usedf ? "use_df" : "no_df"); |
|
476 |
} |
|
477 |
} |