Search in sources :

Example 1 with HashedBlockInputStream

use of com.keepassdroid.stream.HashedBlockInputStream in project KeePassDX by Kunzisoft.

the class HashedBlock method testSize.

private void testSize(int blockSize, int bufferSize) throws IOException {
    byte[] orig = new byte[blockSize];
    rand.nextBytes(orig);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    HashedBlockOutputStream output = new HashedBlockOutputStream(bos, bufferSize);
    output.write(orig);
    output.close();
    byte[] encoded = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(encoded);
    HashedBlockInputStream input = new HashedBlockInputStream(bis);
    ByteArrayOutputStream decoded = new ByteArrayOutputStream();
    while (true) {
        byte[] buf = new byte[1024];
        int read = input.read(buf);
        if (read == -1) {
            break;
        }
        decoded.write(buf, 0, read);
    }
    byte[] out = decoded.toByteArray();
    assertArrayEquals(orig, out);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashedBlockOutputStream(com.keepassdroid.stream.HashedBlockOutputStream) HashedBlockInputStream(com.keepassdroid.stream.HashedBlockInputStream)

Example 2 with HashedBlockInputStream

use of com.keepassdroid.stream.HashedBlockInputStream in project KeePassDX by Kunzisoft.

the class ImporterV4 method openDatabase.

@Override
public PwDatabaseV4 openDatabase(InputStream inStream, String password, InputStream keyInputStream, UpdateStatus status, long roundsFix) throws IOException, InvalidDBException {
    db = createDB();
    PwDbHeaderV4 header = new PwDbHeaderV4(db);
    db.binPool.clear();
    PwDbHeaderV4.HeaderAndHash hh = header.loadFromFile(inStream);
    version = header.version;
    hashOfHeader = hh.hash;
    pbHeader = hh.header;
    db.setMasterKey(password, keyInputStream);
    db.makeFinalKey(header.masterSeed, db.kdfParameters, roundsFix);
    CipherEngine engine;
    Cipher cipher;
    try {
        engine = CipherFactory.getInstance(db.dataCipher);
        db.dataEngine = engine;
        cipher = engine.getCipher(Cipher.DECRYPT_MODE, db.finalKey, header.encryptionIV);
    } catch (NoSuchAlgorithmException e) {
        throw new IOException("Invalid algorithm.");
    } catch (NoSuchPaddingException e) {
        throw new IOException("Invalid algorithm.");
    } catch (InvalidKeyException e) {
        throw new IOException("Invalid algorithm.");
    } catch (InvalidAlgorithmParameterException e) {
        throw new IOException("Invalid algorithm.");
    }
    InputStream isPlain;
    if (version < PwDbHeaderV4.FILE_VERSION_32_4) {
        InputStream decrypted = AttachCipherStream(inStream, cipher);
        LEDataInputStream dataDecrypted = new LEDataInputStream(decrypted);
        byte[] storedStartBytes = null;
        try {
            storedStartBytes = dataDecrypted.readBytes(32);
            if (storedStartBytes == null || storedStartBytes.length != 32) {
                throw new InvalidPasswordException();
            }
        } catch (IOException e) {
            throw new InvalidPasswordException();
        }
        if (!Arrays.equals(storedStartBytes, header.streamStartBytes)) {
            throw new InvalidPasswordException();
        }
        isPlain = new HashedBlockInputStream(dataDecrypted);
    } else {
        // KDBX 4
        LEDataInputStream isData = new LEDataInputStream(inStream);
        byte[] storedHash = isData.readBytes(32);
        if (!Arrays.equals(storedHash, hashOfHeader)) {
            throw new InvalidDBException();
        }
        byte[] hmacKey = db.hmacKey;
        byte[] headerHmac = PwDbHeaderV4.computeHeaderHmac(pbHeader, hmacKey);
        byte[] storedHmac = isData.readBytes(32);
        if (storedHmac == null || storedHmac.length != 32) {
            throw new InvalidDBException();
        }
        // Mac doesn't match
        if (!Arrays.equals(headerHmac, storedHmac)) {
            throw new InvalidDBException();
        }
        HmacBlockInputStream hmIs = new HmacBlockInputStream(isData, true, hmacKey);
        isPlain = AttachCipherStream(hmIs, cipher);
    }
    InputStream isXml;
    if (db.compressionAlgorithm == PwCompressionAlgorithm.Gzip) {
        isXml = new GZIPInputStream(isPlain);
    } else {
        isXml = isPlain;
    }
    if (version >= header.FILE_VERSION_32_4) {
        LoadInnerHeader(isXml, header);
    }
    if (header.innerRandomStreamKey == null) {
        assert (false);
        throw new IOException("Invalid stream key.");
    }
    randomStream = PwStreamCipherFactory.getInstance(header.innerRandomStream, header.innerRandomStreamKey);
    if (randomStream == null) {
        throw new ArcFourException();
    }
    ReadXmlStreamed(isXml);
    return db;
}
Also used : InvalidDBException(com.keepassdroid.database.exception.InvalidDBException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) GZIPInputStream(java.util.zip.GZIPInputStream) HashedBlockInputStream(com.keepassdroid.stream.HashedBlockInputStream) LEDataInputStream(com.keepassdroid.stream.LEDataInputStream) BetterCipherInputStream(com.keepassdroid.stream.BetterCipherInputStream) HmacBlockInputStream(com.keepassdroid.stream.HmacBlockInputStream) InputStream(java.io.InputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) LEDataInputStream(com.keepassdroid.stream.LEDataInputStream) ArcFourException(com.keepassdroid.database.exception.ArcFourException) GZIPInputStream(java.util.zip.GZIPInputStream) HmacBlockInputStream(com.keepassdroid.stream.HmacBlockInputStream) PwDbHeaderV4(com.keepassdroid.database.PwDbHeaderV4) CipherEngine(com.keepassdroid.crypto.engine.CipherEngine) InvalidPasswordException(com.keepassdroid.database.exception.InvalidPasswordException) StreamCipher(org.spongycastle.crypto.StreamCipher) Cipher(javax.crypto.Cipher) HashedBlockInputStream(com.keepassdroid.stream.HashedBlockInputStream)

Example 3 with HashedBlockInputStream

use of com.keepassdroid.stream.HashedBlockInputStream in project KeePassDX by Kunzisoft.

the class HashedBlock method testGZIPStream.

public void testGZIPStream() throws IOException {
    final int testLength = 32000;
    byte[] orig = new byte[testLength];
    rand.nextBytes(orig);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    HashedBlockOutputStream hos = new HashedBlockOutputStream(bos);
    GZIPOutputStream zos = new GZIPOutputStream(hos);
    zos.write(orig);
    zos.close();
    byte[] compressed = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
    HashedBlockInputStream his = new HashedBlockInputStream(bis);
    GZIPInputStream zis = new GZIPInputStream(his);
    byte[] uncompressed = new byte[testLength];
    int read = 0;
    while (read != -1 && testLength - read > 0) {
        read += zis.read(uncompressed, read, testLength - read);
    }
    assertArrayEquals("Output not equal to input", orig, uncompressed);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashedBlockOutputStream(com.keepassdroid.stream.HashedBlockOutputStream) HashedBlockInputStream(com.keepassdroid.stream.HashedBlockInputStream)

Aggregations

HashedBlockInputStream (com.keepassdroid.stream.HashedBlockInputStream)3 HashedBlockOutputStream (com.keepassdroid.stream.HashedBlockOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 CipherEngine (com.keepassdroid.crypto.engine.CipherEngine)1 PwDbHeaderV4 (com.keepassdroid.database.PwDbHeaderV4)1 ArcFourException (com.keepassdroid.database.exception.ArcFourException)1 InvalidDBException (com.keepassdroid.database.exception.InvalidDBException)1 InvalidPasswordException (com.keepassdroid.database.exception.InvalidPasswordException)1 BetterCipherInputStream (com.keepassdroid.stream.BetterCipherInputStream)1 HmacBlockInputStream (com.keepassdroid.stream.HmacBlockInputStream)1 LEDataInputStream (com.keepassdroid.stream.LEDataInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Cipher (javax.crypto.Cipher)1