Class CipherSpi
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).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]engineDoFinal(byte[] input, int inputOffset, int inputLen) protected intengineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) protected intprotected byte[]protected intengineGetKeySize(Key key) The size of an SM9 key: the 256 bits of the curve the scheme is defined on, as the EC ciphers answer with their curve's field size.protected intengineGetOutputSize(int inputLen) protected AlgorithmParametersprotected voidengineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) protected voidengineInit(int opmode, Key key, SecureRandom random) protected voidengineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) protected voidengineSetMode(String modeName) protected voidengineSetPadding(String padding) protected byte[]engineUpdate(byte[] input, int inputOffset, int inputLen) protected intengineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) Methods inherited from class CipherSpi
engineDoFinal, engineUnwrap, engineUpdate, engineUpdateAAD, engineUpdateAAD, engineWrap
-
Constructor Details
-
CipherSpi
public CipherSpi()
-
-
Method Details
-
engineSetMode
- Specified by:
engineSetModein classCipherSpi- Throws:
NoSuchAlgorithmException
-
engineSetPadding
- Specified by:
engineSetPaddingin classCipherSpi- Throws:
NoSuchPaddingException
-
engineGetBlockSize
protected int engineGetBlockSize()- Specified by:
engineGetBlockSizein classCipherSpi
-
engineGetKeySize
The size of an SM9 key: the 256 bits of the curve the scheme is defined on, as the EC ciphers answer with their curve's field size. javax.crypto.Cipher asks for it when a restricted crypto policy caps the key sizes it allows, and javax.crypto.CipherSpi's default threw UnsupportedOperationException, which escaped Cipher.init in place of the InvalidKeyException it declares.- Overrides:
engineGetKeySizein classCipherSpi- Throws:
InvalidKeyException
-
engineGetOutputSize
protected int engineGetOutputSize(int inputLen) - Specified by:
engineGetOutputSizein classCipherSpi
-
engineGetIV
protected byte[] engineGetIV()- Specified by:
engineGetIVin classCipherSpi
-
engineGetParameters
- Specified by:
engineGetParametersin classCipherSpi
-
engineInit
- Specified by:
engineInitin classCipherSpi- Throws:
InvalidKeyException
-
engineInit
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException - Specified by:
engineInitin classCipherSpi- Throws:
InvalidKeyExceptionInvalidAlgorithmParameterException
-
engineInit
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException - Specified by:
engineInitin classCipherSpi- Throws:
InvalidKeyExceptionInvalidAlgorithmParameterException
-
engineUpdate
protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) - Specified by:
engineUpdatein classCipherSpi
-
engineUpdate
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) - Specified by:
engineUpdatein classCipherSpi
-
engineDoFinal
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException - Specified by:
engineDoFinalin classCipherSpi- Throws:
IllegalBlockSizeExceptionBadPaddingException
-
engineDoFinal
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException - Specified by:
engineDoFinalin classCipherSpi- Throws:
ShortBufferExceptionIllegalBlockSizeExceptionBadPaddingException
-