Search in sources :

Example 61 with ShortBufferException

use of javax.crypto.ShortBufferException in project gerrit by GerritCodeReview.

the class SignedToken method computeToken.

private void computeToken(final byte[] buf, final String text) throws XsrfException {
    final Mac m = newMac();
    m.update(buf, 0, 2 * INT_SZ);
    m.update(toBytes(text));
    try {
        m.doFinal(buf, 2 * INT_SZ);
    } catch (ShortBufferException e) {
        throw new XsrfException("Unexpected token overflow", e);
    }
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) Mac(javax.crypto.Mac)

Example 62 with ShortBufferException

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;
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) SaslException(javax.security.sasl.SaslException)

Example 63 with ShortBufferException

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;
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) SaslException(javax.security.sasl.SaslException)

Example 64 with ShortBufferException

use of javax.crypto.ShortBufferException in project j2objc by google.

the class ShortBufferExceptionTest method testShortBufferException02.

/**
 * Test for <code>ShortBufferException(String)</code> constructor
 * Assertion: constructs ShortBufferException with detail message msg.
 * Parameter <code>msg</code> is not null.
 */
public void testShortBufferException02() {
    ShortBufferException tE;
    for (int i = 0; i < msgs.length; i++) {
        tE = new ShortBufferException(msgs[i]);
        assertEquals("getMessage() must return: ".concat(msgs[i]), tE.getMessage(), msgs[i]);
        assertNull("getCause() must return null", tE.getCause());
    }
}
Also used : ShortBufferException(javax.crypto.ShortBufferException)

Example 65 with ShortBufferException

use of javax.crypto.ShortBufferException in project j2objc by google.

the class myCipherSpi method testCrypt_outputSizeUpdatedAfterShortBufferException.

// In case a call to engineGetOutputSize returns 0 for the whole input size, but a positive
// value for the chunk size to be written, check that the positive output size is used in the
// second attempt to read from the the buffer.
public void testCrypt_outputSizeUpdatedAfterShortBufferException() throws Exception {
    // 4096 is the value hardcoded for a maximum array allocation in CipherSpi#getTempArraySize
    final int maxInternalArrayAllocation = 4096;
    // The length of the input is greater than the max chunk allowed, so the size of the chunk
    // and the size of the input will differ.
    final int testInputLength = maxInternalArrayAllocation + 1;
    // Length to be returned the second time engineGetOutputSize is called (that is, when it's
    // called with maxInternalArrayAllocation). First length returned (that is, when it's
    // called with testInputLength) is 0.
    final int testSecondOutputLength = 1000;
    final AtomicInteger firstGetLength = new AtomicInteger(0);
    final AtomicInteger secondGetLength = new AtomicInteger(0);
    ByteBuffer inputBuffer = new MockNonArrayBackedByteBuffer(testInputLength, false) {

        private boolean getWasCalled = false;

        @Override
        public ByteBuffer get(byte[] dst, int offset, int length) {
            if (!getWasCalled) {
                getWasCalled = true;
                firstGetLength.set(length);
            } else {
                if (secondGetLength.get() == 0) {
                    secondGetLength.set(length);
                }
            }
            return this;
        }
    };
    ByteBuffer outputBuffer = new MockNonArrayBackedByteBuffer(10, false);
    Mock_CipherSpi cipherSpi = new Mock_CipherSpi() {

        @Override
        public int engineGetOutputSize(int inputLength) {
            if (inputLength == testInputLength) {
                return 0;
            } else if (inputLength == maxInternalArrayAllocation) {
                return testSecondOutputLength;
            } else {
                throw new IllegalStateException("Unexpected value " + inputLength);
            }
        }

        @Override
        public int engineUpdate(byte[] inArray, int inOfs, int inLen, byte[] outArray, int outputOffset) throws ShortBufferException {
            if (inLen == maxInternalArrayAllocation) {
                throw new ShortBufferException("to be caught in order to retry with a new" + "output size");
            }
            return 0;
        }
    };
    cipherSpi.engineUpdate(inputBuffer, outputBuffer);
    assertEquals("first call to get must use the input length, as the output length " + "from engineGetOutputSize is 0", maxInternalArrayAllocation, firstGetLength.get());
    assertEquals("second call to get must use the new output length", testSecondOutputLength, secondGetLength.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ShortBufferException(javax.crypto.ShortBufferException) DirectByteBuffer(java.nio.DirectByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ShortBufferException (javax.crypto.ShortBufferException)69 BadPaddingException (javax.crypto.BadPaddingException)24 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 Cipher (javax.crypto.Cipher)22 InvalidKeyException (java.security.InvalidKeyException)17 ByteBuffer (java.nio.ByteBuffer)15 IvParameterSpec (javax.crypto.spec.IvParameterSpec)15 SecretKeySpec (javax.crypto.spec.SecretKeySpec)13 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)12 BufferOverflowException (java.nio.BufferOverflowException)10 IOException (java.io.IOException)9 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)9 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)7 SecretKey (javax.crypto.SecretKey)6 NoSuchProviderException (java.security.NoSuchProviderException)5 Random (java.util.Random)5 KeyGenerator (javax.crypto.KeyGenerator)5 MessageDigest (java.security.MessageDigest)4