sm4_setkey(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’œ sm4_setkey(9) β€” like, the SM4 key setter, bestie

okay so this is a page from the Kernel API Manual and it's all about one little function that sets your SM4 key. it's short, it's sweet, it's giving "small but mighty" energy, and yes we're gonna go through every single detail together because that's just how we do things around here 🫢


✨ SYNOPSIS ✨

int sm4_setkey (struct crypto_tfm *tfm , const u8 *in_key , unsigned int
key_len );

like, that's the whole vibe. three args, one int back. slay πŸ’…


πŸŽ€ ARGUMENTS πŸŽ€

  • tfm β€” the crypto_tfm that's being used in the context. it's the main character here, honestly πŸ‘‘
  • in_key β€” the input key. the actual key. the one you're setting. no cap πŸ”‘
  • key_len β€” the size of the key. it matters. it really matters (more on that in a sec 😭)

🌸 DESCRIPTION 🌸

so what does it actually do? it calls sm4_expandkey to expand the key. that's it, that's the job. iconic behaviour honestly ✨

but here's the part where you gotta pay attention, bestie: sm4_ctx _must_ be the private data embedded in tfm, and you get that private data by retrieving it with crypto_tfm_ctx. like, that's not optional. it's a must. capital-M Must. the emphasis is theirs, not mine πŸ™ˆ

so the flow is: grab the ctx from the tfm via crypto_tfm_ctx, and that ctx has to be a sm4_ctx living as the private data embedded in tfm. then the key expansion does its thing. periodt πŸ’€


⭐ RETURN ⭐

  • 0 on success β€” we love a happy ending πŸ’–
  • -EINVAL on failure β€” and ngl this only happens for bad key lengths, so if you're getting this it's a key_len situation, not a you situation 🫢

πŸ¦‹ SEE ALSO πŸ¦‹

  • Kernel file ./crypto/sm4_generic.c β€” go read it, it's right there, iykyk 🧚

lowkey a reminder: bad key lengths are the only failure mode listed here, so double-check that key_len before you come crying to the error code 😭

and that's the page!! 🌈 this little gem is from ./crypto, dated October 2026, and it's sm4_setkey(9) β€” go forth and set those keys bestie, you're gonna do amazing πŸŽ€πŸ’œπŸ”₯

πŸ“¦ where this page comes from