Class CipherSpi

java.lang.Object
javax.crypto.CipherSpi
org.bouncycastle.jcajce.provider.asymmetric.sm9.CipherSpi

public class CipherSpi extends CipherSpi
JCA Cipher for SM9 public-key encryption (GM/T 0044.4).

Encrypt with the recipient's public key, formed from the published master public key and the recipient's identity via SM9EncMasterPublicKey.getUserPublicKey(byte[]); decrypt with the recipient's private key. The default data-encapsulation mode is SM4/ECB/PKCS#7 (enType = 1), which Cipher.getInstance("SM9") gives; the KDF stream mode is selected with the transformation "SM9/XOR/NoPadding". The transformation's padding is always NoPadding: the SM4 mode's PKCS#7 is part of GM/T 0044.4's data encapsulation, applied inside the mechanism, not a padding the caller chooses, so "SM9/SM4/PKCS7Padding" is refused rather than taken as a second name for the default.

The SM4 mode is ECB with no IV, as in the Chinese edition's GM/T 0044.5-2016 Annex D.1 example. The official English edition and GB/T 38635.2-2020 describe this method differently (CBC with a zero IV; and a 16-byte IV carried at the front of C2), so a peer built from either does not interoperate with it - see SM9Engine.Mode.SM4.

The mode applies to decryption as well as encryption: the ciphertext is a DER SM9Cipher whose enType records the mode, and the mode this Cipher was configured with decides - a stream-mode ciphertext is decrypted through a stream-mode Cipher ("SM9/XOR/NoPadding"), an SM4-mode one through the default - and a ciphertext whose enType disagrees with it is rejected as malformed. Neither mode produces or accepts a 16-byte C2 (see SM9Engine): a stream-mode message of exactly 16 bytes and an SM4-mode message of fewer than 16 bytes are refused on encryption, so the first has to be sent in SM4 mode and the second in stream mode.

Usage warning: an identity's key should be used for this service or for the SM9 KEM (KeyGenerator.SM9-KEM, KEM.SM9-KEM), but not for both. A deployment needing both has its KGC publish a separate hid for each function, as it already does for the key exchange, so that the two keys are distinct. init also refuses for decryption a key declared for the KEM, and one that has been used for it - see SM9EncPrivateKeyParameters.recordUsage(int).

The ciphertext is the GM/T 0080-2020 SM9Cipher structure (see SM9Cipher).