Skip to content

Commit

Permalink
4887998: Use Integer.rotateLeft() and rotateRight() in crypto impleme…
Browse files Browse the repository at this point in the history
…ntations

Reviewed-by: weijun
  • Loading branch information
mcpowers authored and wangweij committed Jul 18, 2022
1 parent 6c8d0e6 commit b65f7ec
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/java.base/share/classes/sun/security/provider/MD4.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -138,17 +138,17 @@ void implDigest(byte[] out, int ofs) {

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

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

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

/**
Expand Down
10 changes: 5 additions & 5 deletions src/java.base/share/classes/sun/security/provider/MD5.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -120,22 +120,22 @@ void implDigest(byte[] out, int ofs) {

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

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

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

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

/**
Expand Down
18 changes: 9 additions & 9 deletions src/java.base/share/classes/sun/security/provider/SHA.java
Expand Up @@ -156,7 +156,7 @@ private void implCompress0(byte[] buf, int ofs) {
// the buffer
for (int t = 16; t <= 79; t++) {
int temp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
W[t] = (temp << 1) | (temp >>> 31);
W[t] = Integer.rotateLeft(temp, 1);
}

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

// Round 1
for (int i = 0; i < 20; i++) {
int temp = ((a<<5) | (a>>>(32-5))) +
int temp = Integer.rotateLeft(a, 5) +
((b&c)|((~b)&d))+ e + W[i] + round1_kt;
e = d;
d = c;
c = ((b<<30) | (b>>>(32-30)));
c = Integer.rotateLeft(b, 30);
b = a;
a = temp;
}

// Round 2
for (int i = 20; i < 40; i++) {
int temp = ((a<<5) | (a>>>(32-5))) +
int temp = Integer.rotateLeft(a, 5) +
(b ^ c ^ d) + e + W[i] + round2_kt;
e = d;
d = c;
c = ((b<<30) | (b>>>(32-30)));
c = Integer.rotateLeft(b, 30);
b = a;
a = temp;
}

// Round 3
for (int i = 40; i < 60; i++) {
int temp = ((a<<5) | (a>>>(32-5))) +
int temp = Integer.rotateLeft(a, 5) +
((b&c)|(b&d)|(c&d)) + e + W[i] + round3_kt;
e = d;
d = c;
c = ((b<<30) | (b>>>(32-30)));
c = Integer.rotateLeft(b, 30);
b = a;
a = temp;
}

// Round 4
for (int i = 60; i < 80; i++) {
int temp = ((a<<5) | (a>>>(32-5))) +
int temp = Integer.rotateLeft(a, 5) +
(b ^ c ^ d) + e + W[i] + round4_kt;
e = d;
d = c;
c = ((b<<30) | (b>>>(32-30)));
c = Integer.rotateLeft(b, 30);
b = a;
a = temp;
}
Expand Down
22 changes: 11 additions & 11 deletions src/java.base/share/classes/sun/security/provider/SHA2.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -156,14 +156,14 @@ private void implCompress0(byte[] buf, int ofs) {

// delta0(x) = S(x, 7) ^ S(x, 18) ^ R(x, 3)
int delta0_W_t15 =
((W_t15 >>> 7) | (W_t15 << 25)) ^
((W_t15 >>> 18) | (W_t15 << 14)) ^
Integer.rotateRight(W_t15, 7) ^
Integer.rotateRight(W_t15, 18) ^
(W_t15 >>> 3);

// delta1(x) = S(x, 17) ^ S(x, 19) ^ R(x, 10)
int delta1_W_t2 =
((W_t2 >>> 17) | (W_t2 << 15)) ^
((W_t2 >>> 19) | (W_t2 << 13)) ^
Integer.rotateRight(W_t2, 17) ^
Integer.rotateRight(W_t2, 19) ^
(W_t2 >>> 10);

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

// sigma0(x) = S(x,2) xor S(x,13) xor S(x,22)
int sigma0_a =
((a >>> 2) | (a << 30)) ^
((a >>> 13) | (a << 19)) ^
((a >>> 22) | (a << 10));
Integer.rotateRight(a, 2) ^
Integer.rotateRight(a, 13) ^
Integer.rotateRight(a, 22);

// sigma1(x) = S(x,6) xor S(x,11) xor S(x,25)
int sigma1_e =
((e >>> 6) | (e << 26)) ^
((e >>> 11) | (e << 21)) ^
((e >>> 25) | (e << 7));
Integer.rotateRight(e, 6) ^
Integer.rotateRight(e, 11) ^
Integer.rotateRight(e, 25);

// ch(x,y,z) = (x and y) xor ((complement x) and z)
int ch_efg = (e & f) ^ ((~e) & g);
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/sun/security/provider/SHA5.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -173,7 +173,7 @@ private static long lf_R(long x, int s) {
* @param s int
*/
private static long lf_S(long x, int s) {
return (x >>> s) | (x << (64 - s));
return Long.rotateRight(x, s);
}

/**
Expand Down

1 comment on commit b65f7ec

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.