Search in sources :

Example 16 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project toshi-android-client by toshiapp.

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.
 * @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 17 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project libsignal-service-java by signalapp.

the class ProfileCipher method encryptName.

public byte[] encryptName(byte[] input, int paddedLength) {
    try {
        byte[] inputPadded = new byte[paddedLength];
        if (input.length > inputPadded.length) {
            throw new IllegalArgumentException("Input is too long: " + new String(input));
        }
        System.arraycopy(input, 0, inputPadded, 0, input.length);
        byte[] nonce = Util.getSecretBytes(12);
        GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
        cipher.init(true, new AEADParameters(new KeyParameter(key), 128, nonce));
        byte[] ciphertext = new byte[cipher.getUpdateOutputSize(inputPadded.length)];
        cipher.processBytes(inputPadded, 0, inputPadded.length, ciphertext, 0);
        byte[] tag = new byte[cipher.getOutputSize(0)];
        cipher.doFinal(tag, 0);
        return ByteUtil.combine(nonce, ciphertext, tag);
    } catch (InvalidCipherTextException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) AEADParameters(org.spongycastle.crypto.params.AEADParameters) KeyParameter(org.spongycastle.crypto.params.KeyParameter) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) GCMBlockCipher(org.spongycastle.crypto.modes.GCMBlockCipher)

Example 18 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project rskj by rsksmart.

the class ECIESCoder method makeIESEngine.

private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] iv) {
    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(isEncrypt, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(pub, CURVE), parametersWithIV);
    return iesEngine;
}
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 19 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project rskj by rsksmart.

the class CryptoTest method test15.

// ECIES_AES128_SHA256 + Ephemeral Key + IV(all zeroes)
@Test
public void test15() throws Throwable {
    byte[] privKey = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1");
    ECKey ecKey = ECKey.fromPrivate(privKey);
    ECPrivateKeyParameters ecPrivKey = new ECPrivateKeyParameters(ecKey.getPrivKey(), ECKey.CURVE);
    ECPublicKeyParameters ecPubKey = new ECPublicKeyParameters(ecKey.getPubKeyPoint(), ECKey.CURVE);
    AsymmetricCipherKeyPair myKey = new AsymmetricCipherKeyPair(ecPubKey, ecPrivKey);
    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);
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);
    EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(generator, new KeyEncoder() {

        public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
            return ((ECPublicKeyParameters) keyParameter).getQ().getEncoded();
        }
    });
    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom()));
    iesEngine.init(myKey.getPublic(), parametersWithIV, kGen);
    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(myKey.getPrivate(), parametersWithIV, new ECIESPublicKeyParser(ECKey.CURVE));
    byte[] orig = decryptorIES_Engine.processBlock(cipher, 0, cipher.length);
    log.info("orig: " + Hex.toHexString(orig));
}
Also used : ECKeyPairGenerator(org.spongycastle.crypto.generators.ECKeyPairGenerator) EphemeralKeyPairGenerator(org.spongycastle.crypto.generators.EphemeralKeyPairGenerator) KeyEncoder(org.spongycastle.crypto.KeyEncoder) 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) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) IESEngine(org.spongycastle.crypto.engines.IESEngine) ECDHBasicAgreement(org.spongycastle.crypto.agreement.ECDHBasicAgreement) KDF2BytesGenerator(org.spongycastle.crypto.generators.KDF2BytesGenerator) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) ECIESPublicKeyParser(org.spongycastle.crypto.parsers.ECIESPublicKeyParser) Test(org.junit.Test)

Example 20 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project rskj by rsksmart.

the class CryptoTest method test12.

// big packet encryption
@Test
public void test12() throws Throwable {
    AESFastEngine engine = new AESFastEngine();
    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));
}
Also used : SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) Test(org.junit.Test)

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