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 orbuflenis too small.EOPNOTSUPPβ One of the keys was not a valid user key. wrong key type, babe πEMSGSIZEβ When usingkeyctl_dh_compute_kdf(), the size of eitherotherinfolenorbuflenis 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
- alpine 3.22: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- alpine 3.23: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- alpine 3.24: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- arch rolling: keyutils 1.6.3-4 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- centos-stream 10: keyutils-libs-devel 1.6.3-5.el10 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- centos-stream 9: keyutils-libs-devel 1.6.3-1.el9 Β· GPLv2+ and LGPLv2+
- centos-stream 9: keyutils-libs-devel 1.6.3-1.el9 Β· GPLv2+ and LGPLv2+
- debian bookworm: libkeyutils-dev 1.6.3-2 Β· GPL-2.0-or-later
- debian trixie: libkeyutils-dev 1.6.3-6 Β· GPL-2.0-or-later
- fedora 43: keyutils-libs-devel 1.6.3-6.fc43 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 43: keyutils-libs-devel 1.6.3-6.fc43 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 44: keyutils-libs-devel 1.6.3-7.fc44 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 44: keyutils-libs-devel 1.6.3-7.fc44 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- nixpkgs unstable: keyutils 1.6.3
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- ubuntu jammy: libkeyutils-dev 1.6.1-2ubuntu3 Β· GPL-2.0-or-later
- ubuntu noble: libkeyutils-dev 1.6.3-3build1 Β· GPL-2.0-or-later
- ubuntu questing: libkeyutils-dev 1.6.3-6ubuntu2
- ubuntu resolute: libkeyutils-dev 1.6.3-6ubuntu3 Β· GPL-2.0-or-later
- ubuntu stonking: libkeyutils-dev 1.6.3-6ubuntu3 Β· GPL-2.0-or-later
- Copyright (C) 2006 Red Hat, Inc. All Rights Reserved
- Copyright (C) 2016 Intel Corporation. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms