author | weijun |
Wed, 01 Aug 2018 13:35:08 +0800 | |
changeset 51272 | 9d92ff04a29c |
parent 47216 | 71c04702a3d5 |
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.security.*; |
|
32 |
import java.util.Arrays; |
|
33 |
import java.util.Locale; |
|
34 |
||
35 |
public class CtrDrbg extends AbstractDrbg { |
|
36 |
||
37 |
private static final int AES_LIMIT; |
|
38 |
||
39 |
static { |
|
40 |
try { |
|
41 |
AES_LIMIT = Cipher.getMaxAllowedKeyLength("AES"); |
|
42 |
} catch (Exception e) { |
|
43 |
// should not happen |
|
44 |
throw new AssertionError("Cannot detect AES", e); |
|
45 |
} |
|
46 |
} |
|
47 |
||
38853 | 48 |
private Cipher cipher; |
37796 | 49 |
|
50 |
private String cipherAlg; |
|
51 |
private String keyAlg; |
|
52 |
||
53 |
private int ctrLen; |
|
54 |
private int blockLen; |
|
55 |
private int keyLen; |
|
56 |
private int seedLen; |
|
57 |
||
38853 | 58 |
private byte[] v; |
59 |
private byte[] k; |
|
37796 | 60 |
|
61 |
public CtrDrbg(SecureRandomParameters params) { |
|
62 |
mechName = "CTR_DRBG"; |
|
63 |
configure(params); |
|
64 |
} |
|
65 |
||
66 |
private static int alg2strength(String algorithm) { |
|
67 |
switch (algorithm.toUpperCase(Locale.ROOT)) { |
|
68 |
case "AES-128": |
|
69 |
return 128; |
|
70 |
case "AES-192": |
|
71 |
return 192; |
|
72 |
case "AES-256": |
|
73 |
return 256; |
|
74 |
default: |
|
75 |
throw new IllegalArgumentException(algorithm + |
|
76 |
" not supported in CTR_DBRG"); |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
@Override |
|
81 |
protected void chooseAlgorithmAndStrength() { |
|
82 |
if (requestedAlgorithm != null) { |
|
42929
032d90a24440
6972386: issues with String.toLowerCase/toUpperCase
xuelei
parents:
39481
diff
changeset
|
83 |
algorithm = requestedAlgorithm.toUpperCase(Locale.ROOT); |
37796 | 84 |
int supportedStrength = alg2strength(algorithm); |
85 |
if (requestedInstantiationSecurityStrength >= 0) { |
|
86 |
int tryStrength = getStandardStrength( |
|
87 |
requestedInstantiationSecurityStrength); |
|
88 |
if (tryStrength > supportedStrength) { |
|
89 |
throw new IllegalArgumentException(algorithm + |
|
90 |
" does not support strength " + |
|
91 |
requestedInstantiationSecurityStrength); |
|
92 |
} |
|
93 |
this.securityStrength = tryStrength; |
|
94 |
} else { |
|
95 |
this.securityStrength = (DEFAULT_STRENGTH > supportedStrength) ? |
|
96 |
supportedStrength : DEFAULT_STRENGTH; |
|
97 |
} |
|
98 |
} else { |
|
99 |
int tryStrength = (requestedInstantiationSecurityStrength < 0) ? |
|
100 |
DEFAULT_STRENGTH : requestedInstantiationSecurityStrength; |
|
101 |
tryStrength = getStandardStrength(tryStrength); |
|
102 |
// Default algorithm, use AES-128 if AES-256 is not available. |
|
103 |
// Remember to sync with "securerandom.drbg.config" in java.security |
|
104 |
if (tryStrength <= 128 && AES_LIMIT < 256) { |
|
105 |
algorithm = "AES-128"; |
|
106 |
} else if (AES_LIMIT >= 256) { |
|
107 |
algorithm = "AES-256"; |
|
108 |
} else { |
|
109 |
throw new IllegalArgumentException("unsupported strength " + |
|
110 |
requestedInstantiationSecurityStrength); |
|
111 |
} |
|
112 |
this.securityStrength = tryStrength; |
|
113 |
} |
|
114 |
switch (algorithm.toUpperCase(Locale.ROOT)) { |
|
115 |
case "AES-128": |
|
116 |
case "AES-192": |
|
117 |
case "AES-256": |
|
118 |
this.keyAlg = "AES"; |
|
119 |
this.cipherAlg = "AES/ECB/NoPadding"; |
|
120 |
switch (algorithm) { |
|
121 |
case "AES-128": |
|
122 |
this.keyLen = 128 / 8; |
|
123 |
break; |
|
124 |
case "AES-192": |
|
125 |
this.keyLen = 192 / 8; |
|
126 |
if (AES_LIMIT < 192) { |
|
127 |
throw new IllegalArgumentException(algorithm + |
|
128 |
" not available (because policy) in CTR_DBRG"); |
|
129 |
} |
|
130 |
break; |
|
131 |
case "AES-256": |
|
132 |
this.keyLen = 256 / 8; |
|
133 |
if (AES_LIMIT < 256) { |
|
134 |
throw new IllegalArgumentException(algorithm + |
|
135 |
" not available (because policy) in CTR_DBRG"); |
|
136 |
} |
|
137 |
break; |
|
138 |
default: |
|
139 |
throw new IllegalArgumentException(algorithm + |
|
140 |
" not supported in CTR_DBRG"); |
|
141 |
} |
|
142 |
this.blockLen = 128 / 8; |
|
143 |
break; |
|
144 |
default: |
|
145 |
throw new IllegalArgumentException(algorithm + |
|
146 |
" not supported in CTR_DBRG"); |
|
147 |
} |
|
148 |
this.seedLen = this.blockLen + this.keyLen; |
|
149 |
this.ctrLen = this.blockLen; // TODO |
|
150 |
if (usedf) { |
|
151 |
this.minLength = this.securityStrength / 8; |
|
152 |
} else { |
|
153 |
this.minLength = this.maxLength = |
|
154 |
this.maxPersonalizationStringLength = |
|
155 |
this.maxAdditionalInputLength = seedLen; |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* This call, used by the constructors, instantiates the digest. |
|
161 |
*/ |
|
162 |
@Override |
|
163 |
protected void initEngine() { |
|
164 |
try { |
|
165 |
/* |
|
38853 | 166 |
* Use the local SunJCE implementation to avoid native |
37796 | 167 |
* performance overhead. |
168 |
*/ |
|
169 |
cipher = Cipher.getInstance(cipherAlg, "SunJCE"); |
|
170 |
} catch (NoSuchProviderException | NoSuchAlgorithmException |
|
171 |
| NoSuchPaddingException e) { |
|
172 |
// Fallback to any available. |
|
173 |
try { |
|
174 |
cipher = Cipher.getInstance(cipherAlg); |
|
175 |
} catch (NoSuchAlgorithmException | NoSuchPaddingException exc) { |
|
176 |
throw new InternalError( |
|
177 |
"internal error: " + cipherAlg + " not available.", exc); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
private void status() { |
|
183 |
if (debug != null) { |
|
184 |
debug.println(this, "Key = " + hex(k)); |
|
185 |
debug.println(this, "V = " + hex(v)); |
|
186 |
debug.println(this, "reseed counter = " + reseedCounter); |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
// 800-90Ar1 10.2.1.2. CTR_DRBG_Update |
|
191 |
private void update(byte[] input) { |
|
192 |
if (input.length != seedLen) { |
|
193 |
// Should not happen |
|
194 |
throw new IllegalArgumentException("input length not seedLen: " |
|
195 |
+ input.length); |
|
196 |
} |
|
197 |
try { |
|
198 |
||
199 |
int m = (seedLen + blockLen - 1) / blockLen; |
|
200 |
byte[] temp = new byte[m * blockLen]; |
|
201 |
||
202 |
// Step 1. temp = Null. |
|
203 |
||
204 |
// Step 2. Loop |
|
205 |
for (int i = 0; i < m; i++) { |
|
206 |
// Step 2.1. Increment |
|
207 |
addOne(v, ctrLen); |
|
208 |
// Step 2.2. Block_Encrypt |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37895
diff
changeset
|
209 |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg)); |
37796 | 210 |
// Step 2.3. Encrypt into right position, no need to cat |
211 |
cipher.doFinal(v, 0, blockLen, temp, i * blockLen); |
|
212 |
} |
|
213 |
||
214 |
// Step 3. Truncate |
|
215 |
temp = Arrays.copyOf(temp, seedLen); |
|
216 |
||
217 |
// Step 4: Add |
|
218 |
for (int i = 0; i < seedLen; i++) { |
|
219 |
temp[i] ^= input[i]; |
|
220 |
} |
|
221 |
||
222 |
// Step 5: leftmost |
|
223 |
k = Arrays.copyOf(temp, keyLen); |
|
224 |
||
225 |
// Step 6: rightmost |
|
226 |
v = Arrays.copyOfRange(temp, seedLen - blockLen, seedLen); |
|
227 |
||
228 |
// Step 7. Return |
|
229 |
} catch (GeneralSecurityException e) { |
|
230 |
throw new InternalError(e); |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
protected void instantiateAlgorithm(byte[] ei) { |
|
236 |
if (debug != null) { |
|
237 |
debug.println(this, "instantiate"); |
|
238 |
} |
|
239 |
byte[] more; |
|
240 |
if (usedf) { |
|
241 |
// 800-90Ar1 10.2.1.3.2 Step 1-2. cat bytes |
|
242 |
if (personalizationString == null) { |
|
243 |
more = nonce; |
|
244 |
} else { |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
245 |
if (nonce.length + personalizationString.length < 0) { |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
246 |
// Length must be represented as a 32 bit integer in df() |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
247 |
throw new IllegalArgumentException( |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
248 |
"nonce plus personalization string is too long"); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
249 |
} |
37796 | 250 |
more = Arrays.copyOf( |
251 |
nonce, nonce.length + personalizationString.length); |
|
252 |
System.arraycopy(personalizationString, 0, more, nonce.length, |
|
253 |
personalizationString.length); |
|
254 |
} |
|
255 |
} else { |
|
256 |
// 800-90Ar1 10.2.1.3.1 |
|
257 |
// Step 1-2, no need to expand personalizationString, we only XOR |
|
258 |
// with shorter length |
|
259 |
more = personalizationString; |
|
260 |
} |
|
261 |
reseedAlgorithm(ei, more); |
|
262 |
} |
|
263 |
||
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
264 |
/** |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
265 |
* Block_cipher_df in 10.3.2 |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
266 |
* |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
267 |
* @param input the input string |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
268 |
* @return the output block (always of seedLen) |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
269 |
*/ |
37796 | 270 |
private byte[] df(byte[] input) { |
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
271 |
// 800-90Ar1 10.3.2 |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
272 |
// 2. L = len (input_string)/8 |
37796 | 273 |
int l = input.length; |
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
274 |
// 3. N = number_of_bits_to_return/8 |
37796 | 275 |
int n = seedLen; |
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
276 |
// 4. S = L || N || input_string || 0x80 |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
277 |
byte[] ln = new byte[8]; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
278 |
ln[0] = (byte)(l >> 24); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
279 |
ln[1] = (byte)(l >> 16); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
280 |
ln[2] = (byte)(l >> 8); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
281 |
ln[3] = (byte)(l); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
282 |
ln[4] = (byte)(n >> 24); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
283 |
ln[5] = (byte)(n >> 16); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
284 |
ln[6] = (byte)(n >> 8); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
285 |
ln[7] = (byte)(n); |
37796 | 286 |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
287 |
// 5. Zero padding of S |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
288 |
// Not necessary, see bcc |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
289 |
|
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
290 |
// 8. K = leftmost (0x00010203...1D1E1F, keylen). |
37796 | 291 |
byte[] k = new byte[keyLen]; |
292 |
for (int i = 0; i < k.length; i++) { |
|
293 |
k[i] = (byte)i; |
|
294 |
} |
|
295 |
||
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
296 |
// 6. temp = the Null String |
37796 | 297 |
byte[] temp = new byte[seedLen]; |
298 |
||
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
299 |
// 7. i = 0 |
37796 | 300 |
for (int i = 0; i * blockLen < temp.length; i++) { |
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
301 |
// 9.1 IV = i || 0^(outlen - len (i)). outLen is blockLen |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
302 |
byte[] iv = new byte[blockLen]; |
37796 | 303 |
iv[0] = (byte)(i >> 24); |
304 |
iv[1] = (byte)(i >> 16); |
|
305 |
iv[2] = (byte)(i >> 8); |
|
306 |
iv[3] = (byte)(i); |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
307 |
|
37796 | 308 |
int tailLen = temp.length - blockLen*i; |
309 |
if (tailLen > blockLen) { |
|
310 |
tailLen = blockLen; |
|
311 |
} |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
312 |
// 9.2 temp = temp || BCC (K, (IV || S)). |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
313 |
System.arraycopy(bcc(k, iv, ln, input, new byte[]{(byte)0x80}), |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
314 |
0, temp, blockLen*i, tailLen); |
37796 | 315 |
} |
316 |
||
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
317 |
// 10. K = leftmost(temp, keylen) |
37796 | 318 |
k = Arrays.copyOf(temp, keyLen); |
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
319 |
|
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
320 |
// 11. x = select(temp, keylen+1, keylen+outlen) |
37796 | 321 |
byte[] x = Arrays.copyOfRange(temp, keyLen, temp.length); |
322 |
||
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
323 |
// 12. temp = the Null string |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
324 |
// No need to clean up, temp will be overwritten |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
325 |
|
37796 | 326 |
for (int i = 0; i * blockLen < seedLen; i++) { |
327 |
try { |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37895
diff
changeset
|
328 |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg)); |
37796 | 329 |
int tailLen = temp.length - blockLen*i; |
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
330 |
// 14. requested_bits = leftmost(temp, nuumber_of_bits_to_return) |
37796 | 331 |
if (tailLen > blockLen) { |
332 |
tailLen = blockLen; |
|
333 |
} |
|
334 |
x = cipher.doFinal(x); |
|
335 |
System.arraycopy(x, 0, temp, blockLen * i, tailLen); |
|
336 |
} catch (GeneralSecurityException e) { |
|
337 |
throw new InternalError(e); |
|
338 |
} |
|
339 |
} |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
340 |
|
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
341 |
// 15. Return |
37796 | 342 |
return temp; |
343 |
} |
|
344 |
||
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
345 |
/** |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
346 |
* Block_Encrypt in 10.3.3 |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
347 |
* |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
348 |
* @param k the key |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
349 |
* @param data after concatenated, the data to be operated upon. This is |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
350 |
* a series of byte[], each with an arbitrary length. Note |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
351 |
* that the full length is not necessarily a multiple of |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
352 |
* outlen. XOR with zero is no-op. |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
353 |
* @return the result |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
354 |
*/ |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
355 |
private byte[] bcc(byte[] k, byte[]... data) { |
37796 | 356 |
byte[] chain = new byte[blockLen]; |
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
357 |
int n1 = 0; // index in data |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
358 |
int n2 = 0; // index in data[n1] |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
359 |
// pack blockLen of bytes into chain from data[][], again and again |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
360 |
while (n1 < data.length) { |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
361 |
int j; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
362 |
out: for (j = 0; j < blockLen; j++) { |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
363 |
while (n2 >= data[n1].length) { |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
364 |
n1++; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
365 |
if (n1 >= data.length) { |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
366 |
break out; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
367 |
} |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
368 |
n2 = 0; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
369 |
} |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
370 |
chain[j] ^= data[n1][n2]; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
371 |
n2++; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
372 |
} |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
373 |
if (j == 0) { // all data happens to be consumed in the last loop |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
374 |
break; |
37796 | 375 |
} |
376 |
try { |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37895
diff
changeset
|
377 |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg)); |
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
378 |
chain = cipher.doFinal(chain); |
37796 | 379 |
} catch (GeneralSecurityException e) { |
380 |
throw new InternalError(e); |
|
381 |
} |
|
382 |
} |
|
383 |
return chain; |
|
384 |
} |
|
385 |
||
386 |
@Override |
|
37895 | 387 |
protected synchronized void reseedAlgorithm( |
37796 | 388 |
byte[] ei, |
389 |
byte[] additionalInput) { |
|
390 |
if (usedf) { |
|
391 |
// 800-90Ar1 10.2.1.3.2 Instantiate. |
|
392 |
// 800-90Ar1 10.2.1.4.2 Reseed. |
|
393 |
||
394 |
// Step 1: cat bytes |
|
395 |
if (additionalInput != null) { |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
396 |
if (ei.length + additionalInput.length < 0) { |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
397 |
// Length must be represented as a 32 bit integer in df() |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
398 |
throw new IllegalArgumentException( |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
399 |
"entropy plus additional input is too long"); |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
400 |
} |
37796 | 401 |
byte[] temp = Arrays.copyOf( |
402 |
ei, ei.length + additionalInput.length); |
|
403 |
System.arraycopy(additionalInput, 0, temp, ei.length, |
|
404 |
additionalInput.length); |
|
405 |
ei = temp; |
|
406 |
} |
|
407 |
// Step 2. df (seed_material, seedlen). |
|
408 |
ei = df(ei); |
|
409 |
} else { |
|
410 |
// 800-90Ar1 10.2.1.3.1 Instantiate |
|
411 |
// 800-90Ar1 10.2.1.4.1 Reseed |
|
412 |
// Step 1-2. Needless |
|
413 |
// Step 3. seed_material = entropy_input XOR more |
|
414 |
if (additionalInput != null) { |
|
415 |
// additionalInput.length <= seedLen |
|
416 |
for (int i = 0; i < additionalInput.length; i++) { |
|
417 |
ei[i] ^= additionalInput[i]; |
|
418 |
} |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
if (v == null) { |
|
423 |
// 800-90Ar1 10.2.1.3.2 Instantiate. Step 3-4 |
|
424 |
// 800-90Ar1 10.2.1.3.1 Instantiate. Step 4-5 |
|
425 |
k = new byte[keyLen]; |
|
426 |
v = new byte[blockLen]; |
|
427 |
} |
|
428 |
//status(); |
|
429 |
||
430 |
// 800-90Ar1 10.2.1.3.1 Instantiate. Step 6 |
|
431 |
// 800-90Ar1 10.2.1.3.2 Instantiate. Step 5 |
|
432 |
// 800-90Ar1 10.2.1.4.1 Reseed. Step 4 |
|
433 |
// 800-90Ar1 10.2.1.4.2 Reseed. Step 3 |
|
434 |
update(ei); |
|
435 |
// 800-90Ar1 10.2.1.3.1 Instantiate. Step 7 |
|
436 |
// 800-90Ar1 10.2.1.3.2 Instantiate. Step 6 |
|
437 |
// 800-90Ar1 10.2.1.4.1 Reseed. Step 5 |
|
438 |
// 800-90Ar1 10.2.1.4.2 Reseed. Step 4 |
|
439 |
reseedCounter = 1; |
|
440 |
//status(); |
|
441 |
||
442 |
// Whatever step. Return |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* Add one to data, only touch the last len bytes. |
|
447 |
*/ |
|
448 |
private static void addOne(byte[] data, int len) { |
|
449 |
for (int i = 0; i < len; i++) { |
|
450 |
data[data.length - 1 - i]++; |
|
451 |
if (data[data.length - 1 - i] != 0) { |
|
452 |
break; |
|
453 |
} |
|
454 |
} |
|
455 |
} |
|
456 |
||
457 |
@Override |
|
458 |
public synchronized void generateAlgorithm( |
|
459 |
byte[] result, byte[] additionalInput) { |
|
460 |
||
461 |
if (debug != null) { |
|
462 |
debug.println(this, "generateAlgorithm"); |
|
463 |
} |
|
464 |
||
465 |
// 800-90Ar1 10.2.1.5.1 Generate |
|
466 |
// 800-90Ar1 10.2.1.5.2 Generate |
|
467 |
||
468 |
// Step 1: Check reseed_counter. Will not fail. Already checked in |
|
469 |
// AbstractDrbg#engineNextBytes. |
|
470 |
||
471 |
if (additionalInput != null) { |
|
472 |
if (usedf) { |
|
473 |
// 10.2.1.5.2 Step 2.1 |
|
474 |
additionalInput = df(additionalInput); |
|
475 |
} else { |
|
476 |
// 10.2.1.5.1 Step 2.1-2.2 |
|
477 |
additionalInput = Arrays.copyOf(additionalInput, seedLen); |
|
478 |
} |
|
479 |
// 10.2.1.5.1 Step 2.3 |
|
480 |
// 10.2.1.5.2 Step 2.2 |
|
481 |
update(additionalInput); |
|
482 |
} else { |
|
483 |
// 10.2.1.5.1 Step 2 Else |
|
484 |
// 10.2.1.5.2 Step 2 Else |
|
485 |
additionalInput = new byte[seedLen]; |
|
486 |
} |
|
487 |
||
488 |
// Step 3. temp = Null |
|
489 |
int pos = 0; |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
490 |
int len = result.length; |
37796 | 491 |
|
492 |
// Step 4. Loop |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
493 |
while (len > 0) { |
37796 | 494 |
// Step 4.1. Increment |
495 |
addOne(v, ctrLen); |
|
496 |
try { |
|
497 |
// Step 4.2. Encrypt |
|
37896
cd841af7dcd0
8156213: Remove SHA-1 and 3KeyTDEA algorithms from DRBG
weijun
parents:
37895
diff
changeset
|
498 |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(k, keyAlg)); |
37796 | 499 |
byte[] out = cipher.doFinal(v); |
500 |
||
501 |
// Step 4.3 and 5. Cat bytes and leftmost |
|
502 |
System.arraycopy(out, 0, result, pos, |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
503 |
(len > blockLen) ? blockLen : len); |
37796 | 504 |
} catch (GeneralSecurityException e) { |
505 |
throw new InternalError(e); |
|
506 |
} |
|
39481
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
507 |
len -= blockLen; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
508 |
if (len <= 0) { |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
509 |
// shortcut, so that pos needn't be updated |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
510 |
break; |
63ceb7ef04d4
8158589: Possible integer overflow issues for DRBG
weijun
parents:
38853
diff
changeset
|
511 |
} |
37796 | 512 |
pos += blockLen; |
513 |
} |
|
514 |
||
515 |
// Step 6. Update |
|
516 |
update(additionalInput); |
|
517 |
||
518 |
// Step 7. reseed_counter++ |
|
519 |
reseedCounter++; |
|
520 |
||
521 |
//status(); |
|
522 |
||
523 |
// Step 8. Return |
|
524 |
} |
|
525 |
||
526 |
@Override |
|
527 |
public String toString() { |
|
38462 | 528 |
return super.toString() + "," |
37796 | 529 |
+ (usedf ? "use_df" : "no_df"); |
530 |
} |
|
531 |
} |