Search in sources :

Example 6 with SICBlockCipher

use of org.spongycastle.crypto.modes.SICBlockCipher in project java-tron by tronprotocol.

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 7 with SICBlockCipher

use of org.spongycastle.crypto.modes.SICBlockCipher in project fitness-app by seemoo-lab.

the class Firmware method rebootToBSLold.

public static String rebootToBSLold(Activity activity) throws UnsupportedEncodingException {
    /*int headerlength = 14;
        int inlength = 0;
        int plainlength = 0;
        int trailerlenght = 11;*/
    String outStr = "";
    /*File bslupdate = new File("/sdcard/788-bsl-plain.bin");
        byte[] rawInput = {0} ; //ExternalStorage.loadByteArray("/sdcard/788-bsl-plain.bin", activity);

        try {
            rawInput = fullyReadFileToBytes(bslupdate);
            //rawInput = ExternalStorage.loadByteArray("fwup-bsl-plain.bin",activity);
        }catch(IOException e) {

        }

        byte[] header = new byte[headerlength];
        byte[] trailer = new byte[trailerlenght];
        inlength = rawInput.length;
        plainlength = inlength - headerlength-trailerlenght;
        byte[] plain = new byte[plainlength];

        System.arraycopy(rawInput, 0, header, 0, headerlength);
        System.arraycopy(rawInput, headerlength, plain, 0, plainlength);
        System.arraycopy(rawInput, inlength-trailerlenght, trailer,0,trailerlenght);


        // get the nonce from the dump
        byte[] nonce = Arrays.copyOfRange(rawInput, 6, 10);

        //compute the initial counter value using the nonce
        byte[] counter = computeCounter(nonce);

        // use the XTEA block cipher in counter mode (CTR)
        SICBlockCipher cipher = new SICBlockCipher(new XTEAEngine());
        // initialize using the key and the initial counter value.
        cipher.init(true,new ParametersWithIV(new KeyParameter(ConstantValues.FITBIT_KEY), counter));


        byte[] encrypted = new byte[plainlength];


        //decrypt the encrypted part of the megadump, that is starting after byte 16
        cipher.processBytes(plain, 0, plainlength, encrypted, 0);

        //Log.e(TAG, "Decryped Dump");
        String strenc = toHexString(encrypted);
        Log.e(TAG, strenc);

        byte[] out = new byte[inlength];

        System.arraycopy(header, 0, out, 0, headerlength);
        System.arraycopy(encrypted, 0, out, headerlength, encrypted.length);

        byte[] cmac = calculateCMAC(nonce, plain, plainlength);

        System.arraycopy(cmac, 0, out, headerlength + encrypted.length, cmac.length);
        System.arraycopy(header, headerlength - 4, out, headerlength + encrypted.length + cmac.length, 3);*/
    int bslHeaderLength = ConstantValues.REBOOT_TO_BSL_HEADER.length;
    int bslDataLength = ConstantValues.REBOOT_TO_BSL_DATA.length;
    byte[] command = new byte[bslHeaderLength + bslDataLength];
    byte[] nonce = Arrays.copyOfRange(ConstantValues.REBOOT_TO_BSL_HEADER, 6, 10);
    // compute the initial counter value using the nonce
    // Crypto.computeCounter(nonce);
    byte[] counter = { 0 };
    // use the XTEA block cipher in counter mode (CTR)
    SICBlockCipher cipher = new SICBlockCipher(new XTEAEngine());
    // initialize using the key and the initial counter value.
    cipher.init(true, new ParametersWithIV(new KeyParameter(Utilities.hexStringToByteArray(AuthValues.ENCRYPTION_KEY)), counter));
    byte[] encrypted = new byte[bslDataLength];
    // decrypt the encrypted part of the megadump, that is starting after byte 16
    cipher.processBytes(command, 0, bslDataLength, encrypted, 0);
    System.arraycopy(ConstantValues.REBOOT_TO_BSL_HEADER, 0, command, 0, bslHeaderLength);
    System.arraycopy(encrypted, 0, command, bslHeaderLength, bslDataLength);
    outStr = Utilities.byteArrayToHexString(command);
    return outStr.toLowerCase();
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) KeyParameter(org.spongycastle.crypto.params.KeyParameter) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) XTEAEngine(org.spongycastle.crypto.engines.XTEAEngine)

Example 8 with SICBlockCipher

use of org.spongycastle.crypto.modes.SICBlockCipher in project Zom-Android by zom.

the class OtrCryptoEngineImpl method aesDecrypt.

public byte[] aesDecrypt(byte[] key, byte[] ctr, byte[] b) throws OtrCryptoException {
    AESFastEngine aesDec = new AESFastEngine();
    SICBlockCipher sicAesDec = new SICBlockCipher(aesDec);
    BufferedBlockCipher bufSicAesDec = new BufferedBlockCipher(sicAesDec);
    // Create initial counter value 0.
    if (ctr == null)
        ctr = ZERO_CTR;
    bufSicAesDec.init(false, new ParametersWithIV(new KeyParameter(key), ctr));
    byte[] aesOutLwDec = new byte[b.length];
    int done = bufSicAesDec.processBytes(b, 0, b.length, aesOutLwDec, 0);
    try {
        bufSicAesDec.doFinal(aesOutLwDec, done);
    } catch (Exception e) {
        throw new OtrCryptoException(e);
    }
    return aesOutLwDec;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 9 with SICBlockCipher

use of org.spongycastle.crypto.modes.SICBlockCipher in project Zom-Android by zom.

the class OtrCryptoEngineImpl method aesEncrypt.

public byte[] aesEncrypt(byte[] key, byte[] ctr, byte[] b) throws OtrCryptoException {
    AESFastEngine aesEnc = new AESFastEngine();
    SICBlockCipher sicAesEnc = new SICBlockCipher(aesEnc);
    BufferedBlockCipher bufSicAesEnc = new BufferedBlockCipher(sicAesEnc);
    // Create initial counter value 0.
    if (ctr == null)
        ctr = ZERO_CTR;
    bufSicAesEnc.init(true, new ParametersWithIV(new KeyParameter(key), ctr));
    byte[] aesOutLwEnc = new byte[b.length];
    int done = bufSicAesEnc.processBytes(b, 0, b.length, aesOutLwEnc, 0);
    try {
        bufSicAesEnc.doFinal(aesOutLwEnc, done);
    } catch (Exception e) {
        throw new OtrCryptoException(e);
    }
    return aesOutLwEnc;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 10 with SICBlockCipher

use of org.spongycastle.crypto.modes.SICBlockCipher 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)

Aggregations

SICBlockCipher (org.spongycastle.crypto.modes.SICBlockCipher)14 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)13 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)6 KeyParameter (org.spongycastle.crypto.params.KeyParameter)6 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)6 ECDHBasicAgreement (org.spongycastle.crypto.agreement.ECDHBasicAgreement)5 SHA256Digest (org.spongycastle.crypto.digests.SHA256Digest)5 HMac (org.spongycastle.crypto.macs.HMac)5 Test (org.junit.Test)4 ECPoint (org.spongycastle.math.ec.ECPoint)4 ConcatKDFBytesGenerator (org.ethereum.ConcatKDFBytesGenerator)3 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)3 IOException (java.io.IOException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SecureRandom (java.security.SecureRandom)2 AsymmetricCipherKeyPair (org.spongycastle.crypto.AsymmetricCipherKeyPair)2 KeyGenerationParameters (org.spongycastle.crypto.KeyGenerationParameters)2 IESEngine (org.spongycastle.crypto.engines.IESEngine)2 ECKeyPairGenerator (org.spongycastle.crypto.generators.ECKeyPairGenerator)2