APIdia Launch Partner
Top Description Fields Constructors Methods
sun.security.provider

public abstract Class NamedKeyPairGenerator

extends KeyPairGeneratorSpi
Class Inheritance
Known Direct Subclasses
com.sun.crypto.provider.ML_KEM_Impls.KPG, sun.security.provider.ML_DSA_Impls.KPG
Imports
sun.security.pkcs.NamedPKCS8Key, sun.security.x509.NamedX509Key, java.security.InvalidAlgorithmParameterException, .InvalidParameterException, .KeyPair, .KeyPairGeneratorSpi, .ProviderException, .SecureRandom, java.security.spec.AlgorithmParameterSpec, .NamedParameterSpec

A base class for all KeyPairGenerator implementations that can be configured with a named parameter set.

Together with NamedKeyFactory, NamedKEM, and NamedSignature, these classes form a compact framework designed to support any public key algorithm standardized with named parameter sets. In this scenario, the algorithm name is the "family name" and each standardized parameter set has a "parameter set name". Implementations of these classes are able to instantiate a KeyPairGenerator, KeyFactory, or KEM or Signature object using either the family name or a parameter set name. All keys used in this context will be of the type NamedPKCS8Key or NamedX509Key, with getAlgorithm returning the family name, and getParams returning the parameter set name as a NamedParameterSpec object.

A NamedKeyPairGenerator or NamedKeyFactory implementation must include a zero-argument public constructor that calls super(fname, pnames), where fname is the family name of the algorithm and pnames are its supported parameter set names. pnames must contain at least one element. For an implementation of NamedKeyPairGenerator, the first element becomes its default parameter set, i.e. the parameter set used by generated keys unless initialize(AlgorithmParameterSpec, java.security.SecureRandom) is called on a different parameter set.

A NamedKEM or NamedSignature implementation must include a zero-argument public constructor that calls super(fname, factory), where fname is the family name of the algorithm and factory is the NamedKeyFactory object that is used to translate foreign keys. factory only recognizes parameter sets supported by this implementation.

An implementation must implement all abstract methods. For all these methods, the implementation must relinquish any "ownership" of any input and output array argument. Precisely, the implementation must not retain any reference to a returning array so that it won't be able to modify its content later. Similarly, the implementation must not modify any input array argument and must not retain any reference to an input array argument after the call.

Also, an implementation must not keep any extra copy of a private key in any format. For key generation, the only copy is the one returned in the implGenerateKeyPair call. For all other methods, it must not make a copy of the input private key. A KEM implementation also must not keep a copy of the shared secret key, no matter if it's an encapsulator or a decapsulator. Only the code that owns these sensitive data can choose to perform cleanup when it determines they are no longer needed.

The NamedSignature and NamedKEM classes provide implCheckPublicKey and implCheckPrivateKey methods that allow an implementation to validate a key before using it. An implementation may return a parsed key in a local type, and this parsed key will be passed to an operational method (For example, implSign) later. An implementation must not retain a reference of the parsed key.

The private key, represented as a byte array when used in NamedKEM or NamedSignature, is referred to as its expanded format. For some algorithms, this format may differ from the key material inside a PKCS #8 file. For example, FIPS 204 Table 2 defines the ML-DSA-65 private key as a 4032-byte array, which is used in the ML-DSA.Sign function in Algorithm 2, representing the expanded format. However, in RFC 9881, a private key can be encoded into a CHOICE of three formats, none in the same as the FIPS 204 format. The choices are defined in sun.security.util.KeyChoices. A NamedKeyPairGenerator implementation should return both the expanded key and a preferred encoding in its implGenerateKeyPair method.

A NamedKeyFactory must override the implExpand method to derive the expanded format from an encoding format, or return null if there is no difference.

Implementations may support multiple encoding formats.

A NamedKeyFactory must not modify the encoding when generating a key from a KeySpec object, ensuring that when re-encoded, the key retains its original encoding format.

A NamedKeyFactory can choose a different encoding format when translateKey is called.

When constructing a NamedX509Key or NamedPKCS8Key object from raw key bytes, the key bytes are directly referenced within the object, so the caller must not modify them afterward. Similarly, the key's getRawBytes method returns direct references to the underlying raw key bytes, meaning the caller must not alter the contents of the returned value.

Together, these measures ensure the classes are as efficient as possible, preventing unnecessary array cloning and potential data leaks. While these classes should not be considered immutable, strictly adhering to the rules above will ensure data integrity is maintained.

Note

A limitation of NamedKeyPairGenerator and NamedKeyFactory is that the keys generated by their implementations will always be of type NamedX509Key or NamedPKCS8Key. Existing implementations of algorithms like EdDSA and XDH have been generating keys implementing EdECKey or XECKey interfaces, and they are not rewritten with this framework. NamedParameterSpec fields not implemented with this framework include Ed25519, Ed448, X25519, and X448.

Field Summary

Modifier and TypeField and Description
private final String
protected String
private final String[]
private SecureRandom

Constructor Summary

AccessConstructor and Description
protected
NamedKeyPairGenerator(String
the family name
fname
,
String...
supported parameter set names, at least one is needed. If multiple, the first one becomes the default parameter set name.
pnames
)

Creates a new NamedKeyPairGenerator object.

Method Summary

Modifier and TypeMethod and Description
private String
public KeyPair
generateKeyPair()

Implements abstract java.security.KeyPairGeneratorSpi.generateKeyPair.

Generates a key pair.

protected abstract byte[][]

Returns:

the public key, the private key in its encoding format, and the private key in its expanded format (in this order) in raw bytes. If the expanded format of the private key is the same as its encoding format, the 3rd element must be omitted.
implGenerateKeyPair
(String
parameter set name
pname
,
SecureRandom
SecureRandom object, null if not initialized
sr
)

User-defined key pair generator.

public void
initialize(AlgorithmParameterSpec params, SecureRandom random)

Overrides java.security.KeyPairGeneratorSpi.initialize.

Initializes the key pair generator using the specified parameter set and user-provided source of randomness.

public void
initialize(int keysize, SecureRandom random)

Implements abstract java.security.KeyPairGeneratorSpi.initialize.

Initializes the key pair generator for a certain keysize, using the default parameter set.

Field Detail

fnameback to summary
private final String fname
pnameback to summary
protected String pname
pnamesback to summary
private final String[] pnames
secureRandomback to summary
private SecureRandom secureRandom

Constructor Detail

NamedKeyPairGeneratorback to summary
protected NamedKeyPairGenerator(String fname, String... pnames)

Creates a new NamedKeyPairGenerator object.

Parameters
fname:String

the family name

pnames:String[]

supported parameter set names, at least one is needed. If multiple, the first one becomes the default parameter set name.

Method Detail

checkNameback to summary
private String checkName(String pname) throws InvalidAlgorithmParameterException
generateKeyPairback to summary
public KeyPair generateKeyPair()

Implements abstract java.security.KeyPairGeneratorSpi.generateKeyPair.

Doc from java.security.KeyPairGeneratorSpi.generateKeyPair.

Generates a key pair. Unless an initialization method is called using a KeyPairGenerator interface, algorithm-specific defaults will be used. This will generate a new key pair every time it is called.

Annotations
@Override
implGenerateKeyPairback to summary
protected abstract byte[][] implGenerateKeyPair(String pname, SecureRandom sr)

User-defined key pair generator.

Parameters
pname:String

parameter set name

sr:SecureRandom

SecureRandom object, null if not initialized

Returns:byte[][]

the public key, the private key in its encoding format, and the private key in its expanded format (in this order) in raw bytes. If the expanded format of the private key is the same as its encoding format, the 3rd element must be omitted.

Exceptions
ProviderException:
if there is an internal error
initializeback to summary
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException

Overrides java.security.KeyPairGeneratorSpi.initialize.

Doc from java.security.KeyPairGeneratorSpi.initialize.

Initializes the key pair generator using the specified parameter set and user-provided source of randomness.

This concrete method has been added to this previously-defined abstract class. (For backwards compatibility, it cannot be abstract.) It may be overridden by a provider to initialize the key pair generator. Such an override is expected to throw an InvalidAlgorithmParameterException if a parameter is inappropriate for this key pair generator. If this method is not overridden, it always throws an UnsupportedOperationException.

Annotations
@Override
Exceptions
InvalidAlgorithmParameterException:
if the given parameters are inappropriate for this key pair generator.
initializeback to summary
public void initialize(int keysize, SecureRandom random)

Implements abstract java.security.KeyPairGeneratorSpi.initialize.

Doc from java.security.KeyPairGeneratorSpi.initialize.

Initializes the key pair generator for a certain keysize, using the default parameter set.

Annotations
@Override