java.lang.Object
net.i2p.router.crypto.pqc.MLKEM

public final class MLKEMextends Object
Wrapper around bouncycastle.
  • Field Details

    • MLKEM512KeyFactory

      public static final KeyFactory MLKEM512KeyFactory
      MLKEM-512 key factory.
    • MLKEM768KeyFactory

      public static final KeyFactory MLKEM768KeyFactory
      MLKEM-768 key factory.
    • MLKEM1024KeyFactory

      public static final KeyFactory MLKEM1024KeyFactory
      MLKEM-1024 key factory.
  • Constructor Details

    • MLKEM

      private MLKEM()
      Utility class, not intended for instantiation.
  • Method Details

    • getKeys

      public static KeyPair getKeys(EncType type) throws GeneralSecurityException
      Alice side, key generation.
      Parameters:
      type - must be one of the internal types MLKEM*_INT
      Returns:
      the key pair, public key in [0] and private key in [1]
      Throws:
      GeneralSecurityException - if the type is not an ML-KEM type
    • generateKeys

      public static byte[][] generateKeys(EncType type) throws GeneralSecurityException
      Alice side, raw key generation.
      Parameters:
      type - must be one of the internal types MLKEM*_INT
      Returns:
      a two element array holding the encoded public key in [0] and the encoded private key in [1]
      Throws:
      GeneralSecurityException - if the type is not an ML-KEM type
    • encaps

      public static byte[][] encaps(EncType type, byte[] pub) throws GeneralSecurityException
      Bob side.

      Fails if the public key is not a well formed ML-KEM public key of the given parameter set, or if the type is not an ML-KEM type.

      Parameters:
      type - the encryption type
      pub - the public key, of exactly the length for this parameter set
      Returns:
      ciphertext and shared secret, both non-null
      Throws:
      GeneralSecurityException - if the type is unsupported or the key is malformed
    • decaps

      public static byte[] decaps(EncType type, byte[] ciphertext, byte[] decapkey) throws GeneralSecurityException
      Alice side.

      Decapsulation implements the implicit rejection required by FIPS 203 section 7.3: a ciphertext that is malformed, truncated, tampered with, or addressed to a different key never raises. Instead the shared secret is replaced by a pseudorandom value derived from the decapsulation key and the rejected ciphertext, so the result simply does not match the peer's. The replacement is deterministic, so retrying learns nothing.

      A caller that needs to authenticate the peer must therefore compare the returned secret against the expected one, because no exception distinguishes a bad ciphertext. A mismatched 32 byte secret is the only signal of rejection.

      Parameters:
      type - the encryption type
      ciphertext - the ciphertext to decrypt
      decapkey - the decapsulation key
      Returns:
      the 32 byte shared secret, never null; a rejected ciphertext yields a pseudorandom value rather than an error
      Throws:
      GeneralSecurityException - if the type is not an ML-KEM type
    • getParam

      private static MLKEMParameters getParam(EncType type) throws GeneralSecurityException
      EncType to ML-KEM parameters.
      Parameters:
      type - the encryption type
      Returns:
      the parameters for the parameter set named by the type
      Throws:
      GeneralSecurityException - if the type is not an ML-KEM type