PKCS12_PBE_KEYIVGEN(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… PKCS12_PBE_KEYIVGEN β€” okay bestie, let's talk PKCS#12 password based encryption

So this is the 3ossl girlies' guide to PKCS12_PBE_keyivgen, PKCS12_PBE_keyivgen_ex, PKCS12_pbe_crypt, and PKCS12_pbe_crypt_ex β€” the whole squad that handles PKCS#12 Password based encryption. Literally the "turn a password into a key" department. ✨


πŸŽ€ SYNOPSIS

#include <openssl/evp.h>

int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
                        ASN1_TYPE *param, const EVP_CIPHER *cipher,
                        const EVP_MD *md_type, int en_de);
int PKCS12_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
                           ASN1_TYPE *param, const EVP_CIPHER *cipher,
                           const EVP_MD *md_type, int en_de,
                           OSSL_LIB_CTX *libctx, const char *propq);
unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
                                const char *pass, int passlen,
                                const unsigned char *in, int inlen,
                                unsigned char **data, int *datalen,
                                int en_de);
unsigned char *PKCS12_pbe_crypt_ex(const X509_ALGOR *algor,
                                   const char *pass, int passlen,
                                   const unsigned char *in, int inlen,
                                   unsigned char **data, int *datalen,
                                   int en_de, OSSL_LIB_CTX *libctx,
                                   const char *propq);

πŸ’– DESCRIPTION

PKCS12_PBE_keyivgen() and PKCS12_PBE_keyivgen_ex() are the key-derivation besties 🫢 They take a password pass of length passlen, parameters param, and a message digest function md_type, and they do a key derivation according to PKCS#12. Then that shiny new key gets used to initialise the cipher context ctx with a cipher cipher, either for encryption (en_de=1) or decryption (en_de=0). Encrypt girlboss or decrypt girlboss, your call πŸ’…

PKCS12_PBE_keyivgen_ex() is the upgraded version β€” it also lets the application specify a library context libctx and a property query propq to select the appropriate algorithm implementations. Fancy. πŸ‘‘

PKCS12_pbe_crypt() and PKCS12_pbe_crypt_ex() will encrypt or decrypt a whole buffer based on the algorithm in algor and the password pass of length passlen. The input comes from in of length inlen, and the output goes into a malloc'd buffer returned in *data of length datalen. What's it doing? Depends on en_de: encryption (en_de=1) or decryption (en_de=0). Choose your fighter βš”οΈ

PKCS12_pbe_crypt_ex() again β€” the ex one β€” lets the application specify a library context libctx and property query propq to select appropriate algorithm implementations. Same glow-up, different function ✨

Now the parameter lore, because these ones lowkey matter:

  • pass is the password used in the derivation, of length passlen. It's an optional parameter and can be NULL. And if passlen is -1, the function will calculate the length of pass using strlen(). Convenient! πŸŽ€
  • salt is the salt used in the derivation, of length saltlen. If the salt is NULL, then saltlen must be 0. Heads up though: the function will not attempt to calculate the length of the salt, because it is not assumed to be NULL terminated. So don't try it πŸ™ˆ
  • iter is the iteration count, and its value should be greater than or equal to 1. RFC 2898 suggests an iteration count of at least 1000. But listen β€” any iter less than 1 is treated as a single iteration. So yeah, it forgives you, but at what cost πŸ’€
  • digest is the message digest function used in the derivation.

And one last thing, fr fr: functions ending in _ex() take optional parameters libctx and propq, which are used to select appropriate algorithm implementations.


πŸ¦‹ NOTES

These functions are typically used in PKCS#12 to encrypt objects. That's their whole ~main character~ thing.

Also super important: these functions make no assumption regarding the given password. It will simply be treated as a byte sequence. Like, no judgement, no vibes check, just raw bytes. Beautiful. 😌


🌸 RETURN VALUES

  • PKCS12_PBE_keyivgen(), PKCS12_PBE_keyivgen_ex() return 1 on success or 0 on error.
  • PKCS12_pbe_crypt() and PKCS12_pbe_crypt_ex() return a buffer containing the output, or NULL if an error occurred. (Check that pointer, bestie, always 🫢)

πŸ‘‘ CONFORMING TO

IETF RFC 7292 (https://tools.ietf.org/html/rfc7292) β€” the source material, the blueprint, the origin story πŸ“œ


🌈 SEE ALSO

Go read those too, iykyk πŸ’œ


⭐ HISTORY

PKCS12_PBE_keyivgen_ex() and PKCS12_pbe_crypt_ex() were added in OpenSSL 3.0. So the _ex girlies are the newer arrivals β€” respect the timeline ✨


Copyright 2014-2021 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.


okay that's the whole page, bestie β€” you ate that πŸ’… This little love letter to PKCS12_PBE_KEYIVGEN(3ossl) came straight from OpenSSL version 3.5.0, dated 2026-09-25. Mwah, go forth and derive keys responsibly πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from