Search in sources :

Example 1 with EncryptedDataContainer

use of com.dracoon.sdk.crypto.model.EncryptedDataContainer in project cyberduck by iterate-ch.

the class TripleCryptDecryptingInputStream method readNextChunk.

private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(SDSSession.DEFAULT_CHUNKSIZE);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if (lastread == 0) {
        return IOUtils.EOF;
    }
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        final PlainDataContainer pDataContainer;
        if (read == 0) {
            final PlainDataContainer c1 = cipher.processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
            final PlainDataContainer c2 = cipher.doFinal(new EncryptedDataContainer(null, tag));
            pDataContainer = new PlainDataContainer(ArrayUtils.addAll(c1.getContent(), c2.getContent()));
        } else {
            pDataContainer = cipher.processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
        }
        final byte[] content = pDataContainer.getContent();
        buffer = ByteBuffer.allocate(content.length);
        buffer.put(content);
        buffer.flip();
        lastread = read;
        return content.length;
    } catch (CryptoException e) {
        throw new IOException(e);
    }
}
Also used : EncryptedDataContainer(com.dracoon.sdk.crypto.model.EncryptedDataContainer) PlainDataContainer(com.dracoon.sdk.crypto.model.PlainDataContainer) IOException(java.io.IOException) CryptoException(com.dracoon.sdk.crypto.error.CryptoException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with EncryptedDataContainer

use of com.dracoon.sdk.crypto.model.EncryptedDataContainer in project cyberduck by iterate-ch.

the class TripleCryptEncryptingInputStream method fillBuffer.

private void fillBuffer(final int len) throws IOException, CryptoException {
    // Read ahead to the next chunk end
    final int allocate = (len / IOUtils.DEFAULT_BUFFER_SIZE) * IOUtils.DEFAULT_BUFFER_SIZE + IOUtils.DEFAULT_BUFFER_SIZE;
    buffer = ByteBuffer.allocate(allocate);
    final byte[] plain = new byte[allocate];
    final int read = IOUtils.read(proxy, plain, 0, allocate);
    if (read > 0) {
        int position = 0;
        for (int chunkOffset = 0; chunkOffset < read; chunkOffset += SDSSession.DEFAULT_CHUNKSIZE) {
            int chunkLen = Math.min(SDSSession.DEFAULT_CHUNKSIZE, read - chunkOffset);
            final byte[] bytes = Arrays.copyOfRange(plain, chunkOffset, chunkOffset + chunkLen);
            final PlainDataContainer data = TripleCryptKeyPair.createPlainDataContainer(bytes, bytes.length);
            final EncryptedDataContainer encrypted = cipher.processBytes(data);
            final byte[] encBuf = encrypted.getContent();
            System.arraycopy(encBuf, 0, buffer.array(), position, encBuf.length);
            position += encBuf.length;
        }
        buffer.limit(position);
    }
    if (read < allocate) {
        // EOF in proxy stream, finalize cipher and put remaining bytes into buffer
        eof = true;
        final EncryptedDataContainer encContainer = cipher.doFinal();
        final byte[] content = encContainer.getContent();
        buffer = this.combine(buffer, ByteBuffer.wrap(content));
        final String tag = CryptoUtils.byteArrayToString(encContainer.getTag());
        final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
        final FileKey fileKey = reader.readValue(status.getFilekey().array());
        if (null == fileKey.getTag()) {
            // Only override if not already set pre-computed in bulk feature
            fileKey.setTag(tag);
            final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            writer.writeValue(out, fileKey);
            status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
        } else {
            log.warn(String.format("Skip setting tag in file key already found in %s", status));
        }
    }
}
Also used : FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) EncryptedDataContainer(com.dracoon.sdk.crypto.model.EncryptedDataContainer) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) PlainDataContainer(com.dracoon.sdk.crypto.model.PlainDataContainer) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

EncryptedDataContainer (com.dracoon.sdk.crypto.model.EncryptedDataContainer)2 PlainDataContainer (com.dracoon.sdk.crypto.model.PlainDataContainer)2 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)1 CryptoException (com.dracoon.sdk.crypto.error.CryptoException)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1