Search in sources :

Example 31 with ShortBufferException

use of javax.crypto.ShortBufferException in project cassandra by apache.

the class EncryptionUtils method decrypt.

/**
     * Decrypt the input data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough,
     * deallocate current, and allocate a large enough buffer.
     *
     * @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity,
     * or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times).
     */
public static ByteBuffer decrypt(ReadableByteChannel channel, ByteBuffer outputBuffer, boolean allowBufferResize, Cipher cipher) throws IOException {
    ByteBuffer metadataBuffer = reusableBuffers.get();
    if (metadataBuffer.capacity() < ENCRYPTED_BLOCK_HEADER_SIZE) {
        metadataBuffer = ByteBufferUtil.ensureCapacity(metadataBuffer, ENCRYPTED_BLOCK_HEADER_SIZE, true);
        reusableBuffers.set(metadataBuffer);
    }
    metadataBuffer.position(0).limit(ENCRYPTED_BLOCK_HEADER_SIZE);
    channel.read(metadataBuffer);
    if (metadataBuffer.remaining() < ENCRYPTED_BLOCK_HEADER_SIZE)
        throw new IllegalStateException("could not read encrypted blocked metadata header");
    int encryptedLength = metadataBuffer.getInt();
    // this is the length of the compressed data
    int plainTextLength = metadataBuffer.getInt();
    outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, Math.max(plainTextLength, encryptedLength), allowBufferResize);
    outputBuffer.position(0).limit(encryptedLength);
    channel.read(outputBuffer);
    ByteBuffer dupe = outputBuffer.duplicate();
    dupe.clear();
    try {
        cipher.doFinal(outputBuffer, dupe);
    } catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        throw new IOException("failed to decrypt commit log block", e);
    }
    dupe.position(0).limit(plainTextLength);
    return dupe;
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 32 with ShortBufferException

use of javax.crypto.ShortBufferException in project robovm by robovm.

the class CipherTest method testAES_ECB_PKCS5Padding_ShortBuffer_Failure.

private void testAES_ECB_PKCS5Padding_ShortBuffer_Failure(String provider) throws Exception {
    SecretKey key = new SecretKeySpec(AES_128_KEY, "AES");
    Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding", provider);
    c.init(Cipher.ENCRYPT_MODE, key);
    final byte[] fragmentOutput = c.update(AES_128_ECB_PKCS5Padding_TestVector_1_Plaintext);
    if (fragmentOutput != null) {
        assertEquals(0, fragmentOutput.length);
    }
    // Provide null buffer.
    {
        try {
            c.doFinal(null, 0);
            fail("Should throw NullPointerException on null output buffer");
        } catch (NullPointerException expected) {
        } catch (IllegalArgumentException expected) {
        }
    }
    // Provide short buffer.
    {
        final byte[] output = new byte[c.getBlockSize() - 1];
        try {
            c.doFinal(output, 0);
            fail("Should throw ShortBufferException on short output buffer");
        } catch (ShortBufferException expected) {
        }
    }
    // Start 1 byte into output buffer.
    {
        final byte[] output = new byte[c.getBlockSize()];
        try {
            c.doFinal(output, 1);
            fail("Should throw ShortBufferException on short output buffer");
        } catch (ShortBufferException expected) {
        }
    }
    // Should keep data for real output buffer
    {
        final byte[] output = new byte[c.getBlockSize()];
        assertEquals(AES_128_ECB_PKCS5Padding_TestVector_1_Encrypted.length, c.doFinal(output, 0));
        assertTrue(Arrays.equals(AES_128_ECB_PKCS5Padding_TestVector_1_Encrypted, output));
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher)

Example 33 with ShortBufferException

use of javax.crypto.ShortBufferException in project robovm by robovm.

the class ShortBufferExceptionTest method testShortBufferException01.

/**
     * Test for <code>ShortBufferException()</code> constructor Assertion:
     * constructs ShortBufferException with no detail message
     */
public void testShortBufferException01() {
    ShortBufferException tE = new ShortBufferException();
    assertNull("getMessage() must return null.", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : ShortBufferException(javax.crypto.ShortBufferException)

Example 34 with ShortBufferException

use of javax.crypto.ShortBufferException in project robovm by robovm.

the class ExemptionMechanismTest method test_genExemptionBlob$BI.

public void test_genExemptionBlob$BI() throws InvalidKeyException, ExemptionMechanismException, ShortBufferException {
    Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
    ExemptionMechanism em = new ExemptionMechanism(new Mock_ExemptionMechanismSpi(), mProv, defaultAlg) {
    };
    Key key = new Mock_ExemptionMechanismSpi().new tmpKey("Proba", new byte[0]);
    try {
        em.genExemptionBlob(new byte[10], 2);
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    //failed
    }
    em.init(key);
    assertEquals(5, (em.genExemptionBlob(new byte[10], 5)));
    try {
        em.genExemptionBlob(new byte[7], 3);
        fail("ShortBufferException expected");
    } catch (ShortBufferException e) {
    //failed
    }
    byte[] b = new byte[] { 0, 0, 0, 1, 2, 3, 33, 0 };
    try {
        em.genExemptionBlob(b, 3);
        fail("ExemptionMechanismException expected");
    } catch (ExemptionMechanismException e) {
    //failed
    }
}
Also used : SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) ShortBufferException(javax.crypto.ShortBufferException) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) ExemptionMechanism(javax.crypto.ExemptionMechanism) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Key(java.security.Key) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Provider(java.security.Provider)

Example 35 with ShortBufferException

use of javax.crypto.ShortBufferException in project robovm by robovm.

the class ExemptionMechanismTest method test_genExemptionBlob$B.

public void test_genExemptionBlob$B() throws InvalidKeyException, ExemptionMechanismException, ShortBufferException {
    Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
    ExemptionMechanism em = new ExemptionMechanism(new Mock_ExemptionMechanismSpi(), mProv, defaultAlg) {
    };
    Key key = new Mock_ExemptionMechanismSpi().new tmpKey("Proba", new byte[0]);
    try {
        em.genExemptionBlob(new byte[10]);
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    //failed
    }
    em.init(key);
    assertEquals(5, (em.genExemptionBlob(new byte[10])));
    try {
        em.genExemptionBlob(new byte[2]);
        fail("ShortBufferException expected");
    } catch (ShortBufferException e) {
    //failed
    }
    byte[] b = new byte[] { 0, 0, 0, 33, 0 };
    try {
        em.genExemptionBlob(b);
        fail("ExemptionMechanismException expected");
    } catch (ExemptionMechanismException e) {
    //failed
    }
}
Also used : SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) ShortBufferException(javax.crypto.ShortBufferException) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) ExemptionMechanism(javax.crypto.ExemptionMechanism) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Key(java.security.Key) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Provider(java.security.Provider)

Aggregations

ShortBufferException (javax.crypto.ShortBufferException)48 Cipher (javax.crypto.Cipher)15 BadPaddingException (javax.crypto.BadPaddingException)14 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)14 BufferOverflowException (java.nio.BufferOverflowException)10 ByteBuffer (java.nio.ByteBuffer)10 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)9 IvParameterSpec (javax.crypto.spec.IvParameterSpec)9 InvalidKeyException (java.security.InvalidKeyException)7 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)6 SecretKey (javax.crypto.SecretKey)6 NoSuchProviderException (java.security.NoSuchProviderException)5 Random (java.util.Random)5 KeyGenerator (javax.crypto.KeyGenerator)5 SecretKeySpec (javax.crypto.spec.SecretKeySpec)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 IOException (java.io.IOException)3 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)2