Search in sources :

Example 1 with AESFastEngine

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;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) KeyParameter(org.spongycastle.crypto.params.KeyParameter) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) ECPoint(org.spongycastle.math.ec.ECPoint) BCECPrivateKey(org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)

Example 2 with AESFastEngine

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));
}
Also used : SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) Test(org.junit.Test)

Example 3 with AESFastEngine

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));
}
Also used : ECKeyPairGenerator(org.spongycastle.crypto.generators.ECKeyPairGenerator) HMac(org.spongycastle.crypto.macs.HMac) SecureRandom(java.security.SecureRandom) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) KeyGenerationParameters(org.spongycastle.crypto.KeyGenerationParameters) IESEngine(org.spongycastle.crypto.engines.IESEngine) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) ECDHBasicAgreement(org.spongycastle.crypto.agreement.ECDHBasicAgreement) KDF2BytesGenerator(org.spongycastle.crypto.generators.KDF2BytesGenerator) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) Test(org.junit.Test)

Example 4 with AESFastEngine

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);
}
Also used : HMac(org.spongycastle.crypto.macs.HMac) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) ECDHBasicAgreement(org.spongycastle.crypto.agreement.ECDHBasicAgreement) ConcatKDFBytesGenerator(org.ethereum.ConcatKDFBytesGenerator) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher)

Example 5 with AESFastEngine

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;
}
Also used : SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) ECPoint(org.spongycastle.math.ec.ECPoint)

Aggregations

AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)22 SICBlockCipher (org.spongycastle.crypto.modes.SICBlockCipher)13 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)12 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)11 KeyParameter (org.spongycastle.crypto.params.KeyParameter)10 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)6 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)6 ECDHBasicAgreement (org.spongycastle.crypto.agreement.ECDHBasicAgreement)5 SHA256Digest (org.spongycastle.crypto.digests.SHA256Digest)5 HMac (org.spongycastle.crypto.macs.HMac)5 IOException (java.io.IOException)4 Test (org.junit.Test)4 InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)4 ECPoint (org.spongycastle.math.ec.ECPoint)4 ConcatKDFBytesGenerator (org.ethereum.ConcatKDFBytesGenerator)3 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)3 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SecureRandom (java.security.SecureRandom)2