use of org.spongycastle.crypto.engines.AESFastEngine in project aion by aionnetwork.
the class ECKeySecp256k1 method decryptAES.
/**
* Decrypt cipher by AES in SIC(also know as CTR) mode
*
* @param cipher
* -proper cipher
* @return decrypted cipher, equal length to the cipher.
* @deprecated should not use EC private scalar value as an AES key
*/
public byte[] decryptAES(byte[] cipher) {
if (privKey == null) {
throw new MissingPrivateKeyException();
}
if (!(privKey instanceof BCECPrivateKey)) {
throw new UnsupportedOperationException("Cannot use the private key as an AES key");
}
AESFastEngine engine = new AESFastEngine();
SICBlockCipher ctrEngine = new SICBlockCipher(engine);
KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD()));
ParametersWithIV params = new ParametersWithIV(key, new byte[16]);
ctrEngine.init(false, params);
int i = 0;
byte[] out = new byte[cipher.length];
while (i < cipher.length) {
ctrEngine.processBlock(cipher, i, out, i);
i += engine.getBlockSize();
if (cipher.length - i < engine.getBlockSize()) {
break;
}
}
// process left bytes
if (cipher.length - i > 0) {
byte[] tmpBlock = new byte[16];
System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
}
return out;
}
use of org.spongycastle.crypto.engines.AESFastEngine in project rskj by rsksmart.
the class CryptoTest method test11.
// basic encryption/decryption
@Test
public void test11() throws Throwable {
byte[] keyBytes = HashUtil.keccak256("...".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]);
AESFastEngine engine = new AESFastEngine();
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));
}
use of org.spongycastle.crypto.engines.AESFastEngine in project rskj by rsksmart.
the class CryptoTest method test14.
// ECIES_AES128_SHA256 + No Ephemeral Key + IV(all zeroes)
@Test
public void test14() throws Throwable {
AESFastEngine aesFastEngine = new AESFastEngine();
IESEngine iesEngine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));
byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
IESParameters p = new IESWithCipherParameters(d, e, 64, 128);
ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[16]);
ECKeyPairGenerator eGen = new ECKeyPairGenerator();
KeyGenerationParameters gParam = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
eGen.init(gParam);
AsymmetricCipherKeyPair p1 = eGen.generateKeyPair();
AsymmetricCipherKeyPair p2 = eGen.generateKeyPair();
ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
ECKeyPairGenerator generator = new ECKeyPairGenerator();
generator.init(keygenParams);
ECKeyPairGenerator gen = new ECKeyPairGenerator();
gen.init(new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom()));
iesEngine.init(true, p1.getPrivate(), p2.getPublic(), parametersWithIV);
byte[] message = Hex.decode("010101");
log.info("payload: {}", Hex.toHexString(message));
byte[] cipher = iesEngine.processBlock(message, 0, message.length);
log.info("cipher: {}", Hex.toHexString(cipher));
IESEngine decryptorIES_Engine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));
decryptorIES_Engine.init(false, p2.getPrivate(), p1.getPublic(), parametersWithIV);
byte[] orig = decryptorIES_Engine.processBlock(cipher, 0, cipher.length);
log.info("orig: " + Hex.toHexString(orig));
}
use of org.spongycastle.crypto.engines.AESFastEngine in project rskj by rsksmart.
the class ECIESCoder method decrypt.
public static byte[] decrypt(ECPoint ephem, BigInteger prv, byte[] iv, byte[] cipher, byte[] macData) throws InvalidCipherTextException {
AESFastEngine aesFastEngine = new AESFastEngine();
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);
}
use of org.spongycastle.crypto.engines.AESFastEngine in project rskj by rsksmart.
the class ECKey method decryptAES.
/**
* Decrypt cipher by AES in SIC(also know as CTR) mode
*
* @param cipher -proper cipher
* @return decrypted cipher, equal length to the cipher.
*/
public byte[] decryptAES(byte[] cipher) {
if (priv == null) {
throw new MissingPrivateKeyException();
}
AESFastEngine engine = new AESFastEngine();
SICBlockCipher ctrEngine = new SICBlockCipher(engine);
KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(priv));
ParametersWithIV params = new ParametersWithIV(key, new byte[16]);
ctrEngine.init(false, params);
int i = 0;
byte[] out = new byte[cipher.length];
while (i < cipher.length) {
ctrEngine.processBlock(cipher, i, out, i);
i += engine.getBlockSize();
if (cipher.length - i < engine.getBlockSize()) {
break;
}
}
// process left bytes
if (cipher.length - i > 0) {
byte[] tmpBlock = new byte[16];
System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
}
return out;
}
Aggregations