java.security.ProviderException Java Examples

The following examples show how to use java.security.ProviderException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SessionManager.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Session openSession() throws PKCS11Exception {
    if ((maxSessions != Integer.MAX_VALUE) &&
            (activeSessions.get() >= maxSessions)) {
        throw new ProviderException("No more sessions available");
    }

    long id = token.p11.C_OpenSession
                (token.provider.slotID, openSessionFlags, null, null);
    Session session = new Session(token, id);
    activeSessions.incrementAndGet();
    if (debug != null) {
        synchronized(maxActiveSessionsLock) {
            if (activeSessions.get() > maxActiveSessions) {
                maxActiveSessions = activeSessions.get();
                if (maxActiveSessions % 10 == 0) {
                    System.out.println("Open sessions: " + maxActiveSessions);
                }
            }
        }
    }
    return session;
}
 
Example #2
Source File: SessionManager.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private Session openSession() throws PKCS11Exception {
    if ((maxSessions != Integer.MAX_VALUE) &&
            (activeSessions.get() >= maxSessions)) {
        throw new ProviderException("No more sessions available");
    }

    long id = token.p11.C_OpenSession
                (token.provider.slotID, openSessionFlags, null, null);
    Session session = new Session(token, id);
    activeSessions.incrementAndGet();
    if (debug != null) {
        synchronized(maxActiveSessionsLock) {
            if (activeSessions.get() > maxActiveSessions) {
                maxActiveSessions = activeSessions.get();
                if (maxActiveSessions % 10 == 0) {
                    System.out.println("Open sessions: " + maxActiveSessions);
                }
            }
        }
    }
    return session;
}
 
Example #3
Source File: PCBC.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs decryption operation.
 *
 * <p>The input cipher text <code>cipher</code>, starting at
 * <code>cipherOffset</code> and ending at
 * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
 * The result is stored in <code>plain</code>, starting at
 * <code>plainOffset</code>.
 *
 * @param cipher the buffer with the input data to be decrypted
 * @param cipherOffset the offset in <code>cipherOffset</code>
 * @param cipherLen the length of the input data
 * @param plain the buffer for the result
 * @param plainOffset the offset in <code>plain</code>
 * @exception ProviderException if <code>cipherLen</code> is not
 * a multiple of the block size
 * @return the length of the decrypted data
 */
int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
            byte[] plain, int plainOffset)
{
    if ((cipherLen % blockSize) != 0) {
         throw new ProviderException("Internal error in input buffering");
    }
    int i;
    int endIndex = cipherOffset + cipherLen;

    for (; cipherOffset < endIndex;
         plainOffset += blockSize, cipherOffset += blockSize) {
        embeddedCipher.decryptBlock(cipher, cipherOffset,
                               plain, plainOffset);
        for (i = 0; i < blockSize; i++) {
            plain[i + plainOffset] ^= k[i];
        }
        for (i = 0; i < blockSize; i++) {
            k[i] = (byte)(plain[i + plainOffset] ^ cipher[i + cipherOffset]);
        }
    }
    return cipherLen;
}
 
Example #4
Source File: SessionManager.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private Session openSession() throws PKCS11Exception {
    if ((maxSessions != Integer.MAX_VALUE) &&
            (activeSessions.get() >= maxSessions)) {
        throw new ProviderException("No more sessions available");
    }

    long id = token.p11.C_OpenSession
                (token.provider.slotID, openSessionFlags, null, null);
    Session session = new Session(token, id);
    activeSessions.incrementAndGet();
    if (debug != null) {
        synchronized(maxActiveSessionsLock) {
            if (activeSessions.get() > maxActiveSessions) {
                maxActiveSessions = activeSessions.get();
                if (maxActiveSessions % 10 == 0) {
                    System.out.println("Open sessions: " + maxActiveSessions);
                }
            }
        }
    }
    return session;
}
 
Example #5
Source File: CipherFeedback.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset) {
    if ((plainLen % numBytes) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }

    int nShift = blockSize - numBytes;
    int loopCount = plainLen / numBytes;

    for (; loopCount > 0 ;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        for (int i = 0; i < numBytes; i++) {
            register[nShift + i] = cipher[i + cipherOffset] =
                    (byte)(k[i] ^ plain[i + plainOffset]);
        }
    }
    return plainLen;
}
 
Example #6
Source File: CipherFeedback.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs decryption operation.
 *
 * <p>The input cipher text <code>cipher</code>, starting at
 * <code>cipherOffset</code> and ending at
 * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
 * The result is stored in <code>plain</code>, starting at
 * <code>plainOffset</code>.
 *
 * @param cipher the buffer with the input data to be decrypted
 * @param cipherOffset the offset in <code>cipherOffset</code>
 * @param cipherLen the length of the input data
 * @param plain the buffer for the result
 * @param plainOffset the offset in <code>plain</code>
 * @exception ProviderException if <code>cipherLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the decrypted data
 */
int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
            byte[] plain, int plainOffset) {
    if ((cipherLen % numBytes) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }

    int nShift = blockSize - numBytes;
    int loopCount = cipherLen / numBytes;

    for (; loopCount > 0;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        for (int i = 0; i < numBytes; i++) {
            register[i + nShift] = cipher[i + cipherOffset];
            plain[i + plainOffset]
                = (byte)(cipher[i + cipherOffset] ^ k[i]);
        }
    }
    return cipherLen;
}
 
Example #7
Source File: CipherBlockChaining.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>len</code> is not
 * a multiple of the block size
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset)
{
    if (plainLen <= 0) {
        return plainLen;
    }
    if ((plainLen % blockSize) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }
    int endIndex = plainOffset + plainLen;

    for (; plainOffset < endIndex;
         plainOffset+=blockSize, cipherOffset += blockSize) {
        for (int i = 0; i < blockSize; i++) {
            k[i] = (byte)(plain[i + plainOffset] ^ r[i]);
        }
        embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
        System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
    }
    return plainLen;
}
 
Example #8
Source File: PCBC.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs decryption operation.
 *
 * <p>The input cipher text <code>cipher</code>, starting at
 * <code>cipherOffset</code> and ending at
 * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
 * The result is stored in <code>plain</code>, starting at
 * <code>plainOffset</code>.
 *
 * @param cipher the buffer with the input data to be decrypted
 * @param cipherOffset the offset in <code>cipherOffset</code>
 * @param cipherLen the length of the input data
 * @param plain the buffer for the result
 * @param plainOffset the offset in <code>plain</code>
 * @exception ProviderException if <code>cipherLen</code> is not
 * a multiple of the block size
 * @return the length of the decrypted data
 */
int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
            byte[] plain, int plainOffset)
{
    if ((cipherLen % blockSize) != 0) {
         throw new ProviderException("Internal error in input buffering");
    }
    int i;
    int endIndex = cipherOffset + cipherLen;

    for (; cipherOffset < endIndex;
         plainOffset += blockSize, cipherOffset += blockSize) {
        embeddedCipher.decryptBlock(cipher, cipherOffset,
                               plain, plainOffset);
        for (i = 0; i < blockSize; i++) {
            plain[i + plainOffset] ^= k[i];
        }
        for (i = 0; i < blockSize; i++) {
            k[i] = (byte)(plain[i + plainOffset] ^ cipher[i + cipherOffset]);
        }
    }
    return cipherLen;
}
 
Example #9
Source File: OutputFeedback.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset) {

    if ((plainLen % numBytes) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }
    int nShift = blockSize - numBytes;
    int loopCount = plainLen / numBytes;

    for (; loopCount > 0;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        for (int i = 0; i < numBytes; i++) {
            cipher[i + cipherOffset] =
                (byte)(k[i] ^ plain[i + plainOffset]);
        }
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        System.arraycopy(k, 0, register, nShift, numBytes);
    }
    return plainLen;
}
 
Example #10
Source File: FinalizeHalf.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void test(String algo, Provider provider, boolean priv,
        Consumer<Key> method) throws Exception {
    KeyPairGenerator generator;
    try {
        generator = KeyPairGenerator.getInstance(algo, provider);
    } catch (NoSuchAlgorithmException nsae) {
        return;
    }

    System.out.println("Checking " + provider.getName() + ", " + algo);

    KeyPair pair = generator.generateKeyPair();
    Key key = priv ? pair.getPrivate() : pair.getPublic();

    pair = null;
    for (int i = 0; i < 32; ++i) {
        System.gc();
    }

    try {
        method.accept(key);
    } catch (ProviderException pe) {
        failures++;
    }
}
 
Example #11
Source File: PCBC.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the block size
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset)
{
    if ((plainLen % blockSize) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }
    int i;
    int endIndex = plainOffset + plainLen;

    for (; plainOffset < endIndex;
         plainOffset += blockSize, cipherOffset += blockSize) {
        for (i = 0; i < blockSize; i++) {
            k[i] ^= plain[i + plainOffset];
        }
        embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
        for (i = 0; i < blockSize; i++) {
            k[i] = (byte)(plain[i + plainOffset] ^ cipher[i + cipherOffset]);
        }
    }
    return plainLen;
}
 
Example #12
Source File: OutputFeedback.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset) {

    if ((plainLen % numBytes) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }
    int nShift = blockSize - numBytes;
    int loopCount = plainLen / numBytes;

    for (; loopCount > 0;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        for (int i = 0; i < numBytes; i++) {
            cipher[i + cipherOffset] =
                (byte)(k[i] ^ plain[i + plainOffset]);
        }
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        System.arraycopy(k, 0, register, nShift, numBytes);
    }
    return plainLen;
}
 
Example #13
Source File: SessionManager.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private Session openSession() throws PKCS11Exception {
    if ((maxSessions != Integer.MAX_VALUE) &&
            (activeSessions.get() >= maxSessions)) {
        throw new ProviderException("No more sessions available");
    }

    long id = token.p11.C_OpenSession
                (token.provider.slotID, openSessionFlags, null, null);
    Session session = new Session(token, id);
    activeSessions.incrementAndGet();
    if (debug != null) {
        synchronized(maxActiveSessionsLock) {
            if (activeSessions.get() > maxActiveSessions) {
                maxActiveSessions = activeSessions.get();
                if (maxActiveSessions % 10 == 0) {
                    System.out.println("Open sessions: " + maxActiveSessions);
                }
            }
        }
    }
    return session;
}
 
Example #14
Source File: SessionManager.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
Session getOpSession() throws PKCS11Exception {
    Session session = opSessions.poll();
    if (session != null) {
        return ensureValid(session);
    }
    // create a new session rather than re-using an obj session
    // that avoids potential expensive cancels() for Signatures & RSACipher
    if (maxSessions == Integer.MAX_VALUE ||
            activeSessions.get() < maxSessions) {
        session = openSession();
        return ensureValid(session);
    }
    session = objSessions.poll();
    if (session != null) {
        return ensureValid(session);
    }
    throw new ProviderException("Could not obtain session");
}
 
Example #15
Source File: FinalizeHalf.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static void test(String algo, Provider provider, boolean priv,
        Consumer<Key> method) throws Exception {
    KeyPairGenerator generator;
    try {
        generator = KeyPairGenerator.getInstance(algo, provider);
    } catch (NoSuchAlgorithmException nsae) {
        return;
    }

    System.out.println("Checking " + provider.getName() + ", " + algo);

    KeyPair pair = generator.generateKeyPair();
    Key key = priv ? pair.getPrivate() : pair.getPublic();

    pair = null;
    for (int i = 0; i < 32; ++i) {
        System.gc();
    }

    try {
        method.accept(key);
    } catch (ProviderException pe) {
        failures++;
    }
}
 
Example #16
Source File: SessionManager.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
Session getOpSession() throws PKCS11Exception {
    Session session = opSessions.poll();
    if (session != null) {
        return ensureValid(session);
    }
    // create a new session rather than re-using an obj session
    // that avoids potential expensive cancels() for Signatures & RSACipher
    if (maxSessions == Integer.MAX_VALUE ||
            activeSessions.get() < maxSessions) {
        session = openSession();
        return ensureValid(session);
    }
    session = objSessions.poll();
    if (session != null) {
        return ensureValid(session);
    }
    throw new ProviderException("Could not obtain session");
}
 
Example #17
Source File: SessionManager.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private Session openSession() throws PKCS11Exception {
    if ((maxSessions != Integer.MAX_VALUE) &&
            (activeSessions.get() >= maxSessions)) {
        throw new ProviderException("No more sessions available");
    }

    long id = token.p11.C_OpenSession
                (token.provider.slotID, openSessionFlags, null, null);
    Session session = new Session(token, id);
    activeSessions.incrementAndGet();
    if (debug != null) {
        synchronized(maxActiveSessionsLock) {
            if (activeSessions.get() > maxActiveSessions) {
                maxActiveSessions = activeSessions.get();
                if (maxActiveSessions % 10 == 0) {
                    System.out.println("Open sessions: " + maxActiveSessions);
                }
            }
        }
    }
    return session;
}
 
Example #18
Source File: FinalizeHalf.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static void test(String algo, Provider provider, boolean priv,
        Consumer<Key> method) throws Exception {
    KeyPairGenerator generator;
    try {
        generator = KeyPairGenerator.getInstance(algo, provider);
    } catch (NoSuchAlgorithmException nsae) {
        return;
    }

    System.out.println("Checking " + provider.getName() + ", " + algo);

    KeyPair pair = generator.generateKeyPair();
    Key key = priv ? pair.getPrivate() : pair.getPublic();

    pair = null;
    for (int i = 0; i < 32; ++i) {
        System.gc();
    }

    try {
        method.accept(key);
    } catch (ProviderException pe) {
        failures++;
    }
}
 
Example #19
Source File: FinalizeHalf.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void test(String algo, Provider provider, boolean priv,
        Consumer<Key> method) throws Exception {
    KeyPairGenerator generator;
    try {
        generator = KeyPairGenerator.getInstance(algo, provider);
    } catch (NoSuchAlgorithmException nsae) {
        return;
    }

    System.out.println("Checking " + provider.getName() + ", " + algo);

    KeyPair pair = generator.generateKeyPair();
    Key key = priv ? pair.getPrivate() : pair.getPublic();

    pair = null;
    for (int i = 0; i < 32; ++i) {
        System.gc();
    }

    try {
        method.accept(key);
    } catch (ProviderException pe) {
        failures++;
    }
}
 
Example #20
Source File: CipherFeedback.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset) {
    if ((plainLen % numBytes) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }

    int nShift = blockSize - numBytes;
    int loopCount = plainLen / numBytes;

    for (; loopCount > 0 ;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        for (int i = 0; i < numBytes; i++) {
            register[nShift + i] = cipher[i + cipherOffset] =
                    (byte)(k[i] ^ plain[i + plainOffset]);
        }
    }
    return plainLen;
}
 
Example #21
Source File: JdkSASL.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object newInstance(Object ctrParamObj)
    throws NoSuchAlgorithmException {
    String type = getType();
    if (ctrParamObj != null) {
        throw new InvalidParameterException
            ("constructorParameter not used with " + type + " engines");
    }
    String algo = getAlgorithm();
    // GSSAPI uses same impl class for client and server
    try {
        if (algo.equals("GSSAPI")) {
            return new com.sun.security.sasl.gsskerb.FactoryImpl();
        }
    } catch (Exception ex) {
        throw new NoSuchAlgorithmException("Error constructing " +
             type + " for " + algo + " using JdkSASL", ex);
    }
    throw new ProviderException("No impl for " + algo +
        " " + type);
}
 
Example #22
Source File: OutputFeedback.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset) {

    if ((plainLen % numBytes) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }
    int nShift = blockSize - numBytes;
    int loopCount = plainLen / numBytes;

    for (; loopCount > 0;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        for (int i = 0; i < numBytes; i++) {
            cipher[i + cipherOffset] =
                (byte)(k[i] ^ plain[i + plainOffset]);
        }
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        System.arraycopy(k, 0, register, nShift, numBytes);
    }
    return plainLen;
}
 
Example #23
Source File: CipherFeedback.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs decryption operation.
 *
 * <p>The input cipher text <code>cipher</code>, starting at
 * <code>cipherOffset</code> and ending at
 * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
 * The result is stored in <code>plain</code>, starting at
 * <code>plainOffset</code>.
 *
 * @param cipher the buffer with the input data to be decrypted
 * @param cipherOffset the offset in <code>cipherOffset</code>
 * @param cipherLen the length of the input data
 * @param plain the buffer for the result
 * @param plainOffset the offset in <code>plain</code>
 * @exception ProviderException if <code>cipherLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the decrypted data
 */
int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
            byte[] plain, int plainOffset) {
    if ((cipherLen % numBytes) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }

    int nShift = blockSize - numBytes;
    int loopCount = cipherLen / numBytes;

    for (; loopCount > 0;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        for (int i = 0; i < numBytes; i++) {
            register[i + nShift] = cipher[i + cipherOffset];
            plain[i + plainOffset]
                = (byte)(cipher[i + cipherOffset] ^ k[i]);
        }
    }
    return cipherLen;
}
 
Example #24
Source File: SessionManager.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Session openSession() throws PKCS11Exception {
    if ((maxSessions != Integer.MAX_VALUE) &&
            (activeSessions.get() >= maxSessions)) {
        throw new ProviderException("No more sessions available");
    }

    long id = token.p11.C_OpenSession
                (token.provider.slotID, openSessionFlags, null, null);
    Session session = new Session(token, id);
    activeSessions.incrementAndGet();
    if (debug != null) {
        synchronized(maxActiveSessionsLock) {
            if (activeSessions.get() > maxActiveSessions) {
                maxActiveSessions = activeSessions.get();
                if (maxActiveSessions % 10 == 0) {
                    System.out.println("Open sessions: " + maxActiveSessions);
                }
            }
        }
    }
    return session;
}
 
Example #25
Source File: PCBC.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the block size
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset)
{
    if ((plainLen % blockSize) != 0) {
        throw new ProviderException("Internal error in input buffering");
    }
    int i;
    int endIndex = plainOffset + plainLen;

    for (; plainOffset < endIndex;
         plainOffset += blockSize, cipherOffset += blockSize) {
        for (i = 0; i < blockSize; i++) {
            k[i] ^= plain[i + plainOffset];
        }
        embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
        for (i = 0; i < blockSize; i++) {
            k[i] = (byte)(plain[i + plainOffset] ^ cipher[i + cipherOffset]);
        }
    }
    return plainLen;
}
 
Example #26
Source File: PRNG.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a user-specified number of random bytes.
 *
 * @param bytes the array to be filled in with random bytes.
 */
@Override
protected void engineNextBytes(byte[] bytes) {
    if (bytes != null) {
        if (generateSeed(0, bytes) == null) {
            throw new ProviderException("Error generating random bytes");
        }
    }
}
 
Example #27
Source File: RSASignature.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new RSASignature. Used by subclasses.
 */
RSASignature(String digestName) {

    try {
        messageDigest = MessageDigest.getInstance(digestName);
        // Get the digest's canonical name
        messageDigestAlgorithm = messageDigest.getAlgorithm();

    } catch (NoSuchAlgorithmException e) {
       throw new ProviderException(e);
    }

    needsReset = false;
}
 
Example #28
Source File: DigestBase.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected final byte[] engineDigest() {
    byte[] b = new byte[digestLength];
    try {
        engineDigest(b, 0, b.length);
    } catch (DigestException e) {
        throw (ProviderException)
            new ProviderException("Internal error").initCause(e);
    }
    return b;
}
 
Example #29
Source File: PRNG.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the given number of seed bytes.  This call may be used to
 * seed other random number generators.
 *
 * @param numBytes the number of seed bytes to generate.
 *
 * @return the seed bytes.
 */
@Override
protected byte[] engineGenerateSeed(int numBytes) {
    byte[] seed = generateSeed(numBytes, null);

    if (seed == null) {
        throw new ProviderException("Error generating seed bytes");
    }
    return seed;
}
 
Example #30
Source File: PRNG.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a user-specified number of random bytes.
 *
 * @param bytes the array to be filled in with random bytes.
 */
@Override
protected void engineNextBytes(byte[] bytes) {
    if (bytes != null) {
        if (generateSeed(0, bytes) == null) {
            throw new ProviderException("Error generating random bytes");
        }
    }
}