private void doSum(KeccakDigest mac, byte[] out) { // doFinal without resetting the MAC by using clone of digest state new KeccakDigest(mac).doFinal(out, 0); }
KeccakDigest mac1 = new KeccakDigest(MAC_SIZE); mac1.update(xor(secrets.mac, responderNonce), 0, secrets.mac.length); byte[] buf = new byte[32]; new KeccakDigest(mac1).doFinal(buf, 0); mac1.update(initiatePacket, 0, initiatePacket.length); new KeccakDigest(mac1).doFinal(buf, 0); KeccakDigest mac2 = new KeccakDigest(MAC_SIZE); mac2.update(xor(secrets.mac, initiatorNonce), 0, secrets.mac.length); new KeccakDigest(mac2).doFinal(buf, 0); mac2.update(responsePacket, 0, responsePacket.length); new KeccakDigest(mac2).doFinal(buf, 0); if (isInitiator) { secrets.egressMac = mac1;
/** * @param data * - message to hash * @return - reipmd160 hash of the message */ public static byte[] ripemd160(byte[] data) { Digest digest = new RIPEMD160Digest(); if (data != null) { byte[] resBuf = new byte[digest.getDigestSize()]; digest.update(data, 0, data.length); digest.doFinal(resBuf, 0); return resBuf; } throw new NullPointerException("Can't hash a NULL value"); }
private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] IV) { AESEngine aesFastEngine = new AESEngine(); EthereumIESEngine iesEngine = new EthereumIESEngine( new ECDHBasicAgreement(), new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine))); byte[] d = new byte[] {}; byte[] e = new byte[] {}; IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE); ParametersWithIV parametersWithIV = new ParametersWithIV(p, IV); iesEngine.init(isEncrypt, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(pub, CURVE), parametersWithIV); return iesEngine; }
CombinedHash(TlsClientContext context) { this.context = context; this.md5 = new MD5Digest(); this.sha1 = new SHA1Digest(); }
/** * Encryption equivalent to the Crypto++ default ECIES<ECP> settings: * * DL_KeyAgreementAlgorithm: DL_KeyAgreementAlgorithm_DH<struct ECPPoint,struct EnumToType<enum CofactorMultiplicationOption,0> > * DL_KeyDerivationAlgorithm: DL_KeyDerivationAlgorithm_P1363<struct ECPPoint,0,class P1363_KDF2<class SHA1> > * DL_SymmetricEncryptionAlgorithm: DL_EncryptionAlgorithm_Xor<class HMAC<class SHA1>,0> * DL_PrivateKey: DL_Key<ECPPoint> * DL_PrivateKey_EC<class ECP> * * Used for Whisper V3 */ public static byte[] decryptSimple(BigInteger privKey, byte[] cipher) throws IOException, InvalidCipherTextException { EthereumIESEngine iesEngine = new EthereumIESEngine( new ECDHBasicAgreement(), new MGF1BytesGeneratorExt(new SHA1Digest(), 1), new HMac(new SHA1Digest()), new SHA1Digest(), null); IESParameters p = new IESParameters(null, null, KEY_SIZE); ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[0]); iesEngine.setHashMacKey(false); iesEngine.init(new ECPrivateKeyParameters(privKey, CURVE), parametersWithIV, new ECIESPublicKeyParser(ECKey.CURVE)); return iesEngine.processBlock(cipher, 0, cipher.length); }
private byte[] updateMac(KeccakDigest mac, byte[] seed, int offset, byte[] out, int outOffset, boolean egress) throws IOException { byte[] aesBlock = new byte[mac.getDigestSize()]; doSum(mac, aesBlock); makeMacCipher().processBlock(aesBlock, 0, aesBlock, 0); // Note that although the mac digest size is 32 bytes, we only use 16 bytes in the computation int length = 16; for (int i = 0; i < length; i++) { aesBlock[i] ^= seed[i + offset]; } mac.update(aesBlock, 0, length); byte[] result = new byte[mac.getDigestSize()]; doSum(mac, result); if (egress) { System.arraycopy(result, 0, out, outOffset, length); } else { for (int i = 0; i < length; i++) { if (out[i + outOffset] != result[i]) { throw new IOException("MAC mismatch"); } } } return result; }
assertArrayEquals(decode("2ea74ec5dae199227dff1af715362700e989d889d7a493cb0639691efb8e5f98"), handshakerB.getSecrets().mac); byte[] fooHash = new byte[32]; handshakerB.getSecrets().ingressMac.update("foo".getBytes(), 0, "foo".getBytes().length); handshakerB.getSecrets().ingressMac.doFinal(fooHash, 0); assertArrayEquals(decode("0c7ec6340062cc46f5e9f1e3cf86f8c8c403c5a0964f5df0ebd34a75ddc86db5"), fooHash);
public int doFinal(byte[] out, int outOff) { absorbBits(0x02, 2); return super.doFinal(out, outOff); }
public byte[] getEncodedState() { byte[] encoded = new byte[getEncodedStateSize()]; super.populateState(encoded); return encoded; } }
public static byte[] decrypt(ECPoint ephem, BigInteger prv, byte[] IV, byte[] cipher, byte[] macData) throws InvalidCipherTextException { AESEngine aesFastEngine = new AESEngine(); EthereumIESEngine iesEngine = new EthereumIESEngine( new ECDHBasicAgreement(), new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine))); byte[] d = new byte[] {}; byte[] e = new byte[] {}; IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE); ParametersWithIV parametersWithIV = new ParametersWithIV(p, IV); iesEngine.init(false, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(ephem, CURVE), parametersWithIV); return iesEngine.processBlock(cipher, 0, cipher.length, macData); }
EthereumIESEngine iesEngine = new EthereumIESEngine( new ECDHBasicAgreement(), new MGF1BytesGeneratorExt(new SHA1Digest(), 1), new HMac(new SHA1Digest()), new SHA1Digest(), null);
enc.processBytes(ptype, 0, ptype.length, buff, 0); out.write(buff, 0, ptype.length); egressMac.update(buff, 0, ptype.length); while (true) { int n = frame.payload.read(buff); if (n <= 0) break; enc.processBytes(buff, 0, n, buff, 0); egressMac.update(buff, 0, n); out.write(buff, 0, n); if (padding < 16) { enc.processBytes(pad, 0, padding, buff, 0); egressMac.update(buff, 0, padding); out.write(buff, 0, padding); byte[] macBuffer = new byte[egressMac.getDigestSize()];
@Test public void testKDF() { ConcatKDFBytesGenerator kdf = new ConcatKDFBytesGenerator(new SHA256Digest()); kdf.init(new KDFParameters("Hello".getBytes(), new byte[0])); byte[] bytes = new byte[2]; kdf.generateBytes(bytes, 0, bytes.length); assertArrayEquals(new byte[]{-66, -89}, bytes); }
ingressMac.update(buffer, 0, frameSize); dec.processBytes(buffer, 0, frameSize, buffer, 0); int pos = 0; InputStream payload = new ByteArrayInputStream(buffer, pos, totalBodySize - pos); int size = totalBodySize - pos; byte[] macBuffer = new byte[ingressMac.getDigestSize()];
throw new MissingPrivateKeyException(); if (privKey instanceof BCECPrivateKey) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE); signer.init(true, privKeyParams);
private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] IV) { AESEngine aesFastEngine = new AESEngine(); EthereumIESEngine iesEngine = new EthereumIESEngine( new ECDHBasicAgreement(), new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine))); byte[] d = new byte[] {}; byte[] e = new byte[] {}; IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE); ParametersWithIV parametersWithIV = new ParametersWithIV(p, IV); iesEngine.init(isEncrypt, new ECPrivateKeyParameters(prv, curve), new ECPublicKeyParameters(pub, curve), parametersWithIV); return iesEngine; }
new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine))); new KDF2BytesGenerator (new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));
new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine))); new KDF2BytesGenerator (new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));