Bouncy Castle Cryptography Library 1.87

org.bouncycastle.jcajce.provider.asymmetric.sm9
Class CipherSpi

java.lang.Object
  extended byjavax.crypto.CipherSpi
      extended byorg.bouncycastle.jcajce.provider.asymmetric.sm9.CipherSpi

public class CipherSpi
extends javax.crypto.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 ( = 1), which gives; the KDF stream mode is selected with the transformation . The transformation's padding is always : 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 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 records the mode, and the mode this was configured with decides - a stream-mode ciphertext is decrypted through a stream-mode (), an SM4-mode one through the default - and a ciphertext whose 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 (, ), 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. 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
CipherSpi()
           
 
Method Summary
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)
           
 
Methods inherited from class javax.crypto.CipherSpi
engineUnwrap, engineWrap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CipherSpi

public CipherSpi()
Method Detail

engineSetMode

protected void engineSetMode(java.lang.String modeName)
                      throws java.security.NoSuchAlgorithmException
Throws:
java.security.NoSuchAlgorithmException

engineSetPadding

protected void engineSetPadding(java.lang.String padding)
                         throws javax.crypto.NoSuchPaddingException
Throws:
javax.crypto.NoSuchPaddingException

engineGetBlockSize

protected int engineGetBlockSize()

engineGetKeySize

protected int engineGetKeySize(java.security.Key key)
                        throws java.security.InvalidKeyException
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.

Throws:
java.security.InvalidKeyException

engineGetOutputSize

protected int engineGetOutputSize(int inputLen)

engineGetIV

protected byte[] engineGetIV()

engineGetParameters

protected java.security.AlgorithmParameters engineGetParameters()

engineInit

protected void engineInit(int opmode,
                          java.security.Key key,
                          java.security.SecureRandom random)
                   throws java.security.InvalidKeyException
Throws:
java.security.InvalidKeyException

engineInit

protected void engineInit(int opmode,
                          java.security.Key key,
                          java.security.spec.AlgorithmParameterSpec params,
                          java.security.SecureRandom random)
                   throws java.security.InvalidKeyException,
                          java.security.InvalidAlgorithmParameterException
Throws:
java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException

engineInit

protected void engineInit(int opmode,
                          java.security.Key key,
                          java.security.AlgorithmParameters params,
                          java.security.SecureRandom random)
                   throws java.security.InvalidKeyException,
                          java.security.InvalidAlgorithmParameterException
Throws:
java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException

engineUpdate

protected byte[] engineUpdate(byte[] input,
                              int inputOffset,
                              int inputLen)

engineUpdate

protected int engineUpdate(byte[] input,
                           int inputOffset,
                           int inputLen,
                           byte[] output,
                           int outputOffset)

engineDoFinal

protected byte[] engineDoFinal(byte[] input,
                               int inputOffset,
                               int inputLen)
                        throws javax.crypto.IllegalBlockSizeException,
                               javax.crypto.BadPaddingException
Throws:
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException

engineDoFinal

protected int engineDoFinal(byte[] input,
                            int inputOffset,
                            int inputLen,
                            byte[] output,
                            int outputOffset)
                     throws javax.crypto.ShortBufferException,
                            javax.crypto.IllegalBlockSizeException,
                            javax.crypto.BadPaddingException
Throws:
javax.crypto.ShortBufferException
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException

Bouncy Castle Cryptography Library 1.87