Skip to content

Commit 35c9da7

Browse files
committedJan 20, 2021
8259498: Reduce overhead of MD5 and SHA digests
Reviewed-by: valeriep
1 parent 27cc62a commit 35c9da7

File tree

8 files changed

+305
-576
lines changed

8 files changed

+305
-576
lines changed
 

‎src/java.base/share/classes/sun/security/provider/ByteArrayAccess.java

+79-409
Large diffs are not rendered by default.

‎src/java.base/share/classes/sun/security/provider/MD4.java

+65-56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,8 +47,6 @@ public final class MD4 extends DigestBase {
4747

4848
// state of this object
4949
private int[] state;
50-
// temporary buffer, used by implCompress()
51-
private int[] x;
5250

5351
// rotation constants
5452
private static final int S11 = 3;
@@ -93,15 +91,13 @@ public static MessageDigest getInstance() {
9391
public MD4() {
9492
super("MD4", 16, 64);
9593
state = new int[4];
96-
x = new int[16];
9794
resetHashes();
9895
}
9996

10097
// clone this object
10198
public Object clone() throws CloneNotSupportedException {
10299
MD4 copy = (MD4) super.clone();
103100
copy.state = copy.state.clone();
104-
copy.x = new int[16];
105101
return copy;
106102
}
107103

@@ -111,8 +107,6 @@ public Object clone() throws CloneNotSupportedException {
111107
void implReset() {
112108
// Load magic initialization constants.
113109
resetHashes();
114-
// clear out old data
115-
Arrays.fill(x, 0);
116110
}
117111

118112
private void resetHashes() {
@@ -162,66 +156,81 @@ private static int HH(int a, int b, int c, int d, int x, int s) {
162156
* bytes from the buffer, beginning at the specified offset.
163157
*/
164158
void implCompress(byte[] buf, int ofs) {
165-
b2iLittle64(buf, ofs, x);
159+
int x0 = (int) LE.INT_ARRAY.get(buf, ofs);
160+
int x1 = (int) LE.INT_ARRAY.get(buf, ofs + 4);
161+
int x2 = (int) LE.INT_ARRAY.get(buf, ofs + 8);
162+
int x3 = (int) LE.INT_ARRAY.get(buf, ofs + 12);
163+
int x4 = (int) LE.INT_ARRAY.get(buf, ofs + 16);
164+
int x5 = (int) LE.INT_ARRAY.get(buf, ofs + 20);
165+
int x6 = (int) LE.INT_ARRAY.get(buf, ofs + 24);
166+
int x7 = (int) LE.INT_ARRAY.get(buf, ofs + 28);
167+
int x8 = (int) LE.INT_ARRAY.get(buf, ofs + 32);
168+
int x9 = (int) LE.INT_ARRAY.get(buf, ofs + 36);
169+
int x10 = (int) LE.INT_ARRAY.get(buf, ofs + 40);
170+
int x11 = (int) LE.INT_ARRAY.get(buf, ofs + 44);
171+
int x12 = (int) LE.INT_ARRAY.get(buf, ofs + 48);
172+
int x13 = (int) LE.INT_ARRAY.get(buf, ofs + 52);
173+
int x14 = (int) LE.INT_ARRAY.get(buf, ofs + 56);
174+
int x15 = (int) LE.INT_ARRAY.get(buf, ofs + 60);
166175

167176
int a = state[0];
168177
int b = state[1];
169178
int c = state[2];
170179
int d = state[3];
171180

172181
/* Round 1 */
173-
a = FF (a, b, c, d, x[ 0], S11); /* 1 */
174-
d = FF (d, a, b, c, x[ 1], S12); /* 2 */
175-
c = FF (c, d, a, b, x[ 2], S13); /* 3 */
176-
b = FF (b, c, d, a, x[ 3], S14); /* 4 */
177-
a = FF (a, b, c, d, x[ 4], S11); /* 5 */
178-
d = FF (d, a, b, c, x[ 5], S12); /* 6 */
179-
c = FF (c, d, a, b, x[ 6], S13); /* 7 */
180-
b = FF (b, c, d, a, x[ 7], S14); /* 8 */
181-
a = FF (a, b, c, d, x[ 8], S11); /* 9 */
182-
d = FF (d, a, b, c, x[ 9], S12); /* 10 */
183-
c = FF (c, d, a, b, x[10], S13); /* 11 */
184-
b = FF (b, c, d, a, x[11], S14); /* 12 */
185-
a = FF (a, b, c, d, x[12], S11); /* 13 */
186-
d = FF (d, a, b, c, x[13], S12); /* 14 */
187-
c = FF (c, d, a, b, x[14], S13); /* 15 */
188-
b = FF (b, c, d, a, x[15], S14); /* 16 */
182+
a = FF (a, b, c, d, x0, S11); /* 1 */
183+
d = FF (d, a, b, c, x1, S12); /* 2 */
184+
c = FF (c, d, a, b, x2, S13); /* 3 */
185+
b = FF (b, c, d, a, x3, S14); /* 4 */
186+
a = FF (a, b, c, d, x4, S11); /* 5 */
187+
d = FF (d, a, b, c, x5, S12); /* 6 */
188+
c = FF (c, d, a, b, x6, S13); /* 7 */
189+
b = FF (b, c, d, a, x7, S14); /* 8 */
190+
a = FF (a, b, c, d, x8, S11); /* 9 */
191+
d = FF (d, a, b, c, x9, S12); /* 10 */
192+
c = FF (c, d, a, b, x10, S13); /* 11 */
193+
b = FF (b, c, d, a, x11, S14); /* 12 */
194+
a = FF (a, b, c, d, x12, S11); /* 13 */
195+
d = FF (d, a, b, c, x13, S12); /* 14 */
196+
c = FF (c, d, a, b, x14, S13); /* 15 */
197+
b = FF (b, c, d, a, x15, S14); /* 16 */
189198

190199
/* Round 2 */
191-
a = GG (a, b, c, d, x[ 0], S21); /* 17 */
192-
d = GG (d, a, b, c, x[ 4], S22); /* 18 */
193-
c = GG (c, d, a, b, x[ 8], S23); /* 19 */
194-
b = GG (b, c, d, a, x[12], S24); /* 20 */
195-
a = GG (a, b, c, d, x[ 1], S21); /* 21 */
196-
d = GG (d, a, b, c, x[ 5], S22); /* 22 */
197-
c = GG (c, d, a, b, x[ 9], S23); /* 23 */
198-
b = GG (b, c, d, a, x[13], S24); /* 24 */
199-
a = GG (a, b, c, d, x[ 2], S21); /* 25 */
200-
d = GG (d, a, b, c, x[ 6], S22); /* 26 */
201-
c = GG (c, d, a, b, x[10], S23); /* 27 */
202-
b = GG (b, c, d, a, x[14], S24); /* 28 */
203-
a = GG (a, b, c, d, x[ 3], S21); /* 29 */
204-
d = GG (d, a, b, c, x[ 7], S22); /* 30 */
205-
c = GG (c, d, a, b, x[11], S23); /* 31 */
206-
b = GG (b, c, d, a, x[15], S24); /* 32 */
200+
a = GG (a, b, c, d, x0, S21); /* 17 */
201+
d = GG (d, a, b, c, x4, S22); /* 18 */
202+
c = GG (c, d, a, b, x8, S23); /* 19 */
203+
b = GG (b, c, d, a, x12, S24); /* 20 */
204+
a = GG (a, b, c, d, x1, S21); /* 21 */
205+
d = GG (d, a, b, c, x5, S22); /* 22 */
206+
c = GG (c, d, a, b, x9, S23); /* 23 */
207+
b = GG (b, c, d, a, x13, S24); /* 24 */
208+
a = GG (a, b, c, d, x2, S21); /* 25 */
209+
d = GG (d, a, b, c, x6, S22); /* 26 */
210+
c = GG (c, d, a, b, x10, S23); /* 27 */
211+
b = GG (b, c, d, a, x14, S24); /* 28 */
212+
a = GG (a, b, c, d, x3, S21); /* 29 */
213+
d = GG (d, a, b, c, x7, S22); /* 30 */
214+
c = GG (c, d, a, b, x11, S23); /* 31 */
215+
b = GG (b, c, d, a, x15, S24); /* 32 */
207216

208217
/* Round 3 */
209-
a = HH (a, b, c, d, x[ 0], S31); /* 33 */
210-
d = HH (d, a, b, c, x[ 8], S32); /* 34 */
211-
c = HH (c, d, a, b, x[ 4], S33); /* 35 */
212-
b = HH (b, c, d, a, x[12], S34); /* 36 */
213-
a = HH (a, b, c, d, x[ 2], S31); /* 37 */
214-
d = HH (d, a, b, c, x[10], S32); /* 38 */
215-
c = HH (c, d, a, b, x[ 6], S33); /* 39 */
216-
b = HH (b, c, d, a, x[14], S34); /* 40 */
217-
a = HH (a, b, c, d, x[ 1], S31); /* 41 */
218-
d = HH (d, a, b, c, x[ 9], S32); /* 42 */
219-
c = HH (c, d, a, b, x[ 5], S33); /* 43 */
220-
b = HH (b, c, d, a, x[13], S34); /* 44 */
221-
a = HH (a, b, c, d, x[ 3], S31); /* 45 */
222-
d = HH (d, a, b, c, x[11], S32); /* 46 */
223-
c = HH (c, d, a, b, x[ 7], S33); /* 47 */
224-
b = HH (b, c, d, a, x[15], S34); /* 48 */
218+
a = HH (a, b, c, d, x0, S31); /* 33 */
219+
d = HH (d, a, b, c, x8, S32); /* 34 */
220+
c = HH (c, d, a, b, x4, S33); /* 35 */
221+
b = HH (b, c, d, a, x12, S34); /* 36 */
222+
a = HH (a, b, c, d, x2, S31); /* 37 */
223+
d = HH (d, a, b, c, x10, S32); /* 38 */
224+
c = HH (c, d, a, b, x6, S33); /* 39 */
225+
b = HH (b, c, d, a, x14, S34); /* 40 */
226+
a = HH (a, b, c, d, x1, S31); /* 41 */
227+
d = HH (d, a, b, c, x9, S32); /* 42 */
228+
c = HH (c, d, a, b, x5, S33); /* 43 */
229+
b = HH (b, c, d, a, x13, S34); /* 44 */
230+
a = HH (a, b, c, d, x3, S31); /* 45 */
231+
d = HH (d, a, b, c, x11, S32); /* 46 */
232+
c = HH (c, d, a, b, x7, S33); /* 47 */
233+
b = HH (b, c, d, a, x15, S34); /* 48 */
225234

226235
state[0] += a;
227236
state[1] += b;

‎src/java.base/share/classes/sun/security/provider/MD5.java

+90-79
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,9 @@
2525

2626
package sun.security.provider;
2727

28+
import java.lang.invoke.MethodHandles;
29+
import java.lang.invoke.VarHandle;
30+
import java.nio.ByteOrder;
2831
import java.util.Arrays;
2932
import java.util.Objects;
3033

@@ -44,8 +47,6 @@ public final class MD5 extends DigestBase {
4447

4548
// state of this object
4649
private int[] state;
47-
// temporary buffer, used by implCompress()
48-
private int[] x;
4950

5051
// rotation constants
5152
private static final int S11 = 7;
@@ -69,15 +70,13 @@ public final class MD5 extends DigestBase {
6970
public MD5() {
7071
super("MD5", 16, 64);
7172
state = new int[4];
72-
x = new int[16];
73-
resetHashes();
73+
implReset();
7474
}
7575

7676
// clone this object
7777
public Object clone() throws CloneNotSupportedException {
7878
MD5 copy = (MD5) super.clone();
7979
copy.state = copy.state.clone();
80-
copy.x = new int[16];
8180
return copy;
8281
}
8382

@@ -86,12 +85,6 @@ public Object clone() throws CloneNotSupportedException {
8685
*/
8786
void implReset() {
8887
// Load magic initialization constants.
89-
resetHashes();
90-
// clear out old data
91-
Arrays.fill(x, 0);
92-
}
93-
94-
private void resetHashes() {
9588
state[0] = 0x67452301;
9689
state[1] = 0xefcdab89;
9790
state[2] = 0x98badcfe;
@@ -156,11 +149,12 @@ void implCompress(byte[] buf, int ofs) {
156149
private void implCompressCheck(byte[] buf, int ofs) {
157150
Objects.requireNonNull(buf);
158151

159-
// The checks performed by the method 'b2iBig64'
160-
// are sufficient for the case when the method
152+
// These checks are sufficient for the case when the method
161153
// 'implCompressImpl' is replaced with a compiler
162154
// intrinsic.
163-
b2iLittle64(buf, ofs, x);
155+
if ((ofs < 0) || ((buf.length - ofs) < 64)) {
156+
throw new ArrayIndexOutOfBoundsException();
157+
}
164158
}
165159

166160
// The method 'implCompress0 seems not to use its parameters.
@@ -175,77 +169,94 @@ void implCompress0(byte[] buf, int ofs) {
175169
int c = state[2];
176170
int d = state[3];
177171

172+
int x0 = (int) LE.INT_ARRAY.get(buf, ofs);
173+
int x1 = (int) LE.INT_ARRAY.get(buf, ofs + 4);
174+
int x2 = (int) LE.INT_ARRAY.get(buf, ofs + 8);
175+
int x3 = (int) LE.INT_ARRAY.get(buf, ofs + 12);
176+
int x4 = (int) LE.INT_ARRAY.get(buf, ofs + 16);
177+
int x5 = (int) LE.INT_ARRAY.get(buf, ofs + 20);
178+
int x6 = (int) LE.INT_ARRAY.get(buf, ofs + 24);
179+
int x7 = (int) LE.INT_ARRAY.get(buf, ofs + 28);
180+
int x8 = (int) LE.INT_ARRAY.get(buf, ofs + 32);
181+
int x9 = (int) LE.INT_ARRAY.get(buf, ofs + 36);
182+
int x10 = (int) LE.INT_ARRAY.get(buf, ofs + 40);
183+
int x11 = (int) LE.INT_ARRAY.get(buf, ofs + 44);
184+
int x12 = (int) LE.INT_ARRAY.get(buf, ofs + 48);
185+
int x13 = (int) LE.INT_ARRAY.get(buf, ofs + 52);
186+
int x14 = (int) LE.INT_ARRAY.get(buf, ofs + 56);
187+
int x15 = (int) LE.INT_ARRAY.get(buf, ofs + 60);
188+
178189
/* Round 1 */
179-
a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
180-
d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
181-
c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
182-
b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
183-
a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
184-
d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
185-
c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
186-
b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
187-
a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
188-
d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
189-
c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
190-
b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
191-
a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
192-
d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
193-
c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
194-
b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
190+
a = FF ( a, b, c, d, x0, S11, 0xd76aa478); /* 1 */
191+
d = FF ( d, a, b, c, x1, S12, 0xe8c7b756); /* 2 */
192+
c = FF ( c, d, a, b, x2, S13, 0x242070db); /* 3 */
193+
b = FF ( b, c, d, a, x3, S14, 0xc1bdceee); /* 4 */
194+
a = FF ( a, b, c, d, x4, S11, 0xf57c0faf); /* 5 */
195+
d = FF ( d, a, b, c, x5, S12, 0x4787c62a); /* 6 */
196+
c = FF ( c, d, a, b, x6, S13, 0xa8304613); /* 7 */
197+
b = FF ( b, c, d, a, x7, S14, 0xfd469501); /* 8 */
198+
a = FF ( a, b, c, d, x8, S11, 0x698098d8); /* 9 */
199+
d = FF ( d, a, b, c, x9, S12, 0x8b44f7af); /* 10 */
200+
c = FF ( c, d, a, b, x10, S13, 0xffff5bb1); /* 11 */
201+
b = FF ( b, c, d, a, x11, S14, 0x895cd7be); /* 12 */
202+
a = FF ( a, b, c, d, x12, S11, 0x6b901122); /* 13 */
203+
d = FF ( d, a, b, c, x13, S12, 0xfd987193); /* 14 */
204+
c = FF ( c, d, a, b, x14, S13, 0xa679438e); /* 15 */
205+
b = FF ( b, c, d, a, x15, S14, 0x49b40821); /* 16 */
195206

196207
/* Round 2 */
197-
a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
198-
d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
199-
c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
200-
b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
201-
a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
202-
d = GG ( d, a, b, c, x[10], S22, 0x2441453); /* 22 */
203-
c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
204-
b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
205-
a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
206-
d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
207-
c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
208-
b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
209-
a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
210-
d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
211-
c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
212-
b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
208+
a = GG ( a, b, c, d, x1, S21, 0xf61e2562); /* 17 */
209+
d = GG ( d, a, b, c, x6, S22, 0xc040b340); /* 18 */
210+
c = GG ( c, d, a, b, x11, S23, 0x265e5a51); /* 19 */
211+
b = GG ( b, c, d, a, x0, S24, 0xe9b6c7aa); /* 20 */
212+
a = GG ( a, b, c, d, x5, S21, 0xd62f105d); /* 21 */
213+
d = GG ( d, a, b, c, x10, S22, 0x2441453); /* 22 */
214+
c = GG ( c, d, a, b, x15, S23, 0xd8a1e681); /* 23 */
215+
b = GG ( b, c, d, a, x4, S24, 0xe7d3fbc8); /* 24 */
216+
a = GG ( a, b, c, d, x9, S21, 0x21e1cde6); /* 25 */
217+
d = GG ( d, a, b, c, x14, S22, 0xc33707d6); /* 26 */
218+
c = GG ( c, d, a, b, x3, S23, 0xf4d50d87); /* 27 */
219+
b = GG ( b, c, d, a, x8, S24, 0x455a14ed); /* 28 */
220+
a = GG ( a, b, c, d, x13, S21, 0xa9e3e905); /* 29 */
221+
d = GG ( d, a, b, c, x2, S22, 0xfcefa3f8); /* 30 */
222+
c = GG ( c, d, a, b, x7, S23, 0x676f02d9); /* 31 */
223+
b = GG ( b, c, d, a, x12, S24, 0x8d2a4c8a); /* 32 */
213224

214225
/* Round 3 */
215-
a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
216-
d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
217-
c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
218-
b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
219-
a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
220-
d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
221-
c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
222-
b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
223-
a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
224-
d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
225-
c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
226-
b = HH ( b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
227-
a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
228-
d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
229-
c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
230-
b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
226+
a = HH ( a, b, c, d, x5, S31, 0xfffa3942); /* 33 */
227+
d = HH ( d, a, b, c, x8, S32, 0x8771f681); /* 34 */
228+
c = HH ( c, d, a, b, x11, S33, 0x6d9d6122); /* 35 */
229+
b = HH ( b, c, d, a, x14, S34, 0xfde5380c); /* 36 */
230+
a = HH ( a, b, c, d, x1, S31, 0xa4beea44); /* 37 */
231+
d = HH ( d, a, b, c, x4, S32, 0x4bdecfa9); /* 38 */
232+
c = HH ( c, d, a, b, x7, S33, 0xf6bb4b60); /* 39 */
233+
b = HH ( b, c, d, a, x10, S34, 0xbebfbc70); /* 40 */
234+
a = HH ( a, b, c, d, x13, S31, 0x289b7ec6); /* 41 */
235+
d = HH ( d, a, b, c, x0, S32, 0xeaa127fa); /* 42 */
236+
c = HH ( c, d, a, b, x3, S33, 0xd4ef3085); /* 43 */
237+
b = HH ( b, c, d, a, x6, S34, 0x4881d05); /* 44 */
238+
a = HH ( a, b, c, d, x9, S31, 0xd9d4d039); /* 45 */
239+
d = HH ( d, a, b, c, x12, S32, 0xe6db99e5); /* 46 */
240+
c = HH ( c, d, a, b, x15, S33, 0x1fa27cf8); /* 47 */
241+
b = HH ( b, c, d, a, x2, S34, 0xc4ac5665); /* 48 */
231242

232243
/* Round 4 */
233-
a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
234-
d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
235-
c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
236-
b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
237-
a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
238-
d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
239-
c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
240-
b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
241-
a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
242-
d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
243-
c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
244-
b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
245-
a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
246-
d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
247-
c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
248-
b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
244+
a = II ( a, b, c, d, x0, S41, 0xf4292244); /* 49 */
245+
d = II ( d, a, b, c, x7, S42, 0x432aff97); /* 50 */
246+
c = II ( c, d, a, b, x14, S43, 0xab9423a7); /* 51 */
247+
b = II ( b, c, d, a, x5, S44, 0xfc93a039); /* 52 */
248+
a = II ( a, b, c, d, x12, S41, 0x655b59c3); /* 53 */
249+
d = II ( d, a, b, c, x3, S42, 0x8f0ccc92); /* 54 */
250+
c = II ( c, d, a, b, x10, S43, 0xffeff47d); /* 55 */
251+
b = II ( b, c, d, a, x1, S44, 0x85845dd1); /* 56 */
252+
a = II ( a, b, c, d, x8, S41, 0x6fa87e4f); /* 57 */
253+
d = II ( d, a, b, c, x15, S42, 0xfe2ce6e0); /* 58 */
254+
c = II ( c, d, a, b, x6, S43, 0xa3014314); /* 59 */
255+
b = II ( b, c, d, a, x13, S44, 0x4e0811a1); /* 60 */
256+
a = II ( a, b, c, d, x4, S41, 0xf7537e82); /* 61 */
257+
d = II ( d, a, b, c, x11, S42, 0xbd3af235); /* 62 */
258+
c = II ( c, d, a, b, x2, S43, 0x2ad7d2bb); /* 63 */
259+
b = II ( b, c, d, a, x9, S44, 0xeb86d391); /* 64 */
249260

250261
state[0] += a;
251262
state[1] += b;

‎src/java.base/share/classes/sun/security/provider/SHA.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,7 +62,6 @@ public final class SHA extends DigestBase {
6262
public SHA() {
6363
super("SHA-1", 20, 64);
6464
state = new int[5];
65-
W = new int[80];
6665
resetHashes();
6766
}
6867

@@ -72,7 +71,7 @@ public SHA() {
7271
public Object clone() throws CloneNotSupportedException {
7372
SHA copy = (SHA) super.clone();
7473
copy.state = copy.state.clone();
75-
copy.W = new int[80];
74+
copy.W = null;
7675
return copy;
7776
}
7877

@@ -83,7 +82,9 @@ void implReset() {
8382
// Load magic initialization constants.
8483
resetHashes();
8584
// clear out old data
86-
Arrays.fill(W, 0);
85+
if (W != null) {
86+
Arrays.fill(W, 0);
87+
}
8788
}
8889

8990
private void resetHashes() {
@@ -132,11 +133,12 @@ void implCompress(byte[] buf, int ofs) {
132133
private void implCompressCheck(byte[] buf, int ofs) {
133134
Objects.requireNonNull(buf);
134135

135-
// The checks performed by the method 'b2iBig64'
136-
// are sufficient for the case when the method
137-
// 'implCompress0' is replaced with a compiler
138-
// intrinsic.
139-
b2iBig64(buf, ofs, W);
136+
// Checks similar to those performed by the method 'b2iBig64'
137+
// are sufficient for the case when the method 'implCompress0' is
138+
// replaced with a compiler intrinsic.
139+
if (ofs < 0 || (buf.length - ofs) < 64) {
140+
throw new ArrayIndexOutOfBoundsException();
141+
}
140142
}
141143

142144
// The method 'implCompress0 seems not to use its parameters.
@@ -146,6 +148,10 @@ private void implCompressCheck(byte[] buf, int ofs) {
146148
// must be passed as parameter to the method.
147149
@IntrinsicCandidate
148150
private void implCompress0(byte[] buf, int ofs) {
151+
if (W == null) {
152+
W = new int[80];
153+
}
154+
b2iBig64(buf, ofs, W);
149155
// The first 16 ints have the byte stream, compute the rest of
150156
// the buffer
151157
for (int t = 16; t <= 79; t++) {

‎src/java.base/share/classes/sun/security/provider/SHA2.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -83,7 +83,6 @@ abstract class SHA2 extends DigestBase {
8383
super(name, digestLength, 64);
8484
this.initialHashes = initialHashes;
8585
state = new int[8];
86-
W = new int[64];
8786
resetHashes();
8887
}
8988

@@ -92,7 +91,9 @@ abstract class SHA2 extends DigestBase {
9291
*/
9392
void implReset() {
9493
resetHashes();
95-
Arrays.fill(W, 0);
94+
if (W != null) {
95+
Arrays.fill(W, 0);
96+
}
9697
}
9798

9899
private void resetHashes() {
@@ -124,11 +125,12 @@ void implCompress(byte[] buf, int ofs) {
124125
private void implCompressCheck(byte[] buf, int ofs) {
125126
Objects.requireNonNull(buf);
126127

127-
// The checks performed by the method 'b2iBig64'
128-
// are sufficient for the case when the method
129-
// 'implCompressImpl' is replaced with a compiler
130-
// intrinsic.
131-
b2iBig64(buf, ofs, W);
128+
// Checks similar to those performed by the method 'b2iBig64'
129+
// are sufficient for the case when the method 'implCompress0' is
130+
// replaced with a compiler intrinsic.
131+
if (ofs < 0 || (buf.length - ofs) < 64) {
132+
throw new ArrayIndexOutOfBoundsException();
133+
}
132134
}
133135

134136
// The method 'implCompressImpl' seems not to use its parameters.
@@ -138,6 +140,10 @@ private void implCompressCheck(byte[] buf, int ofs) {
138140
// must be passed as parameter to the method.
139141
@IntrinsicCandidate
140142
private void implCompress0(byte[] buf, int ofs) {
143+
if (W == null) {
144+
W = new int[64];
145+
}
146+
b2iBig64(buf, ofs, W);
141147
// The first 16 ints are from the byte stream, compute the rest of
142148
// the W[]'s
143149
for (int t = 16; t < ITERATION; t++) {
@@ -220,7 +226,7 @@ private void implCompress0(byte[] buf, int ofs) {
220226
public Object clone() throws CloneNotSupportedException {
221227
SHA2 copy = (SHA2) super.clone();
222228
copy.state = copy.state.clone();
223-
copy.W = new int[64];
229+
copy.W = null;
224230
return copy;
225231
}
226232

‎src/java.base/share/classes/sun/security/provider/SHA5.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -98,13 +98,14 @@ abstract class SHA5 extends DigestBase {
9898
super(name, digestLength, 128);
9999
this.initialHashes = initialHashes;
100100
state = new long[8];
101-
W = new long[80];
102101
resetHashes();
103102
}
104103

105104
final void implReset() {
106105
resetHashes();
107-
Arrays.fill(W, 0L);
106+
if (W != null) {
107+
Arrays.fill(W, 0L);
108+
}
108109
}
109110

110111
private void resetHashes() {
@@ -225,11 +226,12 @@ final void implCompress(byte[] buf, int ofs) {
225226
private void implCompressCheck(byte[] buf, int ofs) {
226227
Objects.requireNonNull(buf);
227228

228-
// The checks performed by the method 'b2iBig128'
229-
// are sufficient for the case when the method
230-
// 'implCompressImpl' is replaced with a compiler
231-
// intrinsic.
232-
b2lBig128(buf, ofs, W);
229+
// Checks similar to those performed by the method 'b2lBig128'
230+
// are sufficient for the case when the method 'implCompress0' is
231+
// replaced with a compiler intrinsic.
232+
if (ofs < 0 || (buf.length - ofs) < 128) {
233+
throw new ArrayIndexOutOfBoundsException();
234+
}
233235
}
234236

235237
// The method 'implCompressImpl' seems not to use its parameters.
@@ -239,6 +241,10 @@ private void implCompressCheck(byte[] buf, int ofs) {
239241
// must be passed as parameter to the method.
240242
@IntrinsicCandidate
241243
private final void implCompress0(byte[] buf, int ofs) {
244+
if (W == null) {
245+
W = new long[80];
246+
}
247+
b2lBig128(buf, ofs, W);
242248
// The first 16 longs are from the byte stream, compute the rest of
243249
// the W[]'s
244250
for (int t = 16; t < ITERATION; t++) {
@@ -280,7 +286,7 @@ private final void implCompress0(byte[] buf, int ofs) {
280286
public Object clone() throws CloneNotSupportedException {
281287
SHA5 copy = (SHA5) super.clone();
282288
copy.state = copy.state.clone();
283-
copy.W = new long[80];
289+
copy.W = null;
284290
return copy;
285291
}
286292

‎test/micro/org/openjdk/bench/java/security/MessageDigests.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -45,8 +45,8 @@
4545
*/
4646
@State(Scope.Thread)
4747
@OutputTimeUnit(TimeUnit.MILLISECONDS)
48-
@Warmup(iterations = 5)
49-
@Measurement(iterations = 10)
48+
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
49+
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
5050
@Fork(jvmArgsAppend = {"-Xms1024m", "-Xmx1024m", "-Xmn768m", "-XX:+UseParallelGC"}, value = 5)
5151
public class MessageDigests {
5252

@@ -77,4 +77,15 @@ public void setup() throws NoSuchAlgorithmException, DigestException, NoSuchProv
7777
public byte[] digest() throws DigestException {
7878
return digester.digest(inputBytes);
7979
}
80+
81+
@Benchmark
82+
public byte[] getAndDigest() throws DigestException, NoSuchAlgorithmException, NoSuchProviderException {
83+
MessageDigest md;
84+
if ("DEFAULT".equals(provider)) {
85+
md = MessageDigest.getInstance(digesterName);
86+
} else {
87+
md = MessageDigest.getInstance(digesterName, provider);
88+
}
89+
return md.digest(inputBytes);
90+
}
8091
}

‎test/micro/org/openjdk/bench/java/util/UUIDBench.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,8 @@ public class UUIDBench {
3737
@Param("20000")
3838
private int size;
3939

40+
private byte[][] uuidBytes;
41+
4042
private UUID[] uuids;
4143

4244
private String[] uuidStrings;
@@ -45,11 +47,14 @@ public class UUIDBench {
4547

4648
@Setup
4749
public void setup() {
50+
uuidBytes = new byte[size][];
4851
uuids = new UUID[size];
4952
uuidStrings = new String[size];
53+
java.util.Random r = new java.util.Random(0);
5054
for (int i = 0; i < this.uuidStrings.length; i++) {
5155
final UUID uuid = UUID.randomUUID();
52-
56+
this.uuidBytes[i] = new byte[16];
57+
r.nextBytes(this.uuidBytes[i]);
5358
this.uuids[i] = uuid;
5459
this.uuidStrings[i] = uuid.toString();
5560
}
@@ -72,4 +77,9 @@ public UUID fromString() {
7277
public String toString() {
7378
return uuids[index].toString();
7479
}
80+
81+
@Benchmark
82+
public UUID fromType3Bytes() {
83+
return UUID.nameUUIDFromBytes(uuidBytes[index]);
84+
}
7585
}

0 commit comments

Comments
 (0)
Please sign in to comment.