APIdia Launch Partner
Top Description Inners Constructors Methods
sun.security.util

public final Class KeyChoices

extends Object
Class Inheritance
Imports
java.security.*, java.util.Arrays, .Locale, java.util.function.BiFunction

The content of an ML-KEM or ML-DSA private key is defined as a CHOICE among three different representations. For example:
 ML-KEM-1024-PrivateKey ::= CHOICE {
      seed [0] OCTET STRING (SIZE (64)),
      expandedKey OCTET STRING (SIZE (3168)),
      both SEQUENCE {
          seed OCTET STRING (SIZE (64)),
          expandedKey OCTET STRING (SIZE (3168))
          }
      }
This class supports reading, writing, and converting between them.

Current code follows RFC 9935 and RFC 9881.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record
public static enum

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

the preferred encoding
choiceToChoice
(KeyChoices.Type
preferred output choice type
type
,
String
parameter set name
pname
,
int
seed length
seedLen
,
byte[]
the input encoding
input
,
BiFunction<String, byte[], byte[]>
function to calculate expanded from seed, could be null if there is already expanded in input
expander
)

Reads from any encoding and write to the specified type.

public static byte[]

Returns:

the expanded key
choiceToExpanded
(String
parameter set name
pname
,
int
seed length
seedLen
,
byte[]
input encoding
input
,
BiFunction<String, byte[], byte[]>
function to calculate expanded from seed, could be null if there is already expanded in input
expander
)

Reads from any choice of encoding and return the expanded format.

public static KeyChoices.Type

Returns:

the type
getPreferred
(String
"mlkem" or "mldsa".
name
)

Gets the preferred choice type for an algorithm, defined as an overridable security property "jdk.

private static KeyChoices.Choice

Returns:

a Choice object. Byte arrays inside are newly allocated
readFromChoice
(int
correct seed length
seedLen
,
byte[]
input bytes
input
)

Splits one of the ML-KEM or ML-DSA private key formats into seed and expandedKey, if exists.

private static int
readShortLength(byte[] input, int from)

public static KeyChoices.Type

Returns:

the type
typeOfChoice
(byte[]
input bytes
input
)

Gets the type of input.

private static void
writeShortLength(byte[] input, int from, int value)

public static byte[]

Returns:

one of the choices
writeToChoice
(KeyChoices.Type
preferred output choice type
type
,
byte[]
the seed, could be null
seed
,
byte[]
the expanded key, could be null
expanded
)

Writes one of the ML-KEM or ML-DSA private key formats.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

KeyChoicesback to summary
public KeyChoices()

Method Detail

choiceToChoiceback to summary
public static byte[] choiceToChoice(KeyChoices.Type type, String pname, int seedLen, byte[] input, BiFunction<String, byte[], byte[]> expander) throws InvalidKeyException

Reads from any encoding and write to the specified type.

Parameters
type:KeyChoices.Type

preferred output choice type

pname:String

parameter set name

seedLen:int

seed length

input:byte[]

the input encoding

expander:BiFunction<String, byte[], byte[]>

function to calculate expanded from seed, could be null if there is already expanded in input

Returns:byte[]

the preferred encoding

Exceptions
InvalidKeyException:
if input is invalid or does not have enough information to generate the output
choiceToExpandedback to summary
public static byte[] choiceToExpanded(String pname, int seedLen, byte[] input, BiFunction<String, byte[], byte[]> expander) throws InvalidKeyException

Reads from any choice of encoding and return the expanded format.

Parameters
pname:String

parameter set name

seedLen:int

seed length

input:byte[]

input encoding

expander:BiFunction<String, byte[], byte[]>

function to calculate expanded from seed, could be null if there is already expanded in input

Returns:byte[]

the expanded key

Exceptions
InvalidKeyException:
if input is invalid
getPreferredback to summary
public static KeyChoices.Type getPreferred(String name)

Gets the preferred choice type for an algorithm, defined as an overridable security property "jdk..pkcs8.encoding".

Parameters
name:String

"mlkem" or "mldsa".

Returns:KeyChoices.Type

the type

Exceptions
IllegalArgumentException:
if property is invalid value
readFromChoiceback to summary
private static KeyChoices.Choice readFromChoice(int seedLen, byte[] input) throws InvalidKeyException

Splits one of the ML-KEM or ML-DSA private key formats into seed and expandedKey, if exists.

Parameters
seedLen:int

correct seed length

input:byte[]

input bytes

Returns:KeyChoices.Choice

a Choice object. Byte arrays inside are newly allocated

Exceptions
InvalidKeyException:
if input is invalid
readShortLengthback to summary
private static int readShortLength(byte[] input, int from) throws InvalidKeyException
typeOfChoiceback to summary
public static KeyChoices.Type typeOfChoice(byte[] input) throws InvalidKeyException

Gets the type of input.

Parameters
input:byte[]

input bytes

Returns:KeyChoices.Type

the type

Exceptions
InvalidKeyException:
if input is invalid
writeShortLengthback to summary
private static void writeShortLength(byte[] input, int from, int value)
writeToChoiceback to summary
public static byte[] writeToChoice(KeyChoices.Type type, byte[] seed, byte[] expanded)

Writes one of the ML-KEM or ML-DSA private key formats.

This method does not check the length of the inputs or whether they match each other. The caller must make sure `seed` and/or `expanded` are provided if `type` requires any of them.

Parameters
type:KeyChoices.Type

preferred output choice type

seed:byte[]

the seed, could be null

expanded:byte[]

the expanded key, could be null

Returns:byte[]

one of the choices

sun.security.util back to summary

private final Record KeyChoices.Choice

extends Record
Class Inheritance
Record Components
type:KeyChoices.Type 
seed:byte[] 
expanded:byte[] 

Field Summary

Modifier and TypeField and Description
private final byte[]
expanded

Record Component accessed by expanded().

private final byte[]
seed

Record Component accessed by seed().

private final KeyChoices.Type
type

Record Component accessed by type().

Constructor Summary

AccessConstructor and Description
private
Choice(KeyChoices.Type type, byte[] seed, byte[] expanded)

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public byte[]
expanded()

Record Component getter of expanded.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public byte[]
seed()

Record Component getter of seed.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

public KeyChoices.Type
type()

Record Component getter of type.

Field Detail

expandedback to summary
private final byte[] expanded

Record Component accessed by expanded().

seedback to summary
private final byte[] seed

Record Component accessed by seed().

typeback to summary
private final KeyChoices.Type type

Record Component accessed by type().

Constructor Detail

Choiceback to summary
private Choice(KeyChoices.Type type, byte[] seed, byte[] expanded)

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
expandedback to summary
public byte[] expanded()

Record Component getter of expanded.

hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

seedback to summary
public byte[] seed()

Record Component getter of seed.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

typeback to summary
public KeyChoices.Type type()

Record Component getter of type.

sun.security.util back to summary

public final Enum KeyChoices.Type

extends Enum<KeyChoices.Type>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final KeyChoices.Type
public static final KeyChoices.Type
public static final KeyChoices.Type

Constructor Summary

AccessConstructor and Description
private
Type()

Method Summary

Modifier and TypeMethod and Description
public static KeyChoices.Type
public static KeyChoices.Type[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BOTHback to summary
public static final KeyChoices.Type BOTH
EXPANDED_KEYback to summary
public static final KeyChoices.Type EXPANDED_KEY
SEEDback to summary
public static final KeyChoices.Type SEED

Constructor Detail

Typeback to summary
private Type()

Method Detail

valueOfback to summary
public static KeyChoices.Type valueOf(String name)
valuesback to summary
public static KeyChoices.Type[] values()