EVP_PKEY_KEYGEN(3ossl)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’– EVP_PKEY_KEYGEN πŸ’–

okay so this is the 3ossl girlies' guide to key generation in OpenSSL β€” like, the whole keygen-and-paramgen-and-callbacks situation, explained by someone who actually reads the source (me). buckle up bestie πŸ’…

✨ NAME ✨

EVP_PKEY_Q_keygen, EVP_PKEY_keygen_init, EVP_PKEY_paramgen_init, EVP_PKEY_generate, EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb, EVP_PKEY_CTX_get_keygen_info, EVP_PKEY_CTX_set_app_data, EVP_PKEY_CTX_get_app_data, EVP_PKEY_gen_cb, EVP_PKEY_paramgen, EVP_PKEY_keygen β€” literally all the key and parameter generation and check functions in one big iconic squad. πŸŽ€


🌸 SYNOPSIS 🌸

#include <openssl/evp.h>

EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
                            const char *type, ...);

int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
int EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);

typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);

void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);

int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);

void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);

πŸ¦‹ DESCRIPTION πŸ¦‹

so like. generating keys is sometimes straightforward β€” you just generate the key's numbers and you're done, slay, no notes. 🫢 but then there are key types that need key parameters too (people call them "domain parameters" but it's not strictly limited to that, iykyk), and those parameters also have to be generated. AND on top of that, the caller might wanna set their own user-provided generation parameters that further affect key parameter generation or key generation β€” like, say, the desired key size. πŸ’…

to flexibly allow all that, key parameter generation and key generation are split into three little acts: 🌟

  1. an initialization of a key algorithm context,
  2. functions to set user-provided parameters,
  3. and finally the key parameter or key generation function itself.

the key algorithm context has to be created with EVP_PKEY_CTX_new(3) or its variants β€” go read that manual for the deets, bestie.

EVP_PKEY_keygen_init() initializes a public key algorithm context ctx for a key generation operation. ✨

EVP_PKEY_paramgen_init() is basically EVP_PKEY_keygen_init()'s sister except key parameters get generated instead of a key.

after initialization, you can pass generation parameters with EVP_PKEY_CTX_ctrl(3) or EVP_PKEY_CTX_set_params(3), or literally any other function described in those manuals. go wild, but like, read them first.

EVP_PKEY_generate() performs the generation operation, and the resulting key parameters or key get written to *ppkey. πŸŽ€ and here's the cute part: if *ppkey is NULL when you call this function, it'll get allocated for you, and you (the caller) should free it with EVP_PKEY_free(3) when it's no longer useful. don't leave it hanging, that's not the vibe. 😭

EVP_PKEY_paramgen() and EVP_PKEY_keygen() do exactly the same thing as EVP_PKEY_generate(), but only after checking that the corresponding EVP_PKEY_paramgen_init() or EVP_PKEY_keygen_init() was used to initialize ctx. these are the older functions, kept around for backward compatibility β€” it is safe to use EVP_PKEY_generate() instead. periodt. πŸ‘‘

the function EVP_PKEY_set_cb() sets the key or parameter generation callback to cb. and EVP_PKEY_CTX_get_cb() returns the key or parameter generation callback. πŸ”

EVP_PKEY_CTX_get_keygen_info() returns parameters associated with the generation operation. if idx is -1, you get the total number of parameters available back. any non-negative value returns the value of that parameter. also, EVP_PKEY_CTX_gen_keygen_info() with a nonnegative value for idx should only be called within the generation callback β€” that's a whole boundary thing, respect it. πŸ™ˆ

if the callback returns 0, the key generation operation is aborted and an error occurs. lowkey relatable β€” this is what happens during a time-consuming operation when the user clicks a "cancel" button. πŸ’€

EVP_PKEY_CTX_set_app_data() and EVP_PKEY_CTX_get_app_data() associate an opaque, application-defined pointer with an EVP_PKEY_CTX object. 🧚

that pointer is not interpreted by the library and is reserved entirely for use by your application. you can stash whatever arbitrary context or state you want in there, as long as it needs to be accessible wherever the corresponding EVP_PKEY_CTX is around. it's giving ✨personal storage locker✨.

EVP_PKEY_Q_keygen() abstracts away the explicit EVP_PKEY_CTX use while giving you a 'quick' but limited way of generating a new asymmetric key pair. it provides shorthands for simple and common cases of key generation. as usual, the library context libctx and property query propq can be given for fetching algorithms from providers. 🌈

so, the type situation:

  • if type is "RSA", a size_t parameter must be given to specify the size of the RSA key. πŸ”’
  • if type is "EC", a string parameter must be given to specify the name of the EC curve. 🌊
  • if type is: "ED25519", "ED448", "SM2", "X25519", "X448", "ML-DSA-44", "ML-DSA-65", "ML-DSA-87", "ML-KEM-512", "ML-KEM-768", or "ML-KEM-1024" β€” no further parameters are needed. ate. πŸ’…

other key types may be possible if they're supplied by the loaded providers. EVP_PKEY_Q_keygen() may be usable with such key types as long as they do not require further parameters. πŸ‘‘


πŸ’œ RETURN VALUES πŸ’œ

EVP_PKEY_keygen_init(), EVP_PKEY_paramgen_init(), EVP_PKEY_keygen() and EVP_PKEY_paramgen() return 1 for success and 0 or a negative value for failure. and heads up: a return value of -2 in particular indicates the operation is not supported by the public key algorithm. 😀

EVP_PKEY_Q_keygen() returns an EVP_PKEY, or NULL on failure. 🫠


⭐ NOTES ⭐

after you call EVP_PKEY_keygen_init() or EVP_PKEY_paramgen_init(), you can perform algorithm-specific control operations to set any appropriate parameters for the operation. πŸŽ€

EVP_PKEY_keygen() and EVP_PKEY_paramgen() can be called more than once on the same context if you're doing several operations with the same parameters. efficient queen. πŸ‘‘

the meaning of the parameters passed to the callback depends on the algorithm and the specific implementation of that algorithm. some might not give any useful information at all during key or parameter generation. others might not even call the callback. like, at all. ghosting you fr fr. πŸ’€

the operation performed by key generation or parameter generation depends on the algorithm used. in some cases (e.g. EC with a supplied named curve) the "generation" option merely sets the appropriate fields in an EVP_PKEY structure. no big dramatic computation, just field-setting. πŸ¦‹

in OpenSSL, an EVP_PKEY structure containing a private key also contains the public key components and parameters (if any). so an OpenSSL private key is equivalent to what some libraries call a "key pair" β€” yes, it's the whole package. and a private key can be used in functions which require the use of a public key or parameters. 🌸


πŸ”₯ EXAMPLES πŸ”₯

generate a 2048 bit RSA key:

#include <openssl/evp.h>
#include <openssl/rsa.h>

EVP_PKEY_CTX *ctx;
EVP_PKEY *pkey = NULL;

ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (!ctx)
    /* Error occurred */
if (EVP_PKEY_keygen_init(ctx) <= 0)
    /* Error */
if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0)
    /* Error */

/* Generate key */
if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
    /* Error */

generate a key from a set of parameters:

#include <openssl/evp.h>
#include <openssl/rsa.h>

EVP_PKEY_CTX *ctx;
ENGINE *eng;
EVP_PKEY *pkey = NULL, *param;

/* Assumed param, eng are set up already */
ctx = EVP_PKEY_CTX_new(param, eng);
if (!ctx)
    /* Error occurred */
if (EVP_PKEY_keygen_init(ctx) <= 0)
    /* Error */

/* Generate key */
if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
    /* Error */

example of generation callback for OpenSSL public key implementations:

/* Application data is a BIO to output status to */

EVP_PKEY_CTX_set_app_data(ctx, status_bio);

static int genpkey_cb(EVP_PKEY_CTX *ctx)
{
    char c = '*';
    BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
    int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);

    if (p == 0)
        c = '.';
    if (p == 1)
        c = '+';
    if (p == 2)
        c = '*';
    if (p == 3)
        c = '\n';
    BIO_write(b, &c, 1);
    (void)BIO_flush(b);
    return 1;
}

bestie this callback is literally printing progress dots and stars to a BIO, it's adorable. 🌟


πŸŽ€ SEE ALSO πŸŽ€

EVP_RSA_gen(3), EVP_EC_gen(3), EVP_PKEY_CTX_new(3), EVP_PKEY_encrypt(3), EVP_PKEY_decrypt(3), EVP_PKEY_sign(3), EVP_PKEY_verify(3), EVP_PKEY_verify_recover(3), EVP_PKEY_derive(3)

go read those next, you know you want to. πŸ’…


🌈 HISTORY 🌈

EVP_PKEY_keygen_init(), int EVP_PKEY_paramgen_init(), EVP_PKEY_keygen(), EVP_PKEY_paramgen(), EVP_PKEY_gen_cb(), EVP_PKEY_CTX_set_cb(), EVP_PKEY_CTX_get_cb(), EVP_PKEY_CTX_get_keygen_info(), EVP_PKEY_CTX_set_app_data() and EVP_PKEY_CTX_get_app_data() were added in OpenSSL 1.0.0. πŸ•°οΈ

EVP_PKEY_Q_keygen() and EVP_PKEY_generate() were added in OpenSSL 3.0. ✨


Copyright 2006-2026 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


and that's the whole page, bestie!! πŸ’– this was 3.5.6, dated 2026-04-07, and it's filed under EVP_PKEY_KEYGEN(3ossl). now go generate some keys, you absolute icon. 🧚✨ πŸ“

πŸ“¦ where this page comes from