KEYCTL_DH_COMPUTE(3)

πŸ’– KEYCTL_DH_COMPUTE(3) β€” Linux Key Management Calls πŸ’–

okay so bestie, welcome to the cryptography corner of the keyutils library 🧚✨ this is keyctl_dh_compute and keyctl_dh_compute_kdf from the Linux Key Management Calls family. it's giving math girlboss energy, it's giving secret-sharing, and honestly? obsessed. πŸŽ€


🌈 NAME 🌈

  • keyctl_dh_compute β€” Compute a Diffie-Hellman shared secret or public key πŸ’…
  • keyctl_dh_compute_kdf β€” Derive key from a Diffie-Hellman shared secret ✨

πŸ“œ SYNOPSIS πŸ“œ

#include <keyutils.h>

long keyctl_dh_compute(key_serial_t private, key_serial_t prime,
key_serial_t base, char *buffer, size_t buflen);

long keyctl_dh_compute_alloc(key_serial_t private,
key_serial_t prime, key_serial_t base, void **_buffer);

long keyctl_dh_compute_kdf(key_serial_t private, key_serial_t prime,
key_serial_t base, char *hashname, char *otherinfo,
size_t otherinfolen, char *buffer, size_t buflen);

✨ DESCRIPTION ✨

so keyctl_dh_compute() computes a Diffie-Hellman public key or a shared secret. the computation it's doing is literally this, no cap:

base ^ private ( mod prime )

when base is a key containing the shared generator value, the remote public key gets computed. when base is a key containing the remote public key, you get the shared secret. pick your fighter 🫢

base, private, and prime must all refer to user-type keys containing the parameters for the computation. and every single one of those keys has to grant the caller read permission or it's a no-go, sorry bestie πŸ™ˆ

buffer and buflen specify the buffer where the computed result gets dropped. and here's the cute trick: buflen may be zero, in which case the buffer is not used and the minimum buffer length is fetched instead. window shopping, basically πŸ’…

keyctl_dh_compute_alloc() is the same vibe as keyctl_dh_compute() except it allocates a buffer big enough to hold the payload data and places the data in it. if it succeeds, a pointer to the buffer is placed in *_buffer. and the caller must free the buffer β€” that's your homework, don't leave it in the group chat 😭

keyctl_dh_compute_kdf() derives a key from a Diffie-Hellman shared secret according to the protocol specified in SP800-56A. the Diffie-Hellman computation is based on the same primitives as discussed for keyctl_dh_compute(). same energy, extra steps ✨

to implement the protocol of SP800-56A, base is a key containing the remote public key, so it computes the Diffie-Hellman shared secret. that shared secret then gets post-processed with a key derivation function. glow-up era 🌸

hashname specifies the Linux kernel crypto API name for a hash that shall be used for the key derivation function, such as sha256. hashname must be a NULL terminated string. and ooh β€” check /proc/crypto for available hashes on the system. iykyk πŸ”

following the specification of SP800-56A section 5.8.1.2, the otherinfo parameter may be provided. the format of the OtherInfo field is defined by the caller. the caller may also specify NULL as a valid argument when no OtherInfo data shall be processed. the length of the otherinfo parameter is specified with otherinfolen and is restricted to a maximum length by the kernel. respect the limits, queen πŸ‘‘

the KDF returns the requested number of bytes specified with the genlen or the buflen parameter depending on the invoked function.

buffer and buflen specify the buffer into which the computed result will be placed. πŸŽ€


🫢 RETURN VALUE 🫢

on success, keyctl_dh_compute() returns the amount of data placed into the buffer when buflen is non-zero. when buflen is zero, the minimum buffer length to hold the data is returned. very "tell me how much space i need first" of it ⭐

on success, keyctl_dh_compute_alloc() returns the amount of data in the buffer.

on error, both functions set errno to an appropriate code and return the value -1. πŸ’€


πŸ’… ERRORS πŸ’…

  • ENOKEY β€” One of the keys specified is invalid or not readable. girl, that key is not invited to the party 🚫
  • EINVAL β€” The buffer pointer is invalid or buflen is too small.
  • EOPNOTSUPP β€” One of the keys was not a valid user key. wrong key type, babe 😭
  • EMSGSIZE β€” When using keyctl_dh_compute_kdf(), the size of either otherinfolen or buflen is too big. that's too much, bestie, scale it back πŸ“

πŸ”— LINKING πŸ”—

this is a library function that can be found in libkeyutils. when linking, -lkeyutils should be specified to the linker. don't forget it or the linker will simply not know her 🫢


πŸ‘€ SEE ALSO πŸ‘€


and that's the whole page bestie, you ate that πŸ’– stay iconic, keep your shared secrets secret, and remember β€” this one comes from Linux, dated 07 Apr 2016. periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from