diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-30 14:41:33 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-30 14:41:33 +0400 |
commit | 059588c765337534aacdd9706289553976029488 (patch) | |
tree | bc58aebd503aa691ad4a32313b966fc17594ba10 /core | |
parent | 82985fa247458e3d0d47880576dd6074260c843d (diff) |
Replace some repetitive calculations with a lookup
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/modes/gcm/GCMUtil.java | 56 |
1 files changed, 34 insertions, 22 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/modes/gcm/GCMUtil.java b/core/src/main/java/org/bouncycastle/crypto/modes/gcm/GCMUtil.java index 48753011..e9ce75b1 100644 --- a/core/src/main/java/org/bouncycastle/crypto/modes/gcm/GCMUtil.java +++ b/core/src/main/java/org/bouncycastle/crypto/modes/gcm/GCMUtil.java @@ -5,6 +5,30 @@ import org.bouncycastle.util.Arrays; abstract class GCMUtil { + private static final int E1 = 0xe1000000; + + private static int[] generateLookup() + { + int[] lookup = new int[256]; + + for (int lsw = 0; lsw < 256; ++lsw) + { + int v = 0; + for (int i = 7; i >= 0; --i) + { + if ((lsw & (1 << i)) != 0) + { + v ^= (E1 >>> (7 - i)); + } + } + lookup[lsw] = v; + } + + return lookup; + } + + private static final int[] LOOKUP = generateLookup(); + static byte[] oneAsBytes() { byte[] tmp = new byte[16]; @@ -76,17 +100,17 @@ abstract class GCMUtil { // R = new int[]{ 0xe1000000, 0, 0, 0 }; // xor(v, R); - x[0] ^= 0xe1000000; + x[0] ^= E1; } } - static void multiplyP(int[] x, int[] output) + static void multiplyP(int[] x, int[] y) { boolean lsb = (x[3] & 1) != 0; - shiftRight(x, output); + shiftRight(x, y); if (lsb) { - output[0] ^= 0xe1000000; + y[0] ^= E1; } } @@ -98,28 +122,16 @@ abstract class GCMUtil // multiplyP(x); // } - int lsw = x[3]; + int lsw = x[3] & 0xFF; shiftRightN(x, 8); - for (int i = 7; i >= 0; --i) - { - if ((lsw & (1 << i)) != 0) - { - x[0] ^= (0xe1000000 >>> (7 - i)); - } - } + x[0] ^= LOOKUP[lsw]; } - static void multiplyP8(int[] x, int[] output) + static void multiplyP8(int[] x, int[] y) { - int lsw = x[3]; - shiftRightN(x, 8, output); - for (int i = 7; i >= 0; --i) - { - if ((lsw & (1 << i)) != 0) - { - output[0] ^= (0xe1000000 >>> (7 - i)); - } - } + int lsw = x[3] & 0xFF; + shiftRightN(x, 8, y); + y[0] ^= LOOKUP[lsw]; } static void shiftRight(byte[] block) |