Search in sources :

Example 6 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV 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 7 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project KeePassDX by Kunzisoft.

the class PwStreamCipherFactory method getSalsa20.

private static StreamCipher getSalsa20(byte[] key) {
    // Build stream cipher key
    byte[] key32 = CryptoUtil.hashSha256(key);
    KeyParameter keyParam = new KeyParameter(key32);
    ParametersWithIV ivParam = new ParametersWithIV(keyParam, SALSA_IV);
    StreamCipher cipher = new Salsa20Engine();
    cipher.init(true, ivParam);
    return cipher;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) KeyParameter(org.spongycastle.crypto.params.KeyParameter) Salsa20Engine(org.spongycastle.crypto.engines.Salsa20Engine) StreamCipher(org.spongycastle.crypto.StreamCipher)

Example 8 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project jmulticard by ctt-gob-es.

the class AmAESCrypto method initCiphers.

private void initCiphers(final byte[] key, final byte[] iv) {
    // get the keyBytes
    this.keyBytes = new byte[key.length];
    System.arraycopy(key, 0, this.keyBytes, 0, key.length);
    this.keyP = new KeyParameter(this.keyBytes);
    // get the IV
    this.IV = new byte[BLOCK_SIZE];
    System.arraycopy(iv, 0, this.IV, 0, this.IV.length);
    // create the ciphers
    // AES block cipher in CBC mode with ISO7816d4 padding
    this.encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new ISO7816d4Padding());
    this.decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new ISO7816d4Padding());
    // create the IV parameter
    final ParametersWithIV parameterIV = new ParametersWithIV(this.keyP, this.IV);
    this.encryptCipher.init(true, parameterIV);
    this.decryptCipher.init(false, parameterIV);
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.spongycastle.crypto.engines.AESEngine) ISO7816d4Padding(org.spongycastle.crypto.paddings.ISO7816d4Padding) KeyParameter(org.spongycastle.crypto.params.KeyParameter) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher)

Example 9 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project SightRemote by TebbeUbben.

the class Cryptograph method produceCCMTag.

public static byte[] produceCCMTag(byte[] nonce, byte[] payload, byte[] header, byte[] key) {
    TwofishEngine engine = new TwofishEngine();
    engine.init(true, new KeyParameter(key));
    byte[] initializationVector = new byte[engine.getBlockSize()];
    engine.processBlock(produceIV(nonce, (short) payload.length), 0, initializationVector, 0);
    CBCBlockCipher cbc = new CBCBlockCipher(new TwofishEngine());
    cbc.init(true, new ParametersWithIV(new KeyParameter(key), initializationVector));
    byte[] processedHeader = blockCipherZeroPad(processHeader(header));
    byte[] processedPayload = blockCipherZeroPad(payload);
    byte[] combine = combine(processedHeader, blockCipherZeroPad(processedPayload));
    byte[] result = new byte[combine.length];
    for (int i = 0; i < combine.length / 16; i++) cbc.processBlock(combine, i * 16, result, i * 16);
    byte[] result2 = new byte[8];
    System.arraycopy(result, result.length - 16, result2, 0, 8);
    byte[] ctr = new byte[engine.getBlockSize()];
    engine.processBlock(produceCTRBlock(nonce, (short) 0), 0, ctr, 0);
    return byteArrayXOR(result2, ctr);
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) AsymmetricKeyParameter(org.spongycastle.crypto.params.AsymmetricKeyParameter) KeyParameter(org.spongycastle.crypto.params.KeyParameter) TwofishEngine(org.spongycastle.crypto.engines.TwofishEngine) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher)

Example 10 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project bitcoin-wallet by bitcoin-wallet.

the class Crypto method decryptRaw.

/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param bytesToDecode
 *            The bytes to decrypt
 * @param passwordbThe
 *            password to use for decryption
 * @return The decrypted bytes
 * @throws IOException
 */
private static byte[] decryptRaw(final byte[] bytesToDecode, final char[] password) throws IOException {
    try {
        // separate the salt and bytes to decrypt
        final byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(bytesToDecode, 0, salt, 0, SALT_LENGTH);
        final byte[] cipherBytes = new byte[bytesToDecode.length - SALT_LENGTH];
        System.arraycopy(bytesToDecode, SALT_LENGTH, cipherBytes, 0, bytesToDecode.length - SALT_LENGTH);
        final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
        // decrypt the message
        final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, key);
        final byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int processLen = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int doFinalLen = cipher.doFinal(decryptedBytes, processLen);
        return Arrays.copyOf(decryptedBytes, processLen + doFinalLen);
    } catch (final InvalidCipherTextException | DataLengthException x) {
        throw new IOException("Could not decrypt bytes", x);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) DataLengthException(org.spongycastle.crypto.DataLengthException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException)

Aggregations

ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)17 KeyParameter (org.spongycastle.crypto.params.KeyParameter)12 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)11 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)8 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)8 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)7 SICBlockCipher (org.spongycastle.crypto.modes.SICBlockCipher)6 IOException (java.io.IOException)4 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)3 ECPoint (org.spongycastle.math.ec.ECPoint)3 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 DataLengthException (org.spongycastle.crypto.DataLengthException)2 InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)2 StreamCipher (org.spongycastle.crypto.StreamCipher)2 PBEParametersGenerator (org.spongycastle.crypto.PBEParametersGenerator)1 AESEngine (org.spongycastle.crypto.engines.AESEngine)1 ChaCha7539Engine (org.spongycastle.crypto.engines.ChaCha7539Engine)1 Salsa20Engine (org.spongycastle.crypto.engines.Salsa20Engine)1