aboutsummaryrefslogtreecommitdiffstats
path: root/java-c-jni
diff options
context:
space:
mode:
Diffstat (limited to 'java-c-jni')
-rw-r--r--java-c-jni/SHA3.c871
-rw-r--r--java-c-jni/SHA3.java384
-rw-r--r--java-c-jni/sha3sum.java485
3 files changed, 0 insertions, 1740 deletions
diff --git a/java-c-jni/SHA3.c b/java-c-jni/SHA3.c
deleted file mode 100644
index 76b1d61..0000000
--- a/java-c-jni/SHA3.c
+++ /dev/null
@@ -1,871 +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/>.
- */
-#include <stdlib.h>
-#include "SHA3.h"
-
-
-#if __x86_64__ || __ppc64__
- #define llong long int
-#else
- #define llong long long int
-#endif
-
-
-#define null 0
-#define byte char
-#define boolean long
-#define true 1
-#define false 0
-
-
-
-/**
- * Round contants
- */
-static const llong 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
- */
-static llong B[25];
-
-/**
- * Keccak-f round temporary
- */
-static llong C[5];
-
-
-/**
- * The bitrate
- */
-static long r = 0;
-
-/**
- * The capacity
- */
-static long c = 0;
-
-/**
- * The output size
- */
-static long n = 0;
-
-/**
- * The state size
- */
-static long b = 0;
-
-/**
- * The word size
- */
-static long w = 0;
-
-/**
- * The word mask
- */
-static llong wmod = 0;
-
-/**
- * ℓ, the binary logarithm of the word size
- */
-static long l = 0;
-
-/**
- * 12 + 2ℓ, the number of rounds
- */
-static long nr = 0;
-
-
-/**
- * The current state
- */
-static llong* S = null;
-
-/**
- * Left over water to fill the sponge with at next update
- */
-static byte* M = null;
-
-/**
- * Pointer for {@link #M}
- */
-static long mptr = 0;
-
-/**
- * Size of {@link #M}
- */
-static long mlen = 0;
-
-
-
-/**
- * Gets the smallest, in value, of the arguments
- *
- * @param X The first candidate
- * @param Y The second candidate
- * @return The lowest candidate
- */
-#define min(X, Y) ((X) < (Y) ? (X) : (Y))
-
-
-
-/**
- * Copy an array segment into an array in start to end order
- *
- * @param src The source array
- * @param soff The source array offset
- * @param dest The destination array
- * @param doff The destination array offset
- * @param length The number of elements to copy
- */
-inline void arraycopy(byte* src, long soff, byte* dest, long doff, long length)
-{
- long i;
- src += soff;
- dest += doff;
-
- #define __(X) dest[X] = src[X]
- #define __0 *dest = *src
- #define __1 __(0x01)
- #define __2 __(0x02); __(0x03)
- #define __3 __(0x04); __(0x05); __(0x06); __(0x07)
- #define __4 __(0x08); __(0x09); __(0x0A); __(0x0B); __(0x0C); __(0x0D); __(0x0E); __(0x0F)
- #define __5 __(0x10); __(0x11); __(0x12); __(0x13); __(0x14); __(0x15); __(0x16); __(0x17); __(0x18); __(0x19); __(0x1A); __(0x1B); __(0x1C); __(0x1D); __(0x1E); __(0x1F)
- #define __6 __(0x20); __(0x21); __(0x22); __(0x23); __(0x24); __(0x25); __(0x26); __(0x27); __(0x28); __(0x29); __(0x2A); __(0x2B); __(0x2C); __(0x2D); __(0x2E); __(0x2F); \
- __(0x30); __(0x31); __(0x32); __(0x33); __(0x34); __(0x35); __(0x36); __(0x37); __(0x38); __(0x39); __(0x3A); __(0x3B); __(0x3C); __(0x3D); __(0x3E); __(0x3F)
- #define __7 __(0x40); __(0x41); __(0x42); __(0x43); __(0x44); __(0x45); __(0x46); __(0x47); __(0x48); __(0x49); __(0x4A); __(0x4B); __(0x4C); __(0x4D); __(0x4E); __(0x4F); \
- __(0x50); __(0x51); __(0x52); __(0x53); __(0x54); __(0x55); __(0x56); __(0x57); __(0x58); __(0x59); __(0x5A); __(0x5B); __(0x5C); __(0x5D); __(0x5E); __(0x5F); \
- __(0x60); __(0x61); __(0x62); __(0x63); __(0x64); __(0x65); __(0x66); __(0x67); __(0x68); __(0x69); __(0x6A); __(0x6B); __(0x6C); __(0x6D); __(0x6E); __(0x6F); \
- __(0x70); __(0x71); __(0x72); __(0x73); __(0x74); __(0x75); __(0x76); __(0x77); __(0x78); __(0x79); __(0x7A); __(0x7B); __(0x7C); __(0x7D); __(0x7E); __(0x7F)
- #define __8 __(0x80); __(0x81); __(0x82); __(0x83); __(0x84); __(0x85); __(0x86); __(0x87); __(0x88); __(0x89); __(0x8A); __(0x8B); __(0x8C); __(0x8D); __(0x8E); __(0x8F); \
- __(0x90); __(0x91); __(0x92); __(0x93); __(0x94); __(0x95); __(0x96); __(0x97); __(0x98); __(0x99); __(0x9A); __(0x9B); __(0x9C); __(0x9D); __(0x9E); __(0x9F); \
- __(0xA0); __(0xA1); __(0xA2); __(0xA3); __(0xA4); __(0xA5); __(0xA6); __(0xA7); __(0xA8); __(0xA9); __(0xAA); __(0xAB); __(0xAC); __(0xAD); __(0xAE); __(0xAF); \
- __(0xB0); __(0xB1); __(0xB2); __(0xB3); __(0xB4); __(0xB5); __(0xB6); __(0xB7); __(0xB8); __(0xB9); __(0xBA); __(0xBB); __(0xBC); __(0xBD); __(0xBE); __(0xBF); \
- __(0xC0); __(0xC1); __(0xC2); __(0xC3); __(0xC4); __(0xC5); __(0xC6); __(0xC7); __(0xC8); __(0xC9); __(0xCA); __(0xCB); __(0xCC); __(0xCD); __(0xCE); __(0xCF); \
- __(0xD0); __(0xD1); __(0xD2); __(0xD3); __(0xD4); __(0xD5); __(0xD6); __(0xD7); __(0xD8); __(0xD9); __(0xDA); __(0xDB); __(0xDC); __(0xDD); __(0xDE); __(0xDF); \
- __(0xE0); __(0xE1); __(0xE2); __(0xE3); __(0xE4); __(0xE5); __(0xE6); __(0xE7); __(0xE8); __(0xE9); __(0xEA); __(0xEB); __(0xEC); __(0xED); __(0xEE); __(0xEF); \
- __(0xF0); __(0xF1); __(0xF2); __(0xF3); __(0xF4); __(0xF5); __(0xF6); __(0xF7); __(0xF8); __(0xF9); __(0xFA); __(0xFB); __(0xFC); __(0xFD); __(0xFE); __(0xFF)
-
- if ((length & 15))
- {
- if ((length & 1)) { __0; src += 1; dest += 1; }
- if ((length & 2)) { __0; __1; src += 2; dest += 2; }
- if ((length & 4)) { __0; __1; __2; src += 4; dest += 4; }
- if ((length & 8)) { __0; __1; __2; __3; src += 8; dest += 8; }
- }
- if ((length & 240))
- {
- if ((length & 16)) { __0; __1; __2; __3; __4; src += 16; dest += 16; }
- if ((length & 32)) { __0; __1; __2; __3; __4; __5; src += 32; dest += 32; }
- if ((length & 64)) { __0; __1; __2; __3; __4; __5; __6; src += 64; dest += 64; }
- if ((length & 128)) { __0; __1; __2; __3; __4; __5; __6; __7; src += 128; dest += 128; }
- }
- length &= ~255;
- for (i = 0; i < length; i += 256)
- {
- __0; __1; __2; __3; __4; __5; __6; __7; __8; src += 256; dest += 256;
- }
-
- #undef __8
- #undef __7
- #undef __6
- #undef __5
- #undef __4
- #undef __3
- #undef __2
- #undef __1
- #undef __0
- #undef __
-}
-
-
-/**
- * Copy an array segment into an array in end to start order
- *
- * @param src The source array
- * @param soff The source array offset
- * @param dest The destination array
- * @param doff The destination array offset
- * @param length The number of elements to copy
- */
-inline void revarraycopy(byte* src, long soff, byte* dest, long doff, long length)
-{
- long copyi;
- for (copyi = length - 1; copyi >= 0; copyi--)
- dest[copyi + doff] = src[copyi + soff];
-}
-
-
-/**
- * Rotate a word
- *
- * @param X:llong The value to rotate
- * @param N:long Rotation steps, may not be 0
- * @return :llong The value rotated
- */
-#define rotate(X, N) ((((X) >> (w - ((N) % w))) + ((X) << ((N) % w))) & wmod)
-
-
-/**
- * Rotate a 64-bit word
- *
- * @param X:llong The value to rotate
- * @param N:long Rotation steps, may not be 0
- * @return :llong The value rotated
- */
-#define rotate64(X, N) ((llong)((unsigned llong)(X) >> (64 - (N))) + ((X) << (N)))
-
-
-/**
- * Binary logarithm
- *
- * @param x The value of which to calculate the binary logarithm
- * @return The binary logarithm
- */
-static long lb(long x)
-{
- long 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
- */
-static void keccakFRound(llong* A, llong rc)
-{
- llong da, db, dc, dd, de;
-
- /* θ step (step 1 and 2 of 3) */
- #define __C(I, J0, J1, J2, J3, J4) C[I] = (A[J0] ^ A[J1]) ^ (A[J2] ^ A[J3]) ^ A[J4]
- __C(0, 0, 1, 2, 3, 4);
- __C(1, 5, 6, 7, 8, 9);
- __C(2, 10, 11, 12, 13, 14);
- __C(3, 15, 16, 17, 18, 19);
- __C(4, 20, 21, 22, 23, 24);
- #undef __C
-
- if (w == 64)
- {
- da = C[4] ^ rotate64(C[1], 1);
- dd = C[2] ^ rotate64(C[4], 1);
- db = C[0] ^ rotate64(C[2], 1);
- de = C[3] ^ rotate64(C[0], 1);
- dc = C[1] ^ rotate64(C[3], 1);
-
- /* ρ and π steps, with last two part of θ */
- #define __B(Bi, Ai, Dv, R) B[Bi] = rotate64(A[Ai] ^ Dv, R)
- B[0] = A[0] ^ da; __B( 1, 15, dd, 28); __B( 2, 5, db, 1); __B( 3, 20, de, 27); __B( 4, 10, dc, 62);
- __B( 5, 6, db, 44); __B( 6, 21, de, 20); __B( 7, 11, dc, 6); __B( 8, 1, da, 36); __B( 9, 16, dd, 55);
- __B(10, 12, dc, 43); __B(11, 2, da, 3); __B(12, 17, dd, 25); __B(13, 7, db, 10); __B(14, 22, de, 39);
- __B(15, 18, dd, 21); __B(16, 8, db, 45); __B(17, 23, de, 8); __B(18, 13, dc, 15); __B(19, 3, da, 41);
- __B(20, 24, de, 14); __B(21, 14, dc, 61); __B(22, 4, da, 18); __B(23, 19, dd, 56); __B(24, 9, db, 2);
- #undef __B
- }
- else
- {
- da = C[4] ^ rotate(C[1], 1);
- dd = C[2] ^ rotate(C[4], 1);
- db = C[0] ^ rotate(C[2], 1);
- de = C[3] ^ rotate(C[0], 1);
- dc = C[1] ^ rotate(C[3], 1);
-
- /* ρ and π steps, with last two part of θ */
- #define __B(Bi, Ai, Dv, R) B[Bi] = rotate(A[Ai] ^ Dv, R)
- B[0] = A[0] ^ da; __B( 1, 15, dd, 28); __B( 2, 5, db, 1); __B( 3, 20, de, 27); __B( 4, 10, dc, 62);
- __B( 5, 6, db, 44); __B( 6, 21, de, 20); __B( 7, 11, dc, 6); __B( 8, 1, da, 36); __B( 9, 16, dd, 55);
- __B(10, 12, dc, 43); __B(11, 2, da, 3); __B(12, 17, dd, 25); __B(13, 7, db, 10); __B(14, 22, de, 39);
- __B(15, 18, dd, 21); __B(16, 8, db, 45); __B(17, 23, de, 8); __B(18, 13, dc, 15); __B(19, 3, da, 41);
- __B(20, 24, de, 14); __B(21, 14, dc, 61); __B(22, 4, da, 18); __B(23, 19, dd, 56); __B(24, 9, db, 2);
- #undef __B
- }
-
- /* ξ step */
- #define __A(X, X5, X10) A[X] = B[X] ^ ((~(B[X5])) & B[X10])
- __A( 0, 5, 10); __A( 1, 6, 11); __A( 2, 7, 12); __A( 3, 8, 13); __A( 4, 9, 14);
- __A( 5, 10, 15); __A( 6, 11, 16); __A( 7, 12, 17); __A( 8, 13, 18); __A( 9, 14, 19);
- __A(10, 15, 20); __A(11, 16, 21); __A(12, 17, 22); __A(13, 18, 23); __A(14, 19, 24);
- __A(15, 20, 0); __A(16, 21, 1); __A(17, 22, 2); __A(18, 23, 3); __A(19, 24, 4);
- __A(20, 0, 5); __A(21, 1, 6); __A(22, 2, 7); __A(23, 3, 8); __A(24, 4, 9);
- #undef __A
-
- /* ι step */
- A[0] ^= rc;
-}
-
-
-/**
- * Perform Keccak-f function
- *
- * @param A The current state
- */
-static void keccakF(llong* A)
-{
- long i;
- if (nr == 24)
- {
- keccakFRound(A, 0x0000000000000001);
- keccakFRound(A, 0x0000000000008082);
- keccakFRound(A, 0x800000000000808A);
- keccakFRound(A, 0x8000000080008000);
- keccakFRound(A, 0x000000000000808B);
- keccakFRound(A, 0x0000000080000001);
- keccakFRound(A, 0x8000000080008081);
- keccakFRound(A, 0x8000000000008009);
- keccakFRound(A, 0x000000000000008A);
- keccakFRound(A, 0x0000000000000088);
- keccakFRound(A, 0x0000000080008009);
- keccakFRound(A, 0x000000008000000A);
- keccakFRound(A, 0x000000008000808B);
- keccakFRound(A, 0x800000000000008B);
- keccakFRound(A, 0x8000000000008089);
- keccakFRound(A, 0x8000000000008003);
- keccakFRound(A, 0x8000000000008002);
- keccakFRound(A, 0x8000000000000080);
- keccakFRound(A, 0x000000000000800A);
- keccakFRound(A, 0x800000008000000A);
- keccakFRound(A, 0x8000000080008081);
- keccakFRound(A, 0x8000000000008080);
- keccakFRound(A, 0x0000000080000001);
- keccakFRound(A, 0x8000000080008008);
- }
- else
- for (i = 0; i < nr; i++)
- keccakFRound(A, RC[i] & wmod);
-}
-
-
-/**
- * Convert a chunk of byte:s to a word
- *
- * @param message The message
- * @param msglen The length of the message
- * @param rr Bitrate in bytes
- * @param ww Word size in bytes
- * @param off The offset in the message
- * @return Lane
- */
-inline llong toLane(byte* message, long msglen, long rr, long ww, long off)
-{
- llong rc = 0;
- long n = min(msglen, rr), i;
- for (i = off + ww - 1; i >= off; i--)
- rc = (rc << 8) | ((i < n) ? (llong)(message[i] & 255) : 0L);
- return rc;
-}
-
-
-/**
- * Convert a chunk of byte:s to a 64-bit word
- *
- * @param message The message
- * @param msglen The length of the message
- * @param rr Bitrate in bytes
- * @param off The offset in the message
- * @return Lane
- */
-inline llong toLane64(byte* message, long msglen, long rr, long off)
-{
- long n = min(msglen, rr);
- return ((off + 7 < n) ? ((llong)(message[off + 7] & 255) << 56) : 0L) |
- ((off + 6 < n) ? ((llong)(message[off + 6] & 255) << 48) : 0L) |
- ((off + 5 < n) ? ((llong)(message[off + 5] & 255) << 40) : 0L) |
- ((off + 4 < n) ? ((llong)(message[off + 4] & 255) << 32) : 0L) |
- ((off + 3 < n) ? ((llong)(message[off + 3] & 255) << 24) : 0L) |
- ((off + 2 < n) ? ((llong)(message[off + 2] & 255) << 16) : 0L) |
- ((off + 1 < n) ? ((llong)(message[off + 1] & 255) << 8) : 0L) |
- ((off < n) ? ((llong)(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
- * @param outlen The length of the padded message (out parameter)
- * @return The message padded
- */
-inline byte* pad10star1(byte* msg, long len, long r, long bits, long* outlen)
-{
- byte* message;
- byte b;
- long i, ll, nbrf, nrf;
-
- len = ((len - (bits + 7) / 8) << 3) + bits;
- nrf = len >> 3;
- nbrf = len & 7;
- ll = len % r;
-
- b = (byte)(nbrf == 0 ? 1 : (msg[nrf] | (1 << nbrf)));
-
- if ((r - 8 <= ll) && (ll <= r - 2))
- {
- message = (byte*)malloc(len = nrf + 1);
- message[nrf] = (byte)(b ^ 128);
- }
- else
- {
- byte* M;
- long N;
- len = (nrf + 1) << 3;
- len = ((len - (len % r) + (r - 8)) >> 3) + 1;
- message = (byte*)malloc(len);
- message[nrf] = b;
- N = len - nrf - 1;
- M = message + nrf + 1;
-
- #define __(X) M[X] = 0
- #define __0 *M = 0
- #define __1 __(0x01)
- #define __2 __(0x02); __(0x03)
- #define __3 __(0x04); __(0x05); __(0x06); __(0x07)
- #define __4 __(0x08); __(0x09); __(0x0A); __(0x0B); __(0x0C); __(0x0D); __(0x0E); __(0x0F)
- #define __5 __(0x10); __(0x11); __(0x12); __(0x13); __(0x14); __(0x15); __(0x16); __(0x17); __(0x18); __(0x19); __(0x1A); __(0x1B); __(0x1C); __(0x1D); __(0x1E); __(0x1F)
- #define __6 __(0x20); __(0x21); __(0x22); __(0x23); __(0x24); __(0x25); __(0x26); __(0x27); __(0x28); __(0x29); __(0x2A); __(0x2B); __(0x2C); __(0x2D); __(0x2E); __(0x2F); \
- __(0x30); __(0x31); __(0x32); __(0x33); __(0x34); __(0x35); __(0x36); __(0x37); __(0x38); __(0x39); __(0x3A); __(0x3B); __(0x3C); __(0x3D); __(0x3E); __(0x3F)
- #define __7 __(0x40); __(0x41); __(0x42); __(0x43); __(0x44); __(0x45); __(0x46); __(0x47); __(0x48); __(0x49); __(0x4A); __(0x4B); __(0x4C); __(0x4D); __(0x4E); __(0x4F); \
- __(0x50); __(0x51); __(0x52); __(0x53); __(0x54); __(0x55); __(0x56); __(0x57); __(0x58); __(0x59); __(0x5A); __(0x5B); __(0x5C); __(0x5D); __(0x5E); __(0x5F); \
- __(0x60); __(0x61); __(0x62); __(0x63); __(0x64); __(0x65); __(0x66); __(0x67); __(0x68); __(0x69); __(0x6A); __(0x6B); __(0x6C); __(0x6D); __(0x6E); __(0x6F); \
- __(0x70); __(0x71); __(0x72); __(0x73); __(0x74); __(0x75); __(0x76); __(0x77); __(0x78); __(0x79); __(0x7A); __(0x7B); __(0x7C); __(0x7D); __(0x7E); __(0x7F)
- #define __8 __(0x80); __(0x81); __(0x82); __(0x83); __(0x84); __(0x85); __(0x86); __(0x87); __(0x88); __(0x89); __(0x8A); __(0x8B); __(0x8C); __(0x8D); __(0x8E); __(0x8F); \
- __(0x90); __(0x91); __(0x92); __(0x93); __(0x94); __(0x95); __(0x96); __(0x97); __(0x98); __(0x99); __(0x9A); __(0x9B); __(0x9C); __(0x9D); __(0x9E); __(0x9F); \
- __(0xA0); __(0xA1); __(0xA2); __(0xA3); __(0xA4); __(0xA5); __(0xA6); __(0xA7); __(0xA8); __(0xA9); __(0xAA); __(0xAB); __(0xAC); __(0xAD); __(0xAE); __(0xAF); \
- __(0xB0); __(0xB1); __(0xB2); __(0xB3); __(0xB4); __(0xB5); __(0xB6); __(0xB7); __(0xB8); __(0xB9); __(0xBA); __(0xBB); __(0xBC); __(0xBD); __(0xBE); __(0xBF); \
- __(0xC0); __(0xC1); __(0xC2); __(0xC3); __(0xC4); __(0xC5); __(0xC6); __(0xC7); __(0xC8); __(0xC9); __(0xCA); __(0xCB); __(0xCC); __(0xCD); __(0xCE); __(0xCF); \
- __(0xD0); __(0xD1); __(0xD2); __(0xD3); __(0xD4); __(0xD5); __(0xD6); __(0xD7); __(0xD8); __(0xD9); __(0xDA); __(0xDB); __(0xDC); __(0xDD); __(0xDE); __(0xDF); \
- __(0xE0); __(0xE1); __(0xE2); __(0xE3); __(0xE4); __(0xE5); __(0xE6); __(0xE7); __(0xE8); __(0xE9); __(0xEA); __(0xEB); __(0xEC); __(0xED); __(0xEE); __(0xEF); \
- __(0xF0); __(0xF1); __(0xF2); __(0xF3); __(0xF4); __(0xF5); __(0xF6); __(0xF7); __(0xF8); __(0xF9); __(0xFA); __(0xFB); __(0xFC); __(0xFD); __(0xFE); __(0xFF)
-
- if ((N & 15))
- {
- if ((N & 1)) { __0; M += 1; }
- if ((N & 2)) { __0; __1; M += 2; }
- if ((N & 4)) { __0; __1; __2; M += 4; }
- if ((N & 8)) { __0; __1; __2; __3; M += 8; }
- }
- if ((N & 240))
- {
- if ((N & 16)) { __0; __1; __2; __3; __4; M += 16; }
- if ((N & 32)) { __0; __1; __2; __3; __4; __5; M += 32; }
- if ((N & 64)) { __0; __1; __2; __3; __4; __5; __6; M += 64; }
- if ((N & 128)) { __0; __1; __2; __3; __4; __5; __6; __7; M += 128; }
- }
- N &= ~255;
- for (i = 0; i < N; i += 256)
- {
- __0; __1; __2; __3; __4; __5; __6; __7; __8; M += 256;
- }
-
- #undef __8
- #undef __7
- #undef __6
- #undef __5
- #undef __4
- #undef __3
- #undef __2
- #undef __1
- #undef __0
- #undef __
-
- message[len - 1] = -128;
- }
- arraycopy(msg, 0, message, 0, nrf);
-
- *outlen = len;
- return message;
-}
-
-
-/**
- * Initialise Keccak sponge
- *
- * @param bitrate The bitrate
- * @param capacity The capacity
- * @param output The output size
- */
-void initialise(long bitrate, long capacity, long output)
-{
- long i;
-
- r = bitrate;
- n = output;
- c = capacity;
- b = r + c;
- w = b / 25;
- l = lb(w);
- nr = 12 + (l << 1);
- if (w == 64)
- wmod = -1;
- else
- {
- wmod = 1;
- wmod <<= w;
- wmod--;
- }
- S = (llong*)malloc(25 * sizeof(llong));
- M = (byte*)malloc(mlen = (r * b) >> 2);
- mptr = 0;
-
- for (i = 0; i < 25; i++)
- *(S + i) = 0;
-}
-
-/**
- * Dispose of the Keccak sponge
- */
-extern void dispose()
-{
- if (S != null)
- {
- free(S);
- S = null;
- }
- if (M != null)
- {
- free(M);
- M = null;
- }
-}
-
-/**
- * Absorb the more of the message message to the Keccak sponge
- *
- * @param msg The partial message
- * @param msglen The length of the partial message
- */
-void update(byte* msg, long msglen)
-{
- long rr = r >> 3;
- long ww = w >> 3;
- long i, len;
- byte* message;
- byte* _msg;
- long nnn;
-
- if (mptr + msglen > mlen)
- {
- byte* buf = (byte*)malloc(mlen = (mlen + msglen) << 1);
- arraycopy(M, 0, buf, 0, mptr);
- free(M);
- M = buf;
- }
- arraycopy(msg, 0, M, mptr, msglen);
- len = mptr += msglen;
- len -= len % ((r * b) >> 3);
- message = (byte*)malloc(len);
- arraycopy(M, 0, message, 0, len);
- mptr -= len;
- revarraycopy(M, nnn = len, M, 0, mptr);
- _msg = message;
-
- /* Absorbing phase */
- if (ww == 8)
- for (i = 0; i < nnn; i += rr)
- {
- #define __S(Si, OFF) S[Si] ^= toLane64(message, len, rr, OFF)
- __S( 0, 0); __S( 5, 8); __S(10, 16); __S(15, 24); __S(20, 32);
- __S( 1, 40); __S( 6, 48); __S(11, 56); __S(16, 64); __S(21, 72);
- __S( 2, 80); __S( 7, 88); __S(12, 96); __S(17, 104); __S(22, 112);
- __S( 3, 120); __S( 8, 128); __S(13, 136); __S(18, 144); __S(23, 152);
- __S( 4, 160); __S( 9, 168); __S(14, 176); __S(19, 184); __S(24, 192);
- #undef __S
- keccakF(S);
- message += rr;
- len -= rr;
- }
- else
- for (i = 0; i < nnn; i += rr)
- {
- #define __S(Si, OFF) S[Si] ^= toLane(message, len, rr, ww, OFF * ww)
- __S( 0, 0); __S( 5, 1); __S(10, 2); __S(15, 3); __S(20, 4);
- __S( 1, 5); __S( 6, 6); __S(11, 7); __S(16, 8); __S(21, 9);
- __S( 2, 10); __S( 7, 11); __S(12, 12); __S(17, 13); __S(22, 14);
- __S( 3, 15); __S( 8, 16); __S(13, 17); __S(18, 18); __S(23, 19);
- __S( 4, 20); __S( 9, 21); __S(14, 22); __S(19, 23); __S(24, 24);
- #undef __S
- keccakF(S);
- message += rr;
- len -= rr;
- }
-
- free(_msg);
-}
-
-
-/**
- * Absorb the last part of the message and squeeze the Keccak sponge
- *
- * @param msg The rest of the message, may be {@code null}
- * @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 suffix_len The length of the suffix
- * @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}
- */
-byte* digest(byte* msg, long msglen, long bits, int* suffix, long suffix_len, boolean withReturn)
-{
- byte* message;
- byte* _msg;
- byte* rc;
- long rr = r >> 3, len;
- long nn = (n + 7) >> 3, olen;
- long ww = w >> 3, ni;
- long i, j = 0, ptr = 0, _;
- long nnn;
-
- if (msg == null)
- msglen = bits = 0;
-
- msglen += bits >> 3;
- if ((bits &= 7))
- msg[msglen] &= (1 << bits) - 1;
- if (suffix_len)
- {
- msg = (byte*)realloc(msg, msglen + ((suffix_len + bits + 7) >> 3));
- if (!bits)
- msg[msglen] = 0;
- for (i = 0; i < suffix_len; i++)
- {
- msg[msglen] |= suffix[i] << bits++;
- if (bits == 8)
- {
- bits = 0;
- msglen++;
- msg[msglen] = 0;
- }
- }
- }
- if (bits)
- msglen++;
-
- if (mptr + msglen > mlen)
- {
- byte* buf = (byte*)malloc(mlen += msglen);
- arraycopy(M, 0, buf, 0, mptr);
- free(M);
- M = buf;
- }
- arraycopy(msg, 0, M, mptr, msglen);
- message = pad10star1(M, mptr + msglen, r, bits, &len);
-
- free(M);
- M = null;
- rc = (byte*)malloc((n + 7) >> 3);
- _msg = message;
- nnn = len;
-
- /* Absorbing phase */
- if (ww == 8)
- for (i = 0; i < nnn; i += rr)
- {
- #define __S(Si, OFF) S[Si] ^= toLane64(message, len, rr, OFF)
- __S( 0, 0); __S( 5, 8); __S(10, 16); __S(15, 24); __S(20, 32);
- __S( 1, 40); __S( 6, 48); __S(11, 56); __S(16, 64); __S(21, 72);
- __S( 2, 80); __S( 7, 88); __S(12, 96); __S(17, 104); __S(22, 112);
- __S( 3, 120); __S( 8, 128); __S(13, 136); __S(18, 144); __S(23, 152);
- __S( 4, 160); __S( 9, 168); __S(14, 176); __S(19, 184); __S(24, 192);
- #undef __S
- keccakF(S);
- message += rr;
- len -= rr;
- }
- else
- for (i = 0; i < nnn; i += rr)
- {
- #define __S(Si, OFF) S[Si] ^= toLane(message, len, rr, ww, OFF * ww)
- __S( 0, 0); __S( 5, 1); __S(10, 2); __S(15, 3); __S(20, 4);
- __S( 1, 5); __S( 6, 6); __S(11, 7); __S(16, 8); __S(21, 9);
- __S( 2, 10); __S( 7, 11); __S(12, 12); __S(17, 13); __S(22, 14);
- __S( 3, 15); __S( 8, 16); __S(13, 17); __S(18, 18); __S(23, 19);
- __S( 4, 20); __S( 9, 21); __S(14, 22); __S(19, 23); __S(24, 24);
- #undef __S
- keccakF(S);
- message += rr;
- len -= rr;
- }
-
- free(_msg);
-
- /* Squeezing phase */
- olen = n;
- if (withReturn)
- {
- ni = rr / ww;
- while (olen > 0)
- {
- i = 0;
- while ((i < ni) && (j < nn))
- {
- llong v = S[(i % 5) * 5 + i / 5];
- for (_ = 0; _ < ww; _++)
- {
- if (j < nn)
- rc[ptr++] = (byte)v;
- v >>= 8;
- j += 1;
- }
- i += 1;
- }
- olen -= r;
- if (olen > 0)
- keccakF(S);
- }
- if ((n & 7))
- rc[n >> 3] &= (1 << (n & 7)) - 1;
-
- return rc;
- }
- while ((olen -= r) > 0)
- keccakF(S);
- return null;
-}
-
-
-/**
- * Force some rounds of Keccak-f
- *
- * @param times The number of rounds
- */
-void simpleSqueeze(long times)
-{
- long i;
- for (i = 0; i < times; i++)
- keccakF(S);
-}
-
-
-/**
- * Squeeze as much as is needed to get a digest a number of times
- *
- * @param times The number of digests
- */
-void fastSqueeze(long times)
-{
- long i, olen;
- for (i = 0; i < times; i++)
- {
- keccakF(S); /* Last squeeze did not do a ending squeeze */
- olen = n;
- while ((olen -= r) > 0)
- keccakF(S);
- }
-}
-
-
-/**
- * Squeeze out another digest
- *
- * @return The hash sum
- */
-byte* squeeze()
-{
- long nn, ww, olen, i, j, ptr, ni;
- byte* rc;
-
- keccakF(S); /* Last squeeze did not do a ending squeeze */
-
- ww = w >> 3;
- rc = (byte*)malloc(nn = (n + 7) >> 3);
- olen = n;
- j = ptr = 0;
- ni = (r >> 3) / ww;
-
- while (olen > 0)
- {
- i = 0;
- while ((i < ni) && (j < nn))
- {
- long _, v = S[(i % 5) * 5 + i / 5];
- for (_ = 0; _ < ww; _++)
- {
- if (j < nn)
- *(rc + ptr++) = (byte)v;
- v >>= 8;
- j += 1;
- }
- i += 1;
- }
- olen -= r;
- if (olen > 0)
- keccakF(S);
- }
- if (n & 7)
- rc[nn - 1] &= (1 << (n & 7)) - 1;
-
- return rc;
-}
-
-
-
-JNIEXPORT void JNICALL Java_SHA3_initialise(JNIEnv* env, jclass class, jint bitrate, jint capacity, jint output)
-{
- (void) env;
- (void) class;
-
- initialise(bitrate, capacity, output);
-}
-
-
-JNIEXPORT void JNICALL Java_SHA3_update(JNIEnv* env, jclass class, jbyteArray msg, jint msglen)
-{
- (void) class;
- if ((msg != null) && (msglen != 0))
- update((byte*)((*env)->GetByteArrayElements(env, msg, 0)), msglen);
-}
-
-
-JNIEXPORT jbyteArray JNICALL Java_SHA3_digest(JNIEnv* env, jclass class, jbyteArray msg, jint msglen, jint bits, jintArray suffix, jboolean withReturn)
-{
- jbyte* rcn;
- jbyteArray rcj = null;
- int* suffix_elems = (int*)((*env)->GetIntArrayElements(env, suffix, 0));
- long suffix_len = (long)((*env)->GetArrayLength(env, suffix));
-
- (void) class;
-
- if ((msg != null) && (msglen != 0))
- rcn = (jbyte*)digest((byte*)((*env)->GetByteArrayElements(env, msg, 0)), msglen,
- bits, suffix_elems, suffix_len, withReturn);
- else
- rcn = (jbyte*)digest(null, 0, 0, suffix_elems, suffix_len, withReturn);
- if (withReturn)
- {
- rcj = (*env)->NewByteArray(env, (n + 7) >> 3);
- (*env)->SetByteArrayRegion(env, rcj, 0, (n + 7) >> 3, rcn);
- }
- return rcj;
-}
-
diff --git a/java-c-jni/SHA3.java b/java-c-jni/SHA3.java
deleted file mode 100644
index 4be013e..0000000
--- a/java-c-jni/SHA3.java
+++ /dev/null
@@ -1,384 +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 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";
-
-
-
- /**
- * Hidden constructor
- */
- private SHA3()
- {
- // Inhibit instansiation
- }
-
-
-
- /**
- * Class initialiser
- */
- static
- {
- try
- {
- System.load((new File("./SHA3.so")).getCanonicalPath());
- }
- catch (IOException err)
- {
- throw new Error("SHA3 library cannot be found");
- }
- }
-
-
-
- /**
- * Initialise Keccak sponge
- *
- * @param r The bitrate
- * @param c The capacity
- * @param n The output size
- */
- public static native void initialise(int r, int c, int n);
-
-
- /**
- * 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 native void update(byte[] msg, int msglen);
-
-
- /**
- * 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
- * @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[] suffix_ = new int[suffix.length()];
- for (int i = 0, n = suffix_.length; i < n; i++)
- suffix_[i] = suffix.charAt(i) == '1' ? 1 : 0;
-
- return digest(msg, msglen, bits, 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 static native byte[] digest(byte[] msg, int msglen, int bits, int[] suffix, boolean withReturn);
-
-
- /**
- * Force a round of Keccak-f
- */
- public static void simpleSqueeze()
- {
- simpleSqueeze(1);
- }
-
-
- /**
- * Force some rounds of Keccak-f
- *
- * @param times The number of rounds
- */
- public static native void simpleSqueeze(int times);
-
-
- /**
- * Squeeze as much as is needed to get a digest
- */
- public static void fastSqueeze()
- {
- fastSqueeze(1);
- }
-
-
- /**
- * Squeeze as much as is needed to get a digest a number of times
- *
- * @param times The number of digests
- */
- public static native void fastSqueeze(int times);
-
-
- /**
- * Squeeze out another digest
- *
- * @return The hash sum
- */
- public static native byte[] squeeze();
-
-}
-
diff --git a/java-c-jni/sha3sum.java b/java-c-jni/sha3sum.java
deleted file mode 100644
index 9347c93..0000000
--- a/java-c-jni/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 checksum. (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);
- }
-
-}
-