4887998: Use Integer.rotateLeft() and rotateRight() in crypto implementations

Reviewed-by: weijun
This commit is contained in:
Mark Powers 2022-07-18 20:50:54 +00:00 committed by Weijun Wang
parent 6c8d0e617f
commit b65f7ec2f1
5 changed files with 31 additions and 31 deletions

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -138,17 +138,17 @@ public final class MD4 extends DigestBase {
private static int FF(int a, int b, int c, int d, int x, int s) { private static int FF(int a, int b, int c, int d, int x, int s) {
a += ((b & c) | ((~b) & d)) + x; 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) { 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; 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) { private static int HH(int a, int b, int c, int d, int x, int s) {
a += ((b ^ c) ^ d) + x + 0x6ed9eba1; a += ((b ^ c) ^ d) + x + 0x6ed9eba1;
return ((a << s) | (a >>> (32 - s))); return Integer.rotateLeft(a, s);
} }
/** /**

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -120,22 +120,22 @@ public final class MD5 extends DigestBase {
private static int FF(int a, int b, int c, int d, int x, int s, int ac) { 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; 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) { 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; 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) { private static int HH(int a, int b, int c, int d, int x, int s, int ac) {
a += ((b ^ c) ^ d) + x + 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) { private static int II(int a, int b, int c, int d, int x, int s, int ac) {
a += (c ^ (b | (~d))) + x + ac; a += (c ^ (b | (~d))) + x + ac;
return ((a << s) | (a >>> (32 - s))) + b; return Integer.rotateLeft(a, s) + b;
} }
/** /**

View file

@ -156,7 +156,7 @@ public final class SHA extends DigestBase {
// the buffer // the buffer
for (int t = 16; t <= 79; t++) { for (int t = 16; t <= 79; t++) {
int temp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]; 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]; int a = state[0];
@ -167,44 +167,44 @@ public final class SHA extends DigestBase {
// Round 1 // Round 1
for (int i = 0; i < 20; i++) { 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; ((b&c)|((~b)&d))+ e + W[i] + round1_kt;
e = d; e = d;
d = c; d = c;
c = ((b<<30) | (b>>>(32-30))); c = Integer.rotateLeft(b, 30);
b = a; b = a;
a = temp; a = temp;
} }
// Round 2 // Round 2
for (int i = 20; i < 40; i++) { 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; (b ^ c ^ d) + e + W[i] + round2_kt;
e = d; e = d;
d = c; d = c;
c = ((b<<30) | (b>>>(32-30))); c = Integer.rotateLeft(b, 30);
b = a; b = a;
a = temp; a = temp;
} }
// Round 3 // Round 3
for (int i = 40; i < 60; i++) { 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; ((b&c)|(b&d)|(c&d)) + e + W[i] + round3_kt;
e = d; e = d;
d = c; d = c;
c = ((b<<30) | (b>>>(32-30))); c = Integer.rotateLeft(b, 30);
b = a; b = a;
a = temp; a = temp;
} }
// Round 4 // Round 4
for (int i = 60; i < 80; i++) { 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; (b ^ c ^ d) + e + W[i] + round4_kt;
e = d; e = d;
d = c; d = c;
c = ((b<<30) | (b>>>(32-30))); c = Integer.rotateLeft(b, 30);
b = a; b = a;
a = temp; a = temp;
} }

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -156,14 +156,14 @@ abstract class SHA2 extends DigestBase {
// delta0(x) = S(x, 7) ^ S(x, 18) ^ R(x, 3) // delta0(x) = S(x, 7) ^ S(x, 18) ^ R(x, 3)
int delta0_W_t15 = int delta0_W_t15 =
((W_t15 >>> 7) | (W_t15 << 25)) ^ Integer.rotateRight(W_t15, 7) ^
((W_t15 >>> 18) | (W_t15 << 14)) ^ Integer.rotateRight(W_t15, 18) ^
(W_t15 >>> 3); (W_t15 >>> 3);
// delta1(x) = S(x, 17) ^ S(x, 19) ^ R(x, 10) // delta1(x) = S(x, 17) ^ S(x, 19) ^ R(x, 10)
int delta1_W_t2 = int delta1_W_t2 =
((W_t2 >>> 17) | (W_t2 << 15)) ^ Integer.rotateRight(W_t2, 17) ^
((W_t2 >>> 19) | (W_t2 << 13)) ^ Integer.rotateRight(W_t2, 19) ^
(W_t2 >>> 10); (W_t2 >>> 10);
W[t] = delta0_W_t15 + delta1_W_t2 + W[t-7] + W[t-16]; W[t] = delta0_W_t15 + delta1_W_t2 + W[t-7] + W[t-16];
@ -184,15 +184,15 @@ abstract class SHA2 extends DigestBase {
// sigma0(x) = S(x,2) xor S(x,13) xor S(x,22) // sigma0(x) = S(x,2) xor S(x,13) xor S(x,22)
int sigma0_a = int sigma0_a =
((a >>> 2) | (a << 30)) ^ Integer.rotateRight(a, 2) ^
((a >>> 13) | (a << 19)) ^ Integer.rotateRight(a, 13) ^
((a >>> 22) | (a << 10)); Integer.rotateRight(a, 22);
// sigma1(x) = S(x,6) xor S(x,11) xor S(x,25) // sigma1(x) = S(x,6) xor S(x,11) xor S(x,25)
int sigma1_e = int sigma1_e =
((e >>> 6) | (e << 26)) ^ Integer.rotateRight(e, 6) ^
((e >>> 11) | (e << 21)) ^ Integer.rotateRight(e, 11) ^
((e >>> 25) | (e << 7)); Integer.rotateRight(e, 25);
// ch(x,y,z) = (x and y) xor ((complement x) and z) // ch(x,y,z) = (x and y) xor ((complement x) and z)
int ch_efg = (e & f) ^ ((~e) & g); int ch_efg = (e & f) ^ ((~e) & g);

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -173,7 +173,7 @@ abstract class SHA5 extends DigestBase {
* @param s int * @param s int
*/ */
private static long lf_S(long x, int s) { private static long lf_S(long x, int s) {
return (x >>> s) | (x << (64 - s)); return Long.rotateRight(x, s);
} }
/** /**