Skip to content

Commit d46f6f5

Browse files
committed
8256523: Streamline Java SHA2 implementation
Reviewed-by: valeriep
1 parent 1aa90ac commit d46f6f5

File tree

1 file changed

+45
-82
lines changed
  • src/java.base/share/classes/sun/security/provider

1 file changed

+45
-82
lines changed

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

Lines changed: 45 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -113,84 +113,6 @@ void implDigest(byte[] out, int ofs) {
113113
i2bBig(state, 0, out, ofs, engineGetDigestLength());
114114
}
115115

116-
/**
117-
* logical function ch(x,y,z) as defined in spec:
118-
* @return (x and y) xor ((complement x) and z)
119-
* @param x int
120-
* @param y int
121-
* @param z int
122-
*/
123-
private static int lf_ch(int x, int y, int z) {
124-
return (x & y) ^ ((~x) & z);
125-
}
126-
127-
/**
128-
* logical function maj(x,y,z) as defined in spec:
129-
* @return (x and y) xor (x and z) xor (y and z)
130-
* @param x int
131-
* @param y int
132-
* @param z int
133-
*/
134-
private static int lf_maj(int x, int y, int z) {
135-
return (x & y) ^ (x & z) ^ (y & z);
136-
}
137-
138-
/**
139-
* logical function R(x,s) - right shift
140-
* @return x right shift for s times
141-
* @param x int
142-
* @param s int
143-
*/
144-
private static int lf_R( int x, int s ) {
145-
return (x >>> s);
146-
}
147-
148-
/**
149-
* logical function S(x,s) - right rotation
150-
* @return x circular right shift for s times
151-
* @param x int
152-
* @param s int
153-
*/
154-
private static int lf_S(int x, int s) {
155-
return (x >>> s) | (x << (32 - s));
156-
}
157-
158-
/**
159-
* logical function sigma0(x) - xor of results of right rotations
160-
* @return S(x,2) xor S(x,13) xor S(x,22)
161-
* @param x int
162-
*/
163-
private static int lf_sigma0(int x) {
164-
return lf_S(x, 2) ^ lf_S(x, 13) ^ lf_S(x, 22);
165-
}
166-
167-
/**
168-
* logical function sigma1(x) - xor of results of right rotations
169-
* @return S(x,6) xor S(x,11) xor S(x,25)
170-
* @param x int
171-
*/
172-
private static int lf_sigma1(int x) {
173-
return lf_S( x, 6 ) ^ lf_S( x, 11 ) ^ lf_S( x, 25 );
174-
}
175-
176-
/**
177-
* logical function delta0(x) - xor of results of right shifts/rotations
178-
* @return int
179-
* @param x int
180-
*/
181-
private static int lf_delta0(int x) {
182-
return lf_S(x, 7) ^ lf_S(x, 18) ^ lf_R(x, 3);
183-
}
184-
185-
/**
186-
* logical function delta1(x) - xor of results of right shifts/rotations
187-
* @return int
188-
* @param x int
189-
*/
190-
private static int lf_delta1(int x) {
191-
return lf_S(x, 17) ^ lf_S(x, 19) ^ lf_R(x, 10);
192-
}
193-
194116
/**
195117
* Process the current block to update the state variable state.
196118
*/
@@ -219,8 +141,27 @@ private void implCompress0(byte[] buf, int ofs) {
219141
// The first 16 ints are from the byte stream, compute the rest of
220142
// the W[]'s
221143
for (int t = 16; t < ITERATION; t++) {
222-
W[t] = lf_delta1(W[t-2]) + W[t-7] + lf_delta0(W[t-15])
223-
+ W[t-16];
144+
int W_t2 = W[t - 2];
145+
int W_t15 = W[t - 15];
146+
147+
// S(x,s) is right rotation of x by s positions:
148+
// S(x,s) = (x >>> s) | (x << (32 - s))
149+
// R(x,s) is right shift of x by s positions:
150+
// R(x,s) = (x >>> s)
151+
152+
// delta0(x) = S(x, 7) ^ S(x, 18) ^ R(x, 3)
153+
int delta0_W_t15 =
154+
((W_t15 >>> 7) | (W_t15 << 25)) ^
155+
((W_t15 >>> 18) | (W_t15 << 14)) ^
156+
(W_t15 >>> 3);
157+
158+
// delta1(x) = S(x, 17) ^ S(x, 19) ^ R(x, 10)
159+
int delta1_W_t2 =
160+
((W_t2 >>> 17) | (W_t2 << 15)) ^
161+
((W_t2 >>> 19) | (W_t2 << 13)) ^
162+
(W_t2 >>> 10);
163+
164+
W[t] = delta0_W_t15 + delta1_W_t2 + W[t-7] + W[t-16];
224165
}
225166

226167
int a = state[0];
@@ -233,8 +174,29 @@ private void implCompress0(byte[] buf, int ofs) {
233174
int h = state[7];
234175

235176
for (int i = 0; i < ITERATION; i++) {
236-
int T1 = h + lf_sigma1(e) + lf_ch(e,f,g) + ROUND_CONSTS[i] + W[i];
237-
int T2 = lf_sigma0(a) + lf_maj(a,b,c);
177+
// S(x,s) is right rotation of x by s positions:
178+
// S(x,s) = (x >>> s) | (x << (32 - s))
179+
180+
// sigma0(x) = S(x,2) xor S(x,13) xor S(x,22)
181+
int sigma0_a =
182+
((a >>> 2) | (a << 30)) ^
183+
((a >>> 13) | (a << 19)) ^
184+
((a >>> 22) | (a << 10));
185+
186+
// sigma1(x) = S(x,6) xor S(x,11) xor S(x,25)
187+
int sigma1_e =
188+
((e >>> 6) | (e << 26)) ^
189+
((e >>> 11) | (e << 21)) ^
190+
((e >>> 25) | (e << 7));
191+
192+
// ch(x,y,z) = (x and y) xor ((complement x) and z)
193+
int ch_efg = (e & f) ^ ((~e) & g);
194+
195+
// maj(x,y,z) = (x and y) xor (x and z) xor (y and z)
196+
int maj_abc = (a & b) ^ (a & c) ^ (b & c);
197+
198+
int T1 = h + sigma1_e + ch_efg + ROUND_CONSTS[i] + W[i];
199+
int T2 = sigma0_a + maj_abc;
238200
h = g;
239201
g = f;
240202
f = e;
@@ -244,6 +206,7 @@ private void implCompress0(byte[] buf, int ofs) {
244206
b = a;
245207
a = T1 + T2;
246208
}
209+
247210
state[0] += a;
248211
state[1] += b;
249212
state[2] += c;

0 commit comments

Comments
 (0)