public static byte[] intToLittleEndian(int n) { byte[] bs = new byte[4]; intToLittleEndian(n, bs, 0); return bs; }
public static byte[] intToLittleEndian(int[] ns) { byte[] bs = new byte[4 * ns.length]; intToLittleEndian(ns, bs, 0); return bs; }
public static void intToLittleEndian(int[] ns, byte[] bs, int off) { for (int i = 0; i < ns.length; ++i) { intToLittleEndian(ns[i], bs, off); off += 4; } }
private void packBlock(byte[] bytes, int off) { Pack.intToLittleEndian(this.C0, bytes, off); Pack.intToLittleEndian(this.C1, bytes, off + 4); Pack.intToLittleEndian(this.C2, bytes, off + 8); Pack.intToLittleEndian(this.C3, bytes, off + 12); }
public static void longToLittleEndian(long n, byte[] bs, int off) { intToLittleEndian((int)(n & 0xffffffffL), bs, off); intToLittleEndian((int)(n >>> 32), bs, off + 4); }
protected void generateKeyStream(byte[] output) { salsaCore(rounds, engineState, x); Pack.intToLittleEndian(x, output, 0); }
protected void generateKeyStream(byte[] output) { chachaCore(rounds, engineState, x); Pack.intToLittleEndian(x, output, 0); }
protected void generateKeyStream(byte[] output) { ChaChaEngine.chachaCore(rounds, engineState, x); Pack.intToLittleEndian(x, output, 0); } }
f3 = (((h3 >>> 18) | (h4 << 8 )) & 0xffffffffl) + (0xffffffffL & k3); Pack.intToLittleEndian((int)f0, out, outOff); f1 += (f0 >>> 32); Pack.intToLittleEndian((int)f1, out, outOff + 4); f2 += (f1 >>> 32); Pack.intToLittleEndian((int)f2, out, outOff + 8); f3 += (f2 >>> 32); Pack.intToLittleEndian((int)f3, out, outOff + 12);
void chacha_permute(byte[] out, byte[] in) { int i; int[] x = new int[16]; for (i = 0; i < 16; i++) { x[i] = Pack.littleEndianToInt(in, 4 * i); } permute(CHACHA_ROUNDS, x); // for (i = 0;i < 16;++i) x[i] = PLUS(x[i],input[i]); // XXX: Bad idea if we later xor the input to the state? for (i = 0; i < 16; ++i) { Pack.intToLittleEndian(x[i], out, 4 * i); } } }
private static byte[] mfcrypt(byte[] P, byte[] S, final int N, final int r, int p, int dkLen) throws InterruptedException, ExecutionException { int MFLenBytes = r * 128; byte[] bytes = singleIterationPBKDF2(P, S, p * MFLenBytes); int[] B = null; try { int BLen = bytes.length >>> 2; B = new int[BLen]; Pack.littleEndianToInt(bytes, 0, B); int MFLenWords = MFLenBytes >>> 2; ArrayList<Future<int[]>> futures = new ArrayList<>(); final int BCount = r * 32; for (int BOff = 0; BOff < BLen; BOff += MFLenWords) { final int[] X = new int[BCount]; System.arraycopy(B, BOff, X, 0, BCount); futures.add(THREAD_POOL_EXECUTOR.submit(() -> { sMix(X, N, r); return X; })); } for (int BOff = 0, i = 0; BOff < BLen; BOff += MFLenWords, i++) { System.arraycopy(futures.get(i).get(), 0, B, BOff, BCount); } Pack.intToLittleEndian(B, bytes, 0); return singleIterationPBKDF2(P, bytes, dkLen); } finally { clear(bytes); clear(B); } }
private byte[] padKey(byte[] in) { int paddedLen = ((in.length + engine.getByteLength() - 1) / engine.getByteLength()) * engine.getByteLength(); int extra = engine.getByteLength() - (int)(in.length % engine.getByteLength()); if (extra < 13) // terminator byte + 96 bits of length { paddedLen += engine.getByteLength(); } byte[] padded = new byte[paddedLen]; System.arraycopy(in, 0, padded, 0, in.length); padded[in.length] = (byte)0x80; // Defined in standard; Pack.intToLittleEndian(in.length * BITS_IN_BYTE, padded, padded.length - 12); // Defined in standard; return padded; } }
private static byte[] MFcrypt(byte[] P, byte[] S, int N, int r, int p, int dkLen) { int MFLenBytes = r * 128; byte[] bytes = SingleIterationPBKDF2(P, S, p * MFLenBytes); int[] B = null; try { int BLen = bytes.length >>> 2; B = new int[BLen]; Pack.littleEndianToInt(bytes, 0, B); int MFLenWords = MFLenBytes >>> 2; for (int BOff = 0; BOff < BLen; BOff += MFLenWords) { // TODO These can be done in parallel threads SMix(B, BOff, N, r); } Pack.intToLittleEndian(B, bytes, 0); return SingleIterationPBKDF2(P, bytes, dkLen); } finally { Clear(bytes); Clear(B); } }
private static byte[] cryptoProDiversify(byte[] K, byte[] ukm, byte[] sBox) { for (int i = 0; i != 8; i++) { int sOn = 0; int sOff = 0; for (int j = 0; j != 8; j++) { int kj = Pack.littleEndianToInt(K, j * 4); if (bitSet(ukm[i], j)) { sOn += kj; } else { sOff += kj; } } byte[] s = new byte[8]; Pack.intToLittleEndian(sOn, s, 0); Pack.intToLittleEndian(sOff, s, 4); GCFBBlockCipher c = new GCFBBlockCipher(new GOST28147Engine()); c.init(true, new ParametersWithIV(new ParametersWithSBox(new KeyParameter(K), sBox), s)); c.processBlock(K, 0, K, 0); c.processBlock(K, 8, K, 8); c.processBlock(K, 16, K, 16); c.processBlock(K, 24, K, 24); } return K; }
sb7(wKey[124] ^ X0, wKey[125] ^ X1, wKey[126] ^ X2, wKey[127] ^ X3); Pack.intToLittleEndian(wKey[128] ^ X0, output, outOff); Pack.intToLittleEndian(wKey[129] ^ X1, output, outOff + 4); Pack.intToLittleEndian(wKey[130] ^ X2, output, outOff + 8); Pack.intToLittleEndian(wKey[131] ^ X3, output, outOff + 12);
inverseLT(); ib0(X0, X1, X2, X3); Pack.intToLittleEndian(X0 ^ wKey[0], output, outOff); Pack.intToLittleEndian(X1 ^ wKey[1], output, outOff + 4); Pack.intToLittleEndian(X2 ^ wKey[2], output, outOff + 8); Pack.intToLittleEndian(X3 ^ wKey[3], output, outOff + 12);