public FrameCodec(EncryptionHandshake.Secrets secrets) { this.mac = secrets.mac; BlockCipher cipher; enc = new SICBlockCipher(cipher = new AESEngine()); enc.init(true, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[cipher.getBlockSize()])); dec = new SICBlockCipher(cipher = new AESEngine()); dec.init(false, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[cipher.getBlockSize()])); egressMac = secrets.egressMac; ingressMac = secrets.ingressMac; }
AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); while(i < cipher.length){ ctrEngine.processBlock(cipher, i, out, i); i += engine.getBlockSize(); if (cipher.length - i < engine.getBlockSize()) break;
/** * Create a final key from the parameters passed */ public static byte[] getFinalKeyDigest(byte[] key, byte[] masterSeed, byte[] transformSeed, long transformRounds) { AESEngine engine = new AESEngine(); engine.init(true, new KeyParameter(transformSeed)); // copy input key byte[] transformedKey = new byte[key.length]; System.arraycopy(key, 0, transformedKey, 0, transformedKey.length); // transform rounds times for (long rounds = 0; rounds < transformRounds; rounds++) { engine.processBlock(transformedKey, 0, transformedKey, 0); engine.processBlock(transformedKey, 16, transformedKey, 16); } MessageDigest md = getMessageDigestInstance(); byte[] transformedKeyDigest = md.digest(transformedKey); md.update(masterSeed); return md.digest(transformedKeyDigest); }
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; }
xor[i] ^= privateKeyBytes[i]; AESEngine cipher = new AESEngine(); cipher.init(true, new KeyParameter(key)); byte[] encryptedHalf1 = new byte[16]; byte[] encryptedHalf2 = new byte[16]; cipher.processBlock(xor, 0, encryptedHalf1, 0); cipher.processBlock(xor, 16, encryptedHalf2, 0); byte[] result = new byte[43]; result[0] = 1;
@Test // basic encryption/decryption public void test11() throws Throwable { byte[] keyBytes = sha3("...".getBytes()); log.info("key: {}", Hex.toHexString(keyBytes)); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode("22400891000000000000000000000000"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, new byte[16]); AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); ctrEngine.init(true, params); byte[] cipher = new byte[16]; ctrEngine.processBlock(payload, 0, cipher, 0); log.info("cipher: {}", Hex.toHexString(cipher)); byte[] output = new byte[cipher.length]; ctrEngine.init(false, params); ctrEngine.processBlock(cipher, 0, output, 0); assertEquals(Hex.toHexString(output), Hex.toHexString(payload)); log.info("original: {}", Hex.toHexString(payload)); }
seedB[i] ^= derived[i]; AESEngine cipher = new AESEngine(); cipher.init(true, new KeyParameter(key)); byte[] encryptedHalf1 = new byte[16]; byte[] encryptedHalf2 = new byte[16]; cipher.processBlock(seedB, 0, encryptedHalf1, 0); byte[] secondBlock = new byte[16]; System.arraycopy(encryptedHalf1, 8, secondBlock, 0, 8); secondBlock[i] ^= derived[i + 16]; cipher.processBlock(secondBlock, 0, encryptedHalf2, 0); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(0x01); pointB[i + 1] ^= derived[i]; cipher.processBlock(pointB, 1, encryptedPointB, 1); cipher.processBlock(pointB, 17, encryptedPointB, 17); baos.reset(); baos.write(0x64);
@Test // big packet encryption public void test12() throws Throwable { AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); byte[] keyBytes = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1"); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode("0109efc76519b683d543db9d0991bcde99cc9a3d14b1d0ecb8e9f1f66f31558593d746eaa112891b04ef7126e1dce17c9ac92ebf39e010f0028b8ec699f56f5d0c0d00"); byte[] cipherText = Hex.decode("f9fab4e9dd9fc3e5d0d0d16da254a2ac24df81c076e3214e2c57da80a46e6ae4752f4b547889fa692b0997d74f36bb7c047100ba71045cb72cfafcc7f9a251762cdf8f"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, ivBytes); ctrEngine.init(true, params); byte[] in = payload; byte[] out = new byte[in.length]; int i = 0; while(i < in.length){ ctrEngine.processBlock(in, i, out, i); i += engine.getBlockSize(); if (in.length - i < engine.getBlockSize()) break; } // process left bytes if (in.length - i > 0){ byte[] tmpBlock = new byte[16]; System.arraycopy(in, i, tmpBlock, 0, in.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, in.length - i); } log.info("cipher: {}", Hex.toHexString(out)); assertEquals(Hex.toHexString(cipherText), Hex.toHexString(out)); }
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; }
System.arraycopy(encryptedPrivateKeyBytes, 3, addressHash, 0, 4); boolean compressed = (encryptedPrivateKeyBytes[2] & 0x20) == 0x20; AESEngine cipher = new AESEngine(); byte[] key = new byte[32]; System.arraycopy(passwordDerived, 32, key, 0, 32); cipher.init(false, new KeyParameter(key)); byte[] secret = new byte[32]; cipher.processBlock(encryptedSecret, 0, secret, 0); cipher.processBlock(encryptedSecret, 16, secret, 16); for (int i = 0; i < 32; i++) { secret[i] ^= passwordDerived[i]; byte[] key = new byte[32]; System.arraycopy(derived, 32, key, 0, 32); cipher.init(false, new KeyParameter(key)); byte[] decryptedHalf2 = new byte[16]; cipher.processBlock(encryptedPrivateKeyBytes, 23, decryptedHalf2, 0); for (int i = 0; i < 16; i++) { decryptedHalf2[i] ^= derived[i + 16]; System.arraycopy(decryptedHalf2, 0, encryptedHalf1, 8, 8); byte[] decryptedHalf1 = new byte[16]; cipher.processBlock(encryptedHalf1, 0, decryptedHalf1, 0); for (int i = 0; i < 16; i++) { decryptedHalf1[i] ^= derived[i];
@Test // big packet encryption public void test12() { AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); byte[] keyBytes = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1"); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode("0109efc76519b683d543db9d0991bcde99cc9a3d14b1d0ecb8e9f1f66f31558593d746eaa112891b04ef7126e1dce17c9ac92ebf39e010f0028b8ec699f56f5d0c0d00"); byte[] cipherText = Hex.decode("f9fab4e9dd9fc3e5d0d0d16da254a2ac24df81c076e3214e2c57da80a46e6ae4752f4b547889fa692b0997d74f36bb7c047100ba71045cb72cfafcc7f9a251762cdf8f"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, ivBytes); ctrEngine.init(true, params); byte[] out = new byte[payload.length]; int i = 0; while (i < payload.length) { ctrEngine.processBlock(payload, i, out, i); i += engine.getBlockSize(); if (payload.length - i < engine.getBlockSize()) { break; } } // process left bytes if (payload.length - i > 0) { byte[] tmpBlock = new byte[16]; System.arraycopy(payload, i, tmpBlock, 0, payload.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, payload.length - i); } log.info("cipher: {}", Hex.toHexString(out)); assertEquals(Hex.toHexString(cipherText), Hex.toHexString(out)); }
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); }
byte[] key = new byte[32]; System.arraycopy(derived, 32, key, 0, 32); AESEngine cipher = new AESEngine(); cipher.init(false, new KeyParameter(key)); cipher.processBlock(encryptedPointB, 1, pointB, 1); cipher.processBlock(encryptedPointB, 17, pointB, 17);
AESEngine aesFastEngine = new AESEngine();
AESEngine aesFastEngine = new AESEngine();
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; }
/** * Create a regular AESWrapEngine specifying the encrypt for wrapping, decrypt for unwrapping. */ public AESWrapEngine() { super(new AESEngine()); }
public BlockCipher get() { return new AESEngine(); } });
public AESWrapEngine() { super(new AESEngine()); } }