public static void littleEndianToInt(byte[] bs, int bOff, int[] ns, int nOff, int count) { for (int i = 0; i < count; ++i) { ns[nOff + i] = littleEndianToInt(bs, bOff); bOff += 4; } }
public static long littleEndianToLong(byte[] bs, int off) { int lo = littleEndianToInt(bs, off); int hi = littleEndianToInt(bs, off + 4); return ((long)(hi & 0xffffffffL) << 32) | (long)(lo & 0xffffffffL); }
public static void littleEndianToInt(byte[] bs, int off, int[] ns) { for (int i = 0; i < ns.length; ++i) { ns[i] = littleEndianToInt(bs, off); off += 4; } }
public static void littleEndianToInt(byte[] bs, int bOff, int[] ns, int nOff, int count) { for (int i = 0; i < count; ++i) { ns[nOff + i] = littleEndianToInt(bs, bOff); bOff += 4; } }
public static void littleEndianToInt(byte[] bs, int off, int[] ns) { for (int i = 0; i < ns.length; ++i) { ns[i] = littleEndianToInt(bs, off); off += 4; } }
public static int[] littleEndianToInt(byte[] bs, int off, int count) { int[] ns = new int[count]; for (int i = 0; i < ns.length; ++i) { ns[i] = littleEndianToInt(bs, off); off += 4; } return ns; }
public static long littleEndianToLong(byte[] bs, int off) { int lo = littleEndianToInt(bs, off); int hi = littleEndianToInt(bs, off + 4); return ((long)(hi & 0xffffffffL) << 32) | (long)(lo & 0xffffffffL); }
private void unpackBlock(byte[] bytes, int off) { this.C0 = Pack.littleEndianToInt(bytes, off); this.C1 = Pack.littleEndianToInt(bytes, off + 4); this.C2 = Pack.littleEndianToInt(bytes, off + 8); this.C3 = Pack.littleEndianToInt(bytes, off + 12); }
private void unpackBlock(byte[] bytes, int off) { this.C0 = Pack.littleEndianToInt(bytes, off); this.C1 = Pack.littleEndianToInt(bytes, off + 4); this.C2 = Pack.littleEndianToInt(bytes, off + 8); this.C3 = Pack.littleEndianToInt(bytes, off + 12); }
private void init() { if (chainValue == null) { chainValue = new int[8]; chainValue[0] = blake2s_IV[0] ^ (digestLength | (keyLength << 8) | 0x1010000); // 0x1010000 = ((fanout << 16) | (depth << 24)); // with fanout = 1; depth = 0; chainValue[1] = blake2s_IV[1];// ^ leafLength; with leafLength = 0; chainValue[2] = blake2s_IV[2];// ^ nodeOffset; with nodeOffset = 0; chainValue[3] = blake2s_IV[3];// ^ ( (nodeOffset << 32) | // (nodeDepth << 16) | (innerHashLength << 24) ); // with nodeDepth = 0; innerHashLength = 0; chainValue[4] = blake2s_IV[4]; chainValue[5] = blake2s_IV[5]; if (salt != null) { chainValue[4] ^= Pack.littleEndianToInt(salt, 0); chainValue[5] ^= Pack.littleEndianToInt(salt, 4); } chainValue[6] = blake2s_IV[6]; chainValue[7] = blake2s_IV[7]; if (personalization != null) { chainValue[6] ^= Pack.littleEndianToInt(personalization, 0); chainValue[7] ^= Pack.littleEndianToInt(personalization, 4); } } }
protected void setKey(byte[] keyBytes, byte[] ivBytes) { if (keyBytes != null) { if ((keyBytes.length != 16) && (keyBytes.length != 32)) { throw new IllegalArgumentException(getAlgorithmName() + " requires 128 bit or 256 bit key"); } int tsOff = (keyBytes.length - 16) / 4; engineState[0 ] = TAU_SIGMA[tsOff ]; engineState[5 ] = TAU_SIGMA[tsOff + 1]; engineState[10] = TAU_SIGMA[tsOff + 2]; engineState[15] = TAU_SIGMA[tsOff + 3]; // Key Pack.littleEndianToInt(keyBytes, 0, engineState, 1, 4); Pack.littleEndianToInt(keyBytes, keyBytes.length - 16, engineState, 11, 4); } // IV Pack.littleEndianToInt(ivBytes, 0, engineState, 6, 2); }
protected void setKey(byte[] keyBytes, byte[] ivBytes) { if (keyBytes != null) { if ((keyBytes.length != 16) && (keyBytes.length != 32)) { throw new IllegalArgumentException(getAlgorithmName() + " requires 128 bit or 256 bit key"); } packTauOrSigma(keyBytes.length, engineState, 0); // Key Pack.littleEndianToInt(keyBytes, 0, engineState, 4, 4); Pack.littleEndianToInt(keyBytes, keyBytes.length - 16, engineState, 8, 4); } // IV Pack.littleEndianToInt(ivBytes, 0, engineState, 14, 2); }
protected void setKey(byte[] keyBytes, byte[] ivBytes) { if (keyBytes != null) { if (keyBytes.length != 32) { throw new IllegalArgumentException(getAlgorithmName() + " requires 256 bit key"); } packTauOrSigma(keyBytes.length, engineState, 0); // Key Pack.littleEndianToInt(keyBytes, 0, engineState, 4, 8); } // IV Pack.littleEndianToInt(ivBytes, 0, engineState, 13, 3); }
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); } } }
Pack.littleEndianToInt(ivBytes, 8, engineState, 8, 2); Pack.littleEndianToInt(ivBytes, 16, engineState, 6, 2);
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[] 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 void compress(byte[] message, int messagePos) { initializeInternalState(); int[] m = new int[16]; for (int j = 0; j < 16; j++) { m[j] = Pack.littleEndianToInt(message, messagePos + j * 4); } for (int round = 0; round < ROUNDS; round++) { // G apply to columns of internalState:m[blake2s_sigma[round][2 * // blockPos]] /+1 G(m[blake2s_sigma[round][0]], m[blake2s_sigma[round][1]], 0, 4, 8, 12); G(m[blake2s_sigma[round][2]], m[blake2s_sigma[round][3]], 1, 5, 9, 13); G(m[blake2s_sigma[round][4]], m[blake2s_sigma[round][5]], 2, 6, 10, 14); G(m[blake2s_sigma[round][6]], m[blake2s_sigma[round][7]], 3, 7, 11, 15); // G apply to diagonals of internalState: G(m[blake2s_sigma[round][8]], m[blake2s_sigma[round][9]], 0, 5, 10, 15); G(m[blake2s_sigma[round][10]], m[blake2s_sigma[round][11]], 1, 6, 11, 12); G(m[blake2s_sigma[round][12]], m[blake2s_sigma[round][13]], 2, 7, 8, 13); G(m[blake2s_sigma[round][14]], m[blake2s_sigma[round][15]], 3, 4, 9, 14); } // update chain values: for (int offset = 0; offset < chainValue.length; offset++) { chainValue[offset] = chainValue[offset] ^ internalState[offset] ^ internalState[offset + 8]; } }
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; }
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; }