mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
4887998: Use Integer.rotateLeft() and rotateRight() in crypto implementations
Reviewed-by: weijun
This commit is contained in:
parent
6c8d0e617f
commit
b65f7ec2f1
5 changed files with 31 additions and 31 deletions
|
@ -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
|
||||
|
@ -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) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
@ -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) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -156,7 +156,7 @@ public final class SHA extends DigestBase {
|
|||
// 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];
|
||||
|
@ -167,44 +167,44 @@ public final class SHA extends DigestBase {
|
|||
|
||||
// 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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -156,14 +156,14 @@ abstract class SHA2 extends DigestBase {
|
|||
|
||||
// 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];
|
||||
|
@ -184,15 +184,15 @@ abstract class SHA2 extends DigestBase {
|
|||
|
||||
// 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);
|
||||
|
|
|
@ -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
|
||||
|
@ -173,7 +173,7 @@ abstract class SHA5 extends DigestBase {
|
|||
* @param s int
|
||||
*/
|
||||
private static long lf_S(long x, int s) {
|
||||
return (x >>> s) | (x << (64 - s));
|
||||
return Long.rotateRight(x, s);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue