public class CipherSpi
extends javax.crypto.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 and Description |
|---|
CipherSpi() |
| Modifier and Type | Method and Description |
|---|---|
protected byte[] |
engineDoFinal(byte[] input,
int inputOffset,
int inputLen) |
protected int |
engineDoFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset) |
protected int |
engineGetBlockSize() |
protected byte[] |
engineGetIV() |
protected int |
engineGetKeySize(java.security.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. 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.
|
protected int |
engineGetOutputSize(int inputLen) |
protected java.security.AlgorithmParameters |
engineGetParameters() |
protected void |
engineInit(int opmode,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random) |
protected void |
engineInit(int opmode,
java.security.Key key,
java.security.AlgorithmParameters params,
java.security.SecureRandom random) |
protected void |
engineInit(int opmode,
java.security.Key key,
java.security.SecureRandom random) |
protected void |
engineSetMode(java.lang.String modeName) |
protected void |
engineSetPadding(java.lang.String padding) |
protected byte[] |
engineUpdate(byte[] input,
int inputOffset,
int inputLen) |
protected int |
engineUpdate(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset) |
protected void engineSetMode(java.lang.String modeName)
throws java.security.NoSuchAlgorithmException
engineSetMode in class javax.crypto.CipherSpijava.security.NoSuchAlgorithmExceptionprotected void engineSetPadding(java.lang.String padding)
throws javax.crypto.NoSuchPaddingException
engineSetPadding in class javax.crypto.CipherSpijavax.crypto.NoSuchPaddingExceptionprotected int engineGetBlockSize()
engineGetBlockSize in class javax.crypto.CipherSpiprotected int engineGetKeySize(java.security.Key key)
throws java.security.InvalidKeyException
engineGetKeySize in class javax.crypto.CipherSpijava.security.InvalidKeyExceptionprotected int engineGetOutputSize(int inputLen)
engineGetOutputSize in class javax.crypto.CipherSpiprotected byte[] engineGetIV()
engineGetIV in class javax.crypto.CipherSpiprotected java.security.AlgorithmParameters engineGetParameters()
engineGetParameters in class javax.crypto.CipherSpiprotected void engineInit(int opmode,
java.security.Key key,
java.security.SecureRandom random)
throws java.security.InvalidKeyException
engineInit in class javax.crypto.CipherSpijava.security.InvalidKeyExceptionprotected void engineInit(int opmode,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
throws java.security.InvalidKeyException,
java.security.InvalidAlgorithmParameterException
engineInit in class javax.crypto.CipherSpijava.security.InvalidKeyExceptionjava.security.InvalidAlgorithmParameterExceptionprotected void engineInit(int opmode,
java.security.Key key,
java.security.AlgorithmParameters params,
java.security.SecureRandom random)
throws java.security.InvalidKeyException,
java.security.InvalidAlgorithmParameterException
engineInit in class javax.crypto.CipherSpijava.security.InvalidKeyExceptionjava.security.InvalidAlgorithmParameterExceptionprotected byte[] engineUpdate(byte[] input,
int inputOffset,
int inputLen)
engineUpdate in class javax.crypto.CipherSpiprotected int engineUpdate(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
engineUpdate in class javax.crypto.CipherSpiprotected byte[] engineDoFinal(byte[] input,
int inputOffset,
int inputLen)
throws javax.crypto.IllegalBlockSizeException,
javax.crypto.BadPaddingException
engineDoFinal in class javax.crypto.CipherSpijavax.crypto.IllegalBlockSizeExceptionjavax.crypto.BadPaddingExceptionprotected int engineDoFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
throws javax.crypto.ShortBufferException,
javax.crypto.IllegalBlockSizeException,
javax.crypto.BadPaddingException
engineDoFinal in class javax.crypto.CipherSpijavax.crypto.ShortBufferExceptionjavax.crypto.IllegalBlockSizeExceptionjavax.crypto.BadPaddingException