JDK-6733443 : JCA/JCE init does not completely reset the delayed provider selection mechanism.
  • Type: Bug
  • Component: security-libs
  • Sub-Component: javax.crypto
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2008-08-04
  • Updated: 2022-07-27
  • Resolved: 2022-07-27
Related Reports
Duplicate :  
Sub Tasks
JDK-8291414 :  
Description
I wrote:
The PKCS11 provider document makes a statement about the delayed
selection mechanism that I think is wrong given the implementation.
It claims that for Cipher, KeyAgreement, Mac, etc.:

    If an application calls the initialization method multiple times
    (each time with a different key, for example), the proper provider
    for the given key is selected each time.  In other words, a
    different provider may be selected for each initialization call.

Was it the intent, and the code in Cipher is wrong?  Or are the docs wrong, and need to be fixed?

Sean thought he remembers that was the original intent, but wasn't sure. 
-----

Andreas responded:
After some back and forth, this was the intent, but the implementation
never got updated to reflect that. Basically, calling init() should
erase all existing state and give the same result as if called on a
new object.
-----

Andreas thinks there may be a fairly old bug against this, but I can't find it.  Refiling.

Comments
Filed the doc sub-task to address the doc update. Closing this one as "Not-a-Bug".
27-07-2022

If we treat calling init() to erase all prior states and find the right provider each time, it may have some performance implication as we need to search through all providers again to find the right one. For people keep using the same Cipher object against keys from the compatible providers, they are paying additional cost which is unnecessary for them. Callers who need the provider re-selection (e.g. work with keys from vastly different providers, say software vs token-based) is recommended to get a fresh instance via the getInstance() call and then call init() every time a provider re-selection is needed. The first init() call would lock down the provider selection for that particular Service, e.g. Signature, Cipher, and if the 2nd init() is supplied with incompatible keys or parameters, an exception would be thrown. Documentation should be fixed to reflect current implementation.
27-07-2022

A modified version of the test case in JDK-4953555 has been provided with a new bug report JDK-8194631 .
04-01-2018