KCAPI_CIPHER_DEC_SM4(3)

πŸ’– kcapi_cipher_dec_sm4(3) πŸ’–

okay bestie, so this is a page from the libkcapi Manual, and it's literally about the convenience function kcapi_cipher_dec_sm4_ctr β€” the SM4 CTR decryption one. yes it's as iconic as it sounds, let's get into it πŸŽ€


✨ NAME ✨

kcapi_cipher_dec_sm4_ctr β€” the convenience function for SM4 CTR decryption. it does exactly what it says on the tin, no cap πŸ’…


🌸 SYNOPSIS 🌸

ssize_t kcapi_cipher_dec_sm4_ctr(const uint8_t * key, uint32_t keylen,
                                 const uint8_t * in, size_t inlen,
                                 const uint8_t * ctr, uint8_t * out,
                                 size_t outlen);

like, seven arguments. she's a lot, but she's worth it 🫢


πŸ¦‹ ARGUMENTS πŸ¦‹

  • key β€” [in] the key buffer, bestie. the actual key.
  • keylen β€” [in] how long that key buffer is.
  • in β€” [in] the ciphertext data buffer. this is what you're decrypting, mwah.
  • inlen β€” [in] length of the in buffer.
  • ctr β€” [in] the start counter value that gets used for the cipher operation.
  • out β€” [out] the plaintext data buffer. this is where the magic lands ✨
  • outlen β€” [in] length of the out buffer.

πŸ’œ DESCRIPTION πŸ’œ

so this convenience function performs an SM4 counter mode encryption operation β€” using the key you handed it, the input buffer you gave it, and the IV you gave it. the output gets stored in the out buffer. that's the whole vibe.

  • The input buffer can be of arbitrary length. no constraints, she's flexible 🌈
  • The output buffer must be at least as large as the input buffer. periodt. don't try to be cute and make it smaller.
  • The start counter can contain all zeros β€” and listen, that is not a NULL buffer, it's a buffer full of zeros, two very different things bestie β€” and it must be exactly 16 bytes in size.
  • The SM4 key is fixed 16 bytes long. 16. not 15, not 17. sixteen πŸ’…

then, the return: it gives you back the number of bytes generated upon success; and if something went wrong, you get a negative errno-style error code. so if the number is negative, that's not a byte count, that's a problem, okay? 😭

ngl the zero-vs-NULL counter thing is the classic gotcha here. all zeros = fine, NULL = absolutely not. πŸ™ˆ

πŸ‘‘ AUTHOR πŸ‘‘

  • Stephan Mueller <smueller@chronox.de> β€” Author.

and that's the whole page, bestie! this was libkcapi Manual 1.5.0, dated July 2025, and you were reading KCAPI_CIPHER_DEC_SM4(3) the entire time πŸ’–βœ¨ go decrypt something cute, ily πŸ«ΆπŸŽ€

πŸ“¦ where this page comes from