目录

Key Encapsulation and Decapsulation Using KEM (ArkTS)

Starting from API version 26.0.0, key encapsulation and decapsulation using the Module-Lattice-Based Key-Encapsulation Mechanism (ML-KEM) algorithm is supported. For the corresponding algorithm specifications, see KEM Encapsulation and Decapsulation Algorithm Specifications.

ML-KEM is a lattice-based post-quantum key encapsulation mechanism used to securely negotiate a shared secret over an insecure channel. The encapsulating party uses the public key to generate a shared secret and an encapsulated key, while the decapsulating party uses the private key and the encapsulated key to recover the same shared secret.

Algorithm Specifications

Algorithm KEM Value API Version
ML-KEM ML_KEM_512 26.0.0+
ML-KEM ML_KEM_768 26.0.0+
ML-KEM ML_KEM_1024 26.0.0+

How to Develop

Encapsulation

  1. Call cryptoFramework.createAsyKeyGenerator and AsyKeyGenerator.generateKeyPair to generate an asymmetric key pair (KeyPair) with the ML-KEM algorithm.

  2. Call cryptoFramework.createKem with the algorithm parameter set to KemAlgNameId.ML_KEM_768 to create a Kem instance.

  3. Call Kem.encapsulate to perform key encapsulation based on the passed-in public key (KeyPair.pubKey), which returns the encapsulation result (KemEncapResult).

    KemEncapResult contains sharedSecret (shared secret) and wrappedKey (encapsulated key). The encapsulating party retains sharedSecret and sends wrappedKey to the decapsulating party. When the ikme parameter is passed as null, the algorithm library internally generates a random ephemeral key.

Decapsulation

  1. Call cryptoFramework.createKem and specify the algorithm parameter as KemAlgNameId.ML_KEM_768 to create a Kem instance.

  2. Call Kem.decapsulate to perform key decapsulation based on the passed-in private key (KeyPair.priKey) and the encapsulated key (wrappedKey), which returns the shared secret.

    The shared secret obtained from decapsulation should be consistent with the shared secret generated during encapsulation.

Development Examples