1*e71b7053SJung-uk Kim=pod 2*e71b7053SJung-uk Kim 3*e71b7053SJung-uk Kim=head1 NAME 4*e71b7053SJung-uk Kim 5*e71b7053SJung-uk KimEVP_BytesToKey - password based encryption routine 6*e71b7053SJung-uk Kim 7*e71b7053SJung-uk Kim=head1 SYNOPSIS 8*e71b7053SJung-uk Kim 9*e71b7053SJung-uk Kim #include <openssl/evp.h> 10*e71b7053SJung-uk Kim 11*e71b7053SJung-uk Kim int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, 12*e71b7053SJung-uk Kim const unsigned char *salt, 13*e71b7053SJung-uk Kim const unsigned char *data, int datal, int count, 14*e71b7053SJung-uk Kim unsigned char *key, unsigned char *iv); 15*e71b7053SJung-uk Kim 16*e71b7053SJung-uk Kim=head1 DESCRIPTION 17*e71b7053SJung-uk Kim 18*e71b7053SJung-uk KimEVP_BytesToKey() derives a key and IV from various parameters. B<type> is 19*e71b7053SJung-uk Kimthe cipher to derive the key and IV for. B<md> is the message digest to use. 20*e71b7053SJung-uk KimThe B<salt> parameter is used as a salt in the derivation: it should point to 21*e71b7053SJung-uk Kiman 8 byte buffer or NULL if no salt is used. B<data> is a buffer containing 22*e71b7053SJung-uk KimB<datal> bytes which is used to derive the keying data. B<count> is the 23*e71b7053SJung-uk Kimiteration count to use. The derived key and IV will be written to B<key> 24*e71b7053SJung-uk Kimand B<iv> respectively. 25*e71b7053SJung-uk Kim 26*e71b7053SJung-uk Kim=head1 NOTES 27*e71b7053SJung-uk Kim 28*e71b7053SJung-uk KimA typical application of this function is to derive keying material for an 29*e71b7053SJung-uk Kimencryption algorithm from a password in the B<data> parameter. 30*e71b7053SJung-uk Kim 31*e71b7053SJung-uk KimIncreasing the B<count> parameter slows down the algorithm which makes it 32*e71b7053SJung-uk Kimharder for an attacker to perform a brute force attack using a large number 33*e71b7053SJung-uk Kimof candidate passwords. 34*e71b7053SJung-uk Kim 35*e71b7053SJung-uk KimIf the total key and IV length is less than the digest length and 36*e71b7053SJung-uk KimB<MD5> is used then the derivation algorithm is compatible with PKCS#5 v1.5 37*e71b7053SJung-uk Kimotherwise a non standard extension is used to derive the extra data. 38*e71b7053SJung-uk Kim 39*e71b7053SJung-uk KimNewer applications should use a more modern algorithm such as PBKDF2 as 40*e71b7053SJung-uk Kimdefined in PKCS#5v2.1 and provided by PKCS5_PBKDF2_HMAC. 41*e71b7053SJung-uk Kim 42*e71b7053SJung-uk Kim=head1 KEY DERIVATION ALGORITHM 43*e71b7053SJung-uk Kim 44*e71b7053SJung-uk KimThe key and IV is derived by concatenating D_1, D_2, etc until 45*e71b7053SJung-uk Kimenough data is available for the key and IV. D_i is defined as: 46*e71b7053SJung-uk Kim 47*e71b7053SJung-uk Kim D_i = HASH^count(D_(i-1) || data || salt) 48*e71b7053SJung-uk Kim 49*e71b7053SJung-uk Kimwhere || denotes concatenation, D_0 is empty, HASH is the digest 50*e71b7053SJung-uk Kimalgorithm in use, HASH^1(data) is simply HASH(data), HASH^2(data) 51*e71b7053SJung-uk Kimis HASH(HASH(data)) and so on. 52*e71b7053SJung-uk Kim 53*e71b7053SJung-uk KimThe initial bytes are used for the key and the subsequent bytes for 54*e71b7053SJung-uk Kimthe IV. 55*e71b7053SJung-uk Kim 56*e71b7053SJung-uk Kim=head1 RETURN VALUES 57*e71b7053SJung-uk Kim 58*e71b7053SJung-uk KimIf B<data> is NULL, then EVP_BytesToKey() returns the number of bytes 59*e71b7053SJung-uk Kimneeded to store the derived key. 60*e71b7053SJung-uk KimOtherwise, EVP_BytesToKey() returns the size of the derived key in bytes, 61*e71b7053SJung-uk Kimor 0 on error. 62*e71b7053SJung-uk Kim 63*e71b7053SJung-uk Kim=head1 SEE ALSO 64*e71b7053SJung-uk Kim 65*e71b7053SJung-uk KimL<evp(7)>, L<RAND_bytes(3)>, 66*e71b7053SJung-uk KimL<PKCS5_PBKDF2_HMAC(3)>, 67*e71b7053SJung-uk KimL<EVP_EncryptInit(3)> 68*e71b7053SJung-uk Kim 69*e71b7053SJung-uk Kim=head1 COPYRIGHT 70*e71b7053SJung-uk Kim 71*e71b7053SJung-uk KimCopyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved. 72*e71b7053SJung-uk Kim 73*e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License"). You may not use 74*e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 75*e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 76*e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 77*e71b7053SJung-uk Kim 78*e71b7053SJung-uk Kim=cut 79