use of javax.crypto.ShortBufferException in project android_frameworks_base by crdroidandroid.
the class AndroidKeyStoreCipherSpiBase method engineUpdate.
@Override
protected final int engineUpdate(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
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 = engineUpdate(input.array(), input.arrayOffset() + input.position(), inputSize);
input.position(input.position() + inputSize);
} else {
byte[] inputArray = new byte[inputSize];
input.get(inputArray);
outputArray = engineUpdate(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 android_frameworks_base by crdroidandroid.
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 hbase by apache.
the class CryptoAES method wrap.
/**
* Encrypts input data. The result composes of (msg, padding if needed, mac) and sequence num.
* @param data the input byte array
* @param offset the offset in input where the input starts
* @param len the input length
* @return the new encrypted byte array.
* @throws SaslException if error happens
*/
public byte[] wrap(byte[] data, int offset, int len) throws SaslException {
// mac
byte[] mac = integrity.getHMAC(data, offset, len);
integrity.incMySeqNum();
// encrypt
byte[] encrypted = new byte[len + 10];
try {
int n = encryptor.update(data, offset, len, encrypted, 0);
encryptor.update(mac, 0, 10, encrypted, n);
} catch (ShortBufferException sbe) {
// this should not happen
throw new SaslException("Error happens during encrypt data", sbe);
}
// append seqNum used for mac
byte[] wrapped = new byte[encrypted.length + 4];
System.arraycopy(encrypted, 0, wrapped, 0, encrypted.length);
System.arraycopy(integrity.getSeqNum(), 0, wrapped, encrypted.length, 4);
return wrapped;
}
use of javax.crypto.ShortBufferException in project hbase by apache.
the class CryptoAES method unwrap.
/**
* Decrypts input data. The input composes of (msg, padding if needed, mac) and sequence num.
* The result is msg.
* @param data the input byte array
* @param offset the offset in input where the input starts
* @param len the input length
* @return the new decrypted byte array.
* @throws SaslException if error happens
*/
public byte[] unwrap(byte[] data, int offset, int len) throws SaslException {
// get plaintext and seqNum
byte[] decrypted = new byte[len - 4];
byte[] peerSeqNum = new byte[4];
try {
decryptor.update(data, offset, len - 4, decrypted, 0);
} catch (ShortBufferException sbe) {
// this should not happen
throw new SaslException("Error happens during decrypt data", sbe);
}
System.arraycopy(data, offset + decrypted.length, peerSeqNum, 0, 4);
// get msg and mac
byte[] msg = new byte[decrypted.length - 10];
byte[] mac = new byte[10];
System.arraycopy(decrypted, 0, msg, 0, msg.length);
System.arraycopy(decrypted, msg.length, mac, 0, 10);
// check mac integrity and msg sequence
if (!integrity.compareHMAC(mac, peerSeqNum, msg, 0, msg.length)) {
throw new SaslException("Unmatched MAC");
}
if (!integrity.comparePeerSeqNum(peerSeqNum)) {
throw new SaslException("Out of order sequencing of messages. Got: " + integrity.byteToInt(peerSeqNum) + " Expected: " + integrity.peerSeqNum);
}
integrity.incPeerSeqNum();
return msg;
}
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;
}
Aggregations