Search in sources :

Example 21 with ShortBufferException

use of javax.crypto.ShortBufferException in project poi by apache.

the class CryptoAPIDocumentInputStream method read.

@Override
public synchronized int read() {
    int ch = super.read();
    if (ch == -1) {
        return -1;
    }
    oneByte[0] = (byte) ch;
    try {
        cipher.update(oneByte, 0, 1, oneByte);
    } catch (ShortBufferException e) {
        throw new EncryptedDocumentException(e);
    }
    return oneByte[0];
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) ShortBufferException(javax.crypto.ShortBufferException)

Example 22 with ShortBufferException

use of javax.crypto.ShortBufferException in project jdk8u_jdk by JetBrains.

the class DHKeyAgreement method engineGenerateSecret.

/**
     * Generates the shared secret, and places it into the buffer
     * <code>sharedSecret</code>, beginning at <code>offset</code>.
     *
     * <p>If the <code>sharedSecret</code> buffer is too small to hold the
     * result, a <code>ShortBufferException</code> is thrown.
     * In this case, this call should be repeated with a larger output buffer.
     *
     * <p>This method resets this <code>KeyAgreementSpi</code> object,
     * so that it
     * can be reused for further key agreements. Unless this key agreement is
     * reinitialized with one of the <code>engineInit</code> methods, the same
     * private information and algorithm parameters will be used for
     * subsequent key agreements.
     *
     * @param sharedSecret the buffer for the shared secret
     * @param offset the offset in <code>sharedSecret</code> where the
     * shared secret will be stored
     *
     * @return the number of bytes placed into <code>sharedSecret</code>
     *
     * @exception IllegalStateException if this key agreement has not been
     * completed yet
     * @exception ShortBufferException if the given output buffer is too small
     * to hold the secret
     */
protected int engineGenerateSecret(byte[] sharedSecret, int offset) throws IllegalStateException, ShortBufferException {
    if (generateSecret == false) {
        throw new IllegalStateException("Key agreement has not been completed yet");
    }
    if (sharedSecret == null) {
        throw new ShortBufferException("No buffer provided for shared secret");
    }
    BigInteger modulus = init_p;
    int expectedLen = (modulus.bitLength() + 7) >>> 3;
    if ((sharedSecret.length - offset) < expectedLen) {
        throw new ShortBufferException("Buffer too short for shared secret");
    }
    // Reset the key agreement after checking for ShortBufferException
    // above, so user can recover w/o losing internal state
    generateSecret = false;
    /*
         * NOTE: BigInteger.toByteArray() returns a byte array containing
         * the two's-complement representation of this BigInteger with
         * the most significant byte is in the zeroth element. This
         * contains the minimum number of bytes required to represent
         * this BigInteger, including at least one sign bit whose value
         * is always 0.
         *
         * Keys are always positive, and the above sign bit isn't
         * actually used when representing keys.  (i.e. key = new
         * BigInteger(1, byteArray))  To obtain an array containing
         * exactly expectedLen bytes of magnitude, we strip any extra
         * leading 0's, or pad with 0's in case of a "short" secret.
         */
    byte[] secret = this.y.modPow(this.x, modulus).toByteArray();
    if (secret.length == expectedLen) {
        System.arraycopy(secret, 0, sharedSecret, offset, secret.length);
    } else {
        // Array too short, pad it w/ leading 0s
        if (secret.length < expectedLen) {
            System.arraycopy(secret, 0, sharedSecret, offset + (expectedLen - secret.length), secret.length);
        } else {
            // Array too long, check and trim off the excess
            if ((secret.length == (expectedLen + 1)) && secret[0] == 0) {
                // ignore the leading sign byte
                System.arraycopy(secret, 1, sharedSecret, offset, expectedLen);
            } else {
                throw new ProviderException("Generated secret is out-of-range");
            }
        }
    }
    return expectedLen;
}
Also used : ProviderException(java.security.ProviderException) ShortBufferException(javax.crypto.ShortBufferException) BigInteger(java.math.BigInteger)

Example 23 with ShortBufferException

use of javax.crypto.ShortBufferException in project jdk8u_jdk by JetBrains.

the class TestSameBuffer method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        // keep the plain text
        byte[] tmpText = new byte[plainText.length];
        for (int i = 0; i < plainText.length; i++) {
            tmpText[i] = plainText[i];
        }
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        ci.init(Cipher.ENCRYPT_MODE, key);
        int offset = ci.update(plainText, 0, plainText.length, plainText, 0);
        ci.doFinal(plainText, offset);
        if (!mo.equalsIgnoreCase("ECB")) {
            iv = ci.getIV();
            aps = new IvParameterSpec(iv);
        } else {
            aps = null;
        }
        ci.init(Cipher.DECRYPT_MODE, key, aps);
        byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)];
        ci.doFinal(plainText, 0, plainText.length, recoveredText);
        // Comparison
        if (!java.util.Arrays.equals(tmpText, recoveredText)) {
            System.out.println("Original: ");
            dumpBytes(plainText);
            System.out.println("Recovered: ");
            dumpBytes(recoveredText);
            throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and CFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("CFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 24 with ShortBufferException

use of javax.crypto.ShortBufferException in project jdk8u_jdk by JetBrains.

the class Padding method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        Random rdm = new Random();
        byte[] plainText;
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        for (int i = 0; i < 15; i++) {
            plainText = new byte[1600 + i + 1];
            rdm.nextBytes(plainText);
            if (!mo.equalsIgnoreCase("GCM")) {
                ci.init(Cipher.ENCRYPT_MODE, key, aps);
            } else {
                ci.init(Cipher.ENCRYPT_MODE, key);
            }
            byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
            int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
            ci.doFinal(cipherText, offset);
            if (!mo.equalsIgnoreCase("ECB")) {
                iv = ci.getIV();
                aps = new IvParameterSpec(iv);
            } else {
                aps = null;
            }
            if (!mo.equalsIgnoreCase("GCM")) {
                ci.init(Cipher.DECRYPT_MODE, key, aps);
            } else {
                ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
            }
            byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
            int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
            byte[] tmp = new byte[len];
            for (int j = 0; j < len; j++) {
                tmp[j] = recoveredText[j];
            }
            if (!java.util.Arrays.equals(plainText, tmp)) {
                System.out.println("Original: ");
                dumpBytes(plainText);
                System.out.println("Recovered: ");
                dumpBytes(tmp);
                throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
            }
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 25 with ShortBufferException

use of javax.crypto.ShortBufferException in project jdk8u_jdk by JetBrains.

the class TestAESCipher method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        if (!mo.equalsIgnoreCase("GCM")) {
            ci.init(Cipher.ENCRYPT_MODE, key, aps);
        } else {
            ci.init(Cipher.ENCRYPT_MODE, key);
        }
        byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
        int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
        ci.doFinal(cipherText, offset);
        if (!mo.equalsIgnoreCase("ECB")) {
            iv = ci.getIV();
            aps = new IvParameterSpec(iv);
        } else {
            aps = null;
        }
        if (!mo.equalsIgnoreCase("GCM")) {
            ci.init(Cipher.DECRYPT_MODE, key, aps);
        } else {
            ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
        }
        byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
        int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
        byte[] tmp = new byte[len];
        System.arraycopy(recoveredText, 0, tmp, 0, len);
        // Comparison
        if (!java.util.Arrays.equals(plainText, tmp)) {
            System.out.println("Original: ");
            dumpBytes(plainText);
            System.out.println("Recovered: ");
            dumpBytes(tmp);
            throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

ShortBufferException (javax.crypto.ShortBufferException)46 Cipher (javax.crypto.Cipher)14 BadPaddingException (javax.crypto.BadPaddingException)13 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)13 BufferOverflowException (java.nio.BufferOverflowException)10 ByteBuffer (java.nio.ByteBuffer)9 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)9 IvParameterSpec (javax.crypto.spec.IvParameterSpec)9 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)7 SecretKey (javax.crypto.SecretKey)6 InvalidKeyException (java.security.InvalidKeyException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NoSuchProviderException (java.security.NoSuchProviderException)5 Random (java.util.Random)5 KeyGenerator (javax.crypto.KeyGenerator)5 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 IOException (java.io.IOException)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)2