ExCEP.Crypto.SM4 (ex_cep v0.1.1)

Copy Markdown

Pure Elixir implementation of the Chinese national block cipher SM4 (GB/T 32907-2016).

Supports:

Implements ExCEP.Transport.Cipher for CSG CEP TS6.3 encrypted channels.

Summary

Functions

Encrypts or decrypts binary data using SM4-CTR mode.

Decrypts a single 16-byte block with a 16-byte SM4 key.

Decrypts SM4-CBC ciphertext with PKCS#7 unpadding.

Encrypts plaintext with SM4.

Encrypts a single 16-byte block with a 16-byte SM4 key.

Encrypts binary plaintext using SM4-CBC with PKCS#7 padding.

Functions

crypt_ctr(data, key, arg)

@spec crypt_ctr(binary(), <<_::128>>, <<_::128>>) :: binary()

Encrypts or decrypts binary data using SM4-CTR mode.

decrypt_block(block, key)

@spec decrypt_block(<<_::128>>, <<_::128>>) :: <<_::128>>

Decrypts a single 16-byte block with a 16-byte SM4 key.

decrypt_cbc(ciphertext, key, iv)

@spec decrypt_cbc(binary(), <<_::128>>, <<_::128>>) ::
  {:ok, binary()} | {:error, term()}

Decrypts SM4-CBC ciphertext with PKCS#7 unpadding.

encrypt(plaintext, opts)

Encrypts plaintext with SM4.

Options

  • :key - 16-byte SM4 key (required)
  • :mode - :cbc (default, prepends 16-byte random IV) or :ctr (prepends 16-byte random nonce)

encrypt_block(block, key)

@spec encrypt_block(<<_::128>>, <<_::128>>) :: <<_::128>>

Encrypts a single 16-byte block with a 16-byte SM4 key.

encrypt_cbc(plaintext, key, iv)

@spec encrypt_cbc(binary(), <<_::128>>, <<_::128>>) :: binary()

Encrypts binary plaintext using SM4-CBC with PKCS#7 padding.