summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-08-26 12:15:06 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-08-26 12:15:06 +0700
commit6eac22cc8cb5ed16aaccf3cd88c8930265418cff (patch)
tree75e0e3725a0f406c45b1c2228315e726ecdb9e8d /crypto
parentDon't use CLR span-based NextBytes (diff)
downloadBouncyCastle.NET-ed25519-6eac22cc8cb5ed16aaccf3cd88c8930265418cff.tar.xz
Refactoring around rotates
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/crypto/digests/MD4Digest.cs108
-rw-r--r--crypto/src/crypto/engines/RC2Engine.cs2
-rw-r--r--crypto/src/crypto/engines/SerpentEngine.cs6
-rw-r--r--crypto/src/crypto/engines/SerpentEngineBase.cs34
-rw-r--r--crypto/src/crypto/engines/TnepresEngine.cs6
5 files changed, 67 insertions, 89 deletions
diff --git a/crypto/src/crypto/digests/MD4Digest.cs b/crypto/src/crypto/digests/MD4Digest.cs
index 2eb2c8400..f7d5e23c1 100644
--- a/crypto/src/crypto/digests/MD4Digest.cs
+++ b/crypto/src/crypto/digests/MD4Digest.cs
@@ -170,16 +170,6 @@ namespace Org.BouncyCastle.Crypto.Digests
         private const int S34 = 15;
 
         /*
-        * rotate int x left n bits.
-        */
-        private int RotateLeft(
-            int x,
-            int n)
-        {
-            return (x << n) | (int) ((uint) x >> (32 - n));
-        }
-
-        /*
         * F, G, H and I are the basic MD4 functions.
         */
         private int F(
@@ -216,62 +206,62 @@ namespace Org.BouncyCastle.Crypto.Digests
             //
             // Round 1 - F cycle, 16 times.
             //
-            a = RotateLeft((a + F(b, c, d) + X[ 0]), S11);
-            d = RotateLeft((d + F(a, b, c) + X[ 1]), S12);
-            c = RotateLeft((c + F(d, a, b) + X[ 2]), S13);
-            b = RotateLeft((b + F(c, d, a) + X[ 3]), S14);
-            a = RotateLeft((a + F(b, c, d) + X[ 4]), S11);
-            d = RotateLeft((d + F(a, b, c) + X[ 5]), S12);
-            c = RotateLeft((c + F(d, a, b) + X[ 6]), S13);
-            b = RotateLeft((b + F(c, d, a) + X[ 7]), S14);
-            a = RotateLeft((a + F(b, c, d) + X[ 8]), S11);
-            d = RotateLeft((d + F(a, b, c) + X[ 9]), S12);
-            c = RotateLeft((c + F(d, a, b) + X[10]), S13);
-            b = RotateLeft((b + F(c, d, a) + X[11]), S14);
-            a = RotateLeft((a + F(b, c, d) + X[12]), S11);
-            d = RotateLeft((d + F(a, b, c) + X[13]), S12);
-            c = RotateLeft((c + F(d, a, b) + X[14]), S13);
-            b = RotateLeft((b + F(c, d, a) + X[15]), S14);
+            a = Integers.RotateLeft((a + F(b, c, d) + X[ 0]), S11);
+            d = Integers.RotateLeft((d + F(a, b, c) + X[ 1]), S12);
+            c = Integers.RotateLeft((c + F(d, a, b) + X[ 2]), S13);
+            b = Integers.RotateLeft((b + F(c, d, a) + X[ 3]), S14);
+            a = Integers.RotateLeft((a + F(b, c, d) + X[ 4]), S11);
+            d = Integers.RotateLeft((d + F(a, b, c) + X[ 5]), S12);
+            c = Integers.RotateLeft((c + F(d, a, b) + X[ 6]), S13);
+            b = Integers.RotateLeft((b + F(c, d, a) + X[ 7]), S14);
+            a = Integers.RotateLeft((a + F(b, c, d) + X[ 8]), S11);
+            d = Integers.RotateLeft((d + F(a, b, c) + X[ 9]), S12);
+            c = Integers.RotateLeft((c + F(d, a, b) + X[10]), S13);
+            b = Integers.RotateLeft((b + F(c, d, a) + X[11]), S14);
+            a = Integers.RotateLeft((a + F(b, c, d) + X[12]), S11);
+            d = Integers.RotateLeft((d + F(a, b, c) + X[13]), S12);
+            c = Integers.RotateLeft((c + F(d, a, b) + X[14]), S13);
+            b = Integers.RotateLeft((b + F(c, d, a) + X[15]), S14);
 
             //
             // Round 2 - G cycle, 16 times.
             //
-            a = RotateLeft((a + G(b, c, d) + X[ 0] + 0x5a827999), S21);
-            d = RotateLeft((d + G(a, b, c) + X[ 4] + 0x5a827999), S22);
-            c = RotateLeft((c + G(d, a, b) + X[ 8] + 0x5a827999), S23);
-            b = RotateLeft((b + G(c, d, a) + X[12] + 0x5a827999), S24);
-            a = RotateLeft((a + G(b, c, d) + X[ 1] + 0x5a827999), S21);
-            d = RotateLeft((d + G(a, b, c) + X[ 5] + 0x5a827999), S22);
-            c = RotateLeft((c + G(d, a, b) + X[ 9] + 0x5a827999), S23);
-            b = RotateLeft((b + G(c, d, a) + X[13] + 0x5a827999), S24);
-            a = RotateLeft((a + G(b, c, d) + X[ 2] + 0x5a827999), S21);
-            d = RotateLeft((d + G(a, b, c) + X[ 6] + 0x5a827999), S22);
-            c = RotateLeft((c + G(d, a, b) + X[10] + 0x5a827999), S23);
-            b = RotateLeft((b + G(c, d, a) + X[14] + 0x5a827999), S24);
-            a = RotateLeft((a + G(b, c, d) + X[ 3] + 0x5a827999), S21);
-            d = RotateLeft((d + G(a, b, c) + X[ 7] + 0x5a827999), S22);
-            c = RotateLeft((c + G(d, a, b) + X[11] + 0x5a827999), S23);
-            b = RotateLeft((b + G(c, d, a) + X[15] + 0x5a827999), S24);
+            a = Integers.RotateLeft((a + G(b, c, d) + X[ 0] + 0x5a827999), S21);
+            d = Integers.RotateLeft((d + G(a, b, c) + X[ 4] + 0x5a827999), S22);
+            c = Integers.RotateLeft((c + G(d, a, b) + X[ 8] + 0x5a827999), S23);
+            b = Integers.RotateLeft((b + G(c, d, a) + X[12] + 0x5a827999), S24);
+            a = Integers.RotateLeft((a + G(b, c, d) + X[ 1] + 0x5a827999), S21);
+            d = Integers.RotateLeft((d + G(a, b, c) + X[ 5] + 0x5a827999), S22);
+            c = Integers.RotateLeft((c + G(d, a, b) + X[ 9] + 0x5a827999), S23);
+            b = Integers.RotateLeft((b + G(c, d, a) + X[13] + 0x5a827999), S24);
+            a = Integers.RotateLeft((a + G(b, c, d) + X[ 2] + 0x5a827999), S21);
+            d = Integers.RotateLeft((d + G(a, b, c) + X[ 6] + 0x5a827999), S22);
+            c = Integers.RotateLeft((c + G(d, a, b) + X[10] + 0x5a827999), S23);
+            b = Integers.RotateLeft((b + G(c, d, a) + X[14] + 0x5a827999), S24);
+            a = Integers.RotateLeft((a + G(b, c, d) + X[ 3] + 0x5a827999), S21);
+            d = Integers.RotateLeft((d + G(a, b, c) + X[ 7] + 0x5a827999), S22);
+            c = Integers.RotateLeft((c + G(d, a, b) + X[11] + 0x5a827999), S23);
+            b = Integers.RotateLeft((b + G(c, d, a) + X[15] + 0x5a827999), S24);
 
             //
             // Round 3 - H cycle, 16 times.
             //
-            a = RotateLeft((a + H(b, c, d) + X[ 0] + 0x6ed9eba1), S31);
-            d = RotateLeft((d + H(a, b, c) + X[ 8] + 0x6ed9eba1), S32);
-            c = RotateLeft((c + H(d, a, b) + X[ 4] + 0x6ed9eba1), S33);
-            b = RotateLeft((b + H(c, d, a) + X[12] + 0x6ed9eba1), S34);
-            a = RotateLeft((a + H(b, c, d) + X[ 2] + 0x6ed9eba1), S31);
-            d = RotateLeft((d + H(a, b, c) + X[10] + 0x6ed9eba1), S32);
-            c = RotateLeft((c + H(d, a, b) + X[ 6] + 0x6ed9eba1), S33);
-            b = RotateLeft((b + H(c, d, a) + X[14] + 0x6ed9eba1), S34);
-            a = RotateLeft((a + H(b, c, d) + X[ 1] + 0x6ed9eba1), S31);
-            d = RotateLeft((d + H(a, b, c) + X[ 9] + 0x6ed9eba1), S32);
-            c = RotateLeft((c + H(d, a, b) + X[ 5] + 0x6ed9eba1), S33);
-            b = RotateLeft((b + H(c, d, a) + X[13] + 0x6ed9eba1), S34);
-            a = RotateLeft((a + H(b, c, d) + X[ 3] + 0x6ed9eba1), S31);
-            d = RotateLeft((d + H(a, b, c) + X[11] + 0x6ed9eba1), S32);
-            c = RotateLeft((c + H(d, a, b) + X[ 7] + 0x6ed9eba1), S33);
-            b = RotateLeft((b + H(c, d, a) + X[15] + 0x6ed9eba1), S34);
+            a = Integers.RotateLeft((a + H(b, c, d) + X[ 0] + 0x6ed9eba1), S31);
+            d = Integers.RotateLeft((d + H(a, b, c) + X[ 8] + 0x6ed9eba1), S32);
+            c = Integers.RotateLeft((c + H(d, a, b) + X[ 4] + 0x6ed9eba1), S33);
+            b = Integers.RotateLeft((b + H(c, d, a) + X[12] + 0x6ed9eba1), S34);
+            a = Integers.RotateLeft((a + H(b, c, d) + X[ 2] + 0x6ed9eba1), S31);
+            d = Integers.RotateLeft((d + H(a, b, c) + X[10] + 0x6ed9eba1), S32);
+            c = Integers.RotateLeft((c + H(d, a, b) + X[ 6] + 0x6ed9eba1), S33);
+            b = Integers.RotateLeft((b + H(c, d, a) + X[14] + 0x6ed9eba1), S34);
+            a = Integers.RotateLeft((a + H(b, c, d) + X[ 1] + 0x6ed9eba1), S31);
+            d = Integers.RotateLeft((d + H(a, b, c) + X[ 9] + 0x6ed9eba1), S32);
+            c = Integers.RotateLeft((c + H(d, a, b) + X[ 5] + 0x6ed9eba1), S33);
+            b = Integers.RotateLeft((b + H(c, d, a) + X[13] + 0x6ed9eba1), S34);
+            a = Integers.RotateLeft((a + H(b, c, d) + X[ 3] + 0x6ed9eba1), S31);
+            d = Integers.RotateLeft((d + H(a, b, c) + X[11] + 0x6ed9eba1), S32);
+            c = Integers.RotateLeft((c + H(d, a, b) + X[ 7] + 0x6ed9eba1), S33);
+            b = Integers.RotateLeft((b + H(c, d, a) + X[15] + 0x6ed9eba1), S34);
 
             H1 += a;
             H2 += b;
@@ -299,7 +289,5 @@ namespace Org.BouncyCastle.Crypto.Digests
 
 			CopyIn(d);
 		}
-
     }
-
 }
diff --git a/crypto/src/crypto/engines/RC2Engine.cs b/crypto/src/crypto/engines/RC2Engine.cs
index 972c4128a..7fe1cbc3b 100644
--- a/crypto/src/crypto/engines/RC2Engine.cs
+++ b/crypto/src/crypto/engines/RC2Engine.cs
@@ -215,7 +215,7 @@ namespace Org.BouncyCastle.Crypto.Engines
         /**
         * return the result rotating the 16 bit number in x left by y
         */
-        private int RotateWordLeft(int x, int y)
+        private static int RotateWordLeft(int x, int y)
         {
             x &= 0xffff;
             return (x << y) | (x >> (16 - y));
diff --git a/crypto/src/crypto/engines/SerpentEngine.cs b/crypto/src/crypto/engines/SerpentEngine.cs
index 00473fa0a..bec459215 100644
--- a/crypto/src/crypto/engines/SerpentEngine.cs
+++ b/crypto/src/crypto/engines/SerpentEngine.cs
@@ -1,7 +1,7 @@
 using System;
 
-using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Crypto.Utilities;
+using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Crypto.Engines
 {
@@ -64,7 +64,7 @@ namespace Org.BouncyCastle.Crypto.Engines
             //
             for (int i = 8; i < 16; i++)
             {
-                kPad[i] = RotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3] ^ kPad[i - 1] ^ PHI ^ (i - 8), 11);
+                kPad[i] = Integers.RotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3] ^ kPad[i - 1] ^ PHI ^ (i - 8), 11);
             }
 
             Array.Copy(kPad, 8, w, 0, 8);
@@ -74,7 +74,7 @@ namespace Org.BouncyCastle.Crypto.Engines
             //
             for (int i = 8; i < amount; i++)
             {
-                w[i] = RotateLeft(w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i, 11);
+                w[i] = Integers.RotateLeft(w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i, 11);
             }
 
             //
diff --git a/crypto/src/crypto/engines/SerpentEngineBase.cs b/crypto/src/crypto/engines/SerpentEngineBase.cs
index 8ddbc4b6f..5d665fbbc 100644
--- a/crypto/src/crypto/engines/SerpentEngineBase.cs
+++ b/crypto/src/crypto/engines/SerpentEngineBase.cs
@@ -124,16 +124,6 @@ namespace Org.BouncyCastle.Crypto.Engines
         {
         }
 
-        protected static int RotateLeft(int x, int bits)
-        {
-            return ((x << bits) | (int) ((uint)x >> (32 - bits)));
-        }
-
-        private static int RotateRight(int x, int bits)
-        {
-            return ( (int)((uint)x >> bits) | (x << (32 - bits)));
-        }
-
         /*
          * The sboxes below are based on the work of Brian Gladman and
          * Sam Simpson, whose original notice appears below.
@@ -467,15 +457,15 @@ namespace Org.BouncyCastle.Crypto.Engines
         */
         protected void LT()
         {
-            int x0 = RotateLeft(X0, 13);
-            int x2 = RotateLeft(X2, 3);
+            int x0 = Integers.RotateLeft(X0, 13);
+            int x2 = Integers.RotateLeft(X2, 3);
             int x1 = X1 ^ x0 ^ x2;
             int x3 = X3 ^ x2 ^ x0 << 3;
 
-            X1 = RotateLeft(x1, 1);
-            X3 = RotateLeft(x3, 7);
-            X0 = RotateLeft(x0 ^ X1 ^ X3, 5);
-            X2 = RotateLeft(x2 ^ X3 ^ (X1 << 7), 22);
+            X1 = Integers.RotateLeft(x1, 1);
+            X3 = Integers.RotateLeft(x3, 7);
+            X0 = Integers.RotateLeft(x0 ^ X1 ^ X3, 5);
+            X2 = Integers.RotateLeft(x2 ^ X3 ^ (X1 << 7), 22);
         }
 
         /**
@@ -483,14 +473,14 @@ namespace Org.BouncyCastle.Crypto.Engines
         */
         protected void InverseLT()
         {
-            int x2 = RotateRight(X2, 22) ^ X3 ^ (X1 << 7);
-            int x0 = RotateRight(X0, 5) ^ X1 ^ X3;
-            int x3 = RotateRight(X3, 7);
-            int x1 = RotateRight(X1, 1);
+            int x2 = Integers.RotateRight(X2, 22) ^ X3 ^ (X1 << 7);
+            int x0 = Integers.RotateRight(X0, 5) ^ X1 ^ X3;
+            int x3 = Integers.RotateRight(X3, 7);
+            int x1 = Integers.RotateRight(X1, 1);
             X3 = x3 ^ x2 ^ x0 << 3;
             X1 = x1 ^ x0 ^ x2;
-            X2 = RotateRight(x2, 3);
-            X0 = RotateRight(x0, 13);
+            X2 = Integers.RotateRight(x2, 3);
+            X0 = Integers.RotateRight(x0, 13);
         }
 
         protected abstract int[] MakeWorkingKey(byte[] key);
diff --git a/crypto/src/crypto/engines/TnepresEngine.cs b/crypto/src/crypto/engines/TnepresEngine.cs
index cb008a182..50f5a58c4 100644
--- a/crypto/src/crypto/engines/TnepresEngine.cs
+++ b/crypto/src/crypto/engines/TnepresEngine.cs
@@ -1,7 +1,7 @@
 using System;
 
-using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Crypto.Utilities;
+using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Crypto.Engines
 {
@@ -71,7 +71,7 @@ namespace Org.BouncyCastle.Crypto.Engines
             //
             for (int i = 8; i < 16; i++)
             {
-                kPad[i] = RotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3] ^ kPad[i - 1] ^ PHI ^ (i - 8), 11);
+                kPad[i] = Integers.RotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3] ^ kPad[i - 1] ^ PHI ^ (i - 8), 11);
             }
 
             Array.Copy(kPad, 8, w, 0, 8);
@@ -81,7 +81,7 @@ namespace Org.BouncyCastle.Crypto.Engines
             //
             for (int i = 8; i < amount; i++)
             {
-                w[i] = RotateLeft(w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i, 11);
+                w[i] = Integers.RotateLeft(w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i, 11);
             }
 
             //