/* * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.security.util; import java.security.*; import java.util.Arrays; import java.util.Locale; import 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.
*/
public final class KeyChoices {
public enum Type { SEED, EXPANDED_KEY, BOTH }
private record Choice(Type type, byte[] seed, byte[] expanded) {}
/**
* Gets the preferred choice type for an algorithm, defined as an
* overridable security property "jdk.
* 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.
*
* @param type preferred output choice type
* @param seed the seed, could be null
* @param expanded the expanded key, could be null
* @return one of the choices
*/
public static byte[] writeToChoice(Type type, byte[] seed, byte[] expanded) {
byte[] skOctets;
// Ensures using one-byte len in DER
assert seed == null || seed.length < 128;
// Ensures using two-byte len in DER
assert expanded == null || expanded.length > 256 && expanded.length < 60000;
return switch (type) {
case SEED -> {
assert seed != null;
skOctets = new byte[seed.length + 2];
skOctets[0] = (byte)0x80;
skOctets[1] = (byte) seed.length;
System.arraycopy(seed, 0, skOctets, 2, seed.length);
yield skOctets;
}
case EXPANDED_KEY -> {
assert expanded != null;
skOctets = new byte[expanded.length + 4];
skOctets[0] = 0x04;
writeShortLength(skOctets, 1, expanded.length);
System.arraycopy(expanded, 0, skOctets, 4, expanded.length);
yield skOctets;
}
case BOTH -> {
assert seed != null;
assert expanded != null;
skOctets = new byte[10 + seed.length + expanded.length];
skOctets[0] = 0x30;
writeShortLength(skOctets, 1, 6 + seed.length + expanded.length);
skOctets[4] = 0x04;
skOctets[5] = (byte)seed.length;
System.arraycopy(seed, 0, skOctets, 6, seed.length);
skOctets[6 + seed.length] = 0x04;
writeShortLength(skOctets, 7 + seed.length, expanded.length);
System.arraycopy(expanded, 0, skOctets, 10 + seed.length, expanded.length);
yield skOctets;
}
};
}
/**
* Gets the type of input.
*
* @param input input bytes
* @return the type
* @throws InvalidKeyException if input is invalid
*/
public static Type typeOfChoice(byte[] input) throws InvalidKeyException {
if (input.length < 1) {
throw new InvalidKeyException("Empty key");
}
return switch (input[0]) {
case (byte) 0x80 -> Type.SEED;
case 0x04 -> Type.EXPANDED_KEY;
case 0x30 -> Type.BOTH;
default -> throw new InvalidKeyException("Wrong tag: " + input[0]);
};
}
/**
* Splits one of the ML-KEM or ML-DSA private key formats into
* seed and expandedKey, if exists.
*
* @param seedLen correct seed length
* @param input input bytes
* @return a {@code Choice} object. Byte arrays inside are newly allocated
* @throws InvalidKeyException if input is invalid
*/
private static Choice readFromChoice(int seedLen, byte[] input)
throws InvalidKeyException {
if (input.length < seedLen + 2) {
throw new InvalidKeyException("Too short");
}
return switch (input[0]) {
case (byte) 0x80 -> {
// 80 SEED_LEN