Skip to content

Set Default SecureRandom Algorithm - #1580

Open
johnpeck-us-ibm wants to merge 1 commit into
IBM:mainfrom
johnpeck-us-ibm:SecureRandomFix
Open

Set Default SecureRandom Algorithm #1580
johnpeck-us-ibm wants to merge 1 commit into
IBM:mainfrom
johnpeck-us-ibm:SecureRandomFix

Conversation

@johnpeck-us-ibm

@johnpeck-us-ibm johnpeck-us-ibm commented Jul 8, 2026

Copy link
Copy Markdown
Member

Add code to be able to set the Default SecureRandom algorithm from provider.

fixes: #1553

Signed-off-by: johnpeck-us-ibm johnpeck@us.ibm.com

Comment thread src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusFIPS.java
Comment thread src/test/java/ibm/jceplus/junit/tests/TestProviderServices.java
Comment thread src/test/java/ibm/jceplus/junit/tests/TestProviderServices.java
Comment thread PROVIDER_CONFIG_FORMAT.md Outdated
Add code to be able to set the Default SecureRandom algorithm from provider.

fixes: IBM#1553

Signed-off-by: johnpeck-us-ibm <johnpeck@us.ibm.com>
+ " # SecureRandom\n"
+ " # =======================================================================\n"
+ " #\n"
+ "securerandomdefault = SHA256DRBG\n"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we follow a pattern more similar to the existing ones? Like SecureRandom.default = SHA256DRBG.

@johnpeck-us-ibm johnpeck-us-ibm Jul 28, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this way since this was a general parameter like name, and description.

* @param services the list of services to order
* @return a new list with the designated SecureRandom service placed first
*/
public List<ServiceDefinition> orderServices(List<ServiceDefinition> services) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a chance that someone will expect that the algorithm to be used by default is the one they actually specified first in the config file, instead of the one indicated with the specific property?

Is that something we'd want to allow if that property hasn't be set? And if not, we should probably document it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first in the list behavior is not something that is documented anywhere that I could find. Unfortunately, with out this the default Secure Random Alg is random. In my testing I have seen it switch back and forth between the two we define.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I didn't say remove this. It was more of a question for potential additional functionality if we want to or can even do it. Whether we could honour someone's order if the default wasn't set. Of course that might not even be possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can not honor the first item they set as the default if the default is not called out. I have seen this switch back ans forth randomly. We could but we would have to move away from using the Properties class.

Note: There is nothing in any documentation that talks about the first one defined is the default in any documentation. I see this only in the comments in the Oracle code.

assertTrue (acceptableValues.contains(secureRandom.getAlgorithm()), "SecureRandom not SunJCE - " + secureRandom.getAlgorithm());

//Now Put OpenJCEPlus first in the list.
Provider provider = (Provider) Class.forName("com.ibm.crypto.plus.provider.OpenJCEPlus").getDeclaredConstructor().newInstance();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using reflection here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have too, It is just one of a few ways this can be done.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be simpler to do Provider provider = new com.ibm.crypto.plus.provider.OpenJCEPlus();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ether way works. This does follow how we do it in our test code though.

assertTrue (acceptableValues.contains(secureRandom.getAlgorithm()), "SecureRandom not SunJCE");

//Now Put OpenJCEPlus first in the list.
Provider provider = (Provider) Class.forName("com.ibm.crypto.plus.provider.OpenJCEPlusFIPS").getDeclaredConstructor().newInstance();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have too, It is just one of a few ways this can be done.

Comment thread PROVIDER_CONFIG_FORMAT.md
default = true
```

# Set Default Secure Random algorithm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need a ```properties here as well?

@johnpeck-us-ibm johnpeck-us-ibm Jul 28, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if the configuration is not a propertyconfig file like in ProviderDefAttrs.config but is stored in a variable like in DefaultProviderAttrs.java

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm talking about the way the md file is shown. I think we need a ```properties here. Otherwise it doesn't render properly.

Comment thread PROVIDER_CONFIG_FORMAT.md
- **name**: Unique identifier for the provider (required)
- **description**: Human-readable description of the provider (required)
- **default**: Whether to load default provider attributes (optional, values: `true`, `false`, `1`, `0`)
- **securerandomdefault**: Determines which Secure Random Algorithm is the default one if this provider is first in the provider list

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also document that if it's not set, it defaults to a value? That is if we decide that we won't honour user-specifies order if it's not.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can not determine the one that will be selected. It is random. So, not sure what to say here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm getting this right, when someone doesn't specify this in the configuration, this defaults to SHA256DRBG. Should we mention this in this md file?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As indicated in the comment with out this setting the default could be either SHA256DRBG or SHA512DRBG what is selected is random. It could be either one.

@jasonkatonica jasonkatonica left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/test/java/ibm/jceplus/junit/tests/TestProviderServices.java

@taoliult taoliult left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent SecureRandom registration and usage

4 participants