Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/provider/HmacDrbg.java
41159 views
1
/*
2
* Copyright (c) 2016, 2020, 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.Mac;
29
import javax.crypto.spec.SecretKeySpec;
30
import java.security.InvalidKeyException;
31
import java.security.NoSuchAlgorithmException;
32
import java.security.NoSuchProviderException;
33
import java.security.SecureRandomParameters;
34
import java.util.Arrays;
35
import java.util.Collections;
36
import java.util.HexFormat;
37
import java.util.List;
38
39
public class HmacDrbg extends AbstractHashDrbg {
40
41
private Mac mac;
42
43
private String macAlg;
44
45
private byte[] v;
46
private byte[] k;
47
48
public HmacDrbg(SecureRandomParameters params) {
49
mechName = "HMAC_DRBG";
50
configure(params);
51
}
52
53
private void status() {
54
if (debug != null) {
55
debug.println(this, "V = " + HexFormat.of().formatHex(v));
56
debug.println(this, "Key = " + HexFormat.of().formatHex(k));
57
debug.println(this, "reseed counter = " + reseedCounter);
58
}
59
}
60
61
// 800-90Ar1 10.1.2.2: HMAC_DRBG Update Process
62
private void update(List<byte[]> inputs) {
63
try {
64
// Step 1. K = HMAC (K, V || 0x00 || provided_data).
65
mac.init(new SecretKeySpec(k, macAlg));
66
mac.update(v);
67
mac.update((byte) 0);
68
for (byte[] input: inputs) {
69
mac.update(input);
70
}
71
k = mac.doFinal();
72
73
// Step 2. V = HMAC (K, V).
74
mac.init(new SecretKeySpec(k, macAlg));
75
v = mac.doFinal(v);
76
77
if (!inputs.isEmpty()) {
78
// Step 4. K = HMAC (K, V || 0x01 || provided_data).
79
mac.update(v);
80
mac.update((byte) 1);
81
for (byte[] input: inputs) {
82
mac.update(input);
83
}
84
k = mac.doFinal();
85
86
// Step 5. V=HMAC(K,V).
87
mac.init(new SecretKeySpec(k, macAlg));
88
v = mac.doFinal(v);
89
} // else Step 3
90
91
// Step 6. Return
92
} catch (InvalidKeyException e) {
93
throw new InternalError(e);
94
}
95
}
96
97
/**
98
* This call, used by the constructors, instantiates the digest.
99
*/
100
@Override
101
protected void initEngine() {
102
macAlg = "HmacSHA" + algorithm.substring(4);
103
try {
104
/*
105
* Use the local SunJCE implementation to avoid native
106
* performance overhead.
107
*/
108
mac = Mac.getInstance(macAlg, "SunJCE");
109
} catch (NoSuchProviderException | NoSuchAlgorithmException e) {
110
// Fallback to any available.
111
try {
112
mac = Mac.getInstance(macAlg);
113
} catch (NoSuchAlgorithmException exc) {
114
throw new InternalError(
115
"internal error: " + macAlg + " not available.", exc);
116
}
117
}
118
}
119
120
// This method is used by both instantiation and reseeding.
121
@Override
122
protected final synchronized void hashReseedInternal(List<byte[]> input) {
123
124
// 800-90Ar1 10.1.2.3: Instantiate Process.
125
// 800-90Ar1 10.1.2.4: Reseed Process.
126
if (v == null) {
127
k = new byte[outLen];
128
v = new byte[outLen];
129
Arrays.fill(v, (byte) 1);
130
}
131
132
// Step 2: HMAC_DRBG_Update
133
update(input);
134
135
// Step 3: reseed_counter = 1.
136
reseedCounter = 1;
137
//status();
138
139
// Step 4: Return
140
}
141
142
/**
143
* Generates a user-specified number of random bytes.
144
*
145
* @param result the array to be filled in with random bytes.
146
*/
147
@Override
148
public synchronized void generateAlgorithm(
149
byte[] result, byte[] additionalInput) {
150
151
if (debug != null) {
152
debug.println(this, "generateAlgorithm");
153
}
154
155
// 800-90Ar1 10.1.2.5: HMAC_DRBG_Generate Process
156
157
// Step 1: Check reseed_counter. Will not fail. Already checked in
158
// AbstractDrbg#engineNextBytes.
159
160
// Step 2. HMAC_DRBG_Update
161
if (additionalInput != null) {
162
update(Collections.singletonList(additionalInput));
163
}
164
165
// Step 3. temp = Null.
166
int pos = 0;
167
int len = result.length;
168
169
// Step 4. Loop
170
while (len > 0) {
171
// Step 4.1 V = HMAC (Key, V).
172
try {
173
mac.init(new SecretKeySpec(k, macAlg));
174
} catch (InvalidKeyException e) {
175
throw new InternalError(e);
176
}
177
v = mac.doFinal(v);
178
// Step 4.2 temp = temp || V.
179
System.arraycopy(v, 0, result, pos,
180
len > outLen ? outLen : len);
181
182
len -= outLen;
183
if (len <= 0) {
184
// shortcut, so that pos needn't be updated
185
break;
186
}
187
pos += outLen;
188
}
189
190
// Step 5: No need to truncate
191
192
// Step 6. HMAC_DRBG_Update (additional_input, Key, V).
193
if (additionalInput != null) {
194
update(Collections.singletonList(additionalInput));
195
} else {
196
update(Collections.emptyList());
197
}
198
199
// Step 7. reseed_counter = reseed_counter + 1.
200
reseedCounter++;
201
202
//status();
203
204
// Step 8. Return
205
}
206
}
207
208