List of usage examples for org.bouncycastle.util Pack intToBigEndian
public static void intToBigEndian(int[] ns, byte[] bs, int off)
From source file:org.ethereum.ConcatKDFBytesGenerator.java
License:Open Source License
/** * fill len bytes of the output buffer with bytes generated from the * derivation function.//www .j a v a 2s .c o m * * @throws IllegalArgumentException * if the size of the request will cause an overflow. * @throws DataLengthException * if the out buffer is too small. */ public int generateBytes(byte[] out, int outOff, int len) throws DataLengthException, IllegalArgumentException { if ((out.length - len) < outOff) { throw new DataLengthException("output buffer too small"); } long oBytes = len; int outLen = digest.getDigestSize(); // // this is at odds with the standard implementation, the // maximum value should be hBits * (2^32 - 1) where hBits // is the digest output size in bits. We can't have an // array with a long index at the moment... // if (oBytes > ((2L << 32) - 1)) { throw new IllegalArgumentException("Output length too large"); } int cThreshold = (int) ((oBytes + outLen - 1) / outLen); byte[] dig = new byte[digest.getDigestSize()]; byte[] c = new byte[4]; Pack.intToBigEndian(counterStart, c, 0); int counterBase = counterStart & ~0xFF; for (int i = 0; i < cThreshold; i++) { digest.update(c, 0, c.length); digest.update(shared, 0, shared.length); if (iv != null) { digest.update(iv, 0, iv.length); } digest.doFinal(dig, 0); if (len > outLen) { System.arraycopy(dig, 0, out, outOff, outLen); outOff += outLen; len -= outLen; } else { System.arraycopy(dig, 0, out, outOff, len); } if (++c[3] == 0) { counterBase += 0x100; Pack.intToBigEndian(counterBase, c, 0); } } digest.reset(); return (int) oBytes; }
From source file:org.ethereum.crypto.EthereumIESEngine.java
License:Open Source License
private byte[] encryptBlock(byte[] in, int inOff, int inLen, byte[] macData) throws InvalidCipherTextException { byte[] c = null; byte[] k = null; byte[] k1 = null; byte[] k2 = null; int len;//from w w w. j ava2s . c o m if (cipher == null) { // Streaming mode. k1 = new byte[inLen]; k2 = new byte[param.getMacKeySize() / 8]; k = new byte[k1.length + k2.length]; kdf.generateBytes(k, 0, k.length); // if (v.length != 0) // { // System.arraycopy(K, 0, K2, 0, K2.length); // System.arraycopy(K, K2.length, K1, 0, K1.length); // } // else { System.arraycopy(k, 0, k1, 0, k1.length); System.arraycopy(k, inLen, k2, 0, k2.length); } c = new byte[inLen]; for (int i = 0; i != inLen; i++) { c[i] = (byte) (in[inOff + i] ^ k1[i]); } len = inLen; } else { // Block cipher mode. k1 = new byte[((IESWithCipherParameters) param).getCipherKeySize() / 8]; k2 = new byte[param.getMacKeySize() / 8]; k = new byte[k1.length + k2.length]; kdf.generateBytes(k, 0, k.length); System.arraycopy(k, 0, k1, 0, k1.length); System.arraycopy(k, k1.length, k2, 0, k2.length); // If iv provided use it to initialise the cipher if (iv != null) { cipher.init(true, new ParametersWithIV(new KeyParameter(k1), iv)); } else { cipher.init(true, new KeyParameter(k1)); } c = new byte[cipher.getOutputSize(inLen)]; len = cipher.processBytes(in, inOff, inLen, c, 0); len += cipher.doFinal(c, len); } // Convert the length of the encoding vector into a byte array. byte[] p2 = param.getEncodingV(); // Apply the MAC. byte[] t = new byte[mac.getMacSize()]; byte[] k2A; if (hashK2) { k2A = new byte[hash.getDigestSize()]; hash.reset(); hash.update(k2, 0, k2.length); hash.doFinal(k2A, 0); } else { k2A = k2; } mac.init(new KeyParameter(k2A)); mac.update(iv, 0, iv.length); mac.update(c, 0, c.length); if (p2 != null) { mac.update(p2, 0, p2.length); } if (v.length != 0 && p2 != null) { byte[] l2 = new byte[4]; Pack.intToBigEndian(p2.length * 8, l2, 0); mac.update(l2, 0, l2.length); } if (macData != null) { mac.update(macData, 0, macData.length); } mac.doFinal(t, 0); // Output the triple (v,C,T). byte[] output = new byte[v.length + len + t.length]; System.arraycopy(v, 0, output, 0, v.length); System.arraycopy(c, 0, output, v.length, len); System.arraycopy(t, 0, output, v.length + len, t.length); return output; }
From source file:org.ethereum.crypto.EthereumIESEngine.java
License:Open Source License
private byte[] decryptBlock(byte[] inEnc, int inOff, int inLen, byte[] macData) throws InvalidCipherTextException { byte[] m = null; byte[] k = null; byte[] k1 = null; byte[] k2 = null; int len;/*from w w w . j a v a 2 s .c o m*/ // Ensure that the length of the input is greater than the MAC in bytes if (inLen <= (param.getMacKeySize() / 8)) { throw new InvalidCipherTextException("Length of input must be greater than the MAC"); } if (cipher == null) { // Streaming mode. k1 = new byte[inLen - v.length - mac.getMacSize()]; k2 = new byte[param.getMacKeySize() / 8]; k = new byte[k1.length + k2.length]; kdf.generateBytes(k, 0, k.length); // if (v.length != 0) // { // System.arraycopy(K, 0, K2, 0, K2.length); // System.arraycopy(K, K2.length, K1, 0, K1.length); // } // else { System.arraycopy(k, 0, k1, 0, k1.length); System.arraycopy(k, k1.length, k2, 0, k2.length); } m = new byte[k1.length]; for (int i = 0; i != k1.length; i++) { m[i] = (byte) (inEnc[inOff + v.length + i] ^ k1[i]); } len = k1.length; } else { // Block cipher mode. k1 = new byte[((IESWithCipherParameters) param).getCipherKeySize() / 8]; k2 = new byte[param.getMacKeySize() / 8]; k = new byte[k1.length + k2.length]; kdf.generateBytes(k, 0, k.length); System.arraycopy(k, 0, k1, 0, k1.length); System.arraycopy(k, k1.length, k2, 0, k2.length); // If iv provide use it to initialize the cipher if (iv != null) { cipher.init(false, new ParametersWithIV(new KeyParameter(k1), iv)); } else { cipher.init(false, new KeyParameter(k1)); } m = new byte[cipher.getOutputSize(inLen - v.length - mac.getMacSize())]; len = cipher.processBytes(inEnc, inOff + v.length, inLen - v.length - mac.getMacSize(), m, 0); len += cipher.doFinal(m, len); } // Convert the length of the encoding vector into a byte array. byte[] p2 = param.getEncodingV(); // Verify the MAC. int end = inOff + inLen; byte[] t1 = Arrays.copyOfRange(inEnc, end - mac.getMacSize(), end); byte[] t2 = new byte[t1.length]; byte[] k2A; if (hashK2) { k2A = new byte[hash.getDigestSize()]; hash.reset(); hash.update(k2, 0, k2.length); hash.doFinal(k2A, 0); } else { k2A = k2; } mac.init(new KeyParameter(k2A)); mac.update(iv, 0, iv.length); mac.update(inEnc, inOff + v.length, inLen - v.length - t2.length); if (p2 != null) { mac.update(p2, 0, p2.length); } if (v.length != 0 && p2 != null) { byte[] l2 = new byte[4]; Pack.intToBigEndian(p2.length * 8, l2, 0); mac.update(l2, 0, l2.length); } if (macData != null) { mac.update(macData, 0, macData.length); } mac.doFinal(t2, 0); if (!Arrays.constantTimeAreEqual(t1, t2)) { throw new InvalidCipherTextException("Invalid MAC."); } // Output the message. return Arrays.copyOfRange(m, 0, len); }