DH_SET_METHOD(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

πŸ’– NAME πŸ’–

Okay so this is DH_SET_METHOD(3) from the Library Functions Manual, and it's literally all about picking a DH method, bestie. The crew is DH_set_default_method, DH_get_default_method, DH_set_method, DH_new_method, DH_OpenSSL β€” select DH method. Periodt πŸ’…


🌸 SYNOPSIS 🌸

Here's the exact shape of it, no cap:

library β€œlibcrypto” #include <openssl/dh.h>

void
DH_set_default_method(const DH_METHOD *meth);

const DH_METHOD *
DH_get_default_method(void);

int
DH_set_method(DH *dh, const DH_METHOD *meth);

DH *
DH_new_method(ENGINE *engine);

const DH_METHOD *
DH_OpenSSL(void);

πŸ¦‹ DESCRIPTION πŸ¦‹

A DH_METHOD object is basically a little bundle of function pointers used for Diffie-Hellman operations. By default, the internal implementation returned by DH_OpenSSL() is used. But if you select another method, alternative implementations such as hardware accelerators may be used β€” and that's the whole point, iykyk ✨

  • DH_set_default_method() β€” selects meth as the default method for all DH structures created later. Set it and forget it, bestie.
  • DH_get_default_method() β€” returns a pointer to the current default method. It's giving "who's the main character right now" energy.
  • DH_set_method() β€” selects meth to perform all operations using the key dh. This replaces the DH_METHOD used by the dh key. It is possible to have DH keys that only work with certain DH_METHOD implementations, and in such cases attempting to change the DH_METHOD for the key can have unexpected results.
  • DH_new_method() β€” allocates and initializes a DH structure. The engine argument is ignored and the default method controlled by DH_set_default_method() is used.
⚠️ Heads up, bestie: some DH keys only work with certain DH_METHOD implementations, and if you try to change the DH_METHOD for one of those keys, expect unexpected results. No cap, that's literally what the page says 😭

The DH_METHOD structure is defined as follows:

       typedef struct dh_meth_st
       {
            /* name of the implementation */
               const char *name;

            /* generate private and public DH values for key agreement */
               int (*generate_key)(DH *dh);

            /* compute shared secret */
               int (*compute_key)(unsigned char *key, BIGNUM *pub_key, DH *dh);

            /* compute r = a ^ p mod m (May be NULL for some implementations) */
               int (*bn_mod_exp)(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
                                       const BIGNUM *m, BN_CTX *ctx,
                                       BN_MONT_CTX *m_ctx);

            /* called at DH_new */
               int (*init)(DH *dh);

            /* called at DH_free */
               int (*finish)(DH *dh);

               int flags;

               char *app_data; /* ?? */

       } DH_METHOD;

πŸŽ€ RETURN VALUES πŸŽ€

  • DH_OpenSSL() and DH_get_default_method() β€” return pointers to the respective DH_METHOD. Iconic behavior, fr fr.
  • DH_set_method() β€” returns 1 on success or 0 on failure. Currently, it cannot fail. Slay.
  • DH_new_method() β€” returns NULL and sets an error code that can be obtained by ERR_get_error(3) if the allocation fails. Otherwise it returns a pointer to the newly allocated structure. Lowkey important, ngl.

🌈 SEE ALSO 🌈

  • DH_new(3) β€” go read it, bestie 🫢

⭐ HISTORY ⭐

DH_set_default_method(), DH_get_default_method(), DH_set_method(), DH_new_method() and DH_OpenSSL() first appeared in OpenSSL 0.9.5 and have been available since OpenBSD 2.7. Vintage but still iconic πŸ‘‘


Signed, sealed, delivered by Debian on June 8, 2025 β€” page tag DH_SET_METHOD(3) πŸ’…βœ¨

πŸ“¦ where this page comes from