Search in sources :

Example 66 with ShortBufferException

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

the class myCipherSpi method testCipherSpi06.

/**
 * Test for <code>engineDoFinal(ByteBuffer, ByteBuffer)</code> method
 * Assertions:
 * throws NullPointerException if one of these buffers is null;
 * throws ShortBufferException is there is no space in output to hold result
 */
public void testCipherSpi06() throws BadPaddingException, ShortBufferException, IllegalBlockSizeException {
    Mock_CipherSpi cSpi = new Mock_CipherSpi();
    int len = 10;
    byte[] bbuf = new byte[len];
    for (int i = 0; i < bbuf.length; i++) {
        bbuf[i] = (byte) i;
    }
    ByteBuffer bb1 = ByteBuffer.wrap(bbuf);
    ByteBuffer bbNull = null;
    try {
        cSpi.engineDoFinal(bbNull, bb1);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    try {
        cSpi.engineDoFinal(bb1, bbNull);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    ByteBuffer bb2 = ByteBuffer.allocate(len);
    bb1.position(bb1.limit());
    assertEquals("Incorrect result", 0, cSpi.engineDoFinal(bb1, bb2));
    bb1.position(0);
    bb2.position(len - 2);
    try {
        cSpi.engineDoFinal(bb1, bb2);
        fail("ShortBufferException must be thrown. Output buffer remaining: ".concat(Integer.toString(bb2.remaining())));
    } catch (ShortBufferException e) {
    }
    int pos = 5;
    bb1.position(pos);
    bb2.position(0);
    assertTrue("Incorrect result", cSpi.engineDoFinal(bb1, bb2) > 0);
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) DirectByteBuffer(java.nio.DirectByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 67 with ShortBufferException

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

the class CipherAdapter method engineUpdate.

@Override
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException {
    int result = engineGetOutputSize(inputLen);
    if ((output.length - outputOffset) < result)
        throw new ShortBufferException();
    byte[] buf = engineUpdate(input, inputOffset, inputLen);
    result = buf.length;
    if ((output.length - outputOffset) < result)
        throw new ShortBufferException();
    System.arraycopy(buf, 0, output, outputOffset, result);
    return result;
}
Also used : ShortBufferException(javax.crypto.ShortBufferException)

Example 68 with ShortBufferException

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

the class CipherAdapter method engineDoFinal.

@Override
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    int result = engineGetOutputSize(inputLen);
    if ((output.length - outputOffset) < result)
        throw new ShortBufferException();
    byte[] buf = engineDoFinal(input, inputOffset, inputLen);
    result = buf.length;
    if ((output.length - outputOffset) < result)
        throw new ShortBufferException();
    System.arraycopy(buf, 0, output, outputOffset, result);
    return result;
}
Also used : ShortBufferException(javax.crypto.ShortBufferException)

Example 69 with ShortBufferException

use of javax.crypto.ShortBufferException in project elastic-core-maven by OrdinaryDude.

the class Scrypt method hash.

public byte[] hash(final byte[] input) {
    int i, j, k;
    System.arraycopy(input, 0, B, 0, input.length);
    try {
        mac.init(new SecretKeySpec(B, 0, 40, "HmacSHA256"));
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e);
    }
    B[40] = 0;
    B[41] = 0;
    B[42] = 0;
    for (i = 0; i < 4; i++) {
        B[43] = (byte) (i + 1);
        mac.update(B, 0, 44);
        try {
            mac.doFinal(H, 0);
        } catch (ShortBufferException e) {
            throw new IllegalStateException(e);
        }
        for (j = 0; j < 8; j++) {
            X[i * 8 + j] = (H[j * 4 + 0] & 0xff) << 0 | (H[j * 4 + 1] & 0xff) << 8 | (H[j * 4 + 2] & 0xff) << 16 | (H[j * 4 + 3] & 0xff) << 24;
        }
    }
    for (i = 0; i < 1024; i++) {
        System.arraycopy(X, 0, V, i * 32, 32);
        xorSalsa8(0, 16);
        xorSalsa8(16, 0);
    }
    for (i = 0; i < 1024; i++) {
        k = (X[16] & 1023) * 32;
        for (j = 0; j < 32; j++) X[j] ^= V[k + j];
        xorSalsa8(0, 16);
        xorSalsa8(16, 0);
    }
    for (i = 0; i < 32; i++) {
        B[i * 4 + 0] = (byte) (X[i] >> 0);
        B[i * 4 + 1] = (byte) (X[i] >> 8);
        B[i * 4 + 2] = (byte) (X[i] >> 16);
        B[i * 4 + 3] = (byte) (X[i] >> 24);
    }
    B[128 + 3] = 1;
    mac.update(B, 0, 128 + 4);
    try {
        mac.doFinal(H, 0);
    } catch (ShortBufferException e) {
        throw new IllegalStateException(e);
    }
    return H;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) ShortBufferException(javax.crypto.ShortBufferException) InvalidKeyException(java.security.InvalidKeyException)

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