diff options
Diffstat (limited to 'java/SHA3.java')
-rw-r--r-- | java/SHA3.java | 223 |
1 files changed, 115 insertions, 108 deletions
diff --git a/java/SHA3.java b/java/SHA3.java index 48576c1..7e09d5b 100644 --- a/java/SHA3.java +++ b/java/SHA3.java @@ -277,16 +277,16 @@ public class SHA3 /** * Convert a chunk of byte:s to a word * - * @param msgoff The number of times to loop has run times the bitrate - * @param rr Bitrate in bytes + * @param n {@code Math.min(SHA3.message.length, rr) + msgoff} + * msgoff The number of times to loop has run times the bitrate + * rr Bitrate in bytes * @param ww Word size in bytes * @param off The offset in the message * @return Lane */ - private static long toLane(int msgoff, int rr, int ww, int off) + private static long toLane(int n, int ww, int off) { long rc = 0; - int n = Math.min(SHA3.message.length, rr) + msgoff; for (int i = off + ww - 1; i >= off; i--) rc = (rc << 8) | ((i < n) ? (long)(SHA3.message[i] & 255) : 0L); return rc; @@ -296,14 +296,14 @@ public class SHA3 /** * Convert a chunk of byte:s to a 64-bit word * - * @param msgoff The number of times to loop has run times the bitrate - * @param rr Bitrate in bytes + * @param n {@code Math.min(SHA3.message.length, rr) + msgoff} + * msgoff The number of times to loop has run times the bitrate + * rr Bitrate in bytes * @param off The offset in the message * @return Lane */ - private static long toLane64(int msgoff, int rr, int off) + private static long toLane64(int n, int off) { - int n = Math.min(SHA3.message.length, rr) + msgoff; return ((off + 7 < n) ? ((long)(SHA3.message[off + 7] & 255) << 56) : 0L) | ((off + 6 < n) ? ((long)(SHA3.message[off + 6] & 255) << 48) : 0L) | ((off + 5 < n) ? ((long)(SHA3.message[off + 5] & 255) << 40) : 0L) | @@ -399,67 +399,70 @@ public class SHA3 len -= len % ((SHA3.r * SHA3.b) >> 3); System.arraycopy(SHA3.M, 0, SHA3.message = new byte[len], 0, len); System.arraycopy(SHA3.M, len, SHA3.M, 0, SHA3.mptr -= len); + int n = Math.min(SHA3.message.length, rr); /* Absorbing phase */ if (ww == 8) for (int i = 0; i < len; i += rr) { - SHA3.S[ 0] ^= SHA3.toLane64(i, rr, i + 0); - SHA3.S[ 5] ^= SHA3.toLane64(i, rr, i + 8); - SHA3.S[10] ^= SHA3.toLane64(i, rr, i + 16); - SHA3.S[15] ^= SHA3.toLane64(i, rr, i + 24); - SHA3.S[20] ^= SHA3.toLane64(i, rr, i + 32); - SHA3.S[ 1] ^= SHA3.toLane64(i, rr, i + 40); - SHA3.S[ 6] ^= SHA3.toLane64(i, rr, i + 48); - SHA3.S[11] ^= SHA3.toLane64(i, rr, i + 56); - SHA3.S[16] ^= SHA3.toLane64(i, rr, i + 64); - SHA3.S[21] ^= SHA3.toLane64(i, rr, i + 72); - SHA3.S[ 2] ^= SHA3.toLane64(i, rr, i + 80); - SHA3.S[ 7] ^= SHA3.toLane64(i, rr, i + 88); - SHA3.S[12] ^= SHA3.toLane64(i, rr, i + 96); - SHA3.S[17] ^= SHA3.toLane64(i, rr, i + 104); - SHA3.S[22] ^= SHA3.toLane64(i, rr, i + 112); - SHA3.S[ 3] ^= SHA3.toLane64(i, rr, i + 120); - SHA3.S[ 8] ^= SHA3.toLane64(i, rr, i + 128); - SHA3.S[13] ^= SHA3.toLane64(i, rr, i + 136); - SHA3.S[18] ^= SHA3.toLane64(i, rr, i + 144); - SHA3.S[23] ^= SHA3.toLane64(i, rr, i + 152); - SHA3.S[ 4] ^= SHA3.toLane64(i, rr, i + 160); - SHA3.S[ 9] ^= SHA3.toLane64(i, rr, i + 168); - SHA3.S[14] ^= SHA3.toLane64(i, rr, i + 176); - SHA3.S[19] ^= SHA3.toLane64(i, rr, i + 184); - SHA3.S[24] ^= SHA3.toLane64(i, rr, i + 192); + SHA3.S[ 0] ^= SHA3.toLane64(n, i + 0); + SHA3.S[ 5] ^= SHA3.toLane64(n, i + 8); + SHA3.S[10] ^= SHA3.toLane64(n, i + 16); + SHA3.S[15] ^= SHA3.toLane64(n, i + 24); + SHA3.S[20] ^= SHA3.toLane64(n, i + 32); + SHA3.S[ 1] ^= SHA3.toLane64(n, i + 40); + SHA3.S[ 6] ^= SHA3.toLane64(n, i + 48); + SHA3.S[11] ^= SHA3.toLane64(n, i + 56); + SHA3.S[16] ^= SHA3.toLane64(n, i + 64); + SHA3.S[21] ^= SHA3.toLane64(n, i + 72); + SHA3.S[ 2] ^= SHA3.toLane64(n, i + 80); + SHA3.S[ 7] ^= SHA3.toLane64(n, i + 88); + SHA3.S[12] ^= SHA3.toLane64(n, i + 96); + SHA3.S[17] ^= SHA3.toLane64(n, i + 104); + SHA3.S[22] ^= SHA3.toLane64(n, i + 112); + SHA3.S[ 3] ^= SHA3.toLane64(n, i + 120); + SHA3.S[ 8] ^= SHA3.toLane64(n, i + 128); + SHA3.S[13] ^= SHA3.toLane64(n, i + 136); + SHA3.S[18] ^= SHA3.toLane64(n, i + 144); + SHA3.S[23] ^= SHA3.toLane64(n, i + 152); + SHA3.S[ 4] ^= SHA3.toLane64(n, i + 160); + SHA3.S[ 9] ^= SHA3.toLane64(n, i + 168); + SHA3.S[14] ^= SHA3.toLane64(n, i + 176); + SHA3.S[19] ^= SHA3.toLane64(n, i + 184); + SHA3.S[24] ^= SHA3.toLane64(n, i + 192); SHA3.keccakF(SHA3.S); + n += rr; } else for (int i = 0; i < len; i += rr) { - SHA3.S[ 0] ^= SHA3.toLane(i, rr, ww, i + 0 ); - SHA3.S[ 5] ^= SHA3.toLane(i, rr, ww, i + w); - SHA3.S[10] ^= SHA3.toLane(i, rr, ww, i + 2 * w); - SHA3.S[15] ^= SHA3.toLane(i, rr, ww, i + 3 * w); - SHA3.S[20] ^= SHA3.toLane(i, rr, ww, i + 4 * w); - SHA3.S[ 1] ^= SHA3.toLane(i, rr, ww, i + 5 * w); - SHA3.S[ 6] ^= SHA3.toLane(i, rr, ww, i + 6 * w); - SHA3.S[11] ^= SHA3.toLane(i, rr, ww, i + 7 * w); - SHA3.S[16] ^= SHA3.toLane(i, rr, ww, i + 8 * w); - SHA3.S[21] ^= SHA3.toLane(i, rr, ww, i + 9 * w); - SHA3.S[ 2] ^= SHA3.toLane(i, rr, ww, i + 10 * w); - SHA3.S[ 7] ^= SHA3.toLane(i, rr, ww, i + 11 * w); - SHA3.S[12] ^= SHA3.toLane(i, rr, ww, i + 12 * w); - SHA3.S[17] ^= SHA3.toLane(i, rr, ww, i + 13 * w); - SHA3.S[22] ^= SHA3.toLane(i, rr, ww, i + 14 * w); - SHA3.S[ 3] ^= SHA3.toLane(i, rr, ww, i + 15 * w); - SHA3.S[ 8] ^= SHA3.toLane(i, rr, ww, i + 16 * w); - SHA3.S[13] ^= SHA3.toLane(i, rr, ww, i + 17 * w); - SHA3.S[18] ^= SHA3.toLane(i, rr, ww, i + 18 * w); - SHA3.S[23] ^= SHA3.toLane(i, rr, ww, i + 19 * w); - SHA3.S[ 4] ^= SHA3.toLane(i, rr, ww, i + 20 * w); - SHA3.S[ 9] ^= SHA3.toLane(i, rr, ww, i + 21 * w); - SHA3.S[14] ^= SHA3.toLane(i, rr, ww, i + 22 * w); - SHA3.S[19] ^= SHA3.toLane(i, rr, ww, i + 23 * w); - SHA3.S[24] ^= SHA3.toLane(i, rr, ww, i + 24 * w); + SHA3.S[ 0] ^= SHA3.toLane(n, ww, i + 0 ); + SHA3.S[ 5] ^= SHA3.toLane(n, ww, i + w); + SHA3.S[10] ^= SHA3.toLane(n, ww, i + 2 * w); + SHA3.S[15] ^= SHA3.toLane(n, ww, i + 3 * w); + SHA3.S[20] ^= SHA3.toLane(n, ww, i + 4 * w); + SHA3.S[ 1] ^= SHA3.toLane(n, ww, i + 5 * w); + SHA3.S[ 6] ^= SHA3.toLane(n, ww, i + 6 * w); + SHA3.S[11] ^= SHA3.toLane(n, ww, i + 7 * w); + SHA3.S[16] ^= SHA3.toLane(n, ww, i + 8 * w); + SHA3.S[21] ^= SHA3.toLane(n, ww, i + 9 * w); + SHA3.S[ 2] ^= SHA3.toLane(n, ww, i + 10 * w); + SHA3.S[ 7] ^= SHA3.toLane(n, ww, i + 11 * w); + SHA3.S[12] ^= SHA3.toLane(n, ww, i + 12 * w); + SHA3.S[17] ^= SHA3.toLane(n, ww, i + 13 * w); + SHA3.S[22] ^= SHA3.toLane(n, ww, i + 14 * w); + SHA3.S[ 3] ^= SHA3.toLane(n, ww, i + 15 * w); + SHA3.S[ 8] ^= SHA3.toLane(n, ww, i + 16 * w); + SHA3.S[13] ^= SHA3.toLane(n, ww, i + 17 * w); + SHA3.S[18] ^= SHA3.toLane(n, ww, i + 18 * w); + SHA3.S[23] ^= SHA3.toLane(n, ww, i + 19 * w); + SHA3.S[ 4] ^= SHA3.toLane(n, ww, i + 20 * w); + SHA3.S[ 9] ^= SHA3.toLane(n, ww, i + 21 * w); + SHA3.S[14] ^= SHA3.toLane(n, ww, i + 22 * w); + SHA3.S[19] ^= SHA3.toLane(n, ww, i + 23 * w); + SHA3.S[24] ^= SHA3.toLane(n, ww, i + 24 * w); SHA3.keccakF(SHA3.S); + n += rr; } } @@ -550,67 +553,71 @@ public class SHA3 int rr = SHA3.r >> 3; int nn = (SHA3.n + 7) >> 3; int ww = SHA3.w >> 3; + + int n = Math.min(SHA3.message.length, rr); /* Absorbing phase */ if (ww == 8) for (int i = 0; i < len; i += rr) { - SHA3.S[ 0] ^= SHA3.toLane64(i, rr, i + 0); - SHA3.S[ 5] ^= SHA3.toLane64(i, rr, i + 8); - SHA3.S[10] ^= SHA3.toLane64(i, rr, i + 16); - SHA3.S[15] ^= SHA3.toLane64(i, rr, i + 24); - SHA3.S[20] ^= SHA3.toLane64(i, rr, i + 32); - SHA3.S[ 1] ^= SHA3.toLane64(i, rr, i + 40); - SHA3.S[ 6] ^= SHA3.toLane64(i, rr, i + 48); - SHA3.S[11] ^= SHA3.toLane64(i, rr, i + 56); - SHA3.S[16] ^= SHA3.toLane64(i, rr, i + 64); - SHA3.S[21] ^= SHA3.toLane64(i, rr, i + 72); - SHA3.S[ 2] ^= SHA3.toLane64(i, rr, i + 80); - SHA3.S[ 7] ^= SHA3.toLane64(i, rr, i + 88); - SHA3.S[12] ^= SHA3.toLane64(i, rr, i + 96); - SHA3.S[17] ^= SHA3.toLane64(i, rr, i + 104); - SHA3.S[22] ^= SHA3.toLane64(i, rr, i + 112); - SHA3.S[ 3] ^= SHA3.toLane64(i, rr, i + 120); - SHA3.S[ 8] ^= SHA3.toLane64(i, rr, i + 128); - SHA3.S[13] ^= SHA3.toLane64(i, rr, i + 136); - SHA3.S[18] ^= SHA3.toLane64(i, rr, i + 144); - SHA3.S[23] ^= SHA3.toLane64(i, rr, i + 152); - SHA3.S[ 4] ^= SHA3.toLane64(i, rr, i + 160); - SHA3.S[ 9] ^= SHA3.toLane64(i, rr, i + 168); - SHA3.S[14] ^= SHA3.toLane64(i, rr, i + 176); - SHA3.S[19] ^= SHA3.toLane64(i, rr, i + 184); - SHA3.S[24] ^= SHA3.toLane64(i, rr, i + 192); + SHA3.S[ 0] ^= SHA3.toLane64(n, i + 0); + SHA3.S[ 5] ^= SHA3.toLane64(n, i + 8); + SHA3.S[10] ^= SHA3.toLane64(n, i + 16); + SHA3.S[15] ^= SHA3.toLane64(n, i + 24); + SHA3.S[20] ^= SHA3.toLane64(n, i + 32); + SHA3.S[ 1] ^= SHA3.toLane64(n, i + 40); + SHA3.S[ 6] ^= SHA3.toLane64(n, i + 48); + SHA3.S[11] ^= SHA3.toLane64(n, i + 56); + SHA3.S[16] ^= SHA3.toLane64(n, i + 64); + SHA3.S[21] ^= SHA3.toLane64(n, i + 72); + SHA3.S[ 2] ^= SHA3.toLane64(n, i + 80); + SHA3.S[ 7] ^= SHA3.toLane64(n, i + 88); + SHA3.S[12] ^= SHA3.toLane64(n, i + 96); + SHA3.S[17] ^= SHA3.toLane64(n, i + 104); + SHA3.S[22] ^= SHA3.toLane64(n, i + 112); + SHA3.S[ 3] ^= SHA3.toLane64(n, i + 120); + SHA3.S[ 8] ^= SHA3.toLane64(n, i + 128); + SHA3.S[13] ^= SHA3.toLane64(n, i + 136); + SHA3.S[18] ^= SHA3.toLane64(n, i + 144); + SHA3.S[23] ^= SHA3.toLane64(n, i + 152); + SHA3.S[ 4] ^= SHA3.toLane64(n, i + 160); + SHA3.S[ 9] ^= SHA3.toLane64(n, i + 168); + SHA3.S[14] ^= SHA3.toLane64(n, i + 176); + SHA3.S[19] ^= SHA3.toLane64(n, i + 184); + SHA3.S[24] ^= SHA3.toLane64(n, i + 192); SHA3.keccakF(SHA3.S); + n += rr; } else for (int i = 0; i < len; i += rr) { - SHA3.S[ 0] ^= SHA3.toLane(i, rr, ww, i + 0 ); - SHA3.S[ 5] ^= SHA3.toLane(i, rr, ww, i + w); - SHA3.S[10] ^= SHA3.toLane(i, rr, ww, i + 2 * w); - SHA3.S[15] ^= SHA3.toLane(i, rr, ww, i + 3 * w); - SHA3.S[20] ^= SHA3.toLane(i, rr, ww, i + 4 * w); - SHA3.S[ 1] ^= SHA3.toLane(i, rr, ww, i + 5 * w); - SHA3.S[ 6] ^= SHA3.toLane(i, rr, ww, i + 6 * w); - SHA3.S[11] ^= SHA3.toLane(i, rr, ww, i + 7 * w); - SHA3.S[16] ^= SHA3.toLane(i, rr, ww, i + 8 * w); - SHA3.S[21] ^= SHA3.toLane(i, rr, ww, i + 9 * w); - SHA3.S[ 2] ^= SHA3.toLane(i, rr, ww, i + 10 * w); - SHA3.S[ 7] ^= SHA3.toLane(i, rr, ww, i + 11 * w); - SHA3.S[12] ^= SHA3.toLane(i, rr, ww, i + 12 * w); - SHA3.S[17] ^= SHA3.toLane(i, rr, ww, i + 13 * w); - SHA3.S[22] ^= SHA3.toLane(i, rr, ww, i + 14 * w); - SHA3.S[ 3] ^= SHA3.toLane(i, rr, ww, i + 15 * w); - SHA3.S[ 8] ^= SHA3.toLane(i, rr, ww, i + 16 * w); - SHA3.S[13] ^= SHA3.toLane(i, rr, ww, i + 17 * w); - SHA3.S[18] ^= SHA3.toLane(i, rr, ww, i + 18 * w); - SHA3.S[23] ^= SHA3.toLane(i, rr, ww, i + 19 * w); - SHA3.S[ 4] ^= SHA3.toLane(i, rr, ww, i + 20 * w); - SHA3.S[ 9] ^= SHA3.toLane(i, rr, ww, i + 21 * w); - SHA3.S[14] ^= SHA3.toLane(i, rr, ww, i + 22 * w); - SHA3.S[19] ^= SHA3.toLane(i, rr, ww, i + 23 * w); - SHA3.S[24] ^= SHA3.toLane(i, rr, ww, i + 24 * w); + SHA3.S[ 0] ^= SHA3.toLane(n, ww, i + 0 ); + SHA3.S[ 5] ^= SHA3.toLane(n, ww, i + w); + SHA3.S[10] ^= SHA3.toLane(n, ww, i + 2 * w); + SHA3.S[15] ^= SHA3.toLane(n, ww, i + 3 * w); + SHA3.S[20] ^= SHA3.toLane(n, ww, i + 4 * w); + SHA3.S[ 1] ^= SHA3.toLane(n, ww, i + 5 * w); + SHA3.S[ 6] ^= SHA3.toLane(n, ww, i + 6 * w); + SHA3.S[11] ^= SHA3.toLane(n, ww, i + 7 * w); + SHA3.S[16] ^= SHA3.toLane(n, ww, i + 8 * w); + SHA3.S[21] ^= SHA3.toLane(n, ww, i + 9 * w); + SHA3.S[ 2] ^= SHA3.toLane(n, ww, i + 10 * w); + SHA3.S[ 7] ^= SHA3.toLane(n, ww, i + 11 * w); + SHA3.S[12] ^= SHA3.toLane(n, ww, i + 12 * w); + SHA3.S[17] ^= SHA3.toLane(n, ww, i + 13 * w); + SHA3.S[22] ^= SHA3.toLane(n, ww, i + 14 * w); + SHA3.S[ 3] ^= SHA3.toLane(n, ww, i + 15 * w); + SHA3.S[ 8] ^= SHA3.toLane(n, ww, i + 16 * w); + SHA3.S[13] ^= SHA3.toLane(n, ww, i + 17 * w); + SHA3.S[18] ^= SHA3.toLane(n, ww, i + 18 * w); + SHA3.S[23] ^= SHA3.toLane(n, ww, i + 19 * w); + SHA3.S[ 4] ^= SHA3.toLane(n, ww, i + 20 * w); + SHA3.S[ 9] ^= SHA3.toLane(n, ww, i + 21 * w); + SHA3.S[14] ^= SHA3.toLane(n, ww, i + 22 * w); + SHA3.S[19] ^= SHA3.toLane(n, ww, i + 23 * w); + SHA3.S[24] ^= SHA3.toLane(n, ww, i + 24 * w); SHA3.keccakF(SHA3.S); + n += rr; } /* Squeezing phase */ |