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 sun.security.util.HexDumpEncoder;
|
|
29 |
|
|
30 |
import java.util.Arrays;
|
|
31 |
import java.util.Locale;
|
|
32 |
|
|
33 |
public abstract class AbstractHashDrbg extends AbstractDrbg {
|
|
34 |
|
|
35 |
private static final long serialVersionUID = 9L;
|
|
36 |
|
|
37 |
protected int outLen;
|
|
38 |
protected int seedLen;
|
|
39 |
|
|
40 |
private static int alg2strength(String algorithm) {
|
|
41 |
switch (algorithm.toUpperCase(Locale.ROOT)) {
|
|
42 |
case "SHA-224":
|
|
43 |
case "SHA-512/224":
|
|
44 |
return 192;
|
|
45 |
case "SHA-256":
|
|
46 |
case "SHA-512/256":
|
|
47 |
case "SHA-384":
|
|
48 |
case "SHA-512":
|
|
49 |
return 256;
|
|
50 |
default:
|
|
51 |
throw new IllegalArgumentException(algorithm +
|
|
52 |
" not supported in Hash_DBRG");
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
protected void chooseAlgorithmAndStrength() {
|
|
57 |
if (requestedAlgorithm != null) {
|
|
58 |
algorithm = requestedAlgorithm.toUpperCase(Locale.ROOT);
|
|
59 |
int supportedStrength = alg2strength(algorithm);
|
|
60 |
if (requestedInstantiationSecurityStrength >= 0) {
|
|
61 |
int tryStrength = getStandardStrength(
|
|
62 |
requestedInstantiationSecurityStrength);
|
|
63 |
if (tryStrength > supportedStrength) {
|
|
64 |
throw new IllegalArgumentException(algorithm +
|
|
65 |
" does not support strength " +
|
|
66 |
requestedInstantiationSecurityStrength);
|
|
67 |
}
|
|
68 |
this.securityStrength = tryStrength;
|
|
69 |
} else {
|
|
70 |
this.securityStrength = DEFAULT_STRENGTH > supportedStrength ?
|
|
71 |
supportedStrength : DEFAULT_STRENGTH;
|
|
72 |
}
|
|
73 |
} else {
|
|
74 |
int tryStrength = (requestedInstantiationSecurityStrength < 0) ?
|
|
75 |
DEFAULT_STRENGTH : requestedInstantiationSecurityStrength;
|
|
76 |
tryStrength = getStandardStrength(tryStrength);
|
|
77 |
// The default algorithm which is enough for all strengths.
|
|
78 |
// Remember to sync with "securerandom.drbg.config" in java.security
|
|
79 |
algorithm = "SHA-256";
|
|
80 |
this.securityStrength = tryStrength;
|
|
81 |
}
|
|
82 |
switch (algorithm.toUpperCase(Locale.ROOT)) {
|
|
83 |
case "SHA-224":
|
|
84 |
case "SHA-512/224":
|
|
85 |
this.seedLen = 440 / 8;
|
|
86 |
this.outLen = 224 / 8;
|
|
87 |
break;
|
|
88 |
case "SHA-256":
|
|
89 |
case "SHA-512/256":
|
|
90 |
this.seedLen = 440 / 8;
|
|
91 |
this.outLen = 256 / 8;
|
|
92 |
break;
|
|
93 |
case "SHA-384":
|
|
94 |
this.seedLen = 888 / 8;
|
|
95 |
this.outLen = 384 / 8;
|
|
96 |
break;
|
|
97 |
case "SHA-512":
|
|
98 |
this.seedLen = 888 / 8;
|
|
99 |
this.outLen = 512 / 8;
|
|
100 |
break;
|
|
101 |
default:
|
|
102 |
throw new IllegalArgumentException(algorithm +
|
|
103 |
" not supported in Hash_DBRG");
|
|
104 |
}
|
|
105 |
this.minLength = this.securityStrength / 8;
|
|
106 |
}
|
|
107 |
|
|
108 |
@Override
|
|
109 |
public void instantiateAlgorithm(byte[] entropy) {
|
|
110 |
if (debug != null) {
|
|
111 |
debug.println(this, "instantiate");
|
|
112 |
}
|
|
113 |
|
|
114 |
// 800-90Ar1 10.1.1.2: Hash_DRBG Instantiate Process.
|
|
115 |
// 800-90Ar1 10.1.2.3: Hmac_DRBG Instantiate Process.
|
|
116 |
|
|
117 |
// Step 1: entropy_input || nonce || personalization_string.
|
|
118 |
byte[] seed = Arrays.copyOf(entropy, entropy.length + nonce.length +
|
|
119 |
((personalizationString == null) ? 0
|
|
120 |
: personalizationString.length));
|
|
121 |
System.arraycopy(nonce, 0, seed, entropy.length, nonce.length);
|
|
122 |
if (personalizationString != null) {
|
|
123 |
System.arraycopy(personalizationString, 0,
|
|
124 |
seed, entropy.length + nonce.length,
|
|
125 |
personalizationString.length);
|
|
126 |
}
|
|
127 |
hashReseedInternal(seed);
|
|
128 |
}
|
|
129 |
|
|
130 |
@Override
|
|
131 |
protected void reseedAlgorithm(
|
|
132 |
byte[] ei,
|
|
133 |
byte[] additionalInput) {
|
|
134 |
if (debug != null) {
|
|
135 |
debug.println(this, "reseedAlgorithm\n" +
|
|
136 |
new HexDumpEncoder().encodeBuffer(ei) + "\n" +
|
|
137 |
((additionalInput == null) ? "" :
|
|
138 |
new HexDumpEncoder().encodeBuffer(additionalInput)));
|
|
139 |
}
|
|
140 |
|
|
141 |
// 800-90Ar1 10.1.1.3: Hash_DRBG Reseed Process.
|
|
142 |
// 800-90Ar1 10.1.2.4: Hmac_DRBG Reseed Process.
|
|
143 |
|
|
144 |
// Step 1: entropy_input || additional_input.
|
|
145 |
if (additionalInput != null) {
|
|
146 |
ei = Arrays.copyOf(ei, ei.length + additionalInput.length);
|
|
147 |
System.arraycopy(additionalInput, 0, ei,
|
|
148 |
ei.length - additionalInput.length, additionalInput.length);
|
|
149 |
}
|
|
150 |
hashReseedInternal(ei);
|
|
151 |
}
|
|
152 |
|
|
153 |
protected abstract void hashReseedInternal(byte[] seed);
|
|
154 |
}
|