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;
}
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));
}
}
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());
}
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
}
}
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
}
}
Aggregations