Skip to content

Commit b65f7ec

Browse files
mcpowerswangweij
authored andcommitted
4887998: Use Integer.rotateLeft() and rotateRight() in crypto implementations
Reviewed-by: weijun
1 parent 6c8d0e6 commit b65f7ec

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2022, 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
@@ -138,17 +138,17 @@ void implDigest(byte[] out, int ofs) {
138138

139139
private static int FF(int a, int b, int c, int d, int x, int s) {
140140
a += ((b & c) | ((~b) & d)) + x;
141-
return ((a << s) | (a >>> (32 - s)));
141+
return Integer.rotateLeft(a, s);
142142
}
143143

144144
private static int GG(int a, int b, int c, int d, int x, int s) {
145145
a += ((b & c) | (b & d) | (c & d)) + x + 0x5a827999;
146-
return ((a << s) | (a >>> (32 - s)));
146+
return Integer.rotateLeft(a, s);
147147
}
148148

149149
private static int HH(int a, int b, int c, int d, int x, int s) {
150150
a += ((b ^ c) ^ d) + x + 0x6ed9eba1;
151-
return ((a << s) | (a >>> (32 - s)));
151+
return Integer.rotateLeft(a, s);
152152
}
153153

154154
/**

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2022, 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
@@ -120,22 +120,22 @@ void implDigest(byte[] out, int ofs) {
120120

121121
private static int FF(int a, int b, int c, int d, int x, int s, int ac) {
122122
a += ((b & c) | ((~b) & d)) + x + ac;
123-
return ((a << s) | (a >>> (32 - s))) + b;
123+
return Integer.rotateLeft(a, s) + b;
124124
}
125125

126126
private static int GG(int a, int b, int c, int d, int x, int s, int ac) {
127127
a += ((b & d) | (c & (~d))) + x + ac;
128-
return ((a << s) | (a >>> (32 - s))) + b;
128+
return Integer.rotateLeft(a, s) + b;
129129
}
130130

131131
private static int HH(int a, int b, int c, int d, int x, int s, int ac) {
132132
a += ((b ^ c) ^ d) + x + ac;
133-
return ((a << s) | (a >>> (32 - s))) + b;
133+
return Integer.rotateLeft(a, s) + b;
134134
}
135135

136136
private static int II(int a, int b, int c, int d, int x, int s, int ac) {
137137
a += (c ^ (b | (~d))) + x + ac;
138-
return ((a << s) | (a >>> (32 - s))) + b;
138+
return Integer.rotateLeft(a, s) + b;
139139
}
140140

141141
/**

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private void implCompress0(byte[] buf, int ofs) {
156156
// the buffer
157157
for (int t = 16; t <= 79; t++) {
158158
int temp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
159-
W[t] = (temp << 1) | (temp >>> 31);
159+
W[t] = Integer.rotateLeft(temp, 1);
160160
}
161161

162162
int a = state[0];
@@ -167,44 +167,44 @@ private void implCompress0(byte[] buf, int ofs) {
167167

168168
// Round 1
169169
for (int i = 0; i < 20; i++) {
170-
int temp = ((a<<5) | (a>>>(32-5))) +
170+
int temp = Integer.rotateLeft(a, 5) +
171171
((b&c)|((~b)&d))+ e + W[i] + round1_kt;
172172
e = d;
173173
d = c;
174-
c = ((b<<30) | (b>>>(32-30)));
174+
c = Integer.rotateLeft(b, 30);
175175
b = a;
176176
a = temp;
177177
}
178178

179179
// Round 2
180180
for (int i = 20; i < 40; i++) {
181-
int temp = ((a<<5) | (a>>>(32-5))) +
181+
int temp = Integer.rotateLeft(a, 5) +
182182
(b ^ c ^ d) + e + W[i] + round2_kt;
183183
e = d;
184184
d = c;
185-
c = ((b<<30) | (b>>>(32-30)));
185+
c = Integer.rotateLeft(b, 30);
186186
b = a;
187187
a = temp;
188188
}
189189

190190
// Round 3
191191
for (int i = 40; i < 60; i++) {
192-
int temp = ((a<<5) | (a>>>(32-5))) +
192+
int temp = Integer.rotateLeft(a, 5) +
193193
((b&c)|(b&d)|(c&d)) + e + W[i] + round3_kt;
194194
e = d;
195195
d = c;
196-
c = ((b<<30) | (b>>>(32-30)));
196+
c = Integer.rotateLeft(b, 30);
197197
b = a;
198198
a = temp;
199199
}
200200

201201
// Round 4
202202
for (int i = 60; i < 80; i++) {
203-
int temp = ((a<<5) | (a>>>(32-5))) +
203+
int temp = Integer.rotateLeft(a, 5) +
204204
(b ^ c ^ d) + e + W[i] + round4_kt;
205205
e = d;
206206
d = c;
207-
c = ((b<<30) | (b>>>(32-30)));
207+
c = Integer.rotateLeft(b, 30);
208208
b = a;
209209
a = temp;
210210
}

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2022, 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
@@ -156,14 +156,14 @@ private void implCompress0(byte[] buf, int ofs) {
156156

157157
// delta0(x) = S(x, 7) ^ S(x, 18) ^ R(x, 3)
158158
int delta0_W_t15 =
159-
((W_t15 >>> 7) | (W_t15 << 25)) ^
160-
((W_t15 >>> 18) | (W_t15 << 14)) ^
159+
Integer.rotateRight(W_t15, 7) ^
160+
Integer.rotateRight(W_t15, 18) ^
161161
(W_t15 >>> 3);
162162

163163
// delta1(x) = S(x, 17) ^ S(x, 19) ^ R(x, 10)
164164
int delta1_W_t2 =
165-
((W_t2 >>> 17) | (W_t2 << 15)) ^
166-
((W_t2 >>> 19) | (W_t2 << 13)) ^
165+
Integer.rotateRight(W_t2, 17) ^
166+
Integer.rotateRight(W_t2, 19) ^
167167
(W_t2 >>> 10);
168168

169169
W[t] = delta0_W_t15 + delta1_W_t2 + W[t-7] + W[t-16];
@@ -184,15 +184,15 @@ private void implCompress0(byte[] buf, int ofs) {
184184

185185
// sigma0(x) = S(x,2) xor S(x,13) xor S(x,22)
186186
int sigma0_a =
187-
((a >>> 2) | (a << 30)) ^
188-
((a >>> 13) | (a << 19)) ^
189-
((a >>> 22) | (a << 10));
187+
Integer.rotateRight(a, 2) ^
188+
Integer.rotateRight(a, 13) ^
189+
Integer.rotateRight(a, 22);
190190

191191
// sigma1(x) = S(x,6) xor S(x,11) xor S(x,25)
192192
int sigma1_e =
193-
((e >>> 6) | (e << 26)) ^
194-
((e >>> 11) | (e << 21)) ^
195-
((e >>> 25) | (e << 7));
193+
Integer.rotateRight(e, 6) ^
194+
Integer.rotateRight(e, 11) ^
195+
Integer.rotateRight(e, 25);
196196

197197
// ch(x,y,z) = (x and y) xor ((complement x) and z)
198198
int ch_efg = (e & f) ^ ((~e) & g);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2022, 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
@@ -173,7 +173,7 @@ private static long lf_R(long x, int s) {
173173
* @param s int
174174
*/
175175
private static long lf_S(long x, int s) {
176-
return (x >>> s) | (x << (64 - s));
176+
return Long.rotateRight(x, s);
177177
}
178178

179179
/**

0 commit comments

Comments
 (0)