KCAPI_CIPHER_ENC_SM4(3)

πŸ’– KCAPI_CIPHER_ENC_SM4 β€” bestie edition πŸ’–

okay so this is a page from the libkcapi manual (section 3, the library-functions girlies ✨), and we're talking about kcapi_cipher_enc_sm4_ctr. yes that's a mouthful. no you don't have to say it out loud. let's get into it 🫢


✨ NAME ✨

kcapi_cipher_enc_sm4_ctr β€” the convenience function for SM4 CTR encryption. it's giving "i do one thing and i do it well" πŸ’…


🌸 SYNOPSIS 🌸

ssize_t kcapi_cipher_enc_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);

that's the whole vibe. one call, seven arguments, zero drama 🌈


πŸŽ€ ARGUMENTS πŸŽ€

  • key β€” [in] the key buffer. this is the secret, guard it with your life 🫣
  • keylen β€” [in] length of the key buffer.
  • in β€” [in] the plaintext data buffer. the raw tea before it gets wrapped up β˜•
  • inlen β€” [in] length of the in buffer.
  • ctr β€” [in] the start counter value to be used for the cipher operation. yes bestie, counter mode needs a counter, shocking i know πŸ’€
  • out β€” [out] the ciphertext data buffer. where the glow-up gets written ✨
  • outlen β€” [in] length of the out buffer.

πŸ’œ DESCRIPTION πŸ’œ

so what does this little icon actually do? it performs an SM4 counter mode encryption operation using the key you handed over, the input buffer you gave it, and the IV you provided. the result gets stored in the out buffer. periodt. πŸ”₯

the input buffer can be of arbitrary length. go off, no length restrictions here πŸ’ƒ

the output buffer must be at least as large as the input buffer. this is not a suggestion, this is the law, and the law is not playing πŸ™ˆ

the start counter can contain all zeros β€” and yes bestie, that means actual zero bytes, not a NULL buffer! β€” and it must be exactly 16 bytes in size. exactly. sixteen. no more, no less, we're not doing guesswork πŸ’…

the SM4 key is fixed 16 bytes long. that's it. that's the number. don't come at it with 15 or 17, it will simply not slay. 😭

and when you call it: it returns the number of bytes generated upon success, or a negative errno-style error code if an error occurred. so if you see a negative number, that's not a flex, that's a problem, go read your errno 🚨

πŸ‘‘ AUTHOR πŸ‘‘

Stephan Mueller <smueller@chronox.de>

Author. the main character who wrote all of this, we stan 🫢

this page lives under the libkcapi Manual, version 1.5.0, dated July 2025 β€” and that's the whole page bestie, you read it, you understood it, you're basically a cryptographer now πŸ’–πŸ¦‹βœ¨ stay encrypted, stay iconic, mwah πŸŽ€

πŸ“¦ where this page comes from