Search in sources :

Example 6 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbV4Output method attachStreamEncryptor.

private CipherOutputStream attachStreamEncryptor(PwDbHeaderV4 header, OutputStream os) throws PwDbOutputException {
    Cipher cipher;
    try {
        // mPM.makeFinalKey(header.masterSeed, mPM.kdfParameters);
        cipher = engine.getCipher(Cipher.ENCRYPT_MODE, mPM.finalKey, header.encryptionIV);
    } catch (Exception e) {
        throw new PwDbOutputException("Invalid algorithm.", e);
    }
    CipherOutputStream cos = new CipherOutputStream(os, cipher);
    return cos;
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) StreamCipher(org.spongycastle.crypto.StreamCipher) Cipher(javax.crypto.Cipher) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Example 7 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbV4Output method outputHeader.

@Override
public PwDbHeader outputHeader(OutputStream os) throws PwDbOutputException {
    PwDbHeaderV4 header = new PwDbHeaderV4(mPM);
    setIVs(header);
    PwDbHeaderOutputV4 pho = new PwDbHeaderOutputV4(mPM, header, os);
    try {
        pho.output();
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to output the header.", e);
    }
    hashOfHeader = pho.getHashOfHeader();
    headerHmac = pho.headerHmac;
    return header;
}
Also used : PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) PwDbHeaderV4(com.keepassdroid.database.PwDbHeaderV4) IOException(java.io.IOException)

Example 8 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbV4Output method setIVs.

@Override
protected SecureRandom setIVs(PwDbHeader header) throws PwDbOutputException {
    SecureRandom random = super.setIVs(header);
    PwDbHeaderV4 h = (PwDbHeaderV4) header;
    random.nextBytes(h.masterSeed);
    int ivLength = engine.ivLength();
    if (ivLength != h.encryptionIV.length) {
        h.encryptionIV = new byte[ivLength];
    }
    random.nextBytes(h.encryptionIV);
    UUID kdfUUID = mPM.kdfParameters.kdfUUID;
    KdfEngine kdf = KdfFactory.get(kdfUUID);
    kdf.randomize(mPM.kdfParameters);
    if (h.version < PwDbHeaderV4.FILE_VERSION_32_4) {
        h.innerRandomStream = CrsAlgorithm.Salsa20;
        h.innerRandomStreamKey = new byte[32];
    } else {
        h.innerRandomStream = CrsAlgorithm.ChaCha20;
        h.innerRandomStreamKey = new byte[64];
    }
    random.nextBytes(h.innerRandomStreamKey);
    randomStream = PwStreamCipherFactory.getInstance(h.innerRandomStream, h.innerRandomStreamKey);
    if (randomStream == null) {
        throw new PwDbOutputException("Invalid random cipher");
    }
    if (h.version < PwDbHeaderV4.FILE_VERSION_32_4) {
        random.nextBytes(h.streamStartBytes);
    }
    return random;
}
Also used : PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) KdfEngine(com.keepassdroid.crypto.keyDerivation.KdfEngine) PwDbHeaderV4(com.keepassdroid.database.PwDbHeaderV4) SecureRandom(java.security.SecureRandom) UUID(java.util.UUID)

Example 9 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbV3Output method output.

@Override
public void output() throws PwDbOutputException {
    prepForOutput();
    PwDbHeader header = outputHeader(mOS);
    byte[] finalKey = getFinalKey(header);
    Cipher cipher;
    try {
        if (mPM.algorithm == PwEncryptionAlgorithm.Rjindal) {
            cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding");
        } else if (mPM.algorithm == PwEncryptionAlgorithm.Twofish) {
            cipher = CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING");
        } else {
            throw new Exception();
        }
    } catch (Exception e) {
        throw new PwDbOutputException("Algorithm not supported.");
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(finalKey, "AES"), new IvParameterSpec(header.encryptionIV));
        CipherOutputStream cos = new CipherOutputStream(mOS, cipher);
        BufferedOutputStream bos = new BufferedOutputStream(cos);
        outputPlanGroupAndEntries(bos);
        bos.flush();
        bos.close();
    } catch (InvalidKeyException e) {
        throw new PwDbOutputException("Invalid key");
    } catch (InvalidAlgorithmParameterException e) {
        throw new PwDbOutputException("Invalid algorithm parameter.");
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to output final encrypted part.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) PwDbHeader(com.keepassdroid.database.PwDbHeader) BufferedOutputStream(java.io.BufferedOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 10 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbV3Output method getFinalKey.

public byte[] getFinalKey(PwDbHeader header) throws PwDbOutputException {
    try {
        PwDbHeaderV3 h3 = (PwDbHeaderV3) header;
        mPM.makeFinalKey(h3.masterSeed, h3.transformSeed, mPM.numKeyEncRounds);
        return mPM.finalKey;
    } catch (IOException e) {
        throw new PwDbOutputException("Key creation failed: " + e.getMessage());
    }
}
Also used : PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) PwDbHeaderV3(com.keepassdroid.database.PwDbHeaderV3) IOException(java.io.IOException)

Aggregations

PwDbOutputException (com.keepassdroid.database.exception.PwDbOutputException)14 IOException (java.io.IOException)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 BufferedOutputStream (java.io.BufferedOutputStream)4 CipherOutputStream (javax.crypto.CipherOutputStream)4 PwDbHeaderV3 (com.keepassdroid.database.PwDbHeaderV3)3 SecureRandom (java.security.SecureRandom)3 Cipher (javax.crypto.Cipher)3 PwDbHeader (com.keepassdroid.database.PwDbHeader)2 PwDbHeaderV4 (com.keepassdroid.database.PwDbHeaderV4)2 PwEntryV3 (com.keepassdroid.database.PwEntryV3)2 PwGroupV3 (com.keepassdroid.database.PwGroupV3)2 LEDataOutputStream (com.keepassdroid.stream.LEDataOutputStream)2 NullOutputStream (com.keepassdroid.stream.NullOutputStream)2 OutputStream (java.io.OutputStream)2 DigestOutputStream (java.security.DigestOutputStream)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 MessageDigest (java.security.MessageDigest)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2