use of javax.crypto.ShortBufferException in project robovm by robovm.
the class myMac method testMac09.
/**
* Test for <code>update</code> and <code>doFinal</code> methods
* Assertion: throws IllegalStateException when Mac is not initialized
* @throws Exception
*/
public void testMac09() throws Exception {
if (!DEFSupported) {
fail(NotSupportedMsg);
return;
}
Mac[] macs = createMacs();
assertNotNull("Mac objects were not created", macs);
byte[] buf = new byte[10];
ByteBuffer bBuf = ByteBuffer.wrap(buf, 0, 10);
byte[] bb = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
SecretKeySpec sks = new SecretKeySpec(bb, "SHA1");
for (int i = 0; i < macs.length; i++) {
try {
macs[i].update((byte) 0);
fail("IllegalStateException must be thrown");
} catch (IllegalStateException e) {
}
try {
macs[i].update(buf);
fail("IllegalStateException must be thrown");
} catch (IllegalStateException e) {
}
try {
macs[i].update(buf, 0, 3);
fail("IllegalStateException must be thrown");
} catch (IllegalStateException e) {
}
try {
macs[i].update(bBuf);
fail("IllegalStateException must be thrown");
} catch (IllegalStateException e) {
}
try {
macs[i].doFinal();
fail("IllegalStateException must be thrown");
} catch (IllegalStateException e) {
}
try {
macs[i].doFinal(new byte[10]);
fail("IllegalStateException must be thrown");
} catch (IllegalStateException e) {
}
try {
macs[i].doFinal(new byte[10], 0);
fail("IllegalStateException must be thrown");
} catch (IllegalStateException e) {
}
macs[i].init(sks);
try {
macs[i].doFinal(new byte[1], 0);
fail("ShortBufferException expected");
} catch (ShortBufferException e) {
//expected
}
}
}
use of javax.crypto.ShortBufferException in project robovm by robovm.
the class ECDHKeyAgreementTest method testGenerateSecret_withSmallerThanNeededBuffer.
void testGenerateSecret_withSmallerThanNeededBuffer(Provider provider) throws Exception {
KeyAgreement keyAgreement = getKeyAgreement(provider);
keyAgreement.init(KAT_PRIVATE_KEY1);
keyAgreement.doPhase(KAT_PUBLIC_KEY2, true);
try {
// Although the buffer is big enough (1024 bytes) the shared secret should be placed
// at offset 1020 thus leaving only 4 bytes for the secret, which is not enough.
keyAgreement.generateSecret(new byte[1024], 1020);
fail();
} catch (ShortBufferException expected) {
}
}
use of javax.crypto.ShortBufferException in project platform_frameworks_base by android.
the class AndroidKeyStoreCipherSpiBase method engineDoFinal.
@Override
protected final int engineDoFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
if (input == null) {
throw new NullPointerException("input == null");
}
if (output == null) {
throw new NullPointerException("output == null");
}
int inputSize = input.remaining();
byte[] outputArray;
if (input.hasArray()) {
outputArray = engineDoFinal(input.array(), input.arrayOffset() + input.position(), inputSize);
input.position(input.position() + inputSize);
} else {
byte[] inputArray = new byte[inputSize];
input.get(inputArray);
outputArray = engineDoFinal(inputArray, 0, inputSize);
}
int outputSize = (outputArray != null) ? outputArray.length : 0;
if (outputSize > 0) {
int outputBufferAvailable = output.remaining();
try {
output.put(outputArray);
} catch (BufferOverflowException e) {
throw new ShortBufferException("Output buffer too small. Produced: " + outputSize + ", available: " + outputBufferAvailable);
}
}
return outputSize;
}
use of javax.crypto.ShortBufferException in project cassandra by apache.
the class EncryptionUtils method encryptAndWrite.
/**
* Encrypt the input data, and writes out to the same input buffer; if the buffer is not big enough,
* deallocate current, and allocate a large enough buffer.
* Writes the cipher text and headers out to the channel, as well.
*
* Note: channel is a parameter as we cannot write header info to the output buffer as we assume the input and output
* buffers can be the same buffer (and writing the headers to a shared buffer will corrupt any input data). Hence,
* we write out the headers directly to the channel, and then the cipher text (once encrypted).
*/
public static ByteBuffer encryptAndWrite(ByteBuffer inputBuffer, WritableByteChannel channel, boolean allowBufferResize, Cipher cipher) throws IOException {
final int plainTextLength = inputBuffer.remaining();
final int encryptLength = cipher.getOutputSize(plainTextLength);
ByteBuffer outputBuffer = inputBuffer.duplicate();
outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, encryptLength, allowBufferResize);
// it's unfortunate that we need to allocate a small buffer here just for the headers, but if we reuse the input buffer
// for the output, then we would overwrite the first n bytes of the real data with the header data.
ByteBuffer intBuf = ByteBuffer.allocate(ENCRYPTED_BLOCK_HEADER_SIZE);
intBuf.putInt(0, encryptLength);
intBuf.putInt(4, plainTextLength);
channel.write(intBuf);
try {
cipher.doFinal(inputBuffer, outputBuffer);
} catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
throw new IOException("failed to encrypt commit log block", e);
}
outputBuffer.position(0).limit(encryptLength);
channel.write(outputBuffer);
outputBuffer.position(0).limit(encryptLength);
return outputBuffer;
}
use of javax.crypto.ShortBufferException in project wycheproof by google.
the class AesGcmTest method testByteBufferTooShort.
public void testByteBufferTooShort() throws Exception {
for (GcmTestVector test : getTestVectors()) {
// Encryption
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
ByteBuffer ptBuffer = ByteBuffer.wrap(test.pt);
ByteBuffer ctBuffer = ByteBuffer.allocate(test.ct.length - 1);
cipher.init(Cipher.ENCRYPT_MODE, test.key, test.parameters);
cipher.updateAAD(test.aad);
try {
cipher.doFinal(ptBuffer, ctBuffer);
fail("This should not work");
} catch (ShortBufferException ex) {
// expected
}
// Decryption
ctBuffer = ByteBuffer.wrap(test.ct);
ByteBuffer decrypted = ByteBuffer.allocate(test.pt.length - 1);
cipher.init(Cipher.DECRYPT_MODE, test.key, test.parameters);
cipher.updateAAD(test.aad);
try {
cipher.doFinal(ctBuffer, decrypted);
fail("This should not work");
} catch (ShortBufferException ex) {
// expected
}
}
}
Aggregations