diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-11-13 04:31:43 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-11-13 04:31:43 +0100 |
commit | 25d561ca9aa5054896bd7c556a3efc6f4663beed (patch) | |
tree | a4e906d08feb44c13eea7421cfb63cbe575a2b8e /java | |
parent | fix bug (diff) | |
download | sha3sum-25d561ca9aa5054896bd7c556a3efc6f4663beed.tar.gz sha3sum-25d561ca9aa5054896bd7c556a3efc6f4663beed.tar.bz2 sha3sum-25d561ca9aa5054896bd7c556a3efc6f4663beed.tar.xz |
nuke...
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'java')
-rw-r--r-- | java/ConcurrentSHA3.java | 963 | ||||
-rw-r--r-- | java/SHA3.java | 969 | ||||
-rw-r--r-- | java/sha3_224sum.java | 42 | ||||
-rw-r--r-- | java/sha3_256sum.java | 42 | ||||
-rw-r--r-- | java/sha3_384sum.java | 42 | ||||
-rw-r--r-- | java/sha3_512sum.java | 42 | ||||
-rw-r--r-- | java/sha3sum.java | 485 |
7 files changed, 0 insertions, 2585 deletions
diff --git a/java/ConcurrentSHA3.java b/java/ConcurrentSHA3.java deleted file mode 100644 index 69425b9..0000000 --- a/java/ConcurrentSHA3.java +++ /dev/null @@ -1,963 +0,0 @@ -/** - * sha3sum – SHA-3 (Keccak) checksum calculator - * - * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - - -/** - * SHA-3/Keccak hash algorithm implementation with support for concurrent threads - * - * @author Mattias Andrée <a href="mailto:maandree@member.fsf.org">maandree@member.fsf.org</a> - */ -public class ConcurrentSHA3 -{ - /** - * Suffix the message when calculating the Keccak hash sum - */ - public static final String KECCAK_SUFFIX = ""; - - /** - * Suffix the message when calculating the SHA-3 hash sum - */ - public static final String SHA3_SUFFIX = "01"; - - /** - * Suffix the message when calculating the RawSHAKE hash sum - */ - public static final String RawSHAKE_SUFFIX = "11"; - - /** - * Suffix the message when calculating the SHAKE hash sum - */ - public static final String SHAKE_SUFFIX = "1111"; - - - /** - * Round contants - */ - private static final long[] RC = { - 0x0000000000000001L, 0x0000000000008082L, 0x800000000000808AL, 0x8000000080008000L, - 0x000000000000808BL, 0x0000000080000001L, 0x8000000080008081L, 0x8000000000008009L, - 0x000000000000008AL, 0x0000000000000088L, 0x0000000080008009L, 0x000000008000000AL, - 0x000000008000808BL, 0x800000000000008BL, 0x8000000000008089L, 0x8000000000008003L, - 0x8000000000008002L, 0x8000000000000080L, 0x000000000000800AL, 0x800000008000000AL, - 0x8000000080008081L, 0x8000000000008080L, 0x0000000080000001L, 0x8000000080008008L}; - - - - /** - * <p>Constructor</p> - * <p> - * Do not forget to run {@link #Initialise(int, int, int)} - * </p> - */ - public ConcurrentSHA3() - { - /* Do nothing */ - } - - - - /** - * Keccak-f round temporary - */ - private long[] B = new long[25]; - - /** - * Keccak-f round temporary - */ - private long[] C = new long[5]; - - - /** - * The bitrate - */ - private int r = 0; - - /** - * The capacity - */ - private int c = 0; - - /** - * The output size - */ - private int n = 0; - - /** - * The state size - */ - private int b = 0; - - /** - * The word size - */ - private int w = 0; - - /** - * The word mask - */ - private long wmod = 0; - - /** - * ℓ, the binary logarithm of the word size - */ - private int l = 0; - - /** - * 12 + 2ℓ, the number of rounds - */ - private int nr = 0; - - - /** - * Message chunk that is being processed - */ - private byte[] message = null; - - /** - * The current state - */ - private long[] S = null; - - /** - * Left over water to fill the sponge with at next update - */ - private byte[] M = null; - - /** - * Pointer for {@link #M} - */ - private int mptr = 0; - - - - /** - * Rotate a word - * - * @param x The value to rotate - * @param n Rotation steps, may not be 0 - * @return The value rotated - */ - private long rotate(long x, int n) - { - long m; - return ((x >>> (this.w - (m = n % this.w))) + (x << m)) & this.wmod; - } - - - /** - * Rotate a 64-bit word - * - * @param x The value to rotate - * @param n Rotation steps, may not be 0 - * @return The value rotated - */ - private static long rotate64(long x, int n) - { - return (x >>> (64 - n)) + (x << n); - } - - - /** - * Binary logarithm - * - * @param x The value of which to calculate the binary logarithm - * @return The binary logarithm - */ - private static int lb(int x) - { - int rc = 0; - if ((x & 0xFF00) != 0) { rc += 8; x >>= 8; } - if ((x & 0x00F0) != 0) { rc += 4; x >>= 4; } - if ((x & 0x000C) != 0) { rc += 2; x >>= 2; } - if ((x & 0x0002) != 0) rc += 1; - return rc; - } - - /** - * Perform one round of computation - * - * @param A The current state - * @param rc Round constant - */ - private void keccakFRound(long[] A, long rc) - { - /* θ step (step 1 of 3) */ - for (int i = 0, j = 0; i < 5; i++, j += 5) - this.C[i] = (A[j] ^ A[j + 1]) ^ (A[j + 2] ^ A[j + 3]) ^ A[j + 4]; - - long da, db, dc, dd, de; - - if (this.w == 64) - { - /* ρ and π steps, with last two part of θ */ - this.B[0] = A[ 0] ^ (da = this.C[4] ^ ConcurrentSHA3.rotate64(this.C[1], 1)); - this.B[1] = ConcurrentSHA3.rotate64(A[15] ^ (dd = this.C[2] ^ ConcurrentSHA3.rotate64(this.C[4], 1)), 28); - this.B[2] = ConcurrentSHA3.rotate64(A[ 5] ^ (db = this.C[0] ^ ConcurrentSHA3.rotate64(this.C[2], 1)), 1); - this.B[3] = ConcurrentSHA3.rotate64(A[20] ^ (de = this.C[3] ^ ConcurrentSHA3.rotate64(this.C[0], 1)), 27); - this.B[4] = ConcurrentSHA3.rotate64(A[10] ^ (dc = this.C[1] ^ ConcurrentSHA3.rotate64(this.C[3], 1)), 62); - - this.B[5] = ConcurrentSHA3.rotate64(A[ 6] ^ db, 44); - this.B[6] = ConcurrentSHA3.rotate64(A[21] ^ de, 20); - this.B[7] = ConcurrentSHA3.rotate64(A[11] ^ dc, 6); - this.B[8] = ConcurrentSHA3.rotate64(A[ 1] ^ da, 36); - this.B[9] = ConcurrentSHA3.rotate64(A[16] ^ dd, 55); - - this.B[10] = ConcurrentSHA3.rotate64(A[12] ^ dc, 43); - this.B[11] = ConcurrentSHA3.rotate64(A[ 2] ^ da, 3); - this.B[12] = ConcurrentSHA3.rotate64(A[17] ^ dd, 25); - this.B[13] = ConcurrentSHA3.rotate64(A[ 7] ^ db, 10); - this.B[14] = ConcurrentSHA3.rotate64(A[22] ^ de, 39); - - this.B[15] = ConcurrentSHA3.rotate64(A[18] ^ dd, 21); - this.B[16] = ConcurrentSHA3.rotate64(A[ 8] ^ db, 45); - this.B[17] = ConcurrentSHA3.rotate64(A[23] ^ de, 8); - this.B[18] = ConcurrentSHA3.rotate64(A[13] ^ dc, 15); - this.B[19] = ConcurrentSHA3.rotate64(A[ 3] ^ da, 41); - - this.B[20] = ConcurrentSHA3.rotate64(A[24] ^ de, 14); - this.B[21] = ConcurrentSHA3.rotate64(A[14] ^ dc, 61); - this.B[22] = ConcurrentSHA3.rotate64(A[ 4] ^ da, 18); - this.B[23] = ConcurrentSHA3.rotate64(A[19] ^ dd, 56); - this.B[24] = ConcurrentSHA3.rotate64(A[ 9] ^ db, 2); - } - else - { - /* ρ and π steps, with last two part of θ */ - this.B[0] = A[ 0] ^ (da = this.C[4] ^ this.rotate(this.C[1], 1)); - this.B[1] = this.rotate(A[15] ^ (dd = this.C[2] ^ this.rotate(this.C[4], 1)), 28); - this.B[2] = this.rotate(A[ 5] ^ (db = this.C[0] ^ this.rotate(this.C[2], 1)), 1); - this.B[3] = this.rotate(A[20] ^ (de = this.C[3] ^ this.rotate(this.C[0], 1)), 27); - this.B[4] = this.rotate(A[10] ^ (dc = this.C[1] ^ this.rotate(this.C[3], 1)), 62); - - this.B[5] = this.rotate(A[ 6] ^ db, 44); - this.B[6] = this.rotate(A[21] ^ de, 20); - this.B[7] = this.rotate(A[11] ^ dc, 6); - this.B[8] = this.rotate(A[ 1] ^ da, 36); - this.B[9] = this.rotate(A[16] ^ dd, 55); - - this.B[10] = this.rotate(A[12] ^ dc, 43); - this.B[11] = this.rotate(A[ 2] ^ da, 3); - this.B[12] = this.rotate(A[17] ^ dd, 25); - this.B[13] = this.rotate(A[ 7] ^ db, 10); - this.B[14] = this.rotate(A[22] ^ de, 39); - - this.B[15] = this.rotate(A[18] ^ dd, 21); - this.B[16] = this.rotate(A[ 8] ^ db, 45); - this.B[17] = this.rotate(A[23] ^ de, 8); - this.B[18] = this.rotate(A[13] ^ dc, 15); - this.B[19] = this.rotate(A[ 3] ^ da, 41); - - this.B[20] = this.rotate(A[24] ^ de, 14); - this.B[21] = this.rotate(A[14] ^ dc, 61); - this.B[22] = this.rotate(A[ 4] ^ da, 18); - this.B[23] = this.rotate(A[19] ^ dd, 56); - this.B[24] = this.rotate(A[ 9] ^ db, 2); - } - - /* ξ step */ - for (int i = 0; i < 15; i++) - A[i ] = this.B[i ] ^ ((~(this.B[i + 5])) & this.B[i + 10]); - for (int i = 0; i < 5; i++) - { - A[i + 15] = this.B[i + 15] ^ ((~(this.B[i + 20])) & this.B[i ]); - A[i + 20] = this.B[i + 20] ^ ((~(this.B[i ])) & this.B[i + 5]); - } - - /* ι step */ - A[0] ^= rc; - } - - - /** - * Perform Keccak-f function - * - * @param A The current state - */ - private void keccakF(long[] A) - { - if (this.nr == 24) - for (int i = 0; i < 24; i++) - this.keccakFRound(A, ConcurrentSHA3.RC[i]); - else - for (int i = 0; i < this.nr; i++) - this.keccakFRound(A, ConcurrentSHA3.RC[i] & this.wmod); - } - - - /** - * Convert a chunk of byte:s to a word - * - * @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 long toLane(int n, int ww, int off) - { - long rc = 0; - for (int i = off + ww - 1; i >= off; i--) - rc = (rc << 8) | ((i < n) ? (long)(this.message[i] & 255) : 0L); - return rc; - } - - - /** - * Convert a chunk of byte:s to a 64-bit word - * - * @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 long toLane64(int n, int off) - { - return ((off + 7 < n) ? ((long)(this.message[off + 7] & 255) << 56) : 0L) | - ((off + 6 < n) ? ((long)(this.message[off + 6] & 255) << 48) : 0L) | - ((off + 5 < n) ? ((long)(this.message[off + 5] & 255) << 40) : 0L) | - ((off + 4 < n) ? ((long)(this.message[off + 4] & 255) << 32) : 0L) | - ((off + 3 < n) ? ((long)(this.message[off + 3] & 255) << 24) : 0L) | - ((off + 2 < n) ? ((long)(this.message[off + 2] & 255) << 16) : 0L) | - ((off + 1 < n) ? ((long)(this.message[off + 1] & 255) << 8) : 0L) | - ((off < n) ? ((long)(this.message[off] & 255)) : 0L); - } - - - /** - * pad 10*1 - * - * @param msg The message to pad - * @param len The length of the message - * @param r The bitrate - * @param bits The number of bits in the end of the message that does not make a whole byte - * @return The actual length of {@link #message} - */ - private int pad10star1(byte[] msg, int len, int r, int bits) - { - len = ((len - (bits + 7) / 8) << 3) + bits; - - int nrf = len >> 3; - int nbrf = len & 7; - int ll = len % r; - - byte b = (byte)(nbrf == 0 ? 1 : (msg[nrf] | (1 << nbrf))); - - if ((r - 8 <= ll) && (ll <= r - 2)) - { - this.message = new byte[len = nrf + 1]; - this.message[nrf] = (byte)(b ^ 128); - } - else - { - len = (nrf + 1) << 3; - len = ((len - (len % r) + (r - 8)) >> 3) + 1; - this.message = new byte[len]; - this.message[nrf] = b; - this.message[len - 1] = -128; - } - - System.arraycopy(msg, 0, this.message, 0, nrf); - return len; - } - - - /** - * Initialise Keccak sponge - * - * @param r The bitrate - * @param c The capacity - * @param n The output size - */ - public void initialise(int r, int c, int n) - { - this.r = r; - this.c = c; - this.n = n; - this.b = r + c; - this.w = this.b / 25; - this.l = ConcurrentSHA3.lb(this.w); - this.nr = 12 + (this.l << 1); - this.wmod = w == 64 ? -1L : (1L << this.w) - 1L; - this.S = new long[25]; - if ((this.M == null) || ((this.r * this.b) >> 2 != this.M.length)) - this.M = new byte[(this.r * this.b) >> 2]; - this.mptr = 0; - if (this.message == null) - this.message = new byte[8 << 10]; - } - - - /** - * Absorb the more of the message message to the Keccak sponge - * - * @param msg The partial message - */ - public void update(byte[] msg) - { - this.update(msg, msg.length); - } - - - /** - * Absorb the more of the message message to the Keccak sponge - * - * @param msg The partial message - * @param msglen The length of the partial message - */ - public void update(byte[] msg, int msglen) - { - int rr = this.r >> 3; - int ww = this.w >> 3; - - if (this.mptr + msglen > this.M.length) - System.arraycopy(this.M, 0, this.M = new byte[(this.M.length + msglen) << 1], 0, this.mptr); - System.arraycopy(msg, 0, this.M, this.mptr, msglen); - int len = this.mptr += msglen; - len -= len % ((this.r * this.b) >> 3); - System.arraycopy(this.M, 0, (this.message.length < len) ? (this.message = new byte[len]) : this.message, 0, len); - System.arraycopy(this.M, len, this.M, 0, this.mptr -= len); - int n = Math.min(len, rr); - - /* Absorbing phase */ - if (ww == 8) - for (int i = 0; i < len; i += rr) - { - this.S[ 0] ^= this.toLane64(n, i + 0); - this.S[ 5] ^= this.toLane64(n, i + 8); - this.S[10] ^= this.toLane64(n, i + 16); - this.S[15] ^= this.toLane64(n, i + 24); - this.S[20] ^= this.toLane64(n, i + 32); - this.S[ 1] ^= this.toLane64(n, i + 40); - this.S[ 6] ^= this.toLane64(n, i + 48); - this.S[11] ^= this.toLane64(n, i + 56); - this.S[16] ^= this.toLane64(n, i + 64); - this.S[21] ^= this.toLane64(n, i + 72); - this.S[ 2] ^= this.toLane64(n, i + 80); - this.S[ 7] ^= this.toLane64(n, i + 88); - this.S[12] ^= this.toLane64(n, i + 96); - this.S[17] ^= this.toLane64(n, i + 104); - this.S[22] ^= this.toLane64(n, i + 112); - this.S[ 3] ^= this.toLane64(n, i + 120); - this.S[ 8] ^= this.toLane64(n, i + 128); - this.S[13] ^= this.toLane64(n, i + 136); - this.S[18] ^= this.toLane64(n, i + 144); - this.S[23] ^= this.toLane64(n, i + 152); - this.S[ 4] ^= this.toLane64(n, i + 160); - this.S[ 9] ^= this.toLane64(n, i + 168); - this.S[14] ^= this.toLane64(n, i + 176); - this.S[19] ^= this.toLane64(n, i + 184); - this.S[24] ^= this.toLane64(n, i + 192); - this.keccakF(this.S); - n += rr; - } - else - for (int i = 0; i < len; i += rr) - { - this.S[ 0] ^= this.toLane(n, ww, i + 0 ); - this.S[ 5] ^= this.toLane(n, ww, i + ww); - this.S[10] ^= this.toLane(n, ww, i + 2 * ww); - this.S[15] ^= this.toLane(n, ww, i + 3 * ww); - this.S[20] ^= this.toLane(n, ww, i + 4 * ww); - this.S[ 1] ^= this.toLane(n, ww, i + 5 * ww); - this.S[ 6] ^= this.toLane(n, ww, i + 6 * ww); - this.S[11] ^= this.toLane(n, ww, i + 7 * ww); - this.S[16] ^= this.toLane(n, ww, i + 8 * ww); - this.S[21] ^= this.toLane(n, ww, i + 9 * ww); - this.S[ 2] ^= this.toLane(n, ww, i + 10 * ww); - this.S[ 7] ^= this.toLane(n, ww, i + 11 * ww); - this.S[12] ^= this.toLane(n, ww, i + 12 * ww); - this.S[17] ^= this.toLane(n, ww, i + 13 * ww); - this.S[22] ^= this.toLane(n, ww, i + 14 * ww); - this.S[ 3] ^= this.toLane(n, ww, i + 15 * ww); - this.S[ 8] ^= this.toLane(n, ww, i + 16 * ww); - this.S[13] ^= this.toLane(n, ww, i + 17 * ww); - this.S[18] ^= this.toLane(n, ww, i + 18 * ww); - this.S[23] ^= this.toLane(n, ww, i + 19 * ww); - this.S[ 4] ^= this.toLane(n, ww, i + 20 * ww); - this.S[ 9] ^= this.toLane(n, ww, i + 21 * ww); - this.S[14] ^= this.toLane(n, ww, i + 22 * ww); - this.S[19] ^= this.toLane(n, ww, i + 23 * ww); - this.S[24] ^= this.toLane(n, ww, i + 24 * ww); - this.keccakF(this.S); - n += rr; - } - } - - - /** - * Squeeze the Keccak sponge - * - * @return The hash sum - */ - public byte[] digest() - { - return digest(null, 0, 0, SHA3.SHA3_SUFFIX, true); - } - - - /** - * Squeeze the Keccak sponge - * - * @param suffix The suffix concatenate to the message - * @return The hash sum - */ - public byte[] digest(String suffix) - { - return digest(null, 0, 0, suffix, true); - } - - - /** - * Squeeze the Keccak sponge - * - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public byte[] digest(boolean withReturn) - { - return digest(null, 0, 0, SHA3.SHA3_SUFFIX, withReturn); - } - - - /** - * Squeeze the Keccak sponge - * - * @param suffix The suffix concatenate to the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public byte[] digest(String suffix, boolean withReturn) - { - return digest(null, 0, 0, suffix, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @return The hash sum - */ - public byte[] digest(byte[] msg) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param suffix The suffix concatenate to the message - * @return The hash sum - */ - public byte[] digest(byte[] msg, String suffix) - { - return digest(msg, msg == null ? 0 : msg.length, 0, suffix, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public byte[] digest(byte[] msg, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param suffix The suffix concatenate to the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public byte[] digest(byte[] msg, String suffix, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, suffix, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @return The hash sum - */ - public byte[] digest(byte[] msg, int msglen) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param suffix The suffix concatenate to the message - * @return The hash sum - */ - public byte[] digest(byte[] msg, int msglen, String suffix) - { - return digest(msg, msg == null ? 0 : msg.length, 0, suffix, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param bits The number of bits at the end of the message not covered by <tt>msglen</tt> - * @return The hash sum - */ - public byte[] digest(byte[] msg, int msglen, int bits) - { - return digest(msg, msg == null ? 0 : msg.length, bits, SHA3.SHA3_SUFFIX, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param bits The number of bits at the end of the message not covered by <tt>msglen</tt> - * @param suffix The suffix concatenate to the message - * @return The hash sum - */ - public byte[] digest(byte[] msg, int msglen, int bits, String suffix) - { - return digest(msg, msg == null ? 0 : msg.length, bits, suffix, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public byte[] digest(byte[] msg, int msglen, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param suffix The suffix concatenate to the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public byte[] digest(byte[] msg, int msglen, String suffix, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, suffix, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param bits The number of bits at the end of the message not covered by <tt>msglen</tt> - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public byte[] digest(byte[] msg, int msglen, int bits, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message - * @param bits The number of bits at the end of the message not covered by <tt>msglen</tt> - * @param suffix The suffix concatenate to the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public byte[] digest(byte[] msg, int msglen, int bits, String suffix, boolean withReturn) - { - int len; - byte last_byte = 0; - if ((msg == null) || (msglen == 0)) - { - msg = new byte[0]; - bits = 0; - } - - msglen += bits / 8; - if ((bits %= 8) != 0) - last_byte = msg[msglen]; - - byte[] msg_end = new byte[(suffix.length() + bits + 7) / 8]; - int msg_end_ptr = 0; - for (int i = 0, n = suffix.length(); i < n; i++) - { - byte bit = (byte)(suffix.charAt(i) - '0'); - last_byte |= bit << bits++; - if (bits == 8) - { - msg_end[msg_end_ptr++] = last_byte; - last_byte = 0; - bits = 0; - } - } - if (bits != 0) - msg_end[msg_end_ptr++] = last_byte; - if (msg_end_ptr > 0) - { - if (msglen + msg_end_ptr > msg.length) - System.arraycopy(msg, 0, msg = new byte[msglen + msg_end_ptr], 0, msglen); - System.arraycopy(msg_end, 0, msg, msglen, msg_end_ptr); - msglen += msg_end_ptr; - } - - if (this.mptr + msglen > this.M.length) - System.arraycopy(this.M, 0, this.M = new byte[this.M.length + msglen], 0, this.mptr); - System.arraycopy(msg, 0, this.M, this.mptr, msglen); - len = this.pad10star1(this.M, this.mptr + msglen, this.r, bits); - - int rr = this.r >> 3; - int nn = (this.n + 7) >> 3; - int ww = this.w >> 3; - - int n = Math.min(len, rr); - - /* Absorbing phase */ - if (ww == 8) - for (int i = 0; i < len; i += rr) - { - this.S[ 0] ^= this.toLane64(n, i + 0); - this.S[ 5] ^= this.toLane64(n, i + 8); - this.S[10] ^= this.toLane64(n, i + 16); - this.S[15] ^= this.toLane64(n, i + 24); - this.S[20] ^= this.toLane64(n, i + 32); - this.S[ 1] ^= this.toLane64(n, i + 40); - this.S[ 6] ^= this.toLane64(n, i + 48); - this.S[11] ^= this.toLane64(n, i + 56); - this.S[16] ^= this.toLane64(n, i + 64); - this.S[21] ^= this.toLane64(n, i + 72); - this.S[ 2] ^= this.toLane64(n, i + 80); - this.S[ 7] ^= this.toLane64(n, i + 88); - this.S[12] ^= this.toLane64(n, i + 96); - this.S[17] ^= this.toLane64(n, i + 104); - this.S[22] ^= this.toLane64(n, i + 112); - this.S[ 3] ^= this.toLane64(n, i + 120); - this.S[ 8] ^= this.toLane64(n, i + 128); - this.S[13] ^= this.toLane64(n, i + 136); - this.S[18] ^= this.toLane64(n, i + 144); - this.S[23] ^= this.toLane64(n, i + 152); - this.S[ 4] ^= this.toLane64(n, i + 160); - this.S[ 9] ^= this.toLane64(n, i + 168); - this.S[14] ^= this.toLane64(n, i + 176); - this.S[19] ^= this.toLane64(n, i + 184); - this.S[24] ^= this.toLane64(n, i + 192); - this.keccakF(this.S); - n += rr; - } - else - for (int i = 0; i < len; i += rr) - { - this.S[ 0] ^= this.toLane(n, ww, i + 0 ); - this.S[ 5] ^= this.toLane(n, ww, i + ww); - this.S[10] ^= this.toLane(n, ww, i + 2 * ww); - this.S[15] ^= this.toLane(n, ww, i + 3 * ww); - this.S[20] ^= this.toLane(n, ww, i + 4 * ww); - this.S[ 1] ^= this.toLane(n, ww, i + 5 * ww); - this.S[ 6] ^= this.toLane(n, ww, i + 6 * ww); - this.S[11] ^= this.toLane(n, ww, i + 7 * ww); - this.S[16] ^= this.toLane(n, ww, i + 8 * ww); - this.S[21] ^= this.toLane(n, ww, i + 9 * ww); - this.S[ 2] ^= this.toLane(n, ww, i + 10 * ww); - this.S[ 7] ^= this.toLane(n, ww, i + 11 * ww); - this.S[12] ^= this.toLane(n, ww, i + 12 * ww); - this.S[17] ^= this.toLane(n, ww, i + 13 * ww); - this.S[22] ^= this.toLane(n, ww, i + 14 * ww); - this.S[ 3] ^= this.toLane(n, ww, i + 15 * ww); - this.S[ 8] ^= this.toLane(n, ww, i + 16 * ww); - this.S[13] ^= this.toLane(n, ww, i + 17 * ww); - this.S[18] ^= this.toLane(n, ww, i + 18 * ww); - this.S[23] ^= this.toLane(n, ww, i + 19 * ww); - this.S[ 4] ^= this.toLane(n, ww, i + 20 * ww); - this.S[ 9] ^= this.toLane(n, ww, i + 21 * ww); - this.S[14] ^= this.toLane(n, ww, i + 22 * ww); - this.S[19] ^= this.toLane(n, ww, i + 23 * ww); - this.S[24] ^= this.toLane(n, ww, i + 24 * ww); - this.keccakF(this.S); - n += rr; - } - - /* Squeezing phase */ - if (withReturn) - { - byte[] rc = new byte[(this.n + 7) >> 3]; - int ptr = 0; - - int olen = this.n; - int j = 0; - int ni = rr / ww; - while (olen > 0) - { - int i = 0; - while ((i < ni) && (j < nn)) - { - long v = this.S[(i % 5) * 5 + i / 5]; - for (int k = 0; k < ww; k++) - { - if (j < nn) - { - rc[ptr] = (byte)v; - ptr += 1; - } - v >>= 8; - j += 1; - } - i += 1; - } - olen -= this.r; - if (olen > 0) - this.keccakF(this.S); - } - if ((this.n & 7) != 0) - rc[rc.length - 1] &= (1 << (this.n & 7)) - 1; - - return rc; - } - int olen = this.n; - while ((olen -= this.r) > 0) - this.keccakF(this.S); - return null; - } - - - /** - * Force a round of Keccak-f - */ - public void simpleSqueeze() - { - this.keccakF(this.S); - } - - - /** - * Force some rounds of Keccak-f - * - * @param times The number of rounds - */ - public void simpleSqueeze(int times) - { - for (int i = 0; i < times; i++) - this.keccakF(this.S); - } - - - /** - * Squeeze as much as is needed to get a digest - */ - public void fastSqueeze() - { - this.keccakF(this.S); /* Last squeeze did not do a ending squeeze */ - int olen = this.n; - while ((olen -= this.r) > 0) - this.keccakF(this.S); - } - - - /** - * Squeeze as much as is needed to get a digest a number of times - * - * @param times The number of digests - */ - public void fastSqueeze(int times) - { - for (int i = 0; i < times; i++) - { - this.keccakF(this.S); /* Last squeeze did not do a ending squeeze */ - int olen = this.n; - while ((olen -= this.r) > 0) - this.keccakF(this.S); - } - } - - - /** - * Squeeze out another digest - * - * @return The hash sum - */ - public byte[] squeeze() - { - this.keccakF(this.S); /* Last squeeze did not do a ending squeeze */ - - int nn, ww = this.w >> 3; - byte[] rc = new byte[nn = (this.n + 7) >> 3]; - - int olen = this.n; - int j = 0, ptr = 0; - int ni = (this.r >> 3) / ww; - while (olen > 0) - { - int i = 0; - while ((i < ni) && (j < nn)) - { - long v = this.S[(i % 5) * 5 + i / 5]; - for (int k = 0; k < ww; k++) - { - if (j < nn) - { - rc[ptr] = (byte)v; - ptr += 1; - } - v >>= 8; - j += 1; - } - i += 1; - } - olen -= this.r; - if (olen > 0) - this.keccakF(this.S); - } - if ((this.n & 7) != 0) - rc[rc.length - 1] &= (1 << (this.n & 7)) - 1; - - return rc; - } - -} diff --git a/java/SHA3.java b/java/SHA3.java deleted file mode 100644 index 9f41a00..0000000 --- a/java/SHA3.java +++ /dev/null @@ -1,969 +0,0 @@ -/** - * sha3sum – SHA-3 (Keccak) checksum calculator - * - * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - - -/** - * SHA-3/Keccak hash algorithm implementation - * - * @author Mattias Andrée <a href="mailto:maandree@member.fsf.org">maandree@member.fsf.org</a> - */ -public class SHA3 -{ - /** - * Suffix the message when calculating the Keccak hash sum - */ - public static final String KECCAK_SUFFIX = ""; - - /** - * Suffix the message when calculating the SHA-3 hash sum - */ - public static final String SHA3_SUFFIX = "01"; - - /** - * Suffix the message when calculating the RawSHAKE hash sum - */ - public static final String RawSHAKE_SUFFIX = "11"; - - /** - * Suffix the message when calculating the SHAKE hash sum - */ - public static final String SHAKE_SUFFIX = "1111"; - - - /** - * Round contants - */ - private static final long[] RC = { - 0x0000000000000001L, 0x0000000000008082L, 0x800000000000808AL, 0x8000000080008000L, - 0x000000000000808BL, 0x0000000080000001L, 0x8000000080008081L, 0x8000000000008009L, - 0x000000000000008AL, 0x0000000000000088L, 0x0000000080008009L, 0x000000008000000AL, - 0x000000008000808BL, 0x800000000000008BL, 0x8000000000008089L, 0x8000000000008003L, - 0x8000000000008002L, 0x8000000000000080L, 0x000000000000800AL, 0x800000008000000AL, - 0x8000000080008081L, 0x8000000000008080L, 0x0000000080000001L, 0x8000000080008008L}; - - /** - * Keccak-f round temporary - */ - private static long[] B = new long[25]; - - /** - * Keccak-f round temporary - */ - private static long[] C = new long[5]; - - - /** - * The bitrate - */ - private static int r = 0; - - /** - * The capacity - */ - private static int c = 0; - - /** - * The output size - */ - private static int n = 0; - - /** - * The state size - */ - private static int b = 0; - - /** - * The word size - */ - private static int w = 0; - - /** - * The word mask - */ - private static long wmod = 0; - - /** - * ℓ, the binary logarithm of the word size - */ - private static int l = 0; - - /** - * 12 + 2ℓ, the number of rounds - */ - private static int nr = 0; - - - /** - * Message chunk that is being processes - */ - private static byte[] message = null; - - /** - * The current state - */ - private static long[] S = null; - - /** - * Left over water to fill the sponge with at next update - */ - private static byte[] M = null; - - /** - * Pointer for {@link #M} - */ - private static int mptr = 0; - - - - /** - * Hidden constructor - */ - private SHA3() - { - // Inhibit instansiation - } - - - - /** - * Rotate a word - * - * @param x The value to rotate - * @param n Rotation steps, may not be 0 - * @return The value rotated - */ - private static long rotate(long x, int n) - { - long m; - return ((x >>> (SHA3.w - (m = n % SHA3.w))) + (x << m)) & SHA3.wmod; - } - - - /** - * Rotate a 64-bit word - * - * @param x The value to rotate - * @param n Rotation steps, may not be 0 - * @return The value rotated - */ - private static long rotate64(long x, int n) - { - return (x >>> (64 - n)) + (x << n); - } - - - /** - * Binary logarithm - * - * @param x The value of which to calculate the binary logarithm - * @return The binary logarithm - */ - private static int lb(int x) - { - int rc = 0; - if ((x & 0xFF00) != 0) { rc += 8; x >>= 8; } - if ((x & 0x00F0) != 0) { rc += 4; x >>= 4; } - if ((x & 0x000C) != 0) { rc += 2; x >>= 2; } - if ((x & 0x0002) != 0) rc += 1; - return rc; - } - - /** - * Perform one round of computation - * - * @param A The current state - * @param rc Round constant - */ - private static void keccakFRound(long[] A, long rc) - { - /* θ step (step 1 of 3) */ - for (int i = 0, j = 0; i < 5; i++, j += 5) - SHA3.C[i] = (A[j] ^ A[j + 1]) ^ (A[j + 2] ^ A[j + 3]) ^ A[j + 4]; - - long da, db, dc, dd, de; - - if (SHA3.w == 64) - { - /* ρ and π steps, with last two part of θ */ - SHA3.B[0] = A[ 0] ^ (da = SHA3.C[4] ^ SHA3.rotate64(SHA3.C[1], 1)); - SHA3.B[1] = SHA3.rotate64(A[15] ^ (dd = SHA3.C[2] ^ SHA3.rotate64(SHA3.C[4], 1)), 28); - SHA3.B[2] = SHA3.rotate64(A[ 5] ^ (db = SHA3.C[0] ^ SHA3.rotate64(SHA3.C[2], 1)), 1); - SHA3.B[3] = SHA3.rotate64(A[20] ^ (de = SHA3.C[3] ^ SHA3.rotate64(SHA3.C[0], 1)), 27); - SHA3.B[4] = SHA3.rotate64(A[10] ^ (dc = SHA3.C[1] ^ SHA3.rotate64(SHA3.C[3], 1)), 62); - - SHA3.B[5] = SHA3.rotate64(A[ 6] ^ db, 44); - SHA3.B[6] = SHA3.rotate64(A[21] ^ de, 20); - SHA3.B[7] = SHA3.rotate64(A[11] ^ dc, 6); - SHA3.B[8] = SHA3.rotate64(A[ 1] ^ da, 36); - SHA3.B[9] = SHA3.rotate64(A[16] ^ dd, 55); - - SHA3.B[10] = SHA3.rotate64(A[12] ^ dc, 43); - SHA3.B[11] = SHA3.rotate64(A[ 2] ^ da, 3); - SHA3.B[12] = SHA3.rotate64(A[17] ^ dd, 25); - SHA3.B[13] = SHA3.rotate64(A[ 7] ^ db, 10); - SHA3.B[14] = SHA3.rotate64(A[22] ^ de, 39); - - SHA3.B[15] = SHA3.rotate64(A[18] ^ dd, 21); - SHA3.B[16] = SHA3.rotate64(A[ 8] ^ db, 45); - SHA3.B[17] = SHA3.rotate64(A[23] ^ de, 8); - SHA3.B[18] = SHA3.rotate64(A[13] ^ dc, 15); - SHA3.B[19] = SHA3.rotate64(A[ 3] ^ da, 41); - - SHA3.B[20] = SHA3.rotate64(A[24] ^ de, 14); - SHA3.B[21] = SHA3.rotate64(A[14] ^ dc, 61); - SHA3.B[22] = SHA3.rotate64(A[ 4] ^ da, 18); - SHA3.B[23] = SHA3.rotate64(A[19] ^ dd, 56); - SHA3.B[24] = SHA3.rotate64(A[ 9] ^ db, 2); - } - else - { - /* ρ and π steps, with last two part of θ */ - SHA3.B[0] = A[ 0] ^ (da = SHA3.C[4] ^ SHA3.rotate(SHA3.C[1], 1)); - SHA3.B[1] = SHA3.rotate(A[15] ^ (dd = SHA3.C[2] ^ SHA3.rotate(SHA3.C[4], 1)), 28); - SHA3.B[2] = SHA3.rotate(A[ 5] ^ (db = SHA3.C[0] ^ SHA3.rotate(SHA3.C[2], 1)), 1); - SHA3.B[3] = SHA3.rotate(A[20] ^ (de = SHA3.C[3] ^ SHA3.rotate(SHA3.C[0], 1)), 27); - SHA3.B[4] = SHA3.rotate(A[10] ^ (dc = SHA3.C[1] ^ SHA3.rotate(SHA3.C[3], 1)), 62); - - SHA3.B[5] = SHA3.rotate(A[ 6] ^ db, 44); - SHA3.B[6] = SHA3.rotate(A[21] ^ de, 20); - SHA3.B[7] = SHA3.rotate(A[11] ^ dc, 6); - SHA3.B[8] = SHA3.rotate(A[ 1] ^ da, 36); - SHA3.B[9] = SHA3.rotate(A[16] ^ dd, 55); - - SHA3.B[10] = SHA3.rotate(A[12] ^ dc, 43); - SHA3.B[11] = SHA3.rotate(A[ 2] ^ da, 3); - SHA3.B[12] = SHA3.rotate(A[17] ^ dd, 25); - SHA3.B[13] = SHA3.rotate(A[ 7] ^ db, 10); - SHA3.B[14] = SHA3.rotate(A[22] ^ de, 39); - - SHA3.B[15] = SHA3.rotate(A[18] ^ dd, 21); - SHA3.B[16] = SHA3.rotate(A[ 8] ^ db, 45); - SHA3.B[17] = SHA3.rotate(A[23] ^ de, 8); - SHA3.B[18] = SHA3.rotate(A[13] ^ dc, 15); - SHA3.B[19] = SHA3.rotate(A[ 3] ^ da, 41); - - SHA3.B[20] = SHA3.rotate(A[24] ^ de, 14); - SHA3.B[21] = SHA3.rotate(A[14] ^ dc, 61); - SHA3.B[22] = SHA3.rotate(A[ 4] ^ da, 18); - SHA3.B[23] = SHA3.rotate(A[19] ^ dd, 56); - SHA3.B[24] = SHA3.rotate(A[ 9] ^ db, 2); - } - - /* ξ step */ - for (int i = 0; i < 15; i++) - A[i ] = SHA3.B[i ] ^ ((~(SHA3.B[i + 5])) & SHA3.B[i + 10]); - for (int i = 0; i < 5; i++) - { - A[i + 15] = SHA3.B[i + 15] ^ ((~(SHA3.B[i + 20])) & SHA3.B[i ]); - A[i + 20] = SHA3.B[i + 20] ^ ((~(SHA3.B[i ])) & SHA3.B[i + 5]); - } - - /* ι step */ - A[0] ^= rc; - } - - - /** - * Perform Keccak-f function - * - * @param A The current state - */ - private static void keccakF(long[] A) - { - if (SHA3.nr == 24) - for (int i = 0; i < 24; i++) - SHA3.keccakFRound(A, SHA3.RC[i]); - else - for (int i = 0; i < SHA3.nr; i++) - SHA3.keccakFRound(A, SHA3.RC[i] & SHA3.wmod); - } - - - /** - * Convert a chunk of byte:s to a word - * - * @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 n, int ww, int off) - { - long rc = 0; - for (int i = off + ww - 1; i >= off; i--) - rc = (rc << 8) | ((i < n) ? (long)(SHA3.message[i] & 255) : 0L); - return rc; - } - - - /** - * Convert a chunk of byte:s to a 64-bit word - * - * @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 n, int off) - { - 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) | - ((off + 4 < n) ? ((long)(SHA3.message[off + 4] & 255) << 32) : 0L) | - ((off + 3 < n) ? ((long)(SHA3.message[off + 3] & 255) << 24) : 0L) | - ((off + 2 < n) ? ((long)(SHA3.message[off + 2] & 255) << 16) : 0L) | - ((off + 1 < n) ? ((long)(SHA3.message[off + 1] & 255) << 8) : 0L) | - ((off < n) ? ((long)(SHA3.message[off] & 255)) : 0L); - } - - - /** - * pad 10*1 - * - * @param msg The message to pad - * @param len The length of the message - * @param r The bitrate - * @param bits The number of bits in the end of the message that does not make a whole byte - * @return The actual length of {@link #message} - */ - private static int pad10star1(byte[] msg, int len, int r, int bits) - { - len = ((len - (bits + 7) / 8) << 3) + bits; - - int nrf = len >> 3; - int nbrf = len & 7; - int ll = len % r; - - byte b = (byte)(nbrf == 0 ? 1 : (msg[nrf] | (1 << nbrf))); - - if ((r - 8 <= ll) && (ll <= r - 2)) - { - SHA3.message = new byte[len = nrf + 1]; - SHA3.message[nrf] = (byte)(b ^ 128); - } - else - { - len = (nrf + 1) << 3; - len = ((len - (len % r) + (r - 8)) >> 3) + 1; - SHA3.message = new byte[len]; - SHA3.message[nrf] = b; - SHA3.message[len - 1] = -128; - } - - System.arraycopy(msg, 0, SHA3.message, 0, nrf); - return len; - } - - - /** - * Initialise Keccak sponge - * - * @param r The bitrate - * @param c The capacity - * @param n The output size - */ - public static void initialise(int r, int c, int n) - { - SHA3.r = r; - SHA3.c = c; - SHA3.n = n; - SHA3.b = r + c; - SHA3.w = SHA3.b / 25; - SHA3.l = SHA3.lb(SHA3.w); - SHA3.nr = 12 + (SHA3.l << 1); - SHA3.wmod = w == 64 ? -1L : (1L << SHA3.w) - 1L; - SHA3.S = new long[25]; - if ((SHA3.M == null) || ((SHA3.r * SHA3.b) >> 2 != SHA3.M.length)) - SHA3.M = new byte[(SHA3.r * SHA3.b) >> 2]; - SHA3.mptr = 0; - if (SHA3.message == null) - SHA3.message = new byte[8 << 10]; - } - - - /** - * Free up static resources - */ - public static void dispose() - { - SHA3.S = null; - SHA3.M = null; - SHA3.message = null; - } - - - /** - * Absorb the more of the message message to the Keccak sponge - * - * @param msg The partial message - */ - public static void update(byte[] msg) - { - update(msg, msg.length); - } - - - /** - * Absorb the more of the message message to the Keccak sponge - * - * @param msg The partial message - * @param msglen The length of the partial message - */ - public static void update(byte[] msg, int msglen) - { - int rr = SHA3.r >> 3; - int ww = SHA3.w >> 3; - - if (SHA3.mptr + msglen > SHA3.M.length) - System.arraycopy(SHA3.M, 0, SHA3.M = new byte[(SHA3.M.length + msglen) << 1], 0, SHA3.mptr); - System.arraycopy(msg, 0, SHA3.M, SHA3.mptr, msglen); - int len = SHA3.mptr += msglen; - len -= len % ((SHA3.r * SHA3.b) >> 3); - System.arraycopy(SHA3.M, 0, (SHA3.message.length < len) ? (SHA3.message = new byte[len]) : SHA3.message, 0, len); - System.arraycopy(SHA3.M, len, SHA3.M, 0, SHA3.mptr -= len); - int n = Math.min(len, rr); - - /* Absorbing phase */ - if (ww == 8) - for (int i = 0; i < len; i += rr) - { - 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(n, ww, i + 0 ); - SHA3.S[ 5] ^= SHA3.toLane(n, ww, i + ww); - SHA3.S[10] ^= SHA3.toLane(n, ww, i + 2 * ww); - SHA3.S[15] ^= SHA3.toLane(n, ww, i + 3 * ww); - SHA3.S[20] ^= SHA3.toLane(n, ww, i + 4 * ww); - SHA3.S[ 1] ^= SHA3.toLane(n, ww, i + 5 * ww); - SHA3.S[ 6] ^= SHA3.toLane(n, ww, i + 6 * ww); - SHA3.S[11] ^= SHA3.toLane(n, ww, i + 7 * ww); - SHA3.S[16] ^= SHA3.toLane(n, ww, i + 8 * ww); - SHA3.S[21] ^= SHA3.toLane(n, ww, i + 9 * ww); - SHA3.S[ 2] ^= SHA3.toLane(n, ww, i + 10 * ww); - SHA3.S[ 7] ^= SHA3.toLane(n, ww, i + 11 * ww); - SHA3.S[12] ^= SHA3.toLane(n, ww, i + 12 * ww); - SHA3.S[17] ^= SHA3.toLane(n, ww, i + 13 * ww); - SHA3.S[22] ^= SHA3.toLane(n, ww, i + 14 * ww); - SHA3.S[ 3] ^= SHA3.toLane(n, ww, i + 15 * ww); - SHA3.S[ 8] ^= SHA3.toLane(n, ww, i + 16 * ww); - SHA3.S[13] ^= SHA3.toLane(n, ww, i + 17 * ww); - SHA3.S[18] ^= SHA3.toLane(n, ww, i + 18 * ww); - SHA3.S[23] ^= SHA3.toLane(n, ww, i + 19 * ww); - SHA3.S[ 4] ^= SHA3.toLane(n, ww, i + 20 * ww); - SHA3.S[ 9] ^= SHA3.toLane(n, ww, i + 21 * ww); - SHA3.S[14] ^= SHA3.toLane(n, ww, i + 22 * ww); - SHA3.S[19] ^= SHA3.toLane(n, ww, i + 23 * ww); - SHA3.S[24] ^= SHA3.toLane(n, ww, i + 24 * ww); - SHA3.keccakF(SHA3.S); - n += rr; - } - } - - - /** - * Squeeze the Keccak sponge - * - * @return The hash sum - */ - public static byte[] digest() - { - return digest(null, 0, 0, SHA3.SHA3_SUFFIX, true); - } - - - /** - * Squeeze the Keccak sponge - * - * @param suffix The suffix concatenate to the message - * @return The hash sum - */ - public static byte[] digest(String suffix) - { - return digest(null, 0, 0, suffix, true); - } - - - /** - * Squeeze the Keccak sponge - * - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public static byte[] digest(boolean withReturn) - { - return digest(null, 0, 0, SHA3.SHA3_SUFFIX, withReturn); - } - - - /** - * Squeeze the Keccak sponge - * - * @param suffix The suffix concatenate to the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public static byte[] digest(String suffix, boolean withReturn) - { - return digest(null, 0, 0, suffix, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @return The hash sum - */ - public static byte[] digest(byte[] msg) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param suffix The suffix concatenate to the message - * @return The hash sum - */ - public static byte[] digest(byte[] msg, String suffix) - { - return digest(msg, msg == null ? 0 : msg.length, 0, suffix, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public static byte[] digest(byte[] msg, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param suffix The suffix concatenate to the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public static byte[] digest(byte[] msg, String suffix, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, suffix, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @return The hash sum - */ - public static byte[] digest(byte[] msg, int msglen) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param suffix The suffix concatenate to the message - * @return The hash sum - */ - public static byte[] digest(byte[] msg, int msglen, String suffix) - { - return digest(msg, msg == null ? 0 : msg.length, 0, suffix, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param bits The number of bits at the end of the message not covered by <tt>msglen</tt> - * @return The hash sum - */ - public static byte[] digest(byte[] msg, int msglen, int bits) - { - return digest(msg, msg == null ? 0 : msg.length, bits, SHA3.SHA3_SUFFIX, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param bits The number of bits at the end of the message not covered by <tt>msglen</tt> - * @param suffix The suffix concatenate to the message - * @return The hash sum - */ - public static byte[] digest(byte[] msg, int msglen, int bits, String suffix) - { - return digest(msg, msg == null ? 0 : msg.length, bits, suffix, true); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public static byte[] digest(byte[] msg, int msglen, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param suffix The suffix concatenate to the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public static byte[] digest(byte[] msg, int msglen, String suffix, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, suffix, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param bits The number of bits at the end of the message not covered by <tt>msglen</tt> - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public static byte[] digest(byte[] msg, int msglen, int bits, boolean withReturn) - { - return digest(msg, msg == null ? 0 : msg.length, 0, SHA3.SHA3_SUFFIX, withReturn); - } - - - /** - * Absorb the last part of the message and squeeze the Keccak sponge - * - * @param msg The rest of the message - * @param msglen The length of the partial message in while bytes - * @param bits The number of bits at the end of the message not covered by <tt>msglen</tt> - * @param suffix The suffix concatenate to the message - * @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null} - * @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false} - */ - public static byte[] digest(byte[] msg, int msglen, int bits, String suffix, boolean withReturn) - { - int len; - byte last_byte = 0; - if ((msg == null) || (msglen == 0)) - { - msg = new byte[0]; - bits = 0; - } - - msglen += bits / 8; - if ((bits %= 8) != 0) - last_byte = msg[msglen]; - - byte[] msg_end = new byte[(suffix.length() + bits + 7) / 8]; - int msg_end_ptr = 0; - for (int i = 0, n = suffix.length(); i < n; i++) - { - byte bit = (byte)(suffix.charAt(i) - '0'); - last_byte |= bit << bits++; - if (bits == 8) - { - msg_end[msg_end_ptr++] = last_byte; - last_byte = 0; - bits = 0; - } - } - if (bits != 0) - msg_end[msg_end_ptr++] = last_byte; - if (msg_end_ptr > 0) - { - if (msglen + msg_end_ptr > msg.length) - System.arraycopy(msg, 0, msg = new byte[msglen + msg_end_ptr], 0, msglen); - System.arraycopy(msg_end, 0, msg, msglen, msg_end_ptr); - msglen += msg_end_ptr; - } - - if (SHA3.mptr + msglen > SHA3.M.length) - System.arraycopy(SHA3.M, 0, SHA3.M = new byte[SHA3.M.length + msglen], 0, SHA3.mptr); - System.arraycopy(msg, 0, SHA3.M, SHA3.mptr, msglen); - len = SHA3.pad10star1(SHA3.M, SHA3.mptr + msglen, SHA3.r, bits); - - int rr = SHA3.r >> 3; - int nn = (SHA3.n + 7) >> 3; - int ww = SHA3.w >> 3; - - int n = Math.min(len, rr); - - /* Absorbing phase */ - if (ww == 8) - for (int i = 0; i < len; i += rr) - { - 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(n, ww, i + 0 ); - SHA3.S[ 5] ^= SHA3.toLane(n, ww, i + ww); - SHA3.S[10] ^= SHA3.toLane(n, ww, i + 2 * ww); - SHA3.S[15] ^= SHA3.toLane(n, ww, i + 3 * ww); - SHA3.S[20] ^= SHA3.toLane(n, ww, i + 4 * ww); - SHA3.S[ 1] ^= SHA3.toLane(n, ww, i + 5 * ww); - SHA3.S[ 6] ^= SHA3.toLane(n, ww, i + 6 * ww); - SHA3.S[11] ^= SHA3.toLane(n, ww, i + 7 * ww); - SHA3.S[16] ^= SHA3.toLane(n, ww, i + 8 * ww); - SHA3.S[21] ^= SHA3.toLane(n, ww, i + 9 * ww); - SHA3.S[ 2] ^= SHA3.toLane(n, ww, i + 10 * ww); - SHA3.S[ 7] ^= SHA3.toLane(n, ww, i + 11 * ww); - SHA3.S[12] ^= SHA3.toLane(n, ww, i + 12 * ww); - SHA3.S[17] ^= SHA3.toLane(n, ww, i + 13 * ww); - SHA3.S[22] ^= SHA3.toLane(n, ww, i + 14 * ww); - SHA3.S[ 3] ^= SHA3.toLane(n, ww, i + 15 * ww); - SHA3.S[ 8] ^= SHA3.toLane(n, ww, i + 16 * ww); - SHA3.S[13] ^= SHA3.toLane(n, ww, i + 17 * ww); - SHA3.S[18] ^= SHA3.toLane(n, ww, i + 18 * ww); - SHA3.S[23] ^= SHA3.toLane(n, ww, i + 19 * ww); - SHA3.S[ 4] ^= SHA3.toLane(n, ww, i + 20 * ww); - SHA3.S[ 9] ^= SHA3.toLane(n, ww, i + 21 * ww); - SHA3.S[14] ^= SHA3.toLane(n, ww, i + 22 * ww); - SHA3.S[19] ^= SHA3.toLane(n, ww, i + 23 * ww); - SHA3.S[24] ^= SHA3.toLane(n, ww, i + 24 * ww); - SHA3.keccakF(SHA3.S); - n += rr; - } - - /* Squeezing phase */ - if (withReturn) - { - byte[] rc = new byte[(SHA3.n + 7) >> 3]; - int ptr = 0; - - int olen = SHA3.n; - int j = 0; - int ni = rr / ww; - while (olen > 0) - { - int i = 0; - while ((i < ni) && (j < nn)) - { - long v = SHA3.S[(i % 5) * 5 + i / 5]; - for (int k = 0; k < ww; k++) - { - if (j < nn) - { - rc[ptr] = (byte)v; - ptr += 1; - } - v >>= 8; - j += 1; - } - i += 1; - } - olen -= SHA3.r; - if (olen > 0) - SHA3.keccakF(SHA3.S); - } - if ((SHA3.n & 7) != 0) - rc[rc.length - 1] &= (1 << (SHA3.n & 7)) - 1; - - return rc; - } - int olen = SHA3.n; - while ((olen -= SHA3.r) > 0) - SHA3.keccakF(SHA3.S); - return null; - } - - - /** - * Force a round of Keccak-f - */ - public static void simpleSqueeze() - { - SHA3.keccakF(SHA3.S); - } - - - /** - * Force some rounds of Keccak-f - * - * @param times The number of rounds - */ - public static void simpleSqueeze(int times) - { - for (int i = 0; i < times; i++) - SHA3.keccakF(SHA3.S); - } - - - /** - * Squeeze as much as is needed to get a digest - */ - public static void fastSqueeze() - { - SHA3.keccakF(SHA3.S); /* Last squeeze did not do a ending squeeze */ - int olen = SHA3.n; - while ((olen -= SHA3.r) > 0) - SHA3.keccakF(SHA3.S); - } - - - /** - * Squeeze as much as is needed to get a digest a number of times - * - * @param times The number of digests - */ - public static void fastSqueeze(int times) - { - for (int i = 0; i < times; i++) - { - SHA3.keccakF(SHA3.S); /* Last squeeze did not do a ending squeeze */ - int olen = SHA3.n; - while ((olen -= SHA3.r) > 0) - SHA3.keccakF(SHA3.S); - } - } - - - /** - * Squeeze out another digest - * - * @return The hash sum - */ - public static byte[] squeeze() - { - SHA3.keccakF(SHA3.S); /* Last squeeze did not do a ending squeeze */ - - int nn, ww = SHA3.w >> 3; - byte[] rc = new byte[nn = (SHA3.n + 7) >> 3]; - - int olen = SHA3.n; - int j = 0, ptr = 0; - int ni = (SHA3.r >> 3) / ww; - while (olen > 0) - { - int i = 0; - while ((i < ni) && (j < nn)) - { - long v = SHA3.S[(i % 5) * 5 + i / 5]; - for (int k = 0; k < ww; k++) - { - if (j < nn) - { - rc[ptr] = (byte)v; - ptr += 1; - } - v >>= 8; - j += 1; - } - i += 1; - } - olen -= SHA3.r; - if (olen > 0) - SHA3.keccakF(SHA3.S); - } - if ((SHA3.n & 7) != 0) - rc[rc.length - 1] &= (1 << (SHA3.n & 7)) - 1; - - return rc; - } - -} diff --git a/java/sha3_224sum.java b/java/sha3_224sum.java deleted file mode 100644 index d6717d5..0000000 --- a/java/sha3_224sum.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * sha3sum – SHA-3 (Keccak) checksum calculator - * - * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -import java.io.*; -import java.util.*; - - -/** - * SHA-3/Keccak checksum calculator with 224 bit output - * - * @author Mattias Andrée <a href="mailto:maandree@member.fsf.org">maandree@member.fsf.org</a> - */ -public class sha3_224sum -{ - /** - * This is the main entry point of the program - * - * @param args Command line arguments - * @throws IOException On I/O error (such as broken pipes) - */ - public static void main(String[] args) throws IOException - { - sha3sum.run("sha3-224sum", args); - } -} - diff --git a/java/sha3_256sum.java b/java/sha3_256sum.java deleted file mode 100644 index 06375f2..0000000 --- a/java/sha3_256sum.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * sha3sum – SHA-3 (Keccak) checksum calculator - * - * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -import java.io.*; -import java.util.*; - - -/** - * SHA-3/Keccak checksum calculator with 256 bit output - * - * @author Mattias Andrée <a href="mailto:maandree@member.fsf.org">maandree@member.fsf.org</a> - */ -public class sha3_256sum -{ - /** - * This is the main entry point of the program - * - * @param args Command line arguments - * @throws IOException On I/O error (such as broken pipes) - */ - public static void main(String[] args) throws IOException - { - sha3sum.run("sha3-256sum", args); - } -} - diff --git a/java/sha3_384sum.java b/java/sha3_384sum.java deleted file mode 100644 index 40128f4..0000000 --- a/java/sha3_384sum.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * sha3sum – SHA-3 (Keccak) checksum calculator - * - * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -import java.io.*; -import java.util.*; - - -/** - * SHA-3/Keccak checksum calculator with 384 bit output - * - * @author Mattias Andrée <a href="mailto:maandree@member.fsf.org">maandree@member.fsf.org</a> - */ -public class sha3_384sum -{ - /** - * This is the main entry point of the program - * - * @param args Command line arguments - * @throws IOException On I/O error (such as broken pipes) - */ - public static void main(String[] args) throws IOException - { - sha3sum.run("sha3-384sum", args); - } -} - diff --git a/java/sha3_512sum.java b/java/sha3_512sum.java deleted file mode 100644 index 60387d5..0000000 --- a/java/sha3_512sum.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * sha3sum – SHA-3 (Keccak) checksum calculator - * - * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -import java.io.*; -import java.util.*; - - -/** - * SHA-3/Keccak checksum calculator with 512 bit output - * - * @author Mattias Andrée <a href="mailto:maandree@member.fsf.org">maandree@member.fsf.org</a> - */ -public class sha3_512sum -{ - /** - * This is the main entry point of the program - * - * @param args Command line arguments - * @throws IOException On I/O error (such as broken pipes) - */ - public static void main(String[] args) throws IOException - { - sha3sum.run("sha3-512sum", args); - } -} - diff --git a/java/sha3sum.java b/java/sha3sum.java deleted file mode 100644 index 7d70caf..0000000 --- a/java/sha3sum.java +++ /dev/null @@ -1,485 +0,0 @@ -/** - * sha3sum – SHA-3 (Keccak) checksum calculator - * - * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -import java.io.*; -import java.util.*; - - -/** - * SHA-3/Keccak checksum calculator - * - * @author Mattias Andrée <a href="mailto:maandree@member.fsf.org">maandree@member.fsf.org</a> - */ -public class sha3sum -{ - /** - * This is the main entry point of the program - * - * @param args Command line arguments - * @throws IOException On I/O error (such as broken pipes) - */ - public static void main(String[] args) throws IOException - { - run("sha3sum", args); - } - - - /** - * Run the program - * - * @param cmd The command - * @param argv Command line arguments - * @throws IOException On I/O error (such as broken pipes) - */ - public static void run(String cmd, String[] argv) throws IOException - { - if (cmd.indexOf('/') >= 0) - cmd = cmd.substring(cmd.lastIndexOf('/') + 1); - if (cmd.endsWith(".jar")) - cmd = cmd.substring(0, cmd.length() - 4); - cmd = cmd.intern(); - - Integer O = null; int _o = 512; /* --outputsize */ - if (cmd == "sha3-224sum") _o = 224; - else if (cmd == "sha3-256sum") _o = 256; - else if (cmd == "sha3-384sum") _o = 384; - else if (cmd == "sha3-512sum") _o = 512; - Integer S = null; int _s = 1600; /* --statesize */ - Integer C = null; int _c = _s - (_o << 1); /* --capacity */ - Integer R = null; int _r = _s - _c; /* --bitrate */ - Integer W = null; int _w = _s / 25; /* --wordsize */ - Integer I = null; int _i = 1; /* --iterations */ - Integer J = null; int _j = 1; /* --squeezes */ - int o = 0, s = 0, r = 0, c = 0, w = 0, i = 0, j = 0; - - boolean binary = false, hex = false; - int multi = 0; - - String[] files = new String[argv.length + 1]; - int fptr = 0; - boolean dashed = false; - String[] linger = null; - - String[] args = new String[argv.length + 1]; - System.arraycopy(argv, 0, args, 0, argv.length); - for (int a = 0, an = args.length; a < an; a++) - { String arg = args[a]; - arg = arg == null ? null : arg.intern(); - if (linger != null) - { - linger[0] = linger[0].intern(); - if ((linger[0] == "-h") || (linger[0] == "--help")) - { - System.out.println(""); - System.out.println("SHA-3/Keccak checksum calculator"); - System.out.println(""); - System.out.println("USAGE: sha3sum [option...] < file"); - System.out.println(" sha3sum [option...] file..."); - System.out.println(""); - System.out.println(""); - System.out.println("OPTIONS:"); - System.out.println(" -r BITRATE"); - System.out.println(" --bitrate The bitrate to use for ckecksum. (default: " + _r + ")"); - System.out.println(" "); - System.out.println(" -c CAPACITY"); - System.out.println(" --capacity The capacity to use for checksum. (default: " + _c + ")"); - System.out.println(" "); - System.out.println(" -w WORDSIZE"); - System.out.println(" --wordsize The word size to use for checksum. (default: " + _w + ")"); - System.out.println(" "); - System.out.println(" -o OUTPUTSIZE"); - System.out.println(" --outputsize The output size to use for checksum. (default: " + _o + ")"); - System.out.println(" "); - System.out.println(" -s STATESIZE"); - System.out.println(" --statesize The state size to use for checksum. (default: " + _s + ")"); - System.out.println(" "); - System.out.println(" -i ITERATIONS"); - System.out.println(" --iterations The number of hash iterations to run. (default: " + _i + ")"); - System.out.println(" "); - System.out.println(" -j SQUEEZES"); - System.out.println(" --squeezes The number of hash squeezes to run. (default: " + _j + ")"); - System.out.println(" "); - System.out.println(" -x"); - System.out.println(" --hex Read the input in hexadecimal, rather than binary."); - System.out.println(" "); - System.out.println(" -b"); - System.out.println(" --binary Print the checksum in binary, rather than hexadecimal."); - System.out.println(" "); - System.out.println(" -m"); - System.out.println(" --multi Print the checksum at all iterations."); - System.out.println(""); - System.out.println(""); - System.out.println("COPYRIGHT:"); - System.out.println(""); - System.out.println("Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org)"); - System.out.println(""); - System.out.println("This program is free software: you can redistribute it and/or modify"); - System.out.println("it under the terms of the GNU Affero General Public License as published by"); - System.out.println("the Free Software Foundation, either version 3 of the License, or"); - System.out.println("(at your option) any later version."); - System.out.println(""); - System.out.println("This program is distributed in the hope that it will be useful,"); - System.out.println("but WITHOUT ANY WARRANTY; without even the implied warranty of"); - System.out.println("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"); - System.out.println("GNU Affero General Public License for more details."); - System.out.println(""); - System.out.println("You should have received a copy of the GNU Affero General Public License"); - System.out.println("along with this program. If not, see <http://www.gnu.org/licenses/>."); - System.out.println(""); - System.exit(0); - } - else - { - if (linger[1] == null) - { - linger[1] = arg; - arg = null; - } - if ((linger[0] == "-r") || (linger[0] == "--bitrate")) - R = Integer.valueOf(linger[1]); - else if ((linger[0] == "-c") || (linger[0] == "--capacity")) - C = Integer.valueOf(linger[1]); - else if ((linger[0] == "-w") || (linger[0] == "--wordsize")) - W = Integer.valueOf(linger[1]); - else if ((linger[0] == "-o") || (linger[0] == "--outputsize")) - O = Integer.valueOf(linger[1]); - else if ((linger[0] == "-s") || (linger[0] == "--statesize")) - S = Integer.valueOf(linger[1]); - else if ((linger[0] == "-i") || (linger[0] == "--iterations")) - I = Integer.valueOf(linger[1]); - else if ((linger[0] == "-j") || (linger[0] == "--squeezes")) - J = Integer.valueOf(linger[1]); - else - { - System.err.println(cmd + ": unrecognised option: " + linger[0]); - System.exit(1); - } - } - linger = null; - if (arg == null) - continue; - } - if (arg == null) - continue; - if (dashed) - files[fptr++] = arg == "-" ? null : arg; - else if (arg == "--") - dashed = true; - else if (arg == "-") - files[fptr++] = null; - else if (arg.startsWith("--")) - if (arg.indexOf('=') >= 0) - linger = new String[] { arg.substring(0, arg.indexOf('=')), arg.substring(arg.indexOf('=') + 1) }; - else - if (arg == "--binary") - binary = true; - else if (arg == "--multi") - multi++; - else if (arg == "--hex") - hex = true; - else - linger = new String[] { arg, null }; - else if (arg.startsWith("-")) - { - arg = arg.substring(1); - if (arg.charAt(0) == 'b') - { - binary = true; - arg = arg.substring(1); - } - else if (arg.charAt(0) == 'm') - { - multi++; - arg = arg.substring(1); - } - else if (arg.charAt(0) == 'x') - { - hex = true; - arg = arg.substring(1); - } - else if (arg.length() == 1) - linger = new String[] { "-" + arg, null }; - else - linger = new String[] { "-" + arg.charAt(0), arg.substring(1) }; - } - else - files[fptr++] = arg; - } - - - i = I == null ? _i : I.intValue(); - j = J == null ? _j : J.intValue(); - - if (S != null) - { s = S.intValue(); - if ((s <= 0) || (s > 1600) || (s % 25 != 0)) - { System.err.println(cmd + ": the state size must be a positive multiple of 25 and is limited to 1600."); - System.exit(6); - } } - - if (W != null) - { w = W.intValue(); - if ((w <= 0) || (w > 64)) - { System.err.println(cmd + ": the word size must be positive and is limited to 64."); - System.exit(6); - } - if ((S != null) && (s != w * 25)) - { System.err.println(cmd + ": the state size must be 25 times of the word size."); - System.exit(6); - } - else if (S == null) - S = new Integer(w * 25); - } - - if (C != null) - { c = C.intValue(); - if ((c <= 0) || ((c & 7) != 0)) - { System.err.println(cmd + ": the capacity must be a positive multiple of 8."); - System.exit(6); - } } - - if (R != null) - { r = R.intValue(); - if ((r <= 0) || ((r & 7) != 0)) - { System.err.println(cmd + ": the bitrate must be a positive multiple of 8."); - System.exit(6); - } } - - if (O != null) - { o = O.intValue(); - if (o <= 0) - { System.err.println(cmd + ": the output size must be positive."); - System.exit(6); - } } - - - if ((R == null) && (C == null) && (O == null)) // s? - { c = -((r = (o = ((((s = S == null ? _s : s) << 5) / 100 + 7) >> 3) << 3) << 1) - s); - o = o < 8 ? 8 : o; - } - else if ((R == null) && (C == null)) // !o s? - { r = _r; - c = _c; - s = S == null ? (r + c) : s; - } - else if (R == null) // !c o? s? - { r = (s = S == null ? _s : s) - c; - o = O == null ? (c == 8 ? 8 : (c << 1)) : o; - } - else if (C == null) // !r o? s? - { c = (s = S == null ? _s : s) - r; - o = O == null ? (c == 8 ? 8 : (c << 1)) : o; - } - else // !r !c o? s? - { s = S == null ? (r + c) : s; - o = O == null ? (c == 8 ? 8 : (c << 1)) : o; - } - - - System.err.println("Bitrate: " + r); - System.err.println("Capacity: " + c); - System.err.println("Word size: " + w); - System.err.println("State size: " + s); - System.err.println("Output size: " + o); - System.err.println("Iterations: " + i); - System.err.println("Squeezes: " + j); - - - if (r > s) - { System.err.println(cmd + ": the bitrate must not be higher than the state size."); - System.exit(6); - } - if (c > s) - { System.err.println(cmd + ": the capacity must not be higher than the state size."); - System.exit(6); - } - if (r + c != s) - { System.err.println(cmd + ": the sum of the bitrate and the capacity must equal the state size."); - System.exit(6); - } - - - if (fptr == 0) - files[fptr++] = null; - if (i < 1) - { - System.err.println(cmd + ": sorry, I will only do at least one hash iteration!"); - System.exit(3); - } - if (j < 1) - { - System.err.println(cmd + ": sorry, I will only do at least one squeeze iteration!"); - System.exit(3); - } - - byte[] stdin = null; - boolean fail = false; - String filename; - - for (int f = 0; f < fptr; f++) - { String rc = ""; - String fn = (filename = files[f]) == null ? "/dev/stdin" : filename; - InputStream file = null; - try - { - byte[] bs; - if ((filename != null) || (stdin == null)) - { - file = new FileInputStream(fn); - SHA3.initialise(r, c, o); - int blksize = 4096; /** XXX os.stat(os.path.realpath(fn)).st_size; **/ - byte[] chunk = new byte[blksize]; - for (;;) - { - int read = file.read(chunk, 0, blksize); - if (read <= 0) - break; - if (hex == false) - SHA3.update(chunk, read); - else - { - int n = read >> 1; - for (int k = 0; k < n; k++) - { byte a = chunk[k << 1], b = chunk[(k << 1) | 1]; - chunk[k] = (byte)((((a & 15) + (a <= '9' ? 0 : 9)) << 4) | ((b & 15) + (b <= '9' ? 0 : 9))); - } - SHA3.update(chunk, n); - } - } - bs = SHA3.digest(j == 1); - if (j > 2) - SHA3.fastSqueeze(j - 2); - if (j > 1) - bs = SHA3.squeeze(); - if (filename == null) - stdin = bs; - } - else - bs = stdin; - if (multi == 0) - { - for (int k = 1; k < i; k++) - { - SHA3.initialise(r, c, o); - bs = SHA3.digest(bs, j == 1); - if (j > 2) - SHA3.fastSqueeze(j - 2); - if (j > 1) - bs = SHA3.squeeze(); - } - if (binary) - System.out.write(bs); - else - { for (int b = 0, bn = bs.length; b < bn; b++) - { rc += "0123456789ABCDEF".charAt((bs[b] >> 4) & 15); - rc += "0123456789ABCDEF".charAt(bs[b] & 15); - } - rc += " " + (filename == null ? "-" : filename) + "\n"; - System.out.print(rc); - } - } - else if (multi == 1) - { - byte[] out = null; - if (binary) - System.out.write(bs); - else - { - out = new byte[(bs.length << 1) + 1]; - for (int b = 0, bn = bs.length; b < bn; b++) - { out[ b << 1 ] = (byte)("0123456789ABCDEF".charAt((bs[b] >> 4) & 15)); - out[(b << 1) | 1] = (byte)("0123456789ABCDEF".charAt(bs[b] & 15)); - } - out[out.length - 1] = '\n'; - System.out.write(out); - } - for (int k = 1; k < i; k++) - { - SHA3.initialise(r, c, o); - bs = SHA3.digest(bs, j == 1); - if (j > 2) - SHA3.fastSqueeze(j - 2); - if (j > 1) - bs = SHA3.squeeze(); - if (binary) - System.out.write(bs); - else - { - for (int b = 0, bn = bs.length; b < bn; b++) - { out[ b << 1 ] = (byte)("0123456789ABCDEF".charAt((bs[b] >> 4) & 15)); - out[(b << 1) | 1] = (byte)("0123456789ABCDEF".charAt(bs[b] & 15)); - } - System.out.write(out); - } - } - } - else - { - HashSet<String> got = new HashSet<String>(); - String loop = null; - byte[] out = new byte[(bs.length << 1)]; - for (int k = 0; k < i; k++) - { - if (k > 0) - { SHA3.initialise(r, c, o); - bs = SHA3.digest(bs, j == 1); - if (j > 2) - SHA3.fastSqueeze(j - 2); - if (j > 1) - bs = SHA3.squeeze(); - } - for (int b = 0, bn = bs.length; b < bn; b++) - { out[ b << 1 ] = (byte)("0123456789ABCDEF".charAt((bs[b] >> 4) & 15)); - out[(b << 1) | 1] = (byte)("0123456789ABCDEF".charAt(bs[b] & 15)); - } - String now = new String(out, "UTF-8"); - if (loop == null) - if (got.contains(now)) - loop = now; - else - got.add(now); - if ((loop != null) && (loop.equals(now))) - now = "\033[31m" + now + "\033[00m"; - System.out.println(now); - } - if (loop != null) - System.err.println("\033[01;31mLoop found\033[00m"); - } - System.out.flush(); - } - catch (final IOException err) - { System.err.println(cmd + ": cannot read file: " + filename + ": " + err); - fail = true; - } - finally - { if (file != null) - try - { file.close(); - } - catch (final Throwable ignore) - { //ignore - } } } - - System.out.flush(); - if (fail) - System.exit(5); - } - -} - |