Class RFC9980Utils

java.lang.Object
org.bouncycastle.openpgp.operator.RFC9980Utils

public class RFC9980Utils extends Object
Byte-level building blocks of the ML-KEM + ECDH composite encryption schemes of RFC 9980 that are shared by the lightweight (.bc) and JCA (.jcajce) operators: the key combiner of sec. 4.2.1 and the PKESK algorithm-specific field layout of sec. 4.3.1. The composite signature schemes need no such helpers - their signatures are the plain concatenation of the two component signatures.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final byte[]
    The domain separation constant of the key combiner (RFC 9980 sec.
    static final int
    The symmetric algorithm the composite schemes wrap the session key with (RFC 9980 sec.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    checkV3SessionKey(int symmetricKeyAlgorithm, byte[] sessionKey)
    Check that a session key unwrapped from a v3 PKESK packet of a composite scheme is consistent with the symmetric algorithm octet the packet carries in the clear: the algorithm MUST be an AES variant and the key length MUST match it (RFC 9980 sec.
    static byte[]
    deriveKEK(PGPDigestCalculator sha3_256, byte[] mlkemKeyShare, byte[] ecdhKeyShare, byte[] ecdhCipherText, byte[] ecdhPublicKey, int algorithmId)
    Compute the key encryption key of the composite scheme (RFC 9980 sec.
    static int
    Return the offset of the symmetric algorithm octet in the algorithm-specific field of a v3 PKESK packet of a composite scheme: just past the two ciphertexts and the size octet.
    static boolean
    isAllowedV3PKESKSymmetricAlgorithm(int symmetricKeyAlgorithm)
    Return true if the symmetric algorithm may be named by a v3 PKESK packet of a composite scheme.
    static byte[][]
    parseEncryptedSessionKey(byte[] enc, int algorithmId, boolean containsSKAlg)
    Parse the algorithm-specific field of a PKESK packet of a composite scheme (RFC 9980 sec.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DOM_SEP

      public static final byte[] DOM_SEP
      The domain separation constant of the key combiner (RFC 9980 sec. 4.2.1): the UTF-8 encoding of "OpenPGPCompositeKDFv1".
    • KEY_WRAP_ALGORITHM

      public static final int KEY_WRAP_ALGORITHM
      The symmetric algorithm the composite schemes wrap the session key with (RFC 9980 sec. 4.2): AES-256 key wrap as per RFC 3394.
      See Also:
  • Method Details

    • deriveKEK

      public static byte[] deriveKEK(PGPDigestCalculator sha3_256, byte[] mlkemKeyShare, byte[] ecdhKeyShare, byte[] ecdhCipherText, byte[] ecdhPublicKey, int algorithmId) throws PGPException
      Compute the key encryption key of the composite scheme (RFC 9980 sec. 4.2.1):
      KEK = SHA3-256(mlkemKeyShare || ecdhKeyShare || ecdhCipherText || ecdhPublicKey || algId || domSep || len(domSep))
      
      Parameters:
      sha3_256 - a fresh digest calculator for HashAlgorithmTags.SHA3_256
      mlkemKeyShare - the ML-KEM key share (shared secret key) as an octet string
      ecdhKeyShare - the ECDH key share as an octet string
      ecdhCipherText - the ECDH ciphertext (the ephemeral public key) as an octet string
      ecdhPublicKey - the ECDH public key of the recipient as an octet string
      algorithmId - the OpenPGP algorithm ID of the public key encryption algorithm
      Returns:
      the 32 octet KEK
      Throws:
      PGPException - if the digest calculator is not SHA3-256 or the KEK cannot be computed
    • isAllowedV3PKESKSymmetricAlgorithm

      public static boolean isAllowedV3PKESKSymmetricAlgorithm(int symmetricKeyAlgorithm)
      Return true if the symmetric algorithm may be named by a v3 PKESK packet of a composite scheme. RFC 9980 sec. 4.3.1 restricts it to AES-128, AES-192 and AES-256.
      Parameters:
      symmetricKeyAlgorithm - symmetric key algorithm ID
      Returns:
      true for the three AES key sizes
    • checkV3SessionKey

      public static void checkV3SessionKey(int symmetricKeyAlgorithm, byte[] sessionKey) throws PGPException
      Check that a session key unwrapped from a v3 PKESK packet of a composite scheme is consistent with the symmetric algorithm octet the packet carries in the clear: the algorithm MUST be an AES variant and the key length MUST match it (RFC 9980 sec. 4.3.1).
      Parameters:
      symmetricKeyAlgorithm - symmetric key algorithm ID from the PKESK packet
      sessionKey - the unwrapped session key
      Throws:
      PGPException - if the algorithm is not AES or the key length does not match it
    • parseEncryptedSessionKey

      public static byte[][] parseEncryptedSessionKey(byte[] enc, int algorithmId, boolean containsSKAlg) throws PGPException
      Parse the algorithm-specific field of a PKESK packet of a composite scheme (RFC 9980 sec. 4.3.1):
      ecdhCipherText || mlkemCipherText || len(C, symAlgId) (|| symAlgId) || C
      
      where the one-octet symAlgId is present only in a v3 packet.
      Parameters:
      enc - the PKESK's encrypted session key data
      algorithmId - the public key algorithm ID of the packet, which fixes the two ciphertext lengths
      containsSKAlg - whether the packet version carries the symmetric-key algorithm octet (v3)
      Returns:
      a four-element array of the ECDH ciphertext, the ML-KEM ciphertext, the wrapped session key C, and the symmetric algorithm octet (a one-element array for a v3 packet, empty otherwise)
      Throws:
      PGPException - if the data is shorter than its declared lengths require
    • getV3SymmetricAlgorithmOffset

      public static int getV3SymmetricAlgorithmOffset(int algorithmId)
      Return the offset of the symmetric algorithm octet in the algorithm-specific field of a v3 PKESK packet of a composite scheme: just past the two ciphertexts and the size octet.
      Parameters:
      algorithmId - the public key algorithm ID of the packet
      Returns:
      offset of the symmetric algorithm octet