I’m trying to get a KeyPairGenerator
instance for Ed25519.
I’d also like to provide an explicit SecureRandom
instance. There’s an initialize
method that accepts an int
, the keysize, and a SecureRandom
.
What value can I pass as a keysize
argument?
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("Ed25519");
SecureRandom secureRandom = new SecureRandom();
int keysize = 123; /* what's an appropriate value? */
keyPairGenerator.initialize(keysize, secureRandom);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
The above throws
Exception in thread "main" java.security.InvalidParameterException: Unsupported size: 123
at jdk.crypto.ec/sun.security.ec.ParametersMap$1.get(ParametersMap.java:78)
at java.base/java.util.Optional.orElseThrow(Optional.java:403)
at jdk.crypto.ec/sun.security.ec.ParametersMap.getBySize(ParametersMap.java:102)
at jdk.crypto.ec/sun.security.ec.ed.EdDSAParameters.getBySize(EdDSAParameters.java:312)
at jdk.crypto.ec/sun.security.ec.ed.EdDSAKeyPairGenerator.initialize(EdDSAKeyPairGenerator.java:75)
at java.base/java.security.KeyPairGenerator$Delegate.initialize(KeyPairGenerator.java:671)
So what is an appropriate value?
6
See the Javadoc for KeyPairGeneratorSpi
In case the client does not explicitly initialize the
KeyPairGenerator
(via a call to aninitialize
method), each provider must supply (and document) a default initialization. See the Keysize Restriction sections of the JDK Providers document for information on theKeyPairGenerator
defaults used by JDK providers. However, note that defaults may vary across different providers. Additionally, the default value for a provider may change in a future version. Therefore, it is recommended to explicitly initialize theKeyPairGenerator
instead of relying on provider-specific defaults.
The SunEC Provider described in the linked document supports Ed25519
.
Under Keysize Restrictions for the various supported algorithms, it states that the “Default Keysize” for Ed25519
is 255
. It additionally states the following restriction
Keysize must be 255
In other words, the only valid value is 255.
keyPairGenerator.initialize(255, secureRandom);
Alternatively, since this value is fixed, you shouldn’t need to provide it. Instead, use the initialize
overload that accepts an AlgorithmParameterSpec
and pass the corresponding NamedParameterSpec
. For example,
keyPairGenerator.initialize(NamedParameterSpec.ED25519, new SecureRandom());
Solution see code:
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("Ed25519");
keyPairGenerator.initialize(255,new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
Solution found using the debugger: keysize=32, bits=255
The answer is 255!
7