1*b077aed3SPierre Pronchery=pod 2*b077aed3SPierre Pronchery 3*b077aed3SPierre Pronchery=head1 NAME 4*b077aed3SPierre Pronchery 5*b077aed3SPierre ProncheryPKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex, PKCS5_pbe2_set, PKCS5_pbe2_set_iv, 6*b077aed3SPierre ProncheryPKCS5_pbe2_set_iv_ex, PKCS5_pbe_set, PKCS5_pbe_set_ex, PKCS5_pbe2_set_scrypt, 7*b077aed3SPierre ProncheryPKCS5_pbe_set0_algor, PKCS5_pbe_set0_algor_ex, 8*b077aed3SPierre ProncheryPKCS5_v2_PBE_keyivgen, PKCS5_v2_PBE_keyivgen_ex, 9*b077aed3SPierre ProncheryPKCS5_v2_scrypt_keyivgen, PKCS5_v2_scrypt_keyivgen_ex, 10*b077aed3SPierre ProncheryPKCS5_pbkdf2_set, PKCS5_pbkdf2_set_ex, EVP_PBE_scrypt, EVP_PBE_scrypt_ex 11*b077aed3SPierre Pronchery- PKCS#5 Password based encryption routines 12*b077aed3SPierre Pronchery 13*b077aed3SPierre Pronchery=head1 SYNOPSIS 14*b077aed3SPierre Pronchery 15*b077aed3SPierre Pronchery #include <openssl/evp.h> 16*b077aed3SPierre Pronchery 17*b077aed3SPierre Pronchery int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, 18*b077aed3SPierre Pronchery ASN1_TYPE *param, const EVP_CIPHER *cipher, 19*b077aed3SPierre Pronchery const EVP_MD *md, int en_de); 20*b077aed3SPierre Pronchery int PKCS5_PBE_keyivgen_ex(EVP_CIPHER_CTX *cctx, const char *pass, int passlen, 21*b077aed3SPierre Pronchery ASN1_TYPE *param, const EVP_CIPHER *cipher, 22*b077aed3SPierre Pronchery const EVP_MD *md, int en_de, OSSL_LIB_CTX *libctx, 23*b077aed3SPierre Pronchery const char *propq); 24*b077aed3SPierre Pronchery int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, 25*b077aed3SPierre Pronchery ASN1_TYPE *param, const EVP_CIPHER *cipher, 26*b077aed3SPierre Pronchery const EVP_MD *md, int en_de); 27*b077aed3SPierre Pronchery int PKCS5_v2_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, 28*b077aed3SPierre Pronchery ASN1_TYPE *param, const EVP_CIPHER *cipher, 29*b077aed3SPierre Pronchery const EVP_MD *md, int en_de, 30*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx, const char *propq); 31*b077aed3SPierre Pronchery int EVP_PBE_scrypt(const char *pass, size_t passlen, 32*b077aed3SPierre Pronchery const unsigned char *salt, size_t saltlen, 33*b077aed3SPierre Pronchery uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem, 34*b077aed3SPierre Pronchery unsigned char *key, size_t keylen); 35*b077aed3SPierre Pronchery int EVP_PBE_scrypt_ex(const char *pass, size_t passlen, 36*b077aed3SPierre Pronchery const unsigned char *salt, size_t saltlen, 37*b077aed3SPierre Pronchery uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem, 38*b077aed3SPierre Pronchery unsigned char *key, size_t keylen, 39*b077aed3SPierre Pronchery OSSL_LIB_CTX *ctx, const char *propq); 40*b077aed3SPierre Pronchery int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, 41*b077aed3SPierre Pronchery int passlen, ASN1_TYPE *param, 42*b077aed3SPierre Pronchery const EVP_CIPHER *c, const EVP_MD *md, int en_de); 43*b077aed3SPierre Pronchery int PKCS5_v2_scrypt_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, 44*b077aed3SPierre Pronchery int passlen, ASN1_TYPE *param, 45*b077aed3SPierre Pronchery const EVP_CIPHER *c, const EVP_MD *md, int en_de, 46*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx, const char *propq); 47*b077aed3SPierre Pronchery 48*b077aed3SPierre Pronchery #include <openssl/x509.h> 49*b077aed3SPierre Pronchery 50*b077aed3SPierre Pronchery int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter, 51*b077aed3SPierre Pronchery const unsigned char *salt, int saltlen); 52*b077aed3SPierre Pronchery int PKCS5_pbe_set0_algor_ex(X509_ALGOR *algor, int alg, int iter, 53*b077aed3SPierre Pronchery const unsigned char *salt, int saltlen, 54*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx); 55*b077aed3SPierre Pronchery 56*b077aed3SPierre Pronchery X509_ALGOR *PKCS5_pbe_set(int alg, int iter, 57*b077aed3SPierre Pronchery const unsigned char *salt, int saltlen); 58*b077aed3SPierre Pronchery X509_ALGOR *PKCS5_pbe_set_ex(int alg, int iter, 59*b077aed3SPierre Pronchery const unsigned char *salt, int saltlen, 60*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx); 61*b077aed3SPierre Pronchery 62*b077aed3SPierre Pronchery X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, 63*b077aed3SPierre Pronchery unsigned char *salt, int saltlen); 64*b077aed3SPierre Pronchery X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, 65*b077aed3SPierre Pronchery unsigned char *salt, int saltlen, 66*b077aed3SPierre Pronchery unsigned char *aiv, int prf_nid); 67*b077aed3SPierre Pronchery X509_ALGOR *PKCS5_pbe2_set_iv_ex(const EVP_CIPHER *cipher, int iter, 68*b077aed3SPierre Pronchery unsigned char *salt, int saltlen, 69*b077aed3SPierre Pronchery unsigned char *aiv, int prf_nid, 70*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx); 71*b077aed3SPierre Pronchery X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher, 72*b077aed3SPierre Pronchery const unsigned char *salt, int saltlen, 73*b077aed3SPierre Pronchery unsigned char *aiv, uint64_t N, uint64_t r, 74*b077aed3SPierre Pronchery uint64_t p); 75*b077aed3SPierre Pronchery 76*b077aed3SPierre Pronchery X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, 77*b077aed3SPierre Pronchery int prf_nid, int keylen); 78*b077aed3SPierre Pronchery X509_ALGOR *PKCS5_pbkdf2_set_ex(int iter, unsigned char *salt, int saltlen, 79*b077aed3SPierre Pronchery int prf_nid, int keylen, 80*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx); 81*b077aed3SPierre Pronchery 82*b077aed3SPierre Pronchery=head1 DESCRIPTION 83*b077aed3SPierre Pronchery 84*b077aed3SPierre Pronchery=head2 Key Derivation 85*b077aed3SPierre Pronchery 86*b077aed3SPierre ProncheryPKCS5_PBE_keyivgen() and PKCS5_PBE_keyivgen_ex() take a password I<pass> of 87*b077aed3SPierre Proncherylength I<passlen>, parameters I<param> and a message digest function I<md_type> 88*b077aed3SPierre Proncheryand performs a key derivation according to PKCS#5 PBES1. The resulting key is 89*b077aed3SPierre Proncherythen used to initialise the cipher context I<ctx> with a cipher I<cipher> for 90*b077aed3SPierre Proncheryencryption (I<en_de>=1) or decryption (I<en_de>=0). 91*b077aed3SPierre Pronchery 92*b077aed3SPierre ProncheryI<pass> is an optional parameter and can be NULL. If I<passlen> is -1, then the 93*b077aed3SPierre Proncheryfunction will calculate the length of I<pass> using strlen(). 94*b077aed3SPierre Pronchery 95*b077aed3SPierre ProncheryPKCS5_v2_PBE_keyivgen() and PKCS5_v2_PBE_keyivgen_ex() are similar to the above 96*b077aed3SPierre Proncherybut instead use PKCS#5 PBES2 as the encryption algorithm using the supplied 97*b077aed3SPierre Proncheryparameters. 98*b077aed3SPierre Pronchery 99*b077aed3SPierre ProncheryPKCS5_v2_scrypt_keyivgen() and PKCS5_v2_scrypt_keyivgen_ex() use SCRYPT as the 100*b077aed3SPierre Proncherykey derivation part of the encryption algorithm. 101*b077aed3SPierre Pronchery 102*b077aed3SPierre ProncheryI<salt> is the salt used in the derivation of length I<saltlen>. If the 103*b077aed3SPierre ProncheryI<salt> is NULL, then I<saltlen> must be 0. The function will not 104*b077aed3SPierre Proncheryattempt to calculate the length of the I<salt> because it is not assumed to 105*b077aed3SPierre Proncherybe NULL terminated. 106*b077aed3SPierre Pronchery 107*b077aed3SPierre ProncheryI<iter> is the iteration count and its value should be greater than or 108*b077aed3SPierre Proncheryequal to 1. RFC 2898 suggests an iteration count of at least 1000. Any 109*b077aed3SPierre ProncheryI<iter> less than 1 is treated as a single iteration. 110*b077aed3SPierre Pronchery 111*b077aed3SPierre ProncheryI<digest> is the message digest function used in the derivation. 112*b077aed3SPierre Pronchery 113*b077aed3SPierre ProncheryFunctions ending in _ex() take optional parameters I<libctx> and I<propq> which 114*b077aed3SPierre Proncheryare used to select appropriate algorithm implementations. 115*b077aed3SPierre Pronchery 116*b077aed3SPierre Pronchery=head2 Algorithm Identifier Creation 117*b077aed3SPierre Pronchery 118*b077aed3SPierre ProncheryPKCS5_pbe_set(), PKCS5_pbe_set_ex(), PKCS5_pbe2_set(), PKCS5_pbe2_set_iv(), 119*b077aed3SPierre ProncheryPKCS5_pbe2_set_iv_ex() and PKCS5_pbe2_set_scrypt() generate an B<X509_ALGOR> 120*b077aed3SPierre Proncheryobject which represents an AlgorithmIdentifier containing the algorithm OID and 121*b077aed3SPierre Proncheryassociated parameters for the PBE algorithm. 122*b077aed3SPierre Pronchery 123*b077aed3SPierre ProncheryPKCS5_pbkdf2_set() and PKCS5_pbkdf2_set_ex() generate an B<X509_ALGOR> 124*b077aed3SPierre Proncheryobject which represents an AlgorithmIdentifier containing the algorithm OID and 125*b077aed3SPierre Proncheryassociated parameters for the PBKDF2 algorithm. 126*b077aed3SPierre Pronchery 127*b077aed3SPierre ProncheryPKCS5_pbe_set0_algor() and PKCS5_pbe_set0_algor_ex() set the PBE algorithm OID and 128*b077aed3SPierre Proncheryparameters into the supplied B<X509_ALGOR>. 129*b077aed3SPierre Pronchery 130*b077aed3SPierre Pronchery=head1 NOTES 131*b077aed3SPierre Pronchery 132*b077aed3SPierre ProncheryThe *_keyivgen() functions are typically used in PKCS#12 to encrypt objects. 133*b077aed3SPierre Pronchery 134*b077aed3SPierre ProncheryThese functions make no assumption regarding the given password. 135*b077aed3SPierre ProncheryIt will simply be treated as a byte sequence. 136*b077aed3SPierre Pronchery 137*b077aed3SPierre Pronchery=head1 RETURN VALUES 138*b077aed3SPierre Pronchery 139*b077aed3SPierre ProncheryPKCS5_PBE_keyivgen(), PKCS5_v2_PBE_keyivgen(), 140*b077aed3SPierre ProncheryPKCS5_v2_PBE_keyivgen_ex(), PKCS5_v2_scrypt_keyivgen(), 141*b077aed3SPierre ProncheryPKCS5_v2_scrypt_keyivgen_ex(), PKCS5_pbe_set0_algor() and 142*b077aed3SPierre ProncheryPKCS5_pbe_set0_algor_ex() return 1 for success and 0 if an error occurs. 143*b077aed3SPierre Pronchery 144*b077aed3SPierre ProncheryPKCS5_pbe_set(), PKCS5_pbe_set_ex(), PKCS5_pbe2_set(), PKCS5_pbe2_set_iv(), 145*b077aed3SPierre ProncheryPKCS5_pbe2_set_iv_ex(), PKCS5_pbe2_set_scrypt(), 146*b077aed3SPierre ProncheryPKCS5_pbkdf2_set() and PKCS5_pbkdf2_set_ex() return an B<X509_ALGOR> object or 147*b077aed3SPierre ProncheryNULL if an error occurs. 148*b077aed3SPierre Pronchery 149*b077aed3SPierre Pronchery=head1 CONFORMING TO 150*b077aed3SPierre Pronchery 151*b077aed3SPierre ProncheryIETF RFC 8018 (L<https://tools.ietf.org/html/rfc8018>) 152*b077aed3SPierre Pronchery 153*b077aed3SPierre Pronchery=head1 SEE ALSO 154*b077aed3SPierre Pronchery 155*b077aed3SPierre ProncheryL<EVP_PBE_CipherInit_ex(3)>, 156*b077aed3SPierre ProncheryL<PKCS12_pbe_crypt_ex(3)>, 157*b077aed3SPierre ProncheryL<passphrase-encoding(7)> 158*b077aed3SPierre Pronchery 159*b077aed3SPierre Pronchery=head1 HISTORY 160*b077aed3SPierre Pronchery 161*b077aed3SPierre ProncheryPKCS5_v2_PBE_keyivgen_ex(), EVP_PBE_scrypt_ex(), PKCS5_v2_scrypt_keyivgen_ex(), 162*b077aed3SPierre ProncheryPKCS5_pbe_set0_algor_ex(), PKCS5_pbe_set_ex(), PKCS5_pbe2_set_iv_ex() and 163*b077aed3SPierre ProncheryPKCS5_pbkdf2_set_ex() were added in OpenSSL 3.0. 164*b077aed3SPierre Pronchery 165*b077aed3SPierre ProncheryFrom OpenSSL 3.0 the PBKDF1 algorithm used in PKCS5_PBE_keyivgen() and 166*b077aed3SPierre ProncheryPKCS5_PBE_keyivgen_ex() has been moved to the legacy provider as an EVP_KDF. 167*b077aed3SPierre Pronchery 168*b077aed3SPierre Pronchery=head1 COPYRIGHT 169*b077aed3SPierre Pronchery 170*b077aed3SPierre ProncheryCopyright 2021 The OpenSSL Project Authors. All Rights Reserved. 171*b077aed3SPierre Pronchery 172*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 173*b077aed3SPierre Proncherythis file except in compliance with the License. You can obtain a copy 174*b077aed3SPierre Proncheryin the file LICENSE in the source distribution or at 175*b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>. 176*b077aed3SPierre Pronchery 177*b077aed3SPierre Pronchery=cut 178